mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-08-26 04:25:19 +02:00
Ability to specify aliases
Ability to specify aliases that automatically append to /etc/hosts
This commit is contained in:
@@ -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(),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user