From ad1cf388fb4a0e16e74add310b3b2cbc73d4f42e Mon Sep 17 00:00:00 2001 From: Pascal Fischer Date: Wed, 6 Dec 2023 12:08:12 +0100 Subject: [PATCH] Extract private upstream for iOS and fix function headers for other OS --- client/internal/dns/file_linux.go | 2 +- client/internal/dns/host_android.go | 2 +- client/internal/dns/host_darwin.go | 2 +- client/internal/dns/host_linux.go | 2 +- client/internal/dns/host_windows.go | 2 +- client/internal/dns/upstream.go | 34 -------------------- client/internal/dns/upstream_ios.go | 44 ++++++++++++++++++++++++++ client/internal/dns/upstream_nonios.go | 19 +++++++++++ 8 files changed, 68 insertions(+), 39 deletions(-) create mode 100644 client/internal/dns/upstream_ios.go create mode 100644 client/internal/dns/upstream_nonios.go diff --git a/client/internal/dns/file_linux.go b/client/internal/dns/file_linux.go index 67128f79a..5a2ef9da9 100644 --- a/client/internal/dns/file_linux.go +++ b/client/internal/dns/file_linux.go @@ -138,7 +138,7 @@ func prepareResolvConfContent(searchDomains, nameServers, others []string) bytes return buf } -func searchDomains(config hostDNSConfig) []string { +func searchDomains(config HostDNSConfig) []string { listOfDomains := make([]string, 0) for _, dConf := range config.domains { if dConf.matchOnly || dConf.disabled { diff --git a/client/internal/dns/host_android.go b/client/internal/dns/host_android.go index 169cc7c47..624d42dfb 100644 --- a/client/internal/dns/host_android.go +++ b/client/internal/dns/host_android.go @@ -3,7 +3,7 @@ package dns type androidHostManager struct { } -func newHostManager(wgInterface WGIface) (hostManager, error) { +func newHostManager(wgInterface WGIface, dnsManager IosDnsManager) (hostManager, error) { return &androidHostManager{}, nil } diff --git a/client/internal/dns/host_darwin.go b/client/internal/dns/host_darwin.go index c0a213611..a3ab9e6d3 100644 --- a/client/internal/dns/host_darwin.go +++ b/client/internal/dns/host_darwin.go @@ -34,7 +34,7 @@ type systemConfigurator struct { createdKeys map[string]struct{} } -func newHostManager(_ WGIface) (hostManager, error) { +func newHostManager(_ WGIface, dnsManager IosDnsManager) (hostManager, error) { return &systemConfigurator{ createdKeys: make(map[string]struct{}), }, nil diff --git a/client/internal/dns/host_linux.go b/client/internal/dns/host_linux.go index 7838c988f..763ff48c8 100644 --- a/client/internal/dns/host_linux.go +++ b/client/internal/dns/host_linux.go @@ -25,7 +25,7 @@ const ( type osManagerType int -func newHostManager(wgInterface WGIface) (hostManager, error) { +func newHostManager(wgInterface WGIface, dnsManager IosDnsManager) (hostManager, error) { osManager, err := getOSDNSManagerType() if err != nil { return nil, err diff --git a/client/internal/dns/host_windows.go b/client/internal/dns/host_windows.go index 1e88a6c7b..52da1a0b1 100644 --- a/client/internal/dns/host_windows.go +++ b/client/internal/dns/host_windows.go @@ -29,7 +29,7 @@ type registryConfigurator struct { routingAll bool } -func newHostManager(wgInterface WGIface) (hostManager, error) { +func newHostManager(wgInterface WGIface, dnsManager IosDnsManager) (hostManager, error) { guid, err := wgInterface.GetInterfaceGUIDString() if err != nil { return nil, err diff --git a/client/internal/dns/upstream.go b/client/internal/dns/upstream.go index b93dd5bb4..112212b8a 100644 --- a/client/internal/dns/upstream.go +++ b/client/internal/dns/upstream.go @@ -8,13 +8,11 @@ import ( "runtime" "sync" "sync/atomic" - "syscall" "time" "github.com/cenkalti/backoff/v4" "github.com/miekg/dns" log "github.com/sirupsen/logrus" - "golang.org/x/sys/unix" ) const ( @@ -85,38 +83,6 @@ func newUpstreamResolver(parentCTX context.Context, interfaceName string, wgAddr } } -// getClientPrivate returns a new DNS client bound to the local IP address of the Netbird interface -// This method is needed for iOS -func (u *upstreamResolver) getClientPrivate() *dns.Client { - dialer := &net.Dialer{ - LocalAddr: &net.UDPAddr{ - IP: u.lIP, - Port: 0, // Let the OS pick a free port - }, - Timeout: upstreamTimeout, - Control: func(network, address string, c syscall.RawConn) error { - var operr error - fn := func(s uintptr) { - operr = unix.SetsockoptInt(int(s), unix.IPPROTO_IP, unix.IP_BOUND_IF, u.iIndex) - } - - if err := c.Control(fn); err != nil { - return err - } - - if operr != nil { - log.Errorf("error while setting socket option: %s", operr) - } - - return operr - }, - } - client := &dns.Client{ - Dialer: dialer, - } - return client -} - func (u *upstreamResolver) stop() { log.Debugf("stopping serving DNS for upstreams %s", u.upstreamServers) u.cancel() diff --git a/client/internal/dns/upstream_ios.go b/client/internal/dns/upstream_ios.go new file mode 100644 index 000000000..a2dd31ef2 --- /dev/null +++ b/client/internal/dns/upstream_ios.go @@ -0,0 +1,44 @@ +//go:build ios + +package dns + +import ( + "net" + "syscall" + + "github.com/miekg/dns" + log "github.com/sirupsen/logrus" + "golang.org/x/sys/unix" +) + +// getClientPrivate returns a new DNS client bound to the local IP address of the Netbird interface +// This method is needed for iOS +func (u *upstreamResolver) getClientPrivate() *dns.Client { + dialer := &net.Dialer{ + LocalAddr: &net.UDPAddr{ + IP: u.lIP, + Port: 0, // Let the OS pick a free port + }, + Timeout: upstreamTimeout, + Control: func(network, address string, c syscall.RawConn) error { + var operr error + fn := func(s uintptr) { + operr = unix.SetsockoptInt(int(s), unix.IPPROTO_IP, unix.IP_BOUND_IF, u.iIndex) + } + + if err := c.Control(fn); err != nil { + return err + } + + if operr != nil { + log.Errorf("error while setting socket option: %s", operr) + } + + return operr + }, + } + client := &dns.Client{ + Dialer: dialer, + } + return client +} diff --git a/client/internal/dns/upstream_nonios.go b/client/internal/dns/upstream_nonios.go new file mode 100644 index 000000000..a2a541489 --- /dev/null +++ b/client/internal/dns/upstream_nonios.go @@ -0,0 +1,19 @@ +//go:build !ios + +package dns + +import ( + "net" + + "github.com/miekg/dns" +) + +// getClientPrivate returns a new DNS client bound to the local IP address of the Netbird interface +// This method is needed for iOS +func (u *upstreamResolver) getClientPrivate() *dns.Client { + dialer := &net.Dialer{} + client := &dns.Client{ + Dialer: dialer, + } + return client +}