router.go 2.0 KB

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