From 4435a4f19da346366b4989329f7102261731d419 Mon Sep 17 00:00:00 2001 From: Adrien Raffin-Caboisse Date: Mon, 16 May 2022 14:59:46 +0200 Subject: [PATCH] chore: apply lint recommendations --- acls.go | 2 +- cmd/headscale/cli/nodes.go | 10 +++++----- derp_server.go | 2 +- machine.go | 6 ++++-- poll.go | 10 +++++++--- utils.go | 13 ++++++++----- 6 files changed, 26 insertions(+), 17 deletions(-) diff --git a/acls.go b/acls.go index 80660e5a..0387f30b 100644 --- a/acls.go +++ b/acls.go @@ -262,7 +262,7 @@ func expandAlias( } if len(ips) == 0 { return ips, fmt.Errorf( - "%w. %v isn't owned by a TagOwner and no forced tags are defined.", + "%w. %v isn't owned by a TagOwner and no forced tags are defined", errInvalidTag, alias, ) diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index fa8b1b2c..821d90bb 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -498,13 +498,13 @@ func nodesToPtables( namespace = pterm.LightYellow(machine.Namespace.Name) } - var IpV4Address string - var IpV6Address string + var IPV4Address string + var IPV6Address string for _, addr := range machine.IpAddresses { if netaddr.MustParseIP(addr).Is4() { - IpV4Address = addr + IPV4Address = addr } else { - IpV6Address = addr + IPV6Address = addr } } @@ -513,7 +513,7 @@ func nodesToPtables( machine.Name, nodeKey.ShortString(), namespace, - strings.Join([]string{IpV4Address, IpV6Address}, ", "), + strings.Join([]string{IPV4Address, IPV6Address}, ", "), strconv.FormatBool(ephemeral), lastSeenTime, online, diff --git a/derp_server.go b/derp_server.go index 25c26dda..d6fb47de 100644 --- a/derp_server.go +++ b/derp_server.go @@ -128,7 +128,7 @@ func (h *Headscale) DERPHandler(ctx *gin.Context) { if !fastStart { pubKey := h.privateKey.Public() - pubKeyStr := pubKey.UntypedHexString() + pubKeyStr := pubKey.UntypedHexString() // nolint fmt.Fprintf(conn, "HTTP/1.1 101 Switching Protocols\r\n"+ "Upgrade: DERP\r\n"+ "Connection: Upgrade\r\n"+ diff --git a/machine.go b/machine.go index 7da09190..845b6491 100644 --- a/machine.go +++ b/machine.go @@ -664,9 +664,11 @@ func getTags( aclPolicy *ACLPolicy, machine Machine, stripEmailDomain bool, -) (validTags []string, invalidTags []string) { +) ([]string, []string) { + validTags := make([]string, 0) + invalidTags := make([]string, 0) if aclPolicy == nil { - return + return validTags, invalidTags } validTagMap := make(map[string]bool) invalidTagMap := make(map[string]bool) diff --git a/poll.go b/poll.go index 3bad0b89..f6dc1387 100644 --- a/poll.go +++ b/poll.go @@ -20,6 +20,10 @@ const ( updateCheckInterval = 10 * time.Second ) +type contextKey string + +const machineNameContextKey = contextKey("machineName") + // PollNetMapHandler takes care of /machine/:id/map // // This is the busiest endpoint, as it keeps the HTTP long poll that updates @@ -272,7 +276,7 @@ func (h *Headscale) PollNetMapStream( return } - ctx := context.WithValue(ctx.Request.Context(), "machineName", machine.Name) + ctx := context.WithValue(ctx.Request.Context(), machineNameContextKey, machine.Name) ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -576,12 +580,12 @@ func (h *Headscale) scheduledPollWorker( defer closeChanWithLog( updateChan, - fmt.Sprint(ctx.Value("machineName")), + fmt.Sprint(ctx.Value(machineNameContextKey)), "updateChan", ) defer closeChanWithLog( keepAliveChan, - fmt.Sprint(ctx.Value("machineName")), + fmt.Sprint(ctx.Value(machineNameContextKey)), "updateChan", ) diff --git a/utils.go b/utils.go index 0a990375..aa86f1cf 100644 --- a/utils.go +++ b/utils.go @@ -136,26 +136,29 @@ func encode( return privKey.SealTo(*pubKey, b), nil } -func (h *Headscale) getAvailableIPs() (ips MachineAddresses, err error) { +func (h *Headscale) getAvailableIPs() (MachineAddresses, error) { + var ips MachineAddresses + var err error ipPrefixes := h.cfg.IPPrefixes for _, ipPrefix := range ipPrefixes { var ip *netaddr.IP ip, err = h.getAvailableIP(ipPrefix) if err != nil { - return + return ips, err } ips = append(ips, *ip) } - return + return ips, err } -func GetIPPrefixEndpoints(na netaddr.IPPrefix) (network, broadcast netaddr.IP) { +func GetIPPrefixEndpoints(na netaddr.IPPrefix) (netaddr.IP, netaddr.IP) { + var network, broadcast netaddr.IP ipRange := na.Range() network = ipRange.From() broadcast = ipRange.To() - return + return network, broadcast } func (h *Headscale) getAvailableIP(ipPrefix netaddr.IPPrefix) (*netaddr.IP, error) {