Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible checking #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mcbans.syamn.bungee</groupId>
<artifactId>MCBansProxy</artifactId>
<version>1.3-SNAPSHOT</version>
<name>MCBansProxy</name>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>sonatype-oss-public</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-proxy</artifactId>
<version>1.5-SNAPSHOT</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct version?
https://oss.sonatype.org/content/groups/public/net/md-5/bungeecord-proxy/
I couldn't find this in their repository :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. Seems like I've mixed this with my local repo. Furthermore they've did some more changes lately so you only need the API dependency. I'll check if we can now use that one finally at version 1.5-SNAPSHOT

</dependency>
</dependencies>
</project>
Binary file added releases/MCBansProxy-1.3-SNAPSHOT.jar
Binary file not shown.
131 changes: 24 additions & 107 deletions src/com/mcbans/syamn/bungee/LoginEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}


}
35 changes: 35 additions & 0 deletions src/com/mcbans/syamn/bungee/MCBansChecker.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

}

}
Loading