Change external IP after servers started

This is not a Support area! Discuss about the Server here. Non-Server related discussion goes in Off-Topic Discussion.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
Hyrelius
Posts: 257
Joined: Thu Dec 16, 2010 5:16 am

Change external IP after servers started

Post by Hyrelius »

Hello everyone,

please let me explain what I have / need.

I have an internet connection, that resets my IP address every 24 hours and there is no static-IP option available. I have a no-ip redirection, which always contains the correct IP.

I want to run a small test-server for a buddy and myself, which is hosted on my server AT HOME.

So far I managed to be able to run the login- and game-servers on my server and connect to them through external dynamic DNS (using my no-ip host).

However - and this is my problem: at a certain hour my IP changes. I am afraid, that if it does so, it will not allow my buddy to connect to my test-server anymore, because the server (which started with the "old" IP-address) is still running.

Is restarting the server my only option or are there means (using ipconfig.xml or something) to adjust the IP while the login- and game-servers are already up and running?
Could I possibly fill in my no-ip host into the ipconfig.xml instead of an IP-address?


Thank you for your replies in advance! Hints would do - I am sure I could figure it out if you give me a hint or two :).

P.s.: I hope this is the correct forum for this question. If not, I apologize and ask you to move it to where you think the right place is.
P.s.2: I am currently running an Ertheia login- and game-server, but I believe it would be the same if I was using a High-Five login- and game-server instead.
Image
I don't mind helping - however: I only do so if I want to.
No support for other server packs than L2J.
Digitizer
Posts: 18
Joined: Mon May 18, 2015 9:24 pm

Re: Change external IP after servers started

Post by Digitizer »

Here is a untested, quick and dirty solution only for small testservers.
Use at your own risk, if this is buggy it will break the whole login procedure.

What does it?
Changes loginserver to replace the external ip address of every gameserver with
the current external ip address of the loginserver OR ip of a hard coded domain.

Variant 1
:
Does a DNS lookup, you need to set your dynamic domain.

Variant 2:
Does a http lookup for every gameserver to get current external ip when a player connects.
No caching, use only for very small servers, or maybe you will be blocked by no-ip.com

Installation:
Apply patch, uncomment a variant and rebuild loginserver.

Both variants will delay your login. I've used parts of the autoip method of the core for this snippet.

Have fun. :D

Code: Select all

### Eclipse Workspace Patch 1.0
#P L2J_Server
diff --git java/com/l2jserver/loginserver/GameServerTable.java java/com/l2jserver/loginserver/GameServerTable.java
index 7ce9976..f746da1 100644
--- java/com/l2jserver/loginserver/GameServerTable.java
+++ java/com/l2jserver/loginserver/GameServerTable.java
@@ -596,15 +596,63 @@
 		 * @param addr the addr
 		 * @return the server address
 		 */
 		public String getServerAddress(InetAddress addr)
 		{
-			for (GameServerAddress a : _addrs)
+			int last = _addrs.size() - 1;
+			for (int i = 0; i < _addrs.size(); i++)
 			{
-				if (a.equals(addr))
+				GameServerAddress a = _addrs.get(i);
+				
+				if (i != last)
 				{
-					return a.getServerAddress();
+					if (a.equals(addr))
+					{
+						return a.getServerAddress();
+					}
+				}
+				else
+				{
+					/*
+					 * WARNING: Quick, dirty and UNTESTED. For small testservers ONLY!
+					 */
+					
+					// Variant 1: DNS lookup
+					
+					// String domain = "YOUR-DOMAIN-HERE";
+					// try
+					// {
+					// LOGGER.info("IP-Info: Replacing ip " + a.getServerAddress() + " with dns result " + InetAddress.getByName(domain).getHostAddress());
+					// return domain;
+					// }
+					// catch (UnknownHostException e)
+					// {
+					// LOGGER.severe("IP-WARNING: Could not lookup ip of domain \"" + domain + "\". Returning ip (" + a.getServerAddress() + ") gameserver reported at last connect!");
+					// return a.getServerAddress();
+					// }
+					
+					// ------------------------------------------------------------------------------------
+					
+					// Variant 2: Get ip via http lookup by no-ip.com
+					
+					// try
+					// {
+					// URL autoIp = new URL("http://ip1.dynupdate.no-ip.com:8245/");
+					// try (BufferedReader in = new BufferedReader(new InputStreamReader(autoIp.openStream())))
+					// {
+					// String newIp = in.readLine().trim();
+					// LOGGER.info("IP-Info: Replacing ip " + a.getServerAddress() + " with " + newIp);
+					// return newIp;
+					// }
+					// }
+					// catch (IOException e)
+					// {
+					// LOGGER.severe("IP-WARNING: Could not retrieve current ip address. Returning ip (" + a.getServerAddress() + ") gameserver reported at last connect!");
+					// return a.getServerAddress();
+					// }
 				}
 			}
 			return null; // should not happens
 		}
 		
