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

     1  package adapter
     2  
     3  import (
     4  	"context"
     5  	"net"
     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  
    14  	mdns "github.com/miekg/dns"
    15  )
    16  
    17  type Router interface {
    18  	Service
    19  
    20  	Outbounds() []Outbound
    21  	Outbound(tag string) (Outbound, bool)
    22  	DefaultOutbound(network string) Outbound
    23  
    24  	RouteConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
    25  	RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
    26  
    27  	GeoIPReader() *geoip.Reader
    28  	LoadGeosite(code string) (Rule, error)
    29  
    30  	Exchange(ctx context.Context, message *mdns.Msg) (*mdns.Msg, error)
    31  	Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error)
    32  	LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error)
    33  
    34  	InterfaceFinder() control.InterfaceFinder
    35  	UpdateInterfaces() error
    36  	DefaultInterface() string
    37  	AutoDetectInterface() bool
    38  	AutoDetectInterfaceFunc() control.Func
    39  	DefaultMark() int
    40  	NetworkMonitor() tun.NetworkUpdateMonitor
    41  	InterfaceMonitor() tun.DefaultInterfaceMonitor
    42  	PackageManager() tun.PackageManager
    43  	Rules() []Rule
    44  
    45  	TimeService
    46  
    47  	ClashServer() ClashServer
    48  	SetClashServer(server ClashServer)
    49  
    50  	V2RayServer() V2RayServer
    51  	SetV2RayServer(server V2RayServer)
    52  }
    53  
    54  type routerContextKey struct{}
    55  
    56  func ContextWithRouter(ctx context.Context, router Router) context.Context {
    57  	return context.WithValue(ctx, (*routerContextKey)(nil), router)
    58  }
    59  
    60  func RouterFromContext(ctx context.Context) Router {
    61  	metadata := ctx.Value((*routerContextKey)(nil))
    62  	if metadata == nil {
    63  		return nil
    64  	}
    65  	return metadata.(Router)
    66  }
    67  
    68  type Rule interface {
    69  	Service
    70  	Type() string
    71  	UpdateGeosite() error
    72  	Match(metadata *InboundContext) bool
    73  	Outbound() string
    74  	String() string
    75  }
    76  
    77  type DNSRule interface {
    78  	Rule
    79  	DisableCache() bool
    80  }
    81  
    82  type InterfaceUpdateListener interface {
    83  	InterfaceUpdated() error
    84  }