commands.go 3.7 KB

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