2022-06-30 21:27:56 +08:00
|
|
|
package adapter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-10-25 22:24:19 +08:00
|
|
|
"net"
|
2023-12-01 13:24:12 +08:00
|
|
|
"net/http"
|
2022-07-07 21:47:21 +08:00
|
|
|
"net/netip"
|
2024-10-25 22:24:19 +08:00
|
|
|
"sync"
|
2022-06-30 21:27:56 +08:00
|
|
|
|
2022-07-05 13:23:47 +08:00
|
|
|
"github.com/sagernet/sing-box/common/geoip"
|
2024-10-25 22:24:19 +08:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-07-11 18:44:59 +08:00
|
|
|
"github.com/sagernet/sing-dns"
|
2024-10-25 22:24:19 +08:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2023-12-01 13:24:12 +08:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2024-06-07 15:55:21 +08:00
|
|
|
"github.com/sagernet/sing/common/x/list"
|
2022-07-07 21:47:21 +08:00
|
|
|
|
2022-09-13 16:18:39 +08:00
|
|
|
mdns "github.com/miekg/dns"
|
2024-06-07 15:55:21 +08:00
|
|
|
"go4.org/netipx"
|
2022-06-30 21:27:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Router interface {
|
2024-11-09 21:16:11 +08:00
|
|
|
NewService
|
2022-07-14 23:06:03 +08:00
|
|
|
|
2023-03-25 12:03:23 +08:00
|
|
|
FakeIPStore() FakeIPStore
|
|
|
|
|
2023-11-08 12:09:22 +08:00
|
|
|
ConnectionRouter
|
2024-10-22 21:28:22 +08:00
|
|
|
PreMatch(metadata InboundContext) error
|
2024-10-21 23:38:34 +08:00
|
|
|
ConnectionRouterEx
|
2022-07-14 23:06:03 +08:00
|
|
|
|
2022-07-05 13:23:47 +08:00
|
|
|
GeoIPReader() *geoip.Reader
|
2022-07-08 11:00:46 +08:00
|
|
|
LoadGeosite(code string) (Rule, error)
|
2023-12-01 13:24:12 +08:00
|
|
|
RuleSet(tag string) (RuleSet, bool)
|
2024-02-16 13:06:34 +08:00
|
|
|
NeedWIFIState() bool
|
|
|
|
|
2022-09-13 16:18:39 +08:00
|
|
|
Exchange(ctx context.Context, message *mdns.Msg) (*mdns.Msg, error)
|
2022-07-11 18:44:59 +08:00
|
|
|
Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error)
|
2022-07-07 21:47:21 +08:00
|
|
|
LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error)
|
2023-08-24 21:52:38 +08:00
|
|
|
ClearDNSCache()
|
2022-07-19 22:16:49 +08:00
|
|
|
Rules() []Rule
|
2022-09-10 14:09:47 +08:00
|
|
|
|
|
|
|
ClashServer() ClashServer
|
2022-09-26 19:37:06 +08:00
|
|
|
SetClashServer(server ClashServer)
|
|
|
|
|
|
|
|
V2RayServer() V2RayServer
|
|
|
|
SetV2RayServer(server V2RayServer)
|
2023-04-24 10:05:13 +08:00
|
|
|
|
2024-11-10 12:11:21 +08:00
|
|
|
ResetNetwork()
|
2022-07-02 14:07:50 +08:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
// Deprecated: Use ConnectionRouterEx instead.
|
|
|
|
type ConnectionRouter interface {
|
|
|
|
RouteConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error
|
|
|
|
RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error
|
2023-02-08 16:28:52 +08:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
type ConnectionRouterEx interface {
|
|
|
|
ConnectionRouter
|
|
|
|
RouteConnectionEx(ctx context.Context, conn net.Conn, metadata InboundContext, onClose N.CloseHandlerFunc)
|
|
|
|
RoutePacketConnectionEx(ctx context.Context, conn N.PacketConn, metadata InboundContext, onClose N.CloseHandlerFunc)
|
2023-12-01 13:24:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type RuleSet interface {
|
2024-06-07 15:55:21 +08:00
|
|
|
Name() string
|
2024-10-25 22:24:19 +08:00
|
|
|
StartContext(ctx context.Context, startContext *HTTPStartContext) error
|
2024-06-07 15:55:21 +08:00
|
|
|
PostStart() error
|
2023-12-01 13:24:12 +08:00
|
|
|
Metadata() RuleSetMetadata
|
2024-06-07 15:55:21 +08:00
|
|
|
ExtractIPSet() []*netipx.IPSet
|
|
|
|
IncRef()
|
|
|
|
DecRef()
|
|
|
|
Cleanup()
|
|
|
|
RegisterCallback(callback RuleSetUpdateCallback) *list.Element[RuleSetUpdateCallback]
|
|
|
|
UnregisterCallback(element *list.Element[RuleSetUpdateCallback])
|
2023-12-01 13:24:12 +08:00
|
|
|
Close() error
|
|
|
|
HeadlessRule
|
|
|
|
}
|
|
|
|
|
2024-06-07 15:55:21 +08:00
|
|
|
type RuleSetUpdateCallback func(it RuleSet)
|
|
|
|
|
2023-12-01 13:24:12 +08:00
|
|
|
type RuleSetMetadata struct {
|
|
|
|
ContainsProcessRule bool
|
|
|
|
ContainsWIFIRule bool
|
2024-02-03 17:45:27 +08:00
|
|
|
ContainsIPCIDRRule bool
|
2023-12-01 13:24:12 +08:00
|
|
|
}
|
2024-10-25 22:24:19 +08:00
|
|
|
type HTTPStartContext struct {
|
|
|
|
access sync.Mutex
|
|
|
|
httpClientCache map[string]*http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHTTPStartContext() *HTTPStartContext {
|
|
|
|
return &HTTPStartContext{
|
|
|
|
httpClientCache: make(map[string]*http.Client),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HTTPStartContext) HTTPClient(detour string, dialer N.Dialer) *http.Client {
|
|
|
|
c.access.Lock()
|
|
|
|
defer c.access.Unlock()
|
|
|
|
if httpClient, loaded := c.httpClientCache[detour]; loaded {
|
|
|
|
return httpClient
|
|
|
|
}
|
|
|
|
httpClient := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
ForceAttemptHTTP2: true,
|
|
|
|
TLSHandshakeTimeout: C.TCPTimeout,
|
|
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
return dialer.DialContext(ctx, network, M.ParseSocksaddr(addr))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c.httpClientCache[detour] = httpClient
|
|
|
|
return httpClient
|
|
|
|
}
|
2023-12-01 13:24:12 +08:00
|
|
|
|
2024-10-25 22:24:19 +08:00
|
|
|
func (c *HTTPStartContext) Close() {
|
|
|
|
c.access.Lock()
|
|
|
|
defer c.access.Unlock()
|
|
|
|
for _, client := range c.httpClientCache {
|
|
|
|
client.CloseIdleConnections()
|
|
|
|
}
|
2023-12-01 13:24:12 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 10:36:19 +08:00
|
|
|
type InterfaceUpdateListener interface {
|
2023-07-23 14:42:19 +08:00
|
|
|
InterfaceUpdated()
|
2022-11-06 10:36:19 +08:00
|
|
|
}
|
2023-11-24 20:58:07 +08:00
|
|
|
|
|
|
|
type WIFIState struct {
|
|
|
|
SSID string
|
|
|
|
BSSID string
|
|
|
|
}
|