forked from jochenwierum/openvpn-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceConnection.cs
More file actions
105 lines (94 loc) · 3.86 KB
/
ServiceConnection.cs
File metadata and controls
105 lines (94 loc) · 3.86 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
using System;
using System.IO;
using OpenVPNUtils.States;
using System.Threading;
using OpenVPNUtils;
namespace OpenVPNUtils
{
/// <summary>
/// Provides access to OpenVPN.
/// </summary>
public class ServiceConnection : Connection
{
#region constructors/destructors
/// <summary>
/// Initializes a new OVPN Object.
/// Also set a LogEventDelegate so that the first log lines are reveived.
/// </summary>
/// <param name="config">Path to configuration file</param>
/// <param name="earlyLogEvent">Delegate to a event processor</param>
/// <param name="earlyLogLevel">Log level</param>
/// <param name="smartCardSupport">Enable SmartCard support</param>
/// <seealso cref="Connection.Logs"/>
public ServiceConnection(string config,
EventHandler<LogEventArgs> earlyLogEvent, int earlyLogLevel, bool smartCardSupport)
{
if (config == null)
throw new ArgumentNullException(config, "Config file is null");
if (!new FileInfo(config).Exists)
throw new FileNotFoundException(config, "Config file \"" + config + "\" does not exist");
ConfigParser cf = new ConfigParser(config);
//management 127.0.0.1 11194
foreach (var directive in UtilsHelper.managementConfigItems)
{
if(!cf.DirectiveExists(directive.name))
throw new ArgumentException("The directive '" + directive.name + "' is needed in '" + config + "'");
}
if (smartCardSupport)
{
if (!cf.DirectiveExists("pkcs11-id-management"))
throw new ArgumentException(
"The directive 'pkcs11-id-management' is needed in '" + config + "'");
}
int port;
string[] args = cf.GetValue("management");
if(args.GetUpperBound(0) != 2)
throw new ArgumentException("The directive 'management'"
+ " is invalid in '" + config + "'");
if(!int.TryParse(args[2], out port))
throw new ArgumentException("The port '" + args[2]
+ "' is invalid in '" + config + "'");
m_logFile = getLogFile(config);
this.Init(args[1], port, earlyLogEvent, earlyLogLevel, false);
}
#endregion
/// <summary>
/// Connects with the configured parameters.
/// </summary>
/// <seealso cref="Disconnect"/>
public override void Connect()
{
CheckState(VPNConnectionState.Initializing);
State.ChangeState(VPNConnectionState.Initializing);
var del = new UtilsHelper.Function<bool>(ConnectLogic);
del.BeginInvoke(null, null);
}
/// <summary>
/// Disconnects from the OpenVPN Service.
/// </summary>
/// <seealso cref="Connect"/>
public override void Disconnect()
{
StateSnapshot ss = State.CreateSnapshot();
if (ss.ConnectionState == VPNConnectionState.Stopped ||
State.ConnectionState == VPNConnectionState.Error)
{
State.ChangeState(VPNConnectionState.Stopped);
return;
}
State.ChangeState(VPNConnectionState.Stopping);
var del = new UtilsHelper.Action(killConnection);
del.BeginInvoke(null, null);
}
/// <summary>
/// Kill the connection
/// </summary>
private void killConnection()
{
Logic.sendRestart();
Logic.sendDisconnect();
DisconnectLogic();
State.ChangeState(VPNConnectionState.Stopped);
}
}
}