A Frp-Based NAT Traversal Setup
Introduction
Recently I wanted to play Palworld with classmates, but that meant one person’s computer had to keep the world online the whole time. In other words, one specific person had to stay online or nobody else could play. That was annoying. My own computer crashes all the time too.
By coincidence, though, I happen to have a pretty capable little machine at home and also a server with a public IP. The public server is not powerful enough to host the game server by itself, and neither my home network nor the campus network gives me a public IP directly, so the obvious answer is to use the public server as a relay.
So the required hardware for this setup is just:
- one local machine that can host the actual service
- one machine with a public IP that can act as the relay
Basic idea
The idea is simple. The local client, frpc, establishes a TCP connection with the server-side frps on a specific port of the public machine. The local side only needs the public server’s IP address and port. Once connected, frps waits on the relay port you expose, and traffic coming into that public port is forwarded to the local port configured in frpc.
For example, if the game server listens on port 8211 locally and I expose port 6000 on the public server, then traffic sent to port 6000 on the public machine will be forwarded directly to port 8211 on the local host.
Configuration
Download frp first. You can get it directly from the official site: https://gofrp.org/zh-cn/docs/overview/
Upload frps to your public server and frpc to the machine that will host the local service.
Frps
Create a .toml file. The simplest version looks like this:
1 | bindPort = 7000 |
Then start it:
1 | ./frps -c ./frps.toml |
Frpc
Create another .toml file on the local machine:
1 | serverAddr = "8.130.18.93" |
A few practical notes:
- The latency between you and the public server matters. Ping it first. Mine was pretty fast, so I set the timeout to two seconds.
- It is better to set
loginFailExit = false. My packet loss is annoyingly high, especially on campus networks, so letting it keep retrying is much safer. - Make sure you are using the correct protocol for the service you are exposing. Do not spend half a day debugging only to realize the protocol was wrong.
Then start it:
1 | ./frpc -c ./frpc.toml |
Wait a bit. Once it connects, the job is done.
Afterword
It is honestly very simple, which makes me wonder why I even wrote this. Probably because I did not want to study.
For application-specific details, such as which protocol your service needs, go read both the frp documentation and your application’s own docs.
Also, yes, Palworld is fun. My server is probably still running all the time. If anyone actually wants to play, come ask me for the address, though I doubt anyone is reading this blog.
Does anyone want to play Terraria? Does anyone even read the things I write here? Ha. Whatever. I am just writing for fun. I neglected the comment section for too long and it died.
