router.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. M "github.com/sagernet/sing/common/metadata"
  12. N "github.com/sagernet/sing/common/network"
  13. "github.com/sagernet/sing/common/x/list"
  14. mdns "github.com/miekg/dns"
  15. "go4.org/netipx"
  16. )
  17. type Router interface {
  18. Lifecycle
  19. FakeIPStore() FakeIPStore
  20. ConnectionRouter
  21. PreMatch(metadata InboundContext) error
  22. ConnectionRouterEx
  23. GeoIPReader() *geoip.Reader
  24. LoadGeosite(code string) (Rule, error)
  25. RuleSet(tag string) (RuleSet, bool)
  26. NeedWIFIState() bool
  27. Exchange(ctx context.Context, message *mdns.Msg) (*mdns.Msg, error)
  28. Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error)
  29. LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error)
  30. ClearDNSCache()
  31. Rules() []Rule
  32. SetTracker(tracker ConnectionTracker)
  33. ResetNetwork()
  34. }
  35. type ConnectionTracker interface {
  36. RoutedConnection(ctx context.Context, conn net.Conn, metadata InboundContext, matchedRule Rule, matchOutbound Outbound) net.Conn
  37. RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext, matchedRule Rule, matchOutbound Outbound) N.PacketConn
  38. }
  39. // Deprecated: Use ConnectionRouterEx instead.
  40. type ConnectionRouter interface {
  41. RouteConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
  42. RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
  43. }
  44. type ConnectionRouterEx interface {
  45. ConnectionRouter
  46. RouteConnectionEx(ctx context.Context, conn net.Conn, metadata InboundContext, onClose N.CloseHandlerFunc)
  47. RoutePacketConnectionEx(ctx context.Context, conn N.PacketConn, metadata InboundContext, onClose N.CloseHandlerFunc)
  48. }
  49. type RuleSet interface {
  50. Name() string
  51. StartContext(ctx context.Context, startContext *HTTPStartContext) error
  52. PostStart() error
  53. Metadata() RuleSetMetadata
  54. ExtractIPSet() []*netipx.IPSet
  55. IncRef()
  56. DecRef()
  57. Cleanup()
  58. RegisterCallback(callback RuleSetUpdateCallback) *list.Element[RuleSetUpdateCallback]
  59. UnregisterCallback(element *list.Element[RuleSetUpdateCallback])
  60. Close() error
  61. HeadlessRule
  62. }
  63. type RuleSetUpdateCallback func(it RuleSet)
  64. type RuleSetMetadata struct {
  65. ContainsProcessRule bool
  66. ContainsWIFIRule bool
  67. ContainsIPCIDRRule bool
  68. }
  69. type HTTPStartContext struct {
  70. access sync.Mutex
  71. httpClientCache map[string]*http.Client
  72. }
  73. func NewHTTPStartContext() *HTTPStartContext {
  74. return &HTTPStartContext{
  75. httpClientCache: make(map[string]*http.Client),
  76. }
  77. }
  78. func (c *HTTPStartContext) HTTPClient(detour string, dialer N.Dialer) *http.Client {
  79. c.access.Lock()
  80. defer c.access.Unlock()
  81. if httpClient, loaded := c.httpClientCache[detour]; loaded {
  82. return httpClient
  83. }
  84. httpClient := &http.Client{
  85. Transport: &http.Transport{
  86. ForceAttemptHTTP2: true,
  87. TLSHandshakeTimeout: C.TCPTimeout,
  88. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  89. return dialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
  90. },
  91. },
  92. }
  93. c.httpClientCache[detour] = httpClient
  94. return httpClient
  95. }
  96. func (c *HTTPStartContext) Close() {
  97. c.access.Lock()
  98. defer c.access.Unlock()
  99. for _, client := range c.httpClientCache {
  100. client.CloseIdleConnections()
  101. }
  102. }