2023-11-17 20:05:21 +01:00
|
|
|
package mesh
|
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
2023-11-17 20:05:21 +01:00
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/hosts"
|
|
|
|
)
|
2023-11-17 20:05:21 +01:00
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
type MeshAliasManager interface {
|
|
|
|
AddAliases(nodes []MeshNode)
|
|
|
|
RemoveAliases(node []MeshNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
type AliasManager struct {
|
|
|
|
hosts hosts.HostsManipulator
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddAliases: on node update or change add aliases to the hosts file
|
|
|
|
func (a *AliasManager) AddAliases(nodes []MeshNode) {
|
|
|
|
for _, node := range nodes {
|
2023-11-17 20:05:21 +01:00
|
|
|
if node.GetAlias() != "" {
|
2023-11-17 23:13:51 +01:00
|
|
|
a.hosts.AddAddr(hosts.HostsEntry{
|
|
|
|
Alias: fmt.Sprintf("%s.smeg", node.GetAlias()),
|
|
|
|
Ip: node.GetWgHost().IP,
|
|
|
|
})
|
2023-11-17 20:05:21 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-17 23:13:51 +01:00
|
|
|
}
|
2023-11-17 20:05:21 +01:00
|
|
|
|
2023-11-17 23:13:51 +01:00
|
|
|
// RemoveAliases: on node remove remove aliases from the hosts file
|
|
|
|
func (a *AliasManager) RemoveAliases(nodes []MeshNode) {
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.GetAlias() != "" {
|
|
|
|
a.hosts.Remove(hosts.HostsEntry{
|
|
|
|
Alias: fmt.Sprintf("%s.smeg", node.GetAlias()),
|
|
|
|
Ip: node.GetWgHost().IP,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAliasManager() MeshAliasManager {
|
|
|
|
return &AliasManager{
|
|
|
|
hosts: hosts.NewHostsManipulator(),
|
|
|
|
}
|
2023-11-17 20:05:21 +01:00
|
|
|
}
|