diff --git a/cmd/derper/depaware.txt b/cmd/derper/depaware.txt index d6562fb75..00c38dbe8 100644 --- a/cmd/derper/depaware.txt +++ b/cmd/derper/depaware.txt @@ -125,6 +125,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa W tailscale.com/util/clientmetric from tailscale.com/net/tshttpproxy tailscale.com/util/cloudenv from tailscale.com/hostinfo+ W tailscale.com/util/cmpver from tailscale.com/net/tshttpproxy + tailscale.com/util/cmpx from tailscale.com/cmd/derper+ L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics tailscale.com/util/dnsname from tailscale.com/hostinfo+ tailscale.com/util/httpm from tailscale.com/client/tailscale diff --git a/cmd/derper/derper.go b/cmd/derper/derper.go index 02736b6be..8b0feb639 100644 --- a/cmd/derper/derper.go +++ b/cmd/derper/derper.go @@ -33,6 +33,7 @@ import ( "tailscale.com/net/stun" "tailscale.com/tsweb" "tailscale.com/types/key" + "tailscale.com/util/cmpx" ) var ( @@ -436,11 +437,7 @@ func defaultMeshPSKFile() string { } func rateLimitedListenAndServeTLS(srv *http.Server) error { - addr := srv.Addr - if addr == "" { - addr = ":https" - } - ln, err := net.Listen("tcp", addr) + ln, err := net.Listen("tcp", cmpx.Or(srv.Addr, ":https")) if err != nil { return err } diff --git a/cmd/get-authkey/main.go b/cmd/get-authkey/main.go index 5f5e85186..22dd9af82 100644 --- a/cmd/get-authkey/main.go +++ b/cmd/get-authkey/main.go @@ -16,6 +16,7 @@ import ( "golang.org/x/oauth2/clientcredentials" "tailscale.com/client/tailscale" + "tailscale.com/util/cmpx" ) func main() { @@ -39,10 +40,7 @@ func main() { log.Fatal("at least one tag must be specified") } - baseURL := os.Getenv("TS_BASE_URL") - if baseURL == "" { - baseURL = "https://api.tailscale.com" - } + baseURL := cmpx.Or(os.Getenv("TS_BASE_URL"), "https://api.tailscale.com") credentials := clientcredentials.Config{ ClientID: clientID, diff --git a/cmd/tailscale/cli/cli_test.go b/cmd/tailscale/cli/cli_test.go index 309e7ae03..5a29f177d 100644 --- a/cmd/tailscale/cli/cli_test.go +++ b/cmd/tailscale/cli/cli_test.go @@ -22,6 +22,7 @@ import ( "tailscale.com/tstest" "tailscale.com/types/persist" "tailscale.com/types/preftype" + "tailscale.com/util/cmpx" "tailscale.com/version/distro" ) @@ -719,10 +720,7 @@ func TestPrefsFromUpArgs(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var warnBuf tstest.MemLogger - goos := tt.goos - if goos == "" { - goos = "linux" - } + goos := cmpx.Or(tt.goos, "linux") st := tt.st if st == nil { st = new(ipnstate.Status) diff --git a/cmd/tailscale/cli/web.go b/cmd/tailscale/cli/web.go index 38ee25b96..f29eac26a 100644 --- a/cmd/tailscale/cli/web.go +++ b/cmd/tailscale/cli/web.go @@ -29,6 +29,7 @@ import ( "tailscale.com/ipn" "tailscale.com/ipn/ipnstate" "tailscale.com/tailcfg" + "tailscale.com/util/cmpx" "tailscale.com/util/groupmember" "tailscale.com/version/distro" ) @@ -155,10 +156,7 @@ func runWeb(ctx context.Context, args []string) error { // urlOfListenAddr parses a given listen address into a formatted URL func urlOfListenAddr(addr string) string { host, port, _ := net.SplitHostPort(addr) - if host == "" { - host = "127.0.0.1" - } - return fmt.Sprintf("http://%s", net.JoinHostPort(host, port)) + return fmt.Sprintf("http://%s", net.JoinHostPort(cmpx.Or(host, "127.0.0.1"), port)) } // authorize returns the name of the user accessing the web UI after verifying diff --git a/cmd/tailscale/depaware.txt b/cmd/tailscale/depaware.txt index 72444a2a2..8e2401d1d 100644 --- a/cmd/tailscale/depaware.txt +++ b/cmd/tailscale/depaware.txt @@ -114,6 +114,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep tailscale.com/util/clientmetric from tailscale.com/net/netcheck+ tailscale.com/util/cloudenv from tailscale.com/net/dnscache+ W tailscale.com/util/cmpver from tailscale.com/net/tshttpproxy + tailscale.com/util/cmpx from tailscale.com/cmd/tailscale/cli+ L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics tailscale.com/util/dnsname from tailscale.com/cmd/tailscale/cli+ tailscale.com/util/groupmember from tailscale.com/cmd/tailscale/cli diff --git a/cmd/tailscaled/depaware.txt b/cmd/tailscaled/depaware.txt index 24e089651..b66faf36f 100644 --- a/cmd/tailscaled/depaware.txt +++ b/cmd/tailscaled/depaware.txt @@ -308,6 +308,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de tailscale.com/util/clientmetric from tailscale.com/control/controlclient+ tailscale.com/util/cloudenv from tailscale.com/net/dns/resolver+ LW tailscale.com/util/cmpver from tailscale.com/net/dns+ + tailscale.com/util/cmpx from tailscale.com/derp/derphttp+ 💣 tailscale.com/util/deephash from tailscale.com/ipn/ipnlocal+ L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics+ tailscale.com/util/dnsname from tailscale.com/hostinfo+ diff --git a/derp/derphttp/derphttp_client.go b/derp/derphttp/derphttp_client.go index 2ea86b686..07317fcbf 100644 --- a/derp/derphttp/derphttp_client.go +++ b/derp/derphttp/derphttp_client.go @@ -40,6 +40,7 @@ import ( "tailscale.com/tailcfg" "tailscale.com/types/key" "tailscale.com/types/logger" + "tailscale.com/util/cmpx" ) // Client is a DERP-over-HTTP client. @@ -654,10 +655,7 @@ func (c *Client) dialNode(ctx context.Context, n *tailcfg.DERPNode) (net.Conn, e // Start v4 dial } } - dst := dstPrimary - if dst == "" { - dst = n.HostName - } + dst := cmpx.Or(dstPrimary, n.HostName) port := "443" if n.DERPPort != 0 { port = fmt.Sprint(n.DERPPort) diff --git a/ipn/ipnlocal/dnsconfig_test.go b/ipn/ipnlocal/dnsconfig_test.go index 5b0a7e387..fd12d4748 100644 --- a/ipn/ipnlocal/dnsconfig_test.go +++ b/ipn/ipnlocal/dnsconfig_test.go @@ -16,6 +16,7 @@ import ( "tailscale.com/types/dnstype" "tailscale.com/types/netmap" "tailscale.com/util/cloudenv" + "tailscale.com/util/cmpx" "tailscale.com/util/dnsname" ) @@ -308,10 +309,7 @@ func TestDNSConfigForNetmap(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - verOS := tt.os - if verOS == "" { - verOS = "linux" - } + verOS := cmpx.Or(tt.os, "linux") var log tstest.MemLogger got := dnsConfigForNetmap(tt.nm, tt.prefs.View(), log.Logf, verOS) if !reflect.DeepEqual(got, tt.want) { diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 573d9a910..da0df54d4 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -71,6 +71,7 @@ import ( "tailscale.com/types/preftype" "tailscale.com/types/ptr" "tailscale.com/types/views" + "tailscale.com/util/cmpx" "tailscale.com/util/deephash" "tailscale.com/util/dnsname" "tailscale.com/util/mak" @@ -3932,10 +3933,7 @@ func (b *LocalBackend) setNetMapLocked(nm *netmap.NetworkMap) { b.dialer.SetNetMap(nm) var login string if nm != nil { - login = nm.UserProfiles[nm.User].LoginName - if login == "" { - login = "" - } + login = cmpx.Or(nm.UserProfiles[nm.User].LoginName, "") } b.netMap = nm if login != b.activeLogin { diff --git a/ipn/ipnlocal/serve_test.go b/ipn/ipnlocal/serve_test.go index a440bce97..b78e5c63f 100644 --- a/ipn/ipnlocal/serve_test.go +++ b/ipn/ipnlocal/serve_test.go @@ -16,6 +16,7 @@ import ( "testing" "tailscale.com/ipn" + "tailscale.com/util/cmpx" ) func TestExpandProxyArg(t *testing.T) { @@ -140,10 +141,7 @@ func TestGetServeHandler(t *testing.T) { }, TLS: &tls.ConnectionState{ServerName: serverName}, } - port := tt.port - if port == 0 { - port = 443 - } + port := cmpx.Or(tt.port, 443) req = req.WithContext(context.WithValue(req.Context(), serveHTTPContextKey{}, &serveHTTPContext{ DestPort: port, })) diff --git a/net/dnscache/messagecache.go b/net/dnscache/messagecache.go index dc4a74e95..ebbf20faa 100644 --- a/net/dnscache/messagecache.go +++ b/net/dnscache/messagecache.go @@ -13,6 +13,7 @@ import ( "github.com/golang/groupcache/lru" "golang.org/x/net/dns/dnsmessage" + "tailscale.com/util/cmpx" ) // MessageCache is a cache that works at the DNS message layer, @@ -59,10 +60,7 @@ func (c *MessageCache) Flush() { // pruneLocked prunes down the cache size to the configured (or // default) max size. func (c *MessageCache) pruneLocked() { - max := c.cacheSizeSet - if max == 0 { - max = 500 - } + max := cmpx.Or(c.cacheSizeSet, 500) for c.cache.Len() > max { c.cache.RemoveOldest() } diff --git a/net/netcheck/netcheck.go b/net/netcheck/netcheck.go index ac52a7a41..b0a93454a 100644 --- a/net/netcheck/netcheck.go +++ b/net/netcheck/netcheck.go @@ -42,6 +42,7 @@ import ( "tailscale.com/types/opt" "tailscale.com/types/ptr" "tailscale.com/util/clientmetric" + "tailscale.com/util/cmpx" "tailscale.com/util/mak" ) @@ -450,10 +451,9 @@ func makeProbePlan(dm *tailcfg.DERPMap, ifState *interfaces.State, last *Report) do6 = false } n := reg.Nodes[try%len(reg.Nodes)] - prevLatency := last.RegionLatency[reg.RegionID] * 120 / 100 - if prevLatency == 0 { - prevLatency = defaultActiveRetransmitTime - } + prevLatency := cmpx.Or( + last.RegionLatency[reg.RegionID]*120/100, + defaultActiveRetransmitTime) delay := time.Duration(try) * prevLatency if try > 1 { delay += time.Duration(try) * 50 * time.Millisecond @@ -1589,10 +1589,7 @@ func (rs *reportState) runProbe(ctx context.Context, dm *tailcfg.DERPMap, probe // proto is 4 or 6 // If it returns nil, the node is skipped. func (c *Client) nodeAddr(ctx context.Context, n *tailcfg.DERPNode, proto probeProto) (ap netip.AddrPort) { - port := n.STUNPort - if port == 0 { - port = 3478 - } + port := cmpx.Or(n.STUNPort, 3478) if port < 0 || port > 1<<16-1 { return } diff --git a/tsweb/tsweb.go b/tsweb/tsweb.go index a84aacbc9..3d3644c9c 100644 --- a/tsweb/tsweb.go +++ b/tsweb/tsweb.go @@ -27,6 +27,7 @@ import ( "tailscale.com/net/tsaddr" "tailscale.com/tsweb/varz" "tailscale.com/types/logger" + "tailscale.com/util/cmpx" "tailscale.com/util/vizerror" ) @@ -144,10 +145,7 @@ func (h Port80Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Redirect authorized user to the debug handler. path = "/debug/" } - host := h.FQDN - if host == "" { - host = r.Host - } + host := cmpx.Or(h.FQDN, r.Host) target := "https://" + host + path http.Redirect(w, r, target, http.StatusFound) } diff --git a/tsweb/varz/varz.go b/tsweb/varz/varz.go index 024d4ea48..ed88d5ed6 100644 --- a/tsweb/varz/varz.go +++ b/tsweb/varz/varz.go @@ -18,6 +18,7 @@ import ( "time" "tailscale.com/metrics" + "tailscale.com/util/cmpx" "tailscale.com/version" ) @@ -96,16 +97,10 @@ func writePromExpVar(w io.Writer, prefix string, kv expvar.KeyValue) { switch v := kv.Value.(type) { case *expvar.Int: - if typ == "" { - typ = "counter" - } - fmt.Fprintf(w, "# TYPE %s %s\n%s %v\n", name, typ, name, v.Value()) + fmt.Fprintf(w, "# TYPE %s %s\n%s %v\n", name, cmpx.Or(typ, "counter"), name, v.Value()) return case *expvar.Float: - if typ == "" { - typ = "gauge" - } - fmt.Fprintf(w, "# TYPE %s %s\n%s %v\n", name, typ, name, v.Value()) + fmt.Fprintf(w, "# TYPE %s %s\n%s %v\n", name, cmpx.Or(typ, "gauge"), name, v.Value()) return case *metrics.Set: v.Do(func(kv expvar.KeyValue) { diff --git a/util/linuxfw/nftables.go b/util/linuxfw/nftables.go index 066ce12a1..cd41a5d9c 100644 --- a/util/linuxfw/nftables.go +++ b/util/linuxfw/nftables.go @@ -16,6 +16,7 @@ import ( "github.com/josharian/native" "golang.org/x/sys/unix" "tailscale.com/types/logger" + "tailscale.com/util/cmpx" ) // DebugNetfilter prints debug information about netfilter rules to the @@ -52,27 +53,18 @@ func DebugNetfilter(logf logger.Logf) error { for _, ex := range rule.Exprs { switch v := ex.(type) { case *expr.Meta: - key := metaKeyNames[v.Key] - if key == "" { - key = "UNKNOWN" - } + key := cmpx.Or(metaKeyNames[v.Key], "UNKNOWN") logf("netfilter: Meta: key=%s source_register=%v register=%d", key, v.SourceRegister, v.Register) case *expr.Cmp: - op := cmpOpNames[v.Op] - if op == "" { - op = "UNKNOWN" - } + op := cmpx.Or(cmpOpNames[v.Op], "UNKNOWN") logf("netfilter: Cmp: op=%s register=%d data=%s", op, v.Register, formatMaybePrintable(v.Data)) case *expr.Counter: // don't print case *expr.Verdict: - kind := verdictNames[v.Kind] - if kind == "" { - kind = "UNKNOWN" - } + kind := cmpx.Or(verdictNames[v.Kind], "UNKNOWN") logf("netfilter: Verdict: kind=%s data=%s", kind, v.Chain) case *expr.Target: