From b9ad71ca60991896063d569e51da1a71d2208bca Mon Sep 17 00:00:00 2001 From: Cuthead <183076078+Cuthead@users.noreply.github.com> Date: Thu, 6 Aug 2026 12:56:53 +0800 Subject: [PATCH 1/2] Prefer IP rule over domain rule when SNI has no explicit match TProxy/redirect IP-based inbound previously let any TLS SNI sniff override the IP-matched outbound, collapsing to DefaultOutbound (or disconnecting) when the SNI had no explicit domain rule. Domain rules now take priority when they explicitly match; otherwise IP-inbound connections fall back to the outbound chosen by GetOutboundByIP instead of being overridden. Co-Authored-By: Claude Sonnet 5 --- phantomtcp/phantom.go | 21 +++++++++++++++++++++ phantomtcp/proxy.go | 8 +++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/phantomtcp/phantom.go b/phantomtcp/phantom.go index 7bcc6ad..8c7158c 100755 --- a/phantomtcp/phantom.go +++ b/phantomtcp/phantom.go @@ -190,6 +190,27 @@ func (profile *PhantomProfile) GetOutbound(name string) (*Outbound, int) { return DefaultOutbound, 0 } +func (profile *PhantomProfile) HasDomainRule(name string) bool { + if _, ok := profile.DomainMap[name]; ok { + return true + } + + offset := 0 + for i := 0; i < SubdomainDepth; i++ { + off := strings.Index(name[offset:], ".") + if off == -1 { + break + } + offset += off + if _, ok := profile.DomainMap[name[offset:]]; ok { + return true + } + offset++ + } + + return false +} + func (profile *PhantomProfile) GetOutboundByIP(ip net.IP) *Outbound { ip4 := ip.To4() if ip4 != nil { diff --git a/phantomtcp/proxy.go b/phantomtcp/proxy.go index a290aba..305222a 100755 --- a/phantomtcp/proxy.go +++ b/phantomtcp/proxy.go @@ -218,7 +218,13 @@ func tcp_redirect(client net.Conn, addr *net.TCPAddr, domain string, header []by if domain != sni { if ech { logPrintln(2, domain, "tls hello with ECH", sni) - } else { + } else if DefaultProfile.HasDomainRule(sni) { + outbound, _ = DefaultProfile.GetOutbound(sni) + if outbound == nil { + return + } + domain = sni + } else if addr.IP == nil { outbound, _ = DefaultProfile.GetOutbound(sni) if outbound == nil { return From c9db405796c0bb0bcdc6495a75ea610c1c842aff Mon Sep 17 00:00:00 2001 From: Cuthead <183076078+Cuthead@users.noreply.github.com> Date: Thu, 6 Aug 2026 15:59:51 +0000 Subject: [PATCH 2/2] simplify --- phantomtcp/phantom.go | 21 --------------------- phantomtcp/proxy.go | 6 ------ 2 files changed, 27 deletions(-) diff --git a/phantomtcp/phantom.go b/phantomtcp/phantom.go index 8c7158c..7bcc6ad 100755 --- a/phantomtcp/phantom.go +++ b/phantomtcp/phantom.go @@ -190,27 +190,6 @@ func (profile *PhantomProfile) GetOutbound(name string) (*Outbound, int) { return DefaultOutbound, 0 } -func (profile *PhantomProfile) HasDomainRule(name string) bool { - if _, ok := profile.DomainMap[name]; ok { - return true - } - - offset := 0 - for i := 0; i < SubdomainDepth; i++ { - off := strings.Index(name[offset:], ".") - if off == -1 { - break - } - offset += off - if _, ok := profile.DomainMap[name[offset:]]; ok { - return true - } - offset++ - } - - return false -} - func (profile *PhantomProfile) GetOutboundByIP(ip net.IP) *Outbound { ip4 := ip.To4() if ip4 != nil { diff --git a/phantomtcp/proxy.go b/phantomtcp/proxy.go index 305222a..4a89bc5 100755 --- a/phantomtcp/proxy.go +++ b/phantomtcp/proxy.go @@ -218,12 +218,6 @@ func tcp_redirect(client net.Conn, addr *net.TCPAddr, domain string, header []by if domain != sni { if ech { logPrintln(2, domain, "tls hello with ECH", sni) - } else if DefaultProfile.HasDomainRule(sni) { - outbound, _ = DefaultProfile.GetOutbound(sni) - if outbound == nil { - return - } - domain = sni } else if addr.IP == nil { outbound, _ = DefaultProfile.GetOutbound(sni) if outbound == nil {