Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions FactorioWrapper/FactorioWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Serilog;
using System;
using System.IO;
using System.Linq;

namespace FactorioWrapper
{
Expand Down Expand Up @@ -55,6 +56,13 @@ public static void Main(string[] args)
settings.Url = url;
}

// The password is passed to factorio as a command line argument, so it can't contain whitespace or quotes.
if (settings.RconPassword != null && settings.RconPassword.Any(c => char.IsWhiteSpace(c) || c == '"'))
{
Log.Fatal("rcon_password in settings.json must not contain whitespace or quotes");
return;
}

if (args.Length < 3)
{
Log.Fatal("Missing arguments");
Expand Down
20 changes: 13 additions & 7 deletions FactorioWrapper/MainLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -25,7 +26,7 @@ public class MainLoop : IDisposable
private readonly Settings settings;
private readonly string serverId;
private readonly string factorioFileName;
private string factorioArguments;
private readonly string factorioArguments;

// This is to stop multiple threads writing to the factorio process concurrently.
private readonly SemaphoreSlim factorioProcessLock = new SemaphoreSlim(1, 1);
Expand All @@ -45,9 +46,9 @@ public class MainLoop : IDisposable

private volatile RconMessenger rcon;
private int rconPort;
private const string rconPassword = "no_one_will_guess_this_awesome_password.";
private readonly string rconPassword;

private void PrependRconArguments()
private void SetRconPort()
{
int index = factorioArguments.IndexOf("--port");

Expand All @@ -61,8 +62,6 @@ private void PrependRconArguments()
string port = factorioArguments.Substring(start, end - start);

int.TryParse(port, out rconPort);

factorioArguments += $" --rcon-port {rconPort} --rcon-password {rconPassword}";
}

private async Task ConnectRCON()
Expand All @@ -88,7 +87,12 @@ public MainLoop(Settings settings, string serverId, string factorioFileName, str
this.factorioFileName = factorioFileName;
this.factorioArguments = factorioArguments;

PrependRconArguments();
// Fall back to a random password so RCON is never left with a known password.
rconPassword = string.IsNullOrWhiteSpace(settings.RconPassword)
? Convert.ToHexString(RandomNumberGenerator.GetBytes(32))
: settings.RconPassword;

SetRconPort();

messageQueue = new SingleConsumerQueue<Func<Task>>(maxMessageQueueSize, async func =>
{
Expand Down Expand Up @@ -384,7 +388,9 @@ private async Task StartFactorioProcess()
factorioProcess = new Process();
var startInfo = factorioProcess.StartInfo;
startInfo.FileName = factorioFileName;
startInfo.Arguments = factorioArguments;
// The rcon password is added after logging so that it doesn't end up in the log file.
// Rcon is bound to localhost so that it isn't reachable from outside this machine.
startInfo.Arguments = $"{factorioArguments} --rcon-bind 127.0.0.1:{rconPort} --rcon-password {rconPassword}";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardInput = true;
Expand Down
4 changes: 4 additions & 0 deletions FactorioWrapper/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ public class Settings
[JsonProperty(PropertyName = "url")]
[JsonPropertyName("url")]
public string Url { get; set; }

[JsonProperty(PropertyName = "rcon_password")]
[JsonPropertyName("rcon_password")]
public string RconPassword { get; set; }
}
}
3 changes: 2 additions & 1 deletion FactorioWrapper/settings.template.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"token": "asdasdjuteoinbjnlkrutpeoplp.fkgcldfooklisfgsdlafhlfreFGTRydegsJJKGHURTDSwaerqsdfdfsgtJFHDfbgsDASDASDFERGDFBGsdfsdfSFQAWSEAZBBVJNHGJdfghfghSERWSFSVXBGFDgtDRTDGFJHJKykjHjHBKJfyjutygj_bLJyuhHUIOgJKBLnoOUiohuiGh_JKBjkGj-guiGhfu",
"url": "http://localhost/factorioProcessHub"
"url": "http://localhost/factorioProcessHub",
"rcon_password": ""
}
Loading