forked from jochenwierum/openvpn-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserSpaceService.cs
More file actions
191 lines (166 loc) · 5.94 KB
/
UserSpaceService.cs
File metadata and controls
191 lines (166 loc) · 5.94 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Diagnostics;
using System.Globalization;
using System.Security.Principal;
namespace OpenVPNUtils
{
/// <summary>
/// controls a openvpn binary
/// </summary>
internal class UserSpaceService : IDisposable
{
#region variables
/// <summary>
/// information about the OpenVPN binary process start
/// </summary>
private ProcessStartInfo m_psi = new ProcessStartInfo();
/// <summary>
/// information about the OpenVPN binary process
/// </summary>
private Process m_process;
/// <summary>
/// log manager which is used to write log lines to
/// </summary>
private LogManager m_logs;
/// <summary>
/// saves, whether the OpenVPN Process is running
/// </summary>
private bool running;
#endregion
#region events
/// <summary>
/// fired when process closes
/// </summary>
public event EventHandler serviceExited;
#endregion
/// <summary>
/// Initialize a new OpenVPN service.
/// </summary>
/// <param name="binfile">path to openvpn</param>
/// <param name="configfile">path to openvpn config</param>
/// <param name="dir">directory where config lies</param>
/// <param name="logs">provider to write logs to</param>
/// <param name="host">The host to connect to (e.g. 127.0.0.1)</param>
/// <param name="port">The port to connect to</param>
/// <param name="logfile">file to write OpenVPN log to</param>
/// <param name="smartCardSupport">enable SmartCard support</param>
public UserSpaceService(string binfile, string configfile,
string dir, LogManager logs, string host, int port,
string logfile, bool smartCardSupport)
{
m_logs = logs;
m_psi.FileName = binfile;
m_psi.WorkingDirectory = dir;
m_psi.WindowStyle = ProcessWindowStyle.Hidden;
m_psi.UseShellExecute = true;
/*WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
principal.IsInRole(WindowsBuiltInRole.Administrator)*/
if (Environment.OSVersion.Version.Major >= 6)
m_psi.Verb = "runas";
m_psi.CreateNoWindow = true;
m_psi.Arguments =
(logfile != null ? "--log \"" + logfile + "\"" : "") +
" --config \"" + configfile + "\"" +
" --management " + host + " " + port.ToString(CultureInfo.InvariantCulture) +
" --management-query-passwords" +
" --management-hold" +
" --management-signal" +
" --management-forget-disconnect" +
" --auth-retry interact";
if (smartCardSupport)
m_psi.Arguments += " --pkcs11-id-management";
}
/// <summary>
/// Start the OpenVPN binary.
/// </summary>
public void Start()
{
m_logs.logDebugLine(1, "Starting OpenVPN");
m_logs.logLine(LogType.Management, "Starting OpenVPN...");
m_process = new Process();
m_process.StartInfo = m_psi;
m_process.Exited += new EventHandler(this.exited_event);
m_process.EnableRaisingEvents = true;
try
{
m_process.Start();
} catch(System.ComponentModel.Win32Exception) {
m_logs.logLine(LogType.Management, "Could not start OpenVPN");
return;
}
m_logs.logDebugLine(1, "Started");
m_logs.logLine(LogType.Management, "OpenVPN is running");
running = true;
}
/// <summary>
/// Kills the remaining process
/// </summary>
public void kill()
{
if (!running) return;
m_logs.logDebugLine(2, "Forcing OpenVPN to terminate");
try
{
m_process.Kill();
}
catch (InvalidOperationException e)
{
m_logs.logDebugLine(1, "Could not stop openvpn: " + e.Message);
}
}
/// <summary>
/// Reports the state of openvpn
/// </summary>
public bool isRunning
{
get { return running; }
}
/// <summary>
/// Process exited, reset everything important.
/// </summary>
/// <param name="sender">ignored</param>
/// <param name="args">ignored</param>
private void exited_event(object sender, EventArgs args)
{
running = false;
m_logs.logDebugLine(2, "OpenVPN stopped");
m_logs.logLine(LogType.Management, "OpenVPN stopped");
serviceExited(this, new EventArgs());
}
#region IDisposable Members
/// <summary>
/// Destructor. Disposes the object.
/// </summary>
private bool disposed;
~UserSpaceService()
{
Dispose(false);
}
/// <summary>
/// Disposes the object.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes the object.
/// </summary>
/// <param name="disposing">true if called from Dispose()</param>
private void Dispose(bool disposing)
{
if (!disposed)
{
kill();
if (disposing)
{
m_process.Dispose();
}
m_process = null;
disposed = true;
}
}
#endregion
}
}