TUN-8855: fix lint issues

## Summary

Fix lint issues necessary for a subsequent PR. This is only separate to allow a better code review of the actual changes.

Closes TUN-8855
This commit is contained in:
Luis Neto 2025-01-30 03:53:24 -08:00 committed by João "Pisco" Fernandes
parent 45f67c23fd
commit bfdb0c76dc
8 changed files with 53 additions and 62 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"os" "os"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -17,7 +18,7 @@ const (
launchdIdentifier = "com.cloudflare.cloudflared" launchdIdentifier = "com.cloudflare.cloudflared"
) )
func runApp(app *cli.App, graceShutdownC chan struct{}) { func runApp(app *cli.App, _ chan struct{}) {
app.Commands = append(app.Commands, &cli.Command{ app.Commands = append(app.Commands, &cli.Command{
Name: "service", Name: "service",
Usage: "Manages the cloudflared launch agent", Usage: "Manages the cloudflared launch agent",
@ -207,3 +208,15 @@ func uninstallLaunchd(c *cli.Context) error {
} }
return err return err
} }
func userHomeDir() (string, error) {
// This returns the home dir of the executing user using OS-specific method
// for discovering the home dir. It's not recommended to call this function
// when the user has root permission as $HOME depends on what options the user
// use with sudo.
homeDir, err := homedir.Dir()
if err != nil {
return "", errors.Wrap(err, "Cannot determine home directory for the user")
}
return homeDir, nil
}

View File

@ -2,14 +2,11 @@ package main
import ( import (
"fmt" "fmt"
"math/rand"
"os" "os"
"strings" "strings"
"time" "time"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"go.uber.org/automaxprocs/maxprocs" "go.uber.org/automaxprocs/maxprocs"
@ -52,10 +49,8 @@ var (
func main() { func main() {
// FIXME: TUN-8148: Disable QUIC_GO ECN due to bugs in proper detection if supported // FIXME: TUN-8148: Disable QUIC_GO ECN due to bugs in proper detection if supported
os.Setenv("QUIC_GO_DISABLE_ECN", "1") os.Setenv("QUIC_GO_DISABLE_ECN", "1")
rand.Seed(time.Now().UnixNano())
metrics.RegisterBuildInfo(BuildType, BuildTime, Version) metrics.RegisterBuildInfo(BuildType, BuildTime, Version)
maxprocs.Set() _, _ = maxprocs.Set()
bInfo := cliutil.GetBuildInfo(BuildType, Version) bInfo := cliutil.GetBuildInfo(BuildType, Version)
// Graceful shutdown channel used by the app. When closed, app must terminate gracefully. // Graceful shutdown channel used by the app. When closed, app must terminate gracefully.
@ -184,18 +179,6 @@ func action(graceShutdownC chan struct{}) cli.ActionFunc {
}) })
} }
func userHomeDir() (string, error) {
// This returns the home dir of the executing user using OS-specific method
// for discovering the home dir. It's not recommended to call this function
// when the user has root permission as $HOME depends on what options the user
// use with sudo.
homeDir, err := homedir.Dir()
if err != nil {
return "", errors.Wrap(err, "Cannot determine home directory for the user")
}
return homeDir, nil
}
// In order to keep the amount of noise sent to Sentry low, typical network errors can be filtered out here by a substring match. // In order to keep the amount of noise sent to Sentry low, typical network errors can be filtered out here by a substring match.
func captureError(err error) { func captureError(err error) {
errorMessage := err.Error() errorMessage := err.Error()

View File

@ -126,7 +126,7 @@ var (
routeFailMsg = fmt.Sprintf("failed to provision routing, please create it manually via Cloudflare dashboard or UI; "+ routeFailMsg = fmt.Sprintf("failed to provision routing, please create it manually via Cloudflare dashboard or UI; "+
"most likely you already have a conflicting record there. You can also rerun this command with --%s to overwrite "+ "most likely you already have a conflicting record there. You can also rerun this command with --%s to overwrite "+
"any existing DNS records for this hostname.", overwriteDNSFlag) "any existing DNS records for this hostname.", overwriteDNSFlag)
deprecatedClassicTunnelErr = fmt.Errorf("Classic tunnels have been deprecated, please use Named Tunnels. (https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/tunnel-guide/)") errDeprecatedClassicTunnel = fmt.Errorf("Classic tunnels have been deprecated, please use Named Tunnels. (https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/tunnel-guide/)")
// TODO: TUN-8756 the list below denotes the flags that do not possess any kind of sensitive information // TODO: TUN-8756 the list below denotes the flags that do not possess any kind of sensitive information
// however this approach is not maintainble in the long-term. // however this approach is not maintainble in the long-term.
nonSecretFlagsList = []string{ nonSecretFlagsList = []string{
@ -326,7 +326,7 @@ func TunnelCommand(c *cli.Context) error {
// Classic tunnel usage is no longer supported // Classic tunnel usage is no longer supported
if c.String("hostname") != "" { if c.String("hostname") != "" {
return deprecatedClassicTunnelErr return errDeprecatedClassicTunnel
} }
if c.IsSet("proxy-dns") { if c.IsSet("proxy-dns") {
@ -615,8 +615,10 @@ func waitToShutdown(wg *sync.WaitGroup,
log.Debug().Msg("Graceful shutdown signalled") log.Debug().Msg("Graceful shutdown signalled")
if gracePeriod > 0 { if gracePeriod > 0 {
// wait for either grace period or service termination // wait for either grace period or service termination
ticker := time.NewTicker(gracePeriod)
defer ticker.Stop()
select { select {
case <-time.Tick(gracePeriod): case <-ticker.C:
case <-errC: case <-errC:
} }
} }
@ -644,7 +646,7 @@ func waitToShutdown(wg *sync.WaitGroup,
func notifySystemd(waitForSignal *signal.Signal) { func notifySystemd(waitForSignal *signal.Signal) {
<-waitForSignal.Wait() <-waitForSignal.Wait()
daemon.SdNotify(false, "READY=1") _, _ = daemon.SdNotify(false, "READY=1")
} }
func writePidFile(waitForSignal *signal.Signal, pidPathname string, log *zerolog.Logger) { func writePidFile(waitForSignal *signal.Signal, pidPathname string, log *zerolog.Logger) {

View File

@ -36,24 +36,11 @@ const (
) )
var ( var (
developerPortal = "https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup"
serviceUrl = developerPortal + "/tunnel-guide/local/as-a-service/"
argumentsUrl = developerPortal + "/tunnel-guide/local/local-management/arguments/"
secretFlags = [2]*altsrc.StringFlag{credentialsContentsFlag, tunnelTokenFlag} secretFlags = [2]*altsrc.StringFlag{credentialsContentsFlag, tunnelTokenFlag}
configFlags = []string{"autoupdate-freq", "no-autoupdate", "retries", "protocol", "loglevel", "transport-loglevel", "origincert", "metrics", "metrics-update-freq", "edge-ip-version", "edge-bind-address"} configFlags = []string{"autoupdate-freq", "no-autoupdate", "retries", "protocol", "loglevel", "transport-loglevel", "origincert", "metrics", "metrics-update-freq", "edge-ip-version", "edge-bind-address"}
) )
func generateRandomClientID(log *zerolog.Logger) (string, error) {
u, err := uuid.NewRandom()
if err != nil {
log.Error().Msgf("couldn't create UUID for client ID %s", err)
return "", err
}
return u.String(), nil
}
func logClientOptions(c *cli.Context, log *zerolog.Logger) { func logClientOptions(c *cli.Context, log *zerolog.Logger) {
flags := make(map[string]interface{}) flags := make(map[string]interface{})
for _, flag := range c.FlagNames() { for _, flag := range c.FlagNames() {
@ -233,13 +220,13 @@ func prepareTunnelConfig(
Observer: observer, Observer: observer,
ReportedVersion: info.Version(), ReportedVersion: info.Version(),
// Note TUN-3758 , we use Int because UInt is not supported with altsrc // Note TUN-3758 , we use Int because UInt is not supported with altsrc
Retries: uint(c.Int("retries")), Retries: uint(c.Int("retries")), // nolint: gosec
RunFromTerminal: isRunningFromTerminal(), RunFromTerminal: isRunningFromTerminal(),
NamedTunnel: namedTunnel, NamedTunnel: namedTunnel,
ProtocolSelector: protocolSelector, ProtocolSelector: protocolSelector,
EdgeTLSConfigs: edgeTLSConfigs, EdgeTLSConfigs: edgeTLSConfigs,
FeatureSelector: featureSelector, FeatureSelector: featureSelector,
MaxEdgeAddrRetries: uint8(c.Int("max-edge-addr-retries")), MaxEdgeAddrRetries: uint8(c.Int("max-edge-addr-retries")), // nolint: gosec
RPCTimeout: c.Duration(rpcTimeout), RPCTimeout: c.Duration(rpcTimeout),
WriteStreamTimeout: c.Duration(writeStreamTimeout), WriteStreamTimeout: c.Duration(writeStreamTimeout),
DisableQUICPathMTUDiscovery: c.Bool(quicDisablePathMTUDiscovery), DisableQUICPathMTUDiscovery: c.Bool(quicDisablePathMTUDiscovery),

View File

@ -441,7 +441,7 @@ func fmtConnections(connections []cfapi.Connection, showRecentlyDisconnected boo
sort.Strings(sortedColos) sort.Strings(sortedColos)
// Map each colo to its frequency, combine into output string. // Map each colo to its frequency, combine into output string.
var output []string output := make([]string, 0, len(sortedColos))
for _, coloName := range sortedColos { for _, coloName := range sortedColos {
output = append(output, fmt.Sprintf("%dx%s", numConnsPerColo[coloName], coloName)) output = append(output, fmt.Sprintf("%dx%s", numConnsPerColo[coloName], coloName))
} }
@ -467,10 +467,15 @@ func readyCommand(c *cli.Context) error {
} }
requestURL := fmt.Sprintf("http://%s/ready", metricsOpts) requestURL := fmt.Sprintf("http://%s/ready", metricsOpts)
res, err := http.Get(requestURL) req, err := http.NewRequest(http.MethodGet, requestURL, nil)
if err != nil { if err != nil {
return err return err
} }
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 { if res.StatusCode != 200 {
body, err := io.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {

View File

@ -14,7 +14,7 @@ import (
const ( const (
AvailableProtocolFlagMessage = "Available protocols: 'auto' - automatically chooses the best protocol over time (the default; and also the recommended one); 'quic' - based on QUIC, relying on UDP egress to Cloudflare edge; 'http2' - using Go's HTTP2 library, relying on TCP egress to Cloudflare edge" AvailableProtocolFlagMessage = "Available protocols: 'auto' - automatically chooses the best protocol over time (the default; and also the recommended one); 'quic' - based on QUIC, relying on UDP egress to Cloudflare edge; 'http2' - using Go's HTTP2 library, relying on TCP egress to Cloudflare edge"
// edgeH2muxTLSServerName is the server name to establish h2mux connection with edge (unused, but kept for legacy reference). // edgeH2muxTLSServerName is the server name to establish h2mux connection with edge (unused, but kept for legacy reference).
edgeH2muxTLSServerName = "cftunnel.com" _ = "cftunnel.com"
// edgeH2TLSServerName is the server name to establish http2 connection with edge // edgeH2TLSServerName is the server name to establish http2 connection with edge
edgeH2TLSServerName = "h2.cftunnel.com" edgeH2TLSServerName = "h2.cftunnel.com"
// edgeQUICServerName is the server name to establish quic connection with edge. // edgeQUICServerName is the server name to establish quic connection with edge.
@ -24,11 +24,9 @@ const (
ResolveTTL = time.Hour ResolveTTL = time.Hour
) )
var ( // ProtocolList represents a list of supported protocols for communication with the edge
// ProtocolList represents a list of supported protocols for communication with the edge // in order of precedence for remote percentage fetcher.
// in order of precedence for remote percentage fetcher. var ProtocolList = []Protocol{QUIC, HTTP2}
ProtocolList = []Protocol{QUIC, HTTP2}
)
type Protocol int64 type Protocol int64
@ -58,7 +56,7 @@ func (p Protocol) String() string {
case QUIC: case QUIC:
return "quic" return "quic"
default: default:
return fmt.Sprintf("unknown protocol") return "unknown protocol"
} }
} }
@ -246,11 +244,11 @@ func NewProtocolSelector(
return newRemoteProtocolSelector(fetchedProtocol, ProtocolList, threshold, protocolFetcher, resolveTTL, log), nil return newRemoteProtocolSelector(fetchedProtocol, ProtocolList, threshold, protocolFetcher, resolveTTL, log), nil
} }
return nil, fmt.Errorf("Unknown protocol %s, %s", protocolFlag, AvailableProtocolFlagMessage) return nil, fmt.Errorf("unknown protocol %s, %s", protocolFlag, AvailableProtocolFlagMessage)
} }
func switchThreshold(accountTag string) int32 { func switchThreshold(accountTag string) int32 {
h := fnv.New32a() h := fnv.New32a()
_, _ = h.Write([]byte(accountTag)) _, _ = h.Write([]byte(accountTag))
return int32(h.Sum32() % 100) return int32(h.Sum32() % 100) // nolint: gosec
} }

View File

@ -103,9 +103,15 @@ func (q *quicConnection) Serve(ctx context.Context) error {
// amount of the grace period, allowing requests to finish before we cancel the context, which will // amount of the grace period, allowing requests to finish before we cancel the context, which will
// make cloudflared exit. // make cloudflared exit.
if err := q.serveControlStream(ctx, controlStream); err == nil { if err := q.serveControlStream(ctx, controlStream); err == nil {
select { if q.gracePeriod > 0 {
case <-ctx.Done(): // In Go1.23 this can be removed and replaced with time.Ticker
case <-time.Tick(q.gracePeriod): // see https://pkg.go.dev/time#Tick
ticker := time.NewTicker(q.gracePeriod)
defer ticker.Stop()
select {
case <-ctx.Done():
case <-ticker.C:
}
} }
} }
cancel() cancel()

View File

@ -11,15 +11,13 @@ const (
FeatureDatagramV3 = "support_datagram_v3" FeatureDatagramV3 = "support_datagram_v3"
) )
var ( var defaultFeatures = []string{
defaultFeatures = []string{ FeatureAllowRemoteConfig,
FeatureAllowRemoteConfig, FeatureSerializedHeaders,
FeatureSerializedHeaders, FeatureDatagramV2,
FeatureDatagramV2, FeatureQUICSupportEOF,
FeatureQUICSupportEOF, FeatureManagementLogs,
FeatureManagementLogs, }
}
)
// Features set by user provided flags // Features set by user provided flags
type staticFeatures struct { type staticFeatures struct {
@ -47,7 +45,6 @@ const (
// Remove any duplicates from the slice // Remove any duplicates from the slice
func Dedup(slice []string) []string { func Dedup(slice []string) []string {
// Convert the slice into a set // Convert the slice into a set
set := make(map[string]bool, 0) set := make(map[string]bool, 0)
for _, str := range slice { for _, str := range slice {