accounts.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. this.Host = v2net.ParseAddress(string(data[1 : 1+lenHost]))
  51. portStart := 1 + lenHost
  52. if len(data) < portStart+2 {
  53. return transport.CorruptedPacket
  54. }
  55. this.Port = v2net.PortFromBytes(data[portStart : portStart+2])
  56. idStart := portStart + 2
  57. if len(data) < idStart+16 {
  58. return transport.CorruptedPacket
  59. }
  60. this.ID, _ = uuid.ParseBytes(data[idStart : idStart+16])
  61. alterIdStart := idStart + 16
  62. if len(data) < alterIdStart+2 {
  63. return transport.CorruptedPacket
  64. }
  65. this.AlterIds = serial.ParseUint16(data[alterIdStart : alterIdStart+2])
  66. levelStart := alterIdStart + 2
  67. if len(data) < levelStart {
  68. return transport.CorruptedPacket
  69. }
  70. this.Level = vmess.UserLevel(data[levelStart])
  71. timeStart := levelStart + 1
  72. if len(data) < timeStart {
  73. return transport.CorruptedPacket
  74. }
  75. this.ValidMin = data[timeStart]
  76. return nil
  77. }