2015-03-04 23:21:34 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.IO;
|
2015-11-16 01:10:59 +01:00
|
|
|
|
using BlubbFish.Utils;
|
2015-03-04 23:21:34 +01:00
|
|
|
|
|
|
|
|
|
namespace NetMonitorServer.Controller {
|
2015-11-16 01:10:59 +01:00
|
|
|
|
class OpenVpnController : OwnObject {
|
2015-03-04 23:21:34 +01:00
|
|
|
|
private Process proc;
|
|
|
|
|
|
|
|
|
|
public bool HasExited { get; private set; }
|
|
|
|
|
public bool Conntected { get; private set; }
|
|
|
|
|
public string Error { get; private set; }
|
|
|
|
|
public string Ip { get; private set; }
|
|
|
|
|
public string Dhcp { get; private set; }
|
|
|
|
|
public string DhcpLease { get; private set; }
|
|
|
|
|
public string IpDate { get; private set; }
|
|
|
|
|
public string SuccessDate { get; private set; }
|
|
|
|
|
|
2015-11-16 01:10:59 +01:00
|
|
|
|
private string config;
|
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
public OpenVpnController(string path, string config) {
|
2015-11-16 01:10:59 +01:00
|
|
|
|
this.addLog("NetMonitorServer.Start", "Erstelle OpenVPN " + config, LogLevel.Notice);
|
2015-03-04 23:21:34 +01:00
|
|
|
|
this.proc = new Process();
|
|
|
|
|
this.proc.StartInfo.Arguments = "--config " + config;
|
|
|
|
|
this.proc.StartInfo.FileName = path + "bin\\openvpn.exe";
|
|
|
|
|
this.proc.StartInfo.WorkingDirectory = path + "config";
|
|
|
|
|
this.HasExited = true;
|
|
|
|
|
this.Conntected = false;
|
|
|
|
|
this.Error = "";
|
2015-11-16 01:10:59 +01:00
|
|
|
|
this.config = config;
|
2015-03-04 23:21:34 +01:00
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
|
2015-03-04 23:21:34 +01:00
|
|
|
|
private void VpnOutputHandler(object sendingProcess, DataReceivedEventArgs outLine) {
|
|
|
|
|
if(!String.IsNullOrEmpty(outLine.Data)) {
|
|
|
|
|
string pattern = "^(.*?) N.*?IP/netmask of ([0-9\\./]*).*?DHCP-serv: ([0-9\\.]*).*?lease-time: ([0-9]*)";
|
|
|
|
|
Match match = Regex.Match(outLine.Data, pattern);
|
|
|
|
|
if(match.Success) {
|
|
|
|
|
this.IpDate = match.Groups[1].Value;
|
|
|
|
|
this.Ip = match.Groups[2].Value;
|
|
|
|
|
this.Dhcp = match.Groups[3].Value;
|
|
|
|
|
this.DhcpLease = match.Groups[4].Value;
|
|
|
|
|
}
|
|
|
|
|
pattern = "^(.*?) Initialization Sequence Completed";
|
|
|
|
|
match = Regex.Match(outLine.Data, pattern);
|
|
|
|
|
if(match.Success) {
|
|
|
|
|
this.SuccessDate = match.Groups[1].Value;
|
|
|
|
|
this.Conntected = true;
|
|
|
|
|
}
|
2015-11-16 01:10:59 +01:00
|
|
|
|
this.addLog("NetMonitorServer.VpnOutputHandler", "OpenVPN: " + outLine.Data, LogLevel.Info);
|
2015-03-04 23:21:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VpnErrorHandler(object sendingProcess, DataReceivedEventArgs outLine) {
|
|
|
|
|
this.Error += outLine.Data;
|
2015-11-16 01:10:59 +01:00
|
|
|
|
this.addLog("NetMonitorServer.VpnErrorHandler", "OpenVPN:" + outLine.Data, LogLevel.Warn);
|
2015-03-04 23:21:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool Start() {
|
2015-11-16 01:10:59 +01:00
|
|
|
|
this.addLog("NetMonitorServer.Start", "Starte OpenVPN " + this.config, LogLevel.Notice);
|
2015-03-04 23:21:34 +01:00
|
|
|
|
if(!this.HasExited) {
|
2015-11-16 01:10:59 +01:00
|
|
|
|
this.addLog("NetMonitorServer.Start", "OpenVPN wurde schon gestartet " + this.config, LogLevel.Warn);
|
2015-03-04 23:21:34 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
this.HasExited = false;
|
|
|
|
|
this.Conntected = false;
|
|
|
|
|
this.Error = "";
|
|
|
|
|
this.proc.StartInfo.UseShellExecute = false;
|
|
|
|
|
this.proc.StartInfo.RedirectStandardOutput = true;
|
|
|
|
|
this.proc.StartInfo.RedirectStandardError = true;
|
|
|
|
|
this.proc.StartInfo.RedirectStandardInput = true;
|
|
|
|
|
this.proc.OutputDataReceived += new DataReceivedEventHandler(VpnOutputHandler);
|
|
|
|
|
this.proc.ErrorDataReceived += new DataReceivedEventHandler(VpnErrorHandler);
|
|
|
|
|
this.proc.Start();
|
|
|
|
|
this.proc.BeginOutputReadLine();
|
|
|
|
|
this.proc.BeginErrorReadLine();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool Kill() {
|
2015-11-16 01:10:59 +01:00
|
|
|
|
this.addLog("NetMonitorServer.Kill", "Stoppe OpenVPN " + this.config, LogLevel.Notice);
|
2015-03-04 23:21:34 +01:00
|
|
|
|
this.proc.CancelErrorRead();
|
|
|
|
|
this.proc.CancelOutputRead();
|
|
|
|
|
this.proc.Kill();
|
|
|
|
|
this.proc.WaitForExit();
|
|
|
|
|
this.HasExited = true;
|
|
|
|
|
this.Conntected = false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|