commands.go 3.7 KB

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