diff --git a/src/main/kotlin/net/trueog/staffog/Events.kt b/src/main/kotlin/net/trueog/staffog/Events.kt index d806887..61995da 100644 --- a/src/main/kotlin/net/trueog/staffog/Events.kt +++ b/src/main/kotlin/net/trueog/staffog/Events.kt @@ -10,8 +10,10 @@ class Events : Listener { @EventHandler fun onPlayerPreLogin(event: AsyncPlayerPreLoginEvent) { IpCheckEndpoint.checkIpMap[event.playerProfile.id]?.let { - it.complete(event.address.hostAddress) - event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, Component.text("IP sent to Staff-Auth")) + // Kick only if this login answered a pending check + if (it.complete(event.address.hostAddress)) { + event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, Component.text("IP sent to Staff-Auth")) + } } } } \ No newline at end of file diff --git a/src/main/kotlin/net/trueog/staffog/grpc/endpoint/IpCheckEndpoint.kt b/src/main/kotlin/net/trueog/staffog/grpc/endpoint/IpCheckEndpoint.kt index f7ae951..85d17fb 100644 --- a/src/main/kotlin/net/trueog/staffog/grpc/endpoint/IpCheckEndpoint.kt +++ b/src/main/kotlin/net/trueog/staffog/grpc/endpoint/IpCheckEndpoint.kt @@ -4,9 +4,9 @@ import io.grpc.Status import io.grpc.StatusException import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CompletableDeferred -import kotlinx.coroutines.launch -import kotlinx.coroutines.time.delay -import net.trueog.staffog.StaffOG +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.ensureActive +import kotlinx.coroutines.time.withTimeout import proto.IpCheckReply import proto.IpCheckRequest import proto.IpCheckerGrpcKt @@ -23,20 +23,22 @@ class IpCheckEndpoint : IpCheckerGrpcKt.IpCheckerCoroutineImplBase() { override suspend fun checkIp(request: IpCheckRequest): IpCheckReply { val check = CompletableDeferred() val uuid = UUID.fromString(request.uuid) - checkIpMap[uuid] = check - StaffOG.scope.launch { - delay(Duration.ofMinutes(1)) - check.cancel() - checkIpMap.remove(uuid) - } + // A newer check replaces an older one, which fails at once + checkIpMap.put(uuid, check)?.cancel() val ip = try { - check.await() + withTimeout(Duration.ofMinutes(1)) { check.await() } } catch (_: CancellationException) { + // Propagate if the caller itself went away + currentCoroutineContext().ensureActive() + // Timed out or replaced by a newer check throw StatusException(Status.DEADLINE_EXCEEDED) + } finally { + // Remove only this check's own entry + checkIpMap.remove(uuid, check) + // A late login must not answer a finished check + check.cancel() } - checkIpMap.remove(uuid) - return ipCheckReply { this.ip = ip }