Fix fakeip routing

This commit is contained in:
世界 2023-06-10 16:26:40 +08:00
parent 0cb9cff690
commit 52e9059a8d
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 38 additions and 18 deletions

View file

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

View file

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

View file

@ -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)
}

View file

@ -44,23 +44,28 @@ 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
}
}
r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour)
}
}
if domainStrategy, dsLoaded := r.transportDomainStrategy[r.defaultTransport]; dsLoaded {
return ctx, r.defaultTransport, domainStrategy

View file

@ -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
}