v2fly/proxy/vmess/encoding/commands.go

149 lines
3.8 KiB
Go
Raw Normal View History

2016-07-23 11:17:51 +00:00
package encoding
2016-02-26 23:05:53 +00:00
import (
"io"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
2016-08-20 18:55:45 +00:00
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
"v2ray.com/core/common/uuid"
2016-02-26 23:05:53 +00:00
)
var (
2016-06-27 06:53:35 +00:00
ErrCommandTypeMismatch = errors.New("Command type mismatch.")
ErrUnknownCommand = errors.New("Unknown command.")
ErrCommandTooLarge = errors.New("Command too large.")
2016-02-26 23:05:53 +00:00
)
func MarshalCommand(command interface{}, writer io.Writer) error {
2016-02-27 16:28:21 +00:00
if command == nil {
2016-06-27 06:53:35 +00:00
return ErrUnknownCommand
2016-02-27 16:28:21 +00:00
}
2016-02-27 15:41:21 +00:00
var cmdId byte
2016-02-26 23:05:53 +00:00
var factory CommandFactory
switch command.(type) {
case *protocol.CommandSwitchAccount:
factory = new(CommandSwitchAccountFactory)
2016-02-27 15:41:21 +00:00
cmdId = 1
2016-02-26 23:05:53 +00:00
default:
2016-06-27 06:53:35 +00:00
return ErrUnknownCommand
2016-02-26 23:05:53 +00:00
}
2016-02-27 15:41:21 +00:00
2016-12-09 11:08:25 +00:00
buffer := buf.NewLocal(512)
2016-07-16 11:22:18 +00:00
defer buffer.Release()
2016-02-27 15:41:21 +00:00
err := factory.Marshal(command, buffer)
if err != nil {
return err
}
2016-12-05 20:33:24 +00:00
auth := Authenticate(buffer.Bytes())
2016-02-27 15:41:21 +00:00
len := buffer.Len() + 4
if len > 255 {
2016-06-27 06:53:35 +00:00
return ErrCommandTooLarge
2016-02-27 15:41:21 +00:00
}
writer.Write([]byte{cmdId, byte(len), byte(auth >> 24), byte(auth >> 16), byte(auth >> 8), byte(auth)})
2016-12-05 20:33:24 +00:00
writer.Write(buffer.Bytes())
2016-02-27 15:41:21 +00:00
return nil
2016-02-26 23:05:53 +00:00
}
func UnmarshalCommand(cmdId byte, data []byte) (protocol.ResponseCommand, error) {
2016-02-27 15:41:21 +00:00
if len(data) <= 4 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|Command: Insufficient length.")
2016-02-27 15:41:21 +00:00
}
expectedAuth := Authenticate(data[4:])
2016-05-24 20:09:22 +00:00
actualAuth := serial.BytesToUint32(data[:4])
2016-02-27 15:41:21 +00:00
if expectedAuth != actualAuth {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|Command: Invalid auth.")
2016-02-27 15:41:21 +00:00
}
2016-02-26 23:05:53 +00:00
var factory CommandFactory
switch cmdId {
case 1:
factory = new(CommandSwitchAccountFactory)
default:
2016-06-27 06:53:35 +00:00
return nil, ErrUnknownCommand
2016-02-26 23:05:53 +00:00
}
2016-02-27 15:41:21 +00:00
return factory.Unmarshal(data[4:])
2016-02-26 23:05:53 +00:00
}
type CommandFactory interface {
Marshal(command interface{}, writer io.Writer) error
Unmarshal(data []byte) (interface{}, error)
}
type CommandSwitchAccountFactory struct {
}
2016-11-27 20:39:09 +00:00
func (v *CommandSwitchAccountFactory) Marshal(command interface{}, writer io.Writer) error {
2016-02-26 23:05:53 +00:00
cmd, ok := command.(*protocol.CommandSwitchAccount)
if !ok {
2016-06-27 06:53:35 +00:00
return ErrCommandTypeMismatch
2016-02-26 23:05:53 +00:00
}
hostStr := ""
if cmd.Host != nil {
hostStr = cmd.Host.String()
}
writer.Write([]byte{byte(len(hostStr))})
if len(hostStr) > 0 {
writer.Write([]byte(hostStr))
}
2016-06-26 20:34:48 +00:00
writer.Write(cmd.Port.Bytes(nil))
2016-02-26 23:05:53 +00:00
idBytes := cmd.ID.Bytes()
writer.Write(idBytes)
2016-06-26 20:34:48 +00:00
writer.Write(serial.Uint16ToBytes(cmd.AlterIds, nil))
2016-02-26 23:05:53 +00:00
writer.Write([]byte{byte(cmd.Level)})
writer.Write([]byte{cmd.ValidMin})
return nil
}
2016-11-27 20:39:09 +00:00
func (v *CommandSwitchAccountFactory) Unmarshal(data []byte) (interface{}, error) {
2016-02-26 23:05:53 +00:00
cmd := new(protocol.CommandSwitchAccount)
if len(data) == 0 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
2016-02-26 23:05:53 +00:00
}
lenHost := int(data[0])
if len(data) < lenHost+1 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
2016-02-26 23:05:53 +00:00
}
if lenHost > 0 {
cmd.Host = v2net.ParseAddress(string(data[1 : 1+lenHost]))
}
portStart := 1 + lenHost
if len(data) < portStart+2 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
2016-02-26 23:05:53 +00:00
}
cmd.Port = v2net.PortFromBytes(data[portStart : portStart+2])
idStart := portStart + 2
if len(data) < idStart+16 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
2016-02-26 23:05:53 +00:00
}
cmd.ID, _ = uuid.ParseBytes(data[idStart : idStart+16])
alterIdStart := idStart + 16
if len(data) < alterIdStart+2 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
2016-02-26 23:05:53 +00:00
}
2016-05-24 19:55:46 +00:00
cmd.AlterIds = serial.BytesToUint16(data[alterIdStart : alterIdStart+2])
2016-02-26 23:05:53 +00:00
levelStart := alterIdStart + 2
if len(data) < levelStart+1 {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
2016-02-26 23:05:53 +00:00
}
2016-09-17 22:41:21 +00:00
cmd.Level = uint32(data[levelStart])
2016-02-26 23:05:53 +00:00
timeStart := levelStart + 1
if len(data) < timeStart {
2016-11-19 13:38:13 +00:00
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
2016-02-26 23:05:53 +00:00
}
cmd.ValidMin = data[timeStart]
return cmd, nil
}