refactor: Modular network manager

This commit is contained in:
世界 2024-11-10 12:11:21 +08:00
parent 19fb214226
commit a1be455202
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
35 changed files with 612 additions and 603 deletions

View file

@ -38,6 +38,7 @@ func init() {
type Transport struct {
options dns.TransportOptions
router adapter.Router
networkManager adapter.NetworkManager
interfaceName string
autoInterface bool
interfaceCallback *list.Element[tun.DefaultInterfaceUpdateCallback]
@ -54,15 +55,11 @@ func NewTransport(options dns.TransportOptions) (*Transport, error) {
if linkURL.Host == "" {
return nil, E.New("missing interface name for DHCP")
}
router := service.FromContext[adapter.Router](options.Context)
if router == nil {
return nil, E.New("missing router in context")
}
transport := &Transport{
options: options,
router: router,
interfaceName: linkURL.Host,
autoInterface: linkURL.Host == "auto",
options: options,
networkManager: service.FromContext[adapter.NetworkManager](options.Context),
interfaceName: linkURL.Host,
autoInterface: linkURL.Host == "auto",
}
return transport, nil
}
@ -77,7 +74,7 @@ func (t *Transport) Start() error {
return err
}
if t.autoInterface {
t.interfaceCallback = t.router.InterfaceMonitor().RegisterCallback(t.interfaceUpdated)
t.interfaceCallback = t.networkManager.InterfaceMonitor().RegisterCallback(t.interfaceUpdated)
}
return nil
}
@ -93,7 +90,7 @@ func (t *Transport) Close() error {
transport.Close()
}
if t.interfaceCallback != nil {
t.router.InterfaceMonitor().UnregisterCallback(t.interfaceCallback)
t.networkManager.InterfaceMonitor().UnregisterCallback(t.interfaceCallback)
}
return nil
}
@ -125,10 +122,10 @@ func (t *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg,
func (t *Transport) fetchInterface() (*net.Interface, error) {
interfaceName := t.interfaceName
if t.autoInterface {
if t.router.InterfaceMonitor() == nil {
if t.networkManager.InterfaceMonitor() == nil {
return nil, E.New("missing monitor for auto DHCP, set route.auto_detect_interface")
}
interfaceName = t.router.InterfaceMonitor().DefaultInterfaceName(netip.Addr{})
interfaceName = t.networkManager.InterfaceMonitor().DefaultInterfaceName(netip.Addr{})
}
if interfaceName == "" {
return nil, E.New("missing default interface")
@ -177,7 +174,7 @@ func (t *Transport) interfaceUpdated(int) {
func (t *Transport) fetchServers0(ctx context.Context, iface *net.Interface) error {
var listener net.ListenConfig
listener.Control = control.Append(listener.Control, control.BindToInterface(t.router.InterfaceFinder(), iface.Name, iface.Index))
listener.Control = control.Append(listener.Control, control.BindToInterface(t.networkManager.InterfaceFinder(), iface.Name, iface.Index))
listener.Control = control.Append(listener.Control, control.ReuseAddr())
listenAddr := "0.0.0.0:68"
if runtime.GOOS == "linux" || runtime.GOOS == "android" {
@ -255,7 +252,7 @@ func (t *Transport) recreateServers(iface *net.Interface, serverAddrs []netip.Ad
return it.String()
}), ","), "]")
}
serverDialer := common.Must1(dialer.NewDefault(t.router, option.DialerOptions{
serverDialer := common.Must1(dialer.NewDefault(t.networkManager, option.DialerOptions{
BindInterface: iface.Name,
UDPFragmentDefault: true,
}))

View file

@ -34,7 +34,7 @@ type SystemDevice struct {
closeOnce sync.Once
}
func NewSystemDevice(router adapter.Router, interfaceName string, localPrefixes []netip.Prefix, mtu uint32, gso bool) (*SystemDevice, error) {
func NewSystemDevice(networkManager adapter.NetworkManager, interfaceName string, localPrefixes []netip.Prefix, mtu uint32, gso bool) (*SystemDevice, error) {
var inet4Addresses []netip.Prefix
var inet6Addresses []netip.Prefix
for _, prefixes := range localPrefixes {
@ -49,7 +49,7 @@ func NewSystemDevice(router adapter.Router, interfaceName string, localPrefixes
}
return &SystemDevice{
dialer: common.Must1(dialer.NewDefault(router, option.DialerOptions{
dialer: common.Must1(dialer.NewDefault(networkManager, option.DialerOptions{
BindInterface: interfaceName,
})),
name: interfaceName,