Skip to content

Commit

Permalink
adding option to select http port in standalone mode
Browse files Browse the repository at this point in the history
preparing for 1.1.4 release
  • Loading branch information
aloopkin committed Oct 21, 2019
1 parent 80e9fb1 commit 7f52b3f
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 12 deletions.
4 changes: 2 additions & 2 deletions MSIPackaging/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.3.0")]
[assembly: AssemblyFileVersion("1.1.3.0")]
[assembly: AssemblyVersion("1.1.4.0")]
[assembly: AssemblyFileVersion("1.1.4.0")]
2 changes: 1 addition & 1 deletion MSIPackaging/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static public void Main(string[] args)
}
);
project.GUID = new Guid("bb0a8e11-24a8-4d7e-a7d6-6fc5bd8166d2");
project.Version = Version.Parse("1.1.3");
project.Version = Version.Parse("1.1.4");
project.LicenceFile = path + @"\MSIPackaging\Resources\gpl-3.0.rtf";
project.BannerImage = path + @"\MSIPackaging\Resources\banner.png";
project.BackgroundImage = path + @"\MSIPackaging\Resources\background.png";
Expand Down
4 changes: 2 additions & 2 deletions WinCertes/ChallengeValidator/HTTPChallengeValidatorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class HTTPChallengeValidatorFactory
/// <param name="standalone">true if we use the built-in webserver, false otherwise</param>
/// <param name="webRoot">the full path to the web server root, when not using built-in</param>
/// <returns>the HTTP challenge Validator</returns>
public static IHTTPChallengeValidator GetHTTPChallengeValidator(bool standalone, string webRoot = null)
public static IHTTPChallengeValidator GetHTTPChallengeValidator(bool standalone, int httpPort, string webRoot = null)
{
IHTTPChallengeValidator challengeValidator = null;
if (standalone) {
if (!CheckAvailableServerPort(80)) return null;
challengeValidator = new HTTPChallengeWebServerValidator();
challengeValidator = new HTTPChallengeWebServerValidator(httpPort);
} else if (webRoot != null) {
challengeValidator = new HTTPChallengeFileValidator(webRoot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ class HTTPChallengeWebServerValidator : IHTTPChallengeValidator
private Thread _serverThread;
private HttpListener _listener;
private string _tokenContents;
private int httpPort;

private void Listen()
{
try {
_listener = new HttpListener();
_listener.Prefixes.Add("http://*:80/");
_listener.Prefixes.Add("http://*:"+this.httpPort+"/");
_listener.Start();
logger.Debug("Started Listener on port 80");
logger.Debug("Started Listener on port "+this.httpPort);
while (true) {
try {
HttpListenerContext context = _listener.GetContext();
Expand Down Expand Up @@ -55,8 +56,9 @@ private void Process(HttpListenerContext context)
/// Class constructor. Starts the simple web server on port 80.
/// HTTPChallengeWebServerValidator.Stop() MUST be called after use.
/// </summary>
public HTTPChallengeWebServerValidator()
public HTTPChallengeWebServerValidator(int httpPort)
{
this.httpPort = httpPort;
try {
_serverThread = new Thread(this.Listen) {
IsBackground = true
Expand Down
8 changes: 8 additions & 0 deletions WinCertes/Config/IConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ interface IConfig
/// <returns></returns>
int ReadIntParameter(string parameter, int defaultValue = 0);

/// <summary>
/// Tries to read parameter value from configuration. If it does not exist, uses provided value instead, and writes it to configuration
/// </summary>
/// <param name="parameter">the configuration parameter to manage</param>
/// <param name="value">the default value is parameter does not exist in configuration</param>
/// <returns>the value of the configuration parameter</returns>
int ReadOrWriteIntParameter(string parameter, int value);

/// <summary>
/// Tries to read parameter value from configuration. If it does not exist, uses provided value instead, and writes it to configuration
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions WinCertes/Config/RegistryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ public int ReadIntParameter(string parameter, int defaultValue = 0)
return (int)Registry.GetValue(_registryKey, parameter, defaultValue);
}

/// <summary>
/// Tries to read parameter value from configuration. If it does not exist, uses provided value instead, and writes it to configuration
/// </summary>
/// <param name="parameter">the configuration parameter to manage</param>
/// <param name="value">the default value is parameter does not exist in configuration</param>
/// <returns>the value of the configuration parameter</returns>
public int ReadOrWriteIntParameter(string parameter, int value)
{
int myValue = ReadIntParameter(parameter, 0);
if (myValue == 0)
{
WriteIntParameter(parameter, value);
}
return ReadIntParameter(parameter);
}

/// <summary>
/// Writes integer parameter into configuration
/// </summary>
Expand Down
9 changes: 7 additions & 2 deletions WinCertes/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public WinCertesOptions()
Revoke = -1;
Csp = null;
RenewalDelay = 30;
HttpPort = 80;
}
public string ServiceUri { get; set; }
public string Email { get; set; }
Expand All @@ -39,6 +40,7 @@ public WinCertesOptions()
public int Revoke { get; set; }
public string Csp { get; set; }
public int RenewalDelay { get; set; }
public int HttpPort { get; set; }

/// <summary>
/// Writes command line parameters into the specified config
Expand All @@ -61,6 +63,8 @@ public void WriteOptionsIntoConfiguration(IConfig config)
ScriptFile = config.WriteAndReadStringParameter("scriptFile", ScriptFile);
// Writing renewal delay to conf
config.WriteIntParameter("renewalDays", RenewalDelay);
// Writing HTTP listening Port in conf
HttpPort = config.ReadOrWriteIntParameter("httpPort", HttpPort);
} catch (Exception e) {
_logger.Error($"Could not Read/Write command line parameters to configuration: {e.Message}");
}
Expand Down Expand Up @@ -100,7 +104,8 @@ private static bool HandleOptions(string[] args)
{ "a|standalone", "should WinCertes create its own WebServer for validation. Activates HTTP validation mode. WARNING: it will use port 80", v => _winCertesOptions.Standalone = (v != null) },
{ "r|revoke:", "should WinCertes revoke the certificate identified by its domains (to be used only with -d). {REASON} is an optional integer between 0 and 5.", (int v) => _winCertesOptions.Revoke = v },
{ "k|csp=", "import the certificate into specified csp. By default WinCertes imports in the default CSP.", v => _winCertesOptions.Csp = v },
{ "t|renewal=", "trigger certificate renewal {N} days before expiration", (int v) => _winCertesOptions.RenewalDelay = v }
{ "t|renewal=", "trigger certificate renewal {N} days before expiration", (int v) => _winCertesOptions.RenewalDelay = v },
{ "l|listenport=", "listen on port {N} in standalone mode (for use with -a switch, default 80)", (int v) => _winCertesOptions.HttpPort = v }
};

// and the handling of these options
Expand Down Expand Up @@ -256,7 +261,7 @@ static void Main(string[] args)
if (!IsThereCertificateAndIsItToBeRenewed(_domains)) { Utils.CreateScheduledTask(taskName, _domains); return; }

// Now the real stuff: we register the order for the domains, and have them validated by the ACME service
IHTTPChallengeValidator httpChallengeValidator = HTTPChallengeValidatorFactory.GetHTTPChallengeValidator(_winCertesOptions.Standalone, _winCertesOptions.WebRoot);
IHTTPChallengeValidator httpChallengeValidator = HTTPChallengeValidatorFactory.GetHTTPChallengeValidator(_winCertesOptions.Standalone, _winCertesOptions.HttpPort, _winCertesOptions.WebRoot);
IDNSChallengeValidator dnsChallengeValidator = DNSChallengeValidatorFactory.GetDNSChallengeValidator(_config);
if ((httpChallengeValidator == null) && (dnsChallengeValidator == null)) { WriteErrorMessageWithUsage(_options, "Specify either an HTTP or a DNS validation method."); return; }
if (!(Task.Run(() => _certesWrapper.RegisterNewOrderAndVerify(_domains, httpChallengeValidator, dnsChallengeValidator)).GetAwaiter().GetResult())) { if (httpChallengeValidator != null) httpChallengeValidator.EndAllChallengeValidations(); return; }
Expand Down
4 changes: 2 additions & 2 deletions WinCertes/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.3.0")]
[assembly: AssemblyFileVersion("1.1.3.0")]
[assembly: AssemblyVersion("1.1.4.0")]
[assembly: AssemblyFileVersion("1.1.4.0")]

0 comments on commit 7f52b3f

Please sign in to comment.