-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process.cs
64 lines (61 loc) · 1.83 KB
/
Process.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
#if !NETFRAMEWORK
using System.Runtime.Versioning;
#endif
using System.Security.Principal;
using System.Threading.Tasks;
namespace OpenVPNClient
{
public class Process
{
public static async Task<int> Start(string workingdir, string openvpnoptions, string stdin)
{
#if !NETFRAMEWORK
if (OperatingSystem.IsOSPlatform("windows"))
{
return await StartOnWindows(workingdir, openvpnoptions, stdin);
}
else if (OperatingSystem.IsOSPlatform("linux"))
{
return await StartProcess("openvpn", workingdir, openvpnoptions, stdin);
}
throw new PlatformNotSupportedException();
#else
return await StartOnWindows(workingdir, openvpnoptions, stdin);
#endif
}
#if !NETFRAMEWORK
[SupportedOSPlatform("windows")]
#endif
private static async Task<int> StartOnWindows(string workingdir, string openvpnoptions, string stdin)
{
using (var identity = WindowsIdentity.GetCurrent())
{
var principal = new WindowsPrincipal(identity);
if (principal.IsInRole(WindowsBuiltInRole.Administrator))
{
return await StartProcess($"{Environment.GetEnvironmentVariable("ProgramFiles")}\\OpenVpn\\bin\\openvpn.exe"
, workingdir, openvpnoptions, stdin);
}
else
{
return await InteractiveService.Call(@"\\.\pipe\openvpn\service", workingdir, openvpnoptions, stdin);
}
}
}
private static async Task<int> StartProcess(string filename, string workingdir, string openvpnoptions, string stdin)
{
using (var proc = new System.Diagnostics.Process())
{
proc.StartInfo.FileName = filename;
proc.StartInfo.Arguments = openvpnoptions;
proc.StartInfo.WorkingDirectory = workingdir;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
await proc.StandardInput.WriteLineAsync(stdin);
return proc.Id;
}
}
}
}