1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00
v2fly/common/protocol/headers.go

101 lines
2.1 KiB
Go
Raw Normal View History

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