From 8d59c3fd2e751eded9825aae9359da86e7507fdd Mon Sep 17 00:00:00 2001 From: Jakob K Date: Sat, 15 May 2021 18:32:37 +0200 Subject: [PATCH] Use kotlinx.serialization for ip requests --- .../axay/kspigot/ipaddress/BadIPDetection.kt | 18 +++-- .../axay/kspigot/ipaddress/IPAddressData.kt | 69 ++++++++++++------- .../badipdetectionservices/GetIPIntel.kt | 13 ++-- .../ipaddress/badipdetectionservices/IPHub.kt | 14 ++-- .../badipdetectionservices/IPInfo.kt | 18 ++--- .../badipdetectionservices/VPNBlocker.kt | 19 ++--- 6 files changed, 91 insertions(+), 60 deletions(-) diff --git a/src/main/kotlin/net/axay/kspigot/ipaddress/BadIPDetection.kt b/src/main/kotlin/net/axay/kspigot/ipaddress/BadIPDetection.kt index 7568c467..6f5da542 100644 --- a/src/main/kotlin/net/axay/kspigot/ipaddress/BadIPDetection.kt +++ b/src/main/kotlin/net/axay/kspigot/ipaddress/BadIPDetection.kt @@ -1,9 +1,11 @@ package net.axay.kspigot.ipaddress +import kotlinx.serialization.SerializationException +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject import net.axay.kspigot.ipaddress.badipdetectionservices.GetIPIntel import org.bukkit.entity.Player -import org.json.JSONException -import org.json.JSONObject import java.net.HttpURLConnection import java.net.URL @@ -47,7 +49,7 @@ fun Player.checkIP( * - [net.axay.kspigot.ipaddress.badipdetectionservices.VPNBlocker] */ class BadIPDetector( - val services: List, + private val services: List, ) { /** * Alternative constructor. @@ -91,7 +93,9 @@ abstract class BadIPDetectionService( ) { protected abstract fun requestString(ip: String): String protected open fun requestHeaders() = emptyMap() - protected abstract fun interpreteResult(result: JSONObject): BadIPDetectionResult + + protected abstract fun interpreteResult(result: JsonObject): BadIPDetectionResult + fun isBad(ip: String): BadIPDetectionResult { val con = URL(requestString(ip)).openConnection() as HttpURLConnection con.requestMethod = "GET" @@ -102,8 +106,8 @@ abstract class BadIPDetectionService( return BadIPDetectionResult.LIMIT else { val result = try { - con.inputStream.use { JSONObject(it.readAllBytes().decodeToString()) } - } catch (exc: JSONException) { + con.inputStream.use { Json.decodeFromString(it.readAllBytes().decodeToString()) } + } catch (exc: SerializationException) { null } ?: return BadIPDetectionResult.ERROR @@ -114,4 +118,4 @@ abstract class BadIPDetectionService( } } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt b/src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt index 38878923..68906242 100644 --- a/src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt +++ b/src/main/kotlin/net/axay/kspigot/ipaddress/IPAddressData.kt @@ -2,15 +2,34 @@ package net.axay.kspigot.ipaddress -import com.google.gson.JsonObject -import net.axay.kspigot.languageextensions.fromUrlJson -import net.axay.kspigot.languageextensions.getStringOrNull -import net.axay.kspigot.main.ValueHolder +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.jsonPrimitive import org.bukkit.entity.Player +@Suppress("HttpUrlsUsage") private const val IP_API = "http://ip-api.com/json/" -private const val IP_API_FIELDS = - "status,message,continent,continentCode,country,countryCode,region,regionName,city,district,zip,lat,lon,timezone,currency,isp,org,query" +private val IP_API_FIELDS = listOf( + "status", + "message", + "continent", + "continentCode", + "country", + "countryCode", + "region", + "regionName", + "city", + "district", + "zip", + "lat", + "lon", + "timezone", + "currency", + "isp", + "org", + "query" +).joinToString(",") /** * @return The players ip address. @@ -30,11 +49,11 @@ val Player.ipAddressData get() = ipAddressData() fun Player.ipAddressData(language: IPAddressDataLanguage = IPAddressDataLanguage.ENGLISH): IPAddressData? { return try { val hostString = address?.hostString ?: return null - val jsonObject = ValueHolder.getGson().fromUrlJson( + val jsonObject = Json.decodeFromString( "$IP_API${hostString}?fields=${IP_API_FIELDS}?lang=${language.code}" - ) ?: return null + ) - if (jsonObject["status"].toString() == "fail") return null + if (jsonObject["status"]?.jsonPrimitive?.toString() == "fail") return null IPAddressData(jsonObject) } catch (exc: Exception) { @@ -54,25 +73,25 @@ enum class IPAddressDataLanguage(val code: String) { } class IPAddressData(private val json: JsonObject) { - val ip get() = json.getStringOrNull("query") + val ip by lazy { json["query"]?.jsonPrimitive?.toString() } // region - val continent get() = json.getStringOrNull("continent") - val continentCode get() = json.getStringOrNull("continentCode") - val country get() = json.getStringOrNull("country") - val countryCode get() = json.getStringOrNull("countryCode") - val region get() = json.getStringOrNull("regionName") - val regionCode get() = json.getStringOrNull("region") - val city get() = json.getStringOrNull("city") - val district get() = json.getStringOrNull("district") - val postalCode get() = json.getStringOrNull("zip") - val timezone get() = json.getStringOrNull("timezone") + val continent by lazy { json["continent"]?.jsonPrimitive?.toString() } + val continentCode by lazy { json["continentCode"]?.jsonPrimitive?.toString() } + val country by lazy { json["country"]?.jsonPrimitive?.toString() } + val countryCode by lazy { json["countryCode"]?.jsonPrimitive?.toString() } + val region by lazy { json["regionName"]?.jsonPrimitive?.toString() } + val regionCode by lazy { json["region"]?.jsonPrimitive?.toString() } + val city by lazy { json["city"]?.jsonPrimitive?.toString() } + val district by lazy { json["district"]?.jsonPrimitive?.toString() } + val postalCode by lazy { json["zip"]?.jsonPrimitive?.toString() } + val timezone by lazy { json["timezone"]?.jsonPrimitive?.toString() } // position - val latitude get() = json.getStringOrNull("lat") - val longitude get() = json.getStringOrNull("lon") + val latitude by lazy { json["lat"]?.jsonPrimitive?.toString() } + val longitude by lazy { json["lon"]?.jsonPrimitive?.toString() } // information - val internetServiceProvider get() = json.getStringOrNull("isp") - val organisation get() = json.getStringOrNull("org") -} \ No newline at end of file + val internetServiceProvider by lazy { json["isp"]?.jsonPrimitive?.toString() } + val organisation by lazy { json["org"]?.jsonPrimitive?.toString() } +} diff --git a/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/GetIPIntel.kt b/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/GetIPIntel.kt index a2a975ba..87836a21 100644 --- a/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/GetIPIntel.kt +++ b/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/GetIPIntel.kt @@ -2,21 +2,22 @@ package net.axay.kspigot.ipaddress.badipdetectionservices +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.floatOrNull +import kotlinx.serialization.json.jsonPrimitive import net.axay.kspigot.ipaddress.BadIPDetectionResult import net.axay.kspigot.ipaddress.BadIPDetectionService -import net.axay.kspigot.languageextensions.getStringOrNull -import org.json.JSONObject class GetIPIntel( private val intensity: Float = 0.99f, private val contactEmail: String = "foo@bar.com", ) : BadIPDetectionService("getipintel.net") { override fun requestString(ip: String) = - "http://check.getipintel.net/check.php?ip=$ip&contact=$contactEmail&format=json" + "https://check.getipintel.net/check.php?ip=$ip&contact=$contactEmail&format=json" - override fun interpreteResult(result: JSONObject): BadIPDetectionResult { - val probability = result.getStringOrNull("result")?.toFloatOrNull() + override fun interpreteResult(result: JsonObject): BadIPDetectionResult { + val probability = result["result"]?.jsonPrimitive?.floatOrNull ?: return BadIPDetectionResult.ERROR return if (probability >= intensity) BadIPDetectionResult.GENERAL_BAD else BadIPDetectionResult.GOOD } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/IPHub.kt b/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/IPHub.kt index 6cb25ea5..48a2adad 100644 --- a/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/IPHub.kt +++ b/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/IPHub.kt @@ -2,19 +2,21 @@ package net.axay.kspigot.ipaddress.badipdetectionservices +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.intOrNull +import kotlinx.serialization.json.jsonPrimitive import net.axay.kspigot.ipaddress.BadIPDetectionResult import net.axay.kspigot.ipaddress.BadIPDetectionService -import net.axay.kspigot.languageextensions.getStringOrNull -import org.json.JSONObject class IPHub( private val apiKey: String, private val ifStrict: Boolean = false, ) : BadIPDetectionService("iphub.info") { - override fun requestString(ip: String) = "http://v2.api.iphub.info/ip/$ip" + override fun requestString(ip: String) = "https://v2.api.iphub.info/ip/$ip" override fun requestHeaders() = mapOf("X-Key" to apiKey) - override fun interpreteResult(result: JSONObject): BadIPDetectionResult { - val ifBlock = result.getStringOrNull("block")?.toInt() ?: return BadIPDetectionResult.ERROR + + override fun interpreteResult(result: JsonObject): BadIPDetectionResult { + val ifBlock = result["block"]?.jsonPrimitive?.intOrNull ?: return BadIPDetectionResult.ERROR return if (ifBlock == 1 || (ifStrict && ifBlock == 2)) BadIPDetectionResult.GENERAL_BAD else BadIPDetectionResult.GOOD } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/IPInfo.kt b/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/IPInfo.kt index 9868128f..90731009 100644 --- a/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/IPInfo.kt +++ b/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/IPInfo.kt @@ -1,21 +1,23 @@ package net.axay.kspigot.ipaddress.badipdetectionservices +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.boolean +import kotlinx.serialization.json.jsonPrimitive import net.axay.kspigot.ipaddress.BadIPDetectionResult import net.axay.kspigot.ipaddress.BadIPDetectionService -import net.axay.kspigot.languageextensions.getStringOrNull -import org.json.JSONObject class IPInfo( private val token: String, ) : BadIPDetectionService("ipinfo.io") { override fun requestString(ip: String) = "https://ipinfo.io/$ip/privacy?token=$token" - override fun interpreteResult(result: JSONObject): BadIPDetectionResult { + + override fun interpreteResult(result: JsonObject): BadIPDetectionResult { return when { - result.getStringOrNull("vpn").toBoolean() -> BadIPDetectionResult.VPN - result.getStringOrNull("proxy").toBoolean() -> BadIPDetectionResult.PROXY - result.getStringOrNull("tor").toBoolean() -> BadIPDetectionResult.TOR - result.getStringOrNull("hosting").toBoolean() -> BadIPDetectionResult.HOSTING + result["vpn"]?.jsonPrimitive?.boolean == true -> BadIPDetectionResult.VPN + result["proxy"]?.jsonPrimitive?.boolean == true -> BadIPDetectionResult.PROXY + result["tor"]?.jsonPrimitive?.boolean == true -> BadIPDetectionResult.TOR + result["hosting"]?.jsonPrimitive?.boolean == true -> BadIPDetectionResult.HOSTING else -> BadIPDetectionResult.GOOD } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/VPNBlocker.kt b/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/VPNBlocker.kt index 64609068..b98c3fa5 100644 --- a/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/VPNBlocker.kt +++ b/src/main/kotlin/net/axay/kspigot/ipaddress/badipdetectionservices/VPNBlocker.kt @@ -1,21 +1,24 @@ package net.axay.kspigot.ipaddress.badipdetectionservices +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.boolean +import kotlinx.serialization.json.intOrNull +import kotlinx.serialization.json.jsonPrimitive import net.axay.kspigot.ipaddress.BadIPDetectionResult import net.axay.kspigot.ipaddress.BadIPDetectionService -import net.axay.kspigot.languageextensions.getStringOrNull -import org.json.JSONObject class VPNBlocker : BadIPDetectionService("vpnblocker.net") { - override fun requestString(ip: String) = "http://api.vpnblocker.net/v2/json/$ip" - override fun interpreteResult(result: JSONObject): BadIPDetectionResult { - val isBad = result.getStringOrNull("host-ip") + override fun requestString(ip: String) = "https://api.vpnblocker.net/v2/json/$ip" + + override fun interpreteResult(result: JsonObject): BadIPDetectionResult { + val isBad = result["host-ip"]?.jsonPrimitive return when { - isBad != null -> if (isBad.toBoolean()) BadIPDetectionResult.GENERAL_BAD else BadIPDetectionResult.GOOD + isBad != null -> if (isBad.boolean) BadIPDetectionResult.GENERAL_BAD else BadIPDetectionResult.GOOD else -> { - val remaining = result.getStringOrNull("remaining_requests")?.toIntOrNull() + val remaining = result["remaining_requests"]?.jsonPrimitive?.intOrNull ?: return BadIPDetectionResult.ERROR if (remaining <= 0) BadIPDetectionResult.LIMIT else BadIPDetectionResult.ERROR } } } -} \ No newline at end of file +}