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

Commit

Permalink
Diffing of configuration data (#136)
Browse files Browse the repository at this point in the history
Prevent full reload of wireguard (ReplacePeers: true,) as that can
break active sessions. This is done with the UpdateOnly and Removed
flags to manage peers.
  • Loading branch information
gertdreyer authored Jul 19, 2021
1 parent a55c01c commit 67ca506
Showing 1 changed file with 45 additions and 2 deletions.
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

0 comments on commit 67ca506

Please sign in to comment.