Compare commits

..
1 Commits
Author SHA1 Message Date
Fangliding 96dbfda86d Fix many panic 2026-12-04 14:56:33 +08:00
15 changed files with 151 additions and 132 deletions
+4
View File
@@ -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 {
+6
View File
@@ -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)
}
+1 -1
View File
@@ -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"`
+3 -3
View File
@@ -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) {
+1 -4
View File
@@ -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`)
}
+6 -9
View File
@@ -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.")
}
+6 -6
View File
@@ -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) {
+3
View File
@@ -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
+3
View File
@@ -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
}
+3
View File
@@ -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")
+5 -8
View File
@@ -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.")
}
+100 -95
View File
@@ -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,49 +274,14 @@ 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
}
switch s[2] {
case "1rtt":
case "0rtt":
account.Seconds = 1
default:
return false
}
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) != 1184 {
return false
}
}
account.Encryption = account.Encryption[27+len(s[2]):]
if padding > 0 {
account.Padding = account.Encryption[:padding-1]
account.Encryption = account.Encryption[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)
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
@@ -368,3 +289,87 @@ func (c *VLessOutboundConfig) Build() (proto.Message, error) {
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":
out.seconds = 1
default:
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)
}
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)
}
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`)
}
}
rest := raw[27+len(s[2]):]
if padding > 0 {
out.padding = rest[:padding-1]
rest = rest[padding:]
}
out.key = rest
return out, nil
}
return parsedVLESSEncryption{}, errors.New(`VLESS `, scope, `: missing "`, field, `" key segment`)
}
+1 -4
View File
@@ -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
+7
View File
@@ -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
+2 -2
View File
@@ -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")
}
}