瀏覽代碼

Fix fakeip routing

世界 2 年之前
父節點
當前提交
52e9059a8d
共有 5 個文件被更改,包括 38 次插入18 次删除
  1. 5 0
      adapter/fakeip.go
  2. 1 0
      adapter/inbound.go
  3. 2 0
      route/router.go
  4. 14 9
      route/router_dns.go
  5. 16 9
      transport/fakeip/server.go

+ 5 - 0
adapter/fakeip.go

@@ -21,3 +21,8 @@ type FakeIPStorage interface {
 	FakeIPLoad(address netip.Addr) (string, bool)
 	FakeIPReset() error
 }
+
+type FakeIPTransport interface {
+	dns.Transport
+	Store() FakeIPStore
+}

+ 1 - 0
adapter/inbound.go

@@ -46,6 +46,7 @@ type InboundContext struct {
 	SourceGeoIPCode      string
 	GeoIPCode            string
 	ProcessInfo          *process.Info
+	FakeIP               bool
 
 	// dns cache
 

+ 2 - 0
route/router.go

@@ -629,6 +629,7 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad
 			Fqdn: domain,
 			Port: metadata.Destination.Port,
 		}
+		metadata.FakeIP = true
 		r.logger.DebugContext(ctx, "found fakeip domain: ", domain)
 	}
 
@@ -738,6 +739,7 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m
 			Fqdn: domain,
 			Port: metadata.Destination.Port,
 		}
+		metadata.FakeIP = true
 		r.logger.DebugContext(ctx, "found fakeip domain: ", domain)
 	}
 

+ 14 - 9
route/router_dns.go

@@ -44,22 +44,27 @@ func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport,
 	}
 	for i, rule := range r.dnsRules {
 		if rule.Match(metadata) {
+			detour := rule.Outbound()
+			transport, loaded := r.transportMap[detour]
+			if !loaded {
+				r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour)
+				continue
+			}
+			if _, isFakeIP := transport.(adapter.FakeIPTransport); isFakeIP && metadata.FakeIP {
+				continue
+			}
+			r.dnsLogger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour)
 			if rule.DisableCache() {
 				ctx = dns.ContextWithDisableCache(ctx, true)
 			}
 			if rewriteTTL := rule.RewriteTTL(); rewriteTTL != nil {
 				ctx = dns.ContextWithRewriteTTL(ctx, *rewriteTTL)
 			}
-			detour := rule.Outbound()
-			r.dnsLogger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour)
-			if transport, loaded := r.transportMap[detour]; loaded {
-				if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded {
-					return ctx, transport, domainStrategy
-				} else {
-					return ctx, transport, r.defaultDomainStrategy
-				}
+			if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded {
+				return ctx, transport, domainStrategy
+			} else {
+				return ctx, transport, r.defaultDomainStrategy
 			}
-			r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour)
 		}
 	}
 	if domainStrategy, dsLoaded := r.transportDomainStrategy[r.defaultTransport]; dsLoaded {

+ 16 - 9
transport/fakeip/server.go

@@ -14,13 +14,16 @@ import (
 	mDNS "github.com/miekg/dns"
 )
 
-var _ dns.Transport = (*Server)(nil)
+var (
+	_ dns.Transport           = (*Transport)(nil)
+	_ adapter.FakeIPTransport = (*Transport)(nil)
+)
 
 func init() {
 	dns.RegisterTransport([]string{"fakeip"}, NewTransport)
 }
 
-type Server struct {
+type Transport struct {
 	name   string
 	router adapter.Router
 	store  adapter.FakeIPStore
@@ -32,18 +35,18 @@ func NewTransport(name string, ctx context.Context, logger logger.ContextLogger,
 	if router == nil {
 		return nil, E.New("missing router in context")
 	}
-	return &Server{
+	return &Transport{
 		name:   name,
 		router: router,
 		logger: logger,
 	}, nil
 }
 
-func (s *Server) Name() string {
+func (s *Transport) Name() string {
 	return s.name
 }
 
-func (s *Server) Start() error {
+func (s *Transport) Start() error {
 	s.store = s.router.FakeIPStore()
 	if s.store == nil {
 		return E.New("fakeip not enabled")
@@ -51,19 +54,19 @@ func (s *Server) Start() error {
 	return nil
 }
 
-func (s *Server) Close() error {
+func (s *Transport) Close() error {
 	return nil
 }
 
-func (s *Server) Raw() bool {
+func (s *Transport) Raw() bool {
 	return false
 }
 
-func (s *Server) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
+func (s *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
 	return nil, os.ErrInvalid
 }
 
-func (s *Server) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {
+func (s *Transport) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {
 	var addresses []netip.Addr
 	if strategy != dns.DomainStrategyUseIPv6 {
 		inet4Address, err := s.store.Create(domain, dns.DomainStrategyUseIPv4)
@@ -81,3 +84,7 @@ func (s *Server) Lookup(ctx context.Context, domain string, strategy dns.DomainS
 	}
 	return addresses, nil
 }
+
+func (s *Transport) Store() adapter.FakeIPStore {
+	return s.store
+}