sing-box/adapter/router.go

135 lines
2.9 KiB
Go
Raw Permalink Normal View History

2022-06-30 13:27:56 +00:00
package adapter
import (
"context"
2023-12-01 05:24:12 +00:00
"net/http"
2022-07-07 13:47:21 +00:00
"net/netip"
2022-06-30 13:27:56 +00:00
2022-07-05 05:23:47 +00:00
"github.com/sagernet/sing-box/common/geoip"
2022-07-11 10:44:59 +00:00
"github.com/sagernet/sing-dns"
2022-08-04 14:01:20 +00:00
"github.com/sagernet/sing-tun"
2022-07-14 15:06:03 +00:00
"github.com/sagernet/sing/common/control"
2023-12-01 05:24:12 +00:00
N "github.com/sagernet/sing/common/network"
2024-06-07 07:55:21 +00:00
"github.com/sagernet/sing/common/x/list"
2023-08-29 05:43:42 +00:00
"github.com/sagernet/sing/service"
2022-07-07 13:47:21 +00:00
2022-09-13 08:18:39 +00:00
mdns "github.com/miekg/dns"
2024-06-07 07:55:21 +00:00
"go4.org/netipx"
2022-06-30 13:27:56 +00:00
)
type Router interface {
2022-07-05 05:23:47 +00:00
Service
PreStarter
2023-12-02 09:47:57 +00:00
PostStarter
2024-06-07 07:55:21 +00:00
Cleanup() error
2022-07-14 15:06:03 +00:00
2022-07-21 13:03:41 +00:00
Outbounds() []Outbound
2022-06-30 13:27:56 +00:00
Outbound(tag string) (Outbound, bool)
2023-12-01 05:24:12 +00:00
DefaultOutbound(network string) (Outbound, error)
2022-07-14 15:06:03 +00:00
2023-03-25 04:03:23 +00:00
FakeIPStore() FakeIPStore
ConnectionRouter
2022-07-14 15:06:03 +00:00
2022-07-05 05:23:47 +00:00
GeoIPReader() *geoip.Reader
2022-07-08 03:00:46 +00:00
LoadGeosite(code string) (Rule, error)
2022-07-14 15:06:03 +00:00
2023-12-01 05:24:12 +00:00
RuleSet(tag string) (RuleSet, bool)
NeedWIFIState() bool
2022-09-13 08:18:39 +00:00
Exchange(ctx context.Context, message *mdns.Msg) (*mdns.Msg, error)
2022-07-11 10:44:59 +00:00
Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error)
2022-07-07 13:47:21 +00:00
LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error)
ClearDNSCache()
2022-07-14 15:06:03 +00:00
2022-09-14 04:46:02 +00:00
InterfaceFinder() control.InterfaceFinder
2023-04-18 06:04:09 +00:00
UpdateInterfaces() error
2022-07-15 03:51:51 +00:00
DefaultInterface() string
AutoDetectInterface() bool
2022-10-25 04:55:00 +00:00
AutoDetectInterfaceFunc() control.Func
2024-06-24 01:49:15 +00:00
DefaultMark() uint32
2024-06-07 07:55:21 +00:00
RegisterAutoRedirectOutputMark(mark uint32) error
AutoRedirectOutputMark() uint32
2022-08-04 14:01:20 +00:00
NetworkMonitor() tun.NetworkUpdateMonitor
InterfaceMonitor() tun.DefaultInterfaceMonitor
PackageManager() tun.PackageManager
WIFIState() WIFIState
2022-07-19 14:16:49 +00:00
Rules() []Rule
2022-09-10 06:09:47 +00:00
ClashServer() ClashServer
2022-09-26 11:37:06 +00:00
SetClashServer(server ClashServer)
V2RayServer() V2RayServer
SetV2RayServer(server V2RayServer)
ResetNetwork() error
2022-07-02 06:07:50 +00:00
}
2023-02-08 08:28:52 +00:00
func ContextWithRouter(ctx context.Context, router Router) context.Context {
2023-08-29 05:43:42 +00:00
return service.ContextWith(ctx, router)
2023-02-08 08:28:52 +00:00
}
func RouterFromContext(ctx context.Context) Router {
2023-08-29 05:43:42 +00:00
return service.FromContext[Router](ctx)
2023-02-08 08:28:52 +00:00
}
2023-12-01 05:24:12 +00:00
type HeadlessRule interface {
Match(metadata *InboundContext) bool
2024-03-24 11:23:11 +00:00
String() string
2023-12-01 05:24:12 +00:00
}
2022-07-02 06:07:50 +00:00
type Rule interface {
2023-12-01 05:24:12 +00:00
HeadlessRule
2022-07-05 05:23:47 +00:00
Service
2022-07-19 14:16:49 +00:00
Type() string
2022-07-05 05:23:47 +00:00
UpdateGeosite() error
2022-07-02 06:07:50 +00:00
Outbound() string
2022-06-30 13:27:56 +00:00
}
2022-07-24 06:05:06 +00:00
type DNSRule interface {
Rule
DisableCache() bool
2023-06-07 12:28:21 +00:00
RewriteTTL() *uint32
ClientSubnet() *netip.Prefix
WithAddressLimit() bool
MatchAddressLimit(metadata *InboundContext) bool
2022-07-24 06:05:06 +00:00
}
2022-11-06 02:36:19 +00:00
2023-12-01 05:24:12 +00:00
type RuleSet interface {
2024-06-07 07:55:21 +00:00
Name() string
2023-12-01 05:24:12 +00:00
StartContext(ctx context.Context, startContext RuleSetStartContext) error
2024-06-07 07:55:21 +00:00
PostStart() error
2023-12-01 05:24:12 +00:00
Metadata() RuleSetMetadata
2024-06-07 07:55:21 +00:00
ExtractIPSet() []*netipx.IPSet
IncRef()
DecRef()
Cleanup()
RegisterCallback(callback RuleSetUpdateCallback) *list.Element[RuleSetUpdateCallback]
UnregisterCallback(element *list.Element[RuleSetUpdateCallback])
2023-12-01 05:24:12 +00:00
Close() error
HeadlessRule
}
2024-06-07 07:55:21 +00:00
type RuleSetUpdateCallback func(it RuleSet)
2023-12-01 05:24:12 +00:00
type RuleSetMetadata struct {
ContainsProcessRule bool
ContainsWIFIRule bool
ContainsIPCIDRRule bool
2023-12-01 05:24:12 +00:00
}
type RuleSetStartContext interface {
HTTPClient(detour string, dialer N.Dialer) *http.Client
Close()
}
2022-11-06 02:36:19 +00:00
type InterfaceUpdateListener interface {
2023-07-23 06:42:19 +00:00
InterfaceUpdated()
2022-11-06 02:36:19 +00:00
}
type WIFIState struct {
SSID string
BSSID string
}