2016-02-15 18:42:52 -05:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2016-12-11 08:58:53 -05:00
|
|
|
"runtime"
|
|
|
|
|
2017-10-21 15:04:24 -04:00
|
|
|
"v2ray.com/core/common/bitmask"
|
2017-01-13 18:27:45 -05:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/uuid"
|
2016-02-15 18:42:52 -05:00
|
|
|
)
|
|
|
|
|
2017-02-20 05:25:05 -05:00
|
|
|
// RequestCommand is a custom command in a proxy request.
|
2016-02-15 18:42:52 -05:00
|
|
|
type RequestCommand byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
RequestCommandTCP = RequestCommand(0x01)
|
|
|
|
RequestCommandUDP = RequestCommand(0x02)
|
2017-05-17 15:13:01 -04:00
|
|
|
RequestCommandMux = RequestCommand(0x03)
|
2016-02-15 18:42:52 -05:00
|
|
|
)
|
|
|
|
|
2017-05-02 16:23:07 -04:00
|
|
|
func (c RequestCommand) TransferType() TransferType {
|
|
|
|
if c == RequestCommandTCP {
|
|
|
|
return TransferTypeStream
|
|
|
|
}
|
|
|
|
|
|
|
|
return TransferTypePacket
|
|
|
|
}
|
|
|
|
|
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.
|
2017-10-21 15:04:24 -04:00
|
|
|
RequestOptionChunkStream bitmask.Byte = 0x01
|
2016-12-21 06:53:31 -05:00
|
|
|
|
|
|
|
// RequestOptionConnectionReuse indicates client side expects to reuse the connection.
|
2017-10-21 15:04:24 -04:00
|
|
|
RequestOptionConnectionReuse bitmask.Byte = 0x02
|
2016-12-21 06:53:31 -05:00
|
|
|
|
2017-10-21 15:04:24 -04:00
|
|
|
RequestOptionChunkMasking bitmask.Byte = 0x04
|
2016-02-15 18:42:52 -05:00
|
|
|
)
|
|
|
|
|
2016-12-07 11:32:40 -05:00
|
|
|
type Security byte
|
|
|
|
|
2017-10-06 15:17:55 -04:00
|
|
|
func (s Security) Is(t SecurityType) bool {
|
|
|
|
return s == Security(t)
|
2016-12-07 11:32:40 -05:00
|
|
|
}
|
|
|
|
|
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
|
2017-10-21 15:04:24 -04:00
|
|
|
Option bitmask.Byte
|
2016-12-07 11:32:40 -05:00
|
|
|
Security Security
|
2017-01-13 18:27:45 -05:00
|
|
|
Port net.Port
|
|
|
|
Address net.Address
|
2016-12-21 09:37:16 -05:00
|
|
|
User *User
|
2016-02-15 18:42:52 -05:00
|
|
|
}
|
|
|
|
|
2017-10-06 15:17:55 -04:00
|
|
|
func (h *RequestHeader) Destination() net.Destination {
|
|
|
|
if h.Command == RequestCommandUDP {
|
|
|
|
return net.UDPDestination(h.Address, h.Port)
|
2016-02-27 11:28:21 -05:00
|
|
|
}
|
2017-10-06 15:17:55 -04:00
|
|
|
return net.TCPDestination(h.Address, h.Port)
|
2016-02-27 11:28:21 -05:00
|
|
|
}
|
|
|
|
|
2016-07-11 09:54:19 -04:00
|
|
|
const (
|
2017-10-21 15:04:24 -04:00
|
|
|
ResponseOptionConnectionReuse bitmask.Byte = 0x01
|
2016-06-02 15:34:25 -04:00
|
|
|
)
|
|
|
|
|
2016-02-15 18:42:52 -05:00
|
|
|
type ResponseCommand interface{}
|
|
|
|
|
|
|
|
type ResponseHeader struct {
|
2017-10-21 15:04:24 -04:00
|
|
|
Option bitmask.Byte
|
2016-02-15 18:42:52 -05:00
|
|
|
Command ResponseCommand
|
|
|
|
}
|
2016-02-26 18:05:53 -05:00
|
|
|
|
|
|
|
type CommandSwitchAccount struct {
|
2017-01-13 18:27:45 -05:00
|
|
|
Host net.Address
|
|
|
|
Port net.Port
|
2016-02-26 18:05:53 -05:00
|
|
|
ID *uuid.UUID
|
2016-09-17 18:41:21 -04:00
|
|
|
Level uint32
|
2017-10-22 16:45:05 -04:00
|
|
|
AlterIds uint16
|
2016-02-26 18:05:53 -05:00
|
|
|
ValidMin byte
|
|
|
|
}
|
2016-12-11 08:58:53 -05:00
|
|
|
|
2017-10-06 15:17:55 -04:00
|
|
|
func (sc *SecurityConfig) AsSecurity() Security {
|
2017-10-30 16:14:04 -04:00
|
|
|
if sc == nil || sc.Type == SecurityType_AUTO {
|
2016-12-11 08:58:53 -05:00
|
|
|
if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" {
|
|
|
|
return Security(SecurityType_AES128_GCM)
|
|
|
|
}
|
|
|
|
return Security(SecurityType_CHACHA20_POLY1305)
|
|
|
|
}
|
2017-10-06 15:17:55 -04:00
|
|
|
return NormSecurity(Security(sc.Type))
|
2016-12-11 08:58:53 -05:00
|
|
|
}
|
2017-10-22 14:17:06 -04:00
|
|
|
|
|
|
|
func IsDomainTooLong(domain string) bool {
|
|
|
|
return len(domain) > 256
|
|
|
|
}
|