mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-30 02:43:05 +00:00
Added the name from the HostInfo hostname
This commit is contained in:
parent
90759688ca
commit
2b1d92429a
1 changed files with 36 additions and 24 deletions
60
machine.go
60
machine.go
|
@ -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
|
||||||
}
|
}
|
||||||
dKey, err := wgcfg.ParseHexKey(m.DiscoKey)
|
|
||||||
if err != nil {
|
var discoKey tailcfg.DiscoKey
|
||||||
return nil, err
|
if m.DiscoKey != "" {
|
||||||
|
dKey, err := wgcfg.ParseHexKey(m.DiscoKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
discoKey = tailcfg.DiscoKey(dKey)
|
||||||
|
} else {
|
||||||
|
discoKey = tailcfg.DiscoKey{}
|
||||||
}
|
}
|
||||||
|
|
||||||
addrs := []netaddr.IPPrefix{}
|
addrs := []netaddr.IPPrefix{}
|
||||||
allowedIPs := []netaddr.IPPrefix{}
|
allowedIPs := []netaddr.IPPrefix{}
|
||||||
|
|
||||||
|
@ -58,38 +68,42 @@ 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{}
|
||||||
be, err := m.Endpoints.MarshalJSON()
|
if len(m.Endpoints.RawMessage) != 0 {
|
||||||
if err != nil {
|
be, err := m.Endpoints.MarshalJSON()
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
err = json.Unmarshal(be, &endpoints)
|
}
|
||||||
if err != nil {
|
err = json.Unmarshal(be, &endpoints)
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hostinfo := tailcfg.Hostinfo{}
|
hostinfo := tailcfg.Hostinfo{}
|
||||||
hi, err := m.HostInfo.MarshalJSON()
|
if len(m.HostInfo.RawMessage) != 0 {
|
||||||
if err != nil {
|
hi, err := m.HostInfo.MarshalJSON()
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
err = json.Unmarshal(hi, &hostinfo)
|
}
|
||||||
if err != nil {
|
err = json.Unmarshal(hi, &hostinfo)
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
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()
|
||||||
|
|
Loading…
Reference in a new issue