diff options
| author | Andrea Gottardo <andrea@gottardo.me> | 2024-09-19 15:52:31 -0700 |
|---|---|---|
| committer | Andrea Gottardo <andrea@gottardo.me> | 2024-09-19 15:52:31 -0700 |
| commit | 025ceed7354a527594c3c422ab4b9e1558326323 (patch) | |
| tree | a04166e9b57b05c760e71703a42fe560736d3fa4 /cmd | |
| parent | 7c02dcf93ad9c29d3732e189a4b445bd3fde1bf7 (diff) | |
| download | tailscale-angott/dns-cli-stream.tar.xz tailscale-angott/dns-cli-stream.zip | |
cli: implement `tailscale dns stream`angott/dns-cli-stream
Updates tailscale/tailscale#13326
This PR adds another subcommand to `tailscale dns`, to stream queries and answers returned by the DNS forwarder as they are handled.
Useful for debugging purposes, and is equivalent to setting the `TS_DEBUG_DNS_FORWARD_SEND` envknob and filtering the logs for relevant entries. This also adds a new envknob, `TS_DEBUG_DNS_INCLUDE_NAMES`, which includes the actual hostnames in the log lines (with a huge privacy warning!). This makes it easier to diagnose issues with DNS resolution.
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/tailscale/cli/dns-stream.go | 72 | ||||
| -rw-r--r-- | cmd/tailscale/cli/dns.go | 9 |
2 files changed, 79 insertions, 2 deletions
diff --git a/cmd/tailscale/cli/dns-stream.go b/cmd/tailscale/cli/dns-stream.go new file mode 100644 index 000000000..ea5ddb2cf --- /dev/null +++ b/cmd/tailscale/cli/dns-stream.go @@ -0,0 +1,72 @@ +package cli + +import ( + "bufio" + "context" + "encoding/json" + "fmt" + "os" + "strings" +) + +func runDNSStream(ctx context.Context, args []string) error { + fmt.Printf(`Privacy warning! To stream DNS queries, this tool will set these Tailscale debug flags, which would normally be disabled by default: + + - TS_DEBUG_DNS_FORWARD_SEND=true + - TS_DEBUG_DNS_INCLUDE_NAMES=true + +TS_DEBUG_DNS_FORWARD_SEND instructs Tailscale to log DNS queries and responses as they are handled by the internal DNS forwarder. + +TS_DEBUG_DNS_INCLUDE_NAMES instructs Tailscale to include queried and resolved DNS hostnames in the logs. + +Unless the 'TS_NO_LOGS_NO_SUPPORT' flag was previously set, logs are uploaded to Tailscale for diagnostic and debugging purposes, which can be a concern in privacy-sensitive environments. + +If you are concerned about the privacy implications of this, run this tool with the '--no-names' flag, which will avoid logging hostnames.`) + fmt.Printf("\n\n") + fmt.Println("Press Enter to start streaming DNS logs, or Ctrl+C to quit this tool.") + + buf := bufio.NewReader(os.Stdin) + _, err := buf.ReadBytes('\n') + if err != nil { + fmt.Println(err) + return nil + } + + err = localClient.DebugEnvknob(ctx, "TS_DEBUG_DNS_FORWARD_SEND", "true") + if err != nil { + fmt.Printf("failed to set TS_DEBUG_DNS_FORWARD_SEND=true: %v\n", err) + return nil + } + err = localClient.DebugEnvknob(ctx, "TS_DEBUG_DNS_INCLUDE_NAMES", "true") + if err != nil { + fmt.Printf("failed to set TS_DEBUG_DNS_INCLUDE_NAMES=true: %v\n", err) + return nil + } + + logs, err := localClient.TailDaemonLogs(ctx) + if err != nil { + return err + } + + fmt.Println("Streaming DNS logs. Press Ctrl+C to stop.") + + d := json.NewDecoder(logs) + for { + var line struct { + Text string `json:"text"` + Verbose int `json:"v"` + Time string `json:"client_time"` + } + err := d.Decode(&line) + if err != nil { + return err + } + text := strings.TrimSpace(line.Text) + dnsPrefix := "dns: resolver: forward: " + if !strings.HasPrefix(text, dnsPrefix) { + continue + } + text = strings.TrimPrefix(text, dnsPrefix) + fmt.Println(text) + } +} diff --git a/cmd/tailscale/cli/dns.go b/cmd/tailscale/cli/dns.go index 042ce1a94..18fa0f8fa 100644 --- a/cmd/tailscale/cli/dns.go +++ b/cmd/tailscale/cli/dns.go @@ -35,8 +35,13 @@ var dnsCmd = &ffcli.Command{ ShortHelp: "Perform a DNS query", LongHelp: "The 'tailscale dns query' subcommand performs a DNS query for the specified name using the internal DNS forwarder (100.100.100.100).\n\nIt also provides information about the resolver(s) used to resolve the query.", }, - - // TODO: implement `tailscale log` here + { + Name: "stream", + ShortUsage: "tailscale dns stream", + Exec: runDNSStream, + ShortHelp: "Stream DNS queries and responses", + LongHelp: "The 'tailscale dns stream' subcommand streams DNS queries and responses to and from the internal DNS forwarder, which is useful for debugging DNS issues.", + }, // The above work is tracked in https://github.com/tailscale/tailscale/issues/13326 }, |
