router.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package adapter
  2. import (
  3. "context"
  4. "net"
  5. "net/http"
  6. "net/netip"
  7. "sync"
  8. "github.com/sagernet/sing-box/common/geoip"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing-dns"
  11. "github.com/sagernet/sing-tun"
  12. "github.com/sagernet/sing/common/control"
  13. M "github.com/sagernet/sing/common/metadata"
  14. N "github.com/sagernet/sing/common/network"
  15. "github.com/sagernet/sing/common/x/list"
  16. "github.com/sagernet/sing/service"
  17. mdns "github.com/miekg/dns"
  18. "go4.org/netipx"
  19. )
  20. type Router interface {
  21. Service
  22. PreStarter
  23. PostStarter
  24. Cleanup() error
  25. Outbounds() []Outbound
  26. Outbound(tag string) (Outbound, bool)
  27. DefaultOutbound(network string) (Outbound, error)
  28. FakeIPStore() FakeIPStore
  29. ConnectionRouter
  30. ConnectionRouterEx
  31. GeoIPReader() *geoip.Reader
  32. LoadGeosite(code string) (Rule, error)
  33. RuleSet(tag string) (RuleSet, bool)
  34. NeedWIFIState() bool
  35. Exchange(ctx context.Context, message *mdns.Msg) (*mdns.Msg, error)
  36. Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error)
  37. LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error)
  38. ClearDNSCache()
  39. InterfaceFinder() control.InterfaceFinder
  40. UpdateInterfaces() error
  41. DefaultInterface() string
  42. AutoDetectInterface() bool
  43. AutoDetectInterfaceFunc() control.Func
  44. DefaultMark() uint32
  45. RegisterAutoRedirectOutputMark(mark uint32) error
  46. AutoRedirectOutputMark() uint32
  47. NetworkMonitor() tun.NetworkUpdateMonitor
  48. InterfaceMonitor() tun.DefaultInterfaceMonitor
  49. PackageManager() tun.PackageManager
  50. WIFIState() WIFIState
  51. Rules() []Rule
  52. ClashServer() ClashServer
  53. SetClashServer(server ClashServer)
  54. V2RayServer() V2RayServer
  55. SetV2RayServer(server V2RayServer)
  56. ResetNetwork() error
  57. }
  58. // Deprecated: Use ConnectionRouterEx instead.
  59. type ConnectionRouter interface {
  60. RouteConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
  61. RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
  62. }
  63. type ConnectionRouterEx interface {
  64. ConnectionRouter
  65. RouteConnectionEx(ctx context.Context, conn net.Conn, metadata InboundContext, onClose N.CloseHandlerFunc)
  66. RoutePacketConnectionEx(ctx context.Context, conn N.PacketConn, metadata InboundContext, onClose N.CloseHandlerFunc)
  67. }
  68. func ContextWithRouter(ctx context.Context, router Router) context.Context {
  69. return service.ContextWith(ctx, router)
  70. }
  71. func RouterFromContext(ctx context.Context) Router {
  72. return service.FromContext[Router](ctx)
  73. }
  74. type RuleSet interface {
  75. Name() string
  76. StartContext(ctx context.Context, startContext *HTTPStartContext) error
  77. PostStart() error
  78. Metadata() RuleSetMetadata
  79. ExtractIPSet() []*netipx.IPSet
  80. IncRef()
  81. DecRef()
  82. Cleanup()
  83. RegisterCallback(callback RuleSetUpdateCallback) *list.Element[RuleSetUpdateCallback]
  84. UnregisterCallback(element *list.Element[RuleSetUpdateCallback])
  85. Close() error
  86. HeadlessRule
  87. }
  88. type RuleSetUpdateCallback func(it RuleSet)
  89. type RuleSetMetadata struct {
  90. ContainsProcessRule bool
  91. ContainsWIFIRule bool
  92. ContainsIPCIDRRule bool
  93. }
  94. type HTTPStartContext struct {
  95. access sync.Mutex
  96. httpClientCache map[string]*http.Client
  97. }
  98. func NewHTTPStartContext() *HTTPStartContext {
  99. return &HTTPStartContext{
  100. httpClientCache: make(map[string]*http.Client),
  101. }
  102. }
  103. func (c *HTTPStartContext) HTTPClient(detour string, dialer N.Dialer) *http.Client {
  104. c.access.Lock()
  105. defer c.access.Unlock()
  106. if httpClient, loaded := c.httpClientCache[detour]; loaded {
  107. return httpClient
  108. }
  109. httpClient := &http.Client{
  110. Transport: &http.Transport{
  111. ForceAttemptHTTP2: true,
  112. TLSHandshakeTimeout: C.TCPTimeout,
  113. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  114. return dialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
  115. },
  116. },
  117. }
  118. c.httpClientCache[detour] = httpClient
  119. return httpClient
  120. }
  121. func (c *HTTPStartContext) Close() {
  122. c.access.Lock()
  123. defer c.access.Unlock()
  124. for _, client := range c.httpClientCache {
  125. client.CloseIdleConnections()
  126. }
  127. }
  128. type InterfaceUpdateListener interface {
  129. InterfaceUpdated()
  130. }
  131. type WIFIState struct {
  132. SSID string
  133. BSSID string
  134. }