separate addPeer into VPN- and store-add (#18)

This commit is contained in:
Benedict Schlüter 2022-03-29 16:49:11 +02:00 committed by GitHub
parent cf738bb973
commit 719b6d5f6f

View File

@ -39,6 +39,14 @@ func (c *Core) GetPeers(resourceVersion int) (int, []peer.Peer, error) {
// AddPeer adds a peer to the store and the VPN.
func (c *Core) AddPeer(peer peer.Peer) error {
if err := c.AddPeerToVPN(peer); err != nil {
return err
}
return c.AddPeerToStore(peer)
}
// AddPeerToVPN adds a peer to the the VPN.
func (c *Core) AddPeerToVPN(peer peer.Peer) error {
publicIP, _, err := net.SplitHostPort(peer.PublicEndpoint)
if err != nil {
c.zaplogger.Info("SplitHostPort", zap.Error(err))
@ -52,12 +60,16 @@ func (c *Core) AddPeer(peer peer.Peer) error {
}
if myIP != peer.VPNIP {
if err := c.vpn.AddPeer(peer.VPNPubKey, publicIP, peer.VPNIP); err != nil {
c.zaplogger.Error("Failed to add peer to VPN", zap.Error(err), zap.String("public_ip", publicIP), zap.String("vpn_ip", peer.VPNIP))
c.zaplogger.Error("failed to add peer to VPN", zap.Error(err), zap.String("peer public_ip", publicIP), zap.String("peer vpn_ip", peer.VPNIP))
return err
}
c.zaplogger.Info("Added peer to VPN", zap.String("public_ip", publicIP), zap.String("vpn_ip", peer.VPNIP))
c.zaplogger.Info("added peer to VPN", zap.String("peer public_ip", publicIP), zap.String("peer vpn_ip", peer.VPNIP))
}
return nil
}
// AddPeerToStore adds a peer to the store.
func (c *Core) AddPeerToStore(peer peer.Peer) error {
tx, err := c.store.BeginTransaction()
if err != nil {
return err
@ -71,8 +83,11 @@ func (c *Core) AddPeer(peer peer.Peer) error {
if err := txdata.PutPeer(peer); err != nil {
return err
}
return tx.Commit()
if err := tx.Commit(); err != nil {
return err
}
c.zaplogger.Info("added peer to store", zap.String("peer public_endpoint", peer.PublicEndpoint), zap.String("peer vpn_ip", peer.VPNIP))
return nil
}
// UpdatePeers synchronizes the peers known to the store and the vpn with the passed peers.