From 024152115cdc13f0921c05bc2dd6dd1b9ac496ae Mon Sep 17 00:00:00 2001 From: i40e <120920147+i40e@users.noreply.github.com> Date: Fri, 20 Nov 2026 07:44:34 +0800 Subject: [PATCH] feat: allow disable redir-host mapping in fake-ip mode (#3496) When a real IP is returned from fake-ip-filter, subsequent connection toward the real IP will be mapped back to domain (redir-host behavior). For configurations with a small number of filtered real IP, the real IP returned is functionally equivalent to a fake IP. Add a configuration option to disable the redir-host behavior in fake IP mode. --- config/config.go | 3 +++ dns/enhancer.go | 33 ++++++++++++++++++--------------- dns/middleware.go | 4 +++- hub/executor/executor.go | 15 ++++++++------- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/config/config.go b/config/config.go index cc8c1016..b8696a94 100644 --- a/config/config.go +++ b/config/config.go @@ -168,6 +168,7 @@ type DNS struct { FakeIPPool6 *fakeip.Pool FakeIPSkipper *fakeip.Skipper FakeIPTTL int + FakeIPDisableMapping bool NameServerPolicy []dns.Policy ProxyServerNameserver []dns.NameServer ProxyServerPolicy []dns.Policy @@ -237,6 +238,7 @@ type RawDNS struct { FakeIPRange6 string `yaml:"fake-ip-range6" json:"fake-ip-range6"` FakeIPFilter []string `yaml:"fake-ip-filter" json:"fake-ip-filter"` FakeIPFilterMode C.FilterMode `yaml:"fake-ip-filter-mode" json:"fake-ip-filter-mode"` + FakeIPDisableMapping bool `yaml:"fake-ip-disable-mapping" json:"fake-ip-disable-mapping"` FakeIPTTL int `yaml:"fake-ip-ttl" json:"fake-ip-ttl"` DefaultNameserver []string `yaml:"default-nameserver" json:"default-nameserver"` CacheAlgorithm string `yaml:"cache-algorithm" json:"cache-algorithm"` @@ -1538,6 +1540,7 @@ func parseDNS(rawCfg *RawConfig, ruleProviders map[string]P.RuleProvider) (*DNS, dnsCfg.FakeIPSkipper = skipper dnsCfg.FakeIPTTL = cfg.FakeIPTTL + dnsCfg.FakeIPDisableMapping = cfg.FakeIPDisableMapping if dnsCfg.FakeIPRange.IsValid() { pool, err := fakeip.New(fakeip.Options{ diff --git a/dns/enhancer.go b/dns/enhancer.go index 9cf3e194..ab097a02 100644 --- a/dns/enhancer.go +++ b/dns/enhancer.go @@ -10,14 +10,15 @@ import ( ) type ResolverEnhancer struct { - ipv6 bool - mode C.DNSMode - fakeIPPool *fakeip.Pool - fakeIPPool6 *fakeip.Pool - fakeIPSkipper *fakeip.Skipper - fakeIPTTL int - mapping *lru.LruCache[netip.Addr, string] - useHosts bool + ipv6 bool + mode C.DNSMode + fakeIPPool *fakeip.Pool + fakeIPPool6 *fakeip.Pool + fakeIPSkipper *fakeip.Skipper + fakeIPTTL int + fakeIPDisableMapping bool + mapping *lru.LruCache[netip.Addr, string] + useHosts bool } func (h *ResolverEnhancer) FakeIPEnabled() bool { @@ -159,13 +160,14 @@ func (h *ResolverEnhancer) StoreFakePoolState() { } type EnhancerConfig struct { - IPv6 bool - EnhancedMode C.DNSMode - FakeIPPool *fakeip.Pool - FakeIPPool6 *fakeip.Pool - FakeIPSkipper *fakeip.Skipper - FakeIPTTL int - UseHosts bool + IPv6 bool + EnhancedMode C.DNSMode + FakeIPPool *fakeip.Pool + FakeIPPool6 *fakeip.Pool + FakeIPSkipper *fakeip.Skipper + FakeIPTTL int + FakeIPDisableMapping bool + UseHosts bool } func NewEnhancer(cfg EnhancerConfig) *ResolverEnhancer { @@ -182,6 +184,7 @@ func NewEnhancer(cfg EnhancerConfig) *ResolverEnhancer { } e.fakeIPSkipper = cfg.FakeIPSkipper e.fakeIPTTL = cfg.FakeIPTTL + e.fakeIPDisableMapping = cfg.FakeIPDisableMapping if e.fakeIPTTL < 1 { e.fakeIPTTL = 1 } diff --git a/dns/middleware.go b/dns/middleware.go index fa6cb68b..cdf95c82 100644 --- a/dns/middleware.go +++ b/dns/middleware.go @@ -237,11 +237,13 @@ func newHandler(resolver resolver.Resolver, mapper *ResolverEnhancer) handler { middlewares = append(middlewares, withHosts(mapper.mapping)) } + disableMapping := false if mapper.mode == C.DNSFakeIP { middlewares = append(middlewares, withFakeIP(mapper.fakeIPSkipper, mapper.fakeIPPool, mapper.fakeIPPool6, mapper.fakeIPTTL)) + disableMapping = mapper.fakeIPDisableMapping } - if mapper.mode != C.DNSNormal { + if mapper.mode != C.DNSNormal && !disableMapping { middlewares = append(middlewares, withMapping(mapper.mapping)) } diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 796face2..9c32b499 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -265,13 +265,14 @@ func updateDNS(c *config.DNS, generalIPv6 bool) { CacheMaxSize: c.CacheMaxSize, }) m := dns.NewEnhancer(dns.EnhancerConfig{ - IPv6: ipv6, - EnhancedMode: c.EnhancedMode, - FakeIPPool: c.FakeIPPool, - FakeIPPool6: c.FakeIPPool6, - FakeIPSkipper: c.FakeIPSkipper, - FakeIPTTL: c.FakeIPTTL, - UseHosts: c.UseHosts, + IPv6: ipv6, + EnhancedMode: c.EnhancedMode, + FakeIPPool: c.FakeIPPool, + FakeIPPool6: c.FakeIPPool6, + FakeIPSkipper: c.FakeIPSkipper, + FakeIPTTL: c.FakeIPTTL, + UseHosts: c.UseHosts, + FakeIPDisableMapping: c.FakeIPDisableMapping, }) // reuse cache of old host mapper