Linting here and there

This commit is contained in:
Juan Font Alonso 2022-03-06 17:35:54 +01:00
parent eb06054a7b
commit de2ea83b3b
3 changed files with 31 additions and 20 deletions

11
acls.go
View File

@ -17,12 +17,11 @@ import (
) )
const ( const (
errEmptyPolicy = Error("empty policy") errEmptyPolicy = Error("empty policy")
errInvalidAction = Error("invalid action") errInvalidAction = Error("invalid action")
errInvalidUserSection = Error("invalid user section") errInvalidGroup = Error("invalid group")
errInvalidGroup = Error("invalid group") errInvalidTag = Error("invalid tag")
errInvalidTag = Error("invalid tag") errInvalidPortFormat = Error("invalid port format")
errInvalidPortFormat = Error("invalid port format")
) )
const ( const (

View File

@ -2,7 +2,6 @@ package cli
import ( import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )

View File

@ -30,12 +30,13 @@ type DERPServer struct {
} }
func (h *Headscale) NewDERPServer() (*DERPServer, error) { func (h *Headscale) NewDERPServer() (*DERPServer, error) {
s := derp.NewServer(key.NodePrivate(*h.privateKey), log.Info().Msgf) server := derp.NewServer(key.NodePrivate(*h.privateKey), log.Info().Msgf)
region, err := h.generateRegionLocalDERP() region, err := h.generateRegionLocalDERP()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &DERPServer{s, region}, nil
return &DERPServer{server, region}, nil
} }
func (h *Headscale) generateRegionLocalDERP() (tailcfg.DERPRegion, error) { func (h *Headscale) generateRegionLocalDERP() (tailcfg.DERPRegion, error) {
@ -99,6 +100,7 @@ func (h *Headscale) DERPHandler(ctx *gin.Context) {
log.Warn().Caller().Msgf("Weird websockets connection upgrade: %q", up) log.Warn().Caller().Msgf("Weird websockets connection upgrade: %q", up)
} }
ctx.String(http.StatusUpgradeRequired, "DERP requires connection upgrade") ctx.String(http.StatusUpgradeRequired, "DERP requires connection upgrade")
return return
} }
@ -122,13 +124,14 @@ func (h *Headscale) DERPHandler(ctx *gin.Context) {
if !fastStart { if !fastStart {
pubKey := h.privateKey.Public() pubKey := h.privateKey.Public()
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"+
"Derp-Version: %v\r\n"+ "Derp-Version: %v\r\n"+
"Derp-Public-Key: %s\r\n\r\n", "Derp-Public-Key: %s\r\n\r\n",
derp.ProtocolVersion, derp.ProtocolVersion,
pubKey.UntypedHexString()) pubKeyStr)
} }
h.DERPServer.tailscaleDERP.Accept(netConn, conn, netConn.RemoteAddr().String()) h.DERPServer.tailscaleDERP.Accept(netConn, conn, netConn.RemoteAddr().String())
@ -163,6 +166,7 @@ func (h *Headscale) DERPBootstrapDNSHandler(ctx *gin.Context) {
addrs, err := r.LookupIP(resolvCtx, "ip", node.HostName) addrs, err := r.LookupIP(resolvCtx, "ip", node.HostName)
if err != nil { if err != nil {
log.Trace().Caller().Err(err).Msgf("bootstrap DNS lookup failed %q", node.HostName) log.Trace().Caller().Err(err).Msgf("bootstrap DNS lookup failed %q", node.HostName)
continue continue
} }
dnsEntries[node.HostName] = addrs dnsEntries[node.HostName] = addrs
@ -178,28 +182,34 @@ func (h *Headscale) ServeSTUN() {
log.Fatal().Msgf("failed to open STUN listener: %v", err) log.Fatal().Msgf("failed to open STUN listener: %v", err)
} }
log.Info().Msgf("STUN server started at %s", packetConn.LocalAddr()) log.Info().Msgf("STUN server started at %s", packetConn.LocalAddr())
serverSTUNListener(context.Background(), packetConn.(*net.UDPConn))
udpConn, ok := packetConn.(*net.UDPConn)
if !ok {
log.Fatal().Msg("STUN listener is not a UDP listener")
}
serverSTUNListener(context.Background(), udpConn)
} }
func serverSTUNListener(ctx context.Context, pc *net.UDPConn) { func serverSTUNListener(ctx context.Context, packetConn *net.UDPConn) {
var buf [64 << 10]byte var buf [64 << 10]byte
var ( var (
n int bytesRead int
ua *net.UDPAddr udpAddr *net.UDPAddr
err error err error
) )
for { for {
n, ua, err = pc.ReadFromUDP(buf[:]) bytesRead, udpAddr, err = packetConn.ReadFromUDP(buf[:])
if err != nil { if err != nil {
if ctx.Err() != nil { if ctx.Err() != nil {
return return
} }
log.Error().Caller().Err(err).Msgf("STUN ReadFrom") log.Error().Caller().Err(err).Msgf("STUN ReadFrom")
time.Sleep(time.Second) time.Sleep(time.Second)
continue continue
} }
log.Trace().Caller().Msgf("STUN request from %v", ua) log.Trace().Caller().Msgf("STUN request from %v", udpAddr)
pkt := buf[:n] pkt := buf[:bytesRead]
if !stun.Is(pkt) { if !stun.Is(pkt) {
continue continue
} }
@ -208,7 +218,10 @@ func serverSTUNListener(ctx context.Context, pc *net.UDPConn) {
continue continue
} }
res := stun.Response(txid, ua.IP, uint16(ua.Port)) res := stun.Response(txid, udpAddr.IP, uint16(udpAddr.Port))
pc.WriteTo(res, ua) _, err = packetConn.WriteTo(res, udpAddr)
if err != nil {
continue
}
} }
} }