From 93e8572c75d6763d0add5c42fada1d30e75fadae Mon Sep 17 00:00:00 2001 From: carbotaniuman Date: Sat, 9 Jan 2021 05:25:11 +0000 Subject: [PATCH] Fix stuff --- CHANGELOG.md | 7 ++++- src/main/kotlin/mdnet/base/ServerManager.kt | 26 ++++++++++++------- .../kotlin/mdnet/base/server/ImageServer.kt | 15 +++++++++-- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 388db28..d3ce4be 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [1.2.3] - 2021-01-08 ### Fixed - [2021-01-08] Fix a bunch of stupid edge cases [@carbotaniuman]. +- [2021-01-08] Fix pathological shutdown issues [@carbotaniuman]. +- [2021-01-08] Provide messages on request failures [@carbotaniuman]. +- [2021-01-08] Exempt well-known test images [@carbotaniuman]. +- [2021-01-08] Give actual errors and not 500 [@carbotaniuman]. ## [1.2.2] - 2020-08-21 ### Changed @@ -246,7 +250,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - [2020-06-11] Tweaked logging configuration to reduce log file sizes by [@carbotaniuman]. -[Unreleased]: https://gitlab.com/mangadex/mangadex_at_home/-/compare/1.2.2...HEAD +[Unreleased]: https://gitlab.com/mangadex/mangadex_at_home/-/compare/1.2.3...HEAD +[1.2.3]: https://gitlab.com/mangadex/mangadex_at_home/-/compare/1.2.2...1.2.3 [1.2.2]: https://gitlab.com/mangadex/mangadex_at_home/-/compare/1.2.1...1.2.2 [1.2.1]: https://gitlab.com/mangadex/mangadex_at_home/-/compare/1.2.0...1.2.1 [1.2.0]: https://gitlab.com/mangadex/mangadex_at_home/-/compare/1.1.5...1.2.0 diff --git a/src/main/kotlin/mdnet/base/ServerManager.kt b/src/main/kotlin/mdnet/base/ServerManager.kt index 6a9c3d9..b4bc1e1 100644 --- a/src/main/kotlin/mdnet/base/ServerManager.kt +++ b/src/main/kotlin/mdnet/base/ServerManager.kt @@ -172,16 +172,22 @@ class ServerManager(serverSettings: ServerSettings, devSettings: DevSettings, ma } private fun pingControl() { + // this is currentSettings, other is newSettings + // if tls is null that means same as previous ping + fun RemoteSettings.logicalEqual(other: RemoteSettings): Boolean { + val test = if (other.tls != null) { + other + } else { + other.copy(tls = this.tls) + } + + return this == test + } + val state = this.state as Running - val newSettings = serverHandler.pingControl(state.settings)?.run { - // copy the old tls over so we can do a simply equality check - if (tls == null) { - copy(tls = state.settings.tls) - } else { - this - } - } + val newSettings = serverHandler.pingControl(state.settings) + if (newSettings != null) { LOGGER.info { "Server settings received: $newSettings" } @@ -190,9 +196,9 @@ class ServerManager(serverSettings: ServerSettings, devSettings: DevSettings, ma "Outdated build detected! Latest: ${newSettings.latestBuild}, Current: ${Constants.CLIENT_BUILD}" } } - if (state.settings != newSettings) { + if (!state.settings.logicalEqual(newSettings)) { // certificates or upstream url must have changed, restart webserver - LOGGER.info { "Doing internal restart of HTTP server to refresh certs/upstream URL" } + LOGGER.info { "Doing internal restart of HTTP server to refresh settings" } this.state = GracefulStop(lastRunning = state) { loginAndStartServer() diff --git a/src/main/kotlin/mdnet/base/server/ImageServer.kt b/src/main/kotlin/mdnet/base/server/ImageServer.kt index d9af41f..d3b5782 100644 --- a/src/main/kotlin/mdnet/base/server/ImageServer.kt +++ b/src/main/kotlin/mdnet/base/server/ImageServer.kt @@ -60,6 +60,7 @@ import org.http4k.client.Apache4Client import org.http4k.core.* import org.http4k.filter.CachingFilters import org.http4k.filter.ServerFilters +import org.http4k.lens.LensFailure import org.http4k.lens.Path import org.http4k.routing.bind import org.http4k.routing.routes @@ -106,9 +107,15 @@ class ImageServer( return@then Response(Status.FORBIDDEN) } - if (tokenized || remoteSettings.forceTokens) { + if ((tokenized || remoteSettings.forceTokens) && !isTestImage(chapterHash)) { val tokenArr = try { - Base64.getUrlDecoder().decode(Path.of("token")(request)) + val toDecode = try { + Path.of("token")(request) + } catch (e: LensFailure) { + LOGGER.info(e) { "Request for $sanitizedUri rejected for missing token" } + return@then Response(Status.FORBIDDEN).body("Token is missing") + } + Base64.getUrlDecoder().decode(toDecode) } catch (e: IllegalArgumentException) { LOGGER.info(e) { "Request for $sanitizedUri rejected for non-base64 token" } return@then Response(Status.FORBIDDEN).body("Token is invalid base64") @@ -338,6 +345,10 @@ class ImageServer( .header("timing-allow-origin", "https://mangadex.org") } }) + + private fun isTestImage(chapter: String): Boolean { + return chapter == "1b682e7b24ae7dbdc5064eeeb8e8e353" || chapter == "8172a46adc798f4f4ace6663322a383e" + } } }