diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e4030b8 --- /dev/null +++ b/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + com.mcbans.syamn.bungee + MCBansProxy + 1.3-SNAPSHOT + MCBansProxy + + src + + + src + + **/*.java + + + + + + maven-compiler-plugin + 3.0 + + 1.7 + 1.7 + + + + + + + + sonatype-oss-public + https://oss.sonatype.org/content/groups/public/ + + true + + + true + + + + + + + net.md-5 + bungeecord-api + 1.5-SNAPSHOT + + + diff --git a/releases/MCBansProxy-1.3-SNAPSHOT.jar b/releases/MCBansProxy-1.3-SNAPSHOT.jar new file mode 100644 index 0000000..4ee1b78 Binary files /dev/null and b/releases/MCBansProxy-1.3-SNAPSHOT.jar differ diff --git a/src/com/mcbans/syamn/bungee/LoginEventHandler.java b/src/com/mcbans/syamn/bungee/LoginEventHandler.java index 0698fe4..89662ee 100644 --- a/src/com/mcbans/syamn/bungee/LoginEventHandler.java +++ b/src/com/mcbans/syamn/bungee/LoginEventHandler.java @@ -4,16 +4,10 @@ */ package com.mcbans.syamn.bungee; -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.SocketTimeoutException; -import java.net.URL; -import java.net.URLConnection; -import java.net.URLEncoder; - import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.PendingConnection; import net.md_5.bungee.api.event.LoginEvent; +import net.md_5.bungee.api.event.ServerConnectEvent; import net.md_5.bungee.api.plugin.Listener; import com.google.common.eventbus.Subscribe; @@ -22,117 +16,40 @@ * LoginEventHandler (LoginEventHandler.java) */ public class LoginEventHandler implements Listener{ - private static final String logPrefix = MCBansProxy.logPrefix; private MCBansProxy plugin; LoginEventHandler(final MCBansProxy plugin){ this.plugin = plugin; } + @Subscribe + public void onServerConnect(ServerConnectEvent event) { + System.out.println("ServerConnectEvent: " + event.getPlayer().getName() + " -> " + event.getTarget().getName()); + if (plugin.checkForServersOnly == null) return; + + String remoteServerName = event.getTarget().getName(); + if (plugin.checkForServersOnly.contains(remoteServerName)) { + String playerName = event.getPlayer().getName(); + String playerHost = event.getPlayer().getAddress().getHostName(); + + ProxyServer.getInstance().getLogger().info(MCBansProxy.logPrefix + "ServerConnectEvent @ " + remoteServerName + " - Checking login for: " + playerName + " (" + playerHost + ")"); + new MCBansChecker(playerName, playerHost).start(); + } + } + @Subscribe public void onLogin(final LoginEvent event){ + if (plugin.checkForServersOnly != null) return; + final PendingConnection pc = event.getConnection(); if (event.isCancelled() || pc == null) return; - if (!plugin.isValidKey){ - ProxyServer.getInstance().getLogger().warning("Missing or invalid API Key! Please check config.yml and restart proxy!"); - return; - } - - try{ - final String uriStr = "http://api.mcbans.com/v2/" + plugin.apiKey + "/login/" - + URLEncoder.encode(pc.getName(), "UTF-8") + "/" - + URLEncoder.encode(String.valueOf(pc.getAddress().getHostName()), "UTF-8"); - final URLConnection conn = new URL(uriStr).openConnection(); - conn.setConnectTimeout(plugin.timeout * 1000); - conn.setReadTimeout(plugin.timeout * 1000); - conn.setUseCaches(false); - - BufferedReader br = null; - String response = null; - try{ - br = new BufferedReader(new InputStreamReader(conn.getInputStream())); - response = br.readLine(); - }finally{ - if (br != null) br.close(); - } - if (response == null){ - if (plugin.failsafe){ - ProxyServer.getInstance().getLogger().info("Null response! Kicked player: " + pc.getName()); - event.setCancelled(true); - event.setCancelReason("MCBans service unavailable!"); - }else{ - ProxyServer.getInstance().getLogger().info(logPrefix + "Null response! Check passed player: " + pc.getName()); - } - return; - } - - plugin.debug("Response: " + response); - String[] s = response.split(";"); - if (s.length == 6 || s.length == 7) { - // check banned - if (s[0].equals("l") || s[0].equals("g") || s[0].equals("t") || s[0].equals("i") || s[0].equals("s")) { - event.setCancelled(true); - event.setCancelReason(s[1]); - return; - } - // check reputation - else if (plugin.minRep > Double.valueOf(s[2])) { - event.setCancelled(true); - event.setCancelReason(plugin.minRepMsg); - return; - } - // check alternate accounts - else if (plugin.enableMaxAlts && plugin.maxAlts < Integer.valueOf(s[3])) { - event.setCancelled(true); - event.setCancelReason(plugin.maxAltsMsg); - return; - } - // check passed, put data to playerCache - else{ - if(s[0].equals("b")){ - ProxyServer.getInstance().getLogger().info(logPrefix + pc.getName() + " has previous ban(s)!"); - } - if(Integer.parseInt(s[3])>0){ - ProxyServer.getInstance().getLogger().info(logPrefix + pc.getName() + " may has " + s[3] + " alt account(s)![" + s[6] + "]"); - } - if(s[4].equals("y")){ - ProxyServer.getInstance().getLogger().info(logPrefix + pc.getName() + " is an MCBans.com Staff Member!"); - } - if(Integer.parseInt(s[5])>0){ - ProxyServer.getInstance().getLogger().info(logPrefix + s[5] + " open dispute(s)!"); - } - } - plugin.debug(pc.getName() + " authenticated with " + s[2] + " rep"); - }else{ - if (response.toString().contains("Server Disabled")) { - ProxyServer.getInstance().getLogger().info(logPrefix + "This Server Disabled by MCBans Administration!"); - return; - } - if (plugin.failsafe){ - ProxyServer.getInstance().getLogger().info(logPrefix + "Null response! Kicked player: " + pc.getName()); - event.setCancelled(true); - event.setCancelReason(plugin.unavailable); - }else{ - ProxyServer.getInstance().getLogger().info(logPrefix + "Invalid response!(" + s.length + ") Check passed player: " + pc.getName()); - } - ProxyServer.getInstance().getLogger().info(logPrefix + "Response: " + response); - return; - } - }catch (SocketTimeoutException ex){ - ProxyServer.getInstance().getLogger().info(logPrefix + "Cannot connect MCBans API server: timeout"); - if (plugin.failsafe){ - event.setCancelled(true); - event.setCancelReason(plugin.unavailable); - } - }catch (Exception ex){ - ProxyServer.getInstance().getLogger().info(logPrefix + "Cannot connect MCBans API server!"); - if (plugin.failsafe){ - event.setCancelled(true); - event.setCancelReason(plugin.unavailable); - } - if (plugin.isDebug) ex.printStackTrace(); - } + String playerName = pc.getName(); + String playerHost = pc.getAddress().getHostName(); + + ProxyServer.getInstance().getLogger().info(MCBansProxy.logPrefix + "GlobalConnectEvent - Checking login for: " + playerName + " (" + playerHost + ")"); + new MCBansChecker(playerName, playerHost).start(); } + } diff --git a/src/com/mcbans/syamn/bungee/MCBansChecker.java b/src/com/mcbans/syamn/bungee/MCBansChecker.java new file mode 100644 index 0000000..2dffda2 --- /dev/null +++ b/src/com/mcbans/syamn/bungee/MCBansChecker.java @@ -0,0 +1,35 @@ +package com.mcbans.syamn.bungee; + +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +public class MCBansChecker extends Thread { + + private String checkPlayerName; + private String checkHostName; + + MCBansChecker(String checkPlayerName, String checkHostName) { + this.checkPlayerName = checkPlayerName; + this.checkHostName = checkHostName; + } + + @Override + public void run() { + if (checkPlayerName == null || checkHostName == null) { + System.out.println("MCBansChecker needs to be innitialized with a playername and hostname"); + return; + } + + String cancelReason = MCBansProxy.checkMCBansForPlayer(checkPlayerName, checkPlayerName); + System.out.println("ServerConnectEvent - cancelreason: " + cancelReason); + if (cancelReason != null) { + ProxiedPlayer player = ProxyServer.getInstance().getPlayer(checkPlayerName); + if (player != null) { + player.disconnect(cancelReason); + } + + } + + } + +} \ No newline at end of file diff --git a/src/com/mcbans/syamn/bungee/MCBansProxy.java b/src/com/mcbans/syamn/bungee/MCBansProxy.java index 455ed39..65635c8 100644 --- a/src/com/mcbans/syamn/bungee/MCBansProxy.java +++ b/src/com/mcbans/syamn/bungee/MCBansProxy.java @@ -4,12 +4,17 @@ */ package com.mcbans.syamn.bungee; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.SocketTimeoutException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; +import java.util.List; + import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.plugin.Plugin; -//import net.md_5.bungee.plugin.JavaPlugin; -//import net.md_5.bungee.plugin.LoginEvent; - /** * MCBansProxy (MCBansProxy.java) */ @@ -23,12 +28,19 @@ public class MCBansProxy extends Plugin{ String apiKey, minRepMsg, maxAltsMsg, unavailable; int minRep, maxAlts, timeout; boolean failsafe, isDebug, enableMaxAlts; + List checkForServersOnly; + + public static MCBansProxy plugin; @Override public void onEnable(){ + MCBansProxy.plugin = this; + confManager = new MCBansConfiguration(this); confManager.loadConfig(); + getConfigs(); + ProxyServer.getInstance().getPluginManager().registerListener(this,new LoginEventHandler(this)); ProxyServer.getInstance().getLogger().info(logPrefix + "MCBansProxy plugin enabled!"); } @@ -44,6 +56,7 @@ private void getConfigs(){ unavailable = confManager.get("unavailableMessage", "Unavailable MCBans Service! Please try again later!"); isDebug = confManager.get("isDebug", false); failsafe = confManager.get("failsafe", false); + checkForServersOnly = confManager.get("checkForServersOnly", null); // check API key if (apiKey.length() != 40){ @@ -54,10 +67,118 @@ private void getConfigs(){ } } - void debug(final String msg){ - if (isDebug){ + static void debug(final String msg){ + if (MCBansProxy.plugin.isDebug){ ProxyServer.getInstance().getLogger().info(logPrefix + "[DEBUG] " + msg); } } + + public static String checkMCBansForPlayer(String playername, String playerHostname) { + boolean isValidKey = MCBansProxy.plugin.isValidKey; + boolean failsafe = MCBansProxy.plugin.failsafe; + boolean enableMaxAlts = MCBansProxy.plugin.enableMaxAlts; + boolean isDebug = MCBansProxy.plugin.isDebug; + + int timeout = MCBansProxy.plugin.timeout; + int minRep = MCBansProxy.plugin.minRep; + int maxAlts = MCBansProxy.plugin.maxAlts; + + String apiKey = MCBansProxy.plugin.apiKey; + String minRepMsg = MCBansProxy.plugin.minRepMsg; + String maxAltsMsg = MCBansProxy.plugin.maxAltsMsg; + String unavailable = MCBansProxy.plugin.unavailable; + + if (!isValidKey){ + ProxyServer.getInstance().getLogger().warning("Missing or invalid API Key! Please check config.yml and restart proxy!"); + return "Server temporarily unavailable. Contact the server administrator."; + } + + try{ + final String uriStr = "http://api.mcbans.com/v2/" + apiKey + "/login/" + + URLEncoder.encode(playername, "UTF-8") + "/" + + URLEncoder.encode(String.valueOf(playerHostname), "UTF-8"); + final URLConnection conn = new URL(uriStr).openConnection(); + conn.setConnectTimeout(timeout * 1000); + conn.setReadTimeout(timeout * 1000); + conn.setUseCaches(false); + + BufferedReader br = null; + String response = null; + try{ + br = new BufferedReader(new InputStreamReader(conn.getInputStream())); + response = br.readLine(); + }finally{ + if (br != null) br.close(); + } + if (response == null){ + if (failsafe){ + ProxyServer.getInstance().getLogger().info("Null response! Kicked player: " + playername); + return "MCBans service unavailable!"; + }else{ + ProxyServer.getInstance().getLogger().info(logPrefix + "Null response! Check passed player: " + playername); + return null; + } + } + + debug("Response: " + response); + String[] s = response.split(";"); + if (s.length == 6 || s.length == 7) { + // check banned + if (s[0].equals("l") || s[0].equals("g") || s[0].equals("t") || s[0].equals("i") || s[0].equals("s")) { + return s[1]; + } + // check reputation + else if (minRep > Double.valueOf(s[2])) { + return minRepMsg; + } + // check alternate accounts + else if (enableMaxAlts && maxAlts < Integer.valueOf(s[3])) { + return maxAltsMsg; + } + // check passed, put data to playerCache + else{ + if(s[0].equals("b")){ + ProxyServer.getInstance().getLogger().info(logPrefix + playername + " has previous ban(s)!"); + } + if(Integer.parseInt(s[3])>0){ + ProxyServer.getInstance().getLogger().info(logPrefix + playername + " may has " + s[3] + " alt account(s)![" + s[6] + "]"); + } + if(s[4].equals("y")){ + ProxyServer.getInstance().getLogger().info(logPrefix + playername + " is an MCBans.com Staff Member!"); + } + if(Integer.parseInt(s[5])>0){ + ProxyServer.getInstance().getLogger().info(logPrefix + s[5] + " open dispute(s)!"); + } + } + debug(playername + " authenticated with " + s[2] + " rep"); + }else{ + if (response.toString().contains("Server Disabled")) { + ProxyServer.getInstance().getLogger().info(logPrefix + "This Server was disabled by MCBans Administration!"); + return null; + } + if (failsafe){ + ProxyServer.getInstance().getLogger().info(logPrefix + "Null response! Kicked player: " + playername); + return unavailable; + }else{ + ProxyServer.getInstance().getLogger().info(logPrefix + "Invalid response!(" + s.length + ") Check passed player: " + playername); + } + ProxyServer.getInstance().getLogger().info(logPrefix + "Response: " + response); + return null; + } + }catch (SocketTimeoutException ex){ + ProxyServer.getInstance().getLogger().info(logPrefix + "Cannot connect MCBans API server: timeout"); + if (failsafe){ + return unavailable; + } + }catch (Exception ex){ + ProxyServer.getInstance().getLogger().info(logPrefix + "Cannot connect MCBans API server!"); + if (failsafe){ + return unavailable; + } + if (isDebug) ex.printStackTrace(); + } + + return null; + } } diff --git a/src/config.yml b/src/config.yml index a74550f..f6cd1b9 100644 --- a/src/config.yml +++ b/src/config.yml @@ -53,4 +53,13 @@ failsafe: false # isDebug: # Enable additional debug messages throughout the plugin # DEFAULT: false -isDebug: false \ No newline at end of file +isDebug: false + +# checkForServersOnly: +# Uncommenting this list and adding the names of servers behind the bungeecord proxy +# disables the global login check and just checks when the user tries to connect to one +# of the specified servers and kicks them if needed. +# DEFAULT: commented out (disabled) +# checkForServersOnly: +# - someRemoteServer +# - someOtherRemoteServer