Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change startup to allow arguments and handle new -BaseRegName option #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 66 additions & 20 deletions Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class OpenVpnService : System.ServiceProcess.ServiceBase
public const string Package = "openvpn";
private List<OpenVpnChild> Subprocesses;

public OpenVpnService()
public OpenVpnService(string[] args)
{
this.ServiceName = DefaultServiceName;
this.CanStop = true;
Expand All @@ -30,11 +30,19 @@ public OpenVpnService()
this.AutoLog = true;

this.Subprocesses = new List<OpenVpnChild>();

// note, args in OnStart is only what is set as "start parameters"
// which is only used when manually starting the service and never saved
ParseArgs(args);
}

protected override void OnStop()
{
RequestAdditionalTime(3000);
if (!Environment.UserInteractive)
{
// throws exception unless running as service
RequestAdditionalTime(3000);
}
foreach (var child in Subprocesses)
{
child.SignalProcess();
Expand All @@ -48,12 +56,14 @@ protected override void OnStop()
}
}

private string ServiceBaseName = "OpenVPN";

private RegistryKey GetRegistrySubkey(RegistryView rView)
{
try
{
return RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, rView)
.OpenSubKey("Software\\OpenVPN");
.OpenSubKey(@"Software\\" + ServiceBaseName);
}
catch (ArgumentException)
{
Expand All @@ -65,19 +75,32 @@ private RegistryKey GetRegistrySubkey(RegistryView rView)
}
}

private void ParseArgs(string[] args)
{
string prevArg = null;
foreach (var arg in args)
{
var curPrev = prevArg;
prevArg = arg;
switch(curPrev)
{
case "-BaseRegName":
ServiceBaseName = arg;
break;
}
}
}

protected override void OnStart(string[] args)
{
try
{
List<RegistryKey> rkOvpns = new List<RegistryKey>();

// Search 64-bit registry, then 32-bit registry for OpenVpn
var key = GetRegistrySubkey(RegistryView.Registry64);
if (key != null) rkOvpns.Add(key);
key = GetRegistrySubkey(RegistryView.Registry32);
if (key != null) rkOvpns.Add(key);
var rkOvpns = (new[] { RegistryView.Registry64, RegistryView.Registry32 })
.Select(GetRegistrySubkey)
.Where(k => k != null).ToList();

if (rkOvpns.Count() == 0)
if (rkOvpns.Count == 0)
throw new Exception("Registry key missing");

var configDirsConsidered = new HashSet<string>();
Expand Down Expand Up @@ -147,7 +170,7 @@ protected override void OnStart(string[] args)
catch (Exception e)
{
EventLog.WriteEntry("Exception occured during OpenVPN service start: " + e.Message + e.StackTrace);
throw e;
throw;
}
}

Expand Down Expand Up @@ -179,15 +202,13 @@ private System.Diagnostics.ProcessPriorityClass GetPriorityClass(string priority

public static int Main(string[] args)
{
if (args.Length == 0)
{
Run(new OpenVpnService());
}
else if (args[0] == "-install")
var arg0 = args.Length == 0 ? null : args[0];
if (arg0 == "-install")
{
try
{
ProjectInstaller.Install();
return 0;
}
catch (Exception e)
{
Expand All @@ -196,12 +217,13 @@ public static int Main(string[] args)
return 1;
}
}
else if (args[0] == "-remove")
else if (arg0 == "-remove")
{
try
{
ProjectInstaller.Stop();
ProjectInstaller.Uninstall();
return 0;
}
catch (Exception e)
{
Expand All @@ -212,10 +234,34 @@ public static int Main(string[] args)
}
else
{
Console.Error.WriteLine("Unknown command: " + args[0]);
return 1;
var servicesToRun = new OpenVpnService[]
{
new OpenVpnService(args)
};

if (Environment.UserInteractive)
{
// if not started as service - we start it in debugable mode instead
foreach (var svc in servicesToRun)
{
// OnStart args is temporary, always use constructor instead
svc.OnStart(args);
}

Console.WriteLine("Press enter to start shutdown");
Console.ReadLine();

foreach (var svc in servicesToRun)
{
svc.OnStop();
}

return 0;
}

Run(servicesToRun);
return 0;
}
return 0;
}

}
Expand Down