Last edited by Digitizer on Tue Jul 14, 2015 10:04 am, edited 1 time in total.
User avatar
Zoey76
L2j Inner Circle
L2j Inner Circle
Posts: 7005
Joined: Tue Aug 11, 2009 3:36 am

Re: Change external IP after servers started

Post by Zoey76 »

Digitizer is onto something :+1:
Powered by Eclipse 4.30 🌌 | Eclipse Temurin 21 ☕ | MariaDB 11.2.2 🗃️ | L2J Server 2.6.3.0 - High Five 🚀

🔗 Join our Discord! 🎮💬
Hyrelius
Posts: 257
Joined: Thu Dec 16, 2010 5:16 am

Re: Change external IP after servers started

Post by Hyrelius »

Wow that looks awesome! Thanks you!

I need to try this out asap, but before I do so: how does it know the external IP changed? I know this, because I know at approx. what hour the IP changes (and I have logs to double-check if I have to).

And also: is it really able to determine the IP from a dynamic host service (e.g. no-ip) and set the login's and game-server's IP addresses to this new value?

In any case - I shall try this out. I do not mind if things become a bit slow and this test-server is for personal use only - I suppose there will never be more than 5 people on it at once. In most cases it will be 2 people only.
Image
I don't mind helping - however: I only do so if I want to.
No support for other server packs than L2J.
Digitizer
Posts: 18
Joined: Mon May 18, 2015 9:24 pm

Re: Change external IP after servers started

Post by Digitizer »

Hyrelius wrote:how does it know the external IP changed?
Everytime a player connects from outside (last ip in list) it does a ip lookup. This is the simplest way. :wink:
Hyrelius wrote:And also: is it really able to determine the IP from a dynamic host service (e.g. no-ip) and set the login's and game-server's IP addresses to this new value?
Yes, as soon as the ttl of your dynamic domain is over, variant1 will get the new ip via dns resolution. How fast this is depends on how fast you report the new ip to your dynamic serivce. Integrated in your router will be faster than a client on a pc behind. (To test yourself: cmd -> nslookup)

Variant2 will work always, independent of your dynamic domain.
Hyrelius
Posts: 257
Joined: Thu Dec 16, 2010 5:16 am

Re: Change external IP after servers started

Post by Hyrelius »

Hello again,

sorry if I may sound like a broken record or even seem stupid, but I still do not understand a couple of things.

1. I think in variant 1 the return value of the lookup should be "ip" not "domain", but that is just a note (and a question of whether I understood it).
2. I still do not exactly understand when the acquiring of a new IP happens, because once my IP changed, I cannot imagine other players, who might want to connect, would get a response.

Say I do this:
  • start loginserver
  • start gameserver
  • connect to the loginserver and the gameserver (by entering the game)
This means I connected to my server and am able to play.

Now imagine the server-IP changes and is almost instantly broadcasted to my no-ip redirect.
New players, who try to connect to the server by using "myaddress.no-ip.org" would be redirected to my new IP - which is fine.
However: when / how would the server know, that its external IP changed (unless it monitors the external IP)?

From what I understand the requests via corresponding login- and gameserver ports would start hitting the new server as soon as my dynamic dns redirect changed to the new IP (of my physical server). Does that mean, that the login- and gameserver would be able to recognize these requests and thus lookup its new external IP?

What happens to the players, who are currently connected to the server? I would not mind if they get disconnected, but the command-line parameter I am using to redirect the client to my server is the dynamic redirect - which means the client would probably not notice that the IP behind the dynamic redirect changed and correctly access my server?

I added the changes and will compile the new l2jlogin.jar to test it, but I would also like to understand how this works so if you could elaborate on this just a bit more, I perhaps would understand :). Thank you very much in advance though!

Edit: MARVELOUS! Many many thanks, Digitizer.
I just tried variant 1 by using "myaddress.no-ip.com" and it worked! I did NOT have to restart the server in order to be able to reconnect after the IP had changed. It simply let me login and when I have chosen a character, it replaced the old external IP by the new one flawlessly.

However: characters, that have been logged in before the server changed its IP, caused the client to hang (and potentially disconnect) and thus had to be logged back in afterwards. However: on a test-server this is absolutely no problem at all. Everything else seemed to work just like it did before the IP changed.

