Persist Network Serial to Store to avoid outdated netmap sent (#260)

Fix outdated update coming from management
even when it is actually not outdated.
This commit is contained in:
Mikhail Bragin 2022-03-10 18:18:38 +01:00 committed by GitHub
parent 8cc93e0dbe
commit be0c5c887c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 25 additions and 25 deletions

View File

@ -85,7 +85,7 @@ type Engine struct {
udpMuxConn *net.UDPConn udpMuxConn *net.UDPConn
udpMuxConnSrflx *net.UDPConn udpMuxConnSrflx *net.UDPConn
// networkSerial is the latest Serial (state ID) of the network sent by the Management service // networkSerial is the latest CurrentSerial (state ID) of the network sent by the Management service
networkSerial uint64 networkSerial uint64
} }

View File

@ -96,7 +96,7 @@ func TestEngine_UpdateNetworkMap(t *testing.T) {
expectedSerial: 1, expectedSerial: 1,
} }
// 2nd case - one extra peer added and network map has Serial grater than local => apply the update // 2nd case - one extra peer added and network map has CurrentSerial grater than local => apply the update
case2 := testCase{ case2 := testCase{
name: "input with an old peer and a new peer to add", name: "input with an old peer and a new peer to add",
networkMap: &mgmtProto.NetworkMap{ networkMap: &mgmtProto.NetworkMap{

View File

@ -836,7 +836,7 @@ type NetworkMap struct {
// Serial is an ID of the network state to be used by clients to order updates. // Serial is an ID of the network state to be used by clients to order updates.
// The larger the Serial the newer the configuration. // The larger the Serial the newer the configuration.
// E.g. the client app should keep track of this id locally and discard all the configurations with a lower value // E.g. the client app should keep track of this id locally and discard all the configurations with a lower value
Serial uint64 `protobuf:"varint,1,opt,name=Serial,proto3" json:"Serial,omitempty"` Serial uint64 `protobuf:"varint,1,opt,name=CurrentSerial,proto3" json:"CurrentSerial,omitempty"`
// PeerConfig represents configuration of a peer // PeerConfig represents configuration of a peer
PeerConfig *PeerConfig `protobuf:"bytes,2,opt,name=peerConfig,proto3" json:"peerConfig,omitempty"` PeerConfig *PeerConfig `protobuf:"bytes,2,opt,name=peerConfig,proto3" json:"peerConfig,omitempty"`
// RemotePeerConfig represents a list of remote peers that the receiver can connect to // RemotePeerConfig represents a list of remote peers that the receiver can connect to

View File

@ -397,7 +397,7 @@ func TestAccountManager_AddPeer(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
serial := account.Network.Serial() //should be 0 serial := account.Network.CurrentSerial() //should be 0
var setupKey *SetupKey var setupKey *SetupKey
for _, key := range account.SetupKeys { for _, key := range account.SetupKeys {
@ -409,8 +409,8 @@ func TestAccountManager_AddPeer(t *testing.T) {
return return
} }
if account.Network.serial != 0 { if account.Network.Serial != 0 {
t.Errorf("expecting account network to have an initial serial=0") t.Errorf("expecting account network to have an initial Serial=0")
return return
} }
@ -446,8 +446,8 @@ func TestAccountManager_AddPeer(t *testing.T) {
t.Errorf("expecting just added peer to have IP = %s, got %s", expectedPeerIP, peer.IP.String()) t.Errorf("expecting just added peer to have IP = %s, got %s", expectedPeerIP, peer.IP.String())
} }
if account.Network.Serial() != 1 { if account.Network.CurrentSerial() != 1 {
t.Errorf("expecting Network serial=%d to be incremented by 1 and be equal to %d when adding new peer to account", serial, account.Network.Serial()) t.Errorf("expecting Network Serial=%d to be incremented by 1 and be equal to %d when adding new peer to account", serial, account.Network.CurrentSerial())
} }
} }
@ -498,8 +498,8 @@ func TestAccountManager_DeletePeer(t *testing.T) {
return return
} }
if account.Network.Serial() != 2 { if account.Network.CurrentSerial() != 2 {
t.Errorf("expecting Network serial=%d to be incremented and be equal to 2 after adding and deleteing a peer", account.Network.Serial()) t.Errorf("expecting Network Serial=%d to be incremented and be equal to 2 after adding and deleteing a peer", account.Network.CurrentSerial())
} }
} }

View File

@ -173,7 +173,7 @@ func (s *Server) registerPeer(peerKey wgtypes.Key, req *proto.LoginRequest) (*Pe
peersToSend = append(peersToSend, p) peersToSend = append(peersToSend, p)
} }
} }
update := toSyncResponse(s.config, peer, peersToSend, nil, networkMap.Network.Serial()) update := toSyncResponse(s.config, peer, peersToSend, nil, networkMap.Network.CurrentSerial())
err = s.peersUpdateManager.SendUpdate(remotePeer.Key, &UpdateMessage{Update: update}) err = s.peersUpdateManager.SendUpdate(remotePeer.Key, &UpdateMessage{Update: update})
if err != nil { if err != nil {
// todo rethink if we should keep this return // todo rethink if we should keep this return
@ -362,7 +362,7 @@ func (s *Server) sendInitialSync(peerKey wgtypes.Key, peer *Peer, srv proto.Mana
} else { } else {
turnCredentials = nil turnCredentials = nil
} }
plainResp := toSyncResponse(s.config, peer, networkMap.Peers, turnCredentials, networkMap.Network.Serial()) plainResp := toSyncResponse(s.config, peer, networkMap.Peers, turnCredentials, networkMap.Network.CurrentSerial())
encryptedResp, err := encryption.EncryptMessage(peerKey, s.wgKey, plainResp) encryptedResp, err := encryption.EncryptMessage(peerKey, s.wgKey, plainResp)
if err != nil { if err != nil {

View File

@ -258,7 +258,7 @@ func Test_SyncProtocol(t *testing.T) {
} }
if networkMap.GetSerial() <= 0 { if networkMap.GetSerial() <= 0 {
t.Fatalf("expecting SyncResponse to have NetworkMap with a positive Network Serial, actual %d", networkMap.GetSerial()) t.Fatalf("expecting SyncResponse to have NetworkMap with a positive Network CurrentSerial, actual %d", networkMap.GetSerial())
} }
} }

