diff --git a/common/protocol/server_spec.go b/common/protocol/server_spec.go index 5c1b99f47a14..d4fe47af3876 100644 --- a/common/protocol/server_spec.go +++ b/common/protocol/server_spec.go @@ -1,6 +1,7 @@ package protocol import ( + "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/net" ) @@ -17,6 +18,9 @@ func NewServerSpec(dest net.Destination, user *MemoryUser) *ServerSpec { } func NewServerSpecFromPB(spec *ServerEndpoint) (*ServerSpec, error) { + if spec == nil || spec.Address == nil { + return nil, errors.New("server address is not set") + } dest := net.TCPDestination(spec.Address.AsAddress(), net.Port(spec.Port)) var dUser *MemoryUser if spec.User != nil { diff --git a/infra/conf/common.go b/infra/conf/common.go index 3ddd0157449c..03f424865405 100644 --- a/infra/conf/common.go +++ b/infra/conf/common.go @@ -47,6 +47,9 @@ type Address struct { // MarshalJSON implements encoding/json.Marshaler.MarshalJSON func (v *Address) MarshalJSON() ([]byte, error) { + if v.Address == nil { + return json.Marshal(net.AnyIP.String()) + } return json.Marshal(v.Address.String()) } @@ -68,6 +71,9 @@ func (v *Address) Build() *net.IPOrDomain { if v == nil { return nil } + if v.Address == nil { + return net.NewIPOrDomain(net.AnyIP) + } return net.NewIPOrDomain(v.Address) } diff --git a/infra/conf/http.go b/infra/conf/http.go index 4e29f6088e80..ae74de742b47 100644 --- a/infra/conf/http.go +++ b/infra/conf/http.go @@ -47,7 +47,7 @@ func (c *HTTPServerConfig) Build() (proto.Message, error) { } type HTTPClientConfig struct { - Address *Address `json:"address"` + Address Address `json:"address"` Port uint16 `json:"port"` Level uint32 `json:"level"` Email string `json:"email"` diff --git a/infra/conf/hysteria.go b/infra/conf/hysteria.go index 0c9fc3c71cdb..e128767915b5 100644 --- a/infra/conf/hysteria.go +++ b/infra/conf/hysteria.go @@ -11,9 +11,9 @@ import ( ) type HysteriaClientConfig struct { - Version int32 `json:"version"` - Address *Address `json:"address"` - Port uint16 `json:"port"` + Version int32 `json:"version"` + Address Address `json:"address"` + Port uint16 `json:"port"` } func (c *HysteriaClientConfig) Build() (proto.Message, error) { diff --git a/infra/conf/masque.go b/infra/conf/masque.go index b128ee593d94..4d0478ddcecf 100644 --- a/infra/conf/masque.go +++ b/infra/conf/masque.go @@ -12,15 +12,12 @@ import ( ) type MasqueClientConfig struct { - Address *Address `json:"address"` + Address Address `json:"address"` Port uint16 `json:"port"` RemoteDNS []string `json:"remoteDNS"` } func (c *MasqueClientConfig) Build() (proto.Message, error) { - if c.Address == nil { - return nil, errors.New(`MASQUE: "address" is not set`) - } if c.Port == 0 { return nil, errors.New(`MASQUE: "port" is not set`) } diff --git a/infra/conf/shadowsocks.go b/infra/conf/shadowsocks.go index cd07cd6d7667..2ec8f5103631 100644 --- a/infra/conf/shadowsocks.go +++ b/infra/conf/shadowsocks.go @@ -178,20 +178,17 @@ func buildShadowsocks2022(v *ShadowsocksServerConfig) (proto.Message, error) { } type ShadowsocksClientConfig struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Level byte `json:"level"` - Email string `json:"email"` - Cipher string `json:"method"` - Password string `json:"password"` + Address Address `json:"address"` + Port uint16 `json:"port"` + Level byte `json:"level"` + Email string `json:"email"` + Cipher string `json:"method"` + Password string `json:"password"` } func (v *ShadowsocksClientConfig) Build() (proto.Message, error) { errors.PrintNonRemovalDeprecatedFeatureWarning("Shadowsocks (with no Forward Secrecy, etc.)", "VLESS Encryption") - if v.Address == nil { - return nil, errors.New("Shadowsocks server address is not set.") - } if v.Port == 0 { return nil, errors.New("Invalid Shadowsocks port.") } diff --git a/infra/conf/socks.go b/infra/conf/socks.go index 026498cb0019..0c0a270ec1f6 100644 --- a/infra/conf/socks.go +++ b/infra/conf/socks.go @@ -65,12 +65,12 @@ func (v *SocksServerConfig) Build() (proto.Message, error) { } type SocksClientConfig struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Level uint32 `json:"level"` - Email string `json:"email"` - Username string `json:"user"` - Password string `json:"pass"` + Address Address `json:"address"` + Port uint16 `json:"port"` + Level uint32 `json:"level"` + Email string `json:"email"` + Username string `json:"user"` + Password string `json:"pass"` } func (v *SocksClientConfig) Build() (proto.Message, error) { diff --git a/infra/conf/transport_finalmask.go b/infra/conf/transport_finalmask.go index 5db631771e2b..97b4a80a0c4b 100644 --- a/infra/conf/transport_finalmask.go +++ b/infra/conf/transport_finalmask.go @@ -929,6 +929,9 @@ func (c *XDNS) Build() (proto.Message, error) { return nil, err } quicConfig = proto.(*internet.QuicParams) + if quicConfig.Congestion == "brutal" { + return nil, errors.New(`XDNS: "brutal" is for Hysteria, use "force-brutal", "bbr" or "reno"`) + } } for j := range c.Resolvers[i].Addrs { var u *url.URL diff --git a/infra/conf/transport_internet.go b/infra/conf/transport_internet.go index efee0363ad43..61a56574b1ff 100644 --- a/infra/conf/transport_internet.go +++ b/infra/conf/transport_internet.go @@ -277,6 +277,9 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) { return nil, errors.New(`XDRIVE: brutal is for Hysteria servers, over h3 "congestion" is "bbr" or "reno", not "`, config.QuicParams.Congestion, `"`) } } + if config.ProtocolName == "splithttp" && config.QuicParams != nil && config.QuicParams.Congestion == "brutal" { + return nil, errors.New(`XHTTP: "brutal" is for Hysteria, use "force-brutal", "bbr" or "reno"`) + } return config, nil } diff --git a/infra/conf/transport_method.go b/infra/conf/transport_method.go index 78f463a2c77a..225c1e595252 100644 --- a/infra/conf/transport_method.go +++ b/infra/conf/transport_method.go @@ -455,6 +455,9 @@ func (c *SplitHTTPConfig) Build() (proto.Message, error) { if c.ServerMaxHeaderBytes < 0 { return nil, errors.New("invalid negative value of maxHeaderBytes") } + if c.ScMaxEachPostBytes.From < 0 || c.ScMaxEachPostBytes.To < 0 || (c.ScMaxEachPostBytes.To > 0 && c.ScMaxEachPostBytes.From <= 0) { + return nil, errors.New("`scMaxEachPostBytes` must be greater than 0") + } if c.Xmux.MaxConnections.To > 0 && c.Xmux.MaxConcurrency.To > 0 { return nil, errors.New("maxConnections cannot be specified together with maxConcurrency") diff --git a/infra/conf/trojan.go b/infra/conf/trojan.go index 45ba0ef3c38b..7788de0b33ff 100644 --- a/infra/conf/trojan.go +++ b/infra/conf/trojan.go @@ -19,11 +19,11 @@ import ( // TrojanClientConfig is configuration of trojan servers type TrojanClientConfig struct { - Address *Address `json:"address"` - Port uint16 `json:"port"` - Level byte `json:"level"` - Email string `json:"email"` - Password string `json:"password"` + Address Address `json:"address"` + Port uint16 `json:"port"` + Level byte `json:"level"` + Email string `json:"email"` + Password string `json:"password"` } // Build implements Buildable @@ -32,9 +32,6 @@ func (c *TrojanClientConfig) Build() (proto.Message, error) { config := &trojan.ClientConfig{} - if c.Address == nil { - return nil, errors.New("Trojan server address is not set.") - } if c.Port == 0 { return nil, errors.New("Invalid Trojan port.") } diff --git a/infra/conf/vless.go b/infra/conf/vless.go index 724f81de48d9..512e1761ffcf 100644 --- a/infra/conf/vless.go +++ b/infra/conf/vless.go @@ -104,56 +104,15 @@ func (c *VLessInboundConfig) Build() (proto.Message, error) { return nil, err } - config.Decryption = c.Decryption - if !func() bool { - s := strings.Split(config.Decryption, ".") - if len(s) < 4 || s[0] != "mlkem768x25519plus" { - return false - } - switch s[1] { - case "native": - case "xorpub": - config.XorMode = 1 - case "random": - config.XorMode = 2 - default: - return false - } - t := strings.SplitN(strings.TrimSuffix(s[2], "s"), "-", 2) - i, err := strconv.Atoi(t[0]) - if err != nil { - return false - } - config.SecondsFrom = int64(i) - if len(t) == 2 { - i, err := strconv.Atoi(t[1]) - if err != nil { - return false - } - config.SecondsTo = int64(i) - } - padding := 0 - for _, r := range s[3:] { - if len(r) < 20 { - padding += len(r) + 1 - continue - } - if b, _ := base64.RawURLEncoding.DecodeString(r); len(b) != 32 && len(b) != 64 { - return false - } - } - config.Decryption = config.Decryption[27+len(s[2]):] - if padding > 0 { - config.Padding = config.Decryption[:padding-1] - config.Decryption = config.Decryption[padding:] - } - return true - }() && config.Decryption != "none" { - if config.Decryption == "" { - return nil, errors.New(`VLESS settings: please add/set "decryption":"none" to every settings`) - } - return nil, errors.New(`VLESS settings: unsupported "decryption": ` + config.Decryption) + dec, err := parseVLESSEncryption(c.Decryption, false) + if err != nil { + return nil, err } + config.Decryption = dec.key + config.Padding = dec.padding + config.XorMode = dec.xorMode + config.SecondsFrom = dec.secondsFrom + config.SecondsTo = dec.secondsTo if config.Decryption != "none" && c.Fallbacks != nil { return nil, errors.New(`VLESS settings: "fallbacks" can not be used together with "decryption"`) @@ -257,7 +216,7 @@ func (c *MultiplexConfig) Build() (*vless.Multiplex, error) { } type VLessOutboundConfig struct { - Address *Address `json:"address"` + Address Address `json:"address"` Port uint16 `json:"port"` Level uint32 `json:"level"` Email string `json:"email"` @@ -279,9 +238,6 @@ func (c *VLessOutboundConfig) Build() (proto.Message, error) { } config := &outbound.Config{Multiplex: multiplex} - if c.Address == nil { - return nil, errors.New(`VLESS: "address" is not set`) - } uid, err := uuid.ParseString(c.Id) if err != nil { return nil, err @@ -318,53 +274,102 @@ func (c *VLessOutboundConfig) Build() (proto.Message, error) { account.Reverse = rvs } - if !func() bool { - s := strings.Split(account.Encryption, ".") - if len(s) < 4 || s[0] != "mlkem768x25519plus" { - return false - } - switch s[1] { - case "native": - case "xorpub": - account.XorMode = 1 - case "random": - account.XorMode = 2 - default: - return false - } + enc, err := parseVLESSEncryption(account.Encryption, true) + if err != nil { + return nil, err + } + account.Encryption = enc.key + account.Padding = enc.padding + account.XorMode = enc.xorMode + account.Seconds = enc.seconds + + user.Account = serial.ToTypedMessage(account) + spec.User = user + + config.Vnext = spec + return config, nil +} + +type parsedVLESSEncryption struct { + key string + padding string + xorMode uint32 + seconds uint32 + secondsFrom int64 + secondsTo int64 +} + +func parseVLESSEncryption(raw string, isOutbound bool) (parsedVLESSEncryption, error) { + field, scope := "decryption", "settings" + if isOutbound { + field, scope = "encryption", "users" + } + if raw == "" { + return parsedVLESSEncryption{}, errors.New(`VLESS `, scope, `: please add/set "`, field, `":"none"`) + } + if raw == "none" { + return parsedVLESSEncryption{key: "none"}, nil + } + s := strings.Split(raw, ".") + if len(s) < 4 || s[0] != "mlkem768x25519plus" { + return parsedVLESSEncryption{}, errors.New(`VLESS `, scope, `: unsupported "`, field, `": `, raw) + } + var out parsedVLESSEncryption + switch s[1] { + case "native": + case "xorpub": + out.xorMode = 1 + case "random": + out.xorMode = 2 + default: + return parsedVLESSEncryption{}, errors.New(`VLESS `, scope, `: unsupported "`, field, `" mode: `, s[1]) + } + if isOutbound { switch s[2] { case "1rtt": case "0rtt": - account.Seconds = 1 + out.seconds = 1 default: - return false + return parsedVLESSEncryption{}, errors.New(`VLESS `, scope, `: unsupported "`, field, `" rtt: `, s[2]) + } + } else { + t := strings.SplitN(strings.TrimSuffix(s[2], "s"), "-", 2) + i, err := strconv.Atoi(t[0]) + if err != nil { + return parsedVLESSEncryption{}, errors.New(`VLESS `, scope, `: invalid "`, field, `" seconds: `, s[2]).Base(err) } - padding := 0 - for _, r := range s[3:] { - if len(r) < 20 { - padding += len(r) + 1 - continue + out.secondsFrom = int64(i) + if len(t) == 2 { + i, err := strconv.Atoi(t[1]) + if err != nil { + return parsedVLESSEncryption{}, errors.New(`VLESS `, scope, `: invalid "`, field, `" seconds: `, s[2]).Base(err) } - if b, _ := base64.RawURLEncoding.DecodeString(r); len(b) != 32 && len(b) != 1184 { - return false + out.secondsTo = int64(i) + } + } + altKeyLen := 64 + if isOutbound { + altKeyLen = 1184 + } + padding := 0 + for i, r := range s[3:] { + if len(r) < 20 { + padding += len(r) + 1 + continue + } + for _, r := range s[3+i:] { + b, err := base64.RawURLEncoding.DecodeString(r) + if err != nil || (len(b) != 32 && len(b) != altKeyLen) { + return parsedVLESSEncryption{}, errors.New(`VLESS `, scope, `: invalid "`, field, `" key segment`) } } - account.Encryption = account.Encryption[27+len(s[2]):] + rest := raw[27+len(s[2]):] if padding > 0 { - account.Padding = account.Encryption[:padding-1] - account.Encryption = account.Encryption[padding:] + out.padding = rest[:padding-1] + rest = rest[padding:] } - return true - }() && account.Encryption != "none" { - if account.Encryption == "" { - return nil, errors.New(`VLESS users: please add/set "encryption":"none" for every user`) - } - return nil, errors.New(`VLESS users: unsupported "encryption": ` + account.Encryption) + out.key = rest + return out, nil } - - user.Account = serial.ToTypedMessage(account) - spec.User = user - - config.Vnext = spec - return config, nil + return parsedVLESSEncryption{}, errors.New(`VLESS `, scope, `: missing "`, field, `" key segment`) } diff --git a/infra/conf/vmess.go b/infra/conf/vmess.go index 7c69dcc8cbdb..cf8c94652682 100644 --- a/infra/conf/vmess.go +++ b/infra/conf/vmess.go @@ -103,7 +103,7 @@ func (c *VMessInboundConfig) Build() (proto.Message, error) { } type VMessOutboundConfig struct { - Address *Address `json:"address"` + Address Address `json:"address"` Port uint16 `json:"port"` Level uint32 `json:"level"` Email string `json:"email"` @@ -123,9 +123,6 @@ func (c *VMessOutboundConfig) Build() (proto.Message, error) { } config := &outbound.Config{Multiplex: multiplex} - if c.Address == nil { - return nil, errors.New(`VMess: "address" is not set`) - } uid, err := uuid.ParseString(c.ID) if err != nil { return nil, err diff --git a/infra/conf/wireguard.go b/infra/conf/wireguard.go index f489d38718ad..bda4c7ada2bc 100644 --- a/infra/conf/wireguard.go +++ b/infra/conf/wireguard.go @@ -3,6 +3,7 @@ package conf import ( "encoding/base64" "encoding/hex" + "net/netip" "strconv" "strings" @@ -124,6 +125,12 @@ func (c *WireGuardConfig) Build() (proto.Message, error) { } config.Reserved = c.Reserved + for _, s := range c.DNS { + if _, err := netip.ParseAddr(s); err != nil { + return nil, errors.New(`WireGuard: invalid "remoteDNS" `, s).Base(err) + } + } + config.IsClient = c.IsClient config.NoKernelTun = c.NoKernelTun config.DNS = c.DNS diff --git a/infra/conf/xray.go b/infra/conf/xray.go index 01c2ddf23fc0..320fc89c6ee4 100644 --- a/infra/conf/xray.go +++ b/infra/conf/xray.go @@ -232,13 +232,13 @@ func validateOutboundTransportSecurity(rawConfig interface{}, senderSettings *pr if vlessCfg.Encryption != "" && vlessCfg.Encryption != "none" { return nil } - if requiresTransportSecurity(vlessCfg.Address) { + if requiresTransportSecurity(&vlessCfg.Address) { return errors.New("vless without TLS or other encryption is prohibited unless the server address is a private IP or domain") } } if tjCfg, ok := rawConfig.(*TrojanClientConfig); ok { - if requiresTransportSecurity(tjCfg.Address) { + if requiresTransportSecurity(&tjCfg.Address) { return errors.New("trojan without TLS is prohibited unless the server address is a private IP or domain") } }