Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Diffing of configuration data #136

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ func (s *Server) configureWireGuard() error {
return err
}

log.Debugf("Getting current Wireguard config")
currentdev, err := wg.Device(*wgLinkName)
if err != nil {
return err
}
currentpeers := currentdev.Peers
diffpeers := make([]wgtypes.PeerConfig, 0);

peers := make([]wgtypes.PeerConfig, 0)
for user, cfg := range s.Config.Users {
for id, dev := range cfg.Clients {
Expand All @@ -297,11 +305,46 @@ func (s *Server) configureWireGuard() error {
}
}

// Determine peers updated and to be removed from WireGuard
for _, i := range currentpeers{
found := false
for _, j := range peers{
if (i.PublicKey == j.PublicKey){
found = true
j.UpdateOnly = true
diffpeers = append(diffpeers, j)
break
}
}
if (!found){
peertoremove := wgtypes.PeerConfig{
PublicKey : i.PublicKey,
Remove : true,
}
diffpeers = append(diffpeers, peertoremove)
}
}

// Determine peers to be added to WireGuard
for _, i := range peers{
found := false
for _, j := range currentpeers{
if (i.PublicKey == j.PublicKey){
found = true
break
}
}
if (!found){
diffpeers = append(diffpeers, i)
}
}


cfg := wgtypes.Config{
PrivateKey: &key,
ListenPort: wgListenPort,
ReplacePeers: true,
Peers: peers,
ReplacePeers: false,
Peers: diffpeers,
}
err = wg.ConfigureDevice(*wgLinkName, cfg)
if err != nil {
Expand Down