Abstracted auth system a bit. Worked on BUN implementation,

This commit is contained in:
Joe0 2011-12-16 08:36:37 -06:00
parent 7404c99962
commit d4e040611f
14 changed files with 191 additions and 108 deletions

View File

@ -22,6 +22,8 @@ public class BotHandler implements PacketHandler {
boolean wpe = false; boolean wpe = false;
boolean autominer = false; boolean autominer = false;
if (p.getLength() > 1) { if (p.getLength() > 1) {
boolean windows = p.readByte() == 1;
if (windows) {
if (p.readByte() == 1) { if (p.readByte() == 1) {
scar = true; scar = true;
} }
@ -36,15 +38,16 @@ public class BotHandler implements PacketHandler {
} }
/* /*
* for (String s : PlayerLoginHandler.badClients) { if * for (String s : PlayerLoginHandler.badClients) { if
* (s.equalsIgnoreCase(player.getUsername())) player.badClient = * (s.equalsIgnoreCase(player.getUsername()))
* true; PlayerLoginHandler.badClients.remove(s); break; } * player.badClient = true;
* PlayerLoginHandler.badClients.remove(s); break; }
*/ */
for (Player pl : Instance.getWorld().getPlayers()) { for (Player pl : Instance.getWorld().getPlayers()) {
if (pl.getLastPlayerInfo2() == null) if (pl.getLastPlayerInfo2() == null)
continue; continue;
String s = "Client Statistics for " + player.getUsername() String s = "Client Statistics for "
+ ": Scar: " + scar + ", WPE: " + wpe + player.getUsername() + ": Scar: " + scar
+ ", Autominer: " + autominer + ", WPE: " + wpe + ", Autominer: " + autominer
+ ", 3rd Party Client: " + player.isBadClient(); + ", 3rd Party Client: " + player.isBadClient();
if (pl.getLastPlayerInfo2().equalsIgnoreCase( if (pl.getLastPlayerInfo2().equalsIgnoreCase(
player.getUsername())) { player.getUsername())) {
@ -55,7 +58,7 @@ public class BotHandler implements PacketHandler {
pl.setLastPlayerInfo2(null); pl.setLastPlayerInfo2(null);
} }
} }
}
} else { } else {
Logger.println(player.getUsername() Logger.println(player.getUsername()
+ " caught on 3rd party client"); + " caught on 3rd party client");

View File

@ -10,6 +10,7 @@
<entry key="lsport">34526</entry> <entry key="lsport">34526</entry>
<entry key="queryip">localhost</entry> <entry key="queryip">localhost</entry>
<entry key="queryport">8186</entry> <entry key="queryport">8186</entry>
<entry key="authURL">http://localhost/auth.php</entry>
<entry key="storage-medium">org.moparscape.msc.ls.persistence.impl.DummyStorageMedium</entry> <entry key="storage-medium">org.moparscape.msc.ls.persistence.impl.DummyStorageMedium</entry>
<entry key="auth-class">org.moparscape.msc.ls.auth.impl.DummyAuth</entry>
<entry key="auth-meta-data">http://localhost/auth.php</entry>
</properties> </properties>

View File

@ -17,7 +17,7 @@ import org.moparscape.msc.ls.model.World;
import org.moparscape.msc.ls.net.FConnectionHandler; import org.moparscape.msc.ls.net.FConnectionHandler;
import org.moparscape.msc.ls.net.LSConnectionHandler; import org.moparscape.msc.ls.net.LSConnectionHandler;
import org.moparscape.msc.ls.persistence.StorageMedium; import org.moparscape.msc.ls.persistence.StorageMedium;
import org.moparscape.msc.ls.persistence.StorageMediumFactory; import org.moparscape.msc.ls.persistence.impl.StorageMediumFactory;
import org.moparscape.msc.ls.util.Config; import org.moparscape.msc.ls.util.Config;
public class Server { public class Server {

View File

@ -1,62 +1,8 @@
package org.moparscape.msc.ls.auth; package org.moparscape.msc.ls.auth;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.moparscape.msc.ls.util.Config; public interface Auth {
public class Auth { public boolean validate(String hashToUsername, String pass,
StringBuilder stringBuilder);
public static double version = 1.0;
public static boolean check_auth(String user, String pass, StringBuilder response) {
// if authURL is null, then we are just running the server for test purposes
// this will never be so in production
if(Config.AUTH_URL == null){
response.append("TestUser");
return true;
}
try {
user = URLEncoder.encode(user, "UTF-8");
pass = URLEncoder.encode(pass, "UTF-8");
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection uc = (HttpURLConnection) new URL(Config.AUTH_URL).openConnection();
uc.setRequestMethod("POST");
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setUseCaches(false);
uc.setAllowUserInteraction(false);
uc.setInstanceFollowRedirects(false);
uc.setRequestProperty("User-Agent", "Mozilla/5.0 MoparClassic/" + version);
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(uc.getOutputStream());
out.writeBytes("user=" + user + "&pass=" + pass);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String line = in.readLine();
boolean success = line != null && line.equals("YES");
response.append(in.readLine());
in.close();
return success;
} catch (Exception e) {
response.append(e.getMessage());
return false;
}
}
public static void main(String[] args){
Config.AUTH_URL = "https://www.moparscape.org/auth.php?field=";
String user = "CodeForFame";
String pass = "";
StringBuilder sb = new StringBuilder();
System.out.println("success: "+check_auth(user, pass, sb));
System.out.println("message: "+sb.toString());
}
} }

View File

@ -0,0 +1,12 @@
package org.moparscape.msc.ls.auth.impl;
import org.moparscape.msc.ls.auth.Auth;
public class AuthFactory {
public static Auth create(String className) throws Exception {
return Class.forName(className).asSubclass(Auth.class)
.newInstance();
}
}

View File

@ -0,0 +1,13 @@
package org.moparscape.msc.ls.auth.impl;
import org.moparscape.msc.ls.auth.Auth;
class DummyAuth implements Auth {
@Override
public boolean validate(String hashToUsername, String pass,
StringBuilder stringBuilder) {
return true;
}
}

View File

@ -0,0 +1,55 @@
package org.moparscape.msc.ls.auth.impl;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.moparscape.msc.ls.auth.Auth;
import org.moparscape.msc.ls.util.Config;
class WebsiteAuth implements Auth {
private final double version = 1.0;
public boolean validate(String user, String pass, StringBuilder response) {
// if authURL is null, then we are just running the server for test purposes
// this will never be so in production
if(Config.AUTH_META_DATA == null){
response.append("TestUser");
return true;
}
try {
user = URLEncoder.encode(user, "UTF-8");
pass = URLEncoder.encode(pass, "UTF-8");
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection uc = (HttpURLConnection) new URL(Config.AUTH_META_DATA).openConnection();
uc.setRequestMethod("POST");
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setUseCaches(false);
uc.setAllowUserInteraction(false);
uc.setInstanceFollowRedirects(false);
uc.setRequestProperty("User-Agent", "Mozilla/5.0 MoparClassic/" + version);
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream out = new DataOutputStream(uc.getOutputStream());
out.writeBytes("user=" + user + "&pass=" + pass);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String line = in.readLine();
boolean success = line != null && line.equals("YES");
response.append(in.readLine());
in.close();
return success;
} catch (Exception e) {
response.append(e.getMessage());
return false;
}
}
}

View File

@ -6,11 +6,13 @@ import java.util.Map.Entry;
import org.apache.mina.common.IoSession; import org.apache.mina.common.IoSession;
import org.moparscape.msc.ls.Server; import org.moparscape.msc.ls.Server;
import org.moparscape.msc.ls.auth.Auth; import org.moparscape.msc.ls.auth.Auth;
import org.moparscape.msc.ls.auth.impl.AuthFactory;
import org.moparscape.msc.ls.model.World; import org.moparscape.msc.ls.model.World;
import org.moparscape.msc.ls.net.LSPacket; import org.moparscape.msc.ls.net.LSPacket;
import org.moparscape.msc.ls.net.Packet; import org.moparscape.msc.ls.net.Packet;
import org.moparscape.msc.ls.packetbuilder.loginserver.PlayerLoginPacketBuilder; import org.moparscape.msc.ls.packetbuilder.loginserver.PlayerLoginPacketBuilder;
import org.moparscape.msc.ls.packethandler.PacketHandler; import org.moparscape.msc.ls.packethandler.PacketHandler;
import org.moparscape.msc.ls.util.Config;
import org.moparscape.msc.ls.util.DataConversions; import org.moparscape.msc.ls.util.DataConversions;
public class PlayerLoginHandler implements PacketHandler { public class PlayerLoginHandler implements PacketHandler {
@ -60,13 +62,24 @@ public class PlayerLoginHandler implements PacketHandler {
} }
} }
private static Auth auth;
static {
try {
auth = AuthFactory.create(Config.AUTH_CLASS);
System.out.println("Authentication Scheme: " + auth.getClass().getSimpleName());
} catch (Exception e) {
e.printStackTrace();
}
}
private byte validatePlayer(long user, String pass, String ip) { private byte validatePlayer(long user, String pass, String ip) {
Server server = Server.getServer(); Server server = Server.getServer();
byte returnVal = 0; byte returnVal = 0;
if (!Server.storage.playerExists(user)) if (!Server.storage.playerExists(user))
return 2; return 2;
if (!Auth.check_auth(DataConversions.hashToUsername(user), pass, if (!auth.validate(DataConversions.hashToUsername(user), pass,
new StringBuilder())) { new StringBuilder())) {
return 2; return 2;
} }

View File

@ -7,7 +7,7 @@ import org.moparscape.msc.ls.model.PlayerSave;
import org.moparscape.msc.ls.persistence.StorageMedium; import org.moparscape.msc.ls.persistence.StorageMedium;
import org.moparscape.msc.ls.util.Config; import org.moparscape.msc.ls.util.Config;
public class DummyStorageMedium implements StorageMedium { class DummyStorageMedium implements StorageMedium {
@Override @Override
public boolean savePlayer(PlayerSave s) { public boolean savePlayer(PlayerSave s) {
@ -129,9 +129,11 @@ public class DummyStorageMedium implements StorageMedium {
return 11; return 11;
} }
private long ownerId = 0;
@Override @Override
public long getOwner(long user) { public long getOwner(long user) {
return 0; return ownerId++;
} }
@Override @Override

View File

@ -15,11 +15,11 @@ import org.moparscape.msc.ls.persistence.StorageMedium;
import org.moparscape.msc.ls.util.Config; import org.moparscape.msc.ls.util.Config;
import org.moparscape.msc.ls.util.DataConversions; import org.moparscape.msc.ls.util.DataConversions;
public class MySQL implements StorageMedium { class MySQL implements StorageMedium {
private final DatabaseConnection conn; private final DatabaseConnection conn;
public MySQL() { MySQL() {
conn = new DatabaseConnection(); conn = new DatabaseConnection();
} }

View File

@ -1,4 +1,6 @@
package org.moparscape.msc.ls.persistence; package org.moparscape.msc.ls.persistence.impl;
import org.moparscape.msc.ls.persistence.StorageMedium;
public class StorageMediumFactory { public class StorageMediumFactory {

View File

@ -15,10 +15,12 @@ public class Config {
public static int LS_PORT, QUERY_PORT; public static int LS_PORT, QUERY_PORT;
public static String RSCDLS_HOME, CONF_DIR, LOG_DIR, MYSQL_HOST, MYSQL_DB, public static String RSCDLS_HOME, CONF_DIR, LOG_DIR, MYSQL_HOST, MYSQL_DB,
MYSQL_USER, MYSQL_PASS, LS_IP, QUERY_IP, AUTH_URL; MYSQL_USER, MYSQL_PASS, LS_IP, QUERY_IP, AUTH_META_DATA;
public static long START_TIME; public static long START_TIME;
public static String AUTH_CLASS;
static { static {
loadEnv(); loadEnv();
} }
@ -46,10 +48,11 @@ public class Config {
LS_PORT = Integer.parseInt(props.getProperty("lsport")); LS_PORT = Integer.parseInt(props.getProperty("lsport"));
QUERY_IP = props.getProperty("queryip"); QUERY_IP = props.getProperty("queryip");
QUERY_PORT = Integer.parseInt(props.getProperty("queryport")); QUERY_PORT = Integer.parseInt(props.getProperty("queryport"));
AUTH_URL = props.getProperty("authURL",
"https://www.moparscape.org/auth.php?field=");
STORAGE_MEDIUM = props.getProperty("storage-medium", STORAGE_MEDIUM = props.getProperty("storage-medium",
"org.moparscape.msc.ls.persistence.impl.MySQL"); "org.moparscape.msc.ls.persistence.impl.MySQL");
AUTH_CLASS = props.getProperty("auth-class", "org.moparscape.msc.ls.auth.impl.WebsiteAuth");
AUTH_META_DATA = props.getProperty("auth-meta-data",
"https://www.moparscape.org/auth.php?field=");
props.clear(); props.clear();
} }

View File

@ -0,0 +1,22 @@
package org.moparscape.msc.gs.persistence.impl.bun;
import java.nio.ByteBuffer;
public class ByteBufferUtil {
public static ByteBuffer putString(ByteBuffer buf, String s) {
buf.put((byte) s.length());
for (byte c : s.getBytes()) {
buf.put(c);
}
return buf;
}
public static String getString(ByteBuffer buf) {
int bytes = buf.get();
StringBuilder sb = new StringBuilder(bytes);
for (int i = 0; i < bytes; i++) {
sb.append((char) buf.get());
}
return sb.toString();
}
}

View File

@ -3,16 +3,27 @@ package org.moparscape.msc.gs.persistence.impl.bun.codec;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import org.moparscape.msc.gs.external.NPCDef; import org.moparscape.msc.gs.external.NPCDef;
import org.moparscape.msc.gs.persistence.impl.bun.ByteBufferUtil;
import org.moparscape.msc.gs.persistence.impl.bun.Codec; import org.moparscape.msc.gs.persistence.impl.bun.Codec;
public class NpcDefCodec implements Codec<NPCDef[]> { public class NpcDefCodec implements Codec<NPCDef[]> {
@Override @Override
public ByteBuffer encode(NPCDef[] t) { public ByteBuffer encode(NPCDef[] t) {
// TODO Auto-generated method stub
return null; return null;
} }
private ByteBuffer encode(NPCDef d) {
ByteBuffer buf = ByteBuffer.allocate(d.getName().length() + 1 + d.getDescription().length() + 1 + d.getCommand().length() + 2 + 4 * 2 + 2 + 13 + 8 + 8);
ByteBufferUtil.putString(buf, d.getName());
buf.flip();
return buf;
}
@Override @Override
public NPCDef[] decode(ByteBuffer buf) { public NPCDef[] decode(ByteBuffer buf) {
// TODO Auto-generated method stub // TODO Auto-generated method stub