diff --git a/config.yml b/config.yml index 5794e98..878eebe 100644 --- a/config.yml +++ b/config.yml @@ -31,6 +31,26 @@ banned-ip: # ########################################### +########################################### +# +# Manage when player is using VPN, proxy or hosting +# +# +check-wrong: + enabled: true + vpn: + kick: true + ban: false + proxy: + kick: true + ban: false + hosting: + kick: true + ban: false + +# +########################################### + ########################################### # Notify alert when multiple player connect on the same IP # @@ -58,6 +78,24 @@ ip-notify: # ########################################### + +########################################### +# +# Manage bans +# +ban: + # Currently, there is only one processor: 'command' + processor: "command" + ##### + # Available placeholders: + # %uuid% : Player UUID + # %name% : Player name + # %reason% : Reason of ban + command: "ban %name% %reason%" +# +########################################### + + messages: log_console: "%name% (%uuid%) just connect with IP %ip%" wrong_proxy: "&cPlease connect via &eplay.myserver.net &c!" @@ -65,6 +103,7 @@ messages: reloaded: "&aPlugin IpManager reloaded !" cannot_found: "&cCannot found player !" not_registered: "&cIP of &e%name% &cnot registered" + not_allowed: "&cYou are not allowed to use &l%type%&c." unknow: "Unknow" yes: "Yes" no: "No" diff --git a/src/com/elikill58/ipmanager/ConnectionEvents.java b/src/com/elikill58/ipmanager/ConnectionEvents.java index dc3c94d..8b07bbf 100644 --- a/src/com/elikill58/ipmanager/ConnectionEvents.java +++ b/src/com/elikill58/ipmanager/ConnectionEvents.java @@ -14,6 +14,9 @@ import org.bukkit.event.player.PlayerLoginEvent.Result; import org.bukkit.event.player.PlayerQuitEvent; +import com.elikill58.ipmanager.ban.Ban; +import com.elikill58.ipmanager.ban.BanManager; +import com.elikill58.ipmanager.handler.IP; import com.elikill58.ipmanager.handler.IpPlayer; import com.elikill58.ipmanager.listeners.WrongProxyEvent; @@ -80,11 +83,52 @@ else if(i < nb && i > tempI) { @EventHandler public void onJoin(PlayerJoinEvent e) { Player p = e.getPlayer(); - IpPlayer ip = IpPlayer.getIpPlayer(p); - ip.loadIP(); - ip.setFaiIP(p.getAddress().getHostName()); - ip.save(); - List playersOnIp = IpPlayer.getPlayersOnIP(ip.getBasicIP()); + IpPlayer ipPlayer = IpPlayer.getIpPlayer(p); + ipPlayer.setFaiIP(p.getAddress().getHostName()); + ipPlayer.save(); + + pl.getServer().getScheduler().runTaskAsynchronously(pl, () -> { + ConfigurationSection config = pl.getConfig(); + if(config.getBoolean("check-wrong.enabled")) { + IP ip = IP.getIP(ipPlayer.getBasicIP()); + ConfigurationSection cwSec = config.getConfigurationSection("check-wrong"); + boolean kick = false, ban = false; + StringJoiner reason = new StringJoiner(", "); + + if(ip.isVPN()) { + ConfigurationSection vpnSec = cwSec.getConfigurationSection("vpn"); + kick = vpnSec.getBoolean("kick", true); + ban = vpnSec.getBoolean("ban", true); + reason.add("VPN"); + } + if(ip.isHosting()) { + ConfigurationSection hostSec = cwSec.getConfigurationSection("hosting"); + kick = hostSec.getBoolean("kick", true) || kick; + ban = hostSec.getBoolean("ban", true) || ban; + reason.add("hosting"); + } + if(ip.isProxy()) { + ConfigurationSection proxySec = cwSec.getConfigurationSection("proxy"); + kick = proxySec.getBoolean("kick", true) || kick; + ban = proxySec.getBoolean("ban", true) || ban; + reason.add("proxy"); + } + + if(kick || ban) { + boolean finalBan = ban; + pl.getServer().getScheduler().runTask(pl, () -> { + String msg = Messages.getMessage("messages.not_allowed", "%type%", reason.toString()); + if(finalBan) { + BanManager.getInstance().getProcessor().run(Ban.create(p.getUniqueId(), msg, "Console")); + } + p.kickPlayer(msg); + }); + } else + ipPlayer.setIp(ip); + } + }); + + List playersOnIp = IpPlayer.getPlayersOnIP(ipPlayer.getBasicIP()); ConfigurationSection playerSection = getConfigForAmountPlayer(pl.getConfig().getConfigurationSection("ip-notify"), playersOnIp.size()); if(playerSection == null) return; @@ -111,6 +155,7 @@ public void onJoin(PlayerJoinEvent e) { }); } }); + } @EventHandler(priority = EventPriority.LOWEST) diff --git a/src/com/elikill58/ipmanager/IpManager.java b/src/com/elikill58/ipmanager/IpManager.java index 55c5b8d..940dca0 100644 --- a/src/com/elikill58/ipmanager/IpManager.java +++ b/src/com/elikill58/ipmanager/IpManager.java @@ -8,6 +8,7 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; +import com.elikill58.ipmanager.handler.IP; import com.elikill58.ipmanager.handler.IpPlayer; public class IpManager extends JavaPlugin { @@ -37,7 +38,7 @@ public void onEnable() { Bukkit.getScheduler().runTaskAsynchronously(this, () -> { for(Player p : Utils.getOnlinePlayers()) { IpPlayer ip = IpPlayer.getIpPlayer(p); - ip.loadIP(); + ip.setIp(IP.getIP(ip.getBasicIP())); ip.setFaiIP(p.getAddress().getHostName()); ip.save(); } diff --git a/src/com/elikill58/ipmanager/ban/Ban.java b/src/com/elikill58/ipmanager/ban/Ban.java new file mode 100644 index 0000000..8b33b9f --- /dev/null +++ b/src/com/elikill58/ipmanager/ban/Ban.java @@ -0,0 +1,60 @@ +package com.elikill58.ipmanager.ban; + +import java.util.Objects; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +public class Ban { + + private final UUID playerId; + private final String reason; + private final String bannedBy; + + public Ban(UUID playerId, String reason, String bannedBy) { + this.playerId = playerId; + this.reason = reason; + this.bannedBy = bannedBy; + } + + public UUID getPlayerId() { + return playerId; + } + + public OfflinePlayer getOfflinePlayer() { + return Bukkit.getOfflinePlayer(getPlayerId()); + } + + public String getReason() { + return reason; + } + + public String getBannedBy() { + return bannedBy; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Ban ban = (Ban) o; + return playerId.equals(ban.playerId) && reason.equals(ban.reason) + && bannedBy.equals(ban.bannedBy); + } + + @Override + public int hashCode() { + return Objects.hash(playerId, reason, bannedBy); + } + + public static Ban from(Ban from) { + return new Ban(from.getPlayerId(), from.getReason(), from.getBannedBy()); + } + + public static Ban create(UUID playerId, String reason, String bannedBy) { + return new Ban(playerId, reason, bannedBy); + } +} diff --git a/src/com/elikill58/ipmanager/ban/BanManager.java b/src/com/elikill58/ipmanager/ban/BanManager.java new file mode 100644 index 0000000..b27d8e2 --- /dev/null +++ b/src/com/elikill58/ipmanager/ban/BanManager.java @@ -0,0 +1,31 @@ +package com.elikill58.ipmanager.ban; + +import org.bukkit.configuration.ConfigurationSection; + +import com.elikill58.ipmanager.IpManager; + +public class BanManager { + + private static BanManager instance; + public static BanManager getInstance() { + if(instance == null) + instance = new BanManager(IpManager.getInstance().getConfig().getConfigurationSection("ban")); + return instance; + } + + private final BanProcessor processor; + private final ConfigurationSection config; + + public BanManager(ConfigurationSection config) { + this.config = config; + processor = BanProcessor.valueOf(config.getString("processor", "command").toUpperCase()); + } + + public BanProcessor getProcessor() { + return processor; + } + + public String getCommand() { + return config.getString("command"); + } +} diff --git a/src/com/elikill58/ipmanager/ban/BanProcessor.java b/src/com/elikill58/ipmanager/ban/BanProcessor.java new file mode 100644 index 0000000..b82bfec --- /dev/null +++ b/src/com/elikill58/ipmanager/ban/BanProcessor.java @@ -0,0 +1,22 @@ +package com.elikill58.ipmanager.ban; + +import java.util.function.Consumer; + +import org.bukkit.Bukkit; + +public enum BanProcessor { + + COMMAND((ban) -> { + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), BanManager.getInstance().getCommand().replaceAll("%uuid%", ban.getPlayerId().toString()).replaceAll("%name%", ban.getOfflinePlayer().getName()).replaceAll("%reason%", ban.getReason())); + }); + + private final Consumer call; + + private BanProcessor(Consumer call) { + this.call = call; + } + + public void run(Ban ban) { + call.accept(ban); + } +} \ No newline at end of file diff --git a/src/com/elikill58/ipmanager/handler/IP.java b/src/com/elikill58/ipmanager/handler/IP.java index 64fdf1c..f1ba50a 100644 --- a/src/com/elikill58/ipmanager/handler/IP.java +++ b/src/com/elikill58/ipmanager/handler/IP.java @@ -18,34 +18,40 @@ public class IP { private String allIpJsonInfos; private HashMap ipInfos = new HashMap<>(); private boolean isVPN = false, isProxy = false, isHosting = false; - + public IP(String ip) { this.ip = ip; + + if(Bukkit.isPrimaryThread()) { + IpManager pl = IpManager.getInstance(); + pl.getLogger().severe("Cannot load IP " + ip + " sync ... Loading it async but few error can appear."); + Bukkit.getScheduler().runTaskAsynchronously(pl, this::loadContent); + } else + loadContent(); + } - Bukkit.getScheduler().runTaskAsynchronously(IpManager.getInstance(), () -> { - try { - String checkingVpn = Utils.getContentFromUrl(Utils.getServerURL() + "ipmanager.php?ip=" + ip); - Object data = new JSONParser().parse(checkingVpn); - if (data instanceof JSONObject) { - JSONObject json = (JSONObject) data; - Object status = json.get("status"); - if (status.toString().equalsIgnoreCase("ok")) { - JSONObject result = ((JSONObject) json.get("result")); - isVPN = result.get("vpn") == "true"; - isProxy = result.get("proxy") == "true"; - isHosting = result.get("hosting") == "true"; - } else { - IpManager.getInstance().getLogger() - .severe("Error while loading VPN data for " + ip + ": " + status.toString()); - } - } else - throw new NoSuchFieldException("Cannot found JSON vpn data for '" + allIpJsonInfos + "' string."); - } catch (Exception e) { - e.printStackTrace(); - } - }); - Bukkit.getScheduler().runTaskAsynchronously(IpManager.getInstance(), - () -> allIpJsonInfos = Utils.getContentFromUrl("https://ipapi.co/" + ip + "/json/")); + private void loadContent() { + try { + String checkingVpn = Utils.getContentFromUrl(Utils.getServerURL() + "ipmanager.php?ip=" + ip); + Object data = new JSONParser().parse(checkingVpn); + if (data instanceof JSONObject) { + JSONObject json = (JSONObject) data; + Object status = json.get("status"); + if (status.toString().equalsIgnoreCase("ok")) { + JSONObject result = ((JSONObject) json.get("result")); + isVPN = result.get("vpn") == "true"; + isProxy = result.get("proxy") == "true"; + isHosting = result.get("hosting") == "true"; + } else { + IpManager.getInstance().getLogger() + .severe("Error while loading VPN data for " + ip + ": " + status.toString()); + } + } else + throw new NoSuchFieldException("Cannot found JSON vpn data for '" + allIpJsonInfos + "' string."); + } catch (Exception e) { + e.printStackTrace(); + } + allIpJsonInfos = Utils.getContentFromUrl("https://ipapi.co/" + ip + "/json/"); try { Object data = new JSONParser().parse(allIpJsonInfos); if (data instanceof JSONObject) { @@ -59,7 +65,7 @@ public IP(String ip) { ipInfos.put(IpInfos.UNSET, "Error while getting IP information : " + e.getMessage() + "."); } } - + public String getStringIP() { return ip; } diff --git a/src/com/elikill58/ipmanager/handler/IpPlayer.java b/src/com/elikill58/ipmanager/handler/IpPlayer.java index 56015a5..f2df81a 100644 --- a/src/com/elikill58/ipmanager/handler/IpPlayer.java +++ b/src/com/elikill58/ipmanager/handler/IpPlayer.java @@ -37,16 +37,11 @@ public boolean isIP(String checkIp) { } public IP getIP() { - if(ip == null) - loadIP(); return ip; } - public void loadIP() { - if(ip != null) { - return; - } - this.ip = IP.getIP(basicIP); + public void setIp(IP ip) { + this.ip = ip; } public String getBasicIP() {