2023-12-18 11:46:58 +01:00
|
|
|
//go:build ios
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/sys/unix"
|
2024-03-12 19:06:16 +01:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/internal/peer"
|
2023-12-18 11:46:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type upstreamResolverIOS struct {
|
|
|
|
*upstreamResolverBase
|
|
|
|
lIP net.IP
|
|
|
|
lNet *net.IPNet
|
|
|
|
iIndex int
|
|
|
|
}
|
|
|
|
|
2024-03-12 19:06:16 +01:00
|
|
|
func newUpstreamResolver(
|
|
|
|
ctx context.Context,
|
|
|
|
interfaceName string,
|
|
|
|
ip net.IP,
|
|
|
|
net *net.IPNet,
|
|
|
|
statusRecorder *peer.Status,
|
2024-05-07 12:28:30 +02:00
|
|
|
_ *hostsDNSHolder,
|
2024-03-12 19:06:16 +01:00
|
|
|
) (*upstreamResolverIOS, error) {
|
|
|
|
upstreamResolverBase := newUpstreamResolverBase(ctx, statusRecorder)
|
2023-12-18 11:46:58 +01:00
|
|
|
|
|
|
|
index, err := getInterfaceIndex(interfaceName)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("unable to get interface index for %s: %s", interfaceName, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ios := &upstreamResolverIOS{
|
|
|
|
upstreamResolverBase: upstreamResolverBase,
|
|
|
|
lIP: ip,
|
|
|
|
lNet: net,
|
|
|
|
iIndex: index,
|
|
|
|
}
|
|
|
|
ios.upstreamClient = ios
|
|
|
|
|
|
|
|
return ios, nil
|
|
|
|
}
|
|
|
|
|
2024-01-29 17:10:47 +01:00
|
|
|
func (u *upstreamResolverIOS) exchange(ctx context.Context, upstream string, r *dns.Msg) (rm *dns.Msg, t time.Duration, err error) {
|
2023-12-18 11:46:58 +01:00
|
|
|
client := &dns.Client{}
|
|
|
|
upstreamHost, _, err := net.SplitHostPort(upstream)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error while parsing upstream host: %s", err)
|
|
|
|
}
|
2024-02-16 14:37:47 +01:00
|
|
|
|
|
|
|
timeout := upstreamTimeout
|
|
|
|
if deadline, ok := ctx.Deadline(); ok {
|
|
|
|
timeout = time.Until(deadline)
|
|
|
|
}
|
|
|
|
client.DialTimeout = timeout
|
|
|
|
|
2023-12-18 11:46:58 +01:00
|
|
|
upstreamIP := net.ParseIP(upstreamHost)
|
|
|
|
if u.lNet.Contains(upstreamIP) || net.IP.IsPrivate(upstreamIP) {
|
|
|
|
log.Debugf("using private client to query upstream: %s", upstream)
|
2024-02-16 14:37:47 +01:00
|
|
|
client = u.getClientPrivate(timeout)
|
2023-12-18 11:46:58 +01:00
|
|
|
}
|
|
|
|
|
2024-02-16 14:37:47 +01:00
|
|
|
// Cannot use client.ExchangeContext because it overwrites our Dialer
|
|
|
|
return client.Exchange(r, upstream)
|
2023-12-18 11:46:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// getClientPrivate returns a new DNS client bound to the local IP address of the Netbird interface
|
|
|
|
// This method is needed for iOS
|
2024-02-16 14:37:47 +01:00
|
|
|
func (u *upstreamResolverIOS) getClientPrivate(dialTimeout time.Duration) *dns.Client {
|
2023-12-18 11:46:58 +01:00
|
|
|
dialer := &net.Dialer{
|
|
|
|
LocalAddr: &net.UDPAddr{
|
|
|
|
IP: u.lIP,
|
|
|
|
Port: 0, // Let the OS pick a free port
|
|
|
|
},
|
2024-02-16 14:37:47 +01:00
|
|
|
Timeout: dialTimeout,
|
2023-12-18 11:46:58 +01:00
|
|
|
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 getInterfaceIndex(interfaceName string) (int, error) {
|
|
|
|
iface, err := net.InterfaceByName(interfaceName)
|
|
|
|
return iface.Index, err
|
|
|
|
}
|