Factorio Multiplayer Protocol

Post your ideas and suggestions how to improve the game.

Moderator: ickputzdirwech

User avatar
BBBBSammich
Burner Inserter
Burner Inserter
Posts: 13
Joined: Sun Mar 07, 2021 8:45 am
Contact:

Factorio Multiplayer Protocol

Post by BBBBSammich »

I would like to make a proxy for Factorio which serves to redirect joining players to a server which is not necessarily running. It’s expensive for me to keep a factorion server running 24/7 and it makes financial sense to only run the server when someone is playing. I have done this for Minecraft, and it worked great. I believe Minecraft’s multiplayer protocol is reverse engineered by the community https://minecraft.wiki/w/Java_Edition_protocol. But I do not have the capacity to do so for Factorio, some attempts were made in the past, but not to a significant degree. It would be great if some insight could be given to the workings of the factorion multiplayer protocol, (specifically the server joining part). I know that maintaining documentation about the full protocol is impossible because of the way the game works.
Rseding91
Factorio Staff
Factorio Staff
Posts: 15144
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Factorio Multiplayer Protocol

Post by Rseding91 »

The Factorio server auto paused by default when no one is online, so in that sense it will use virtually no CPU. Is that not the goal?
If you want to get ahold of me I'm almost always on Discord.
Tertius
Smart Inserter
Smart Inserter
Posts: 1148
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Factorio Multiplayer Protocol

Post by Tertius »

Is it really necessary to write a whole proxy service?
What do you need to start on demand, is it just a service (the Factorio server process) or do you need to start up a whole virtual machine?

With systemd, it's possible to listen on a port (in this case the Factorio udp port) with a *.socket unit and start a *.service unit that's supposed to serve this port. So the service isn't running as default, and if systemd detects activity on the port, it starts the associated *.service unit.

This *.service doesn't need to be the factorio server service, it can be any *.sh script. I guess you're able to start your vm from within, and if it is up, add an iptables rule to forward the local udp port to the vm udp port. This is actually your proxy: a corresponding iptables NAT rule for udp traffic on the given port. Don't need to look inside the packets.
User avatar
BBBBSammich
Burner Inserter
Burner Inserter
Posts: 13
Joined: Sun Mar 07, 2021 8:45 am
Contact:

Re: Factorio Multiplayer Protocol

Post by BBBBSammich »

Yes, it does have to do with starting a virtual machine. In my country at least, it is a lot cheaper to run a virtual machine on demand rather than using a hosting service or other solutions. I have so far written a discord bot where players can send a command to start the vm, but it is more convenient for players to start the vm by trying to join the server. I am a uni student hosting a game for my class, and it worked great with minecraft. Setting up the proxy was actually easier than the discord bot, as the edge cases were actually a lot fewer. I just had to look at the active TCP connections to see if anyone was online for instance. I know it's a bit different for UDP but I believe the principles stay the same.

I'd only have to detect when a player joins, maybe send some sort of message and kick them off. The vm is then started and any other connection attempts will be deferred until the server has successfully started. Then any further connection attempts will route all further messages directly between server and client. So I just need to know how to accept a connection and then immediately kick the player. A ping would also be useful to check if the server is online.
User avatar
BBBBSammich
Burner Inserter
Burner Inserter
Posts: 13
Joined: Sun Mar 07, 2021 8:45 am
Contact:

Re: Factorio Multiplayer Protocol

Post by BBBBSammich »

Tertius wrote: Sun Mar 16, 2025 1:39 pm Is it really necessary to write a whole proxy service?
What do you need to start on demand, is it just a service (the Factorio server process) or do you need to start up a whole virtual machine?

With systemd, it's possible to listen on a port (in this case the Factorio udp port) with a *.socket unit and start a *.service unit that's supposed to serve this port. So the service isn't running as default, and if systemd detects activity on the port, it starts the associated *.service unit.

This *.service doesn't need to be the factorio server service, it can be any *.sh script. I guess you're able to start your vm from within, and if it is up, add an iptables rule to forward the local udp port to the vm udp port. This is actually your proxy: a corresponding iptables NAT rule for udp traffic on the given port. Don't need to look inside the packets.
This could work, I'd just have to set up a subprocess wrapper that reads the console logs, and sends an api call to stop the vm when all players leave. Or I can maybe check if the network activity ceases from the "proxy" machine, which would also be a failsafe if the server fails to start. It would just miss the interactive element that gives feedback to the player whether the server is responding and actually starting or not.

So if it is possible to do what my previous comment suggests with the existing factorio protocol, then that would still work better.
Post Reply

Return to “Ideas and Suggestions”