Ver Fonte

BETA: Adding auto network configuration method.
* When '''ipconfig.xml''' exists in config folder manual configuration method will be selected (What ipconfig.xml contains that would be it) otherwise your network configuration will be setup automatically.
* Patch by: mrTJO, UnAfraid

Rumen Nikiforov há 12 anos atrás
pai
commit
1883866b7b

+ 1 - 0
L2J_Server_BETA/.classpath

@@ -14,5 +14,6 @@
 	<classpathentry kind="lib" path="dist/libs/netcon-1.7.jar" />
 	<classpathentry kind="lib" path="dist/libs/netcon-1.7.jar" />
 	<classpathentry kind="lib" path="dist/libs/smtp.jar" />
 	<classpathentry kind="lib" path="dist/libs/smtp.jar" />
 	<classpathentry kind="lib" path="dist/libs/trove-3.0.3.jar" />
 	<classpathentry kind="lib" path="dist/libs/trove-3.0.3.jar" />
+	<classpathentry kind="lib" path="dist/libs/Subnet.jar" />
 	<classpathentry kind="output" path="bin" />
 	<classpathentry kind="output" path="bin" />
 </classpath>
 </classpath>

+ 1 - 0
L2J_Server_BETA/dist/game/config/ipconfig.xml → L2J_Server_BETA/dist/game/config/default-ipconfig.xml

@@ -1,4 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <?xml version="1.0" encoding="UTF-8"?>
+<!-- Note: If file is named "ipconfig.xml" this data will be used as network configuration, otherwise server will configure it automatically! -->
 <!-- Externalhost here (Internet IP) or Localhost IP for local test -->
 <!-- Externalhost here (Internet IP) or Localhost IP for local test -->
 <gameserver address="127.0.0.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../data/xsd/ipconfig.xsd">
 <gameserver address="127.0.0.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../data/xsd/ipconfig.xsd">
 	<!-- Localhost here -->
 	<!-- Localhost here -->

BIN
L2J_Server_BETA/dist/libs/Subnet.jar


+ 93 - 13
L2J_Server_BETA/java/com/l2jserver/Config.java

@@ -14,6 +14,8 @@
  */
  */
 package com.l2jserver;
 package com.l2jserver;
 
 
+import info.tak11.subnet.Subnet;
+
 import java.io.BufferedReader;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileInputStream;
@@ -21,15 +23,22 @@ import java.io.FileOutputStream;
 import java.io.FileReader;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.LineNumberReader;
 import java.io.LineNumberReader;
 import java.io.OutputStream;
 import java.io.OutputStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.lang.reflect.Modifier;
 import java.math.BigInteger;
 import java.math.BigInteger;
+import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.InetAddress;
+import java.net.InterfaceAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.net.URL;
 import java.net.UnknownHostException;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Arrays;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
