Page 1 of 1

mod communicating with the outside world

Posted: Wed Nov 22, 2023 10:59 am
by leo_85
hello!

first thing first, my english is broken, i'll do my best (sorry in advance)

so! i'd love to make a mod that allow you to monitor and manage you factory through your phone via an app, by this i mean:
- read/write in the chat
- kick/ban/mute/promote player (if admin)
- see progression of and be able to change current research
- manage trains (modify their schedule)
- activate/deactivate interruptors
- save/save and quit the game
- see electric network / production and consumption state
- send a notification when bitters attack / there is a power outage / a research is finished / a rocket is launched

all of thoses thing are not really a problem, the problem is (like the title said) communicating with outside...

so i've started by looking for the Lua socket module and of course it doesn't work with factorio that have it's very own Lua implementation so HTTP request isn't an option too... (wich seems to make people really mad really quick on the forum for some reasons), after that i've come across this article https://medium.com/expected-behavior/in ... 5d63fecdd0 that's one step! now i have a (pretty terrible) way to extract data from factorio to the outside world, next thing is to import commands from the outside world into factorio, some research later i've find two things:

- factorio implement RCon (Remote Console) wich look pretty easy to access https://pypi.org/project/factorio-rcon-py/
- clusterio mod exist

so here are my questions: can we add custom commands to RCon via mod ? can clusterio help me with what i want to achieve ? or is there an easier way than what i've found to make factorio communicate with outside ?

thanks in advance, have a good day!

Re: mod communicating with the outside world

Posted: Wed Nov 22, 2023 1:05 pm
by mmmPI
Not sure if this is related to what you want or if you are familiar with it but i have seen factorio game communicating to people on phone using discord. I know it is used for moderation so you can read the ingame chat from discord. You can see research, and i think also receive custom notification when things are "bad" like power or biters.

There is mod for this, but also github projects that are not mods and do not require them. I have only see them used from the perspective of someone joining a server when it was running though i have no idea how it works.

Re: mod communicating with the outside world

Posted: Wed Nov 22, 2023 1:30 pm
by leo_85
mmmPI wrote: Wed Nov 22, 2023 1:05 pm Not sure if this is related to what you want or if you are familiar with it but i have seen factorio game communicating to people on phone using discord. I know it is used for moderation so you can read the ingame chat from discord. You can see research, and i think also receive custom notification when things are "bad" like power or biters.

There is mod for this, but also github projects that are not mods and do not require them. I have only see them used from the perspective of someone joining a server when it was running though i have no idea how it works.
Well it does help in a way but i'd like to minimise the constraints for the end user, having to create a discord account and have a discord client running is not optimal... also it's a one way solution (factorio -> outside) and i'm looking for the most difficult part (outside -> factorio). but thank anyway i'm gonna look what i've been made.

Re: mod communicating with the outside world

Posted: Wed Nov 22, 2023 3:55 pm
by Qon
I was interested in this before.
But you have already found all the info you need.
Rcon for data in, file writing for data out.
It's up to you to make a good protocol. But you can send data io as JSON so writing and parsing is trivially handled my your chosen languages (for the external program) JSON library. And you only need 1 rcon command with string input. The input string is simply lua code that the mod just receives the string and runs the lua code.

Re: mod communicating with the outside world

Posted: Wed Nov 22, 2023 5:26 pm
by leo_85
Qon wrote: Wed Nov 22, 2023 3:55 pm I was interested in this before.
But you have already found all the info you need.
Rcon for data in, file writing for data out.
It's up to you to make a good protocol. But you can send data io as JSON so writing and parsing is trivially handled my your chosen languages (for the external program) JSON library. And you only need 1 rcon command with string input. The input string is simply lua code that the mod just receives the string and runs the lua code.
thank you!

so can i make custom rcon command or do i have to use an already defined one that run lua ? and if so, isn't it a major security issue to be able to run arbitrary code from a remote device ?

Re: mod communicating with the outside world

Posted: Wed Nov 22, 2023 5:58 pm
by Nidan
leo_85 wrote: Wed Nov 22, 2023 5:26 pm isn't it a major security issue to be able to run arbitrary code from a remote device ?
What you can do via rcon is the same as what any mod and any player with access to /c can do; you're still constrained by the limits of the modding API. And since the server owner has to explicitly enable rcon via command line or config file, it is their responsibility to secure access to it.

Re: mod communicating with the outside world

Posted: Wed Nov 22, 2023 7:36 pm
by Qon
leo_85 wrote: Wed Nov 22, 2023 5:26 pm so can i make custom rcon command or do i have to use an already defined one that run lua ? and if so, isn't it a major security issue to be able to run arbitrary code from a remote device ?
The person that is running the Factorio server starts Factorio with the program sending the RCON commands, with CLI options for password. And only 1 RCON connection is possible if I remember it correctly.
If you read the doc
https://wiki.factorio.com/Command_line_parameters wrote:--rcon-port N Port to use for RCON
--rcon-bind ADDRESS:PORT IP address and port to use for RCON
--rcon-password PASSWORD Password for RCON
You see that you can set a password also.
But you aren't letting anyone setup the RCON connection to the server, the Factorio server and the program sending RCON commands are on the same machine (well it doesn't have to but if the server is your computer you use to play the game with then this seems likely, since you want the same program to handle connections to many other things and read the logs).
The RCON program can inject code freely, yes. But it's your trusted program, so there's no risk. Also, any user with privileges to use /c can inject same code as the RCON program. It's basically only making an external program an admin player without a character entity and without connecting additional Factorio clients. And all the code injected only has access to the lua API, it can't really do anything outside the Factorio world it is playing. Except the external program already has access to your machine, you are running it...

And https://lua-api.factorio.com/latest/cla ... aRCON.html says you can output to RCON as well, so no need for file output.

Re: mod communicating with the outside world

Posted: Thu Nov 23, 2023 6:18 pm
by leo_85
thank you everyone i think i have all i need to start my project!