commands.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package encoding
  2. import (
  3. "io"
  4. "v2ray.com/core/common/buf"
  5. "v2ray.com/core/common/net"
  6. "v2ray.com/core/common/protocol"
  7. "v2ray.com/core/common/serial"
  8. "v2ray.com/core/common/uuid"
  9. )
  10. var (
  11. ErrCommandTypeMismatch = newError("Command type mismatch.")
  12. ErrUnknownCommand = newError("Unknown command.")
  13. ErrCommandTooLarge = newError("Command too large.")
  14. )
  15. func MarshalCommand(command interface{}, writer io.Writer) error {
  16. if command == nil {
  17. return ErrUnknownCommand
  18. }
  19. var cmdID byte
  20. var factory CommandFactory
  21. switch command.(type) {
  22. case *protocol.CommandSwitchAccount:
  23. factory = new(CommandSwitchAccountFactory)
  24. cmdID = 1
  25. default:
  26. return ErrUnknownCommand
  27. }
  28. buffer := buf.NewLocal(512)
  29. defer buffer.Release()
  30. err := factory.Marshal(command, buffer)
  31. if err != nil {
  32. return err
  33. }
  34. auth := Authenticate(buffer.Bytes())
  35. len := buffer.Len() + 4
  36. if len > 255 {
  37. return ErrCommandTooLarge
  38. }
  39. writer.Write([]byte{cmdID, byte(len), byte(auth >> 24), byte(auth >> 16), byte(auth >> 8), byte(auth)})
  40. writer.Write(buffer.Bytes())
  41. return nil
  42. }
  43. func UnmarshalCommand(cmdID byte, data []byte) (protocol.ResponseCommand, error) {
  44. if len(data) <= 4 {
  45. return nil, newError("insufficient length")
  46. }
  47. expectedAuth := Authenticate(data[4:])
  48. actualAuth := serial.BytesToUint32(data[:4])
  49. if expectedAuth != actualAuth {
  50. return nil, newError("invalid auth")
  51. }
  52. var factory CommandFactory
  53. switch cmdID {
  54. case 1:
  55. factory = new(CommandSwitchAccountFactory)
  56. default:
  57. return nil, ErrUnknownCommand
  58. }
  59. return factory.Unmarshal(data[4:])
  60. }
  61. type CommandFactory interface {
  62. Marshal(command interface{}, writer io.Writer) error
  63. Unmarshal(data []byte) (interface{}, error)
  64. }
  65. type CommandSwitchAccountFactory struct {
  66. }
  67. func (v *CommandSwitchAccountFactory) Marshal(command interface{}, writer io.Writer) error {
  68. cmd, ok := command.(*protocol.CommandSwitchAccount)
  69. if !ok {
  70. return ErrCommandTypeMismatch
  71. }
  72. hostStr := ""
  73. if cmd.Host != nil {
  74. hostStr = cmd.Host.String()
  75. }
  76. writer.Write([]byte{byte(len(hostStr))})
  77. if len(hostStr) > 0 {
  78. writer.Write([]byte(hostStr))
  79. }
  80. writer.Write(cmd.Port.Bytes(nil))
  81. idBytes := cmd.ID.Bytes()
  82. writer.Write(idBytes)
  83. writer.Write(serial.Uint16ToBytes(cmd.AlterIds, nil))
  84. writer.Write([]byte{byte(cmd.Level)})
  85. writer.Write([]byte{cmd.ValidMin})
  86. return nil
  87. }
  88. func (v *CommandSwitchAccountFactory) Unmarshal(data []byte) (interface{}, error) {
  89. cmd := new(protocol.CommandSwitchAccount)
  90. if len(data) == 0 {
  91. return nil, newError("insufficient length.")
  92. }
  93. lenHost := int(data[0])
  94. if len(data) < lenHost+1 {
  95. return nil, newError("insufficient length.")
  96. }
  97. if lenHost > 0 {
  98. cmd.Host = net.ParseAddress(string(data[1 : 1+lenHost]))
  99. }
  100. portStart := 1 + lenHost
  101. if len(data) < portStart+2 {
  102. return nil, newError("insufficient length.")
  103. }
  104. cmd.Port = net.PortFromBytes(data[portStart : portStart+2])
  105. idStart := portStart + 2
  106. if len(data) < idStart+16 {
  107. return nil, newError("insufficient length.")
  108. }
  109. cmd.ID, _ = uuid.ParseBytes(data[idStart : idStart+16])
  110. alterIDStart := idStart + 16
  111. if len(data) < alterIDStart+2 {
  112. return nil, newError("insufficient length.")
  113. }
  114. cmd.AlterIds = serial.BytesToUint16(data[alterIDStart : alterIDStart+2])
  115. levelStart := alterIDStart + 2
  116. if len(data) < levelStart+1 {
  117. return nil, newError("insufficient length.")
  118. }
  119. cmd.Level = uint32(data[levelStart])
  120. timeStart := levelStart + 1
  121. if len(data) < timeStart {
  122. return nil, newError("insufficient length.")
  123. }
  124. cmd.ValidMin = data[timeStart]
  125. return cmd, nil
  126. }