88 lines
3.0 KiB
C#
88 lines
3.0 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;
|
|
|
|
namespace NetMonitorServer.Controller {
|
|
class OpenVpnController {
|
|
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; }
|
|
|
|
public OpenVpnController(string path, string config) {
|
|
// TODO: Complete member initialization
|
|
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 = "";
|
|
}
|
|
|
|
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;
|
|
}
|
|
//Console.WriteLine("L: |" + outLine.Data + "|");
|
|
}
|
|
}
|
|
|
|
private void VpnErrorHandler(object sendingProcess, DataReceivedEventArgs outLine) {
|
|
this.Error += outLine.Data;
|
|
}
|
|
|
|
internal bool Start() {
|
|
if(!this.HasExited) {
|
|
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.proc.CancelErrorRead();
|
|
this.proc.CancelOutputRead();
|
|
this.proc.Kill();
|
|
this.proc.WaitForExit();
|
|
this.HasExited = true;
|
|
this.Conntected = false;
|
|
return true;
|
|
}
|
|
}
|
|
}
|