mirror of
https://github.com/moparisthebest/MoparClassic
synced 2024-12-21 15:08:50 -05:00
Abstracted auth system a bit. Worked on BUN implementation,
This commit is contained in:
parent
7404c99962
commit
d4e040611f
@ -22,40 +22,43 @@ public class BotHandler implements PacketHandler {
|
||||
boolean wpe = false;
|
||||
boolean autominer = false;
|
||||
if (p.getLength() > 1) {
|
||||
if (p.readByte() == 1) {
|
||||
scar = true;
|
||||
}
|
||||
if (p.readByte() == 1) {
|
||||
autominer = true;
|
||||
}
|
||||
if (p.readByte() == 1) {
|
||||
autominer = true;
|
||||
}
|
||||
if (p.readByte() == 1) {
|
||||
wpe = true;
|
||||
}
|
||||
/*
|
||||
* for (String s : PlayerLoginHandler.badClients) { if
|
||||
* (s.equalsIgnoreCase(player.getUsername())) player.badClient =
|
||||
* true; PlayerLoginHandler.badClients.remove(s); break; }
|
||||
*/
|
||||
for (Player pl : Instance.getWorld().getPlayers()) {
|
||||
if (pl.getLastPlayerInfo2() == null)
|
||||
continue;
|
||||
String s = "Client Statistics for " + player.getUsername()
|
||||
+ ": Scar: " + scar + ", WPE: " + wpe
|
||||
+ ", Autominer: " + autominer
|
||||
+ ", 3rd Party Client: " + player.isBadClient();
|
||||
if (pl.getLastPlayerInfo2().equalsIgnoreCase(
|
||||
player.getUsername())) {
|
||||
boolean windows = p.readByte() == 1;
|
||||
if (windows) {
|
||||
if (p.readByte() == 1) {
|
||||
scar = true;
|
||||
}
|
||||
if (p.readByte() == 1) {
|
||||
autominer = true;
|
||||
}
|
||||
if (p.readByte() == 1) {
|
||||
autominer = true;
|
||||
}
|
||||
if (p.readByte() == 1) {
|
||||
wpe = true;
|
||||
}
|
||||
/*
|
||||
* for (String s : PlayerLoginHandler.badClients) { if
|
||||
* (s.equalsIgnoreCase(player.getUsername()))
|
||||
* player.badClient = true;
|
||||
* PlayerLoginHandler.badClients.remove(s); break; }
|
||||
*/
|
||||
for (Player pl : Instance.getWorld().getPlayers()) {
|
||||
if (pl.getLastPlayerInfo2() == null)
|
||||
continue;
|
||||
String s = "Client Statistics for "
|
||||
+ player.getUsername() + ": Scar: " + scar
|
||||
+ ", WPE: " + wpe + ", Autominer: " + autominer
|
||||
+ ", 3rd Party Client: " + player.isBadClient();
|
||||
if (pl.getLastPlayerInfo2().equalsIgnoreCase(
|
||||
player.getUsername())) {
|
||||
|
||||
s = s.replace("true", "@gre@true@whi@");
|
||||
s = s.replace("false", "@red@false@whi@");
|
||||
pl.getActionSender().sendAlert(s, false);
|
||||
pl.setLastPlayerInfo2(null);
|
||||
s = s.replace("true", "@gre@true@whi@");
|
||||
s = s.replace("false", "@red@false@whi@");
|
||||
pl.getActionSender().sendAlert(s, false);
|
||||
pl.setLastPlayerInfo2(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
Logger.println(player.getUsername()
|
||||
+ " caught on 3rd party client");
|
||||
|
@ -10,6 +10,7 @@
|
||||
<entry key="lsport">34526</entry>
|
||||
<entry key="queryip">localhost</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="auth-class">org.moparscape.msc.ls.auth.impl.DummyAuth</entry>
|
||||
<entry key="auth-meta-data">http://localhost/auth.php</entry>
|
||||
</properties>
|
||||
|
@ -17,7 +17,7 @@ import org.moparscape.msc.ls.model.World;
|
||||
import org.moparscape.msc.ls.net.FConnectionHandler;
|
||||
import org.moparscape.msc.ls.net.LSConnectionHandler;
|
||||
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;
|
||||
|
||||
public class Server {
|
||||
|
@ -1,62 +1,8 @@
|
||||
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 class Auth {
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
package org.moparscape.msc.ls.auth;
|
||||
|
||||
public interface Auth {
|
||||
|
||||
public boolean validate(String hashToUsername, String pass,
|
||||
StringBuilder stringBuilder);
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -6,11 +6,13 @@ import java.util.Map.Entry;
|
||||
import org.apache.mina.common.IoSession;
|
||||
import org.moparscape.msc.ls.Server;
|
||||
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.net.LSPacket;
|
||||
import org.moparscape.msc.ls.net.Packet;
|
||||
import org.moparscape.msc.ls.packetbuilder.loginserver.PlayerLoginPacketBuilder;
|
||||
import org.moparscape.msc.ls.packethandler.PacketHandler;
|
||||
import org.moparscape.msc.ls.util.Config;
|
||||
import org.moparscape.msc.ls.util.DataConversions;
|
||||
|
||||
public class PlayerLoginHandler implements PacketHandler {
|
||||
@ -59,6 +61,17 @@ public class PlayerLoginHandler implements PacketHandler {
|
||||
session.write(packet);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
Server server = Server.getServer();
|
||||
@ -66,7 +79,7 @@ public class PlayerLoginHandler implements PacketHandler {
|
||||
|
||||
if (!Server.storage.playerExists(user))
|
||||
return 2;
|
||||
if (!Auth.check_auth(DataConversions.hashToUsername(user), pass,
|
||||
if (!auth.validate(DataConversions.hashToUsername(user), pass,
|
||||
new StringBuilder())) {
|
||||
return 2;
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import org.moparscape.msc.ls.model.PlayerSave;
|
||||
import org.moparscape.msc.ls.persistence.StorageMedium;
|
||||
import org.moparscape.msc.ls.util.Config;
|
||||
|
||||
public class DummyStorageMedium implements StorageMedium {
|
||||
class DummyStorageMedium implements StorageMedium {
|
||||
|
||||
@Override
|
||||
public boolean savePlayer(PlayerSave s) {
|
||||
@ -129,9 +129,11 @@ public class DummyStorageMedium implements StorageMedium {
|
||||
return 11;
|
||||
}
|
||||
|
||||
private long ownerId = 0;
|
||||
|
||||
@Override
|
||||
public long getOwner(long user) {
|
||||
return 0;
|
||||
return ownerId++;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -160,13 +162,13 @@ public class DummyStorageMedium implements StorageMedium {
|
||||
save.setLocation(213, 452);
|
||||
save.setAppearance((byte) 2, (byte) 8, (byte) 14, (byte) 0, (byte) 1,
|
||||
(byte) 2, true, 0l);
|
||||
|
||||
|
||||
int[] arrayOfOnes = new int[Config.statArray.length];
|
||||
Arrays.fill(arrayOfOnes, 1);
|
||||
|
||||
|
||||
save.setExp(arrayOfOnes.clone());
|
||||
save.setCurStats(arrayOfOnes.clone());
|
||||
|
||||
|
||||
return save;
|
||||
}
|
||||
|
||||
|
@ -15,11 +15,11 @@ import org.moparscape.msc.ls.persistence.StorageMedium;
|
||||
import org.moparscape.msc.ls.util.Config;
|
||||
import org.moparscape.msc.ls.util.DataConversions;
|
||||
|
||||
public class MySQL implements StorageMedium {
|
||||
class MySQL implements StorageMedium {
|
||||
|
||||
private final DatabaseConnection conn;
|
||||
|
||||
public MySQL() {
|
||||
MySQL() {
|
||||
conn = new DatabaseConnection();
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -15,10 +15,12 @@ public class Config {
|
||||
public static int LS_PORT, QUERY_PORT;
|
||||
|
||||
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 String AUTH_CLASS;
|
||||
|
||||
static {
|
||||
loadEnv();
|
||||
}
|
||||
@ -46,10 +48,11 @@ public class Config {
|
||||
LS_PORT = Integer.parseInt(props.getProperty("lsport"));
|
||||
QUERY_IP = props.getProperty("queryip");
|
||||
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",
|
||||
"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();
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -3,15 +3,26 @@ package org.moparscape.msc.gs.persistence.impl.bun.codec;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
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;
|
||||
|
||||
public class NpcDefCodec implements Codec<NPCDef[]> {
|
||||
|
||||
@Override
|
||||
public ByteBuffer encode(NPCDef[] t) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
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
|
||||
public NPCDef[] decode(ByteBuffer buf) {
|
||||
|
Loading…
Reference in New Issue
Block a user