github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/adapter/router.go (about)

     1  package adapter
     2  
     3  import (
     4  	"context"
     5  	"github.com/inazumav/sing-box/option"
     6  	"net"
     7  	"net/netip"
     8  
     9  	"github.com/inazumav/sing-box/common/geoip"
    10  	"github.com/sagernet/sing-dns"
    11  	"github.com/sagernet/sing-tun"
    12  	"github.com/sagernet/sing/common/control"
    13  	N "github.com/sagernet/sing/common/network"
    14  	"github.com/sagernet/sing/service"
    15  
    16  	mdns "github.com/miekg/dns"
    17  )
    18  
    19  type Router interface {
    20  	Service
    21  
    22  	Outbounds() []Outbound
    23  	Outbound(tag string) (Outbound, bool)
    24  	DefaultOutbound(network string) Outbound
    25  
    26  	FakeIPStore() FakeIPStore
    27  
    28  	RouteConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
    29  	RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
    30  
    31  	GeoIPReader() *geoip.Reader
    32  	LoadGeosite(code string) (Rule, error)
    33  
    34  	Exchange(ctx context.Context, message *mdns.Msg) (*mdns.Msg, error)
    35  	Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error)
    36  	LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error)
    37  	ClearDNSCache()
    38  
    39  	InterfaceFinder() control.InterfaceFinder
    40  	UpdateInterfaces() error
    41  	DefaultInterface() string
    42  	AutoDetectInterface() bool
    43  	AutoDetectInterfaceFunc() control.Func
    44  	DefaultMark() int
    45  	NetworkMonitor() tun.NetworkUpdateMonitor
    46  	InterfaceMonitor() tun.DefaultInterfaceMonitor
    47  	PackageManager() tun.PackageManager
    48  	Rules() []Rule
    49  
    50  	ClashServer() ClashServer
    51  	SetClashServer(server ClashServer)
    52  
    53  	V2RayServer() V2RayServer
    54  	SetV2RayServer(server V2RayServer)
    55  	ResetNetwork() error
    56  
    57  	// for v2bx
    58  	AddInbound(in Inbound) error
    59  	DelInbound(tag string) error
    60  	UpdateDnsRules(rules []option.DNSRule) error
    61  }
    62  
    63  func ContextWithRouter(ctx context.Context, router Router) context.Context {
    64  	return service.ContextWith(ctx, router)
    65  }
    66  
    67  func RouterFromContext(ctx context.Context) Router {
    68  	return service.FromContext[Router](ctx)
    69  }
    70  
    71  type Rule interface {
    72  	Service
    73  	Type() string
    74  	UpdateGeosite() error
    75  	Match(metadata *InboundContext) bool
    76  	Outbound() string
    77  	String() string
    78  }
    79  
    80  type DNSRule interface {
    81  	Rule
    82  	DisableCache() bool
    83  	RewriteTTL() *uint32
    84  }
    85  
    86  type InterfaceUpdateListener interface {
    87  	InterfaceUpdated()
    88  }