cmd/tailscaled: add debug flag to print interfaces just once

It previously only had a polling monitor mode.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/2714/head
Brad Fitzpatrick 3 years ago
parent b49d9bc74d
commit 4aab083cae

@ -35,6 +35,7 @@ import (
) )
var debugArgs struct { var debugArgs struct {
ifconfig bool // print network state once and exit
monitor bool monitor bool
getURL string getURL string
derpCheck string derpCheck string
@ -45,6 +46,7 @@ var debugModeFunc = debugMode // so it can be addressable
func debugMode(args []string) error { func debugMode(args []string) error {
fs := flag.NewFlagSet("debug", flag.ExitOnError) fs := flag.NewFlagSet("debug", flag.ExitOnError)
fs.BoolVar(&debugArgs.ifconfig, "ifconfig", false, "If true, print network interface state")
fs.BoolVar(&debugArgs.monitor, "monitor", false, "If true, run link monitor forever. Precludes all other options.") fs.BoolVar(&debugArgs.monitor, "monitor", false, "If true, run link monitor forever. Precludes all other options.")
fs.BoolVar(&debugArgs.portmap, "portmap", false, "If true, run portmap debugging. Precludes all other options.") fs.BoolVar(&debugArgs.portmap, "portmap", false, "If true, run portmap debugging. Precludes all other options.")
fs.StringVar(&debugArgs.getURL, "get-url", "", "If non-empty, fetch provided URL.") fs.StringVar(&debugArgs.getURL, "get-url", "", "If non-empty, fetch provided URL.")
@ -59,8 +61,11 @@ func debugMode(args []string) error {
if debugArgs.derpCheck != "" { if debugArgs.derpCheck != "" {
return checkDerp(ctx, debugArgs.derpCheck) return checkDerp(ctx, debugArgs.derpCheck)
} }
if debugArgs.ifconfig {
return runMonitor(ctx, false)
}
if debugArgs.monitor { if debugArgs.monitor {
return runMonitor(ctx) return runMonitor(ctx, true)
} }
if debugArgs.portmap { if debugArgs.portmap {
return debugPortmap(ctx) return debugPortmap(ctx)
@ -71,7 +76,7 @@ func debugMode(args []string) error {
return errors.New("only --monitor is available at the moment") return errors.New("only --monitor is available at the moment")
} }
func runMonitor(ctx context.Context) error { func runMonitor(ctx context.Context, loop bool) error {
dump := func(st *interfaces.State) { dump := func(st *interfaces.State) {
j, _ := json.MarshalIndent(st, "", " ") j, _ := json.MarshalIndent(st, "", " ")
os.Stderr.Write(j) os.Stderr.Write(j)
@ -88,8 +93,13 @@ func runMonitor(ctx context.Context) error {
log.Printf("Link monitor fired. New state:") log.Printf("Link monitor fired. New state:")
dump(st) dump(st)
}) })
log.Printf("Starting link change monitor; initial state:") if loop {
log.Printf("Starting link change monitor; initial state:")
}
dump(mon.InterfaceState()) dump(mon.InterfaceState())
if !loop {
return nil
}
mon.Start() mon.Start()
log.Printf("Started link change monitor; waiting...") log.Printf("Started link change monitor; waiting...")
select {} select {}

Loading…
Cancel
Save