Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Maslakov committed Nov 8, 2015
1 parent b295396 commit 681ca94
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
45 changes: 42 additions & 3 deletions OpenVpnManagement/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Diagnostics;
using System.IO;

namespace OpenVpnManagement {
public class Manager : IDisposable {
Expand All @@ -16,10 +18,42 @@ public enum Signal {
Usr2
}

public enum State {
Connecting,
Wait,
Auth,
GetConfig,
AssignIP,
AddRoutes,
Connected,
Reconnecting,
Exiting
}

private Socket socket;
private const int bufferSize = 1024;
private string? ovpnFilePath;

private void RunOpenVpnProcess() {
Process prc = new Process();
prc.StartInfo.CreateNoWindow = false;
prc.EnableRaisingEvents = true;
prc.StartInfo.Arguments = string.Format("--config {}", ovpnFilePath);
prc.StartInfo.FileName = "openvpn.exe";
prc.StartInfo.WorkingDirectory = @"C:\Program Files\OpenVPN\config";
prc.Start();
}
public Manager(string host, int port, string? ovpnFilePath) {
if (ovpnFilePath != null && ovpnFilePath.ToString() != string.Empty) {
var ovpnFilePath2 = ovpnFilePath.ToString();
var res = File.ReadAllLines(ovpnFilePath2).Where(x => x == "management ");
if (!res.Any()) {
File.AppendAllText(ovpnFilePath2, string.Format("management {} {}", host, port));
}

RunOpenVpnProcess();
}

public Manager(String host, int port) {
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(host, port);
GreetServer();
Expand All @@ -33,8 +67,9 @@ public enum Signal {
/// State
/// </summary>
/// <returns></returns>
public string GetState() {
return this.SendCommand("state");
public State GetState() {
var res = this.SendCommand("state");
return State.Auth; //todo
}

public string GetState(int n = 1) {
Expand Down Expand Up @@ -166,6 +201,10 @@ public enum Signal {

public void Dispose() {
if (socket != null) {
if (ovpnFilePath != null && ovpnFilePath != string.Empty) {
SendSignal(Signal.Term);
}

socket.Dispose();
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ A library in C# for communicating with OpenVPN Management Interface
```
using (var mng = new Manager("127.0.0.1", 8888)) {
var res1 = mng.GetPid();
Console.WriteLine(res);
Console.WriteLine(res1);
var res2 = mng.GetHelp();
Console.WriteLine(res2);
Expand Down

0 comments on commit 681ca94

Please sign in to comment.