diff --git a/README.md b/README.md index 1c678fd1..1901ce63 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,13 @@ make build Hoàng Đức Hiếu + + + Mevan +
+ Mevan Samaratunga +
+ Michael @@ -283,6 +290,8 @@ make build Casey Marshall + + Silver @@ -290,8 +299,6 @@ make build Silver Bullet - - Stefan @@ -327,6 +334,8 @@ make build Artem Klevtsov + + Arthur @@ -334,8 +343,6 @@ make build Arthur Woimbée - - Bryan @@ -371,6 +378,8 @@ make build Jamie Greeff + + Jim @@ -378,8 +387,6 @@ make build Jim Tittsler - - Pierre @@ -415,6 +422,8 @@ make build Shaanan Cohney + + Tanner/ @@ -422,8 +431,6 @@ make build Tanner - - Teteros/ @@ -452,6 +459,15 @@ make build Tjerk Woudsma + + + Yang +
+ Yang Bin +
+ + + Zakhar @@ -466,8 +482,13 @@ make build ZiYuan - - + + + bravechamp/ +
+ bravechamp +
+ derelm/ @@ -489,6 +510,8 @@ make build lion24 + + pernila/ diff --git a/api.go b/api.go index 1023e6fc..eab8076e 100644 --- a/api.go +++ b/api.go @@ -568,8 +568,13 @@ func (h *Headscale) handleAuthKey( Str("func", "handleAuthKey"). Str("machine", registerRequest.Hostinfo.Hostname). Msg("Failed authentication via AuthKey") - machineRegistrations.WithLabelValues("new", RegisterMethodAuthKey, "error", pak.Namespace.Name). - Inc() + + if pak != nil { + machineRegistrations.WithLabelValues("new", RegisterMethodAuthKey, "error", pak.Namespace.Name). + Inc() + } else { + machineRegistrations.WithLabelValues("new", RegisterMethodAuthKey, "error").Inc() + } return } @@ -580,35 +585,51 @@ func (h *Headscale) handleAuthKey( Msg("Authentication key was valid, proceeding to acquire IP addresses") nodeKey := NodePublicKeyStripPrefix(registerRequest.NodeKey) - now := time.Now().UTC() - machineToRegister := Machine{ - Name: registerRequest.Hostinfo.Hostname, - NamespaceID: pak.Namespace.ID, - MachineKey: machineKeyStr, - RegisterMethod: RegisterMethodAuthKey, - Expiry: ®isterRequest.Expiry, - NodeKey: nodeKey, - LastSeen: &now, - AuthKeyID: uint(pak.ID), - } - - machine, err := h.RegisterMachine( - machineToRegister, - ) - if err != nil { - log.Error(). + // retrieve machine information if it exist + // The error is not important, because if it does not + // exist, then this is a new machine and we will move + // on to registration. + machine, _ := h.GetMachineByMachineKey(machineKey) + if machine != nil { + log.Trace(). Caller(). - Err(err). - Msg("could not register machine") - machineRegistrations.WithLabelValues("new", RegisterMethodAuthKey, "error", pak.Namespace.Name). - Inc() - ctx.String( - http.StatusInternalServerError, - "could not register machine", - ) + Str("machine", machine.Name). + Msg("machine already registered, refreshing with new auth key") - return + machine.NodeKey = nodeKey + machine.AuthKeyID = uint(pak.ID) + h.RefreshMachine(machine, registerRequest.Expiry) + } else { + now := time.Now().UTC() + machineToRegister := Machine{ + Name: registerRequest.Hostinfo.Hostname, + NamespaceID: pak.Namespace.ID, + MachineKey: machineKeyStr, + RegisterMethod: RegisterMethodAuthKey, + Expiry: ®isterRequest.Expiry, + NodeKey: nodeKey, + LastSeen: &now, + AuthKeyID: uint(pak.ID), + } + + machine, err = h.RegisterMachine( + machineToRegister, + ) + if err != nil { + log.Error(). + Caller(). + Err(err). + Msg("could not register machine") + machineRegistrations.WithLabelValues("new", RegisterMethodAuthKey, "error", pak.Namespace.Name). + Inc() + ctx.String( + http.StatusInternalServerError, + "could not register machine", + ) + + return + } } h.UsePreAuthKey(pak) diff --git a/app.go b/app.go index f1426bbb..1809f95c 100644 --- a/app.go +++ b/app.go @@ -409,8 +409,6 @@ func (h *Headscale) httpAuthenticationMiddleware(ctx *gin.Context) { return } - ctx.AbortWithStatus(http.StatusUnauthorized) - valid, err := h.ValidateAPIKey(strings.TrimPrefix(authHeader, AuthPrefix)) if err != nil { log.Error(). diff --git a/docs/running-headscale-container.md b/docs/running-headscale-container.md index d39f4d49..36e63de9 100644 --- a/docs/running-headscale-container.md +++ b/docs/running-headscale-container.md @@ -55,6 +55,7 @@ docker run \ --rm \ --volume $(pwd)/config:/etc/headscale/ \ --publish 127.0.0.1:8080:8080 \ + --publish 127.0.0.1:9090:9090 \ headscale/headscale: \ headscale serve @@ -80,7 +81,7 @@ docker ps Verify `headscale` is available: ```shell -curl http://127.0.0.1:8080/metrics +curl http://127.0.0.1:9090/metrics ``` 6. Create a namespace ([tailnet](https://tailscale.com/kb/1136/tailnet/)): diff --git a/docs/running-headscale-linux.md b/docs/running-headscale-linux.md index 09e43dc7..1e9d11c4 100644 --- a/docs/running-headscale-linux.md +++ b/docs/running-headscale-linux.md @@ -67,7 +67,7 @@ To run `headscale` in the background, please follow the steps in the [SystemD se Verify `headscale` is available: ```shell -curl http://127.0.0.1:8080/metrics +curl http://127.0.0.1:9090/metrics ``` 8. Create a namespace ([tailnet](https://tailscale.com/kb/1136/tailnet/)):