From 52e9059a8d8b63b834feeac092286b78cac10689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 10 Jun 2023 16:26:40 +0800 Subject: [PATCH] Fix fakeip routing --- adapter/fakeip.go | 5 +++++ adapter/inbound.go | 1 + route/router.go | 2 ++ route/router_dns.go | 23 ++++++++++++++--------- transport/fakeip/server.go | 25 ++++++++++++++++--------- 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/adapter/fakeip.go b/adapter/fakeip.go index 6153f8ce..6854bebc 100644 --- a/adapter/fakeip.go +++ b/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 +} diff --git a/adapter/inbound.go b/adapter/inbound.go index 356a3200..6a566dc2 100644 --- a/adapter/inbound.go +++ b/adapter/inbound.go @@ -46,6 +46,7 @@ type InboundContext struct { SourceGeoIPCode string GeoIPCode string ProcessInfo *process.Info + FakeIP bool // dns cache diff --git a/route/router.go b/route/router.go index 0bf1f6d9..84a7050d 100644 --- a/route/router.go +++ b/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) } diff --git a/route/router_dns.go b/route/router_dns.go index 0b693371..0fe37352 100644 --- a/route/router_dns.go +++ b/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 { diff --git a/transport/fakeip/server.go b/transport/fakeip/server.go index 058a2192..9247c4f6 100644 --- a/transport/fakeip/server.go +++ b/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 +}