Fixing an issue where packets are dropped each time

we change wg configuration
This commit is contained in:
Tim Beatham 2023-11-01 10:39:46 +00:00
parent a1caf2e8ae
commit e63edea763
6 changed files with 30 additions and 14 deletions

2
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/akamensky/argparse v1.4.0
github.com/automerge/automerge-go v0.0.0-20230903201930-b80ce8aadbb9
github.com/google/uuid v1.3.0
github.com/jmespath/go-jmespath v0.4.0
github.com/sirupsen/logrus v1.9.3
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6
google.golang.org/grpc v1.58.1
@ -16,7 +17,6 @@ require (
require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/mdlayher/genetlink v1.3.2 // indirect
github.com/mdlayher/netlink v1.7.2 // indirect

View File

@ -1,9 +1,5 @@
package lib
import (
logging "github.com/tim-beatham/wgmesh/pkg/log"
)
// MapToSlice converts a map to a slice in go
func MapValues[K comparable, V any](m map[K]V) []V {
return MapValuesWithExclude(m, map[K]struct{}{})
@ -23,8 +19,6 @@ func MapValuesWithExclude[K comparable, V any](m map[K]V, exclude map[K]struct{}
continue
}
logging.Log.WriteInfof("Key %s", k)
values[i] = v
i++
}

View File

@ -16,7 +16,7 @@ type WgMeshConfigApplyer struct {
meshManager *MeshManager
}
func ConvertMeshNode(node MeshNode) (*wgtypes.PeerConfig, error) {
func convertMeshNode(node MeshNode) (*wgtypes.PeerConfig, error) {
endpoint, err := net.ResolveUDPAddr("udp", node.GetWgEndpoint())
if err != nil {
@ -59,7 +59,7 @@ func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider) error {
var count int = 0
for _, n := range nodes {
peer, err := ConvertMeshNode(n)
peer, err := convertMeshNode(n)
if err != nil {
return err
@ -71,7 +71,6 @@ func (m *WgMeshConfigApplyer) updateWgConf(mesh MeshProvider) error {
cfg := wgtypes.Config{
Peers: peerConfigs,
ReplacePeers: true,
}
dev, err := mesh.GetDevice()

View File

@ -218,6 +218,10 @@ func (s *MeshManager) GetSelf(meshId string) (MeshNode, error) {
return node, nil
}
func (s *MeshManager) ApplyConfig() error {
return s.configApplyer.ApplyConfig()
}
// UpdateTimeStamp updates the timestamp of this node in all meshes
func (s *MeshManager) UpdateTimeStamp() error {
for _, mesh := range s.Meshes {

View File

@ -21,13 +21,18 @@ type SyncerImpl struct {
manager *mesh.MeshManager
requester SyncRequester
authenticatedNodes []crdt.MeshNodeCrdt
infectionCount int
}
const subSetLength = 3
const infectionCount = 3
// Sync: Sync random nodes
func (s *SyncerImpl) Sync(meshId string) error {
if !s.manager.HasChanges(meshId) {
logging.Log.WriteInfof("UPDATING WG CONF")
s.manager.ApplyConfig()
if !s.manager.HasChanges(meshId) && s.infectionCount == 0 {
logging.Log.WriteInfof("No changes for %s", meshId)
return nil
}
@ -76,6 +81,9 @@ func (s *SyncerImpl) Sync(meshId string) error {
waitGroup.Wait()
logging.Log.WriteInfof("SYNC TIME: %v", time.Now().Sub(before))
s.infectionCount = ((infectionCount + s.infectionCount - 1) % infectionCount)
return nil
}
@ -93,5 +101,5 @@ func (s *SyncerImpl) SyncMeshes() error {
}
func NewSyncer(m *mesh.MeshManager, r SyncRequester) Syncer {
return &SyncerImpl{manager: m, requester: r}
return &SyncerImpl{manager: m, requester: r, infectionCount: 0}
}

View File

@ -1,6 +1,7 @@
package wg
import (
"errors"
"fmt"
"net"
"os/exec"
@ -75,19 +76,29 @@ func flushInterface(ifName string) error {
// EnableInterface flushes the interface and sets the ip address of the
// interface
func (m *WgInterfaceManipulatorImpl) EnableInterface(ifName string, ip string) error {
if len(ifName) == 0 {
return errors.New("ifName not provided")
}
err := flushInterface(ifName)
if err != nil {
return err
}
cmd := exec.Command("/usr/bin/ip", "link", "set", "up", "dev", ifName)
if err := cmd.Run(); err != nil {
return err
}
hostIp, _, err := net.ParseCIDR(ip)
if err != nil {
return err
}
cmd := exec.Command("/usr/bin/ip", "addr", "add", hostIp.String()+"/64", "dev", ifName)
cmd = exec.Command("/usr/bin/ip", "addr", "add", hostIp.String()+"/64", "dev", ifName)
if err := cmd.Run(); err != nil {
return err