router.go 4.1 KB

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