2022-06-30 21:27:56 +08:00
|
|
|
package adapter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-01 13:24:12 +08:00
|
|
|
"net/http"
|
2022-07-07 21:47:21 +08:00
|
|
|
"net/netip"
|
2022-06-30 21:27:56 +08:00
|
|
|
|
2022-07-05 13:23:47 +08:00
|
|
|
"github.com/sagernet/sing-box/common/geoip"
|
2022-07-11 18:44:59 +08:00
|
|
|
"github.com/sagernet/sing-dns"
|
2022-08-04 22:01:20 +08:00
|
|
|
"github.com/sagernet/sing-tun"
|
2022-07-14 23:06:03 +08:00
|
|
|
"github.com/sagernet/sing/common/control"
|
2023-12-01 13:24:12 +08:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2023-08-29 13:43:42 +08:00
|
|
|
"github.com/sagernet/sing/service"
|
2022-07-07 21:47:21 +08:00
|
|
|
|
2022-09-13 16:18:39 +08:00
|
|
|
mdns "github.com/miekg/dns"
|
2022-06-30 21:27:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Router interface {
|
2022-07-05 13:23:47 +08:00
|
|
|
Service
|
2023-12-20 20:00:00 +08:00
|
|
|
PreStarter
|
2023-12-02 17:47:57 +08:00
|
|
|
PostStarter
|
2022-07-14 23:06:03 +08:00
|
|
|
|
2022-07-21 21:03:41 +08:00
|
|
|
Outbounds() []Outbound
|
2022-06-30 21:27:56 +08:00
|
|
|
Outbound(tag string) (Outbound, bool)
|
2023-12-01 13:24:12 +08:00
|
|
|
DefaultOutbound(network string) (Outbound, error)
|
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
|
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)
|
2022-07-14 23:06:03 +08:00
|
|
|
|
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-14 23:06:03 +08:00
|
|
|
|
2022-09-14 12:46:02 +08:00
|
|
|
InterfaceFinder() control.InterfaceFinder
|
2023-04-18 14:04:09 +08:00
|
|
|
UpdateInterfaces() error
|
2022-07-15 11:51:51 +08:00
|
|
|
DefaultInterface() string
|
2022-07-10 08:18:52 +08:00
|
|
|
AutoDetectInterface() bool
|
2022-10-25 12:55:00 +08:00
|
|
|
AutoDetectInterfaceFunc() control.Func
|
2022-07-24 17:46:25 +08:00
|
|
|
DefaultMark() int
|
2022-08-04 22:01:20 +08:00
|
|
|
NetworkMonitor() tun.NetworkUpdateMonitor
|
|
|
|
InterfaceMonitor() tun.DefaultInterfaceMonitor
|
2022-08-15 11:40:49 +08:00
|
|
|
PackageManager() tun.PackageManager
|
2023-11-24 20:58:07 +08:00
|
|
|
WIFIState() WIFIState
|
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
|
|
|
|
|
|
|
ResetNetwork() error
|
2022-07-02 14:07:50 +08:00
|
|
|
}
|
|
|
|
|
2023-02-08 16:28:52 +08:00
|
|
|
func ContextWithRouter(ctx context.Context, router Router) context.Context {
|
2023-08-29 13:43:42 +08:00
|
|
|
return service.ContextWith(ctx, router)
|
2023-02-08 16:28:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func RouterFromContext(ctx context.Context) Router {
|
2023-08-29 13:43:42 +08:00
|
|
|
return service.FromContext[Router](ctx)
|
2023-02-08 16:28:52 +08:00
|
|
|
}
|
|
|
|
|
2023-12-01 13:24:12 +08:00
|
|
|
type HeadlessRule interface {
|
|
|
|
Match(metadata *InboundContext) bool
|
|
|
|
}
|
|
|
|
|
2022-07-02 14:07:50 +08:00
|
|
|
type Rule interface {
|
2023-12-01 13:24:12 +08:00
|
|
|
HeadlessRule
|
2022-07-05 13:23:47 +08:00
|
|
|
Service
|
2022-07-19 22:16:49 +08:00
|
|
|
Type() string
|
2022-07-05 13:23:47 +08:00
|
|
|
UpdateGeosite() error
|
2022-07-02 14:07:50 +08:00
|
|
|
Outbound() string
|
|
|
|
String() string
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-24 14:05:06 +08:00
|
|
|
|
|
|
|
type DNSRule interface {
|
|
|
|
Rule
|
|
|
|
DisableCache() bool
|
2023-06-07 20:28:21 +08:00
|
|
|
RewriteTTL() *uint32
|
2024-05-12 15:06:21 +08:00
|
|
|
ClientSubnet() *netip.Prefix
|
2024-02-03 17:45:27 +08:00
|
|
|
WithAddressLimit() bool
|
|
|
|
MatchAddressLimit(metadata *InboundContext) bool
|
2022-07-24 14:05:06 +08:00
|
|
|
}
|
2022-11-06 10:36:19 +08:00
|
|
|
|
2023-12-01 13:24:12 +08:00
|
|
|
type RuleSet interface {
|
|
|
|
StartContext(ctx context.Context, startContext RuleSetStartContext) error
|
|
|
|
PostStart() error
|
|
|
|
Metadata() RuleSetMetadata
|
|
|
|
Close() error
|
|
|
|
HeadlessRule
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
type RuleSetStartContext interface {
|
|
|
|
HTTPClient(detour string, dialer N.Dialer) *http.Client
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|