accounts.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package command
  2. import (
  3. "io"
  4. v2net "github.com/v2ray/v2ray-core/common/net"
  5. "github.com/v2ray/v2ray-core/common/serial"
  6. "github.com/v2ray/v2ray-core/common/uuid"
  7. "github.com/v2ray/v2ray-core/proxy/vmess"
  8. "github.com/v2ray/v2ray-core/transport"
  9. )
  10. func init() {
  11. RegisterResponseCommand(1, func() Command { return new(SwitchAccount) })
  12. }
  13. // Structure
  14. // 1 byte: host len N
  15. // N bytes: host
  16. // 2 bytes: port
  17. // 16 bytes: uuid
  18. // 2 bytes: alterid
  19. // 1 byte: level
  20. // 1 bytes: time
  21. type SwitchAccount struct {
  22. Host v2net.Address
  23. Port v2net.Port
  24. ID *uuid.UUID
  25. AlterIds serial.Uint16Literal
  26. Level vmess.UserLevel
  27. ValidMin byte
  28. }
  29. func (this *SwitchAccount) Marshal(writer io.Writer) {
  30. hostStr := ""
  31. if this.Host != nil {
  32. hostStr = this.Host.String()
  33. }
  34. writer.Write([]byte{byte(len(hostStr))})
  35. if len(hostStr) > 0 {
  36. writer.Write([]byte(hostStr))
  37. }
  38. writer.Write(this.Port.Bytes())
  39. idBytes := this.ID.Bytes()
  40. writer.Write(idBytes)
  41. writer.Write(this.AlterIds.Bytes())
  42. writer.Write([]byte{byte(this.Level)})
  43. writer.Write([]byte{this.ValidMin})
  44. }
  45. func (this *SwitchAccount) Unmarshal(data []byte) error {
  46. lenHost := int(data[0])
  47. if len(data) < lenHost+1 {
  48. return transport.CorruptedPacket
  49. }
  50. if lenHost > 0 {
  51. this.Host = v2net.ParseAddress(string(data[1 : 1+lenHost]))
  52. }
  53. portStart := 1 + lenHost
  54. if len(data) < portStart+2 {
  55. return transport.CorruptedPacket
  56. }
  57. this.Port = v2net.PortFromBytes(data[portStart : portStart+2])
  58. idStart := portStart + 2
  59. if len(data) < idStart+16 {
  60. return transport.CorruptedPacket
  61. }
  62. this.ID, _ = uuid.ParseBytes(data[idStart : idStart+16])
  63. alterIdStart := idStart + 16
  64. if len(data) < alterIdStart+2 {
  65. return transport.CorruptedPacket
  66. }
  67. this.AlterIds = serial.ParseUint16(data[alterIdStart : alterIdStart+2])
  68. levelStart := alterIdStart + 2
  69. if len(data) < levelStart {
  70. return transport.CorruptedPacket
  71. }
  72. this.Level = vmess.UserLevel(data[levelStart])
  73. timeStart := levelStart + 1
  74. if len(data) < timeStart {
  75. return transport.CorruptedPacket
  76. }
  77. this.ValidMin = data[timeStart]
  78. return nil
  79. }