mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-10-28 10:51:44 +01:00 
			
		
		
		
	Merge pull request #74 from kradalby/deadlock-logging
Switch to a structured logger
This commit is contained in:
		
						commit
						d421c7b665
					
				@ -119,6 +119,11 @@ Headscale's configuration file is named `config.json` or `config.yaml`. Headscal
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
`server_url` is the external URL via which Headscale is reachable. `listen_addr` is the IP address and port the Headscale program should listen on. `ip_prefix` is the IP prefix (range) in which IP addresses for nodes will be allocated (default 100.64.0.0/10, e.g., 192.168.4.0/24, 10.0.0.0/8)
 | 
					`server_url` is the external URL via which Headscale is reachable. `listen_addr` is the IP address and port the Headscale program should listen on. `ip_prefix` is the IP prefix (range) in which IP addresses for nodes will be allocated (default 100.64.0.0/10, e.g., 192.168.4.0/24, 10.0.0.0/8)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					    "log_level": "debug"
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					`log_level` can be used to set the Log level for Headscale, it defaults to `debug`, and the available levels are: `trace`, `debug`, `info`, `warn` and `error`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
    "private_key_path": "private.key",
 | 
					    "private_key_path": "private.key",
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										9
									
								
								acls.go
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								acls.go
									
									
									
									
									
								
							@ -4,11 +4,12 @@ import (
 | 
				
			|||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"log"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/rs/zerolog/log"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/tailscale/hujson"
 | 
						"github.com/tailscale/hujson"
 | 
				
			||||||
	"inet.af/netaddr"
 | 
						"inet.af/netaddr"
 | 
				
			||||||
	"tailscale.com/tailcfg"
 | 
						"tailscale.com/tailcfg"
 | 
				
			||||||
@ -66,7 +67,8 @@ func (h *Headscale) generateACLRules() (*[]tailcfg.FilterRule, error) {
 | 
				
			|||||||
		for j, u := range a.Users {
 | 
							for j, u := range a.Users {
 | 
				
			||||||
			srcs, err := h.generateACLPolicySrcIP(u)
 | 
								srcs, err := h.generateACLPolicySrcIP(u)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Printf("Error parsing ACL %d, User %d", i, j)
 | 
									log.Error().
 | 
				
			||||||
 | 
										Msgf("Error parsing ACL %d, User %d", i, j)
 | 
				
			||||||
				return nil, err
 | 
									return nil, err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			srcIPs = append(srcIPs, *srcs...)
 | 
								srcIPs = append(srcIPs, *srcs...)
 | 
				
			||||||
@ -77,7 +79,8 @@ func (h *Headscale) generateACLRules() (*[]tailcfg.FilterRule, error) {
 | 
				
			|||||||
		for j, d := range a.Ports {
 | 
							for j, d := range a.Ports {
 | 
				
			||||||
			dests, err := h.generateACLPolicyDestPorts(d)
 | 
								dests, err := h.generateACLPolicyDestPorts(d)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Printf("Error parsing ACL %d, Port %d", i, j)
 | 
									log.Error().
 | 
				
			||||||
 | 
										Msgf("Error parsing ACL %d, Port %d", i, j)
 | 
				
			||||||
				return nil, err
 | 
									return nil, err
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			destPorts = append(destPorts, *dests...)
 | 
								destPorts = append(destPorts, *dests...)
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										271
									
								
								api.go
									
									
									
									
									
								
							
							
						
						
									
										271
									
								
								api.go
									
									
									
									
									
								
							@ -6,10 +6,11 @@ import (
 | 
				
			|||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"log"
 | 
					 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/rs/zerolog/log"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/gin-gonic/gin"
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
	"github.com/klauspost/compress/zstd"
 | 
						"github.com/klauspost/compress/zstd"
 | 
				
			||||||
	"gorm.io/datatypes"
 | 
						"gorm.io/datatypes"
 | 
				
			||||||
@ -63,21 +64,27 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) {
 | 
				
			|||||||
	mKeyStr := c.Param("id")
 | 
						mKeyStr := c.Param("id")
 | 
				
			||||||
	mKey, err := wgkey.ParseHex(mKeyStr)
 | 
						mKey, err := wgkey.ParseHex(mKeyStr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("Cannot parse machine key: %s", err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("handler", "Registration").
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Cannot parse machine key")
 | 
				
			||||||
		c.String(http.StatusInternalServerError, "Sad!")
 | 
							c.String(http.StatusInternalServerError, "Sad!")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	req := tailcfg.RegisterRequest{}
 | 
						req := tailcfg.RegisterRequest{}
 | 
				
			||||||
	err = decode(body, &req, &mKey, h.privateKey)
 | 
						err = decode(body, &req, &mKey, h.privateKey)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("Cannot decode message: %s", err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("handler", "Registration").
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Cannot decode message")
 | 
				
			||||||
		c.String(http.StatusInternalServerError, "Very sad!")
 | 
							c.String(http.StatusInternalServerError, "Very sad!")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var m Machine
 | 
						var m Machine
 | 
				
			||||||
	if result := h.db.Preload("Namespace").First(&m, "machine_key = ?", mKey.HexString()); errors.Is(result.Error, gorm.ErrRecordNotFound) {
 | 
						if result := h.db.Preload("Namespace").First(&m, "machine_key = ?", mKey.HexString()); errors.Is(result.Error, gorm.ErrRecordNotFound) {
 | 
				
			||||||
		log.Println("New Machine!")
 | 
							log.Info().Str("machine", req.Hostinfo.Hostname).Msg("New machine")
 | 
				
			||||||
		m = Machine{
 | 
							m = Machine{
 | 
				
			||||||
			Expiry:     &req.Expiry,
 | 
								Expiry:     &req.Expiry,
 | 
				
			||||||
			MachineKey: mKey.HexString(),
 | 
								MachineKey: mKey.HexString(),
 | 
				
			||||||
@ -85,7 +92,10 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) {
 | 
				
			|||||||
			NodeKey:    wgkey.Key(req.NodeKey).HexString(),
 | 
								NodeKey:    wgkey.Key(req.NodeKey).HexString(),
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if err := h.db.Create(&m).Error; err != nil {
 | 
							if err := h.db.Create(&m).Error; err != nil {
 | 
				
			||||||
			log.Printf("Could not create row: %s", err)
 | 
								log.Error().
 | 
				
			||||||
 | 
									Str("handler", "Registration").
 | 
				
			||||||
 | 
									Err(err).
 | 
				
			||||||
 | 
									Msg("Could not create row")
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -100,13 +110,20 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) {
 | 
				
			|||||||
	// We have the updated key!
 | 
						// We have the updated key!
 | 
				
			||||||
	if m.NodeKey == wgkey.Key(req.NodeKey).HexString() {
 | 
						if m.NodeKey == wgkey.Key(req.NodeKey).HexString() {
 | 
				
			||||||
		if m.Registered {
 | 
							if m.Registered {
 | 
				
			||||||
			log.Printf("[%s] Client is registered and we have the current NodeKey. All clear to /map", m.Name)
 | 
								log.Debug().
 | 
				
			||||||
 | 
									Str("handler", "Registration").
 | 
				
			||||||
 | 
									Str("machine", m.Name).
 | 
				
			||||||
 | 
									Msg("Client is registered and we have the current NodeKey. All clear to /mSending keepaliveap")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			resp.AuthURL = ""
 | 
								resp.AuthURL = ""
 | 
				
			||||||
			resp.MachineAuthorized = true
 | 
								resp.MachineAuthorized = true
 | 
				
			||||||
			resp.User = *m.Namespace.toUser()
 | 
								resp.User = *m.Namespace.toUser()
 | 
				
			||||||
			respBody, err := encode(resp, &mKey, h.privateKey)
 | 
								respBody, err := encode(resp, &mKey, h.privateKey)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Printf("Cannot encode message: %s", err)
 | 
									log.Error().
 | 
				
			||||||
 | 
										Str("handler", "Registration").
 | 
				
			||||||
 | 
										Err(err).
 | 
				
			||||||
 | 
										Msg("Cannot encode message")
 | 
				
			||||||
				c.String(http.StatusInternalServerError, "")
 | 
									c.String(http.StatusInternalServerError, "")
 | 
				
			||||||
				return
 | 
									return
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@ -114,12 +131,18 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) {
 | 
				
			|||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		log.Printf("[%s] Not registered and not NodeKey rotation. Sending a authurl to register", m.Name)
 | 
							log.Debug().
 | 
				
			||||||
 | 
								Str("handler", "Registration").
 | 
				
			||||||
 | 
								Str("machine", m.Name).
 | 
				
			||||||
 | 
								Msg("Not registered and not NodeKey rotation. Sending a authurl to register")
 | 
				
			||||||
		resp.AuthURL = fmt.Sprintf("%s/register?key=%s",
 | 
							resp.AuthURL = fmt.Sprintf("%s/register?key=%s",
 | 
				
			||||||
			h.cfg.ServerURL, mKey.HexString())
 | 
								h.cfg.ServerURL, mKey.HexString())
 | 
				
			||||||
		respBody, err := encode(resp, &mKey, h.privateKey)
 | 
							respBody, err := encode(resp, &mKey, h.privateKey)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Printf("Cannot encode message: %s", err)
 | 
								log.Error().
 | 
				
			||||||
 | 
									Str("handler", "Registration").
 | 
				
			||||||
 | 
									Err(err).
 | 
				
			||||||
 | 
									Msg("Cannot encode message")
 | 
				
			||||||
			c.String(http.StatusInternalServerError, "")
 | 
								c.String(http.StatusInternalServerError, "")
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@ -129,7 +152,10 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// The NodeKey we have matches OldNodeKey, which means this is a refresh after an key expiration
 | 
						// The NodeKey we have matches OldNodeKey, which means this is a refresh after an key expiration
 | 
				
			||||||
	if m.NodeKey == wgkey.Key(req.OldNodeKey).HexString() {
 | 
						if m.NodeKey == wgkey.Key(req.OldNodeKey).HexString() {
 | 
				
			||||||
		log.Printf("[%s] We have the OldNodeKey in the database. This is a key refresh", m.Name)
 | 
							log.Debug().
 | 
				
			||||||
 | 
								Str("handler", "Registration").
 | 
				
			||||||
 | 
								Str("machine", m.Name).
 | 
				
			||||||
 | 
								Msg("We have the OldNodeKey in the database. This is a key refresh")
 | 
				
			||||||
		m.NodeKey = wgkey.Key(req.NodeKey).HexString()
 | 
							m.NodeKey = wgkey.Key(req.NodeKey).HexString()
 | 
				
			||||||
		h.db.Save(&m)
 | 
							h.db.Save(&m)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -137,7 +163,10 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) {
 | 
				
			|||||||
		resp.User = *m.Namespace.toUser()
 | 
							resp.User = *m.Namespace.toUser()
 | 
				
			||||||
		respBody, err := encode(resp, &mKey, h.privateKey)
 | 
							respBody, err := encode(resp, &mKey, h.privateKey)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Printf("Cannot encode message: %s", err)
 | 
								log.Error().
 | 
				
			||||||
 | 
									Str("handler", "Registration").
 | 
				
			||||||
 | 
									Err(err).
 | 
				
			||||||
 | 
									Msg("Cannot encode message")
 | 
				
			||||||
			c.String(http.StatusInternalServerError, "Extremely sad!")
 | 
								c.String(http.StatusInternalServerError, "Extremely sad!")
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@ -148,25 +177,38 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) {
 | 
				
			|||||||
	// We arrive here after a client is restarted without finalizing the authentication flow or
 | 
						// We arrive here after a client is restarted without finalizing the authentication flow or
 | 
				
			||||||
	// when headscale is stopped in the middle of the auth process.
 | 
						// when headscale is stopped in the middle of the auth process.
 | 
				
			||||||
	if m.Registered {
 | 
						if m.Registered {
 | 
				
			||||||
		log.Printf("[%s] The node is sending us a new NodeKey, but machine is registered. All clear for /map", m.Name)
 | 
							log.Debug().
 | 
				
			||||||
 | 
								Str("handler", "Registration").
 | 
				
			||||||
 | 
								Str("machine", m.Name).
 | 
				
			||||||
 | 
								Msg("The node is sending us a new NodeKey, but machine is registered. All clear for /map")
 | 
				
			||||||
		resp.AuthURL = ""
 | 
							resp.AuthURL = ""
 | 
				
			||||||
		resp.MachineAuthorized = true
 | 
							resp.MachineAuthorized = true
 | 
				
			||||||
		resp.User = *m.Namespace.toUser()
 | 
							resp.User = *m.Namespace.toUser()
 | 
				
			||||||
		respBody, err := encode(resp, &mKey, h.privateKey)
 | 
							respBody, err := encode(resp, &mKey, h.privateKey)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Printf("Cannot encode message: %s", err)
 | 
								log.Error().
 | 
				
			||||||
 | 
									Str("handler", "Registration").
 | 
				
			||||||
 | 
									Err(err).
 | 
				
			||||||
 | 
									Msg("Cannot encode message")
 | 
				
			||||||
			c.String(http.StatusInternalServerError, "")
 | 
								c.String(http.StatusInternalServerError, "")
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		c.Data(200, "application/json; charset=utf-8", respBody)
 | 
							c.Data(200, "application/json; charset=utf-8", respBody)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	log.Printf("[%s] The node is sending us a new NodeKey, sending auth url", m.Name)
 | 
					
 | 
				
			||||||
 | 
						log.Debug().
 | 
				
			||||||
 | 
							Str("handler", "Registration").
 | 
				
			||||||
 | 
							Str("machine", m.Name).
 | 
				
			||||||
 | 
							Msg("The node is sending us a new NodeKey, sending auth url")
 | 
				
			||||||
	resp.AuthURL = fmt.Sprintf("%s/register?key=%s",
 | 
						resp.AuthURL = fmt.Sprintf("%s/register?key=%s",
 | 
				
			||||||
		h.cfg.ServerURL, mKey.HexString())
 | 
							h.cfg.ServerURL, mKey.HexString())
 | 
				
			||||||
	respBody, err := encode(resp, &mKey, h.privateKey)
 | 
						respBody, err := encode(resp, &mKey, h.privateKey)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("Cannot encode message: %s", err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("handler", "Registration").
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Cannot encode message")
 | 
				
			||||||
		c.String(http.StatusInternalServerError, "")
 | 
							c.String(http.StatusInternalServerError, "")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -183,28 +225,45 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) {
 | 
				
			|||||||
//
 | 
					//
 | 
				
			||||||
// At this moment the updates are sent in a quite horrendous way, but they kinda work.
 | 
					// At this moment the updates are sent in a quite horrendous way, but they kinda work.
 | 
				
			||||||
func (h *Headscale) PollNetMapHandler(c *gin.Context) {
 | 
					func (h *Headscale) PollNetMapHandler(c *gin.Context) {
 | 
				
			||||||
 | 
						log.Trace().
 | 
				
			||||||
 | 
							Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
							Str("id", c.Param("id")).
 | 
				
			||||||
 | 
							Msg("PollNetMapHandler called")
 | 
				
			||||||
	body, _ := io.ReadAll(c.Request.Body)
 | 
						body, _ := io.ReadAll(c.Request.Body)
 | 
				
			||||||
	mKeyStr := c.Param("id")
 | 
						mKeyStr := c.Param("id")
 | 
				
			||||||
	mKey, err := wgkey.ParseHex(mKeyStr)
 | 
						mKey, err := wgkey.ParseHex(mKeyStr)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("Cannot parse client key: %s", err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Cannot parse client key")
 | 
				
			||||||
		c.String(http.StatusBadRequest, "")
 | 
							c.String(http.StatusBadRequest, "")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	req := tailcfg.MapRequest{}
 | 
						req := tailcfg.MapRequest{}
 | 
				
			||||||
	err = decode(body, &req, &mKey, h.privateKey)
 | 
						err = decode(body, &req, &mKey, h.privateKey)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("Cannot decode message: %s", err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Cannot decode message")
 | 
				
			||||||
		c.String(http.StatusBadRequest, "")
 | 
							c.String(http.StatusBadRequest, "")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var m Machine
 | 
						var m Machine
 | 
				
			||||||
	if result := h.db.Preload("Namespace").First(&m, "machine_key = ?", mKey.HexString()); errors.Is(result.Error, gorm.ErrRecordNotFound) {
 | 
						if result := h.db.Preload("Namespace").First(&m, "machine_key = ?", mKey.HexString()); errors.Is(result.Error, gorm.ErrRecordNotFound) {
 | 
				
			||||||
		log.Printf("Ignoring request, cannot find machine with key %s", mKey.HexString())
 | 
							log.Warn().
 | 
				
			||||||
 | 
								Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
								Msgf("Ignoring request, cannot find machine with key %s", mKey.HexString())
 | 
				
			||||||
		c.String(http.StatusUnauthorized, "")
 | 
							c.String(http.StatusUnauthorized, "")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						log.Trace().
 | 
				
			||||||
 | 
							Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
							Str("id", c.Param("id")).
 | 
				
			||||||
 | 
							Str("machine", m.Name).
 | 
				
			||||||
 | 
							Msg("Found machine in database")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	hostinfo, _ := json.Marshal(req.Hostinfo)
 | 
						hostinfo, _ := json.Marshal(req.Hostinfo)
 | 
				
			||||||
	m.Name = req.Hostinfo.Hostname
 | 
						m.Name = req.Hostinfo.Hostname
 | 
				
			||||||
@ -227,17 +286,36 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	h.db.Save(&m)
 | 
						h.db.Save(&m)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pollData := make(chan []byte, 1)
 | 
					 | 
				
			||||||
	update := make(chan []byte, 1)
 | 
						update := make(chan []byte, 1)
 | 
				
			||||||
	cancelKeepAlive := make(chan []byte, 1)
 | 
					
 | 
				
			||||||
 | 
						pollData := make(chan []byte, 1)
 | 
				
			||||||
	defer close(pollData)
 | 
						defer close(pollData)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cancelKeepAlive := make(chan []byte, 1)
 | 
				
			||||||
	defer close(cancelKeepAlive)
 | 
						defer close(cancelKeepAlive)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log.Trace().
 | 
				
			||||||
 | 
							Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
							Str("id", c.Param("id")).
 | 
				
			||||||
 | 
							Str("machine", m.Name).
 | 
				
			||||||
 | 
							Msg("Locking poll mutex")
 | 
				
			||||||
	h.pollMu.Lock()
 | 
						h.pollMu.Lock()
 | 
				
			||||||
	h.clientsPolling[m.ID] = update
 | 
						h.clientsPolling[m.ID] = update
 | 
				
			||||||
	h.pollMu.Unlock()
 | 
						h.pollMu.Unlock()
 | 
				
			||||||
 | 
						log.Trace().
 | 
				
			||||||
 | 
							Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
							Str("id", c.Param("id")).
 | 
				
			||||||
 | 
							Str("machine", m.Name).
 | 
				
			||||||
 | 
							Msg("Unlocking poll mutex")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	data, err := h.getMapResponse(mKey, req, m)
 | 
						data, err := h.getMapResponse(mKey, req, m)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
								Str("id", c.Param("id")).
 | 
				
			||||||
 | 
								Str("machine", m.Name).
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Failed to get Map response")
 | 
				
			||||||
		c.String(http.StatusInternalServerError, ":(")
 | 
							c.String(http.StatusInternalServerError, ":(")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -247,37 +325,71 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
 | 
				
			|||||||
	// empty endpoints to peers)
 | 
						// empty endpoints to peers)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Details on the protocol can be found in https://github.com/tailscale/tailscale/blob/main/tailcfg/tailcfg.go#L696
 | 
						// Details on the protocol can be found in https://github.com/tailscale/tailscale/blob/main/tailcfg/tailcfg.go#L696
 | 
				
			||||||
	log.Printf("[%s] ReadOnly=%t   OmitPeers=%t    Stream=%t", m.Name, req.ReadOnly, req.OmitPeers, req.Stream)
 | 
						log.Debug().
 | 
				
			||||||
 | 
							Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
							Str("id", c.Param("id")).
 | 
				
			||||||
 | 
							Str("machine", m.Name).
 | 
				
			||||||
 | 
							Bool("readOnly", req.ReadOnly).
 | 
				
			||||||
 | 
							Bool("omitPeers", req.OmitPeers).
 | 
				
			||||||
 | 
							Bool("stream", req.Stream).
 | 
				
			||||||
 | 
							Msg("Client map request processed")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if req.ReadOnly {
 | 
						if req.ReadOnly {
 | 
				
			||||||
		log.Printf("[%s] Client is starting up. Asking for DERP map", m.Name)
 | 
							log.Info().
 | 
				
			||||||
 | 
								Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
								Str("machine", m.Name).
 | 
				
			||||||
 | 
								Msg("Client is starting up. Asking for DERP map")
 | 
				
			||||||
		c.Data(200, "application/json; charset=utf-8", *data)
 | 
							c.Data(200, "application/json; charset=utf-8", *data)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if req.OmitPeers && !req.Stream {
 | 
						if req.OmitPeers && !req.Stream {
 | 
				
			||||||
		log.Printf("[%s] Client sent endpoint update and is ok with a response without peer list", m.Name)
 | 
							log.Info().
 | 
				
			||||||
 | 
								Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
								Str("machine", m.Name).
 | 
				
			||||||
 | 
								Msg("Client sent endpoint update and is ok with a response without peer list")
 | 
				
			||||||
		c.Data(200, "application/json; charset=utf-8", *data)
 | 
							c.Data(200, "application/json; charset=utf-8", *data)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	} else if req.OmitPeers && req.Stream {
 | 
						} else if req.OmitPeers && req.Stream {
 | 
				
			||||||
		log.Printf("[%s] Warning, ignoring request, don't know how to handle it", m.Name)
 | 
							log.Warn().
 | 
				
			||||||
 | 
								Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
								Str("machine", m.Name).
 | 
				
			||||||
 | 
								Msg("Ignoring request, don't know how to handle it")
 | 
				
			||||||
		c.String(http.StatusBadRequest, "")
 | 
							c.String(http.StatusBadRequest, "")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	log.Printf("[%s] Client is ready to access the tailnet", m.Name)
 | 
						log.Info().
 | 
				
			||||||
	log.Printf("[%s] Sending initial map", m.Name)
 | 
							Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
							Str("machine", m.Name).
 | 
				
			||||||
 | 
							Msg("Client is ready to access the tailnet")
 | 
				
			||||||
 | 
						log.Info().
 | 
				
			||||||
 | 
							Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
							Str("machine", m.Name).
 | 
				
			||||||
 | 
							Msg("Sending initial map")
 | 
				
			||||||
	pollData <- *data
 | 
						pollData <- *data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	log.Printf("[%s] Notifying peers", m.Name)
 | 
						log.Info().
 | 
				
			||||||
 | 
							Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
							Str("machine", m.Name).
 | 
				
			||||||
 | 
							Msg("Notifying peers")
 | 
				
			||||||
	peers, _ := h.getPeers(m)
 | 
						peers, _ := h.getPeers(m)
 | 
				
			||||||
	h.pollMu.Lock()
 | 
						h.pollMu.Lock()
 | 
				
			||||||
	for _, p := range *peers {
 | 
						for _, p := range *peers {
 | 
				
			||||||
		pUp, ok := h.clientsPolling[uint64(p.ID)]
 | 
							pUp, ok := h.clientsPolling[uint64(p.ID)]
 | 
				
			||||||
		if ok {
 | 
							if ok {
 | 
				
			||||||
			log.Printf("[%s] Notifying peer %s (%s)", m.Name, p.Name, p.Addresses[0])
 | 
								log.Info().
 | 
				
			||||||
 | 
									Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
									Str("machine", m.Name).
 | 
				
			||||||
 | 
									Str("peer", m.Name).
 | 
				
			||||||
 | 
									Str("address", p.Addresses[0].String()).
 | 
				
			||||||
 | 
									Msgf("Notifying peer %s (%s)", p.Name, p.Addresses[0])
 | 
				
			||||||
			pUp <- []byte{}
 | 
								pUp <- []byte{}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			log.Printf("[%s] Peer %s does not appear to be polling", m.Name, p.Name)
 | 
								log.Info().
 | 
				
			||||||
 | 
									Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
									Str("machine", m.Name).
 | 
				
			||||||
 | 
									Str("peer", m.Name).
 | 
				
			||||||
 | 
									Msgf("Peer %s does not appear to be polling", p.Name)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	h.pollMu.Unlock()
 | 
						h.pollMu.Unlock()
 | 
				
			||||||
@ -287,10 +399,18 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
 | 
				
			|||||||
	c.Stream(func(w io.Writer) bool {
 | 
						c.Stream(func(w io.Writer) bool {
 | 
				
			||||||
		select {
 | 
							select {
 | 
				
			||||||
		case data := <-pollData:
 | 
							case data := <-pollData:
 | 
				
			||||||
			log.Printf("[%s] Sending data (%d bytes)", m.Name, len(data))
 | 
								log.Trace().
 | 
				
			||||||
 | 
									Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
									Str("machine", m.Name).
 | 
				
			||||||
 | 
									Int("bytes", len(data)).
 | 
				
			||||||
 | 
									Msg("Sending data")
 | 
				
			||||||
			_, err := w.Write(data)
 | 
								_, err := w.Write(data)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Printf("[%s] Cannot write data: %s", m.Name, err)
 | 
									log.Error().
 | 
				
			||||||
 | 
										Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
										Str("machine", m.Name).
 | 
				
			||||||
 | 
										Err(err).
 | 
				
			||||||
 | 
										Msg("Cannot write data")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			now := time.Now().UTC()
 | 
								now := time.Now().UTC()
 | 
				
			||||||
			m.LastSeen = &now
 | 
								m.LastSeen = &now
 | 
				
			||||||
@ -298,19 +418,33 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
 | 
				
			|||||||
			return true
 | 
								return true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		case <-update:
 | 
							case <-update:
 | 
				
			||||||
			log.Printf("[%s] Received a request for update", m.Name)
 | 
								log.Debug().
 | 
				
			||||||
 | 
									Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
									Str("machine", m.Name).
 | 
				
			||||||
 | 
									Msg("Received a request for update")
 | 
				
			||||||
			data, err := h.getMapResponse(mKey, req, m)
 | 
								data, err := h.getMapResponse(mKey, req, m)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Printf("[%s] Could not get the map update: %s", m.Name, err)
 | 
									log.Error().
 | 
				
			||||||
 | 
										Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
										Str("machine", m.Name).
 | 
				
			||||||
 | 
										Err(err).
 | 
				
			||||||
 | 
										Msg("Could not get the map update")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			_, err = w.Write(*data)
 | 
								_, err = w.Write(*data)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Printf("[%s] Could not write the map response: %s", m.Name, err)
 | 
									log.Error().
 | 
				
			||||||
 | 
										Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
										Str("machine", m.Name).
 | 
				
			||||||
 | 
										Err(err).
 | 
				
			||||||
 | 
										Msg("Could not write the map response")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			return true
 | 
								return true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		case <-c.Request.Context().Done():
 | 
							case <-c.Request.Context().Done():
 | 
				
			||||||
			log.Printf("[%s] The client has closed the connection", m.Name)
 | 
								log.Info().
 | 
				
			||||||
 | 
									Str("handler", "PollNetMap").
 | 
				
			||||||
 | 
									Str("machine", m.Name).
 | 
				
			||||||
 | 
									Msg("The client has closed the connection")
 | 
				
			||||||
			now := time.Now().UTC()
 | 
								now := time.Now().UTC()
 | 
				
			||||||
			m.LastSeen = &now
 | 
								m.LastSeen = &now
 | 
				
			||||||
			h.db.Save(&m)
 | 
								h.db.Save(&m)
 | 
				
			||||||
@ -335,10 +469,16 @@ func (h *Headscale) keepAlive(cancel chan []byte, pollData chan []byte, mKey wgk
 | 
				
			|||||||
			h.pollMu.Lock()
 | 
								h.pollMu.Lock()
 | 
				
			||||||
			data, err := h.getMapKeepAliveResponse(mKey, req, m)
 | 
								data, err := h.getMapKeepAliveResponse(mKey, req, m)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Printf("Error generating the keep alive msg: %s", err)
 | 
									log.Error().
 | 
				
			||||||
 | 
										Str("func", "keepAlive").
 | 
				
			||||||
 | 
										Err(err).
 | 
				
			||||||
 | 
										Msg("Error generating the keep alive msg")
 | 
				
			||||||
				return
 | 
									return
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			log.Printf("[%s] Sending keepalive", m.Name)
 | 
								log.Debug().
 | 
				
			||||||
 | 
									Str("func", "keepAlive").
 | 
				
			||||||
 | 
									Str("machine", m.Name).
 | 
				
			||||||
 | 
									Msg("Sending keepalive")
 | 
				
			||||||
			pollData <- *data
 | 
								pollData <- *data
 | 
				
			||||||
			h.pollMu.Unlock()
 | 
								h.pollMu.Unlock()
 | 
				
			||||||
			time.Sleep(60 * time.Second)
 | 
								time.Sleep(60 * time.Second)
 | 
				
			||||||
@ -347,14 +487,24 @@ func (h *Headscale) keepAlive(cancel chan []byte, pollData chan []byte, mKey wgk
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (h *Headscale) getMapResponse(mKey wgkey.Key, req tailcfg.MapRequest, m Machine) (*[]byte, error) {
 | 
					func (h *Headscale) getMapResponse(mKey wgkey.Key, req tailcfg.MapRequest, m Machine) (*[]byte, error) {
 | 
				
			||||||
 | 
						log.Trace().
 | 
				
			||||||
 | 
							Str("func", "getMapResponse").
 | 
				
			||||||
 | 
							Str("machine", req.Hostinfo.Hostname).
 | 
				
			||||||
 | 
							Msg("Creating Map response")
 | 
				
			||||||
	node, err := m.toNode()
 | 
						node, err := m.toNode()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("Cannot convert to node: %s", err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("func", "getMapResponse").
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Cannot convert to node")
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	peers, err := h.getPeers(m)
 | 
						peers, err := h.getPeers(m)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("Cannot fetch peers: %s", err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("func", "getMapResponse").
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Cannot fetch peers")
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -426,26 +576,49 @@ func (h *Headscale) getMapKeepAliveResponse(mKey wgkey.Key, req tailcfg.MapReque
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (h *Headscale) handleAuthKey(c *gin.Context, db *gorm.DB, idKey wgkey.Key, req tailcfg.RegisterRequest, m Machine) {
 | 
					func (h *Headscale) handleAuthKey(c *gin.Context, db *gorm.DB, idKey wgkey.Key, req tailcfg.RegisterRequest, m Machine) {
 | 
				
			||||||
 | 
						log.Debug().
 | 
				
			||||||
 | 
							Str("func", "handleAuthKey").
 | 
				
			||||||
 | 
							Str("machine", req.Hostinfo.Hostname).
 | 
				
			||||||
 | 
							Msgf("Processing auth key for %s", req.Hostinfo.Hostname)
 | 
				
			||||||
	resp := tailcfg.RegisterResponse{}
 | 
						resp := tailcfg.RegisterResponse{}
 | 
				
			||||||
	pak, err := h.checkKeyValidity(req.Auth.AuthKey)
 | 
						pak, err := h.checkKeyValidity(req.Auth.AuthKey)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		resp.MachineAuthorized = false
 | 
							resp.MachineAuthorized = false
 | 
				
			||||||
		respBody, err := encode(resp, &idKey, h.privateKey)
 | 
							respBody, err := encode(resp, &idKey, h.privateKey)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Printf("Cannot encode message: %s", err)
 | 
								log.Error().
 | 
				
			||||||
 | 
									Str("func", "handleAuthKey").
 | 
				
			||||||
 | 
									Str("machine", m.Name).
 | 
				
			||||||
 | 
									Err(err).
 | 
				
			||||||
 | 
									Msg("Cannot encode message")
 | 
				
			||||||
			c.String(http.StatusInternalServerError, "")
 | 
								c.String(http.StatusInternalServerError, "")
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		c.Data(200, "application/json; charset=utf-8", respBody)
 | 
							c.Data(200, "application/json; charset=utf-8", respBody)
 | 
				
			||||||
		log.Printf("[%s] Failed authentication via AuthKey", m.Name)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("func", "handleAuthKey").
 | 
				
			||||||
 | 
								Str("machine", m.Name).
 | 
				
			||||||
 | 
								Msg("Failed authentication via AuthKey")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log.Debug().
 | 
				
			||||||
 | 
							Str("func", "handleAuthKey").
 | 
				
			||||||
 | 
							Str("machine", m.Name).
 | 
				
			||||||
 | 
							Msg("Authentication key was valid, proceeding to acquire an IP address")
 | 
				
			||||||
	ip, err := h.getAvailableIP()
 | 
						ip, err := h.getAvailableIP()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Println(err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("func", "handleAuthKey").
 | 
				
			||||||
 | 
								Str("machine", m.Name).
 | 
				
			||||||
 | 
								Msg("Failed to find an available IP")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	log.Printf("Assigning %s to %s", ip, m.Name)
 | 
						log.Info().
 | 
				
			||||||
 | 
							Str("func", "handleAuthKey").
 | 
				
			||||||
 | 
							Str("machine", m.Name).
 | 
				
			||||||
 | 
							Str("ip", ip.String()).
 | 
				
			||||||
 | 
							Msgf("Assining %s to %s", ip, m.Name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	m.AuthKeyID = uint(pak.ID)
 | 
						m.AuthKeyID = uint(pak.ID)
 | 
				
			||||||
	m.IPAddress = ip.String()
 | 
						m.IPAddress = ip.String()
 | 
				
			||||||
@ -459,10 +632,18 @@ func (h *Headscale) handleAuthKey(c *gin.Context, db *gorm.DB, idKey wgkey.Key,
 | 
				
			|||||||
	resp.User = *pak.Namespace.toUser()
 | 
						resp.User = *pak.Namespace.toUser()
 | 
				
			||||||
	respBody, err := encode(resp, &idKey, h.privateKey)
 | 
						respBody, err := encode(resp, &idKey, h.privateKey)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("Cannot encode message: %s", err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("func", "handleAuthKey").
 | 
				
			||||||
 | 
								Str("machine", m.Name).
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Cannot encode message")
 | 
				
			||||||
		c.String(http.StatusInternalServerError, "Extremely sad!")
 | 
							c.String(http.StatusInternalServerError, "Extremely sad!")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	c.Data(200, "application/json; charset=utf-8", respBody)
 | 
						c.Data(200, "application/json; charset=utf-8", respBody)
 | 
				
			||||||
	log.Printf("[%s] Successfully authenticated via AuthKey", m.Name)
 | 
						log.Info().
 | 
				
			||||||
 | 
							Str("func", "handleAuthKey").
 | 
				
			||||||
 | 
							Str("machine", m.Name).
 | 
				
			||||||
 | 
							Str("ip", ip.String()).
 | 
				
			||||||
 | 
							Msg("Successfully authenticated via AuthKey")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										22
									
								
								app.go
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								app.go
									
									
									
									
									
								
							@ -3,13 +3,14 @@ package headscale
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
					 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/rs/zerolog/log"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/gin-gonic/gin"
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
	"golang.org/x/crypto/acme/autocert"
 | 
						"golang.org/x/crypto/acme/autocert"
 | 
				
			||||||
	"gorm.io/gorm"
 | 
						"gorm.io/gorm"
 | 
				
			||||||
@ -120,21 +121,21 @@ func (h *Headscale) ExpireEphemeralNodes(milliSeconds int64) {
 | 
				
			|||||||
func (h *Headscale) expireEphemeralNodesWorker() {
 | 
					func (h *Headscale) expireEphemeralNodesWorker() {
 | 
				
			||||||
	namespaces, err := h.ListNamespaces()
 | 
						namespaces, err := h.ListNamespaces()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("Error listing namespaces: %s", err)
 | 
							log.Error().Err(err).Msg("Error listing namespaces")
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for _, ns := range *namespaces {
 | 
						for _, ns := range *namespaces {
 | 
				
			||||||
		machines, err := h.ListMachinesInNamespace(ns.Name)
 | 
							machines, err := h.ListMachinesInNamespace(ns.Name)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Printf("Error listing machines in namespace %s: %s", ns.Name, err)
 | 
								log.Error().Err(err).Str("namespace", ns.Name).Msg("Error listing machines in namespace")
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		for _, m := range *machines {
 | 
							for _, m := range *machines {
 | 
				
			||||||
			if m.AuthKey != nil && m.LastSeen != nil && m.AuthKey.Ephemeral && time.Now().After(m.LastSeen.Add(h.cfg.EphemeralNodeInactivityTimeout)) {
 | 
								if m.AuthKey != nil && m.LastSeen != nil && m.AuthKey.Ephemeral && time.Now().After(m.LastSeen.Add(h.cfg.EphemeralNodeInactivityTimeout)) {
 | 
				
			||||||
				log.Printf("[%s] Ephemeral client removed from database\n", m.Name)
 | 
									log.Info().Str("machine", m.Name).Msg("Ephemeral client removed from database")
 | 
				
			||||||
				err = h.db.Unscoped().Delete(m).Error
 | 
									err = h.db.Unscoped().Delete(m).Error
 | 
				
			||||||
				if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
					log.Printf("[%s] 🤮 Cannot delete ephemeral machine from the database: %s", m.Name, err)
 | 
										log.Error().Err(err).Str("machine", m.Name).Msg("🤮 Cannot delete ephemeral machine from the database")
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@ -168,7 +169,7 @@ func (h *Headscale) Serve() error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	if h.cfg.TLSLetsEncryptHostname != "" {
 | 
						if h.cfg.TLSLetsEncryptHostname != "" {
 | 
				
			||||||
		if !strings.HasPrefix(h.cfg.ServerURL, "https://") {
 | 
							if !strings.HasPrefix(h.cfg.ServerURL, "https://") {
 | 
				
			||||||
			log.Println("WARNING: listening with TLS but ServerURL does not start with https://")
 | 
								log.Warn().Msg("Listening with TLS but ServerURL does not start with https://")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		m := autocert.Manager{
 | 
							m := autocert.Manager{
 | 
				
			||||||
@ -191,7 +192,10 @@ func (h *Headscale) Serve() error {
 | 
				
			|||||||
			// port 80 for the certificate validation in addition to the headscale
 | 
								// port 80 for the certificate validation in addition to the headscale
 | 
				
			||||||
			// service, which can be configured to run on any other port.
 | 
								// service, which can be configured to run on any other port.
 | 
				
			||||||
			go func() {
 | 
								go func() {
 | 
				
			||||||
				log.Fatal(http.ListenAndServe(h.cfg.TLSLetsEncryptListen, m.HTTPHandler(http.HandlerFunc(h.redirect))))
 | 
					
 | 
				
			||||||
 | 
									log.Fatal().
 | 
				
			||||||
 | 
										Err(http.ListenAndServe(h.cfg.TLSLetsEncryptListen, m.HTTPHandler(http.HandlerFunc(h.redirect)))).
 | 
				
			||||||
 | 
										Msg("failed to set up a HTTP server")
 | 
				
			||||||
			}()
 | 
								}()
 | 
				
			||||||
			err = s.ListenAndServeTLS("", "")
 | 
								err = s.ListenAndServeTLS("", "")
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
@ -199,12 +203,12 @@ func (h *Headscale) Serve() error {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	} else if h.cfg.TLSCertPath == "" {
 | 
						} else if h.cfg.TLSCertPath == "" {
 | 
				
			||||||
		if !strings.HasPrefix(h.cfg.ServerURL, "http://") {
 | 
							if !strings.HasPrefix(h.cfg.ServerURL, "http://") {
 | 
				
			||||||
			log.Println("WARNING: listening without TLS but ServerURL does not start with http://")
 | 
								log.Warn().Msg("Listening without TLS but ServerURL does not start with http://")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		err = r.Run(h.cfg.Addr)
 | 
							err = r.Run(h.cfg.Addr)
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		if !strings.HasPrefix(h.cfg.ServerURL, "https://") {
 | 
							if !strings.HasPrefix(h.cfg.ServerURL, "https://") {
 | 
				
			||||||
			log.Println("WARNING: listening with TLS but ServerURL does not start with https://")
 | 
								log.Warn().Msg("Listening with TLS but ServerURL does not start with https://")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		err = r.RunTLS(h.cfg.Addr, h.cfg.TLSCertPath, h.cfg.TLSKeyPath)
 | 
							err = r.RunTLS(h.cfg.Addr, h.cfg.TLSCertPath, h.cfg.TLSKeyPath)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -2,8 +2,9 @@ package cli
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"github.com/spf13/cobra"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
@ -17,7 +18,7 @@ var rootCmd = &cobra.Command{
 | 
				
			|||||||
headscale is an open source implementation of the Tailscale control server
 | 
					headscale is an open source implementation of the Tailscale control server
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Juan Font Alonso <juanfontalonso@gmail.com> - 2021
 | 
					Juan Font Alonso <juanfontalonso@gmail.com> - 2021
 | 
				
			||||||
https://gitlab.com/juanfont/headscale`,
 | 
					https://github.com/juanfont/headscale`,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Execute() {
 | 
					func Execute() {
 | 
				
			||||||
 | 
				
			|||||||
@ -5,13 +5,13 @@ import (
 | 
				
			|||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"log"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/juanfont/headscale"
 | 
						"github.com/juanfont/headscale"
 | 
				
			||||||
 | 
						"github.com/rs/zerolog/log"
 | 
				
			||||||
	"github.com/spf13/viper"
 | 
						"github.com/spf13/viper"
 | 
				
			||||||
	"gopkg.in/yaml.v2"
 | 
						"gopkg.in/yaml.v2"
 | 
				
			||||||
	"inet.af/netaddr"
 | 
						"inet.af/netaddr"
 | 
				
			||||||
@ -39,6 +39,8 @@ func LoadConfig(path string) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	viper.SetDefault("ip_prefix", "100.64.0.0/10")
 | 
						viper.SetDefault("ip_prefix", "100.64.0.0/10")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						viper.SetDefault("log_level", "debug")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err := viper.ReadInConfig()
 | 
						err := viper.ReadInConfig()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return fmt.Errorf("Fatal error reading config file: %s \n", err)
 | 
							return fmt.Errorf("Fatal error reading config file: %s \n", err)
 | 
				
			||||||
@ -52,7 +54,8 @@ func LoadConfig(path string) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	if (viper.GetString("tls_letsencrypt_hostname") != "") && (viper.GetString("tls_letsencrypt_challenge_type") == "TLS-ALPN-01") && (!strings.HasSuffix(viper.GetString("listen_addr"), ":443")) {
 | 
						if (viper.GetString("tls_letsencrypt_hostname") != "") && (viper.GetString("tls_letsencrypt_challenge_type") == "TLS-ALPN-01") && (!strings.HasSuffix(viper.GetString("listen_addr"), ":443")) {
 | 
				
			||||||
		// this is only a warning because there could be something sitting in front of headscale that redirects the traffic (e.g. an iptables rule)
 | 
							// this is only a warning because there could be something sitting in front of headscale that redirects the traffic (e.g. an iptables rule)
 | 
				
			||||||
		log.Println("Warning: when using tls_letsencrypt_hostname with TLS-ALPN-01 as challenge type, headscale must be reachable on port 443, i.e. listen_addr should probably end in :443")
 | 
							log.Warn().
 | 
				
			||||||
 | 
								Msg("Warning: when using tls_letsencrypt_hostname with TLS-ALPN-01 as challenge type, headscale must be reachable on port 443, i.e. listen_addr should probably end in :443")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (viper.GetString("tls_letsencrypt_challenge_type") != "HTTP-01") && (viper.GetString("tls_letsencrypt_challenge_type") != "TLS-ALPN-01") {
 | 
						if (viper.GetString("tls_letsencrypt_challenge_type") != "HTTP-01") && (viper.GetString("tls_letsencrypt_challenge_type") != "TLS-ALPN-01") {
 | 
				
			||||||
@ -82,9 +85,13 @@ func absPath(path string) string {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getHeadscaleApp() (*headscale.Headscale, error) {
 | 
					func getHeadscaleApp() (*headscale.Headscale, error) {
 | 
				
			||||||
	derpMap, err := loadDerpMap(absPath(viper.GetString("derp_map_path")))
 | 
						derpPath := absPath(viper.GetString("derp_map_path"))
 | 
				
			||||||
 | 
						derpMap, err := loadDerpMap(derpPath)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("Could not load DERP servers map file: %s", err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("path", derpPath).
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Could not load DERP servers map file")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Minimum inactivity time out is keepalive timeout (60s) plus a few seconds
 | 
						// Minimum inactivity time out is keepalive timeout (60s) plus a few seconds
 | 
				
			||||||
@ -129,9 +136,13 @@ func getHeadscaleApp() (*headscale.Headscale, error) {
 | 
				
			|||||||
	// We are doing this here, as in the future could be cool to have it also hot-reload
 | 
						// We are doing this here, as in the future could be cool to have it also hot-reload
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if viper.GetString("acl_policy_path") != "" {
 | 
						if viper.GetString("acl_policy_path") != "" {
 | 
				
			||||||
		err = h.LoadACLPolicy(absPath(viper.GetString("acl_policy_path")))
 | 
							aclPath := absPath(viper.GetString("acl_policy_path"))
 | 
				
			||||||
 | 
							err = h.LoadACLPolicy(aclPath)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Printf("Could not load the ACL policy: %s", err)
 | 
								log.Error().
 | 
				
			||||||
 | 
									Str("path", aclPath).
 | 
				
			||||||
 | 
									Err(err).
 | 
				
			||||||
 | 
									Msg("Could not load the ACL policy")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -161,24 +172,24 @@ func JsonOutput(result interface{}, errResult error, outputFormat string) {
 | 
				
			|||||||
		if errResult != nil {
 | 
							if errResult != nil {
 | 
				
			||||||
			j, err = json.MarshalIndent(ErrorOutput{errResult.Error()}, "", "\t")
 | 
								j, err = json.MarshalIndent(ErrorOutput{errResult.Error()}, "", "\t")
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Fatalln(err)
 | 
									log.Fatal().Err(err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			j, err = json.MarshalIndent(result, "", "\t")
 | 
								j, err = json.MarshalIndent(result, "", "\t")
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Fatalln(err)
 | 
									log.Fatal().Err(err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	case "json-line":
 | 
						case "json-line":
 | 
				
			||||||
		if errResult != nil {
 | 
							if errResult != nil {
 | 
				
			||||||
			j, err = json.Marshal(ErrorOutput{errResult.Error()})
 | 
								j, err = json.Marshal(ErrorOutput{errResult.Error()})
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Fatalln(err)
 | 
									log.Fatal().Err(err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			j, err = json.Marshal(result)
 | 
								j, err = json.Marshal(result)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				log.Fatalln(err)
 | 
									log.Fatal().Err(err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,15 +1,41 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"log"
 | 
						"os"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/juanfont/headscale/cmd/headscale/cli"
 | 
						"github.com/juanfont/headscale/cmd/headscale/cli"
 | 
				
			||||||
 | 
						"github.com/rs/zerolog"
 | 
				
			||||||
 | 
						"github.com/rs/zerolog/log"
 | 
				
			||||||
 | 
						"github.com/spf13/viper"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
 | 
						zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
 | 
				
			||||||
 | 
						log.Logger = log.Output(zerolog.ConsoleWriter{
 | 
				
			||||||
 | 
							Out:        os.Stdout,
 | 
				
			||||||
 | 
							TimeFormat: time.RFC3339,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err := cli.LoadConfig("")
 | 
						err := cli.LoadConfig("")
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Fatalf(err.Error())
 | 
							log.Fatal().Err(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						logLevel := viper.GetString("log_level")
 | 
				
			||||||
 | 
						switch logLevel {
 | 
				
			||||||
 | 
						case "trace":
 | 
				
			||||||
 | 
							zerolog.SetGlobalLevel(zerolog.TraceLevel)
 | 
				
			||||||
 | 
						case "debug":
 | 
				
			||||||
 | 
							zerolog.SetGlobalLevel(zerolog.DebugLevel)
 | 
				
			||||||
 | 
						case "info":
 | 
				
			||||||
 | 
							zerolog.SetGlobalLevel(zerolog.InfoLevel)
 | 
				
			||||||
 | 
						case "warn":
 | 
				
			||||||
 | 
							zerolog.SetGlobalLevel(zerolog.WarnLevel)
 | 
				
			||||||
 | 
						case "error":
 | 
				
			||||||
 | 
							zerolog.SetGlobalLevel(zerolog.ErrorLevel)
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							zerolog.SetGlobalLevel(zerolog.DebugLevel)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	cli.Execute()
 | 
						cli.Execute()
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@ -9,6 +9,7 @@ require (
 | 
				
			|||||||
	github.com/klauspost/compress v1.13.1
 | 
						github.com/klauspost/compress v1.13.1
 | 
				
			||||||
	github.com/lib/pq v1.10.2 // indirect
 | 
						github.com/lib/pq v1.10.2 // indirect
 | 
				
			||||||
	github.com/mattn/go-sqlite3 v1.14.7 // indirect
 | 
						github.com/mattn/go-sqlite3 v1.14.7 // indirect
 | 
				
			||||||
 | 
						github.com/rs/zerolog v1.23.0 // indirect
 | 
				
			||||||
	github.com/spf13/cobra v1.1.3
 | 
						github.com/spf13/cobra v1.1.3
 | 
				
			||||||
	github.com/spf13/viper v1.8.1
 | 
						github.com/spf13/viper v1.8.1
 | 
				
			||||||
	github.com/tailscale/hujson v0.0.0-20200924210142-dde312d0d6a2
 | 
						github.com/tailscale/hujson v0.0.0-20200924210142-dde312d0d6a2
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							@ -683,6 +683,8 @@ github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
 | 
				
			|||||||
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
 | 
					github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
 | 
				
			||||||
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
 | 
					github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
 | 
				
			||||||
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
 | 
					github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
 | 
				
			||||||
 | 
					github.com/rs/zerolog v1.23.0 h1:UskrK+saS9P9Y789yNNulYKdARjPZuS35B8gJF2x60g=
 | 
				
			||||||
 | 
					github.com/rs/zerolog v1.23.0/go.mod h1:6c7hFfxPOy7TacJc4Fcdi24/J0NKYGzjG8FWRI916Qo=
 | 
				
			||||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 | 
					github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 | 
				
			||||||
github.com/ryancurrah/gomodguard v1.1.0/go.mod h1:4O8tr7hBODaGE6VIhfJDHcwzh5GUccKSJBU0UMXJFVM=
 | 
					github.com/ryancurrah/gomodguard v1.1.0/go.mod h1:4O8tr7hBODaGE6VIhfJDHcwzh5GUccKSJBU0UMXJFVM=
 | 
				
			||||||
github.com/ryanrolds/sqlclosecheck v0.3.0/go.mod h1:1gREqxyTGR3lVtpngyFo3hZAgk0KCtEdgEkHwDbigdA=
 | 
					github.com/ryanrolds/sqlclosecheck v0.3.0/go.mod h1:1gREqxyTGR3lVtpngyFo3hZAgk0KCtEdgEkHwDbigdA=
 | 
				
			||||||
 | 
				
			|||||||
@ -3,11 +3,12 @@ package headscale
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
					 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/rs/zerolog/log"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"gorm.io/datatypes"
 | 
						"gorm.io/datatypes"
 | 
				
			||||||
	"inet.af/netaddr"
 | 
						"inet.af/netaddr"
 | 
				
			||||||
	"tailscale.com/tailcfg"
 | 
						"tailscale.com/tailcfg"
 | 
				
			||||||
@ -157,7 +158,7 @@ func (h *Headscale) getPeers(m Machine) (*[]*tailcfg.Node, error) {
 | 
				
			|||||||
	machines := []Machine{}
 | 
						machines := []Machine{}
 | 
				
			||||||
	if err := h.db.Where("namespace_id = ? AND machine_key <> ? AND registered",
 | 
						if err := h.db.Where("namespace_id = ? AND machine_key <> ? AND registered",
 | 
				
			||||||
		m.NamespaceID, m.MachineKey).Find(&machines).Error; err != nil {
 | 
							m.NamespaceID, m.MachineKey).Find(&machines).Error; err != nil {
 | 
				
			||||||
		log.Printf("Error accessing db: %s", err)
 | 
							log.Error().Err(err).Msg("Error accessing db")
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -4,9 +4,9 @@ import (
 | 
				
			|||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
					 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/rs/zerolog/log"
 | 
				
			||||||
	"gorm.io/gorm"
 | 
						"gorm.io/gorm"
 | 
				
			||||||
	"tailscale.com/tailcfg"
 | 
						"tailscale.com/tailcfg"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@ -33,7 +33,10 @@ func (h *Headscale) CreateNamespace(name string) (*Namespace, error) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	n.Name = name
 | 
						n.Name = name
 | 
				
			||||||
	if err := h.db.Create(&n).Error; err != nil {
 | 
						if err := h.db.Create(&n).Error; err != nil {
 | 
				
			||||||
		log.Printf("Could not create row: %s", err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("func", "CreateNamespace").
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Could not create row")
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return &n, nil
 | 
						return &n, nil
 | 
				
			||||||
@ -133,7 +136,10 @@ func (h *Headscale) RequestMapUpdates(namespaceID uint) error {
 | 
				
			|||||||
	names = append(names, namespace.Name)
 | 
						names = append(names, namespace.Name)
 | 
				
			||||||
	data, err := json.Marshal(names)
 | 
						data, err := json.Marshal(names)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Printf("Could not marshal namespaces_pending_updates: %s", err)
 | 
							log.Error().
 | 
				
			||||||
 | 
								Str("func", "RequestMapUpdates").
 | 
				
			||||||
 | 
								Err(err).
 | 
				
			||||||
 | 
								Msg("Could not marshal namespaces_pending_updates")
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return h.setValue("namespaces_pending_updates", string(data))
 | 
						return h.setValue("namespaces_pending_updates", string(data))
 | 
				
			||||||
@ -154,7 +160,10 @@ func (h *Headscale) checkForNamespacesPendingUpdates() {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	for _, name := range names {
 | 
						for _, name := range names {
 | 
				
			||||||
		log.Printf("Sending updates to nodes in namespace %s", name)
 | 
							log.Trace().
 | 
				
			||||||
 | 
								Str("func", "RequestMapUpdates").
 | 
				
			||||||
 | 
								Str("machine", name).
 | 
				
			||||||
 | 
								Msg("Sending updates to nodes in namespace")
 | 
				
			||||||
		machines, err := h.ListMachinesInNamespace(name)
 | 
							machines, err := h.ListMachinesInNamespace(name)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
@ -165,10 +174,19 @@ func (h *Headscale) checkForNamespacesPendingUpdates() {
 | 
				
			|||||||
			for _, p := range *peers {
 | 
								for _, p := range *peers {
 | 
				
			||||||
				pUp, ok := h.clientsPolling[uint64(p.ID)]
 | 
									pUp, ok := h.clientsPolling[uint64(p.ID)]
 | 
				
			||||||
				if ok {
 | 
									if ok {
 | 
				
			||||||
					log.Printf("[%s] Notifying peer %s (%s)", m.Name, p.Name, p.Addresses[0])
 | 
										log.Info().
 | 
				
			||||||
 | 
											Str("func", "checkForNamespacesPendingUpdates").
 | 
				
			||||||
 | 
											Str("machine", m.Name).
 | 
				
			||||||
 | 
											Str("peer", m.Name).
 | 
				
			||||||
 | 
											Str("address", p.Addresses[0].String()).
 | 
				
			||||||
 | 
											Msgf("Notifying peer %s (%s)", p.Name, p.Addresses[0])
 | 
				
			||||||
					pUp <- []byte{}
 | 
										pUp <- []byte{}
 | 
				
			||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					log.Printf("[%s] Peer %s does not appear to be polling", m.Name, p.Name)
 | 
										log.Info().
 | 
				
			||||||
 | 
											Str("func", "checkForNamespacesPendingUpdates").
 | 
				
			||||||
 | 
											Str("machine", m.Name).
 | 
				
			||||||
 | 
											Str("peer", m.Name).
 | 
				
			||||||
 | 
											Msgf("Peer %s does not appear to be polling", p.Name)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			h.pollMu.Unlock()
 | 
								h.pollMu.Unlock()
 | 
				
			||||||
@ -181,7 +199,10 @@ func (h *Headscale) checkForNamespacesPendingUpdates() {
 | 
				
			|||||||
	if v == newV { // only clear when no changes, so we notified everybody
 | 
						if v == newV { // only clear when no changes, so we notified everybody
 | 
				
			||||||
		err = h.setValue("namespaces_pending_updates", "")
 | 
							err = h.setValue("namespaces_pending_updates", "")
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			log.Printf("Could not save to KV: %s", err)
 | 
								log.Error().
 | 
				
			||||||
 | 
									Str("func", "checkForNamespacesPendingUpdates").
 | 
				
			||||||
 | 
									Err(err).
 | 
				
			||||||
 | 
									Msg("Could not save to KV")
 | 
				
			||||||
			return
 | 
								return
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user