2021-06-19 08:45:43 -04:00
|
|
|
package v4
|
2019-02-10 13:04:11 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v5/common/protocol"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/serial"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon/loader"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon/socketcfg"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/infra/conf/cfgcommon/tlscfg"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet/domainsocket"
|
|
|
|
httpheader "github.com/v2fly/v2ray-core/v5/transport/internet/headers/http"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet/http"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet/kcp"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet/quic"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet/tcp"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet/websocket"
|
2019-02-10 13:04:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-06-19 08:28:20 -04:00
|
|
|
kcpHeaderLoader = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{
|
2019-02-10 13:04:11 -05:00
|
|
|
"none": func() interface{} { return new(NoOpAuthenticator) },
|
|
|
|
"srtp": func() interface{} { return new(SRTPAuthenticator) },
|
|
|
|
"utp": func() interface{} { return new(UTPAuthenticator) },
|
|
|
|
"wechat-video": func() interface{} { return new(WechatVideoAuthenticator) },
|
|
|
|
"dtls": func() interface{} { return new(DTLSAuthenticator) },
|
|
|
|
"wireguard": func() interface{} { return new(WireguardAuthenticator) },
|
|
|
|
}, "type", "")
|
|
|
|
|
2021-06-19 08:28:20 -04:00
|
|
|
tcpHeaderLoader = loader.NewJSONConfigLoader(loader.ConfigCreatorCache{
|
2019-02-10 13:04:11 -05:00
|
|
|
"none": func() interface{} { return new(NoOpConnectionAuthenticator) },
|
2020-10-11 07:22:46 -04:00
|
|
|
"http": func() interface{} { return new(Authenticator) },
|
2019-02-10 13:04:11 -05:00
|
|
|
}, "type", "")
|
|
|
|
)
|
|
|
|
|
|
|
|
type KCPConfig struct {
|
|
|
|
Mtu *uint32 `json:"mtu"`
|
|
|
|
Tti *uint32 `json:"tti"`
|
|
|
|
UpCap *uint32 `json:"uplinkCapacity"`
|
|
|
|
DownCap *uint32 `json:"downlinkCapacity"`
|
|
|
|
Congestion *bool `json:"congestion"`
|
|
|
|
ReadBufferSize *uint32 `json:"readBufferSize"`
|
|
|
|
WriteBufferSize *uint32 `json:"writeBufferSize"`
|
|
|
|
HeaderConfig json.RawMessage `json:"header"`
|
2020-06-08 20:36:40 -04:00
|
|
|
Seed *string `json:"seed"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build implements Buildable.
|
|
|
|
func (c *KCPConfig) Build() (proto.Message, error) {
|
|
|
|
config := new(kcp.Config)
|
|
|
|
|
|
|
|
if c.Mtu != nil {
|
|
|
|
mtu := *c.Mtu
|
|
|
|
if mtu < 576 || mtu > 1460 {
|
|
|
|
return nil, newError("invalid mKCP MTU size: ", mtu).AtError()
|
|
|
|
}
|
|
|
|
config.Mtu = &kcp.MTU{Value: mtu}
|
|
|
|
}
|
|
|
|
if c.Tti != nil {
|
|
|
|
tti := *c.Tti
|
|
|
|
if tti < 10 || tti > 100 {
|
|
|
|
return nil, newError("invalid mKCP TTI: ", tti).AtError()
|
|
|
|
}
|
|
|
|
config.Tti = &kcp.TTI{Value: tti}
|
|
|
|
}
|
|
|
|
if c.UpCap != nil {
|
|
|
|
config.UplinkCapacity = &kcp.UplinkCapacity{Value: *c.UpCap}
|
|
|
|
}
|
|
|
|
if c.DownCap != nil {
|
|
|
|
config.DownlinkCapacity = &kcp.DownlinkCapacity{Value: *c.DownCap}
|
|
|
|
}
|
|
|
|
if c.Congestion != nil {
|
|
|
|
config.Congestion = *c.Congestion
|
|
|
|
}
|
|
|
|
if c.ReadBufferSize != nil {
|
|
|
|
size := *c.ReadBufferSize
|
|
|
|
if size > 0 {
|
|
|
|
config.ReadBuffer = &kcp.ReadBuffer{Size: size * 1024 * 1024}
|
|
|
|
} else {
|
|
|
|
config.ReadBuffer = &kcp.ReadBuffer{Size: 512 * 1024}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.WriteBufferSize != nil {
|
|
|
|
size := *c.WriteBufferSize
|
|
|
|
if size > 0 {
|
|
|
|
config.WriteBuffer = &kcp.WriteBuffer{Size: size * 1024 * 1024}
|
|
|
|
} else {
|
|
|
|
config.WriteBuffer = &kcp.WriteBuffer{Size: 512 * 1024}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(c.HeaderConfig) > 0 {
|
|
|
|
headerConfig, _, err := kcpHeaderLoader.Load(c.HeaderConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("invalid mKCP header config.").Base(err).AtError()
|
|
|
|
}
|
2021-06-19 08:04:50 -04:00
|
|
|
ts, err := headerConfig.(cfgcommon.Buildable).Build()
|
2019-02-10 13:04:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("invalid mKCP header config").Base(err).AtError()
|
|
|
|
}
|
|
|
|
config.HeaderConfig = serial.ToTypedMessage(ts)
|
|
|
|
}
|
|
|
|
|
2020-06-08 20:36:40 -04:00
|
|
|
if c.Seed != nil {
|
|
|
|
config.Seed = &kcp.EncryptionSeed{Seed: *c.Seed}
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:04:11 -05:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type TCPConfig struct {
|
2020-08-21 09:31:44 -04:00
|
|
|
HeaderConfig json.RawMessage `json:"header"`
|
|
|
|
AcceptProxyProtocol bool `json:"acceptProxyProtocol"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build implements Buildable.
|
|
|
|
func (c *TCPConfig) Build() (proto.Message, error) {
|
|
|
|
config := new(tcp.Config)
|
|
|
|
if len(c.HeaderConfig) > 0 {
|
|
|
|
headerConfig, _, err := tcpHeaderLoader.Load(c.HeaderConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("invalid TCP header config").Base(err).AtError()
|
|
|
|
}
|
2021-06-19 08:04:50 -04:00
|
|
|
ts, err := headerConfig.(cfgcommon.Buildable).Build()
|
2019-02-10 13:04:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("invalid TCP header config").Base(err).AtError()
|
|
|
|
}
|
|
|
|
config.HeaderSettings = serial.ToTypedMessage(ts)
|
|
|
|
}
|
2020-08-21 09:31:44 -04:00
|
|
|
if c.AcceptProxyProtocol {
|
|
|
|
config.AcceptProxyProtocol = c.AcceptProxyProtocol
|
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type WebSocketConfig struct {
|
2021-03-20 17:36:20 -04:00
|
|
|
Path string `json:"path"`
|
|
|
|
Headers map[string]string `json:"headers"`
|
|
|
|
AcceptProxyProtocol bool `json:"acceptProxyProtocol"`
|
|
|
|
MaxEarlyData int32 `json:"maxEarlyData"`
|
|
|
|
UseBrowserForwarding bool `json:"useBrowserForwarding"`
|
2021-05-01 10:49:42 -04:00
|
|
|
EarlyDataHeaderName string `json:"earlyDataHeaderName"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build implements Buildable.
|
|
|
|
func (c *WebSocketConfig) Build() (proto.Message, error) {
|
|
|
|
path := c.Path
|
|
|
|
header := make([]*websocket.Header, 0, 32)
|
|
|
|
for key, value := range c.Headers {
|
|
|
|
header = append(header, &websocket.Header{
|
|
|
|
Key: key,
|
|
|
|
Value: value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
config := &websocket.Config{
|
2021-03-20 17:36:20 -04:00
|
|
|
Path: path,
|
|
|
|
Header: header,
|
|
|
|
MaxEarlyData: c.MaxEarlyData,
|
|
|
|
UseBrowserForwarding: c.UseBrowserForwarding,
|
2021-05-01 10:49:42 -04:00
|
|
|
EarlyDataHeaderName: c.EarlyDataHeaderName,
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
2020-08-26 00:29:12 -04:00
|
|
|
if c.AcceptProxyProtocol {
|
|
|
|
config.AcceptProxyProtocol = c.AcceptProxyProtocol
|
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPConfig struct {
|
2021-03-01 15:55:31 -05:00
|
|
|
Host *cfgcommon.StringList `json:"host"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
Headers map[string]*cfgcommon.StringList `json:"headers"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
2020-09-21 10:56:43 -04:00
|
|
|
// Build implements Buildable.
|
2019-02-10 13:04:11 -05:00
|
|
|
func (c *HTTPConfig) Build() (proto.Message, error) {
|
|
|
|
config := &http.Config{
|
|
|
|
Path: c.Path,
|
|
|
|
}
|
|
|
|
if c.Host != nil {
|
|
|
|
config.Host = []string(*c.Host)
|
|
|
|
}
|
2021-03-01 15:55:31 -05:00
|
|
|
if c.Method != "" {
|
|
|
|
config.Method = c.Method
|
|
|
|
}
|
|
|
|
if len(c.Headers) > 0 {
|
|
|
|
config.Header = make([]*httpheader.Header, 0, len(c.Headers))
|
|
|
|
headerNames := sortMapKeys(c.Headers)
|
|
|
|
for _, key := range headerNames {
|
|
|
|
value := c.Headers[key]
|
|
|
|
if value == nil {
|
|
|
|
return nil, newError("empty HTTP header value: " + key).AtError()
|
|
|
|
}
|
|
|
|
config.Header = append(config.Header, &httpheader.Header{
|
|
|
|
Name: key,
|
|
|
|
Value: append([]string(nil), (*value)...),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type QUICConfig struct {
|
|
|
|
Header json.RawMessage `json:"header"`
|
|
|
|
Security string `json:"security"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
}
|
|
|
|
|
2020-09-21 10:56:43 -04:00
|
|
|
// Build implements Buildable.
|
2019-02-10 13:04:11 -05:00
|
|
|
func (c *QUICConfig) Build() (proto.Message, error) {
|
|
|
|
config := &quic.Config{
|
|
|
|
Key: c.Key,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Header) > 0 {
|
|
|
|
headerConfig, _, err := kcpHeaderLoader.Load(c.Header)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("invalid QUIC header config.").Base(err).AtError()
|
|
|
|
}
|
2021-06-19 08:04:50 -04:00
|
|
|
ts, err := headerConfig.(cfgcommon.Buildable).Build()
|
2019-02-10 13:04:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("invalid QUIC header config").Base(err).AtError()
|
|
|
|
}
|
|
|
|
config.Header = serial.ToTypedMessage(ts)
|
|
|
|
}
|
|
|
|
|
|
|
|
var st protocol.SecurityType
|
|
|
|
switch strings.ToLower(c.Security) {
|
|
|
|
case "aes-128-gcm":
|
|
|
|
st = protocol.SecurityType_AES128_GCM
|
|
|
|
case "chacha20-poly1305":
|
|
|
|
st = protocol.SecurityType_CHACHA20_POLY1305
|
|
|
|
default:
|
|
|
|
st = protocol.SecurityType_NONE
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Security = &protocol.SecurityConfig{
|
|
|
|
Type: st,
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DomainSocketConfig struct {
|
2020-10-29 03:30:38 -04:00
|
|
|
Path string `json:"path"`
|
|
|
|
Abstract bool `json:"abstract"`
|
|
|
|
Padding bool `json:"padding"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
2020-09-21 10:56:43 -04:00
|
|
|
// Build implements Buildable.
|
2019-02-10 13:04:11 -05:00
|
|
|
func (c *DomainSocketConfig) Build() (proto.Message, error) {
|
|
|
|
return &domainsocket.Config{
|
2020-10-29 03:30:38 -04:00
|
|
|
Path: c.Path,
|
|
|
|
Abstract: c.Abstract,
|
|
|
|
Padding: c.Padding,
|
2019-02-10 13:04:11 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransportProtocol string
|
|
|
|
|
|
|
|
// Build implements Buildable.
|
|
|
|
func (p TransportProtocol) Build() (string, error) {
|
|
|
|
switch strings.ToLower(string(p)) {
|
|
|
|
case "tcp":
|
|
|
|
return "tcp", nil
|
|
|
|
case "kcp", "mkcp":
|
|
|
|
return "mkcp", nil
|
|
|
|
case "ws", "websocket":
|
|
|
|
return "websocket", nil
|
|
|
|
case "h2", "http":
|
|
|
|
return "http", nil
|
|
|
|
case "ds", "domainsocket":
|
|
|
|
return "domainsocket", nil
|
|
|
|
case "quic":
|
|
|
|
return "quic", nil
|
2021-03-11 11:01:57 -05:00
|
|
|
case "gun", "grpc":
|
2021-09-07 11:36:26 -04:00
|
|
|
return "gun", nil
|
2019-02-10 13:04:11 -05:00
|
|
|
default:
|
|
|
|
return "", newError("Config: unknown transport protocol: ", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type StreamConfig struct {
|
2021-09-04 07:52:13 -04:00
|
|
|
Network *TransportProtocol `json:"network"`
|
|
|
|
Security string `json:"security"`
|
|
|
|
TLSSettings *tlscfg.TLSConfig `json:"tlsSettings"`
|
|
|
|
TCPSettings *TCPConfig `json:"tcpSettings"`
|
|
|
|
KCPSettings *KCPConfig `json:"kcpSettings"`
|
|
|
|
WSSettings *WebSocketConfig `json:"wsSettings"`
|
|
|
|
HTTPSettings *HTTPConfig `json:"httpSettings"`
|
|
|
|
DSSettings *DomainSocketConfig `json:"dsSettings"`
|
|
|
|
QUICSettings *QUICConfig `json:"quicSettings"`
|
|
|
|
GunSettings *GunConfig `json:"gunSettings"`
|
|
|
|
GRPCSettings *GunConfig `json:"grpcSettings"`
|
|
|
|
SocketSettings *socketcfg.SocketConfig `json:"sockopt"`
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build implements Buildable.
|
|
|
|
func (c *StreamConfig) Build() (*internet.StreamConfig, error) {
|
|
|
|
config := &internet.StreamConfig{
|
|
|
|
ProtocolName: "tcp",
|
|
|
|
}
|
|
|
|
if c.Network != nil {
|
2020-10-11 07:22:46 -04:00
|
|
|
protocol, err := c.Network.Build()
|
2019-02-10 13:04:11 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.ProtocolName = protocol
|
|
|
|
}
|
2019-06-14 09:47:28 -04:00
|
|
|
if strings.EqualFold(c.Security, "tls") {
|
2019-02-10 13:04:11 -05:00
|
|
|
tlsSettings := c.TLSSettings
|
|
|
|
if tlsSettings == nil {
|
2021-09-04 07:48:23 -04:00
|
|
|
tlsSettings = &tlscfg.TLSConfig{}
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
ts, err := tlsSettings.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("Failed to build TLS config.").Base(err)
|
|
|
|
}
|
|
|
|
tm := serial.ToTypedMessage(ts)
|
|
|
|
config.SecuritySettings = append(config.SecuritySettings, tm)
|
2021-06-19 09:36:54 -04:00
|
|
|
config.SecurityType = serial.V2Type(tm)
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
if c.TCPSettings != nil {
|
|
|
|
ts, err := c.TCPSettings.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("Failed to build TCP config.").Base(err)
|
|
|
|
}
|
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
|
|
|
ProtocolName: "tcp",
|
|
|
|
Settings: serial.ToTypedMessage(ts),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if c.KCPSettings != nil {
|
|
|
|
ts, err := c.KCPSettings.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("Failed to build mKCP config.").Base(err)
|
|
|
|
}
|
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
|
|
|
ProtocolName: "mkcp",
|
|
|
|
Settings: serial.ToTypedMessage(ts),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if c.WSSettings != nil {
|
|
|
|
ts, err := c.WSSettings.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("Failed to build WebSocket config.").Base(err)
|
|
|
|
}
|
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
|
|
|
ProtocolName: "websocket",
|
|
|
|
Settings: serial.ToTypedMessage(ts),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if c.HTTPSettings != nil {
|
|
|
|
ts, err := c.HTTPSettings.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("Failed to build HTTP config.").Base(err)
|
|
|
|
}
|
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
|
|
|
ProtocolName: "http",
|
|
|
|
Settings: serial.ToTypedMessage(ts),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if c.DSSettings != nil {
|
|
|
|
ds, err := c.DSSettings.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("Failed to build DomainSocket config.").Base(err)
|
|
|
|
}
|
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
|
|
|
ProtocolName: "domainsocket",
|
|
|
|
Settings: serial.ToTypedMessage(ds),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if c.QUICSettings != nil {
|
|
|
|
qs, err := c.QUICSettings.Build()
|
|
|
|
if err != nil {
|
2021-03-17 06:09:07 -04:00
|
|
|
return nil, newError("Failed to build QUIC config.").Base(err)
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
|
|
|
ProtocolName: "quic",
|
|
|
|
Settings: serial.ToTypedMessage(qs),
|
|
|
|
})
|
|
|
|
}
|
2021-03-17 06:09:07 -04:00
|
|
|
if c.GunSettings == nil {
|
|
|
|
c.GunSettings = c.GRPCSettings
|
|
|
|
}
|
|
|
|
if c.GunSettings != nil {
|
|
|
|
gs, err := c.GunSettings.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("Failed to build Gun config.").Base(err)
|
|
|
|
}
|
|
|
|
config.TransportSettings = append(config.TransportSettings, &internet.TransportConfig{
|
|
|
|
ProtocolName: "gun",
|
|
|
|
Settings: serial.ToTypedMessage(gs),
|
|
|
|
})
|
|
|
|
}
|
2019-02-10 13:04:11 -05:00
|
|
|
if c.SocketSettings != nil {
|
|
|
|
ss, err := c.SocketSettings.Build()
|
|
|
|
if err != nil {
|
2021-03-17 06:09:07 -04:00
|
|
|
return nil, newError("Failed to build sockopt.").Base(err)
|
2019-02-10 13:04:11 -05:00
|
|
|
}
|
|
|
|
config.SocketSettings = ss
|
|
|
|
}
|
|
|
|
return config, nil
|
|
|
|
}
|