mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 08:31:30 +00:00
Fix fakeip routing
This commit is contained in:
parent
0cb9cff690
commit
52e9059a8d
|
@ -21,3 +21,8 @@ type FakeIPStorage interface {
|
||||||
FakeIPLoad(address netip.Addr) (string, bool)
|
FakeIPLoad(address netip.Addr) (string, bool)
|
||||||
FakeIPReset() error
|
FakeIPReset() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FakeIPTransport interface {
|
||||||
|
dns.Transport
|
||||||
|
Store() FakeIPStore
|
||||||
|
}
|
||||||
|
|
|
@ -46,6 +46,7 @@ type InboundContext struct {
|
||||||
SourceGeoIPCode string
|
SourceGeoIPCode string
|
||||||
GeoIPCode string
|
GeoIPCode string
|
||||||
ProcessInfo *process.Info
|
ProcessInfo *process.Info
|
||||||
|
FakeIP bool
|
||||||
|
|
||||||
// dns cache
|
// dns cache
|
||||||
|
|
||||||
|
|
|
@ -629,6 +629,7 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad
|
||||||
Fqdn: domain,
|
Fqdn: domain,
|
||||||
Port: metadata.Destination.Port,
|
Port: metadata.Destination.Port,
|
||||||
}
|
}
|
||||||
|
metadata.FakeIP = true
|
||||||
r.logger.DebugContext(ctx, "found fakeip domain: ", domain)
|
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,
|
Fqdn: domain,
|
||||||
Port: metadata.Destination.Port,
|
Port: metadata.Destination.Port,
|
||||||
}
|
}
|
||||||
|
metadata.FakeIP = true
|
||||||
r.logger.DebugContext(ctx, "found fakeip domain: ", domain)
|
r.logger.DebugContext(ctx, "found fakeip domain: ", domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,22 +44,27 @@ func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport,
|
||||||
}
|
}
|
||||||
for i, rule := range r.dnsRules {
|
for i, rule := range r.dnsRules {
|
||||||
if rule.Match(metadata) {
|
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() {
|
if rule.DisableCache() {
|
||||||
ctx = dns.ContextWithDisableCache(ctx, true)
|
ctx = dns.ContextWithDisableCache(ctx, true)
|
||||||
}
|
}
|
||||||
if rewriteTTL := rule.RewriteTTL(); rewriteTTL != nil {
|
if rewriteTTL := rule.RewriteTTL(); rewriteTTL != nil {
|
||||||
ctx = dns.ContextWithRewriteTTL(ctx, *rewriteTTL)
|
ctx = dns.ContextWithRewriteTTL(ctx, *rewriteTTL)
|
||||||
}
|
}
|
||||||
detour := rule.Outbound()
|
if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded {
|
||||||
r.dnsLogger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour)
|
return ctx, transport, domainStrategy
|
||||||
if transport, loaded := r.transportMap[detour]; loaded {
|
} else {
|
||||||
if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded {
|
return ctx, transport, r.defaultDomainStrategy
|
||||||
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 {
|
if domainStrategy, dsLoaded := r.transportDomainStrategy[r.defaultTransport]; dsLoaded {
|
||||||
|
|
|
@ -14,13 +14,16 @@ import (
|
||||||
mDNS "github.com/miekg/dns"
|
mDNS "github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ dns.Transport = (*Server)(nil)
|
var (
|
||||||
|
_ dns.Transport = (*Transport)(nil)
|
||||||
|
_ adapter.FakeIPTransport = (*Transport)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
dns.RegisterTransport([]string{"fakeip"}, NewTransport)
|
dns.RegisterTransport([]string{"fakeip"}, NewTransport)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server struct {
|
type Transport struct {
|
||||||
name string
|
name string
|
||||||
router adapter.Router
|
router adapter.Router
|
||||||
store adapter.FakeIPStore
|
store adapter.FakeIPStore
|
||||||
|
@ -32,18 +35,18 @@ func NewTransport(name string, ctx context.Context, logger logger.ContextLogger,
|
||||||
if router == nil {
|
if router == nil {
|
||||||
return nil, E.New("missing router in context")
|
return nil, E.New("missing router in context")
|
||||||
}
|
}
|
||||||
return &Server{
|
return &Transport{
|
||||||
name: name,
|
name: name,
|
||||||
router: router,
|
router: router,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Name() string {
|
func (s *Transport) Name() string {
|
||||||
return s.name
|
return s.name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Start() error {
|
func (s *Transport) Start() error {
|
||||||
s.store = s.router.FakeIPStore()
|
s.store = s.router.FakeIPStore()
|
||||||
if s.store == nil {
|
if s.store == nil {
|
||||||
return E.New("fakeip not enabled")
|
return E.New("fakeip not enabled")
|
||||||
|
@ -51,19 +54,19 @@ func (s *Server) Start() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Close() error {
|
func (s *Transport) Close() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Raw() bool {
|
func (s *Transport) Raw() bool {
|
||||||
return false
|
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
|
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
|
var addresses []netip.Addr
|
||||||
if strategy != dns.DomainStrategyUseIPv6 {
|
if strategy != dns.DomainStrategyUseIPv6 {
|
||||||
inet4Address, err := s.store.Create(domain, dns.DomainStrategyUseIPv4)
|
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
|
return addresses, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Transport) Store() adapter.FakeIPStore {
|
||||||
|
return s.store
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue