Allow local/LAN/WAN login-phase connections

Allow clients connecting from different IP domains (local/LAN/WAN) to get past login phase and clean unnecessary local server check.
This commit is contained in:
ronancpl
2021-04-09 20:54:53 -03:00
parent fa992da2eb
commit 7a3d5d2b94
13 changed files with 66 additions and 26 deletions

View File

@@ -199,8 +199,9 @@ server:
LOGIN_ATTEMPT_DURATION: 120 #Period in seconds the login attempt remains registered on the system.
#Ip Configuration
HOST: 127.0.0.1
LOCALSERVER: true
HOST: 127.0.0.1 #WAN IPv4 address
LANHOST: 127.0.0.1 #LAN IPv4 address
LOCALHOST: 127.0.0.1 #Loopback IPv4 address
GMSERVER: false
#Other configuration

View File

@@ -1341,7 +1341,7 @@ public class MapleClient {
return;
}
String[] socket = Server.getInstance().getInetSocket(getWorld(), channel);
String[] socket = Server.getInstance().getInetSocket(this.getSession(), getWorld(), channel);
if(socket == null) {
announce(MaplePacketCreator.serverNotice(1, "Channel " + channel + " is currently disabled. Try another channel."));
announce(MaplePacketCreator.enableActions());

View File

@@ -44,7 +44,8 @@ public class ServerConfig {
//Ip Configuration
public String HOST;
public boolean LOCALSERVER;
public String LANHOST;
public String LOCALHOST;
public boolean GMSERVER;
//Other configuration

View File

@@ -103,6 +103,8 @@ public class MapleServerHandler extends IoHandlerAdapter {
if (remoteHost == null) {
remoteHost = "null";
} else {
remoteHost = MapleSessionCoordinator.fetchRemoteAddress(remoteHost); // thanks dyz for noticing Local/LAN/WAN connections not interacting properly
}
} catch (NullPointerException npe) { // thanks Agassy, Alchemist for pointing out possibility of remoteHost = null.
remoteHost = "null";

View File

@@ -286,8 +286,18 @@ public class Server {
}
public String[] getInetSocket(int world, int channel) {
public String[] getInetSocket(IoSession session, int world, int channel) {
String remoteIp = MapleSessionCoordinator.getSessionRemoteAddress(session);
String[] hostAddress = getIP(world, channel).split(":");
if (MapleSessionCoordinator.isLocalAddress(remoteIp)) {
hostAddress[0] = YamlConfig.config.server.LOCALHOST;
} else if (MapleSessionCoordinator.isLanAddress(remoteIp)) {
hostAddress[0] = YamlConfig.config.server.LANHOST;
}
try {
return getIP(world, channel).split(":");
return hostAddress;
} catch (Exception e) {
return null;
}

View File

@@ -206,7 +206,47 @@ public class MapleSessionCoordinator {
private Lock getCoodinatorLock(String remoteHost) {
return poolLock.get(Math.abs(remoteHost.hashCode()) % 100);
}
private static List<Pattern> localComms = loadLocalCommPatterns();
private static List<Pattern> loadLocalCommPatterns() {
String[] localComms = {"10\\.", "192\\.168\\.", "172\\.(1[6-9]|2[0-9]|3[0-1])\\."};
List<Pattern> llc = new ArrayList<>(localComms.length);
for (String lc : localComms) {
llc.add(Pattern.compile(lc));
}
return llc;
}
private static boolean matchesLanAddress(Pattern inetPattern, String inetAddress) {
Matcher searchM = inetPattern.matcher(inetAddress);
return searchM.find();
}
public static boolean isLanAddress(String inetAddress) {
for (Pattern lanPattern : localComms) {
if (matchesLanAddress(lanPattern, inetAddress)) {
return true;
}
}
return false;
}
public static boolean isLocalAddress(String inetAddress) {
return inetAddress.startsWith("127.");
}
public static String fetchRemoteAddress(String inetAddress) {
if (isLocalAddress(inetAddress) || isLanAddress(inetAddress)) {
return YamlConfig.config.server.HOST;
} else {
return inetAddress;
}
}
public static String getSessionRemoteAddress(IoSession session) {
return (String) session.getAttribute(MapleClient.CLIENT_REMOTE_ADDRESS);
}

View File

@@ -94,7 +94,7 @@ public final class CharSelectedHandler extends AbstractMaplePacketHandler {
return;
}
String[] socket = server.getInetSocket(c.getWorld(), c.getChannel());
String[] socket = server.getInetSocket(session, c.getWorld(), c.getChannel());
if(socket == null) {
c.announce(MaplePacketCreator.getAfterLoginError(10));
return;

View File

@@ -71,7 +71,7 @@ public class CharSelectedWithPicHandler extends AbstractMaplePacketHandler {
return;
}
String[] socket = server.getInetSocket(c.getWorld(), c.getChannel());
String[] socket = server.getInetSocket(session, c.getWorld(), c.getChannel());
if(socket == null) {
c.announce(MaplePacketCreator.getAfterLoginError(10));
return;

View File

@@ -61,21 +61,7 @@ public final class LoginPasswordHandler implements MaplePacketHandler {
@Override
public final void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
String remoteHost = getRemoteIp(c.getSession());
if (!remoteHost.contentEquals("null")) {
if (YamlConfig.config.server.USE_IP_VALIDATION) { // thanks Alex-0000 (CanIGetaPR) for suggesting IP validation as a server flag
if (remoteHost.startsWith("127.")) {
if (!YamlConfig.config.server.LOCALSERVER) { // thanks Mills for noting HOST can also have a field named "localhost"
c.announce(MaplePacketCreator.getLoginFailed(13)); // cannot login as localhost if it's not a local server
return;
}
} else {
if (YamlConfig.config.server.LOCALSERVER) {
c.announce(MaplePacketCreator.getLoginFailed(13)); // cannot login as non-localhost if it's a local server
return;
}
}
}
} else {
if (remoteHost.contentEquals("null")) {
c.announce(MaplePacketCreator.getLoginFailed(14)); // thanks Alchemist for noting remoteHost could be null
return;
}

View File

@@ -79,7 +79,7 @@ public final class RegisterPicHandler extends AbstractMaplePacketHandler {
return;
}
String[] socket = server.getInetSocket(c.getWorld(), c.getChannel());
String[] socket = server.getInetSocket(session, c.getWorld(), c.getChannel());
if(socket == null) {
c.announce(MaplePacketCreator.getAfterLoginError(10));
return;

View File

@@ -82,7 +82,7 @@ public final class ViewAllCharRegisterPicHandler extends AbstractMaplePacketHand
String pic = slea.readMapleAsciiString();
c.setPic(pic);
String[] socket = server.getInetSocket(c.getWorld(), channel);
String[] socket = server.getInetSocket(session, c.getWorld(), channel);
if (socket == null) {
c.announce(MaplePacketCreator.getAfterLoginError(10));
return;

View File

@@ -105,7 +105,7 @@ public final class ViewAllCharSelectedHandler extends AbstractMaplePacketHandler
c.setChannel(1);
}
String[] socket = server.getInetSocket(c.getWorld(), c.getChannel());
String[] socket = server.getInetSocket(session, c.getWorld(), c.getChannel());
if(socket == null) {
c.announce(MaplePacketCreator.getAfterLoginError(10));
return;

View File

@@ -77,7 +77,7 @@ public class ViewAllCharSelectedWithPicHandler extends AbstractMaplePacketHandle
c.setChannel(channel);
if (c.checkPic(pic)) {
String[] socket = server.getInetSocket(c.getWorld(), c.getChannel());
String[] socket = server.getInetSocket(session, c.getWorld(), c.getChannel());
if(socket == null) {
c.announce(MaplePacketCreator.getAfterLoginError(10));
return;