From 5a993e8f98008b3bcbd0f04dcb5b69b4dcef7d47 Mon Sep 17 00:00:00 2001 From: Samuel Hawksby-Robinson Date: Wed, 12 Apr 2023 14:36:38 +0100 Subject: [PATCH] Added functionality to slow stop UDP beacon multicast/listen --- VERSION | 2 +- server/pairing/peers.go | 17 +++++++++++++---- server/pairing/peers/payload.go | 17 +++++++++++++++++ server/pairing/peers/udp_notifier.go | 6 +++--- server/servertest/servertest.go | 3 ++- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/VERSION b/VERSION index 4d71f7f9b..ea147deb5 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.145.2 +0.146.0 diff --git a/server/pairing/peers.go b/server/pairing/peers.go index 062f01d0c..8764a944f 100644 --- a/server/pairing/peers.go +++ b/server/pairing/peers.go @@ -2,6 +2,8 @@ package pairing import ( "runtime" + "sync" + "time" "go.uber.org/zap" @@ -12,8 +14,9 @@ import ( ) type PeerNotifier struct { - logger *zap.Logger - stop chan struct{} + logger *zap.Logger + stop chan struct{} + terminator sync.Once } func NewPeerNotifier() *PeerNotifier { @@ -26,11 +29,17 @@ func NewPeerNotifier() *PeerNotifier { } } +func (p *PeerNotifier) terminateIn(d time.Duration) { + p.terminator.Do(func() { + time.Sleep(d) + p.stop <- struct{}{} + }) +} + func (p *PeerNotifier) handler(hello *peers.LocalPairingPeerHello) { signal.SendLocalPairingEvent(Event{Type: EventPeerDiscovered, Action: ActionPeerDiscovery, Data: hello}) p.logger.Debug("received peers.LocalPairingPeerHello message", zap.Any("hello message", hello)) - // TODO p.stop <- struct{}{} Don't do this immediately start a countdown to kill after 5 seconds to allow the - // peer to discover us. + p.terminateIn(5 * time.Second) } func (p *PeerNotifier) Search() error { diff --git a/server/pairing/peers/payload.go b/server/pairing/peers/payload.go index 0e9b7791f..81dece652 100644 --- a/server/pairing/peers/payload.go +++ b/server/pairing/peers/payload.go @@ -4,6 +4,7 @@ import ( "crypto/ecdsa" "crypto/rand" "crypto/sha256" + "encoding/json" udpp2p "github.com/schollz/peerdiscovery" @@ -30,6 +31,22 @@ func NewLocalPairingPeerHello(id []byte, name, deviceType string, k *ecdsa.Priva return h, nil } +func (h *LocalPairingPeerHello) MarshalJSON() ([]byte, error) { + alias := struct { + PeerID []byte + DeviceName string + DeviceType string + Address string + }{ + PeerID: h.PeerId, + DeviceName: h.DeviceName, + DeviceType: h.DeviceType, + Address: h.Discovered.Address, + } + + return json.Marshal(alias) +} + func (h *LocalPairingPeerHello) hash() []byte { dHash := sha256.Sum256(append(h.PeerId, []byte(h.DeviceName+h.DeviceType)...)) return dHash[:] diff --git a/server/pairing/peers/udp_notifier.go b/server/pairing/peers/udp_notifier.go index fb56e4db4..7de2c1f30 100644 --- a/server/pairing/peers/udp_notifier.go +++ b/server/pairing/peers/udp_notifier.go @@ -19,15 +19,15 @@ type UDPNotifier struct { } func NewUDPNotifier(logger *zap.Logger, outputFunc NotifyHandler) (*UDPNotifier, error) { - randId := make([]byte, 32) - _, err := rand.Read(randId) + randID := make([]byte, 32) + _, err := rand.Read(randID) if err != nil { return nil, err } n := new(UDPNotifier) n.logger = logger - n.id = randId + n.id = randID n.notifyOutput = outputFunc return n, nil } diff --git a/server/servertest/servertest.go b/server/servertest/servertest.go index be43d1518..95db6ab83 100644 --- a/server/servertest/servertest.go +++ b/server/servertest/servertest.go @@ -5,7 +5,6 @@ import ( "crypto/ecdsa" "crypto/elliptic" "encoding/asn1" - "github.com/status-im/status-go/logutils" "math/big" "testing" "time" @@ -13,6 +12,8 @@ import ( "github.com/btcsuite/btcutil/base58" "github.com/stretchr/testify/require" "go.uber.org/zap" + + "github.com/status-im/status-go/logutils" ) const (