accounts.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package command
  2. import (
  3. "io"
  4. v2net "github.com/v2ray/v2ray-core/common/net"
  5. proto "github.com/v2ray/v2ray-core/common/protocol"
  6. "github.com/v2ray/v2ray-core/common/serial"
  7. "github.com/v2ray/v2ray-core/common/uuid"
  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 proto.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. if len(data) == 0 {
  47. return transport.ErrorCorruptedPacket
  48. }
  49. lenHost := int(data[0])
  50. if len(data) < lenHost+1 {
  51. return transport.ErrorCorruptedPacket
  52. }
  53. if lenHost > 0 {
  54. this.Host = v2net.ParseAddress(string(data[1 : 1+lenHost]))
  55. }
  56. portStart := 1 + lenHost
  57. if len(data) < portStart+2 {
  58. return transport.ErrorCorruptedPacket
  59. }
  60. this.Port = v2net.PortFromBytes(data[portStart : portStart+2])
  61. idStart := portStart + 2
  62. if len(data) < idStart+16 {
  63. return transport.ErrorCorruptedPacket
  64. }
  65. this.ID, _ = uuid.ParseBytes(data[idStart : idStart+16])
  66. alterIdStart := idStart + 16
  67. if len(data) < alterIdStart+2 {
  68. return transport.ErrorCorruptedPacket
  69. }
  70. this.AlterIds = serial.BytesLiteral(data[alterIdStart : alterIdStart+2]).Uint16()
  71. levelStart := alterIdStart + 2
  72. if len(data) < levelStart+1 {
  73. return transport.ErrorCorruptedPacket
  74. }
  75. this.Level = proto.UserLevel(data[levelStart])
  76. timeStart := levelStart + 1
  77. if len(data) < timeStart {
  78. return transport.ErrorCorruptedPacket
  79. }
  80. this.ValidMin = data[timeStart]
  81. return nil
  82. }