github.com/sagernet/sing-box@v1.9.0-rc.20/adapter/router.go (about)

     1  package adapter
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/netip"
     7  
     8  	"github.com/sagernet/sing-box/common/geoip"
     9  	"github.com/sagernet/sing-dns"
    10  	"github.com/sagernet/sing-tun"
    11  	"github.com/sagernet/sing/common/control"
    12  	N "github.com/sagernet/sing/common/network"
    13  	"github.com/sagernet/sing/service"
    14  
    15  	mdns "github.com/miekg/dns"
    16  )
    17  
    18  type Router interface {
    19  	Service
    20  	PreStarter
    21  	PostStarter
    22  
    23  	Outbounds() []Outbound
    24  	Outbound(tag string) (Outbound, bool)
    25  	DefaultOutbound(network string) (Outbound, error)
    26  
    27  	FakeIPStore() FakeIPStore
    28  
    29  	ConnectionRouter
    30  
    31  	GeoIPReader() *geoip.Reader
    32  	LoadGeosite(code string) (Rule, error)
    33  
    34  	RuleSet(tag string) (RuleSet, bool)
    35  
    36  	NeedWIFIState() bool
    37  
    38  	Exchange(ctx context.Context, message *mdns.Msg) (*mdns.Msg, error)
    39  	Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error)
    40  	LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error)
    41  	ClearDNSCache()
    42  
    43  	InterfaceFinder() control.InterfaceFinder
    44  	UpdateInterfaces() error
    45  	DefaultInterface() string
    46  	AutoDetectInterface() bool
    47  	AutoDetectInterfaceFunc() control.Func
    48  	DefaultMark() int
    49  	NetworkMonitor() tun.NetworkUpdateMonitor
    50  	InterfaceMonitor() tun.DefaultInterfaceMonitor
    51  	PackageManager() tun.PackageManager
    52  	WIFIState() WIFIState
    53  	Rules() []Rule
    54  
    55  	ClashServer() ClashServer
    56  	SetClashServer(server ClashServer)
    57  
    58  	V2RayServer() V2RayServer
    59  	SetV2RayServer(server V2RayServer)
    60  
    61  	ResetNetwork() error
    62  }
    63  
    64  func ContextWithRouter(ctx context.Context, router Router) context.Context {
    65  	return service.ContextWith(ctx, router)
    66  }
    67  
    68  func RouterFromContext(ctx context.Context) Router {
    69  	return service.FromContext[Router](ctx)
    70  }
    71  
    72  type HeadlessRule interface {
    73  	Match(metadata *InboundContext) bool
    74  	String() string
    75  }
    76  
    77  type Rule interface {
    78  	HeadlessRule
    79  	Service
    80  	Type() string
    81  	UpdateGeosite() error
    82  	Outbound() string
    83  }
    84  
    85  type DNSRule interface {
    86  	Rule
    87  	DisableCache() bool
    88  	RewriteTTL() *uint32
    89  	ClientSubnet() *netip.Prefix
    90  	WithAddressLimit() bool
    91  	MatchAddressLimit(metadata *InboundContext) bool
    92  }
    93  
    94  type RuleSet interface {
    95  	StartContext(ctx context.Context, startContext RuleSetStartContext) error
    96  	PostStart() error
    97  	Metadata() RuleSetMetadata
    98  	Close() error
    99  	HeadlessRule
   100  }
   101  
   102  type RuleSetMetadata struct {
   103  	ContainsProcessRule bool
   104  	ContainsWIFIRule    bool
   105  	ContainsIPCIDRRule  bool
   106  }
   107  
   108  type RuleSetStartContext interface {
   109  	HTTPClient(detour string, dialer N.Dialer) *http.Client
   110  	Close()
   111  }
   112  
   113  type InterfaceUpdateListener interface {
   114  	InterfaceUpdated()
   115  }
   116  
   117  type WIFIState struct {
   118  	SSID  string
   119  	BSSID string
   120  }