From 6a3446a3d5c6e80db3035eb7e37fd0afe6aad784 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Fri, 8 Jan 2021 17:02:24 -0600 Subject: [PATCH] Fix a bunch of stupid edge cases --- src/main/kotlin/mdnet/base/ServerManager.kt | 4 ++-- .../kotlin/mdnet/base/server/ImageServer.kt | 20 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/mdnet/base/ServerManager.kt b/src/main/kotlin/mdnet/base/ServerManager.kt index 4c7e373..585624e 100644 --- a/src/main/kotlin/mdnet/base/ServerManager.kt +++ b/src/main/kotlin/mdnet/base/ServerManager.kt @@ -97,7 +97,7 @@ class ServerManager(serverSettings: ServerSettings, devSettings: DevSettings, ma lastBytesSent = statistics.get().bytesSent val state = this.state - if (state is GracefulStop) { + if (state is GracefulStop && state.nextState != Shutdown) { LOGGER.info { "Aborting graceful shutdown started due to hourly bandwidth limit" } this.state = state.lastRunning @@ -164,7 +164,7 @@ class ServerManager(serverSettings: ServerSettings, devSettings: DevSettings, ma } } } catch (e: Exception) { - LOGGER.warn(e) { "Graceful shutdown checker failed" } + LOGGER.warn(e) { "Bandwidth shutdown checker/ping failed" } } }, 45, 45, TimeUnit.SECONDS) diff --git a/src/main/kotlin/mdnet/base/server/ImageServer.kt b/src/main/kotlin/mdnet/base/server/ImageServer.kt index b7ab581..cd89c53 100644 --- a/src/main/kotlin/mdnet/base/server/ImageServer.kt +++ b/src/main/kotlin/mdnet/base/server/ImageServer.kt @@ -69,6 +69,7 @@ import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.transactions.transaction import org.slf4j.LoggerFactory +import java.lang.IllegalArgumentException private val LOGGER = LoggerFactory.getLogger(ImageServer::class.java) @@ -106,7 +107,12 @@ class ImageServer( } if (tokenized || remoteSettings.forceTokens) { - val tokenArr = Base64.getUrlDecoder().decode(Path.of("token")(request)) + val tokenArr = try { + Base64.getUrlDecoder().decode(Path.of("token")(request)) + } catch (e: IllegalArgumentException) { + LOGGER.info(e) { "Request for $sanitizedUri rejected for non-base64 token" } + return@then Response(Status.FORBIDDEN).body("Token is invalid base64") + } if (tokenArr.size < 24) { LOGGER.info { "Request for $sanitizedUri rejected for invalid token" } return@then Response(Status.FORBIDDEN) @@ -122,17 +128,17 @@ class ImageServer( ) } catch (e: JsonProcessingException) { LOGGER.info(e) { "Request for $sanitizedUri rejected for invalid token" } - return@then Response(Status.FORBIDDEN) + return@then Response(Status.FORBIDDEN).body("Token is invalid") } if (OffsetDateTime.now().isAfter(token.expires)) { LOGGER.info { "Request for $sanitizedUri rejected for expired token" } - return@then Response(Status.GONE) + return@then Response(Status.GONE).body("Token has expired") } if (token.hash != chapterHash) { LOGGER.info { "Request for $sanitizedUri rejected for inapplicable token" } - return@then Response(Status.FORBIDDEN) + return@then Response(Status.FORBIDDEN).body("Token is inapplicable for the image") } } @@ -353,11 +359,11 @@ fun getServer(cache: DiskLruCache, database: Database, remoteSettings: RemoteSet val imageServer = ImageServer(cache, database, statistics, remoteSettings, client) - return timeRequest() + return addCommonHeaders() + .then(timeRequest()) + .then(setHandled(isHandled)) .then(catchAllHideDetails()) .then(ServerFilters.CatchLensFailure) - .then(setHandled(isHandled)) - .then(addCommonHeaders()) .then( routes( "/data/{chapterHash}/{fileName}" bind Method.GET to imageServer.handler(dataSaver = false),