2015-12-12 07:11:49 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2016-01-18 19:21:07 -05:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2015-12-12 07:11:49 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/serial"
|
|
|
|
"github.com/v2ray/v2ray-core/common/uuid"
|
2016-01-20 11:31:43 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
2015-12-12 07:11:49 -05:00
|
|
|
"github.com/v2ray/v2ray-core/transport"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterResponseCommand(1, func() Command { return new(SwitchAccount) })
|
|
|
|
}
|
|
|
|
|
2016-01-18 19:21:07 -05:00
|
|
|
// Structure
|
|
|
|
// 1 byte: host len N
|
|
|
|
// N bytes: host
|
|
|
|
// 2 bytes: port
|
|
|
|
// 16 bytes: uuid
|
|
|
|
// 2 bytes: alterid
|
2016-01-20 11:31:43 -05:00
|
|
|
// 1 byte: level
|
|
|
|
// 1 bytes: time
|
2015-12-12 07:11:49 -05:00
|
|
|
type SwitchAccount struct {
|
2016-01-19 17:41:40 -05:00
|
|
|
Host v2net.Address
|
|
|
|
Port v2net.Port
|
|
|
|
ID *uuid.UUID
|
|
|
|
AlterIds serial.Uint16Literal
|
2016-01-20 11:31:43 -05:00
|
|
|
Level vmess.UserLevel
|
|
|
|
ValidMin byte
|
2015-12-12 07:11:49 -05:00
|
|
|
}
|
|
|
|
|
2016-01-19 17:41:40 -05:00
|
|
|
func (this *SwitchAccount) Marshal(writer io.Writer) {
|
2016-01-18 19:21:07 -05:00
|
|
|
hostStr := ""
|
|
|
|
if this.Host != nil {
|
|
|
|
hostStr = this.Host.String()
|
|
|
|
}
|
|
|
|
writer.Write([]byte{byte(len(hostStr))})
|
|
|
|
|
|
|
|
if len(hostStr) > 0 {
|
|
|
|
writer.Write([]byte(hostStr))
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Write(this.Port.Bytes())
|
|
|
|
|
2015-12-12 07:11:49 -05:00
|
|
|
idBytes := this.ID.Bytes()
|
2016-01-18 19:21:07 -05:00
|
|
|
writer.Write(idBytes)
|
|
|
|
|
|
|
|
writer.Write(this.AlterIds.Bytes())
|
2016-01-20 11:31:43 -05:00
|
|
|
writer.Write([]byte{byte(this.Level)})
|
2015-12-12 07:11:49 -05:00
|
|
|
|
2016-01-20 11:31:43 -05:00
|
|
|
writer.Write([]byte{this.ValidMin})
|
2015-12-12 07:11:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *SwitchAccount) Unmarshal(data []byte) error {
|
2016-01-18 19:21:07 -05:00
|
|
|
lenHost := int(data[0])
|
|
|
|
if len(data) < lenHost+1 {
|
|
|
|
return transport.CorruptedPacket
|
|
|
|
}
|
|
|
|
this.Host = v2net.ParseAddress(string(data[1 : 1+lenHost]))
|
|
|
|
portStart := 1 + lenHost
|
|
|
|
if len(data) < portStart+2 {
|
|
|
|
return transport.CorruptedPacket
|
|
|
|
}
|
|
|
|
this.Port = v2net.PortFromBytes(data[portStart : portStart+2])
|
|
|
|
idStart := portStart + 2
|
|
|
|
if len(data) < idStart+16 {
|
|
|
|
return transport.CorruptedPacket
|
|
|
|
}
|
|
|
|
this.ID, _ = uuid.ParseBytes(data[idStart : idStart+16])
|
|
|
|
alterIdStart := idStart + 16
|
|
|
|
if len(data) < alterIdStart+2 {
|
|
|
|
return transport.CorruptedPacket
|
|
|
|
}
|
|
|
|
this.AlterIds = serial.ParseUint16(data[alterIdStart : alterIdStart+2])
|
2016-01-20 11:31:43 -05:00
|
|
|
levelStart := alterIdStart + 2
|
|
|
|
if len(data) < levelStart {
|
2015-12-12 07:11:49 -05:00
|
|
|
return transport.CorruptedPacket
|
|
|
|
}
|
2016-01-20 11:31:43 -05:00
|
|
|
this.Level = vmess.UserLevel(data[levelStart])
|
|
|
|
timeStart := levelStart + 1
|
|
|
|
if len(data) < timeStart {
|
|
|
|
return transport.CorruptedPacket
|
|
|
|
}
|
|
|
|
this.ValidMin = data[timeStart]
|
2015-12-12 07:11:49 -05:00
|
|
|
return nil
|
|
|
|
}
|