118 lines
2.4 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-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
)
type RequestCommand byte
const (
RequestCommandTCP = RequestCommand(0x01)
RequestCommandUDP = RequestCommand(0x02)
)
2016-12-21 12:53:31 +01:00
// RequestOption is the options of a request.
2016-07-11 15:54:19 +02:00
type RequestOption byte
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.
RequestOptionChunkStream = RequestOption(0x01)
// RequestOptionConnectionReuse indicates client side expects to reuse the connection.
2016-06-02 21:34:25 +02:00
RequestOptionConnectionReuse = RequestOption(0x02)
2016-12-21 12:53:31 +01:00
2017-02-14 14:16:43 +01:00
RequestOptionChunkMasking = RequestOption(0x04)
2016-02-16 00:42:52 +01:00
)
2016-11-27 21:39:09 +01:00
func (v RequestOption) Has(option RequestOption) bool {
return (v & option) == option
2016-06-02 21:34:25 +02:00
}
2016-11-27 21:39:09 +01:00
func (v *RequestOption) Set(option RequestOption) {
*v = (*v | option)
2016-06-02 21:34:25 +02:00
}
2016-11-27 21:39:09 +01:00
func (v *RequestOption) Clear(option RequestOption) {
*v = (*v & (^option))
2016-02-27 16:41:21 +01:00
}
2016-12-07 17:32:40 +01:00
type Security byte
func (v Security) Is(t SecurityType) bool {
return v == Security(t)
}
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
Option RequestOption
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-01-14 00:27:45 +01:00
func (v *RequestHeader) Destination() net.Destination {
2016-11-27 21:39:09 +01:00
if v.Command == RequestCommandUDP {
2017-01-14 00:27:45 +01:00
return net.UDPDestination(v.Address, v.Port)
2016-02-27 17:28:21 +01:00
}
2017-01-14 00:27:45 +01:00
return net.TCPDestination(v.Address, v.Port)
2016-02-27 17:28:21 +01:00
}
2016-06-02 21:34:25 +02:00
type ResponseOption byte
2016-07-11 15:54:19 +02:00
const (
2016-12-21 12:53:31 +01:00
ResponseOptionConnectionReuse = ResponseOption(0x01)
2016-06-02 21:34:25 +02:00
)
2016-11-27 21:39:09 +01:00
func (v *ResponseOption) Set(option ResponseOption) {
*v = (*v | option)
2016-06-02 21:34:25 +02:00
}
2016-11-27 21:39:09 +01:00
func (v ResponseOption) Has(option ResponseOption) bool {
2016-12-12 22:46:15 +01:00
return (v & option) == option
2016-06-02 21:34:25 +02:00
}
2016-11-27 21:39:09 +01:00
func (v *ResponseOption) Clear(option ResponseOption) {
*v = (*v & (^option))
2016-06-02 21:34:25 +02:00
}
2016-02-16 00:42:52 +01:00
type ResponseCommand interface{}
type ResponseHeader struct {
2016-06-02 21:34:25 +02:00
Option ResponseOption
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-05-24 21:55:46 +02:00
AlterIds uint16
2016-09-18 00:41:21 +02:00
Level uint32
2016-02-27 00:05:53 +01:00
ValidMin byte
}
2016-12-11 14:58:53 +01:00
func (v *SecurityConfig) AsSecurity() Security {
if v == nil {
return Security(SecurityType_LEGACY)
}
if v.Type == SecurityType_AUTO {
if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" {
return Security(SecurityType_AES128_GCM)
}
return Security(SecurityType_CHACHA20_POLY1305)
}
2016-12-13 10:45:21 +01:00
return NormSecurity(Security(v.Type))
2016-12-11 14:58:53 +01:00
}