router.go 1.9 KB

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