CSM icon indicating copy to clipboard operation
CSM copied to clipboard

Use relay server so no port forwarding is necessary

Open kaenganxt opened this issue 4 years ago • 10 comments

To simplify hosting a server, we should provide a possibility where port forwarding is not necessary. Different levels of relaying could be possible:

  1. Use a technique called UDP hole punching to create the connection, then send messages directly (https://en.wikipedia.org/wiki/UDP_hole_punching). LiteNetLib has support for this. An example can be found here: https://github.com/RevenantX/LiteNetLib/issues/51#issuecomment-292790482
  2. Relay all messages (needs much more performance on the relay)

Things to discuss:

  • What if the relay is not reachable? Fallback to the current solution?
  • Hardcode the relay IP/domain? Allow the user to enter a custom IP?

kaenganxt avatar Jan 17 '21 00:01 kaenganxt

I have a webserver that we can use for the first method as it doesn't have much performance or bandwidth.

Revilum avatar Jan 17 '21 10:01 Revilum

i have a vps if you want to test these options

embedvr avatar Jan 18 '21 00:01 embedvr

It's not quite as reliable as using a 3rd-party server to holepunch (since some routers don't support the feature or have it disabled), but I would also look into UPnP Port Mapping.

tlitookilakin avatar Mar 23 '21 23:03 tlitookilakin

It's not quite as reliable as using a 3rd-party server to holepunch (since some routers don't support the feature or have it disabled), but I would also look into UPnP Port Mapping.

UPnP Port Mapping is in theory implemented, but I have no idea if it works at all (the async work around might be broken, Cities: Skylines uses an old version of .NET that does not support async/await)

https://github.com/CitiesSkylinesMultiplayer/CSM/blob/c6205aa8c06f8ea00852b15915bb187b537f7de6/src/Networking/Server.cs#L93-L94

DominicMaas avatar Mar 24 '21 04:03 DominicMaas

It does work, I guess partly. For me I was able to launch and play without doing manual port-forwarding. Although I know many do have to do it manually.

Bluenix2 avatar Mar 24 '21 11:03 Bluenix2

Good to know, thanks. I'll take a look today at producing a simple server + client + relay example (for UDP hole punching) (make sure it works), and then attempt to integrate that into the mod (I've got a VPS I can use).

DominicMaas avatar Mar 24 '21 19:03 DominicMaas

Last time I checked on Windows, UPnP was working, although it doesn't work on my current Linux system (But I also didn't bother looking into it, so it may be my fault). I guess many people just have UPnP deactivated on their routers, which is a good decision from a security perspective for most people.

About UDP hole punching: The LunaMultiplayer mod for Kerbal Space Program has an implementation for that. A very elaborate description can be found here: https://github.com/LunaMultiplayer/LunaMultiplayer/wiki/Nat-punchthrough and the source code might also be useful.

kaenganxt avatar Mar 24 '21 23:03 kaenganxt

Awesome, thanks!

DominicMaas avatar Mar 24 '21 23:03 DominicMaas

Another option for hosting a relay server could be the oracle cloud. you can get a free vps there.

ghpdohmen avatar Mar 25 '21 08:03 ghpdohmen

Current testing code is here:

https://github.com/CitiesSkylinesMultiplayer/HolePunchTest

Managed to get it working one way (with no port forwarding), but not the other way. More testing required. (example client code is super messy).

DominicMaas avatar Mar 26 '21 23:03 DominicMaas

UDP NAT hole punching was implemented in #298

kaenganxt avatar Jan 27 '23 16:01 kaenganxt