From 3775769baa4ee8627dbe1c1a3e72a884b9735495 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Thu, 4 Nov 2021 19:09:13 +0900 Subject: [PATCH 1/3] pkg/hostagent: split DNS pkg Signed-off-by: Akihiro Suda --- pkg/hostagent/{ => dns}/dns.go | 14 +++++++------- pkg/hostagent/hostagent.go | 3 ++- 2 files changed, 9 insertions(+), 8 deletions(-) rename pkg/hostagent/{ => dns}/dns.go (94%) diff --git a/pkg/hostagent/dns.go b/pkg/hostagent/dns/dns.go similarity index 94% rename from pkg/hostagent/dns.go rename to pkg/hostagent/dns/dns.go index b8ddb4d8f3d..c8024b7cb33 100644 --- a/pkg/hostagent/dns.go +++ b/pkg/hostagent/dns/dns.go @@ -1,6 +1,6 @@ // This file has been adapted from https://github.com/norouter/norouter/blob/v0.6.4/pkg/agent/dns/dns.go -package hostagent +package dns import ( "fmt" @@ -205,14 +205,14 @@ func (h *Handler) ServeDNS(w dns.ResponseWriter, req *dns.Msg) { } } -func (a *HostAgent) StartDNS() (*Server, error) { +func Start(udpLocalPort, tcpLocalPort int) (*Server, error) { h, err := newHandler() if err != nil { - panic(err) + return nil, err } server := &Server{} - if a.udpDNSLocalPort > 0 { - addr := fmt.Sprintf("127.0.0.1:%d", a.udpDNSLocalPort) + if udpLocalPort > 0 { + addr := fmt.Sprintf("127.0.0.1:%d", udpLocalPort) s := &dns.Server{Net: "udp", Addr: addr, Handler: h} server.udp = s go func() { @@ -221,8 +221,8 @@ func (a *HostAgent) StartDNS() (*Server, error) { } }() } - if a.tcpDNSLocalPort > 0 { - addr := fmt.Sprintf("127.0.0.1:%d", a.tcpDNSLocalPort) + if tcpLocalPort > 0 { + addr := fmt.Sprintf("127.0.0.1:%d", tcpLocalPort) s := &dns.Server{Net: "tcp", Addr: addr, Handler: h} server.tcp = s go func() { diff --git a/pkg/hostagent/hostagent.go b/pkg/hostagent/hostagent.go index 91f74de23be..572da999a51 100644 --- a/pkg/hostagent/hostagent.go +++ b/pkg/hostagent/hostagent.go @@ -23,6 +23,7 @@ import ( guestagentapi "github.com/lima-vm/lima/pkg/guestagent/api" guestagentclient "github.com/lima-vm/lima/pkg/guestagent/api/client" hostagentapi "github.com/lima-vm/lima/pkg/hostagent/api" + "github.com/lima-vm/lima/pkg/hostagent/dns" "github.com/lima-vm/lima/pkg/hostagent/events" "github.com/lima-vm/lima/pkg/limayaml" "github.com/lima-vm/lima/pkg/qemu" @@ -248,7 +249,7 @@ func (a *HostAgent) Run(ctx context.Context) error { }() if *a.y.UseHostResolver { - dnsServer, err := a.StartDNS() + dnsServer, err := dns.Start(a.udpDNSLocalPort, a.tcpDNSLocalPort) if err != nil { return fmt.Errorf("cannot start DNS server: %w", err) } From dff324caf1fe8a24a7c40a421e585bae0907158b Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Thu, 4 Nov 2021 19:22:04 +0900 Subject: [PATCH 2/3] add `limactl debug dns` Signed-off-by: Akihiro Suda --- cmd/limactl/debug.go | 52 ++++++++++++++++++++++++++++++++++++++++++++ cmd/limactl/main.go | 1 + 2 files changed, 53 insertions(+) create mode 100644 cmd/limactl/debug.go diff --git a/cmd/limactl/debug.go b/cmd/limactl/debug.go new file mode 100644 index 00000000000..2554cee9d7b --- /dev/null +++ b/cmd/limactl/debug.go @@ -0,0 +1,52 @@ +package main + +import ( + "strconv" + + "github.com/lima-vm/lima/pkg/hostagent/dns" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func newDebugCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "debug", + Short: "Debug utilities", + Long: "DO NOT USE! THE COMMAND SYNTAX IS SUBJECT TO CHANGE!", + Hidden: true, + } + cmd.AddCommand(newDebugDNSCommand()) + return cmd +} + +func newDebugDNSCommand() *cobra.Command { + var cmd = &cobra.Command{ + Use: "dns UDPPORT [TCPPORT]", + Short: "Debug built-in DNS", + Long: "DO NOT USE! THE COMMAND SYNTAX IS SUBJECT TO CHANGE!", + Args: cobra.RangeArgs(1, 2), + RunE: debugDNSAction, + } + return cmd +} + +func debugDNSAction(cmd *cobra.Command, args []string) error { + udpLocalPort, err := strconv.Atoi(args[0]) + if err != nil { + return err + } + tcpLocalPort := 0 + if len(args) > 2 { + tcpLocalPort, err = strconv.Atoi(args[1]) + if err != nil { + return err + } + } + srv, err := dns.Start(udpLocalPort, tcpLocalPort) + if err != nil { + return err + } + logrus.Infof("Started srv %+v (UDP %d, TCP %d)", srv, udpLocalPort, tcpLocalPort) + for { + } +} diff --git a/cmd/limactl/main.go b/cmd/limactl/main.go index 015e74b7d8b..62fcbbe3616 100644 --- a/cmd/limactl/main.go +++ b/cmd/limactl/main.go @@ -81,6 +81,7 @@ func newApp() *cobra.Command { newHostagentCommand(), newInfoCommand(), newShowSSHCommand(), + newDebugCommand(), ) return rootCmd } From 347a0bf25aced76427e90241014360f09c5ceae8 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Thu, 4 Nov 2021 19:43:33 +0900 Subject: [PATCH 3/3] pkg/hostagent/dns: truncate messages Fix issue 80: `lima busybox nslookup storage.googleapis.com 192.168.5.3` fails with `Can't find storage.googleapis.com: Parse error` Signed-off-by: Akihiro Suda --- pkg/hostagent/dns/dns.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/hostagent/dns/dns.go b/pkg/hostagent/dns/dns.go index c8024b7cb33..6b3887c9f05 100644 --- a/pkg/hostagent/dns/dns.go +++ b/pkg/hostagent/dns/dns.go @@ -11,6 +11,10 @@ import ( "github.com/sirupsen/logrus" ) +// Truncate for avoiding "Parse error" from `busybox nslookup` +// https://github.com/lima-vm/lima/issues/380 +const truncateSize = 512 + type Handler struct { clientConfig *dns.ClientConfig clients []*dns.Client @@ -174,6 +178,7 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) { } } if handled { + reply.Truncate(truncateSize) _ = w.WriteMsg(&reply) return } @@ -186,6 +191,7 @@ func (h *Handler) handleDefault(w dns.ResponseWriter, req *dns.Msg) { addr := fmt.Sprintf("%s:%s", srv, h.clientConfig.Port) reply, _, err := client.Exchange(req, addr) if err == nil { + reply.Truncate(truncateSize) _ = w.WriteMsg(reply) return } @@ -193,6 +199,7 @@ func (h *Handler) handleDefault(w dns.ResponseWriter, req *dns.Msg) { } var reply dns.Msg reply.SetReply(req) + reply.Truncate(truncateSize) _ = w.WriteMsg(&reply) }