v2ray-core/common/protocol/headers.go

101 lines
2.1 KiB
Go
Raw Normal View History

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