From 957a306d6e7846d95a3cdddfbde5f3f7369df8af Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Sun, 7 Mar 2021 13:39:46 -0300 Subject: [PATCH] shorter msg, serviceworker --- .../autoprotocol/ProtocolDetectionState.kt | 4 +- .../autoprotocol/ProtocolDetectorHandler.kt | 5 +- .../viaaas/handler/state/HandshakeState.kt | 2 +- .../handler/state/MinecraftConnectionState.kt | 4 +- .../viaaas/handler/state/Util.kt | 4 +- .../creeper123123321/viaaas/web/WebLogin.kt | 14 +++--- src/main/resources/web/auth.js | 50 +++++++++++-------- src/main/resources/web/sw.js | 6 +++ 8 files changed, 52 insertions(+), 37 deletions(-) create mode 100644 src/main/resources/web/sw.js diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/autoprotocol/ProtocolDetectionState.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/autoprotocol/ProtocolDetectionState.kt index ebdf743..0f1e568 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/autoprotocol/ProtocolDetectionState.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/autoprotocol/ProtocolDetectionState.kt @@ -23,7 +23,7 @@ class ProtocolDetectionState(val future: CompletableFuture) : M JsonParser.parseString(packet.json).asJsonObject.getAsJsonObject("version").get("protocol").asInt ) future.complete(ver) - mcLogger.info("Auto-detected $ver for ${handler.remoteAddress}") + mcLogger.info("A.D.: ${handler.remoteAddress} $ver") } override fun disconnect(handler: MinecraftHandler, msg: String) { @@ -34,4 +34,4 @@ class ProtocolDetectionState(val future: CompletableFuture) : M override fun onInactivated(handler: MinecraftHandler) { future.completeExceptionally(ClosedChannelException()) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/autoprotocol/ProtocolDetectorHandler.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/autoprotocol/ProtocolDetectorHandler.kt index 4f4483d..347293a 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/autoprotocol/ProtocolDetectorHandler.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/autoprotocol/ProtocolDetectorHandler.kt @@ -22,8 +22,7 @@ class ProtocolDetectorHandler(val connectionData: ConnectionData) : ChannelDuple if (ctx.channel().remoteAddress() is InetSocketAddress) { val timeoutRun = ctx.executor().schedule({ mcLogger.warn( - "Timeout for protocol auto-detection in " - + ctx.channel().remoteAddress() + " server" + "Timeout protocol A.D. " + ctx.channel().remoteAddress() ) hold = false drainQueue(ctx) @@ -87,4 +86,4 @@ class ProtocolDetectorHandler(val connectionData: ConnectionData) : ChannelDuple drainQueue(ctx) super.handlerRemoved(ctx) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/HandshakeState.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/HandshakeState.kt index 85fcdea..4b3332f 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/HandshakeState.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/HandshakeState.kt @@ -63,7 +63,7 @@ class HandshakeState : MinecraftConnectionState { val playerAddr = handler.data.frontHandler.remoteAddress mcLogger.info( - "Handshake: ${handler.data.state.state} $playerAddr (${handler.data.frontVer}," + + "HS: $playerAddr (P: ${handler.data.frontVer}, S: ${handler.data.state.state.toString().substring(0, 1)}" + " O: ${ frontOnline.toString().substring(0, 1) }) -> ${packet.address}:${packet.port} (${backProto ?: "AUTO"})" diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/MinecraftConnectionState.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/MinecraftConnectionState.kt index 35b4c6d..96a25e3 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/MinecraftConnectionState.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/MinecraftConnectionState.kt @@ -14,10 +14,10 @@ interface MinecraftConnectionState { ) fun disconnect(handler: MinecraftHandler, msg: String) { - mcLogger.info("Disconnected ${handler.remoteAddress}: $msg") + mcLogger.info("DC ${handler.remoteAddress}: $msg") } fun onInactivated(handler: MinecraftHandler) { - mcLogger.info(handler.remoteAddress.toString() + " inactivated") + mcLogger.info("- ${handler.remoteAddress}") } } diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/Util.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/Util.kt index 19b5a08..a7d6c66 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/Util.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/handler/state/Util.kt @@ -30,7 +30,7 @@ private fun createBackChannel(handler: MinecraftHandler, socketAddr: InetSocketA .connect(socketAddr) .addListener(ChannelFutureListener { if (it.isSuccess) { - mcLogger.info("Connected ${handler.remoteAddress} -> $socketAddr") + mcLogger.info("+ ${handler.remoteAddress} -> $socketAddr") handler.data.backChannel = it.channel() as SocketChannel val packet = Handshake() @@ -105,4 +105,4 @@ fun connectBack(handler: MinecraftHandler, address: String, port: Int, state: St } } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/web/WebLogin.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/web/WebLogin.kt index d2cea69..cede50f 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/web/WebLogin.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/web/WebLogin.kt @@ -24,7 +24,7 @@ class WebLogin : WebState { when (obj.getAsJsonPrimitive("action").asString) { "offline_login" -> { // todo add some spam check - val username = obj.get("username").asString + val username = obj.get("username").asString.trim() val uuid = generateOfflinePlayerUuid(username) val token = webClient.server.generateToken(uuid) @@ -33,7 +33,7 @@ class WebLogin : WebState { | "username": "$username", "uuid": "$uuid", "token": "$token"}""".trimMargin() ) - webLogger.info("${webClient.ws.call.request.local.remoteHost} (O: ${webClient.ws.call.request.origin.remoteHost}) generated a token for offline account $username") + webLogger.info("Token gen: ${webClient.ws.call.request.local.remoteHost} (O: ${webClient.ws.call.request.origin.remoteHost}): offline $username") } "minecraft_id_login" -> { val username = obj.get("username").asString @@ -54,10 +54,10 @@ class WebLogin : WebState { | "username": "$mcIdUser", "uuid": "$uuid", "token": "$token"}""".trimMargin() ) - webLogger.info("${webClient.ws.call.request.local.remoteHost} (O: ${webClient.ws.call.request.origin.remoteHost}) generated a token for account $mcIdUser $uuid") + webLogger.info("Token gen: ${webClient.ws.call.request.local.remoteHost} (O: ${webClient.ws.call.request.origin.remoteHost}): $mcIdUser $uuid") } else { webClient.ws.send("""{"action": "login_result", "success": false}""") - webLogger.info("${webClient.ws.call.request.local.remoteHost} (O: ${webClient.ws.call.request.origin.remoteHost}) failed to generated a token for account $username") + webLogger.info("Token gen fail: ${webClient.ws.call.request.local.remoteHost} (O: ${webClient.ws.call.request.origin.remoteHost}): $username") } } "listen_login_requests" -> { @@ -65,11 +65,11 @@ class WebLogin : WebState { val user = webClient.server.loginTokens.getIfPresent(token) if (user != null && webClient.listenId(user)) { webClient.ws.send("""{"action": "listen_login_requests_result", "token": "$token", "success": true, "user": "$user"}""") - webLogger.info("${webClient.ws.call.request.local.remoteHost} (O: ${webClient.ws.call.request.origin.remoteHost}) listening for logins for $user") + webLogger.info("Listen: ${webClient.ws.call.request.local.remoteHost} (O: ${webClient.ws.call.request.origin.remoteHost}): $user") } else { webClient.server.loginTokens.invalidate(token) webClient.ws.send("""{"action": "listen_login_requests_result", "token": "$token", "success": false}""") - webLogger.info("${webClient.ws.call.request.local.remoteHost} (O: ${webClient.ws.call.request.origin.remoteHost}) failed token") + webLogger.info("Token fail: ${webClient.ws.call.request.local.remoteHost} (O: ${webClient.ws.call.request.origin.remoteHost})") } } "unlisten_login_requests" -> { @@ -96,4 +96,4 @@ class WebLogin : WebState { override suspend fun onException(webClient: WebClient, exception: java.lang.Exception) { } -} \ No newline at end of file +} diff --git a/src/main/resources/web/auth.js b/src/main/resources/web/auth.js index 4a0b5ec..4f9fb23 100644 --- a/src/main/resources/web/auth.js +++ b/src/main/resources/web/auth.js @@ -1,3 +1,5 @@ +navigator.serviceWorker.register("sw.js"); + // Minecraft.id let urlParams = new URLSearchParams(); window.location.hash.substr(1).split("?").map(it => new URLSearchParams(it).forEach((a, b) => urlParams.append(b, a))); @@ -286,29 +288,40 @@ function listen(token) { function confirmJoin(hash) { socket.send(JSON.stringify({action: "session_hash_response", session_hash: hash})); } +var notificationCallbacks = {}; +function handleSWMsg(event) { + console.log("sw msg: " + event); + let data = event.data; + let callback = notificationCallbacks[data.tag]; + if (callback == null) return; + callback(data.action); +} +navigator.serviceWorker.addEventListener("message", handleSWMsg); function authNotification(msg, yes, no) { if ((!pageBlur && !document.hidden) || Notification.permission != "granted") { if (confirm(msg)) yes(); else no(); return; } - let notification = new Notification("Click to allow auth impersionation", { - body: msg, - //actions: [ - // {action: "reject", title: "Reject"}, - // {action: "confirm", title: "Confirm"} - //] + let tag = uuid.v4(); + navigator.serviceWorker.ready.then(r => { + r.showNotification("Click to allow auth impersionation", { + body: msg, + tag: tag, + actions: [ + {action: "reject", title: "Reject"}, + {action: "confirm", title: "Confirm"} + ] + }); + notificationCallbacks[tag] = action => { + if (action == "reject") { + no(); + } else if (!action || action == "confirm") { + yes(); + } else { + return; + } + }; }); - notification.onclick = e => { - e.preventDefault(); - if (e.action == "reject") { - no(); - } else if (!e.action || e.action == "confirm") { - yes(); - } else { - return; - } - notification.close(); - }; } function handleJoinRequest(parsed) { authNotification("Allow auth impersonation from VIAaaS instance?\nUsername: " + parsed.user + "\nSession Hash: " + parsed.session_hash + "\nServer Message: '" + parsed.message + "'", () => { @@ -398,9 +411,6 @@ $(() => { setInterval(refreshCorsStatus, 10 * 60 * 1000); refreshCorsStatus(); - if (Notification.permission == "default") { - Notification.requestPermission(); - } $("#notificate").on("click", e => Notification.requestPermission()); connect(); diff --git a/src/main/resources/web/sw.js b/src/main/resources/web/sw.js new file mode 100644 index 0000000..be3d602 --- /dev/null +++ b/src/main/resources/web/sw.js @@ -0,0 +1,6 @@ +self.addEventListener("notificationclick", event => { + console.log("On notification click: ", event); + event.preventDefault(); + event.notification.close(); + self.clients.matchAll({type: "window"}).then(it => it.forEach(c => c.postMessage({tag: event.notification.tag, action: event.action}))); +});