accounts.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/transport"
  8. )
  9. func init() {
  10. RegisterResponseCommand(1, func() Command { return new(SwitchAccount) })
  11. }
  12. // Structure
  13. // 1 byte: host len N
  14. // N bytes: host
  15. // 2 bytes: port
  16. // 16 bytes: uuid
  17. // 2 bytes: alterid
  18. // 8 bytes: time
  19. type SwitchAccount struct {
  20. Host v2net.Address
  21. Port v2net.Port
  22. ID *uuid.UUID
  23. AlterIds serial.Uint16Literal
  24. ValidSec serial.Uint16Literal
  25. }
  26. func (this *SwitchAccount) Marshal(writer io.Writer) {
  27. hostStr := ""
  28. if this.Host != nil {
  29. hostStr = this.Host.String()
  30. }
  31. writer.Write([]byte{byte(len(hostStr))})
  32. if len(hostStr) > 0 {
  33. writer.Write([]byte(hostStr))
  34. }
  35. writer.Write(this.Port.Bytes())
  36. idBytes := this.ID.Bytes()
  37. writer.Write(idBytes)
  38. writer.Write(this.AlterIds.Bytes())
  39. timeBytes := this.ValidSec.Bytes()
  40. writer.Write(timeBytes)
  41. }
  42. func (this *SwitchAccount) Unmarshal(data []byte) error {
  43. lenHost := int(data[0])
  44. if len(data) < lenHost+1 {
  45. return transport.CorruptedPacket
  46. }
  47. this.Host = v2net.ParseAddress(string(data[1 : 1+lenHost]))
  48. portStart := 1 + lenHost
  49. if len(data) < portStart+2 {
  50. return transport.CorruptedPacket
  51. }
  52. this.Port = v2net.PortFromBytes(data[portStart : portStart+2])
  53. idStart := portStart + 2
  54. if len(data) < idStart+16 {
  55. return transport.CorruptedPacket
  56. }
  57. this.ID, _ = uuid.ParseBytes(data[idStart : idStart+16])
  58. alterIdStart := idStart + 16
  59. if len(data) < alterIdStart+2 {
  60. return transport.CorruptedPacket
  61. }
  62. this.AlterIds = serial.ParseUint16(data[alterIdStart : alterIdStart+2])
  63. timeStart := alterIdStart + 2
  64. if len(data) < timeStart+2 {
  65. return transport.CorruptedPacket
  66. }
  67. this.ValidSec = serial.ParseUint16(data[timeStart : timeStart+2])
  68. return nil
  69. }