1
0

commands.go 3.6 KB

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