Ability to specify aliases

Ability to specify aliases that automatically append to /etc/hosts
This commit is contained in:
Tim Beatham
2023-11-17 22:13:51 +00:00
parent f6160fe138
commit 68db795f47
17 changed files with 421 additions and 99 deletions

View File

@@ -1,15 +1,46 @@
package mesh
import "github.com/tim-beatham/wgmesh/pkg/hosts"
import (
"fmt"
func AddAliases(meshid string, snapshot MeshSnapshot) {
hosts := hosts.NewHostsManipulator(meshid)
"github.com/tim-beatham/wgmesh/pkg/hosts"
)
for _, node := range snapshot.GetNodes() {
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 {
if node.GetAlias() != "" {
hosts.AddAddr(node.GetWgHost().IP, node.GetAlias())
a.hosts.AddAddr(hosts.HostsEntry{
Alias: fmt.Sprintf("%s.smeg", node.GetAlias()),
Ip: node.GetWgHost().IP,
})
}
}
hosts.Write()
}
// 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(),
}
}