From e481527fb34aab3984876f16ef3ec872f21f1a40 Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Sat, 4 Jul 2020 14:00:59 -0500 Subject: [PATCH] Generate stuff automatically --- .gitignore | 2 +- build.gradle | 131 +++++++----------- src/main/kotlin/mdnet/base/Constants.kt | 1 - src/main/kotlin/mdnet/base/Main.kt | 3 +- .../kotlin/mdnet/base/server/ImageServer.kt | 29 ++-- src/main/kotlin/mdnet/base/server/common.kt | 3 +- src/template/java/mdnet/BuildInfo.java | 5 + 7 files changed, 77 insertions(+), 97 deletions(-) create mode 100644 src/template/java/mdnet/BuildInfo.java diff --git a/.gitignore b/.gitignore index 643c7c1..d5a578f 100644 --- a/.gitignore +++ b/.gitignore @@ -106,4 +106,4 @@ nbproject/** log/** cache/** -dev +settings.json \ No newline at end of file diff --git a/build.gradle b/build.gradle index ec87641..7e67164 100644 --- a/build.gradle +++ b/build.gradle @@ -1,10 +1,10 @@ plugins { - id "java" - id "org.jetbrains.kotlin.jvm" version "1.3.72" - id "application" - id "com.github.johnrengelman.shadow" version "5.2.0" - id "com.diffplug.gradle.spotless" version "4.4.0" - id "dev.afanasev.sekret" version "0.0.3" + id "java" + id "org.jetbrains.kotlin.jvm" version "1.3.72" + id "application" + id "com.github.johnrengelman.shadow" version "5.2.0" + id "com.diffplug.gradle.spotless" version "4.4.0" + id "dev.afanasev.sekret" version "0.0.3" } group = "com.mangadex" @@ -12,102 +12,67 @@ version = "git describe --tags --dirty".execute().text.trim() mainClassName = "mdnet.base.Main" repositories { - mavenCentral() - jcenter() + mavenCentral() + jcenter() } configurations { - runtime.exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core" - runtime.exclude group: "com.sun.mail", module: "javax.mail" + runtime.exclude group: "org.jetbrains.kotlinx", module: "kotlinx-coroutines-core" + runtime.exclude group: "com.sun.mail", module: "javax.mail" } dependencies { - compileOnly group: "dev.afanasev", name: "sekret-annotation", version: "0.0.3" + compileOnly group: "dev.afanasev", name: "sekret-annotation", version: "0.0.3" - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" - implementation "org.jetbrains.kotlin:kotlin-reflect" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + implementation "org.jetbrains.kotlin:kotlin-reflect" - implementation group: "commons-io", name: "commons-io", version: "2.7" + implementation group: "commons-io", name: "commons-io", version: "2.7" - implementation group: "org.http4k", name: "http4k-core", version: "$http_4k_version" - implementation group: "org.http4k", name: "http4k-format-jackson", version: "$http_4k_version" - implementation group: "com.fasterxml.jackson.datatype", name: "jackson-datatype-jsr310", version: "2.11.1" - implementation group: "org.http4k", name: "http4k-client-apache", version: "$http_4k_version" - implementation group: "org.http4k", name: "http4k-server-netty", version: "$http_4k_version" - runtimeOnly group: "io.netty", name: "netty-tcnative-boringssl-static", version: "2.0.30.Final" + implementation group: "org.http4k", name: "http4k-core", version: "$http_4k_version" + implementation group: "org.http4k", name: "http4k-format-jackson", version: "$http_4k_version" + implementation group: "com.fasterxml.jackson.datatype", name: "jackson-datatype-jsr310", version: "2.11.1" + implementation group: "org.http4k", name: "http4k-client-apache", version: "$http_4k_version" + implementation group: "org.http4k", name: "http4k-server-netty", version: "$http_4k_version" + runtimeOnly group: "io.netty", name: "netty-tcnative-boringssl-static", version: "2.0.30.Final" - implementation group: "ch.qos.logback", name: "logback-classic", version: "1.3.0-alpha4" - implementation group: "org.jetbrains.exposed", name: "exposed-core", version: "$exposed_version" - implementation group: "org.jetbrains.exposed", name: "exposed-dao", version: "$exposed_version" - implementation group: "org.jetbrains.exposed", name: "exposed-jdbc", version: "$exposed_version" + implementation group: "ch.qos.logback", name: "logback-classic", version: "1.3.0-alpha4" + implementation group: "org.jetbrains.exposed", name: "exposed-core", version: "$exposed_version" + implementation group: "org.jetbrains.exposed", name: "exposed-dao", version: "$exposed_version" + implementation group: "org.jetbrains.exposed", name: "exposed-jdbc", version: "$exposed_version" - implementation group: "org.xerial", name: "sqlite-jdbc", version: "3.30.1" + implementation group: "org.xerial", name: "sqlite-jdbc", version: "3.30.1" - implementation "com.goterl.lazycode:lazysodium-java:4.2.6" - implementation "net.java.dev.jna:jna:5.5.0" + implementation "com.goterl.lazycode:lazysodium-java:4.2.6" + implementation "net.java.dev.jna:jna:5.5.0" } java { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 } spotless { - java { - indentWithSpaces(4) - eclipse() - removeUnusedImports() - trimTrailingWhitespace() - endWithNewline() - } - kotlin { - indentWithSpaces(4) - ktlint() - trimTrailingWhitespace() - endWithNewline() - } -} - -run { - args = ["dev/settings.json"] -} - -tasks.register("depsize") { - description = "Prints dependencies for 'default' configuration" - doLast() { - listConfigurationDependencies(configurations.default) - } -} - -tasks.register("depsize-all-configurations") { - description = "Prints dependencies for all available configurations" - doLast() { - configurations.each { - if (it.isCanBeResolved()) { - listConfigurationDependencies(it) - } + java { + targetExclude("build/generated/**/*") + eclipse() + removeUnusedImports() + trimTrailingWhitespace() + endWithNewline() + } + kotlin { + ktlint() + trimTrailingWhitespace() + endWithNewline() } - } } -def listConfigurationDependencies(Configuration configuration) { - def formatStr = "%,10.2f" +tasks.register("generateVersion", Copy) { + def templateContext = [version: version] + inputs.properties templateContext + from "src/template/java" + into "$buildDir/generated/java" + expand templateContext +} - def size = configuration.collect { it.length() / (1024 * 1024) }.sum() - - def out = new StringBuffer() - out << "\nConfiguration name: \"${configuration.name}\"\n" - if (size) { - out << "Total dependencies size:".padRight(65) - out << "${String.format(formatStr, size)} Mb\n\n" - - configuration.sort { -it.length() } - .each { - out << "${it.name}".padRight(65) - out << "${String.format(formatStr, (it.length() / 1024))} kb\n" - } - } else { - out << "No dependencies found"; - } - println(out) -} \ No newline at end of file +sourceSets.main.java.srcDir generateVersion.outputs.files diff --git a/src/main/kotlin/mdnet/base/Constants.kt b/src/main/kotlin/mdnet/base/Constants.kt index 5e082d0..43105f9 100644 --- a/src/main/kotlin/mdnet/base/Constants.kt +++ b/src/main/kotlin/mdnet/base/Constants.kt @@ -22,7 +22,6 @@ import java.time.Duration object Constants { const val CLIENT_BUILD = 14 - const val CLIENT_VERSION = "1.1.1" const val WEBUI_VERSION = "0.1.1" val MAX_AGE_CACHE: Duration = Duration.ofDays(14) diff --git a/src/main/kotlin/mdnet/base/Main.kt b/src/main/kotlin/mdnet/base/Main.kt index fea408a..9308e0d 100644 --- a/src/main/kotlin/mdnet/base/Main.kt +++ b/src/main/kotlin/mdnet/base/Main.kt @@ -31,6 +31,7 @@ import java.io.FileWriter import java.io.IOException import java.util.regex.Pattern import kotlin.system.exitProcess +import mdnet.BuildInfo import mdnet.base.settings.ClientSettings import org.slf4j.LoggerFactory @@ -41,7 +42,7 @@ object Main { @JvmStatic fun main(args: Array) { println( - "Mangadex@Home Client Version ${Constants.CLIENT_VERSION} (Build ${Constants.CLIENT_BUILD}) initializing" + "Mangadex@Home Client Version ${BuildInfo.VERSION} (Build ${Constants.CLIENT_BUILD}) initializing" ) println() println("Copyright (c) 2020, MangaDex Network") diff --git a/src/main/kotlin/mdnet/base/server/ImageServer.kt b/src/main/kotlin/mdnet/base/server/ImageServer.kt index 93af258..b3db478 100644 --- a/src/main/kotlin/mdnet/base/server/ImageServer.kt +++ b/src/main/kotlin/mdnet/base/server/ImageServer.kt @@ -19,6 +19,7 @@ along with this MangaDex@Home. If not, see . /* ktlint-disable no-wildcard-imports */ package mdnet.base.server +import com.fasterxml.jackson.core.JsonProcessingException import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule @@ -107,18 +108,26 @@ class ImageServer(private val cache: DiskLruCache, private val statistics: Atomi if (tokenized || serverSettings.forceTokens) { val tokenArr = Base64.getUrlDecoder().decode(Path.of("token")(request)) - val token = JACKSON.readValue( - try { - sodium.cryptoBoxOpenEasyAfterNm( - tokenArr.sliceArray(24 until tokenArr.size), tokenArr.sliceArray(0 until 24), serverSettings.tokenKey - ) - } catch (_: SodiumException) { - if (LOGGER.isInfoEnabled) { - LOGGER.info("Request for $sanitizedUri rejected for invalid token") + val token = try { + JACKSON.readValue( + try { + sodium.cryptoBoxOpenEasyAfterNm( + tokenArr.sliceArray(24 until tokenArr.size), tokenArr.sliceArray(0 until 24), serverSettings.tokenKey + ) + } catch (_: SodiumException) { + if (LOGGER.isInfoEnabled) { + LOGGER.info("Request for $sanitizedUri rejected for invalid token") + } + return@then Response(Status.FORBIDDEN) } - return@then Response(Status.FORBIDDEN) + ) + } catch (e: JsonProcessingException) { + if (LOGGER.isInfoEnabled) { + LOGGER.info("Request for $sanitizedUri rejected for invalid token") } - ) + return@then Response(Status.FORBIDDEN) + } + if (OffsetDateTime.now().isAfter(token.expires)) { if (LOGGER.isInfoEnabled) { LOGGER.info("Request for $sanitizedUri rejected for expired token") diff --git a/src/main/kotlin/mdnet/base/server/common.kt b/src/main/kotlin/mdnet/base/server/common.kt index c224627..06f8d0f 100644 --- a/src/main/kotlin/mdnet/base/server/common.kt +++ b/src/main/kotlin/mdnet/base/server/common.kt @@ -23,6 +23,7 @@ import java.time.ZoneOffset import java.time.ZonedDateTime import java.time.format.DateTimeFormatter import java.util.* +import mdnet.BuildInfo import mdnet.base.Constants import org.http4k.core.Filter import org.http4k.core.HttpHandler @@ -39,7 +40,7 @@ fun addCommonHeaders(): Filter { { request: Request -> val response = next(request) response.header("Date", HTTP_TIME_FORMATTER.format(ZonedDateTime.now(ZoneOffset.UTC))) - .header("Server", "Mangadex@Home Node ${Constants.CLIENT_VERSION} (${Constants.CLIENT_BUILD})") + .header("Server", "Mangadex@Home Node ${BuildInfo.VERSION} (${Constants.CLIENT_BUILD})") } } } diff --git a/src/template/java/mdnet/BuildInfo.java b/src/template/java/mdnet/BuildInfo.java new file mode 100644 index 0000000..d0665b5 --- /dev/null +++ b/src/template/java/mdnet/BuildInfo.java @@ -0,0 +1,5 @@ +package mdnet; + +public class BuildInfo { + public static final String VERSION = "${version}"; +}