2020-06-22 17:02:36 +00:00
|
|
|
/*
|
|
|
|
Mangadex@Home
|
|
|
|
Copyright (c) 2020, MangaDex Network
|
|
|
|
This file is part of MangaDex@Home.
|
|
|
|
|
|
|
|
MangaDex@Home is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
MangaDex@Home is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this MangaDex@Home. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-01-24 04:55:11 +00:00
|
|
|
/* ktlint-disable no-wildcard-imports */
|
|
|
|
package mdnet
|
2020-06-19 20:16:24 +00:00
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature
|
|
|
|
import com.fasterxml.jackson.module.kotlin.KotlinModule
|
2021-01-24 04:55:11 +00:00
|
|
|
import mdnet.ServerHandlerJackson.auto
|
|
|
|
import mdnet.logging.info
|
|
|
|
import mdnet.settings.DevSettings
|
|
|
|
import mdnet.settings.RemoteSettings
|
|
|
|
import mdnet.settings.ServerSettings
|
|
|
|
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver
|
|
|
|
import org.apache.hc.client5.http.impl.classic.HttpClients
|
|
|
|
import org.apache.hc.client5.http.impl.routing.DefaultRoutePlanner
|
|
|
|
import org.apache.hc.core5.http.HttpHost
|
|
|
|
import org.apache.hc.core5.http.protocol.HttpContext
|
|
|
|
import org.http4k.client.ApacheClient
|
2020-06-19 20:16:24 +00:00
|
|
|
import org.http4k.core.Body
|
|
|
|
import org.http4k.core.Method
|
|
|
|
import org.http4k.core.Request
|
|
|
|
import org.http4k.format.ConfigurableJackson
|
|
|
|
import org.http4k.format.asConfigurable
|
|
|
|
import org.http4k.format.withStandardMappings
|
|
|
|
import org.slf4j.LoggerFactory
|
2021-01-24 04:55:11 +00:00
|
|
|
import java.net.InetAddress
|
2020-07-02 21:24:12 +00:00
|
|
|
|
2020-06-19 20:16:24 +00:00
|
|
|
object ServerHandlerJackson : ConfigurableJackson(
|
|
|
|
KotlinModule()
|
2021-01-24 04:55:11 +00:00
|
|
|
.asConfigurable()
|
|
|
|
.withStandardMappings()
|
|
|
|
.done()
|
|
|
|
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
2020-06-19 20:16:24 +00:00
|
|
|
)
|
|
|
|
|
2021-01-24 04:55:11 +00:00
|
|
|
class BackendApi(
|
|
|
|
private val serverSettings: ServerSettings,
|
|
|
|
private val devSettings: DevSettings,
|
|
|
|
private val maxCacheSizeInMebibytes: Long
|
|
|
|
) {
|
|
|
|
private val client = ApacheClient(
|
|
|
|
client = HttpClients.custom()
|
|
|
|
.setRoutePlanner(
|
|
|
|
object : DefaultRoutePlanner(DefaultSchemePortResolver()) {
|
|
|
|
override fun determineLocalAddress(firstHop: HttpHost?, context: HttpContext?): InetAddress {
|
|
|
|
return InetAddress.getByName(serverSettings.hostname)
|
2020-08-03 14:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-24 04:55:11 +00:00
|
|
|
)
|
|
|
|
.build()
|
|
|
|
)
|
2020-06-19 20:16:24 +00:00
|
|
|
|
|
|
|
fun logoutFromControl(): Boolean {
|
2020-07-04 19:39:11 +00:00
|
|
|
LOGGER.info { "Disconnecting from the control server" }
|
2020-06-19 20:16:24 +00:00
|
|
|
val params = mapOf<String, Any>(
|
2021-01-24 04:55:11 +00:00
|
|
|
"secret" to serverSettings.secret
|
2020-06-19 20:16:24 +00:00
|
|
|
)
|
|
|
|
|
2020-07-02 16:02:40 +00:00
|
|
|
val request = STRING_ANY_MAP_LENS(params, Request(Method.POST, getServerAddress() + "stop"))
|
2020-06-19 20:16:24 +00:00
|
|
|
val response = client(request)
|
|
|
|
|
|
|
|
return response.status.successful
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getPingParams(tlsCreatedAt: String? = null): Map<String, Any> =
|
2021-01-24 04:55:11 +00:00
|
|
|
mapOf(
|
|
|
|
"secret" to serverSettings.secret,
|
2020-06-28 17:42:40 +00:00
|
|
|
"port" to let {
|
2021-01-24 04:55:11 +00:00
|
|
|
if (serverSettings.externalPort != 0) {
|
|
|
|
serverSettings.externalPort
|
2020-06-28 17:42:40 +00:00
|
|
|
} else {
|
2021-01-24 04:55:11 +00:00
|
|
|
serverSettings.port
|
2020-06-28 17:42:40 +00:00
|
|
|
}
|
|
|
|
},
|
2020-08-11 16:47:04 +00:00
|
|
|
"disk_space" to maxCacheSizeInMebibytes * 1024 * 1024,
|
2021-01-24 04:55:11 +00:00
|
|
|
"network_speed" to serverSettings.externalMaxKilobitsPerSecond * 1000 / 8,
|
2020-06-19 20:16:24 +00:00
|
|
|
"build_version" to Constants.CLIENT_BUILD
|
|
|
|
).let {
|
|
|
|
if (tlsCreatedAt != null) {
|
|
|
|
it.plus("tls_created_at" to tlsCreatedAt)
|
|
|
|
} else {
|
|
|
|
it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 16:47:04 +00:00
|
|
|
fun loginToControl(): RemoteSettings? {
|
2020-07-04 19:39:11 +00:00
|
|
|
LOGGER.info { "Connecting to the control server" }
|
2020-06-19 20:16:24 +00:00
|
|
|
|
2020-07-02 16:02:40 +00:00
|
|
|
val request = STRING_ANY_MAP_LENS(getPingParams(), Request(Method.POST, getServerAddress() + "ping"))
|
2020-06-19 20:16:24 +00:00
|
|
|
val response = client(request)
|
|
|
|
|
|
|
|
return if (response.status.successful) {
|
2020-07-02 21:54:24 +00:00
|
|
|
SERVER_SETTINGS_LENS(response)
|
2020-06-19 20:16:24 +00:00
|
|
|
} else {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 16:47:04 +00:00
|
|
|
fun pingControl(old: RemoteSettings): RemoteSettings? {
|
2020-07-04 19:39:11 +00:00
|
|
|
LOGGER.info { "Pinging the control server" }
|
2020-06-19 20:16:24 +00:00
|
|
|
|
2021-01-24 04:55:11 +00:00
|
|
|
val request = STRING_ANY_MAP_LENS(
|
|
|
|
getPingParams(old.tls!!.createdAt),
|
|
|
|
Request(
|
|
|
|
Method.POST,
|
|
|
|
getServerAddress() + "ping"
|
|
|
|
)
|
|
|
|
)
|
2020-06-19 20:16:24 +00:00
|
|
|
val response = client(request)
|
|
|
|
|
|
|
|
return if (response.status.successful) {
|
|
|
|
SERVER_SETTINGS_LENS(response)
|
|
|
|
} else {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:02:40 +00:00
|
|
|
private fun getServerAddress(): String {
|
2021-01-24 04:55:11 +00:00
|
|
|
return devSettings.devUrl ?: SERVER_ADDRESS
|
2020-07-02 16:02:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:16:24 +00:00
|
|
|
companion object {
|
2021-01-24 04:55:11 +00:00
|
|
|
private val LOGGER = LoggerFactory.getLogger(BackendApi::class.java)
|
2020-06-19 20:16:24 +00:00
|
|
|
private val STRING_ANY_MAP_LENS = Body.auto<Map<String, Any>>().toLens()
|
2020-08-11 16:47:04 +00:00
|
|
|
private val SERVER_SETTINGS_LENS = Body.auto<RemoteSettings>().toLens()
|
2020-06-19 20:16:24 +00:00
|
|
|
private const val SERVER_ADDRESS = "https://api.mangadex.network/"
|
|
|
|
}
|
|
|
|
}
|