Skip to content

Commit

Permalink
Fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Elikill58 committed Jun 23, 2021
1 parent 65cadda commit 86803d1
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 39 deletions.
39 changes: 39 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -58,13 +78,32 @@ 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!"
precise: "&cPrecise the player name or the player UUID"
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"
Expand Down
55 changes: 50 additions & 5 deletions src/com/elikill58/ipmanager/ConnectionEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<IpPlayer> 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<IpPlayer> playersOnIp = IpPlayer.getPlayersOnIP(ipPlayer.getBasicIP());
ConfigurationSection playerSection = getConfigForAmountPlayer(pl.getConfig().getConfigurationSection("ip-notify"), playersOnIp.size());
if(playerSection == null)
return;
Expand All @@ -111,6 +155,7 @@ public void onJoin(PlayerJoinEvent e) {
});
}
});

}

@EventHandler(priority = EventPriority.LOWEST)
Expand Down
3 changes: 2 additions & 1 deletion src/com/elikill58/ipmanager/IpManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
Expand Down
60 changes: 60 additions & 0 deletions src/com/elikill58/ipmanager/ban/Ban.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
31 changes: 31 additions & 0 deletions src/com/elikill58/ipmanager/ban/BanManager.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
22 changes: 22 additions & 0 deletions src/com/elikill58/ipmanager/ban/BanProcessor.java
Original file line number Diff line number Diff line change
@@ -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<Ban> call;

private BanProcessor(Consumer<Ban> call) {
this.call = call;
}

public void run(Ban ban) {
call.accept(ban);
}
}
58 changes: 32 additions & 26 deletions src/com/elikill58/ipmanager/handler/IP.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,40 @@ public class IP {
private String allIpJsonInfos;
private HashMap<IpInfos, String> 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) {
Expand All @@ -59,7 +65,7 @@ public IP(String ip) {
ipInfos.put(IpInfos.UNSET, "Error while getting IP information : " + e.getMessage() + ".");
}
}

public String getStringIP() {
return ip;
}
Expand Down
9 changes: 2 additions & 7 deletions src/com/elikill58/ipmanager/handler/IpPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 86803d1

Please sign in to comment.