Encapsulate ip address logic in new class
This commit is contained in:
@@ -30,6 +30,7 @@ import net.server.audit.LockCollector;
|
||||
import net.server.audit.locks.MonitoredLockType;
|
||||
import net.server.audit.locks.MonitoredReentrantLock;
|
||||
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
|
||||
import net.server.coordinator.session.IpAddresses;
|
||||
import net.server.coordinator.session.MapleSessionCoordinator;
|
||||
import org.apache.mina.core.service.IoHandlerAdapter;
|
||||
import org.apache.mina.core.session.IdleStatus;
|
||||
@@ -105,7 +106,7 @@ 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
|
||||
remoteHost = IpAddresses.evaluateRemoteAddress(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";
|
||||
|
||||
@@ -47,6 +47,7 @@ import net.server.audit.locks.factory.MonitoredReadLockFactory;
|
||||
import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
|
||||
import net.server.audit.locks.factory.MonitoredWriteLockFactory;
|
||||
import net.server.channel.Channel;
|
||||
import net.server.coordinator.session.IpAddresses;
|
||||
import net.server.coordinator.session.MapleSessionCoordinator;
|
||||
import net.server.guild.MapleAlliance;
|
||||
import net.server.guild.MapleGuild;
|
||||
@@ -297,9 +298,9 @@ public class Server {
|
||||
String remoteIp = MapleSessionCoordinator.getSessionRemoteAddress(session);
|
||||
|
||||
String[] hostAddress = getIP(world, channel).split(":");
|
||||
if (MapleSessionCoordinator.isLocalAddress(remoteIp)) {
|
||||
if (IpAddresses.isLocalAddress(remoteIp)) {
|
||||
hostAddress[0] = YamlConfig.config.server.LOCALHOST;
|
||||
} else if (MapleSessionCoordinator.isLanAddress(remoteIp)) {
|
||||
} else if (IpAddresses.isLanAddress(remoteIp)) {
|
||||
hostAddress[0] = YamlConfig.config.server.LANHOST;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package net.server.coordinator.session;
|
||||
|
||||
import config.YamlConfig;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class IpAddresses {
|
||||
private static final List<Pattern> LOCAL_ADDRESS_PATTERNS = loadLocalAddressPatterns();
|
||||
|
||||
private static List<Pattern> loadLocalAddressPatterns() {
|
||||
return Stream.of("10\\.", "192\\.168\\.", "172\\.(1[6-9]|2[0-9]|3[0-1])\\.")
|
||||
.map(Pattern::compile)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static String evaluateRemoteAddress(String inetAddress) {
|
||||
if (isLocalAddress(inetAddress) || isLanAddress(inetAddress)) {
|
||||
return YamlConfig.config.server.HOST;
|
||||
} else {
|
||||
return inetAddress;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isLocalAddress(String inetAddress) {
|
||||
return inetAddress.startsWith("127.");
|
||||
}
|
||||
|
||||
public static boolean isLanAddress(String inetAddress) {
|
||||
return LOCAL_ADDRESS_PATTERNS.stream()
|
||||
.anyMatch(pattern -> matchesPattern(pattern, inetAddress));
|
||||
}
|
||||
|
||||
private static boolean matchesPattern(Pattern pattern, String searchTerm) {
|
||||
return pattern.matcher(searchTerm).find();
|
||||
}
|
||||
}
|
||||
@@ -209,46 +209,6 @@ public class MapleSessionCoordinator {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user