From 0159649d0a6b2f16dbd8e7490bba41a8258756b8 Mon Sep 17 00:00:00 2001 From: Juan Font Alonso Date: Sun, 11 Jul 2021 16:39:19 +0200 Subject: [PATCH] Send the namespace name as user to the clients --- api.go | 15 +++++++++++---- app.go | 2 +- machine.go | 1 - namespaces.go | 4 ++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/api.go b/api.go index cfb35257..088c337f 100644 --- a/api.go +++ b/api.go @@ -76,7 +76,7 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) { } var m Machine - if result := h.db.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!") m = Machine{ Expiry: &req.Expiry, @@ -200,7 +200,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) { } var m Machine - if result := h.db.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()) c.String(http.StatusUnauthorized, "") return @@ -357,16 +357,23 @@ func (h *Headscale) getMapResponse(mKey wgkey.Key, req tailcfg.MapRequest, m Mac log.Printf("Cannot fetch peers: %s", err) return nil, err } + + profile := tailcfg.UserProfile{ + ID: tailcfg.UserID(m.NamespaceID), + LoginName: m.Namespace.Name, + DisplayName: m.Namespace.Name, + } + resp := tailcfg.MapResponse{ KeepAlive: false, Node: node, Peers: *peers, DNS: []netaddr.IP{}, SearchPaths: []string{}, - Domain: "foobar@example.com", + Domain: "headscale.net", PacketFilter: *h.aclRules, DERPMap: h.cfg.DerpMap, - UserProfiles: []tailcfg.UserProfile{}, + UserProfiles: []tailcfg.UserProfile{profile}, } var respBody []byte diff --git a/app.go b/app.go index 7cc1e925..de6e8e6c 100644 --- a/app.go +++ b/app.go @@ -175,7 +175,7 @@ func (h *Headscale) Serve() error { }() err = s.ListenAndServeTLS("", "") } else { - return errors.New("Unknown value for TLSLetsEncryptChallengeType") + return errors.New("unknown value for TLSLetsEncryptChallengeType") } } else if h.cfg.TLSCertPath == "" { if !strings.HasPrefix(h.cfg.ServerURL, "http://") { diff --git a/machine.go b/machine.go index 1892219d..cfc1b8f8 100644 --- a/machine.go +++ b/machine.go @@ -154,7 +154,6 @@ func (m Machine) toNode() (*tailcfg.Node, error) { } func (h *Headscale) getPeers(m Machine) (*[]*tailcfg.Node, error) { - machines := []Machine{} if err := h.db.Where("namespace_id = ? AND machine_key <> ? AND registered", m.NamespaceID, m.MachineKey).Find(&machines).Error; err != nil { diff --git a/namespaces.go b/namespaces.go index ddfbee99..9897640c 100644 --- a/namespaces.go +++ b/namespaces.go @@ -106,10 +106,10 @@ func (h *Headscale) SetMachineNamespace(m *Machine, namespaceName string) error func (n *Namespace) toUser() *tailcfg.User { u := tailcfg.User{ ID: tailcfg.UserID(n.ID), - LoginName: "", + LoginName: n.Name, DisplayName: n.Name, ProfilePicURL: "", - Domain: "", + Domain: "headscale.net", Logins: []tailcfg.LoginID{}, Created: time.Time{}, }