Skip to content

Commit

Permalink
Merge pull request #48 from juanfont/better-profile-info
Browse files Browse the repository at this point in the history
Improving namespace/user support
  • Loading branch information
juanfont authored Jul 11, 2021
2 parents cf9d920 + 0159649 commit 31556e1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
15 changes: 11 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: "[email protected]",
Domain: "headscale.net",
PacketFilter: *h.aclRules,
DERPMap: h.cfg.DerpMap,
UserProfiles: []tailcfg.UserProfile{},
UserProfiles: []tailcfg.UserProfile{profile},
}

var respBody []byte
Expand Down
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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://") {
Expand Down
1 change: 0 additions & 1 deletion machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
}
Expand Down

0 comments on commit 31556e1

Please sign in to comment.