Merge pull request #17 from ronancpI/any-ipdomain-update

Allow local/LAN/WAN login-phase connections
This commit is contained in:
Ponk
2021-04-10 18:56:54 +02:00
committed by GitHub
14 changed files with 70 additions and 28 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

@@ -49,7 +49,7 @@ public class WarpWorldCommand extends Command {
byte worldb = Byte.parseByte(params[0]);
if (worldb <= (server.getWorldsSize() - 1)) {
try {
String[] socket = server.getInetSocket(worldb, c.getChannel());
String[] socket = server.getInetSocket(c.getSession(), worldb, c.getChannel());
c.getWorldServer().removePlayer(player);
player.getMap().removePlayer(player);//LOL FORGOT THIS ><
player.setSessionTransitionState();

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

@@ -56,6 +56,7 @@ import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.buffer.SimpleBufferAllocator;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.slf4j.Logger;
@@ -285,9 +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

@@ -38,6 +38,8 @@ import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
@@ -206,7 +208,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;