Again: many thanks! I will probably stick with the first variant as it really works flawlessly. If there was a way to keep the connected clients connected, it'd be awesome, but I suppose it will not work, because I suspect that the client resolves the host I have given to an IP when it starts up and thus does not notice the change, ultimately disconnecting once the server-IP changes, right?
Image
I don't mind helping - however: I only do so if I want to.
No support for other server packs than L2J.
Digitizer
Posts: 18
Joined: Mon May 18, 2015 9:24 pm

Re: Change external IP after servers started

Post by Digitizer »

Hyrelius wrote:MARVELOUS! Many many thanks, Digitizer.
I'm glad it's working for you. :D

Hyrelius wrote:1. I think in variant 1 the return value of the lookup should be "ip" not "domain", but that is just a note (and a question of whether I understood it).
Technically no, because the calling function does a lookup again with InetAddress.getByName. But a developer might expect an IP, thats true. The local lookup in getServerAddress by me was just for logging purpose in case it isn't working. You can delete it and just return your domain.

Hyrelius wrote:2. I still do not exactly understand when the acquiring of a new IP happens, because once my IP changed, I cannot imagine other players, who might want to connect, would get a response.
A short explaination (I assume you're behind a router):

When you start your loginserver it listens to all network interfaces of your local pc (default configuration). It gives a damn about your external ip, it just sits there and waits for any incoming connection. Your client is configured to use your dynamic domain, and thats the reason why it will always reach your login when your ip (and dynamic domain) changes.

With your gameserver there is a little bit more magic involved. At startup the gameserver detects and sends all ip addresses he is reachable through to loginserver. These ips/subnets are saved and depending on the players source ip address it choose the correct one and sends it to the player. Now: When your external IP address changes the registration on loginserver side does not change, and your loginserver sends the wrong gameserver ip to your players.

There are many ways on how this could be solved, i choosed this one:

After patch, your loginserver does ignore the external IP it got from gameserver and instead uses always the current ip of your dynamic domain.

Hyrelius wrote:What happens to the players, who are currently connected to the server? I would not mind if they get disconnected, but the command-line parameter I am using to redirect the client to my server is the dynamic redirect - which means the client would probably not notice that the IP behind the dynamic redirect changed and correctly access my server?
Hyrelius wrote:I just tried variant 1 by using "myaddress.no-ip.com" and it worked! I did NOT have to restart the server in order to be able to reconnect after the IP had changed. It simply let me login and when I have chosen a character, it replaced the old external IP by the new one flawlessly.

However: characters, that have been logged in before the server changed its IP, caused the client to hang (and potentially disconnect) and thus had to be logged back in afterwards. However: on a test-server this is absolutely no problem at all. Everything else seemed to work just like it did before the IP changed.
Lineage 2 uses TCP for its connection, and a TCP connection is definied by 4 parameters. Local IP, Local Port, Remote Ip and Remote Port.
You cannot just replace one of those parameters and continue communicating. You will receive an instant reset packet because your communication peer doesn't know who it is talking to. That's the reason you need to reconnect to say hello again via tcp 3-way handshake. ;)

Hyrelius wrote:Again: many thanks! I will probably stick with the first variant as it really works flawlessly. If there was a way to keep the connected clients connected, it'd be awesome, but I suppose it will not work, because I suspect that the client resolves the host I have given to an IP when it starts up and thus does not notice the change, ultimately disconnecting once the server-IP changes, right?
There is a way, you can setup a VPN, enable keep-alive with a very low timeout. When it reconnects fast enough all player which are connected via VPN would'nt need to reconnect. But the game would still hang/lag while this whole process is going on. In my opinion it is not worth the hassle.
Hyrelius
Posts: 257
Joined: Thu Dec 16, 2010 5:16 am

Re: Change external IP after servers started

Post by Hyrelius »

Excellent. If I had to run the login- and game-servers from my home-server, I would think about going through the hassle, but seeing as it is just a dev-server and knowing that a VPN would technically be a terrible overhead, I won't do that.

I totally forgot about the TCP-Handshake (my teacher in networking would shout at me >.< ).

Anyway: thank you. It works as expected and this is good enough for development. The actual server is in a datacenter and has a proper static-IP :).
Image
I don't mind helping - however: I only do so if I want to.
No support for other server packs than L2J.
Lupu1
Posts: 335
Joined: Wed Nov 04, 2009 7:51 am

Re: Change external IP after servers started

Post by Lupu1 »

on the latest h5 dev you still need this code for dynamic ip with ddns and router? when ip changes the login server works but can't enter in game without restarting the gameserver to take the new ip
Post Reply