Skip to content

Commit b7e8561

Browse files
committed
Export dns.NewHandler
Signed-off-by: Nino Kodabande <[email protected]>
1 parent e9ba19b commit b7e8561

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

cmd/limactl/debug.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ func debugDNSAction(cmd *cobra.Command, args []string) error {
4747
return err
4848
}
4949
}
50-
srv, err := dns.Start(udpLocalPort, tcpLocalPort, ipv6, map[string]string{})
50+
srvOpts := dns.ServerOptions{
51+
UDPPort: udpLocalPort,
52+
TCPPort: tcpLocalPort,
53+
HandlerOptions: dns.HandlerOptions{
54+
IPv6: ipv6,
55+
StaticHosts: map[string]string{},
56+
},
57+
}
58+
srv, err := dns.Start(srvOpts)
5159
if err != nil {
5260
return err
5361
}

pkg/hostagent/dns/dns.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ import (
1616
// https://github.com/lima-vm/lima/issues/380
1717
const truncateSize = 512
1818

19+
var defaultFallbackIPs = []string{"8.8.8.8", "1.1.1.1"}
20+
21+
type HandlerOptions struct {
22+
IPv6 bool
23+
StaticHosts map[string]string
24+
UpstreamServers []string
25+
}
26+
27+
type ServerOptions struct {
28+
HandlerOptions
29+
Address string
30+
TCPPort int
31+
UDPPort int
32+
}
33+
1934
type Handler struct {
2035
clientConfig *dns.ClientConfig
2136
clients []*dns.Client
@@ -38,21 +53,20 @@ func (s *Server) Shutdown() {
3853
}
3954
}
4055

41-
func newStaticClientConfig(ips []net.IP) (*dns.ClientConfig, error) {
56+
func newStaticClientConfig(ips []string) (*dns.ClientConfig, error) {
4257
s := ``
4358
for _, ip := range ips {
44-
s += fmt.Sprintf("nameserver %s\n", ip.String())
59+
s += fmt.Sprintf("nameserver %s\n", ip)
4560
}
4661
r := strings.NewReader(s)
4762
return dns.ClientConfigFromReader(r)
4863
}
4964

50-
func newHandler(IPv6 bool, hosts map[string]string) (dns.Handler, error) {
65+
func NewHandler(opts HandlerOptions) (dns.Handler, error) {
5166
cc, err := dns.ClientConfigFromFile("/etc/resolv.conf")
5267
if err != nil {
53-
fallbackIPs := []net.IP{net.ParseIP("8.8.8.8"), net.ParseIP("1.1.1.1")}
54-
logrus.WithError(err).Warnf("failed to detect system DNS, falling back to %v", fallbackIPs)
55-
cc, err = newStaticClientConfig(fallbackIPs)
68+
logrus.WithError(err).Warnf("failed to detect system DNS, falling back to %v", defaultFallbackIPs)
69+
cc, err = newStaticClientConfig(defaultFallbackIPs)
5670
if err != nil {
5771
return nil, err
5872
}
@@ -64,11 +78,11 @@ func newHandler(IPv6 bool, hosts map[string]string) (dns.Handler, error) {
6478
h := &Handler{
6579
clientConfig: cc,
6680
clients: clients,
67-
IPv6: IPv6,
81+
IPv6: opts.IPv6,
6882
cname: make(map[string]string),
6983
ip: make(map[string]net.IP),
7084
}
71-
for host, address := range hosts {
85+
for host, address := range opts.StaticHosts {
7286
if ip := net.ParseIP(address); ip != nil {
7387
h.ip[host] = ip
7488
} else {
@@ -268,14 +282,14 @@ func (h *Handler) ServeDNS(w dns.ResponseWriter, req *dns.Msg) {
268282
}
269283
}
270284

271-
func Start(udpLocalPort, tcpLocalPort int, IPv6 bool, hosts map[string]string) (*Server, error) {
272-
h, err := newHandler(IPv6, hosts)
285+
func Start(opts ServerOptions) (*Server, error) {
286+
h, err := NewHandler(opts.HandlerOptions)
273287
if err != nil {
274288
return nil, err
275289
}
276290
server := &Server{}
277-
if udpLocalPort > 0 {
278-
addr := fmt.Sprintf("127.0.0.1:%d", udpLocalPort)
291+
if opts.UDPPort > 0 {
292+
addr := fmt.Sprintf("127.0.0.1:%d", opts.UDPPort)
279293
s := &dns.Server{Net: "udp", Addr: addr, Handler: h}
280294
server.udp = s
281295
go func() {
@@ -284,8 +298,8 @@ func Start(udpLocalPort, tcpLocalPort int, IPv6 bool, hosts map[string]string) (
284298
}
285299
}()
286300
}
287-
if tcpLocalPort > 0 {
288-
addr := fmt.Sprintf("127.0.0.1:%d", tcpLocalPort)
301+
if opts.TCPPort > 0 {
302+
addr := fmt.Sprintf("127.0.0.1:%d", opts.TCPPort)
289303
s := &dns.Server{Net: "tcp", Addr: addr, Handler: h}
290304
server.tcp = s
291305
go func() {

pkg/hostagent/dns/dns_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ func TestTXTRecords(t *testing.T) {
4848

4949
t.Run("test TXT records", func(t *testing.T) {
5050
w := new(TestResponseWriter)
51-
h, err := newHandler(true, map[string]string{
52-
"MY.Host": "host.lima.internal",
53-
})
51+
options := HandlerOptions{
52+
IPv6: true,
53+
StaticHosts: map[string]string{
54+
"MY.Host": "host.lima.internal",
55+
},
56+
}
57+
h, err := NewHandler(options)
5458
if err == nil {
5559
for i := 0; i < len(testDomains); i++ {
5660
req := new(dns.Msg)

pkg/hostagent/hostagent.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,15 @@ func (a *HostAgent) Run(ctx context.Context) error {
255255
hosts := a.y.HostResolver.Hosts
256256
hosts["host.lima.internal."] = qemuconst.SlirpGateway
257257
hosts[fmt.Sprintf("lima-%s.", a.instName)] = qemuconst.SlirpIPAddress
258-
dnsServer, err := dns.Start(a.udpDNSLocalPort, a.tcpDNSLocalPort, *a.y.HostResolver.IPv6, hosts)
258+
srvOpts := dns.ServerOptions{
259+
UDPPort: a.udpDNSLocalPort,
260+
TCPPort: a.tcpDNSLocalPort,
261+
HandlerOptions: dns.HandlerOptions{
262+
IPv6: *a.y.HostResolver.IPv6,
263+
StaticHosts: hosts,
264+
},
265+
}
266+
dnsServer, err := dns.Start(srvOpts)
259267
if err != nil {
260268
return fmt.Errorf("cannot start DNS server: %w", err)
261269
}

0 commit comments

Comments
 (0)