make sure the pongs uuid matches the last sent ping uuid

pull/53/head
Zlatin Balevsky 2020-09-20 18:42:26 +01:00
parent e5891de136
commit 5a38154e15
No known key found for this signature in database
GPG Key ID: A72832072D525E41
1 changed files with 20 additions and 1 deletions

View File

@ -49,6 +49,8 @@ abstract class Connection implements Closeable {
protected final String name protected final String name
long lastPingSentTime, lastPongReceivedTime long lastPingSentTime, lastPongReceivedTime
private volatile UUID lastPingUUID
Connection(EventBus eventBus, Endpoint endpoint, boolean incoming, Connection(EventBus eventBus, Endpoint endpoint, boolean incoming,
HostCache hostCache, TrustService trustService, MuWireSettings settings) { HostCache hostCache, TrustService trustService, MuWireSettings settings) {
@ -132,7 +134,8 @@ abstract class Connection implements Closeable {
def ping = [:] def ping = [:]
ping.type = "Ping" ping.type = "Ping"
ping.version = 1 ping.version = 1
ping.uuid = UUID.randomUUID().toString() lastPingUUID = UUID.randomUUID()
ping.uuid = lastPingUUID.toString()
messages.put(ping) messages.put(ping)
lastPingSentTime = System.currentTimeMillis() lastPingSentTime = System.currentTimeMillis()
} }
@ -177,6 +180,22 @@ abstract class Connection implements Closeable {
lastPongReceivedTime = System.currentTimeMillis() lastPongReceivedTime = System.currentTimeMillis()
if (pong.pongs == null) if (pong.pongs == null)
throw new Exception("Pong doesn't have pongs") throw new Exception("Pong doesn't have pongs")
if (lastPingUUID == null) {
log.fine "$name received an unexpected pong"
return
}
if (pong.uuid == null) {
log.fine "$name pong doesn't have uuid"
return
}
UUID pongUUID = UUID.fromString(pong.uuid)
if (pongUUID != lastPingUUID) {
log.fine "$name ping/pong uuid mismatch"
return
}
lastPingUUID = null
pong.pongs.stream().limit(2).forEach { pong.pongs.stream().limit(2).forEach {
def dest = new Destination(it) def dest = new Destination(it)
eventBus.publish(new HostDiscoveredEvent(destination: dest)) eventBus.publish(new HostDiscoveredEvent(destination: dest))