View File

@ -22,34 +22,34 @@ type Network struct {
Id string Id string
Net net.IPNet Net net.IPNet
Dns string Dns string
// serial is an ID that increments by 1 when any change to the network happened (e.g. new peer has been added). // Serial is an ID that increments by 1 when any change to the network happened (e.g. new peer has been added).
// Used to synchronize state to the client apps. // Used to synchronize state to the client apps.
serial uint64 Serial uint64
mu sync.Mutex `json:"-"` mu sync.Mutex `json:"-"`
} }
// NewNetwork creates a new Network initializing it with a serial=0 // NewNetwork creates a new Network initializing it with a Serial=0
func NewNetwork() *Network { func NewNetwork() *Network {
return &Network{ return &Network{
Id: xid.New().String(), Id: xid.New().String(),
Net: net.IPNet{IP: net.ParseIP("100.64.0.0"), Mask: net.IPMask{255, 192, 0, 0}}, Net: net.IPNet{IP: net.ParseIP("100.64.0.0"), Mask: net.IPMask{255, 192, 0, 0}},
Dns: "", Dns: "",
serial: 0} Serial: 0}
} }
// IncSerial increments serial by 1 reflecting that the network state has been changed // IncSerial increments Serial by 1 reflecting that the network state has been changed
func (n *Network) IncSerial() { func (n *Network) IncSerial() {
n.mu.Lock() n.mu.Lock()
defer n.mu.Unlock() defer n.mu.Unlock()
n.serial = n.serial + 1 n.Serial = n.Serial + 1
} }
// Serial returns the Network.serial of the network (latest state id) // CurrentSerial returns the Network.Serial of the network (latest state id)
func (n *Network) Serial() uint64 { func (n *Network) CurrentSerial() uint64 {
n.mu.Lock() n.mu.Lock()
defer n.mu.Unlock() defer n.mu.Unlock()
return n.serial return n.Serial
} }
func (n *Network) Copy() *Network { func (n *Network) Copy() *Network {
@ -57,7 +57,7 @@ func (n *Network) Copy() *Network {
Id: n.Id, Id: n.Id,
Net: n.Net, Net: n.Net,
Dns: n.Dns, Dns: n.Dns,
serial: n.serial, Serial: n.Serial,
} }
} }

View File

@ -142,7 +142,7 @@ func (am *DefaultAccountManager) DeletePeer(accountId string, peerKey string) (*
RemotePeersIsEmpty: true, RemotePeersIsEmpty: true,
// new field // new field
NetworkMap: &proto.NetworkMap{ NetworkMap: &proto.NetworkMap{
Serial: account.Network.Serial(), Serial: account.Network.CurrentSerial(),
RemotePeers: []*proto.RemotePeerConfig{}, RemotePeers: []*proto.RemotePeerConfig{},
RemotePeersIsEmpty: true, RemotePeersIsEmpty: true,
}, },
@ -173,7 +173,7 @@ func (am *DefaultAccountManager) DeletePeer(accountId string, peerKey string) (*
RemotePeersIsEmpty: len(update) == 0, RemotePeersIsEmpty: len(update) == 0,
// new field // new field
NetworkMap: &proto.NetworkMap{ NetworkMap: &proto.NetworkMap{
Serial: account.Network.Serial(), Serial: account.Network.CurrentSerial(),
RemotePeers: update, RemotePeers: update,
RemotePeersIsEmpty: len(update) == 0, RemotePeersIsEmpty: len(update) == 0,
}, },