From 65458e919fcb3548d44481ea7929031a14bf117e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Thu, 27 Aug 2026 19:57:15 +0800 Subject: [PATCH] Config: Fix some issues (#6640) https://github.com/XTLS/Xray-core/pull/6640#issuecomment-5420106315 Fixes https://github.com/XTLS/Xray-core/issues/6636 Fixes https://github.com/XTLS/Xray-core/issues/6600 ... --- common/reflect/marshal_test.go | 1 + infra/conf/common.go | 3 +++ infra/conf/xray.go | 12 ++++++------ proxy/http/client.go | 9 ++++++--- transport/internet/tcp/hub.go | 3 --- transport/internet/tcp_hub.go | 3 +++ 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/common/reflect/marshal_test.go b/common/reflect/marshal_test.go index d560f1b4..748bc69e 100644 --- a/common/reflect/marshal_test.go +++ b/common/reflect/marshal_test.go @@ -207,6 +207,7 @@ func getConfig() string { "tag": "XHTTP_IN", "streamSettings": { "network": "xhttp", + "security": "tls", "xhttpSettings": { "host": "bing.com", "path": "/xhttp_client_upload", diff --git a/infra/conf/common.go b/infra/conf/common.go index 0de3568c..3ddd0157 100644 --- a/infra/conf/common.go +++ b/infra/conf/common.go @@ -65,6 +65,9 @@ func (v *Address) UnmarshalJSON(data []byte) error { } func (v *Address) Build() *net.IPOrDomain { + if v == nil { + return nil + } return net.NewIPOrDomain(v.Address) } diff --git a/infra/conf/xray.go b/infra/conf/xray.go index bdcabbd5..c029a7bf 100644 --- a/infra/conf/xray.go +++ b/infra/conf/xray.go @@ -140,7 +140,7 @@ func (c *InboundDetourConfig) Build() (*core.InboundHandlerConfig, error) { // TUN inbound doesn't need port configuration as it uses network interface instead if strings.ToLower(c.Protocol) == "tun" { // Skip port validation for TUN - } else if c.ListenOn == nil { + } else if c.ListenOn == nil || len(c.ListenOn.String()) == 0 { // Listen on anyip, must set PortList if c.PortList == nil { return nil, errors.New("Listen on AnyIP but no Port(s) set in InboundDetour.") @@ -251,13 +251,13 @@ func validateOutboundTransportSecurity(rawConfig interface{}, senderSettings *pr if vlessCfg.Encryption != "" && vlessCfg.Encryption != "none" { return nil } - if requiresTransportSecurity(vlessCfg.Address) { + if requiresTransportSecurity(vlessCfg.Vnext[0].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.Servers[0].Address) { return errors.New("trojan without TLS is prohibited unless the server address is a private IP or domain") } } @@ -358,13 +358,13 @@ func (c *OutboundDetourConfig) Build() (*core.OutboundHandlerConfig, error) { if err != nil { return nil, errors.New("failed to load outbound detour config for protocol ", c.Protocol).Base(err) } - if err := validateOutboundTransportSecurity(rawConfig, senderSettings); err != nil { - return nil, err - } ts, err := rawConfig.(Buildable).Build() if err != nil { return nil, errors.New("failed to build outbound handler for protocol ", c.Protocol).Base(err) } + if err := validateOutboundTransportSecurity(rawConfig, senderSettings); err != nil { + return nil, err + } return &core.OutboundHandlerConfig{ SenderSettings: serial.ToTypedMessage(senderSettings), diff --git a/proxy/http/client.go b/proxy/http/client.go index 0e50edba..e82a747f 100644 --- a/proxy/http/client.go +++ b/proxy/http/client.go @@ -173,15 +173,18 @@ func fillRequestHeader(ctx context.Context, header []*Header) ([]*Header, error) outbounds := session.OutboundsFromContext(ctx) ob := outbounds[len(outbounds)-1] - if inbound == nil || ob == nil { - return nil, errors.New("missing inbound or outbound metadata from context") + var src net.Destination + if inbound != nil { + src = inbound.Source + } else { + src = net.TCPDestination(net.AnyIP, 0) } data := struct { Source net.Destination Target net.Destination }{ - Source: inbound.Source, + Source: src, Target: ob.Target, } diff --git a/transport/internet/tcp/hub.go b/transport/internet/tcp/hub.go index ede97499..c68d55dd 100644 --- a/transport/internet/tcp/hub.go +++ b/transport/internet/tcp/hub.go @@ -42,9 +42,6 @@ func ListenTCP(ctx context.Context, address net.Address, port net.Port, streamSe var listener net.Listener var err error if port == net.Port(0) { // unix - if !address.Family().IsDomain() { - return nil, errors.New("invalid unix listen: ", address).AtError() - } listener, err = internet.ListenSystem(ctx, &net.UnixAddr{ Name: address.Domain(), Net: "unix", diff --git a/transport/internet/tcp_hub.go b/transport/internet/tcp_hub.go index cf0ad80d..31183368 100644 --- a/transport/internet/tcp_hub.go +++ b/transport/internet/tcp_hub.go @@ -65,6 +65,9 @@ func ListenTCP(ctx context.Context, address net.Address, port net.Port, settings if address.Family().IsDomain() { return nil, errors.New("domain address is not allowed for listening: ", address.Domain()) } + if port == 0 { + return nil, errors.New("port 0 is not allowed for listening on TCP") + } protocol := settings.ProtocolName listenFunc := transportListenerCache[protocol]