netmonitor/NetMonitorServer/Controller/OpenVpnController.cs

96 lines
3.6 KiB
C#

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;
using BlubbFish.Utils;
namespace NetMonitorServer.Controller {
class OpenVpnController : OwnObject {
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; }
private string config;
public OpenVpnController(string path, string config) {
this.addLog("NetMonitorServer.Start", "Erstelle OpenVPN " + config, LogLevel.Notice);
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 = "";
this.config = config;
}
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;
}
this.addLog("NetMonitorServer.VpnOutputHandler", "OpenVPN: " + outLine.Data, LogLevel.Info);
}
}
private void VpnErrorHandler(object sendingProcess, DataReceivedEventArgs outLine) {
this.Error += outLine.Data;
this.addLog("NetMonitorServer.VpnErrorHandler", "OpenVPN:" + outLine.Data, LogLevel.Warn);
}
internal bool Start() {
this.addLog("NetMonitorServer.Start", "Starte OpenVPN " + this.config, LogLevel.Notice);
if(!this.HasExited) {
this.addLog("NetMonitorServer.Start", "OpenVPN wurde schon gestartet " + this.config, LogLevel.Warn);
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() {
this.addLog("NetMonitorServer.Kill", "Stoppe OpenVPN " + this.config, LogLevel.Notice);
this.proc.CancelErrorRead();
this.proc.CancelOutputRead();
this.proc.Kill();
this.proc.WaitForExit();
this.HasExited = true;
this.Conntected = false;
return true;
}
}
}