diff --git a/OF DL/OF DL.csproj b/OF DL/OF DL.csproj
index 2ee9f51..be35c42 100644
--- a/OF DL/OF DL.csproj
+++ b/OF DL/OF DL.csproj
@@ -18,6 +18,7 @@
+
diff --git a/OF DL/Program.cs b/OF DL/Program.cs
index ae0e740..2c005bb 100644
--- a/OF DL/Program.cs
+++ b/OF DL/Program.cs
@@ -13,14 +13,13 @@ using Serilog;
using Serilog.Core;
using Serilog.Events;
using Spectre.Console;
-using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using static OF_DL.Entities.Messages.Messages;
using Akka.Configuration;
using System.Text;
-using static Akka.Actor.ProviderSelection;
+using Microsoft.Extensions.DependencyInjection;
namespace OF_DL;
@@ -31,11 +30,16 @@ public class Program
private static bool clientIdBlobMissing = false;
private static bool devicePrivateKeyMissing = false;
- private static Entities.Config? config = null;
- private static Auth? auth = null;
+ private readonly Entities.Config config;
+ private Auth? auth = null;
private static LoggingLevelSwitch levelSwitch = new LoggingLevelSwitch();
- private static async Task LoadAuthFromBrowser()
+ public Program(Entities.Config config)
+ {
+ this.config = config;
+ }
+
+ private async Task LoadAuthFromBrowser()
{
bool runningInDocker = Environment.GetEnvironmentVariable("OFDL_DOCKER") != null;
@@ -104,21 +108,48 @@ public class Program
public static async Task Main(string[] args)
{
- bool cliNonInteractive = false;
+ levelSwitch.MinimumLevel = LogEventLevel.Error; //set initial level (until we've read from config)
+
+ Log.Logger = new LoggerConfiguration()
+ .MinimumLevel.ControlledBy(levelSwitch)
+ .WriteTo.File("logs/OFDL.txt", rollingInterval: RollingInterval.Day)
+ .CreateLogger();
+
+ AnsiConsole.Write(new FigletText("Welcome to OF-DL").Color(Color.Red));
+ AnsiConsole.Markup("Documentation: [link]https://docs.ofdl.tools/[/]\n");
+ AnsiConsole.Markup("Discord server: [link]https://discord.com/invite/6bUW8EJ53j[/]\n\n");
+
+ // Load configuration
+ Entities.Config? config = LoadConfiguration(args, out bool cliNonInteractive);
+ if (config == null)
+ {
+ Environment.Exit(3);
+ return;
+ }
+
+ // Set up dependency injection
+ var services = new ServiceCollection();
+ ConfigureServices(services, config);
+ var serviceProvider = services.BuildServiceProvider();
+
+ // Get the Program instance and run
+ var program = serviceProvider.GetRequiredService();
+ await program.RunAsync(args, cliNonInteractive);
+ }
+
+ private static void ConfigureServices(IServiceCollection services, Entities.Config config)
+ {
+ services.AddSingleton(config);
+ services.AddSingleton();
+ }
+
+ private static Entities.Config? LoadConfiguration(string[] args, out bool cliNonInteractive)
+ {
+ cliNonInteractive = false;
+ Entities.Config? config = null;
try
{
- levelSwitch.MinimumLevel = LogEventLevel.Error; //set initial level (until we've read from config)
-
- Log.Logger = new LoggerConfiguration()
- .MinimumLevel.ControlledBy(levelSwitch)
- .WriteTo.File("logs/OFDL.txt", rollingInterval: RollingInterval.Day)
- .CreateLogger();
-
- AnsiConsole.Write(new FigletText("Welcome to OF-DL").Color(Color.Red));
- AnsiConsole.Markup("Documentation: [link]https://docs.ofdl.tools/[/]\n");
- AnsiConsole.Markup("Discord server: [link]https://discord.com/invite/6bUW8EJ53j[/]\n\n");
-
//Remove config.json and convert to config.conf
if (File.Exists("config.json"))
{
@@ -502,7 +533,21 @@ public class Program
Log.Debug(argument);
}
}
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine(e);
+ Log.Error("Configuration loading failed.", e.Message);
+ return null;
+ }
+ return config;
+ }
+
+ private async Task RunAsync(string[] args, bool cliNonInteractive)
+ {
+ try
+ {
var os = Environment.OSVersion;
Log.Debug($"Operating system information: {os.VersionString}");
@@ -668,7 +713,7 @@ public class Program
}
//Added to stop cookie being filled with un-needed headers
- ValidateCookieString();
+ ValidateCookieString(auth!);
if (File.Exists("rules.json"))
{
@@ -917,7 +962,6 @@ public class Program
}
}
-
private static async Task DownloadAllData(APIHelper m_ApiHelper, Auth Auth, Entities.Config Config)
{
DBHelper dBHelper = new DBHelper(Config);
@@ -2959,7 +3003,7 @@ public class Program
foreach (var item in listSelection)
{
long listId = lists[item.Replace("[red]", "").Replace("[/]", "")];
- List usernames = await apiHelper.GetListUsers($"/lists/{listId}/users", config);
+ List usernames = await apiHelper.GetListUsers($"/lists/{listId}/users", currentConfig);
foreach (string user in usernames)
{
listUsernames.Add(user);
@@ -3456,7 +3500,7 @@ public class Program
return null;
}
- public static void ValidateCookieString()
+ public static void ValidateCookieString(Auth auth)
{
string pattern = @"(auth_id=\d+)|(sess=[^;]+)";
var matches = Regex.Matches(auth.COOKIE, pattern);