From 58445842194476a8f71a81881bed2c37c9033ea5 Mon Sep 17 00:00:00 2001 From: jdl Date: Mon, 15 Sep 2025 04:24:00 +0200 Subject: [PATCH] FSM logic cleanup --- peer/cipher-data.go | 5 ----- peer/remotefsm.go | 18 ++++++++++++------ peer/statusserver.go | 3 ++- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/peer/cipher-data.go b/peer/cipher-data.go index 5c407bd..5ce8555 100644 --- a/peer/cipher-data.go +++ b/peer/cipher-data.go @@ -1,7 +1,6 @@ package peer import ( - "bytes" "crypto/aes" "crypto/cipher" "crypto/rand" @@ -39,10 +38,6 @@ func (sc *dataCipher) Key() [32]byte { return sc.key } -func (sc *dataCipher) HasKey(k [32]byte) bool { - return bytes.Equal(k[:], sc.key[:]) -} - func (sc *dataCipher) Encrypt(h Header, data, out []byte) []byte { const s = dataHeaderSize out = out[:s+dataCipherOverhead+len(data)] diff --git a/peer/remotefsm.go b/peer/remotefsm.go index b2158bb..231b13a 100644 --- a/peer/remotefsm.go +++ b/peer/remotefsm.go @@ -154,6 +154,8 @@ func (r *remoteFSM) stateServer_onInit(msg controlMsg[packetInit]) { Version: version, } + // Reset traceID to force state update on SYN. + r.traceID = 0 r.sendControl(conf, init.Marshal(r.buf)) } @@ -161,6 +163,12 @@ func (r *remoteFSM) stateServer_onSyn(msg controlMsg[packetSyn]) { r.lastSeen = time.Now() p := msg.Packet + if p.TraceID == r.traceID { + return + } + + r.traceID = p.TraceID + // Before we can respond to this packet, we need to make sure the // route is setup properly. conf := r.conf() @@ -170,10 +178,7 @@ func (r *remoteFSM) stateServer_onSyn(msg controlMsg[packetSyn]) { conf.Direct = p.Direct conf.DirectAddr = msg.SrcAddr - // Update data cipher if the key has changed. - if conf.DataCipher == nil || !conf.DataCipher.HasKey(p.SharedKey) { - conf.DataCipher = newDataCipherFromKey(p.SharedKey) - } + conf.DataCipher = newDataCipherFromKey(p.SharedKey) r.updateConf(conf) @@ -191,13 +196,14 @@ func (r *remoteFSM) stateServer_onSyn(msg controlMsg[packetSyn]) { return } - // Send probes if not a direct connection. + // Send probes if not a direct connection. The server sends probes without + // trace IDs unless responding to a client probe. for _, addr := range msg.Packet.PossibleAddrs { if !addr.IsValid() { break } r.logf("Probing %v...", addr) - r.sendControlToAddr(packetProbe{TraceID: r.NewTraceID()}.Marshal(r.buf), addr) + r.sendControlToAddr(packetProbe{}.Marshal(r.buf), addr) } } diff --git a/peer/statusserver.go b/peer/statusserver.go index adbf23e..6d9b002 100644 --- a/peer/statusserver.go +++ b/peer/statusserver.go @@ -10,7 +10,8 @@ import ( ) type StatusReport struct { - Remotes []RemoteStatus + RelayPeerIP byte + Remotes []RemoteStatus } type RemoteStatus struct {