router.go 3.7 KB

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