Refactor and fixes

This commit is contained in:
Michiel Hazelhof 2025-08-12 13:42:08 +02:00
parent 52fabd9a95
commit 93cae61d66
No known key found for this signature in database
GPG Key ID: EECB9B96355B5EBF

View File

@ -10,30 +10,33 @@ namespace licenseGen;
internal class Program internal class Program
{ {
private static readonly CommandLineApplication App = new();
private static readonly CommandOption Cert = App.Option("--cert", "cert file", CommandOptionType.SingleValue);
private static readonly CommandOption CoreDll = App.Option("--core", "path to core dll", CommandOptionType.SingleValue);
private static Int32 Main(String[] args) private static Int32 Main(String[] args)
{ {
CommandLineApplication app = new(); App.HelpOption("-? | -h | --help");
CommandOption cert = app.Option("--cert", "cert file", CommandOptionType.SingleValue);
CommandOption coreDll = app.Option("--core", "path to core dll", CommandOptionType.SingleValue);
Boolean CertExists() if (!File.Exists(Cert.Value()))
{ {
return File.Exists(cert.Value()); App.Error.WriteLine($"Can't find certificate at: {Cert.Value()}");
App.ShowHelp();
return 1;
}
if (!File.Exists(CoreDll.Value()))
{
App.Error.WriteLine($"Can't find core dll at: {CoreDll.Value()}");
App.ShowHelp();
return 1;
}
if (Cert == null || String.IsNullOrWhiteSpace(Cert.Value()) || CoreDll == null || String.IsNullOrWhiteSpace(CoreDll.Value()))
{
App.ShowHelp();
return 1;
} }
Boolean CoreExists() App.Command("interactive", config =>
{
return File.Exists(coreDll.Value());
}
Boolean VerifyTopOptions()
{
return !String.IsNullOrWhiteSpace(cert.Value()) &&
!String.IsNullOrWhiteSpace(coreDll.Value()) &&
CertExists() && CoreExists();
}
app.Command("interactive", config =>
{ {
String buff, licenseType = "", name = "", email = "", businessName=""; String buff, licenseType = "", name = "", email = "", businessName="";
Int16 storage = 0; Int16 storage = 0;
@ -43,15 +46,6 @@ internal class Program
config.OnExecute(() => config.OnExecute(() =>
{ {
if (!VerifyTopOptions())
{
if (!CoreExists()) config.Error.WriteLine($"Can't find core dll at: {coreDll.Value()}");
if (!CertExists()) config.Error.WriteLine($"Can't find certificate at: {cert.Value()}");
config.ShowHelp();
return 1;
}
Console.WriteLine("Interactive license mode..."); Console.WriteLine("Interactive license mode...");
while (licenseType == "") while (licenseType == "")
@ -59,7 +53,9 @@ internal class Program
Console.WriteLine("What would you like to generate, a [u]ser license or an [o]rg license: "); Console.WriteLine("What would you like to generate, a [u]ser license or an [o]rg license: ");
buff = Console.ReadLine(); buff = Console.ReadLine();
if(buff == "u") switch (buff)
{
case "u":
{ {
licenseType = "user"; licenseType = "user";
Console.WriteLine("Okay, we will generate a user license."); Console.WriteLine("Okay, we will generate a user license.");
@ -72,8 +68,9 @@ internal class Program
if (Guid.TryParse(buff, out guid))validGuid = true; if (Guid.TryParse(buff, out guid))validGuid = true;
else Console.WriteLine("The user-guid provided does not appear to be valid!"); else Console.WriteLine("The user-guid provided does not appear to be valid!");
} }
break;
} }
else if (buff == "o") case "o":
{ {
licenseType = "org"; licenseType = "org";
Console.WriteLine("Okay, we will generate an organization license."); Console.WriteLine("Okay, we will generate an organization license.");
@ -100,10 +97,11 @@ internal class Program
businessName = buff; businessName = buff;
} }
} }
break;
} }
else default:
{
Console.WriteLine("Unrecognized option \'" + buff + "\'."); Console.WriteLine("Unrecognized option \'" + buff + "\'.");
break;
} }
} }
@ -111,7 +109,7 @@ internal class Program
{ {
Console.WriteLine("Please provide the username this license will be registered to. [username]: "); Console.WriteLine("Please provide the username this license will be registered to. [username]: ");
buff = Console.ReadLine(); buff = Console.ReadLine();
if ( CheckUsername(buff) ) name = buff; if (CheckUsername(buff)) name = buff;
} }
while (email == "") while (email == "")
@ -141,39 +139,46 @@ internal class Program
} }
} }
if (licenseType == "user") switch (licenseType)
{
case "user":
{ {
Console.WriteLine("Confirm creation of \"user\" license for username: \"" + name + "\", email: \"" + email + "\", Storage: \"" + storage + " GB\", User-GUID: \"" + guid + "\"? Y/n"); Console.WriteLine("Confirm creation of \"user\" license for username: \"" + name + "\", email: \"" + email + "\", Storage: \"" + storage + " GB\", User-GUID: \"" + guid + "\"? Y/n");
buff = Console.ReadLine(); buff = Console.ReadLine();
if ( buff is "" or "y" or "Y" ) if (buff is "" or "y" or "Y")
{ {
GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, storage, guid, null); GenerateUserLicense(new X509Certificate2(Cert.Value(), "test"), CoreDll.Value(), name, email, storage, guid, null);
} }
else else
{ {
Console.WriteLine("Exiting..."); Console.WriteLine("Exiting...");
return 0; return 0;
} }
break;
} }
else if (licenseType == "org") case "org":
{ {
Console.WriteLine("Confirm creation of \"organization\" license for business name: \"" + businessName + "\", username: \"" + name + "\", email: \"" + email + "\", Storage: \"" + storage + " GB\", Install-ID: \"" + installid + "\"? Y/n"); Console.WriteLine("Confirm creation of \"organization\" license for business name: \"" + businessName + "\", username: \"" + name + "\", email: \"" + email + "\", Storage: \"" + storage + " GB\", Install-ID: \"" + installid + "\"? Y/n");
buff = Console.ReadLine(); buff = Console.ReadLine();
if ( buff is "" or "y" or "Y" ) if (buff is "" or "y" or "Y")
{ {
GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name, email, storage, installid, businessName, null); GenerateOrgLicense(new X509Certificate2(Cert.Value(), "test"), CoreDll.Value(), name, email, storage, installid, businessName, null);
} }
else else
{ {
Console.WriteLine("Exiting..."); Console.WriteLine("Exiting...");
return 0; return 0;
} }
break;
}
} }
return 0; return 0;
}); });
}); });
app.Command("user", config => App.Command("user", config =>
{ {
CommandArgument name = config.Argument("Name", "your name"); CommandArgument name = config.Argument("Name", "your name");
CommandArgument email = config.Argument("Email", "your email"); CommandArgument email = config.Argument("Email", "your email");
@ -183,15 +188,6 @@ internal class Program
config.OnExecute(() => config.OnExecute(() =>
{ {
if (!VerifyTopOptions())
{
if (!CoreExists()) config.Error.WriteLine($"Can't find core dll at: {coreDll.Value()}");
if (!CertExists()) config.Error.WriteLine($"Can't find certificate at: {cert.Value()}");
config.ShowHelp();
return 1;
}
if (String.IsNullOrWhiteSpace(name.Value) || String.IsNullOrWhiteSpace(email.Value)) if (String.IsNullOrWhiteSpace(name.Value) || String.IsNullOrWhiteSpace(email.Value))
{ {
config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}'"); config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}'");
@ -219,12 +215,12 @@ internal class Program
storageShort = (Int16) parsedStorage; storageShort = (Int16) parsedStorage;
} }
GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, storageShort, userId, key.Value); GenerateUserLicense(new X509Certificate2(Cert.Value()!, "test"), CoreDll.Value(), name.Value, email.Value, storageShort, userId, key.Value);
return 0; return 0;
}); });
}); });
app.Command("org", config => App.Command("org", config =>
{ {
CommandArgument name = config.Argument("Name", "your name"); CommandArgument name = config.Argument("Name", "your name");
CommandArgument email = config.Argument("Email", "your email"); CommandArgument email = config.Argument("Email", "your email");
@ -235,18 +231,7 @@ internal class Program
config.OnExecute(() => config.OnExecute(() =>
{ {
if (!VerifyTopOptions()) if (String.IsNullOrWhiteSpace(name.Value) || String.IsNullOrWhiteSpace(email.Value) || String.IsNullOrWhiteSpace(installId.Value))
{
if (!CoreExists()) config.Error.WriteLine($"Can't find core dll at: {coreDll.Value()}");
if (!CertExists()) config.Error.WriteLine($"Can't find certificate at: {cert.Value()}");
config.ShowHelp();
return 1;
}
if (String.IsNullOrWhiteSpace(name.Value) ||
String.IsNullOrWhiteSpace(email.Value) ||
String.IsNullOrWhiteSpace(installId.Value))
{ {
config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}' InstallId='{installId.Value}'"); config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}' InstallId='{installId.Value}'");
config.ShowHelp(true); config.ShowHelp(true);
@ -271,26 +256,24 @@ internal class Program
config.ShowHelp(true); config.ShowHelp(true);
return 1; return 1;
} }
storageShort = (Int16) parsedStorage; storageShort = (Int16)parsedStorage;
} }
GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, storageShort, installationId, businessName.Value, key.Value); GenerateOrgLicense(new X509Certificate2(Cert.Value()!, "test"), CoreDll.Value(), name.Value, email.Value, storageShort, installationId, businessName.Value, key.Value);
return 0; return 0;
}); });
}); });
app.OnExecute(() => App.OnExecute(() =>
{ {
app.ShowHelp(); App.ShowHelp();
return 10; return 10;
}); });
app.HelpOption("-? | -h | --help");
try try
{ {
return app.Execute(args); return App.Execute(args);
} }
catch (Exception e) catch (Exception e)
{ {
@ -351,8 +334,33 @@ internal class Program
Type type = core.GetType("Bit.Core.Billing.Models.Business.UserLicense"); Type type = core.GetType("Bit.Core.Billing.Models.Business.UserLicense");
Type licenseTypeEnum = core.GetType("Bit.Core.Enums.LicenseType"); Type licenseTypeEnum = core.GetType("Bit.Core.Enums.LicenseType");
if (type == null)
{
Console.WriteLine("Could not find type!");
return;
}
if (licenseTypeEnum == null)
{
Console.WriteLine("Could not find license licenseTypeEnum!");
return;
}
Object license = Activator.CreateInstance(type); Object license = Activator.CreateInstance(type);
MethodInfo computeHash = type.GetMethod("ComputeHash");
if (computeHash == null)
{
Console.WriteLine("Could not find ComputeHash!");
return;
}
MethodInfo sign = type.GetMethod("Sign");
if (sign == null)
{
Console.WriteLine("Could not find sign!");
return;
}
Set("LicenseKey", String.IsNullOrWhiteSpace(key) ? Guid.NewGuid().ToString("n") : key); Set("LicenseKey", String.IsNullOrWhiteSpace(key) ? Guid.NewGuid().ToString("n") : key);
Set("Id", userId); Set("Id", userId);
Set("Name", userName); Set("Name", userName);
@ -365,8 +373,8 @@ internal class Program
Set("Expires", DateTime.UtcNow.AddYears(100)); Set("Expires", DateTime.UtcNow.AddYears(100));
Set("Trial", false); Set("Trial", false);
Set("LicenseType", Enum.Parse(licenseTypeEnum, "User")); Set("LicenseType", Enum.Parse(licenseTypeEnum, "User"));
Set("Hash", Convert.ToBase64String((Byte[])type.GetMethod("ComputeHash").Invoke(license, []))); Set("Hash", Convert.ToBase64String(((Byte[])computeHash.Invoke(license, []))!));
Set("Signature", Convert.ToBase64String((Byte[])type.GetMethod("Sign").Invoke(license, [cert]))); Set("Signature", Convert.ToBase64String((Byte[])sign.Invoke(license, [cert])!));
Console.WriteLine(JsonConvert.SerializeObject(license, Formatting.Indented)); Console.WriteLine(JsonConvert.SerializeObject(license, Formatting.Indented));
return; return;
@ -381,12 +389,42 @@ internal class Program
{ {
Assembly core = AssemblyLoadContext.Default.LoadFromAssemblyPath(corePath); Assembly core = AssemblyLoadContext.Default.LoadFromAssemblyPath(corePath);
Type type = core.GetType("Bit.Core.Models.Billing.Business.OrganizationLicense"); Type type = core.GetType("Bit.Core.Billing.Organizations.Models");
Type licenseTypeEnum = core.GetType("Bit.Core.Enums.LicenseType"); Type licenseTypeEnum = core.GetType("Bit.Core.Enums.LicenseType");
Type planTypeEnum = core.GetType("Bit.Core.Billing.Enums.PlanType"); Type planTypeEnum = core.GetType("Bit.Core.Billing.Enums.PlanType");
if (type == null)
{
Console.WriteLine("Could not find type!");
return;
}
if (licenseTypeEnum == null)
{
Console.WriteLine("Could not find licenseTypeEnum!");
return;
}
if (planTypeEnum == null)
{
Console.WriteLine("Could not find planTypeEnum!");
return;
}
Object license = Activator.CreateInstance(type); Object license = Activator.CreateInstance(type);
MethodInfo computeHash = type.GetMethod("ComputeHash");
if (computeHash == null)
{
Console.WriteLine("Could not find ComputeHash!");
return;
}
MethodInfo sign = type.GetMethod("Sign");
if (sign == null)
{
Console.WriteLine("Could not find sign!");
return;
}
Set("LicenseKey", String.IsNullOrWhiteSpace(key) ? Guid.NewGuid().ToString("n") : key); Set("LicenseKey", String.IsNullOrWhiteSpace(key) ? Guid.NewGuid().ToString("n") : key);
Set("InstallationId", instalId); Set("InstallationId", instalId);
Set("Id", Guid.NewGuid()); Set("Id", Guid.NewGuid());
@ -431,8 +469,8 @@ internal class Program
Set("UseRiskInsights", true); Set("UseRiskInsights", true);
Set("UseOrganizationDomains", true); Set("UseOrganizationDomains", true);
Set("UseAdminSponsoredFamilies", true); Set("UseAdminSponsoredFamilies", true);
Set("Hash", Convert.ToBase64String((Byte[])type.GetMethod("ComputeHash").Invoke(license, []))); Set("Hash", Convert.ToBase64String((Byte[])computeHash.Invoke(license, [])!));
Set("Signature", Convert.ToBase64String((Byte[])type.GetMethod("Sign").Invoke(license, [cert]))); Set("Signature", Convert.ToBase64String((Byte[])sign.Invoke(license, [cert])!));
Console.WriteLine(JsonConvert.SerializeObject(license, Formatting.Indented)); Console.WriteLine(JsonConvert.SerializeObject(license, Formatting.Indented));
return; return;