1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-26 09:25:23 +00:00
v2fly/common/protocol/headers.go

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