1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00
v2fly/proxy/vmess/encoding/commands.go

150 lines
3.8 KiB
Go
Raw Normal View History

2016-07-23 07:17:51 -04:00
package encoding
2016-02-26 18:05:53 -05:00
import (
2018-11-02 10:47:58 -04:00
"encoding/binary"
2016-02-26 18:05:53 -05:00
"io"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/protocol"
"github.com/v2fly/v2ray-core/v5/common/serial"
"github.com/v2fly/v2ray-core/v5/common/uuid"
2016-02-26 18:05:53 -05:00
)
var (
2017-04-08 19:43:25 -04:00
ErrCommandTypeMismatch = newError("Command type mismatch.")
ErrUnknownCommand = newError("Unknown command.")
ErrCommandTooLarge = newError("Command too large.")
2021-12-27 01:28:11 -05:00
ErrInsufficientLength = newError("Insufficient length.")
ErrInvalidAuth = newError("Invalid auth.")
2016-02-26 18:05:53 -05:00
)
func MarshalCommand(command interface{}, writer io.Writer) error {
2016-02-27 11:28:21 -05:00
if command == nil {
2016-06-27 02:53:35 -04:00
return ErrUnknownCommand
2016-02-27 11:28:21 -05:00
}
2016-12-13 05:15:11 -05:00
var cmdID byte
2016-02-26 18:05:53 -05:00
var factory CommandFactory
switch command.(type) {
case *protocol.CommandSwitchAccount:
factory = new(CommandSwitchAccountFactory)
2016-12-13 05:15:11 -05:00
cmdID = 1
2016-02-26 18:05:53 -05:00
default:
2016-06-27 02:53:35 -04:00
return ErrUnknownCommand
2016-02-26 18:05:53 -05:00
}
2016-02-27 10:41:21 -05:00
2018-03-09 05:26:00 -05:00
buffer := buf.New()
2016-07-16 07:22:18 -04:00
defer buffer.Release()
2016-02-27 10:41:21 -05:00
err := factory.Marshal(command, buffer)
if err != nil {
return err
}
2016-12-05 15:33:24 -05:00
auth := Authenticate(buffer.Bytes())
length := buffer.Len() + 4
if length > 255 {
2016-06-27 02:53:35 -04:00
return ErrCommandTooLarge
2016-02-27 10:41:21 -05:00
}
common.Must2(writer.Write([]byte{cmdID, byte(length), byte(auth >> 24), byte(auth >> 16), byte(auth >> 8), byte(auth)}))
2017-09-19 17:27:49 -04:00
common.Must2(writer.Write(buffer.Bytes()))
2016-02-27 10:41:21 -05:00
return nil
2016-02-26 18:05:53 -05:00
}
2016-12-13 05:15:11 -05:00
func UnmarshalCommand(cmdID byte, data []byte) (protocol.ResponseCommand, error) {
2016-02-27 10:41:21 -05:00
if len(data) <= 4 {
2021-12-27 01:28:11 -05:00
return nil, ErrInsufficientLength
2016-02-27 10:41:21 -05:00
}
expectedAuth := Authenticate(data[4:])
2018-11-02 10:47:58 -04:00
actualAuth := binary.BigEndian.Uint32(data[:4])
2016-02-27 10:41:21 -05:00
if expectedAuth != actualAuth {
2021-12-27 01:28:11 -05:00
return nil, ErrInvalidAuth
2016-02-27 10:41:21 -05:00
}
2016-02-26 18:05:53 -05:00
var factory CommandFactory
2016-12-13 05:15:11 -05:00
switch cmdID {
2016-02-26 18:05:53 -05:00
case 1:
factory = new(CommandSwitchAccountFactory)
default:
2016-06-27 02:53:35 -04:00
return nil, ErrUnknownCommand
2016-02-26 18:05:53 -05:00
}
2016-02-27 10:41:21 -05:00
return factory.Unmarshal(data[4:])
2016-02-26 18:05:53 -05:00
}
type CommandFactory interface {
Marshal(command interface{}, writer io.Writer) error
Unmarshal(data []byte) (interface{}, error)
}
2021-05-19 17:28:52 -04:00
type CommandSwitchAccountFactory struct{}
2016-02-26 18:05:53 -05:00
2017-09-19 17:27:49 -04:00
func (f *CommandSwitchAccountFactory) Marshal(command interface{}, writer io.Writer) error {
2016-02-26 18:05:53 -05:00
cmd, ok := command.(*protocol.CommandSwitchAccount)
if !ok {
2016-06-27 02:53:35 -04:00
return ErrCommandTypeMismatch
2016-02-26 18:05:53 -05:00
}
hostStr := ""
if cmd.Host != nil {
hostStr = cmd.Host.String()
}
2017-09-19 17:27:49 -04:00
common.Must2(writer.Write([]byte{byte(len(hostStr))}))
2016-02-26 18:05:53 -05:00
if len(hostStr) > 0 {
2017-09-19 17:27:49 -04:00
common.Must2(writer.Write([]byte(hostStr)))
2016-02-26 18:05:53 -05:00
}
2018-11-03 08:03:02 -04:00
common.Must2(serial.WriteUint16(writer, cmd.Port.Value()))
2016-02-26 18:05:53 -05:00
idBytes := cmd.ID.Bytes()
2017-09-19 17:27:49 -04:00
common.Must2(writer.Write(idBytes))
2018-11-03 08:03:02 -04:00
common.Must2(serial.WriteUint16(writer, cmd.AlterIds))
2017-09-19 17:27:49 -04:00
common.Must2(writer.Write([]byte{byte(cmd.Level)}))
2016-02-26 18:05:53 -05:00
2017-09-19 17:27:49 -04:00
common.Must2(writer.Write([]byte{cmd.ValidMin}))
2016-02-26 18:05:53 -05:00
return nil
}
2017-09-19 17:27:49 -04:00
func (f *CommandSwitchAccountFactory) Unmarshal(data []byte) (interface{}, error) {
2016-02-26 18:05:53 -05:00
cmd := new(protocol.CommandSwitchAccount)
if len(data) == 0 {
2021-12-27 01:28:11 -05:00
return nil, ErrInsufficientLength
2016-02-26 18:05:53 -05:00
}
lenHost := int(data[0])
if len(data) < lenHost+1 {
2021-12-27 01:28:11 -05:00
return nil, ErrInsufficientLength
2016-02-26 18:05:53 -05:00
}
if lenHost > 0 {
2017-01-13 17:42:39 -05:00
cmd.Host = net.ParseAddress(string(data[1 : 1+lenHost]))
2016-02-26 18:05:53 -05:00
}
portStart := 1 + lenHost
if len(data) < portStart+2 {
2021-12-27 01:28:11 -05:00
return nil, ErrInsufficientLength
2016-02-26 18:05:53 -05:00
}
2017-01-13 17:42:39 -05:00
cmd.Port = net.PortFromBytes(data[portStart : portStart+2])
2016-02-26 18:05:53 -05:00
idStart := portStart + 2
if len(data) < idStart+16 {
2021-12-27 01:28:11 -05:00
return nil, ErrInsufficientLength
2016-02-26 18:05:53 -05:00
}
cmd.ID, _ = uuid.ParseBytes(data[idStart : idStart+16])
2016-12-13 05:15:11 -05:00
alterIDStart := idStart + 16
if len(data) < alterIDStart+2 {
2021-12-27 01:28:11 -05:00
return nil, ErrInsufficientLength
2016-02-26 18:05:53 -05:00
}
2018-11-02 10:47:58 -04:00
cmd.AlterIds = binary.BigEndian.Uint16(data[alterIDStart : alterIDStart+2])
2016-12-13 05:15:11 -05:00
levelStart := alterIDStart + 2
2016-02-26 18:05:53 -05:00
if len(data) < levelStart+1 {
2021-12-27 01:28:11 -05:00
return nil, ErrInsufficientLength
2016-02-26 18:05:53 -05:00
}
2016-09-17 18:41:21 -04:00
cmd.Level = uint32(data[levelStart])
2016-02-26 18:05:53 -05:00
timeStart := levelStart + 1
if len(data) < timeStart+1 {
2021-12-27 01:28:11 -05:00
return nil, ErrInsufficientLength
2016-02-26 18:05:53 -05:00
}
cmd.ValidMin = data[timeStart]
return cmd, nil
}