2016-02-15 18:42:52 -05:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2016-12-11 08:58:53 -05:00
|
|
|
"runtime"
|
|
|
|
|
2016-08-20 14:55:45 -04:00
|
|
|
v2net "v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/uuid"
|
2016-02-15 18:42:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type RequestCommand byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
RequestCommandTCP = RequestCommand(0x01)
|
|
|
|
RequestCommandUDP = RequestCommand(0x02)
|
|
|
|
)
|
|
|
|
|
2016-12-21 06:53:31 -05:00
|
|
|
// RequestOption is the options of a request.
|
2016-07-11 09:54:19 -04:00
|
|
|
type RequestOption byte
|
|
|
|
|
2016-02-15 18:42:52 -05:00
|
|
|
const (
|
2016-12-21 06:53:31 -05:00
|
|
|
// RequestOptionChunkStream indicates request payload is chunked. Each chunk consists of length, authentication and payload.
|
|
|
|
RequestOptionChunkStream = RequestOption(0x01)
|
|
|
|
|
|
|
|
// RequestOptionConnectionReuse indicates client side expects to reuse the connection.
|
2016-06-02 15:34:25 -04:00
|
|
|
RequestOptionConnectionReuse = RequestOption(0x02)
|
2016-12-21 06:53:31 -05:00
|
|
|
|
|
|
|
// RequestOptionCompressedStream indicates request payload is compressed.
|
|
|
|
RequestOptionCompressedStream = RequestOption(0x04)
|
2016-02-15 18:42:52 -05:00
|
|
|
)
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v RequestOption) Has(option RequestOption) bool {
|
|
|
|
return (v & option) == option
|
2016-06-02 15:34:25 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *RequestOption) Set(option RequestOption) {
|
|
|
|
*v = (*v | option)
|
2016-06-02 15:34:25 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *RequestOption) Clear(option RequestOption) {
|
|
|
|
*v = (*v & (^option))
|
2016-02-27 10:41:21 -05:00
|
|
|
}
|
|
|
|
|
2016-12-07 11:32:40 -05:00
|
|
|
type Security byte
|
|
|
|
|
|
|
|
func (v Security) Is(t SecurityType) bool {
|
|
|
|
return v == Security(t)
|
|
|
|
}
|
|
|
|
|
2016-12-13 04:45:21 -05:00
|
|
|
func NormSecurity(s Security) Security {
|
|
|
|
if s.Is(SecurityType_UNKNOWN) {
|
|
|
|
return Security(SecurityType_LEGACY)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2016-02-15 18:42:52 -05:00
|
|
|
type RequestHeader struct {
|
2016-12-07 11:32:40 -05:00
|
|
|
Version byte
|
|
|
|
Command RequestCommand
|
|
|
|
Option RequestOption
|
|
|
|
Security Security
|
|
|
|
Port v2net.Port
|
2016-12-21 09:37:16 -05:00
|
|
|
Address v2net.Address
|
|
|
|
User *User
|
2016-02-15 18:42:52 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *RequestHeader) Destination() v2net.Destination {
|
|
|
|
if v.Command == RequestCommandUDP {
|
|
|
|
return v2net.UDPDestination(v.Address, v.Port)
|
2016-02-27 11:28:21 -05:00
|
|
|
}
|
2016-11-27 15:39:09 -05:00
|
|
|
return v2net.TCPDestination(v.Address, v.Port)
|
2016-02-27 11:28:21 -05:00
|
|
|
}
|
|
|
|
|
2016-06-02 15:34:25 -04:00
|
|
|
type ResponseOption byte
|
|
|
|
|
2016-07-11 09:54:19 -04:00
|
|
|
const (
|
2016-12-21 06:53:31 -05:00
|
|
|
ResponseOptionConnectionReuse = ResponseOption(0x01)
|
2016-06-02 15:34:25 -04:00
|
|
|
)
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *ResponseOption) Set(option ResponseOption) {
|
|
|
|
*v = (*v | option)
|
2016-06-02 15:34:25 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v ResponseOption) Has(option ResponseOption) bool {
|
2016-12-12 16:46:15 -05:00
|
|
|
return (v & option) == option
|
2016-06-02 15:34:25 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *ResponseOption) Clear(option ResponseOption) {
|
|
|
|
*v = (*v & (^option))
|
2016-06-02 15:34:25 -04:00
|
|
|
}
|
|
|
|
|
2016-02-15 18:42:52 -05:00
|
|
|
type ResponseCommand interface{}
|
|
|
|
|
|
|
|
type ResponseHeader struct {
|
2016-06-02 15:34:25 -04:00
|
|
|
Option ResponseOption
|
2016-02-15 18:42:52 -05:00
|
|
|
Command ResponseCommand
|
|
|
|
}
|
2016-02-26 18:05:53 -05:00
|
|
|
|
|
|
|
type CommandSwitchAccount struct {
|
|
|
|
Host v2net.Address
|
|
|
|
Port v2net.Port
|
|
|
|
ID *uuid.UUID
|
2016-05-24 15:55:46 -04:00
|
|
|
AlterIds uint16
|
2016-09-17 18:41:21 -04:00
|
|
|
Level uint32
|
2016-02-26 18:05:53 -05:00
|
|
|
ValidMin byte
|
|
|
|
}
|
2016-12-11 08:58:53 -05:00
|
|
|
|
|
|
|
func (v *SecurityConfig) AsSecurity() Security {
|
|
|
|
if v == nil {
|
|
|
|
return Security(SecurityType_LEGACY)
|
|
|
|
}
|
|
|
|
if v.Type == SecurityType_AUTO {
|
|
|
|
if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" {
|
|
|
|
return Security(SecurityType_AES128_GCM)
|
|
|
|
}
|
|
|
|
return Security(SecurityType_CHACHA20_POLY1305)
|
|
|
|
}
|
2016-12-13 04:45:21 -05:00
|
|
|
return NormSecurity(Security(v.Type))
|
2016-12-11 08:58:53 -05:00
|
|
|
}
|