mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-29 18:33:05 +00:00
Map route into machine
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
parent
f7f472ae07
commit
bce8427423
2 changed files with 61 additions and 13 deletions
|
@ -47,9 +47,13 @@ func (hsdb *HSDatabase) ListPeers(machine *types.Machine) (types.Machines, error
|
||||||
Msg("Finding direct peers")
|
Msg("Finding direct peers")
|
||||||
|
|
||||||
machines := types.Machines{}
|
machines := types.Machines{}
|
||||||
if err := hsdb.db.Preload("AuthKey").Preload("AuthKey.User").Preload("User").Where("node_key <> ?",
|
if err := hsdb.db.
|
||||||
machine.NodeKey).Find(&machines).Error; err != nil {
|
Preload("AuthKey").
|
||||||
log.Error().Err(err).Msg("Error accessing db")
|
Preload("AuthKey.User").
|
||||||
|
Preload("User").
|
||||||
|
Preload("Routes").
|
||||||
|
Where("node_key <> ?",
|
||||||
|
machine.NodeKey).Find(&machines).Error; err != nil {
|
||||||
|
|
||||||
return types.Machines{}, err
|
return types.Machines{}, err
|
||||||
}
|
}
|
||||||
|
@ -66,7 +70,13 @@ func (hsdb *HSDatabase) ListPeers(machine *types.Machine) (types.Machines, error
|
||||||
|
|
||||||
func (hsdb *HSDatabase) ListMachines() ([]types.Machine, error) {
|
func (hsdb *HSDatabase) ListMachines() ([]types.Machine, error) {
|
||||||
machines := []types.Machine{}
|
machines := []types.Machine{}
|
||||||
if err := hsdb.db.Preload("AuthKey").Preload("AuthKey.User").Preload("User").Find(&machines).Error; err != nil {
|
if err := hsdb.db.
|
||||||
|
Preload("AuthKey").
|
||||||
|
Preload("AuthKey.User").
|
||||||
|
Preload("User").
|
||||||
|
Preload("Routes").
|
||||||
|
Find(&machines).Error; err != nil {
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +85,13 @@ func (hsdb *HSDatabase) ListMachines() ([]types.Machine, error) {
|
||||||
|
|
||||||
func (hsdb *HSDatabase) ListMachinesByGivenName(givenName string) (types.Machines, error) {
|
func (hsdb *HSDatabase) ListMachinesByGivenName(givenName string) (types.Machines, error) {
|
||||||
machines := types.Machines{}
|
machines := types.Machines{}
|
||||||
if err := hsdb.db.Preload("AuthKey").Preload("AuthKey.User").Preload("User").Where("given_name = ?", givenName).Find(&machines).Error; err != nil {
|
if err := hsdb.db.
|
||||||
|
Preload("AuthKey").
|
||||||
|
Preload("AuthKey.User").
|
||||||
|
Preload("User").
|
||||||
|
Preload("Routes").
|
||||||
|
Where("given_name = ?", givenName).Find(&machines).Error; err != nil {
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +136,12 @@ func (hsdb *HSDatabase) GetMachineByGivenName(
|
||||||
// GetMachineByID finds a Machine by ID and returns the Machine struct.
|
// GetMachineByID finds a Machine by ID and returns the Machine struct.
|
||||||
func (hsdb *HSDatabase) GetMachineByID(id uint64) (*types.Machine, error) {
|
func (hsdb *HSDatabase) GetMachineByID(id uint64) (*types.Machine, error) {
|
||||||
m := types.Machine{}
|
m := types.Machine{}
|
||||||
if result := hsdb.db.Preload("AuthKey").Preload("User").Find(&types.Machine{ID: id}).First(&m); result.Error != nil {
|
if result := hsdb.db.
|
||||||
|
Preload("AuthKey").
|
||||||
|
Preload("AuthKey.User").
|
||||||
|
Preload("User").
|
||||||
|
Preload("Routes").
|
||||||
|
Find(&types.Machine{ID: id}).First(&m); result.Error != nil {
|
||||||
return nil, result.Error
|
return nil, result.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +153,12 @@ func (hsdb *HSDatabase) GetMachineByMachineKey(
|
||||||
machineKey key.MachinePublic,
|
machineKey key.MachinePublic,
|
||||||
) (*types.Machine, error) {
|
) (*types.Machine, error) {
|
||||||
m := types.Machine{}
|
m := types.Machine{}
|
||||||
if result := hsdb.db.Preload("AuthKey").Preload("User").First(&m, "machine_key = ?", util.MachinePublicKeyStripPrefix(machineKey)); result.Error != nil {
|
if result := hsdb.db.
|
||||||
|
Preload("AuthKey").
|
||||||
|
Preload("AuthKey.User").
|
||||||
|
Preload("User").
|
||||||
|
Preload("Routes").
|
||||||
|
First(&m, "machine_key = ?", util.MachinePublicKeyStripPrefix(machineKey)); result.Error != nil {
|
||||||
return nil, result.Error
|
return nil, result.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,8 +170,13 @@ func (hsdb *HSDatabase) GetMachineByNodeKey(
|
||||||
nodeKey key.NodePublic,
|
nodeKey key.NodePublic,
|
||||||
) (*types.Machine, error) {
|
) (*types.Machine, error) {
|
||||||
machine := types.Machine{}
|
machine := types.Machine{}
|
||||||
if result := hsdb.db.Preload("AuthKey").Preload("User").First(&machine, "node_key = ?",
|
if result := hsdb.db.
|
||||||
util.NodePublicKeyStripPrefix(nodeKey)); result.Error != nil {
|
Preload("AuthKey").
|
||||||
|
Preload("AuthKey.User").
|
||||||
|
Preload("User").
|
||||||
|
Preload("Routes").
|
||||||
|
First(&machine, "node_key = ?",
|
||||||
|
util.NodePublicKeyStripPrefix(nodeKey)); result.Error != nil {
|
||||||
return nil, result.Error
|
return nil, result.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,10 +188,15 @@ func (hsdb *HSDatabase) GetMachineByAnyKey(
|
||||||
machineKey key.MachinePublic, nodeKey key.NodePublic, oldNodeKey key.NodePublic,
|
machineKey key.MachinePublic, nodeKey key.NodePublic, oldNodeKey key.NodePublic,
|
||||||
) (*types.Machine, error) {
|
) (*types.Machine, error) {
|
||||||
machine := types.Machine{}
|
machine := types.Machine{}
|
||||||
if result := hsdb.db.Preload("AuthKey").Preload("User").First(&machine, "machine_key = ? OR node_key = ? OR node_key = ?",
|
if result := hsdb.db.
|
||||||
util.MachinePublicKeyStripPrefix(machineKey),
|
Preload("AuthKey").
|
||||||
util.NodePublicKeyStripPrefix(nodeKey),
|
Preload("AuthKey.User").
|
||||||
util.NodePublicKeyStripPrefix(oldNodeKey)); result.Error != nil {
|
Preload("User").
|
||||||
|
Preload("Routes").
|
||||||
|
First(&machine, "machine_key = ? OR node_key = ? OR node_key = ?",
|
||||||
|
util.MachinePublicKeyStripPrefix(machineKey),
|
||||||
|
util.NodePublicKeyStripPrefix(nodeKey),
|
||||||
|
util.NodePublicKeyStripPrefix(oldNodeKey)); result.Error != nil {
|
||||||
return nil, result.Error
|
return nil, result.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,8 @@ type Machine struct {
|
||||||
HostInfo HostInfo
|
HostInfo HostInfo
|
||||||
Endpoints StringList
|
Endpoints StringList
|
||||||
|
|
||||||
|
Routes []Route
|
||||||
|
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
DeletedAt *time.Time
|
DeletedAt *time.Time
|
||||||
|
@ -80,6 +82,16 @@ func (ma MachineAddresses) ToStringSlice() []string {
|
||||||
return strSlice
|
return strSlice
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ma MachineAddresses) Prefixes() []netip.Prefix {
|
||||||
|
addrs := []netip.Prefix{}
|
||||||
|
for _, machineAddress := range ma {
|
||||||
|
ip := netip.PrefixFrom(machineAddress, machineAddress.BitLen())
|
||||||
|
addrs = append(addrs, ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
return addrs
|
||||||
|
}
|
||||||
|
|
||||||
// AppendToIPSet adds the individual ips in MachineAddresses to a
|
// AppendToIPSet adds the individual ips in MachineAddresses to a
|
||||||
// given netipx.IPSetBuilder.
|
// given netipx.IPSetBuilder.
|
||||||
func (ma MachineAddresses) AppendToIPSet(build *netipx.IPSetBuilder) {
|
func (ma MachineAddresses) AppendToIPSet(build *netipx.IPSetBuilder) {
|
||||||
|
|
Loading…
Reference in a new issue