Added the name from the HostInfo hostname

This commit is contained in:
Juan Font Alonso 2021-02-23 20:10:58 +01:00
parent 90759688ca
commit 2b1d92429a

View file

@ -14,12 +14,14 @@ import (
"tailscale.com/wgengine/wgcfg" "tailscale.com/wgengine/wgcfg"
) )
// Machine is a Headscale client
type Machine struct { type Machine struct {
ID uint64 `gorm:"primary_key"` ID uint64 `gorm:"primary_key"`
MachineKey string `gorm:"type:varchar(64);unique_index"` MachineKey string `gorm:"type:varchar(64);unique_index"`
NodeKey string NodeKey string
DiscoKey string DiscoKey string
IPAddress string IPAddress string
Name string
Registered bool // temp Registered bool // temp
LastSeen *time.Time LastSeen *time.Time
@ -47,10 +49,18 @@ func (m Machine) toNode() (*tailcfg.Node, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
var discoKey tailcfg.DiscoKey
if m.DiscoKey != "" {
dKey, err := wgcfg.ParseHexKey(m.DiscoKey) dKey, err := wgcfg.ParseHexKey(m.DiscoKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
discoKey = tailcfg.DiscoKey(dKey)
} else {
discoKey = tailcfg.DiscoKey{}
}
addrs := []netaddr.IPPrefix{} addrs := []netaddr.IPPrefix{}
allowedIPs := []netaddr.IPPrefix{} allowedIPs := []netaddr.IPPrefix{}
@ -58,10 +68,11 @@ func (m Machine) toNode() (*tailcfg.Node, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
addrs = append(addrs, ip) addrs = append(addrs, ip) // missing the ipv6 ?
allowedIPs = append(allowedIPs, ip) // looks like the client expect this allowedIPs = append(allowedIPs, ip) // looks like the client expect this
endpoints := []string{} endpoints := []string{}
if len(m.Endpoints.RawMessage) != 0 {
be, err := m.Endpoints.MarshalJSON() be, err := m.Endpoints.MarshalJSON()
if err != nil { if err != nil {
return nil, err return nil, err
@ -70,8 +81,10 @@ func (m Machine) toNode() (*tailcfg.Node, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
hostinfo := tailcfg.Hostinfo{} hostinfo := tailcfg.Hostinfo{}
if len(m.HostInfo.RawMessage) != 0 {
hi, err := m.HostInfo.MarshalJSON() hi, err := m.HostInfo.MarshalJSON()
if err != nil { if err != nil {
return nil, err return nil, err
@ -80,16 +93,17 @@ func (m Machine) toNode() (*tailcfg.Node, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
n := tailcfg.Node{ n := tailcfg.Node{
ID: tailcfg.NodeID(m.ID), // this is the actual ID ID: tailcfg.NodeID(m.ID), // this is the actual ID
StableID: tailcfg.StableNodeID(strconv.FormatUint(m.ID, 10)), // in headscale, unlike tailcontrol server, IDs are permantent StableID: tailcfg.StableNodeID(strconv.FormatUint(m.ID, 10)), // in headscale, unlike tailcontrol server, IDs are permantent
Name: "", Name: hostinfo.Hostname,
User: 1, User: 1,
Key: tailcfg.NodeKey(nKey), Key: tailcfg.NodeKey(nKey),
KeyExpiry: *m.Expiry, KeyExpiry: *m.Expiry,
Machine: tailcfg.MachineKey(mKey), Machine: tailcfg.MachineKey(mKey),
DiscoKey: tailcfg.DiscoKey(dKey), DiscoKey: discoKey,
Addresses: addrs, Addresses: addrs,
AllowedIPs: allowedIPs, AllowedIPs: allowedIPs,
Endpoints: endpoints, Endpoints: endpoints,
@ -99,7 +113,7 @@ func (m Machine) toNode() (*tailcfg.Node, error) {
Created: m.CreatedAt, Created: m.CreatedAt,
LastSeen: m.LastSeen, LastSeen: m.LastSeen,
KeepAlive: false, KeepAlive: true,
MachineAuthorized: m.Registered, MachineAuthorized: m.Registered,
} }
// n.Key.MarshalText() // n.Key.MarshalText()
@ -114,15 +128,13 @@ func (h *Headscale) getPeers(m Machine) (*[]*tailcfg.Node, error) {
} }
defer db.Close() defer db.Close()
// Add user management here? // Add user management here
machines := []Machine{} machines := []Machine{}
if err = db.Where("machine_key <> ? AND registered", m.MachineKey).Find(&machines).Error; err != nil { if err = db.Where("machine_key <> ? AND registered", m.MachineKey).Find(&machines).Error; err != nil {
log.Printf("Error accessing db: %s", err) log.Printf("Error accessing db: %s", err)
return nil, err return nil, err
} }
log.Printf("Found %d peers of %s", len(machines), m.MachineKey)
peers := []*tailcfg.Node{} peers := []*tailcfg.Node{}
for _, mn := range machines { for _, mn := range machines {
peer, err := mn.toNode() peer, err := mn.toNode()