From 307b28e13ca606eec96e9522020e6dd3a77cd4db Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Fri, 3 Jul 2020 16:22:40 -0500 Subject: [PATCH] Report proper hostname --- CHANGELOG.md | 3 +- .../kotlin/mdnet/base/server/Application.kt | 34 +++++++++---------- .../kotlin/mdnet/base/server/ImageServer.kt | 10 +++++- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ab83d5..dd2d3e5 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [2020-06-28] Added `pasued` field in ServerSettings [@carbotaniuman]. - [2020-06-28] Hopefully fixed connection leaks [@carbotaniuman]. - [2020-07-02] Minor fixes and changes to data handling in web interface [@RedMatriz]. -- [2020-07-02] Renamed loaclstorage keys in web interface [@RedMatriz]. +- [2020-07-02] Renamed localstorage keys in web interface [@RedMatriz]. +- [2020-06-28] Actually report custom `client_hostname` [@carbotaniuman]. ### Deprecated diff --git a/src/main/kotlin/mdnet/base/server/Application.kt b/src/main/kotlin/mdnet/base/server/Application.kt index 295c936..4f647af 100644 --- a/src/main/kotlin/mdnet/base/server/Application.kt +++ b/src/main/kotlin/mdnet/base/server/Application.kt @@ -39,27 +39,27 @@ private val LOGGER = LoggerFactory.getLogger("Application") fun getServer(cache: DiskLruCache, serverSettings: ServerSettings, clientSettings: ClientSettings, statistics: AtomicReference, isHandled: AtomicBoolean): Http4kServer { val database = Database.connect("jdbc:sqlite:cache/data.db", "org.sqlite.JDBC") - val imageServer = ImageServer(cache, statistics, serverSettings, database, isHandled) + val imageServer = ImageServer(cache, statistics, serverSettings, database, clientSettings.clientHostname, isHandled) return timeRequest() - .then(catchAllHideDetails()) - .then(ServerFilters.CatchLensFailure) - .then(addCommonHeaders()) - .then( - routes( - "/data/{chapterHash}/{fileName}" bind Method.GET to imageServer.handler(dataSaver = false), - "/data-saver/{chapterHash}/{fileName}" bind Method.GET to imageServer.handler(dataSaver = true), - "/{token}/data/{chapterHash}/{fileName}" bind Method.GET to imageServer.handler( - dataSaver = false, - tokenized = true - ), - "/{token}/data-saver/{chapterHash}/{fileName}" bind Method.GET to imageServer.handler( - dataSaver = true, - tokenized = true - ) + .then(catchAllHideDetails()) + .then(ServerFilters.CatchLensFailure) + .then(addCommonHeaders()) + .then( + routes( + "/data/{chapterHash}/{fileName}" bind Method.GET to imageServer.handler(dataSaver = false), + "/data-saver/{chapterHash}/{fileName}" bind Method.GET to imageServer.handler(dataSaver = true), + "/{token}/data/{chapterHash}/{fileName}" bind Method.GET to imageServer.handler( + dataSaver = false, + tokenized = true + ), + "/{token}/data-saver/{chapterHash}/{fileName}" bind Method.GET to imageServer.handler( + dataSaver = true, + tokenized = true ) ) - .asServer(Netty(serverSettings.tls!!, clientSettings, statistics)) + ) + .asServer(Netty(serverSettings.tls!!, clientSettings, statistics)) } fun timeRequest(): Filter { diff --git a/src/main/kotlin/mdnet/base/server/ImageServer.kt b/src/main/kotlin/mdnet/base/server/ImageServer.kt index 4c80490..adccfbb 100644 --- a/src/main/kotlin/mdnet/base/server/ImageServer.kt +++ b/src/main/kotlin/mdnet/base/server/ImageServer.kt @@ -31,6 +31,7 @@ import java.io.BufferedInputStream import java.io.BufferedOutputStream import java.io.File import java.io.InputStream +import java.net.InetAddress import java.security.MessageDigest import java.time.Clock import java.time.OffsetDateTime @@ -66,7 +67,7 @@ import org.slf4j.LoggerFactory private const val THREADS_TO_ALLOCATE = 262144 // 2**18 -class ImageServer(private val cache: DiskLruCache, private val statistics: AtomicReference, private val serverSettings: ServerSettings, private val database: Database, private val handled: AtomicBoolean) { +class ImageServer(private val cache: DiskLruCache, private val statistics: AtomicReference, private val serverSettings: ServerSettings, private val database: Database, private val clientHostname: String, private val handled: AtomicBoolean) { init { transaction(database) { SchemaUtils.create(ImageData) @@ -81,6 +82,13 @@ class ImageServer(private val cache: DiskLruCache, private val statistics: Atomi .setConnectTimeout(3000) .setSocketTimeout(3000) .setConnectionRequestTimeout(3000) + .setLocalAddress( + if (clientHostname != "0.0.0.0") { + InetAddress.getByName(clientHostname) + } else { + InetAddress.getLocalHost() + } + ) .build()) .setMaxConnTotal(THREADS_TO_ALLOCATE) .setMaxConnPerRoute(THREADS_TO_ALLOCATE)