Skip to content

Commit

Permalink
Load config string from command line (Windows service) (#11)
Browse files Browse the repository at this point in the history
* Update Helpers.cs

* Update Program.cs

* Update Program.cs

* Load config from command line (non-Win service)

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update Program.cs

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update Program.cs
  • Loading branch information
artemlos authored Apr 25, 2024
1 parent e11fc41 commit 08c2a31
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 15 deletions.
21 changes: 21 additions & 0 deletions LicenseServer/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,27 @@ public static LicenseServerConfiguration ReadFromEnvironmentVariables(LicenseSer

return lsc;
}

public static Dictionary<string,string> ProcessCommandLineArgs(string[] args, Action<string> updates)
{
var result = new Dictionary<string, string>();

for (int i = 0; i < args.Length; i += 2)
{
if (args[i].StartsWith("-") && i + 1 < args.Length)
{
string key = args[i].Substring(1);
string value = args[i + 1];
result[key] = value;
}
else
{
updates($"Warning: Ignored invalid or incomplete argument pair starting at index {i}");
}
}

return result;
}
}

public enum APIMethod
Expand Down
109 changes: 100 additions & 9 deletions LicenseServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ namespace LicenseServer
{
class Program
{
public const string versionInfo = "v2.12-rc2 (2023-03-23)" ;
public const string versionInfo = "v2.13 (2024-04-25)" ;

public const string ServiceName = "license-server";

public class Service : ServiceBase
{
public Service()
public string[] args;
public Service(string[] args)
{
ServiceName = Program.ServiceName;

Expand All @@ -44,13 +45,14 @@ public Service()
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
this.args = args;
}

protected override void OnStart(string[] args)
{
//Program.Start(args);
Program.Initialization(args, runAsService: true);
EventLog.WriteEntry("");
Program.Initialization(this.args, runAsService: true, x=> EventLog.WriteEntry(x),
x => EventLog.WriteEntry(x, System.Diagnostics.EventLogEntryType.Error));
}

protected override void OnStop()
Expand Down Expand Up @@ -83,8 +85,10 @@ static void Main(string[] args)
{
if (!Environment.UserInteractive)
{
using (var service = new Service())
using (var service = new Service(args))
{
ServiceBase.Run(service);
}
}
else
{
Expand All @@ -93,20 +97,33 @@ static void Main(string[] args)

}

public static void Initialization(string[] args, bool runAsService = false)
public static void Initialization(string[] args, bool runAsService = false, Action<string> writeMessage = null, Action<string> writeError = null)
{
Console.WriteLine($"Cryptolens License Server {versionInfo}\n");

var envconfigstring = System.Environment.GetEnvironmentVariable("cryptolens_configurationstring");

if (!string.IsNullOrEmpty(ConfigurationFromCryptolens) || runAsService || !string.IsNullOrEmpty(envconfigstring))
var arguments = Helpers.ProcessCommandLineArgs(args, WriteMessage);

if (!string.IsNullOrEmpty(ConfigurationFromCryptolens) || runAsService || !string.IsNullOrEmpty(envconfigstring)
|| arguments != null && arguments.ContainsKey("config"))
{
LicenseServerConfiguration config = null;

if (string.IsNullOrEmpty(ConfigurationFromCryptolens) && !string.IsNullOrEmpty(envconfigstring))
{
config = Helpers.ReadConfiguration(envconfigstring, Constants.RSAPubKey);
}
else if(string.IsNullOrEmpty(ConfigurationFromCryptolens) && args != null)
{
var parameters = Helpers.ProcessCommandLineArgs(args, WriteMessage);

string configString = "";
if(parameters!= null && parameters.TryGetValue("config", out configString))
{
config = Helpers.ReadConfiguration(configString, Constants.RSAPubKey);
}
}
else
{
config = Helpers.ReadConfiguration(ConfigurationFromCryptolens, Constants.RSAPubKey);
Expand All @@ -116,32 +133,68 @@ public static void Initialization(string[] args, bool runAsService = false)
{
WriteMessage($"Configuration data could not be read. Attempting to read environment variables.");

if(runAsService)
{
writeError($"Configuration data could not be read. Attempting to read environment variables.");
}

config = Helpers.ReadFromEnvironmentVariables(null);
}
else
{
if (config.PathToConfigFile == "USE_ENVIRONMENT_VARIABLES")
{
WriteMessage("Attempting to read environment variables.");
if (runAsService)
{
writeMessage("Attempting to read environment variables.");
}
config = Helpers.ReadFromEnvironmentVariables(config);
}
}

if (config.ValidUntil < DateTimeOffset.UtcNow)
{
WriteMessage($"Configuration data is outdated. Please contact the vendor to receive a new version of the license server.");
if (runAsService)
{
writeError($"Configuration data is outdated. Please contact the vendor to receive a new version of the license server.");
}
return;
}

cacheLength = config.CacheLength;
WriteMessage($"Cache length set to {cacheLength}");

if (runAsService)
{
writeMessage($"Cache length set to {cacheLength}");
}

port = config.Port;
WriteMessage($"Port set to {port}");

if (runAsService)
{
writeMessage($"Port set to {port}");
}

attemptToRefresh = !config.OfflineMode;
WriteMessage($"Offline mode is set to {!attemptToRefresh}");

if (runAsService)
{
writeMessage($"Offline mode is set to {!attemptToRefresh}");
}

localFloatingServer = config.LocalFloatingServer;
WriteMessage($"Local floating license server is set to {localFloatingServer}");

if (runAsService)
{
writeMessage($"Local floating license server is set to {localFloatingServer}");
}

RSAServerKey = config.ServerKey;
RSAPublicKey = config.RSAPublicKey;

Expand All @@ -153,6 +206,10 @@ public static void Initialization(string[] args, bool runAsService = false)
{
string result = Helpers.LoadLicenseFromPath(licenseCache, keysToUpdate, file, WriteMessage) ? "Processed" : "Error";
WriteMessage($"Path '{file}' {result}");
if (runAsService)
{
writeError($"Path '{file}' {result}");
}
}
}

Expand All @@ -168,6 +225,10 @@ public static void Initialization(string[] args, bool runAsService = false)
{
port = configData.Port;
WriteMessage($"Port changed to {port}.");
if (runAsService)
{
writeMessage($"Port changed to {port}.");
}
}

if(configData.ActivationFiles != null)
Expand All @@ -176,19 +237,25 @@ public static void Initialization(string[] args, bool runAsService = false)
{
string result = Helpers.LoadLicenseFromPath(licenseCache, keysToUpdate, file, WriteMessage) ? "Processed" : "Error";
WriteMessage($"Path '{file}' {result}");
if (runAsService)
{
writeMessage($"Path '{file}' {result}");
}
}
}
}
catch (Exception ex)
{
WriteMessage($"Config file {config.PathToConfigFile} could not be read. Detailed error message {ex.Message}");
if (runAsService)
{
writeError($"Config file {config.PathToConfigFile} could not be read. Detailed error message {ex.Message}");
}
}
}
}
else
{


try
{
var config = Newtonsoft.Json.JsonConvert.DeserializeObject<Config>(System.IO.File.ReadAllText((Path.Combine(Directory.GetCurrentDirectory(), "config.json"))));
Expand Down Expand Up @@ -324,24 +391,48 @@ public static void Initialization(string[] args, bool runAsService = false)
httpListener.Prefixes.Add($"http://+:{port}/");
httpListener.Start();
WriteMessage("Starting server...");
if (runAsService)
{
writeMessage("Starting server...");
}

}
catch (Exception ex)
{
WriteMessage($"Error: Please make sure that the license server runs as an administrator " +
$"and that there is no other application that is listening to port {port}.\n\n" +
$"Detailed error shown below: {ex.StackTrace.ToString()}");
if (runAsService)
{
writeError($"Error: Please make sure that the license server runs as an administrator " +
$"and that there is no other application that is listening to port {port}.\n\n" +
$"Detailed error shown below: {ex.StackTrace.ToString()}");
}
Console.ReadLine();
return;
}
WriteMessage("Server started.");
if (runAsService)
{
writeMessage("Server started.");
}

try
{
WriteMessage($"Server address is: {GetLocalIPAddress()}:{port}");
if (runAsService)
{
writeMessage($"Server address is: {GetLocalIPAddress()}:{port}");
}
}
catch (Exception ex)
{
WriteMessage("Could not get the IP of the license server.");
if (runAsService)
{
writeError("Could not get the IP of the license server.");
}

}

if (cacheLength > 0)
Expand Down
Loading

0 comments on commit 08c2a31

Please sign in to comment.