config: make host sync domain configurable

Signed-off-by: Steffen Vogel <post@steffenvogel.de>
This commit is contained in:
Steffen Vogel
2022-09-14 10:44:06 +02:00
parent 2ea6fd1847
commit 9a052efa3d
4 changed files with 19 additions and 5 deletions

View File

@@ -104,6 +104,9 @@ rtsync:
hsync:
enabled: true
# The domain name which is appended to each of the peer host names
domain: wg-local
## Peer discovery
#

View File

@@ -72,6 +72,8 @@ type AutoConfigSettings struct {
type HostSyncSettings struct {
Enabled bool `koanf:"enabled,omitempty"`
Domain string `koanf:"domain,omitempty"`
}
type PeerDiscoverySettings struct {

View File

@@ -41,7 +41,7 @@ func NewFeatures(w *watcher.Watcher, cfg *config.Config, c *wgctrl.Client, b sig
}
if cfg.DefaultInterfaceSettings.HostSync.Enabled {
feats = append(feats, hsync.New(w))
feats = append(feats, hsync.New(w, cfg.DefaultInterfaceSettings.HostSync.Domain))
}
if cfg.DefaultInterfaceSettings.EndpointDisc.Enabled {

View File

@@ -20,12 +20,19 @@ const (
)
type HostsSync struct {
domain string
watcher *watcher.Watcher
logger *zap.Logger
}
func New(w *watcher.Watcher) *HostsSync {
func New(w *watcher.Watcher, d string) *HostsSync {
if d != "" && !strings.HasPrefix(d, ".") {
d = "." + d
}
hs := &HostsSync{
domain: d,
watcher: w,
logger: zap.L().Named("hsync"),
}
@@ -53,8 +60,10 @@ func (hs *HostsSync) Hosts() []Host {
pkName := p.PublicKey().String()[:8]
h := Host{
IP: p.PublicKey().IPv6Address().IP,
Names: []string{pkName},
IP: p.PublicKey().IPv6Address().IP,
Names: []string{
pkName + hs.domain,
},
Comment: fmt.Sprintf("%s: ifname=%s, ifindex=%d, pk=%s", hostsCommentPrefix,
p.Interface.KernelDevice.Name(),
p.Interface.KernelDevice.Index(),
@@ -62,7 +71,7 @@ func (hs *HostsSync) Hosts() []Host {
}
if p.Name != "" {
h.Names = append(h.Names, p.Name)
h.Names = append(h.Names, p.Name+hs.domain)
}
hosts = append(hosts, h)