1
0
Fork 1
mirror of https://gitlab.com/mangadex-pub/mangadex_at_home.git synced 2024-01-19 02:48:37 +00:00
mangadex_at_home/src/main/kotlin/mdnet/BackendApi.kt

142 lines
4.7 KiB
Kotlin
Raw Normal View History

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-25 02:25:49 +00:00
*/
2021-01-24 04:55:11 +00:00
package mdnet
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
2021-01-28 13:50:13 +00:00
import mdnet.settings.*
2021-03-11 20:09:14 +00:00
import okhttp3.Dns
import okhttp3.OkHttpClient
import org.http4k.client.OkHttp
import org.http4k.core.*
import org.http4k.format.ConfigurableJackson
import org.http4k.format.asConfigurable
import org.http4k.format.withStandardMappings
2021-01-28 13:50:13 +00:00
import org.http4k.lens.LensFailure
import org.slf4j.LoggerFactory
2021-03-11 20:09:14 +00:00
import java.net.Inet4Address
import java.net.InetAddress
2020-07-02 21:24:12 +00:00
object ServerHandlerJackson : ConfigurableJackson(
KotlinModule()
2021-01-24 04:55:11 +00:00
.asConfigurable()
.withStandardMappings()
.done()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
)
2021-03-11 20:09:14 +00:00
class BackendApi(private val settings: ClientSettings) {
private val client = OkHttp(
client = OkHttpClient.Builder()
.dns(object : Dns {
override fun lookup(hostname: String): List<InetAddress> {
return Dns.SYSTEM.lookup(hostname).filterIsInstance<Inet4Address>()
}
})
.build()
)
2021-01-25 18:22:07 +00:00
private val serverAddress = settings.devSettings.devUrl ?: SERVER_ADDRESS
fun logoutFromControl(): Boolean {
2021-01-28 13:50:13 +00:00
val serverSettings = settings.serverSettings
2020-07-04 19:39:11 +00:00
LOGGER.info { "Disconnecting from the control server" }
2021-01-28 13:50:13 +00:00
val request = LOGOUT_REQUEST_LENS(
LogoutRequest(serverSettings.secret),
Request(Method.POST, serverAddress + "stop")
)
val response = client(request)
return response.status.successful
}
2021-01-28 13:50:13 +00:00
private fun getPingParams(tlsCreatedAt: String? = null): SettingsRequest {
2021-01-25 18:22:07 +00:00
val serverSettings = settings.serverSettings
2021-01-28 13:50:13 +00:00
return SettingsRequest(
secret = serverSettings.secret,
port = if (serverSettings.externalPort != 0) {
serverSettings.externalPort
} else {
2021-01-28 13:50:13 +00:00
serverSettings.port
},
buildVersion = Constants.CLIENT_BUILD,
diskSpace = settings.maxCacheSizeInMebibytes * 1024 * 1024,
networkSpeed = serverSettings.externalMaxKilobitsPerSecond * 1000 / 8,
ipAddress = serverSettings.externalIp,
tlsCreatedAt = tlsCreatedAt,
)
2021-01-25 18:22:07 +00:00
}
2021-01-28 13:50:13 +00:00
fun loginToControl(): PingResult {
2020-07-04 19:39:11 +00:00
LOGGER.info { "Connecting to the control server" }
2021-01-28 13:50:13 +00:00
val request = SETTINGS_REQUEST_LENS(
2021-01-25 18:22:07 +00:00
getPingParams(null),
Request(
Method.POST,
serverAddress + "ping"
)
)
2021-01-28 13:50:13 +00:00
return client.makeRequest(request)
}
2021-01-28 13:50:13 +00:00
fun pingControl(old: RemoteSettings): PingResult {
2020-07-04 19:39:11 +00:00
LOGGER.info { "Pinging the control server" }
2021-01-28 13:50:13 +00:00
val request = SETTINGS_REQUEST_LENS(
2021-01-24 04:55:11 +00:00
getPingParams(old.tls!!.createdAt),
Request(
Method.POST,
2021-01-25 18:22:07 +00:00
serverAddress + "ping"
2021-01-24 04:55:11 +00:00
)
)
2021-01-28 13:50:13 +00:00
return client.makeRequest(request)
}
private fun HttpHandler.makeRequest(request: Request): PingResult {
val response = this(request)
2021-01-28 13:50:13 +00:00
return when {
response.status.successful -> {
SERVER_SETTINGS_LENS(response)
}
else -> {
try {
PING_FAILURE_LENS(response)
} catch (e: LensFailure) {
PingFailure(response.status.code, response.status.description)
}
}
}
}
companion object {
2021-01-24 04:55:11 +00:00
private val LOGGER = LoggerFactory.getLogger(BackendApi::class.java)
2021-01-28 13:50:13 +00:00
private val SETTINGS_REQUEST_LENS = Body.auto<SettingsRequest>().toLens()
private val PING_FAILURE_LENS = Body.auto<PingFailure>().toLens()
private val LOGOUT_REQUEST_LENS = Body.auto<LogoutRequest>().toLens()
private val SERVER_SETTINGS_LENS = Body.auto<RemoteSettings>().toLens()
private const val SERVER_ADDRESS = "https://api.mangadex.network/"
}
}