Improve code style

This commit is contained in:
Jakob K
2021-05-12 14:17:44 +02:00
parent 3e9b243d3d
commit cf48510756
69 changed files with 144 additions and 609 deletions

View File

@@ -30,7 +30,7 @@ fun Player.hasBadIP(detector: BadIPDetector = BadIPDetector.DEFAULT) =
*/
fun Player.checkIP(
detector: BadIPDetector = BadIPDetector.DEFAULT,
breakOnHit: Boolean = true
breakOnHit: Boolean = true,
): Map<BadIPDetectionService, BadIPDetectionResult> {
val ip = address?.hostString ?: return emptyMap()
return detector.checkIP(ip, breakOnHit)
@@ -47,9 +47,8 @@ fun Player.checkIP(
* - [net.axay.kspigot.ipaddress.badipdetectionservices.VPNBlocker]
*/
class BadIPDetector(
val services: List<BadIPDetectionService>
val services: List<BadIPDetectionService>,
) {
/**
* Alternative constructor.
* @see BadIPDetector
@@ -65,46 +64,35 @@ class BadIPDetector(
fun checkIP(ip: String, breakOnHit: Boolean = true) =
HashMap<BadIPDetectionService, BadIPDetectionResult>().apply {
for (it in services) {
val curResult = it.isBad(ip)
this[it] = curResult
if (curResult.isBad && breakOnHit) break
}
}
}
enum class BadIPDetectionResult(
val isBad: Boolean,
val typeName: String
val typeName: String,
) {
GENERAL_BAD(true, "bad ip"),
VPN(true, "vpn"),
PROXY(true, "proxy"),
TOR(true, "tor network"),
HOSTING(true, "hosting"),
GOOD(false, "valid ip"),
ERROR(false, "error"),
LIMIT(false, "limit");
}
abstract class BadIPDetectionService(
val name: String
val name: String,
) {
protected abstract fun requestString(ip: String): String
protected open fun requestHeaders() = emptyMap<String, String>()
protected abstract fun interpreteResult(result: JSONObject): BadIPDetectionResult
fun isBad(ip: String): BadIPDetectionResult {
val con = URL(requestString(ip)).openConnection() as HttpURLConnection
con.requestMethod = "GET"
requestHeaders().forEach { (field, value) -> con.setRequestProperty(field, value) }
@@ -113,7 +101,6 @@ abstract class BadIPDetectionService(
if (con.responseCode == 429)
return BadIPDetectionResult.LIMIT
else {
val result = try {
con.inputStream.use { JSONObject(it.readAllBytes().decodeToString()) }
} catch (exc: JSONException) {
@@ -125,9 +112,6 @@ abstract class BadIPDetectionService(
} catch (exc: Exception) {
return BadIPDetectionResult.ERROR
}
}
}
}

View File

@@ -28,11 +28,8 @@ val Player.ipAddressData get() = ipAddressData()
* be found out about the IP address of the player.
*/
fun Player.ipAddressData(language: IPAddressDataLanguage = IPAddressDataLanguage.ENGLISH): IPAddressData? {
return try {
val hostString = address?.hostString ?: return null
val jsonObject = ValueHolder.getGson().fromUrlJson(
"$IP_API${hostString}?fields=${IP_API_FIELDS}?lang=${language.code}"
) ?: return null
@@ -40,11 +37,9 @@ fun Player.ipAddressData(language: IPAddressDataLanguage = IPAddressDataLanguage
if (jsonObject["status"].toString() == "fail") return null
IPAddressData(jsonObject)
} catch (exc: Exception) {
null
}
}
enum class IPAddressDataLanguage(val code: String) {
@@ -59,7 +54,6 @@ enum class IPAddressDataLanguage(val code: String) {
}
class IPAddressData(private val json: JsonObject) {
val ip get() = json.getStringOrNull("query")
// region
@@ -81,5 +75,4 @@ class IPAddressData(private val json: JsonObject) {
// information
val internetServiceProvider get() = json.getStringOrNull("isp")
val organisation get() = json.getStringOrNull("org")
}

View File

@@ -9,15 +9,14 @@ import org.json.JSONObject
class GetIPIntel(
private val intensity: Float = 0.99f,
private val contactEmail: String = "foo@bar.com"
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"
override fun requestString(ip: String) =
"http://check.getipintel.net/check.php?ip=$ip&contact=$contactEmail&format=json"
override fun interpreteResult(result: JSONObject): BadIPDetectionResult {
val probability = result.getStringOrNull("result")?.toFloatOrNull()
?: return BadIPDetectionResult.ERROR
return if (probability >= intensity) BadIPDetectionResult.GENERAL_BAD else BadIPDetectionResult.GOOD
}
}

View File

@@ -9,15 +9,12 @@ import org.json.JSONObject
class IPHub(
private val apiKey: String,
private val ifStrict: Boolean = false
private val ifStrict: Boolean = false,
) : BadIPDetectionService("iphub.info") {
override fun requestString(ip: String) = "http://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
return if (ifBlock == 1 || (ifStrict && ifBlock == 2)) BadIPDetectionResult.GENERAL_BAD else BadIPDetectionResult.GOOD
}
}

View File

@@ -6,11 +6,9 @@ import net.axay.kspigot.languageextensions.getStringOrNull
import org.json.JSONObject
class IPInfo(
private val token: String
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 {
return when {
result.getStringOrNull("vpn").toBoolean() -> BadIPDetectionResult.VPN
@@ -20,5 +18,4 @@ class IPInfo(
else -> BadIPDetectionResult.GOOD
}
}
}

View File

@@ -6,9 +6,7 @@ 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")
return when {
@@ -20,5 +18,4 @@ class VPNBlocker : BadIPDetectionService("vpnblocker.net") {
}
}
}
}