2017-10-19 18:51:05 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2017-10-29 20:13:38 +01:00
|
|
|
|
using System.Runtime.Loader;
|
2017-10-19 18:51:05 +02:00
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2017-10-29 20:13:38 +01:00
|
|
|
|
using Microsoft.Extensions.CommandLineUtils;
|
|
|
|
|
using Newtonsoft.Json;
|
2017-10-19 18:51:05 +02:00
|
|
|
|
|
|
|
|
|
namespace bitwardenSelfLicensor
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
2017-10-22 19:08:50 +02:00
|
|
|
|
static int Main(string[] args)
|
2017-10-19 18:51:05 +02:00
|
|
|
|
{
|
2017-10-29 20:13:38 +01:00
|
|
|
|
var app = new Microsoft.Extensions.CommandLineUtils.CommandLineApplication();
|
2017-10-29 20:44:54 +01:00
|
|
|
|
var cert = app.Option("--cert", "cert file", CommandOptionType.SingleValue);
|
2017-10-29 20:13:38 +01:00
|
|
|
|
var coreDll = app.Option("--core", "path to core dll", CommandOptionType.SingleValue);
|
|
|
|
|
|
2017-10-29 20:44:54 +01:00
|
|
|
|
bool certExists() {
|
|
|
|
|
return File.Exists(cert.Value());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool coreExists() {
|
|
|
|
|
return File.Exists(coreDll.Value());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool verifyTopOptions()
|
|
|
|
|
{
|
|
|
|
|
return !string.IsNullOrWhiteSpace(cert.Value()) &&
|
|
|
|
|
!string.IsNullOrWhiteSpace(coreDll.Value()) &&
|
|
|
|
|
certExists() && coreExists();
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-29 20:13:38 +01:00
|
|
|
|
app.Command("user", config =>
|
|
|
|
|
{
|
|
|
|
|
var name = config.Argument("Name", "your name");
|
|
|
|
|
var email = config.Argument("Email", "your email");
|
|
|
|
|
var key = config.Argument("Key", "your key id (optional)");
|
|
|
|
|
var help = config.HelpOption("--help | -h | -?");
|
|
|
|
|
|
|
|
|
|
config.OnExecute(() =>
|
|
|
|
|
{
|
2017-10-29 20:44:54 +01:00
|
|
|
|
if (!verifyTopOptions())
|
2017-10-29 20:13:38 +01:00
|
|
|
|
{
|
2017-10-29 20:44:54 +01:00
|
|
|
|
if(!coreExists())
|
|
|
|
|
{
|
|
|
|
|
config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}");
|
|
|
|
|
}
|
|
|
|
|
if (!certExists()) {
|
|
|
|
|
config.Error.WriteLine($"Cant find certificate at: {cert.Value()}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config.ShowHelp();
|
2017-10-29 20:13:38 +01:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else if (string.IsNullOrWhiteSpace(name.Value) || string.IsNullOrWhiteSpace(email.Value))
|
|
|
|
|
{
|
2017-10-29 20:44:54 +01:00
|
|
|
|
config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}'");
|
2017-10-29 20:13:38 +01:00
|
|
|
|
config.ShowHelp("user");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GenerateUserLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, key.Value);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
app.Command("org", config =>
|
|
|
|
|
{
|
|
|
|
|
var name = config.Argument("Name", "your name");
|
|
|
|
|
var email = config.Argument("Email", "your email");
|
|
|
|
|
var installId = config.Argument("InstallId", "your installation id (GUID)");
|
|
|
|
|
var key = config.Argument("Key", "your key id (optional)");
|
|
|
|
|
var help = config.HelpOption("--help | -h | -?");
|
|
|
|
|
|
|
|
|
|
config.OnExecute(() =>
|
|
|
|
|
{
|
2017-10-29 20:44:54 +01:00
|
|
|
|
if (!verifyTopOptions())
|
2017-10-29 20:13:38 +01:00
|
|
|
|
{
|
2017-10-29 20:44:54 +01:00
|
|
|
|
if(!coreExists())
|
|
|
|
|
{
|
|
|
|
|
config.Error.WriteLine($"Cant find core dll at: {coreDll.Value()}");
|
|
|
|
|
}
|
|
|
|
|
if (!certExists()) {
|
|
|
|
|
config.Error.WriteLine($"Cant find certificate at: {cert.Value()}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
config.ShowHelp();
|
2017-10-29 20:13:38 +01:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else if (string.IsNullOrWhiteSpace(name.Value) ||
|
|
|
|
|
string.IsNullOrWhiteSpace(email.Value) ||
|
|
|
|
|
string.IsNullOrWhiteSpace(installId.Value))
|
|
|
|
|
{
|
2017-10-29 20:44:54 +01:00
|
|
|
|
config.Error.WriteLine($"Some arguments are missing: Name='{name.Value}' Email='{email.Value}' InstallId='{installId.Value}'");
|
2017-10-29 20:13:38 +01:00
|
|
|
|
config.ShowHelp("org");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Guid.TryParse(installId.Value, out Guid installationId))
|
|
|
|
|
{
|
|
|
|
|
config.Error.WriteLine("Unable to parse your installation id as a GUID");
|
2017-10-29 20:44:54 +01:00
|
|
|
|
config.Error.WriteLine($"Here's a new guid: {Guid.NewGuid()}");
|
2017-10-29 20:13:38 +01:00
|
|
|
|
config.ShowHelp("org");
|
2017-10-29 20:44:54 +01:00
|
|
|
|
return 1;
|
2017-10-29 20:13:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-29 20:18:22 +01:00
|
|
|
|
GenerateOrgLicense(new X509Certificate2(cert.Value(), "test"), coreDll.Value(), name.Value, email.Value, installationId, key.Value);
|
2017-10-29 20:13:38 +01:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.OnExecute(() =>
|
|
|
|
|
{
|
|
|
|
|
app.ShowHelp();
|
|
|
|
|
return 10;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.HelpOption("-? | -h | --help");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return app.Execute(args);
|
2017-10-19 18:51:05 +02:00
|
|
|
|
}
|
2017-10-29 20:13:38 +01:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("Oops: {0}", e);
|
|
|
|
|
return 100;
|
2017-10-19 18:51:05 +02:00
|
|
|
|
}
|
2017-10-29 20:13:38 +01:00
|
|
|
|
}
|
2017-10-19 18:51:05 +02:00
|
|
|
|
|
2017-10-29 20:13:38 +01:00
|
|
|
|
static void GenerateUserLicense(X509Certificate2 cert, string corePath,
|
|
|
|
|
string userName, string email, string key)
|
|
|
|
|
{
|
|
|
|
|
var core = AssemblyLoadContext.Default.LoadFromAssemblyPath(corePath);
|
2017-10-19 18:51:05 +02:00
|
|
|
|
|
2017-10-29 20:13:38 +01:00
|
|
|
|
var type = core.GetType("Bit.Core.Models.Business.UserLicense");
|
2017-10-19 18:51:05 +02:00
|
|
|
|
|
2017-10-29 20:13:38 +01:00
|
|
|
|
var license = Activator.CreateInstance(type);
|
2017-10-19 18:51:05 +02:00
|
|
|
|
|
2017-10-29 20:13:38 +01:00
|
|
|
|
void set(string name, object value)
|
|
|
|
|
{
|
|
|
|
|
type.GetProperty(name).SetValue(license, value);
|
|
|
|
|
}
|
2017-10-19 18:51:05 +02:00
|
|
|
|
|
2017-10-29 20:13:38 +01:00
|
|
|
|
set("LicenseKey", string.IsNullOrWhiteSpace(key) ? Guid.NewGuid().ToString("n") : key);
|
|
|
|
|
set("Id", Guid.NewGuid());
|
|
|
|
|
set("Name", userName);
|
|
|
|
|
set("Email", email);
|
|
|
|
|
set("MaxStorageGb", short.MaxValue);
|
|
|
|
|
set("Premium", true);
|
|
|
|
|
set("Version", 1);
|
|
|
|
|
set("Issued", DateTime.UtcNow);
|
|
|
|
|
set("Refresh", DateTime.UtcNow.AddYears(1).AddMonths(-1));
|
|
|
|
|
set("Expires", DateTime.UtcNow.AddYears(1));
|
|
|
|
|
set("Trial", false);
|
|
|
|
|
|
|
|
|
|
set("Hash", Convert.ToBase64String((byte[])type.GetMethod("ComputeHash").Invoke(license, new object[0])));
|
|
|
|
|
set("Signature", Convert.ToBase64String((byte[])type.GetMethod("Sign").Invoke(license, new object[] { cert })));
|
|
|
|
|
|
2017-10-29 20:44:54 +01:00
|
|
|
|
Console.WriteLine(JsonConvert.SerializeObject(license, Formatting.Indented));
|
2017-10-29 20:13:38 +01:00
|
|
|
|
}
|
2017-10-19 18:51:05 +02:00
|
|
|
|
|
2017-10-29 20:13:38 +01:00
|
|
|
|
static void GenerateOrgLicense(X509Certificate2 cert, string corePath,
|
|
|
|
|
string userName, string email, Guid instalId, string key)
|
|
|
|
|
{
|
|
|
|
|
var core = AssemblyLoadContext.Default.LoadFromAssemblyPath(corePath);
|
2017-10-19 18:51:05 +02:00
|
|
|
|
|
2017-10-29 20:13:38 +01:00
|
|
|
|
var type = core.GetType("Bit.Core.Models.Business.OrganizationLicense");
|
2017-10-19 18:51:05 +02:00
|
|
|
|
|
2017-10-29 20:13:38 +01:00
|
|
|
|
var license = Activator.CreateInstance(type);
|
2017-10-19 18:51:05 +02:00
|
|
|
|
|
2017-10-29 20:13:38 +01:00
|
|
|
|
void set(string name, object value)
|
|
|
|
|
{
|
|
|
|
|
type.GetProperty(name).SetValue(license, value);
|
2017-10-19 18:51:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-29 20:13:38 +01:00
|
|
|
|
set("LicenseKey", string.IsNullOrWhiteSpace(key) ? Guid.NewGuid().ToString("n") : key);
|
|
|
|
|
set("InstallationId", instalId);
|
|
|
|
|
set("Id", Guid.NewGuid());
|
|
|
|
|
set("Name", userName);
|
|
|
|
|
set("BillingEmail", email);
|
|
|
|
|
set("BusinessName", "BitBetter");
|
|
|
|
|
set("Enabled", true);
|
|
|
|
|
set("Seats", (short)5);
|
|
|
|
|
set("MaxCollections", short.MaxValue);
|
|
|
|
|
set("MaxStorageGb", short.MaxValue);
|
|
|
|
|
set("SelfHost", true);
|
|
|
|
|
set("UseGroups", true);
|
|
|
|
|
set("UseDirectory", true);
|
|
|
|
|
set("UseTotp", true);
|
|
|
|
|
set("PlanType", (byte)6);
|
|
|
|
|
set("Plan", "Custom");
|
|
|
|
|
set("Version", 1);
|
|
|
|
|
set("Issued", DateTime.UtcNow);
|
|
|
|
|
set("Refresh", DateTime.UtcNow.AddYears(1).AddMonths(-1));
|
|
|
|
|
set("Expires", DateTime.UtcNow.AddYears(1));
|
|
|
|
|
set("Trial", false);
|
|
|
|
|
|
|
|
|
|
set("Hash", Convert.ToBase64String((byte[])type.GetMethod("ComputeHash").Invoke(license, new object[0])));
|
|
|
|
|
set("Signature", Convert.ToBase64String((byte[])type.GetMethod("Sign").Invoke(license, new object[] { cert })));
|
|
|
|
|
|
2017-10-29 20:44:54 +01:00
|
|
|
|
Console.WriteLine(JsonConvert.SerializeObject(license, Formatting.Indented));
|
2017-10-19 18:51:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|