Skip to content

Commit

Permalink
Merge pull request #381 from AkihiroSuda/fix-380
Browse files Browse the repository at this point in the history
pkg/hostagent/dns: truncate messages
  • Loading branch information
AkihiroSuda authored Nov 4, 2021
2 parents 0f9483b + 347a0bf commit 1077846
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
52 changes: 52 additions & 0 deletions cmd/limactl/debug.go
Original file line number Diff line number Diff line change
@@ -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 {
}
}
1 change: 1 addition & 0 deletions cmd/limactl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func newApp() *cobra.Command {
newHostagentCommand(),
newInfoCommand(),
newShowSSHCommand(),
newDebugCommand(),
)
return rootCmd
}
Expand Down
21 changes: 14 additions & 7 deletions pkg/hostagent/dns.go → pkg/hostagent/dns/dns.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -174,6 +178,7 @@ func (h *Handler) handleQuery(w dns.ResponseWriter, req *dns.Msg) {
}
}
if handled {
reply.Truncate(truncateSize)
_ = w.WriteMsg(&reply)
return
}
Expand All @@ -186,13 +191,15 @@ 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
}
}
}
var reply dns.Msg
reply.SetReply(req)
reply.Truncate(truncateSize)
_ = w.WriteMsg(&reply)
}

Expand All @@ -205,14 +212,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() {
Expand All @@ -221,8 +228,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() {
Expand Down
3 changes: 2 additions & 1 deletion pkg/hostagent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 1077846

Please sign in to comment.