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 {
|
2018-02-09 16:29:30 -05:00
|
|
|
switch c {
|
|
|
|
case RequestCommandTCP, RequestCommandMux:
|
|
|
|
return TransferTypeStream
|
|
|
|
case RequestCommandUDP:
|
|
|
|
return TransferTypePacket
|
|
|
|
default:
|
2017-05-02 16:23:07 -04:00
|
|
|
return TransferTypeStream
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2018-07-07 09:42:24 -04:00
|
|
|
|
|
|
|
RequestOptionGlobalPadding bitmask.Byte = 0x08
|
2020-06-02 05:30:48 -04:00
|
|
|
|
|
|
|
RequestOptionEarlyChecksum bitmask.Byte = 0x16
|
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
|
2018-02-23 06:13:02 -05:00
|
|
|
Security SecurityType
|
2017-01-13 18:27:45 -05:00
|
|
|
Port net.Port
|
|
|
|
Address net.Address
|
2018-08-26 18:11:32 -04:00
|
|
|
User *MemoryUser
|
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
|
2018-01-18 17:25:48 -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
|
|
|
|
2018-02-23 06:13:02 -05:00
|
|
|
func (sc *SecurityConfig) GetSecurityType() SecurityType {
|
2017-10-30 16:14:04 -04:00
|
|
|
if sc == nil || sc.Type == SecurityType_AUTO {
|
2018-08-25 04:04:38 -04:00
|
|
|
if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" || runtime.GOARCH == "arm64" {
|
2018-02-23 06:13:02 -05:00
|
|
|
return SecurityType_AES128_GCM
|
2016-12-11 08:58:53 -05:00
|
|
|
}
|
2018-02-23 06:13:02 -05:00
|
|
|
return SecurityType_CHACHA20_POLY1305
|
2016-12-11 08:58:53 -05:00
|
|
|
}
|
2018-02-23 06:13:02 -05:00
|
|
|
return sc.Type
|
2016-12-11 08:58:53 -05:00
|
|
|
}
|
2017-10-22 14:17:06 -04:00
|
|
|
|
2018-03-04 15:06:04 -05:00
|
|
|
func isDomainTooLong(domain string) bool {
|
2017-10-22 14:17:06 -04:00
|
|
|
return len(domain) > 256
|
|
|
|
}
|