Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 32 additions & 1 deletion shared/management/client/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"sync"
"sync/atomic"
"time"

"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -44,6 +45,8 @@ type GrpcClient struct {
conn *grpc.ClientConn
connStateCallback ConnStateNotifier
connStateCallbackLock sync.RWMutex
// lastNetworkMapSerial stores last seen network map serial to optimize sync
lastNetworkMapSerial uint64
}

// NewClient creates a new client to Management service
Expand Down Expand Up @@ -216,11 +219,34 @@ func (c *GrpcClient) GetNetworkMap(sysInfo *system.Info) (*proto.NetworkMap, err
return nil, fmt.Errorf("invalid msg, required network map")
}

// update last seen serial
atomic.StoreUint64(&c.lastNetworkMapSerial, decryptedResp.GetNetworkMap().GetSerial())

return decryptedResp.GetNetworkMap(), nil
}

func (c *GrpcClient) connectToStream(ctx context.Context, serverPubKey wgtypes.Key, sysInfo *system.Info) (proto.ManagementService_SyncClient, error) {
req := &proto.SyncRequest{Meta: infoToMetaData(sysInfo)}
// Always compute latest system info to ensure up-to-date PeerSystemMeta on first and subsequent syncs
recomputed := system.GetInfo(c.ctx)
if sysInfo != nil {
recomputed.SetFlags(
sysInfo.RosenpassEnabled,
sysInfo.RosenpassPermissive,
&sysInfo.ServerSSHAllowed,
sysInfo.DisableClientRoutes,
sysInfo.DisableServerRoutes,
sysInfo.DisableDNS,
sysInfo.DisableFirewall,
sysInfo.BlockLANAccess,
sysInfo.BlockInbound,
sysInfo.LazyConnectionEnabled,
)
Copy link

Copilot AI Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The manual copying of individual flags is error-prone and difficult to maintain. Consider implementing a method like MergeFlags() or CopyFlags() on the system.Info struct to handle this logic centrally and reduce the risk of missing fields when new flags are added.

Suggested change
recomputed.SetFlags(
sysInfo.RosenpassEnabled,
sysInfo.RosenpassPermissive,
&sysInfo.ServerSSHAllowed,
sysInfo.DisableClientRoutes,
sysInfo.DisableServerRoutes,
sysInfo.DisableDNS,
sysInfo.DisableFirewall,
sysInfo.BlockLANAccess,
sysInfo.BlockInbound,
sysInfo.LazyConnectionEnabled,
)
recomputed.CopyFlagsFrom(sysInfo)

Copilot uses AI. Check for mistakes.
// carry over posture files if any were computed
if len(sysInfo.Files) > 0 {
recomputed.Files = sysInfo.Files
}
}
req := &proto.SyncRequest{Meta: infoToMetaData(recomputed), NetworkMapSerial: atomic.LoadUint64(&c.lastNetworkMapSerial)}

myPrivateKey := c.key
myPublicKey := myPrivateKey.PublicKey()
Expand Down Expand Up @@ -258,6 +284,11 @@ func (c *GrpcClient) receiveEvents(stream proto.ManagementService_SyncClient, se
return err
}

// track latest network map serial if present
if decryptedResp.GetNetworkMap() != nil {
atomic.StoreUint64(&c.lastNetworkMapSerial, decryptedResp.GetNetworkMap().GetSerial())
}

if err := msgHandler(decryptedResp); err != nil {
log.Errorf("failed handling an update message received from Management Service: %v", err.Error())
}
Expand Down
Loading
Loading