Skip to content

Commit

Permalink
Merge pull request #29 from DedCRS/bukkit
Browse files Browse the repository at this point in the history
Feature: permission-based IP override
  • Loading branch information
yukonisen authored Jun 19, 2024
2 parents 30e85ac + 3ba9ce8 commit fe4c9b5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 14 deletions.
2 changes: 1 addition & 1 deletion PLUGIN_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PLUGIN=1.4.1
PLUGIN=1.4.2
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ class PotatoIpDisplayCommand : TabExecutor {
val player = Bukkit.getServer().getPlayer(target)
if (player != null) {
sendMsg(sender, "§f查询玩家: §b$target", true)
val playerAddress: String = player.address?.address.toString().replace("/", "")
sendMsg(sender, lookup(playerAddress), false)
sendMsg(sender, lookup(IpParseFactory.getPlayerAddress(player), player.name), false)
return true
}

if (validate(target)) {
if (IpParseFactory.regexValidated(target)) {
sendMsg(sender, "§f查询 IPv4: §b$target", true)
sendMsg(sender, lookup(target), false)
} else sendMsg(sender, "§c查询的玩家离线,或 IPv4 无效", true)
Expand Down Expand Up @@ -148,21 +147,16 @@ class PotatoIpDisplayCommand : TabExecutor {
}
}

private fun lookup(ip: String): String {
private fun lookup(ip: String, playerName: String = ""): String {
val ipParse = IpParseFactory.getIpParse(ip)
return "§f - IP: §e$ip \n" +
val mod = if (IpAttributeMap.playerIpAddressMap[playerName] != null) "§a*" else ""
return "§f - IP: §e$ip$mod \n" +
"§f - 国家: §e${ipParse.getCountry()} \n" +
"§f - 省市: §e${ipParse.getProvince()} ${ipParse.getCity()} \n" +
"§f - ISP: §e${ipParse.getISP()} \n" +
"§f - IP属地: §a${ipParse.getFallback()}"
}

private fun validate(ip: String): Boolean {
val ipRegex =
"""(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}""".toRegex()
return ip.matches(ipRegex)
}

private fun clear(target: String): Int {
val map = IpAttributeMap
var clearedItems = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class PlaceholderIntergration : PlaceholderExpansion() {
player: Player?,
params: String
): String? {
val ip: String = player?.address?.address.toString().replace("/", "")
if (player == null) { return null}
val ip: String = IpParseFactory.getPlayerAddress(player)
val ipParse = IpParseFactory.getIpParse(ip)
return when (params) {
"ip" -> ip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ class PlayerJoinListener: Listener {

object : BukkitRunnable() {
override fun run() {
val playerAddress = event.address.hostAddress
val playerName = event.player.name
val originalIP = event.address.hostAddress

/* Regenerate player permission override cache on join */
IpAttributeMap.playerIpAddressMap.remove(playerName)

val playerAddress = IpParseFactory.getPlayerAddress(event.player, originalIP)
val ipParse = IpParseFactory.getIpParse(playerAddress)
val result = ipParse.getFallback()
IpAttributeMap.playerIpAttributeMap[playerName] = result
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package indi.nightfish.potato_ip_display.parser

import indi.nightfish.potato_ip_display.PotatoIpDisplay
import indi.nightfish.potato_ip_display.PotatoIpDisplay.Instance.plugin
import indi.nightfish.potato_ip_display.parser.provider.Ip2regionParser
import indi.nightfish.potato_ip_display.parser.provider.IpApiParser
import indi.nightfish.potato_ip_display.parser.provider.PconlineParser
import indi.nightfish.potato_ip_display.util.IpAttributeMap
import org.bukkit.entity.Player
import java.util.logging.Level


object IpParseFactory {

Expand All @@ -16,4 +21,37 @@ object IpParseFactory {
else -> throw IllegalArgumentException("Invalid mode in config >> $mode")
}
}

fun getPlayerAddress(player: Player, fallback: String = "0.0.0.0"): String {
val playerName = player.name
val map = IpAttributeMap.playerIpAddressMap[playerName]
if (map != null) return map

val prefix = "potatoipdisplay.override."
val original = player.address?.address.toString().replace("/", "")
val override = player.effectivePermissions.find { it.permission.startsWith(prefix) }

return override?.let {
val overrideIP = it.permission.substring(prefix.length).replace("-", ".")
if (regexValidated(overrideIP)) {
IpAttributeMap.playerIpAddressMap[playerName] = overrideIP
return overrideIP
} else {
plugin.log("Invalid IP for $playerName from permission: ${override.permission}! using fallback $fallback", Level.WARNING)
if (original != "null") return original
else return fallback
}
} ?: run {
if (original != "null") return original
else return fallback
}

}

fun regexValidated(ip: String): Boolean {
val ipRegex =
"""(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}""".toRegex()
return ip.matches(ipRegex)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ object IpAttributeMap {
val pconlineRawDataMap: MutableMap<String, JsonObject> = mutableMapOf()
val ipApiRawDataMap: MutableMap<String, JsonObject> = mutableMapOf()
val playerIpAttributeMap: MutableMap<String, String> = mutableMapOf()
val playerIpAddressMap: MutableMap<String, String> = mutableMapOf()
}

0 comments on commit fe4c9b5

Please sign in to comment.