chore: apply lint recommendations

This commit is contained in:
Adrien Raffin-Caboisse 2022-05-16 14:59:46 +02:00
parent 02ae7a0563
commit 4435a4f19d
No known key found for this signature in database
GPG key ID: 7FB60532DEBEAD6A
6 changed files with 26 additions and 17 deletions

View file

@ -262,7 +262,7 @@ func expandAlias(
} }
if len(ips) == 0 { if len(ips) == 0 {
return ips, fmt.Errorf( 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, errInvalidTag,
alias, alias,
) )

View file

@ -498,13 +498,13 @@ func nodesToPtables(
namespace = pterm.LightYellow(machine.Namespace.Name) namespace = pterm.LightYellow(machine.Namespace.Name)
} }
var IpV4Address string var IPV4Address string
var IpV6Address string var IPV6Address string
for _, addr := range machine.IpAddresses { for _, addr := range machine.IpAddresses {
if netaddr.MustParseIP(addr).Is4() { if netaddr.MustParseIP(addr).Is4() {
IpV4Address = addr IPV4Address = addr
} else { } else {
IpV6Address = addr IPV6Address = addr
} }
} }
@ -513,7 +513,7 @@ func nodesToPtables(
machine.Name, machine.Name,
nodeKey.ShortString(), nodeKey.ShortString(),
namespace, namespace,
strings.Join([]string{IpV4Address, IpV6Address}, ", "), strings.Join([]string{IPV4Address, IPV6Address}, ", "),
strconv.FormatBool(ephemeral), strconv.FormatBool(ephemeral),
lastSeenTime, lastSeenTime,
online, online,

View file

@ -128,7 +128,7 @@ func (h *Headscale) DERPHandler(ctx *gin.Context) {
if !fastStart { if !fastStart {
pubKey := h.privateKey.Public() pubKey := h.privateKey.Public()
pubKeyStr := pubKey.UntypedHexString() pubKeyStr := pubKey.UntypedHexString() // nolint
fmt.Fprintf(conn, "HTTP/1.1 101 Switching Protocols\r\n"+ fmt.Fprintf(conn, "HTTP/1.1 101 Switching Protocols\r\n"+
"Upgrade: DERP\r\n"+ "Upgrade: DERP\r\n"+
"Connection: Upgrade\r\n"+ "Connection: Upgrade\r\n"+

View file

@ -664,9 +664,11 @@ func getTags(
aclPolicy *ACLPolicy, aclPolicy *ACLPolicy,
machine Machine, machine Machine,
stripEmailDomain bool, stripEmailDomain bool,
) (validTags []string, invalidTags []string) { ) ([]string, []string) {
validTags := make([]string, 0)
invalidTags := make([]string, 0)
if aclPolicy == nil { if aclPolicy == nil {
return return validTags, invalidTags
} }
validTagMap := make(map[string]bool) validTagMap := make(map[string]bool)
invalidTagMap := make(map[string]bool) invalidTagMap := make(map[string]bool)

10
poll.go
View file

@ -20,6 +20,10 @@ const (
updateCheckInterval = 10 * time.Second updateCheckInterval = 10 * time.Second
) )
type contextKey string
const machineNameContextKey = contextKey("machineName")
// PollNetMapHandler takes care of /machine/:id/map // PollNetMapHandler takes care of /machine/:id/map
// //
// This is the busiest endpoint, as it keeps the HTTP long poll that updates // This is the busiest endpoint, as it keeps the HTTP long poll that updates
@ -272,7 +276,7 @@ func (h *Headscale) PollNetMapStream(
return return
} }
ctx := context.WithValue(ctx.Request.Context(), "machineName", machine.Name) ctx := context.WithValue(ctx.Request.Context(), machineNameContextKey, machine.Name)
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
@ -576,12 +580,12 @@ func (h *Headscale) scheduledPollWorker(
defer closeChanWithLog( defer closeChanWithLog(
updateChan, updateChan,
fmt.Sprint(ctx.Value("machineName")), fmt.Sprint(ctx.Value(machineNameContextKey)),
"updateChan", "updateChan",
) )
defer closeChanWithLog( defer closeChanWithLog(
keepAliveChan, keepAliveChan,
fmt.Sprint(ctx.Value("machineName")), fmt.Sprint(ctx.Value(machineNameContextKey)),
"updateChan", "updateChan",
) )

View file

@ -136,26 +136,29 @@ func encode(
return privKey.SealTo(*pubKey, b), nil 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 ipPrefixes := h.cfg.IPPrefixes
for _, ipPrefix := range ipPrefixes { for _, ipPrefix := range ipPrefixes {
var ip *netaddr.IP var ip *netaddr.IP
ip, err = h.getAvailableIP(ipPrefix) ip, err = h.getAvailableIP(ipPrefix)
if err != nil { if err != nil {
return return ips, err
} }
ips = append(ips, *ip) 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() ipRange := na.Range()
network = ipRange.From() network = ipRange.From()
broadcast = ipRange.To() broadcast = ipRange.To()
return return network, broadcast
} }
func (h *Headscale) getAvailableIP(ipPrefix netaddr.IPPrefix) (*netaddr.IP, error) { func (h *Headscale) getAvailableIP(ipPrefix netaddr.IPPrefix) (*netaddr.IP, error) {