Major refactor #141

Merged
sim0n00ps merged 55 commits from whimsical-c4lic0/OF-DL:refactor-architecture into master 2026-02-13 00:21:58 +00:00
2 changed files with 66 additions and 21 deletions
Showing only changes of commit a566cd0b71 - Show all commits

View File

@ -18,6 +18,7 @@
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" /> <PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" />
<PackageReference Include="HtmlAgilityPack" Version="1.12.0" /> <PackageReference Include="HtmlAgilityPack" Version="1.12.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.3" /> <PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="protobuf-net" Version="3.2.46" /> <PackageReference Include="protobuf-net" Version="3.2.46" />
<PackageReference Include="PuppeteerSharp" Version="20.2.5" /> <PackageReference Include="PuppeteerSharp" Version="20.2.5" />

View File

@ -13,14 +13,13 @@ using Serilog;
using Serilog.Core; using Serilog.Core;
using Serilog.Events; using Serilog.Events;
using Spectre.Console; using Spectre.Console;
using System.IO;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using static OF_DL.Entities.Messages.Messages; using static OF_DL.Entities.Messages.Messages;
using Akka.Configuration; using Akka.Configuration;
using System.Text; using System.Text;
using static Akka.Actor.ProviderSelection; using Microsoft.Extensions.DependencyInjection;
namespace OF_DL; namespace OF_DL;
@ -31,11 +30,16 @@ public class Program
private static bool clientIdBlobMissing = false; private static bool clientIdBlobMissing = false;
private static bool devicePrivateKeyMissing = false; private static bool devicePrivateKeyMissing = false;
private static Entities.Config? config = null; private readonly Entities.Config config;
private static Auth? auth = null; private Auth? auth = null;
private static LoggingLevelSwitch levelSwitch = new LoggingLevelSwitch(); 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; bool runningInDocker = Environment.GetEnvironmentVariable("OFDL_DOCKER") != null;
@ -103,10 +107,6 @@ public class Program
} }
public static async Task Main(string[] args) public static async Task Main(string[] args)
{
bool cliNonInteractive = false;
try
{ {
levelSwitch.MinimumLevel = LogEventLevel.Error; //set initial level (until we've read from config) levelSwitch.MinimumLevel = LogEventLevel.Error; //set initial level (until we've read from config)
@ -119,6 +119,37 @@ public class Program
AnsiConsole.Markup("Documentation: [link]https://docs.ofdl.tools/[/]\n"); AnsiConsole.Markup("Documentation: [link]https://docs.ofdl.tools/[/]\n");
AnsiConsole.Markup("Discord server: [link]https://discord.com/invite/6bUW8EJ53j[/]\n\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<Program>();
await program.RunAsync(args, cliNonInteractive);
}
private static void ConfigureServices(IServiceCollection services, Entities.Config config)
{
services.AddSingleton(config);
services.AddSingleton<Program>();
}
private static Entities.Config? LoadConfiguration(string[] args, out bool cliNonInteractive)
{
cliNonInteractive = false;
Entities.Config? config = null;
try
{
//Remove config.json and convert to config.conf //Remove config.json and convert to config.conf
if (File.Exists("config.json")) if (File.Exists("config.json"))
{ {
@ -502,7 +533,21 @@ public class Program
Log.Debug(argument); 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; var os = Environment.OSVersion;
Log.Debug($"Operating system information: {os.VersionString}"); Log.Debug($"Operating system information: {os.VersionString}");
@ -668,7 +713,7 @@ public class Program
} }
//Added to stop cookie being filled with un-needed headers //Added to stop cookie being filled with un-needed headers
ValidateCookieString(); ValidateCookieString(auth!);
if (File.Exists("rules.json")) 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) private static async Task DownloadAllData(APIHelper m_ApiHelper, Auth Auth, Entities.Config Config)
{ {
DBHelper dBHelper = new DBHelper(Config); DBHelper dBHelper = new DBHelper(Config);
@ -2959,7 +3003,7 @@ public class Program
foreach (var item in listSelection) foreach (var item in listSelection)
{ {
long listId = lists[item.Replace("[red]", "").Replace("[/]", "")]; long listId = lists[item.Replace("[red]", "").Replace("[/]", "")];
List<string> usernames = await apiHelper.GetListUsers($"/lists/{listId}/users", config); List<string> usernames = await apiHelper.GetListUsers($"/lists/{listId}/users", currentConfig);
foreach (string user in usernames) foreach (string user in usernames)
{ {
listUsernames.Add(user); listUsernames.Add(user);
@ -3456,7 +3500,7 @@ public class Program
return null; return null;
} }
public static void ValidateCookieString() public static void ValidateCookieString(Auth auth)
{ {
string pattern = @"(auth_id=\d+)|(sess=[^;]+)"; string pattern = @"(auth_id=\d+)|(sess=[^;]+)";
var matches = Regex.Matches(auth.COOKIE, pattern); var matches = Regex.Matches(auth.COOKIE, pattern);