mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-08-12 06:29:05 +02:00
Compare commits
5 Commits
74-perform
...
bugfix-nod
Author | SHA1 | Date | |
---|---|---|---|
1f0914e2df | |||
27e00196cd | |||
dea6f1a22d | |||
913de57568 | |||
ce829114b1 |
@ -235,9 +235,19 @@ func NewSmegServer(conf ApiServerConf) (ApiServer, error) {
|
||||
words: words,
|
||||
}
|
||||
|
||||
router.GET("/meshes", smegServer.GetMeshes)
|
||||
router.GET("/mesh/:meshid", smegServer.GetMesh)
|
||||
router.POST("/mesh/create", smegServer.CreateMesh)
|
||||
router.POST("/mesh/join", smegServer.JoinMesh)
|
||||
v1 := router.Group("/api/v1")
|
||||
{
|
||||
meshes := v1.Group("/meshes")
|
||||
{
|
||||
meshes.GET("/", smegServer.GetMeshes)
|
||||
}
|
||||
mesh := v1.Group("/mesh")
|
||||
{
|
||||
mesh.GET("/:meshid", smegServer.GetMesh)
|
||||
mesh.POST("/create", smegServer.CreateMesh)
|
||||
mesh.POST("/join", smegServer.JoinMesh)
|
||||
}
|
||||
}
|
||||
|
||||
return smegServer, nil
|
||||
}
|
||||
|
@ -23,9 +23,10 @@ func binarySearch(global []string, selfId string, groupSize int) (int, int) {
|
||||
|
||||
lower := 0
|
||||
higher := len(global) - 1
|
||||
mid := (lower + higher) / 2
|
||||
|
||||
for (higher+1)-lower > groupSize {
|
||||
mid := (lower + higher) / 2
|
||||
|
||||
if global[mid] < selfId {
|
||||
lower = mid + 1
|
||||
} else if global[mid] > selfId {
|
||||
@ -33,8 +34,6 @@ func binarySearch(global []string, selfId string, groupSize int) (int, int) {
|
||||
} else {
|
||||
break
|
||||
}
|
||||
|
||||
mid = (lower + higher) / 2
|
||||
}
|
||||
|
||||
return lower, int(math.Min(float64(lower+groupSize), float64(len(global))))
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
"github.com/tim-beatham/smegmesh/pkg/conf"
|
||||
"github.com/tim-beatham/smegmesh/pkg/ip"
|
||||
"github.com/tim-beatham/smegmesh/pkg/lib"
|
||||
logging "github.com/tim-beatham/smegmesh/pkg/log"
|
||||
"github.com/tim-beatham/smegmesh/pkg/wg"
|
||||
"golang.zx2c4.com/wireguard/wgctrl"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
@ -355,7 +356,7 @@ func (s *MeshManagerImpl) LeaveMesh(meshId string) error {
|
||||
err := mesh.RemoveNode(s.HostParameters.GetPublicKey())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
logging.Log.WriteErrorf(err.Error())
|
||||
}
|
||||
|
||||
if s.OnDelete != nil {
|
||||
|
@ -28,6 +28,7 @@ type SyncerImpl struct {
|
||||
cluster conn.ConnCluster
|
||||
conf *conf.DaemonConfiguration
|
||||
lastSync map[string]int64
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
// Sync: Sync with random nodes
|
||||
@ -38,11 +39,10 @@ func (s *SyncerImpl) Sync(correspondingMesh mesh.MeshProvider) error {
|
||||
|
||||
// Self can be nil if the node is removed
|
||||
selfID := s.manager.GetPublicKey()
|
||||
self, _ := correspondingMesh.GetNode(selfID.String())
|
||||
self, err := correspondingMesh.GetNode(selfID.String())
|
||||
|
||||
// Mesh has been removed
|
||||
if self == nil {
|
||||
return fmt.Errorf("mesh %s does not exist", correspondingMesh.GetMeshId())
|
||||
if err != nil {
|
||||
logging.Log.WriteErrorf(err.Error())
|
||||
}
|
||||
|
||||
correspondingMesh.Prune()
|
||||
@ -51,7 +51,8 @@ func (s *SyncerImpl) Sync(correspondingMesh mesh.MeshProvider) error {
|
||||
logging.Log.WriteInfof("meshes %s has changes", correspondingMesh.GetMeshId())
|
||||
}
|
||||
|
||||
if self.GetType() == conf.PEER_ROLE && !correspondingMesh.HasChanges() && s.infectionCount == 0 {
|
||||
// If removed sync with other nodes to gossip the node is removed
|
||||
if self != nil && self.GetType() == conf.PEER_ROLE && !correspondingMesh.HasChanges() && s.infectionCount == 0 {
|
||||
logging.Log.WriteInfof("no changes for %s", correspondingMesh.GetMeshId())
|
||||
|
||||
// If not synchronised in certain time pull from random neighbour
|
||||
@ -63,16 +64,19 @@ func (s *SyncerImpl) Sync(correspondingMesh mesh.MeshProvider) error {
|
||||
}
|
||||
|
||||
before := time.Now()
|
||||
s.manager.GetRouteManager().UpdateRoutes()
|
||||
err = s.manager.GetRouteManager().UpdateRoutes()
|
||||
|
||||
if err != nil {
|
||||
logging.Log.WriteErrorf(err.Error())
|
||||
}
|
||||
|
||||
publicKey := s.manager.GetPublicKey()
|
||||
nodeNames := correspondingMesh.GetPeers()
|
||||
|
||||
if self != nil {
|
||||
nodeNames = lib.Filter(nodeNames, func(s string) bool {
|
||||
return s != mesh.NodeID(self)
|
||||
// Filter our only public key out so we dont sync with ourself
|
||||
return s != publicKey.String()
|
||||
})
|
||||
}
|
||||
|
||||
var gossipNodes []string
|
||||
|
||||
@ -101,14 +105,14 @@ func (s *SyncerImpl) Sync(correspondingMesh mesh.MeshProvider) error {
|
||||
|
||||
// Do this synchronously to conserve bandwidth
|
||||
for _, node := range gossipNodes {
|
||||
correspondingPeer := s.manager.GetNode(correspondingMesh.GetMeshId(), node)
|
||||
correspondingPeer, err := correspondingMesh.GetNode(node)
|
||||
|
||||
if correspondingPeer == nil {
|
||||
if correspondingPeer == nil || err != nil {
|
||||
logging.Log.WriteErrorf("node %s does not exist", node)
|
||||
continue
|
||||
}
|
||||
|
||||
err := s.requester.SyncMesh(correspondingMesh.GetMeshId(), correspondingPeer)
|
||||
err = s.requester.SyncMesh(correspondingMesh.GetMeshId(), correspondingPeer)
|
||||
|
||||
if err == nil || err == io.EOF {
|
||||
succeeded = true
|
||||
@ -131,7 +135,9 @@ func (s *SyncerImpl) Sync(correspondingMesh mesh.MeshProvider) error {
|
||||
|
||||
correspondingMesh.SaveChanges()
|
||||
|
||||
s.lock.Lock()
|
||||
s.lastSync[correspondingMesh.GetMeshId()] = time.Now().Unix()
|
||||
s.lock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -188,6 +194,7 @@ func (s *SyncerImpl) SyncMeshes() error {
|
||||
|
||||
go sync()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
logging.Log.WriteInfof("updating the WireGuard configuration")
|
||||
err := s.manager.ApplyConfig()
|
||||
|
Reference in New Issue
Block a user