@@ -4289,8 +4298,8 @@ public final class Config
 	
 	
 	private static class IPConfigData extends DocumentParser
 	private static class IPConfigData extends DocumentParser
 	{
 	{
-		private static final List<String> subnets = new ArrayList<>(5);
-		private static final List<String> hosts = new ArrayList<>(5);
+		private static final List<String> _subnets = new ArrayList<>(5);
+		private static final List<String> _hosts = new ArrayList<>(5);
 		
 		
 		public IPConfigData()
 		public IPConfigData()
 		{
 		{
@@ -4300,7 +4309,17 @@ public final class Config
 		@Override
 		@Override
 		public void load()
 		public void load()
 		{
 		{
-			parseFile(new File(IP_CONFIG_FILE));
+			File f = new File(IP_CONFIG_FILE);
+			if (f.exists())
+			{
+				_log.log(Level.INFO, "Network Config: ipconfig.xml exists using manual configuration...");
+				parseFile(new File(IP_CONFIG_FILE));
+			}
+			else // Auto configuration...
+			{
+				_log.log(Level.INFO, "Network Config: ipconfig.xml doesn't exists using automatic configuration...");
+				autoIpConfig();
+			}
 		}
 		}
 		
 		
 		@Override
 		@Override
@@ -4316,10 +4335,10 @@ public final class Config
 						if ("define".equalsIgnoreCase(d.getNodeName()))
 						if ("define".equalsIgnoreCase(d.getNodeName()))
 						{
 						{
 							attrs = d.getAttributes();
 							attrs = d.getAttributes();
-							subnets.add(attrs.getNamedItem("subnet").getNodeValue());
-							hosts.add(attrs.getNamedItem("address").getNodeValue());
+							_subnets.add(attrs.getNamedItem("subnet").getNodeValue());
+							_hosts.add(attrs.getNamedItem("address").getNodeValue());
 							
 							
-							if (hosts.size() != subnets.size())
+							if (_hosts.size() != _subnets.size())
 							{
 							{
 								_log.log(Level.WARNING, "Failed to Load " + IP_CONFIG_FILE + " File - subnets does not match server addresses.");
 								_log.log(Level.WARNING, "Failed to Load " + IP_CONFIG_FILE + " File - subnets does not match server addresses.");
 							}
 							}
@@ -4330,33 +4349,94 @@ public final class Config
 					if (att == null)
 					if (att == null)
 					{
 					{
 						_log.log(Level.WARNING, "Failed to load " + IP_CONFIG_FILE + " file - default server address is missing.");
 						_log.log(Level.WARNING, "Failed to load " + IP_CONFIG_FILE + " file - default server address is missing.");
-						hosts.add("127.0.0.1");
+						_hosts.add("127.0.0.1");
 					}
 					}
 					else
 					else
 					{
 					{
-						hosts.add(att.getNodeValue());
+						_hosts.add(att.getNodeValue());
+					}
+					_subnets.add("0.0.0.0/0");
+				}
+			}
+		}
+		
+		protected void autoIpConfig()
+		{
+			String externalIp = "127.0.0.1";
+			try 
+			{
+				URL autoIp = new URL("http://api.externalip.net/ip/");
+				try (BufferedReader in = new BufferedReader(new InputStreamReader(autoIp.openStream())))
+				{
+					externalIp = in.readLine();
+				}
+			}
+			catch (IOException e)
+			{
+				_log.log(Level.INFO, "Network Config: Failed to connect to api.externalip.net please check your internet connection using 127.0.0.1!");
+				externalIp = "127.0.0.1";
+			}
+			
+			try
+			{
+				Enumeration<NetworkInterface> niList = NetworkInterface.getNetworkInterfaces();
+				
+				Subnet sub = new Subnet();
+				while (niList.hasMoreElements())
+				{
+					NetworkInterface ni = niList.nextElement();
+					
+					if (!ni.isUp() || ni.isVirtual())
+						continue;
+					
+					if (!ni.isLoopback() && ni.getHardwareAddress().length != 6)
+						continue;
+					
+					for (InterfaceAddress ia : ni.getInterfaceAddresses())
+					{
+						if (ia.getAddress() instanceof Inet6Address)
+							continue;
+						
+						sub.setIPAddress(ia.getAddress().getHostAddress());
+						sub.setMaskedBits(ia.getNetworkPrefixLength());
+						String subnet = sub.getSubnetAddress() + '/' + sub.getMaskedBits();
+						if (!_subnets.contains(subnet) && !subnet.equals("0.0.0.0/0"))
+						{
+							_subnets.add(subnet);
+							_hosts.add(sub.getIPAddress());
+							_log.log(Level.INFO, "Network Config: Adding new subnet: " + subnet + " address: " + sub.getIPAddress());
+						}
 					}
 					}
-					subnets.add("0.0.0.0/0");
 				}
 				}
+				
+				// External host and subnet
+				_hosts.add(externalIp);
+				_subnets.add("0.0.0.0/0");
+				_log.log(Level.INFO, "Network Config: Adding new subnet: 0.0.0.0/0 address: " + externalIp);
+			}
+			catch (SocketException e)
+			{
+				_log.log(Level.INFO, "Network Config: Configuration failed please configure manually using ipconfig.xml", e);
+				System.exit(0);
 			}
 			}
 		}
 		}
 		
 		
 		protected List<String> getSubnets()
 		protected List<String> getSubnets()
 		{
 		{
-			if (subnets.isEmpty())
+			if (_subnets.isEmpty())
 			{
 			{
 				return Arrays.asList("0.0.0.0/0");
 				return Arrays.asList("0.0.0.0/0");
 			}
 			}
-			return subnets;
+			return _subnets;
 		}
 		}
 		
 		
 		protected List<String> getHosts()
 		protected List<String> getHosts()
 		{
 		{
-			if (hosts.isEmpty())
+			if (_hosts.isEmpty())
 			{
 			{
 				return Arrays.asList("127.0.0.1");
 				return Arrays.asList("127.0.0.1");
 			}
 			}
-			return hosts;
+			return _hosts;
 		}
 		}
 	}
 	}
 }
 }