Changed a lot
This commit is contained in:
@@ -5,11 +5,13 @@ using System.Text;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace NetMonitorServer
|
||||
{
|
||||
class SetNetworks : ActionClass
|
||||
{
|
||||
private string netsh_output = "";
|
||||
private bool setNetworkWins(Queue<string> data)
|
||||
{
|
||||
if (data.Count != 2)
|
||||
@@ -42,7 +44,7 @@ namespace NetMonitorServer
|
||||
}
|
||||
string ip = data.Dequeue();
|
||||
string name = data.Dequeue();
|
||||
string netsh = "interface ipv4 set dnsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip + "primary");
|
||||
string netsh = "interface ipv4 set dnsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip + " primary");
|
||||
try
|
||||
{
|
||||
this.runProgramNetsh(netsh);
|
||||
@@ -94,7 +96,7 @@ namespace NetMonitorServer
|
||||
{
|
||||
if (data.Count != 1)
|
||||
{
|
||||
this.sendMessage("To less arguments for SetNetworks.setNetworkDns class");
|
||||
this.sendMessage("To less arguments for SetNetworks.setNetworkAuto class");
|
||||
return false;
|
||||
}
|
||||
string name = data.Dequeue();
|
||||
@@ -113,8 +115,155 @@ namespace NetMonitorServer
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool getNetwork(Queue<string> arguments)
|
||||
{
|
||||
if(arguments.Count != 1) {
|
||||
this.sendMessage("To less arguments for SetNetworks.getNetwork class");
|
||||
return false;
|
||||
}
|
||||
string adapter = arguments.Dequeue();
|
||||
string ip = "";
|
||||
string subnet = "";
|
||||
string gateway = "";
|
||||
string dns = "";
|
||||
string wins = "";
|
||||
try
|
||||
{
|
||||
this.runProgramNetsh("interface ipv4 show interfaces");
|
||||
string[] text = this.netsh_output.Trim().Split('\n');
|
||||
foreach (string line in text)
|
||||
{
|
||||
string l = line.Trim();
|
||||
if (l == "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (Regex.IsMatch(l, "disconnected[ ]+"+adapter+"$", RegexOptions.IgnoreCase))
|
||||
{
|
||||
this.sendMessage("disconnected");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
this.runProgramNetsh("interface ipv4 show addresses");
|
||||
text = this.netsh_output.Trim().Split('\n');
|
||||
bool match = false;
|
||||
foreach (string line in text)
|
||||
{
|
||||
string l = line.Trim();
|
||||
if (l == "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^Konfiguration der Schnittstelle", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = false;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^[a-z0-9 ]+\""+adapter+"\"", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = true;
|
||||
}
|
||||
if (match)
|
||||
{
|
||||
if (Regex.IsMatch(l, "DHCP aktiviert:\\s+Ja", RegexOptions.IgnoreCase))
|
||||
{
|
||||
ip = "auto";
|
||||
break;
|
||||
}
|
||||
if (Regex.IsMatch(l, "IP-Adresse:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
ip = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
if (Regex.IsMatch(l, "Subnetzpräfix:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\/[0-9]+ \\(Maske [0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)", RegexOptions.IgnoreCase))
|
||||
{
|
||||
subnet = Regex.Match(l, "\\(Maske ([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)\\)").Groups[1].ToString();
|
||||
}
|
||||
if (Regex.IsMatch(l, "Standardgateway:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
gateway = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.runProgramNetsh("interface ipv4 show dnsservers");
|
||||
text = this.netsh_output.Trim().Split('\n');
|
||||
match = false;
|
||||
foreach (string line in text)
|
||||
{
|
||||
string l = line.Trim();
|
||||
if (l == "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^Konfiguration der Schnittstelle", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = false;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^[a-z0-9 ]+\"" + adapter + "\"", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = true;
|
||||
}
|
||||
if (match)
|
||||
{
|
||||
if (Regex.IsMatch(l, "Über DHCP konfigurierte DNS-Server:\\s+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
dns = "auto";
|
||||
}
|
||||
if (Regex.IsMatch(l, "Statisch konfigurierte DNS-Server:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
dns = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.runProgramNetsh("interface ipv4 show winsservers");
|
||||
text = this.netsh_output.Trim().Split('\n');
|
||||
match = false;
|
||||
foreach (string line in text)
|
||||
{
|
||||
string l = line.Trim();
|
||||
if (l == "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^Konfiguration der Schnittstelle", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = false;
|
||||
}
|
||||
if (Regex.IsMatch(l, "^[a-z0-9 ]+\"" + adapter + "\"", RegexOptions.IgnoreCase))
|
||||
{
|
||||
match = true;
|
||||
}
|
||||
if (match)
|
||||
{
|
||||
if (Regex.IsMatch(l, "Über DHCP konfigurierte WINS-Server:\\s+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
wins = "auto";
|
||||
}
|
||||
if (Regex.IsMatch(l, "Statisch konfigurierte WINS-Server:\\s+[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", RegexOptions.IgnoreCase))
|
||||
{
|
||||
wins = Regex.Match(l, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
if (ip == "")
|
||||
{
|
||||
this.sendMessage("unknown");
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("[" + ip + "|" + subnet + "|" + gateway + "|" + dns + "|" + wins + "]");
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool runProgramNetsh(string args)
|
||||
{
|
||||
//Console.WriteLine("netsh args: "+args);
|
||||
Process p = new Process();
|
||||
p.StartInfo.Arguments = args;
|
||||
p.StartInfo.FileName = "netsh";
|
||||
@@ -122,14 +271,14 @@ namespace NetMonitorServer
|
||||
p.StartInfo.RedirectStandardOutput = true;
|
||||
p.StartInfo.UseShellExecute = false;
|
||||
p.Start();
|
||||
string output = p.StandardOutput.ReadToEnd();
|
||||
this.netsh_output = p.StandardOutput.ReadToEnd();
|
||||
p.WaitForExit();
|
||||
Console.WriteLine(output.Trim());
|
||||
//Console.WriteLine(this.netsh_output.Trim());
|
||||
if (p.ExitCode == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
throw new Exception(output.Trim());
|
||||
throw new Exception(this.netsh_output.Trim());
|
||||
}
|
||||
internal override bool Run(Queue<string> arguments)
|
||||
{
|
||||
@@ -144,8 +293,11 @@ namespace NetMonitorServer
|
||||
case "ip": return this.setNetworkIp(arguments);
|
||||
case "dns": return this.setNetworkDns(arguments);
|
||||
case "wins": return this.setNetworkWins(arguments);
|
||||
case "network": return this.getNetwork(arguments);
|
||||
default: this.sendMessage("Wrong arguments for service on SetNetwork class"); return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,110 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
using BlubbFish.Utils;
|
||||
using NetMonitorServer.Controller;
|
||||
|
||||
namespace NetMonitorServer
|
||||
{
|
||||
class SetOpenVPNService : ActionClass
|
||||
{
|
||||
internal override bool Run(Queue<string> arguments)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
namespace NetMonitorServer {
|
||||
class SetOpenVPNService : ActionClass {
|
||||
|
||||
internal override bool Run(Queue<string> arguments) {
|
||||
if(arguments.Count < 2) {
|
||||
this.sendMessage("To less arguments for SetOpenVpnService class");
|
||||
return false;
|
||||
}
|
||||
switch(arguments.Dequeue().ToLower()) {
|
||||
case "status":
|
||||
return this.getVpn(arguments);
|
||||
case"start":
|
||||
return this.getVpnStart(arguments);
|
||||
case"stop":
|
||||
return this.getVpnStop(arguments);
|
||||
default:
|
||||
this.sendMessage("Wrong arguments for service on SetOpenVpnService class");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool getVpnStop(Queue<string> arguments) {
|
||||
string config = arguments.Dequeue();
|
||||
if(Model.openvpnInstances.Keys.Contains(config)) { //schon in der Liste
|
||||
OpenVpnController o = Model.openvpnInstances[config];
|
||||
if(o.HasExited) { //läuft nicht! Fehler!
|
||||
this.sendMessage("Fehler! Openvpn schon beendet! " + config + " Config");
|
||||
return false;
|
||||
}
|
||||
o.Kill();
|
||||
} else { //Noch nicht in der liste
|
||||
this.sendMessage("Fehler! Openvpn noch nie gestartet! " + config + " Config");
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool getVpnStart(Queue<string> arguments) {
|
||||
string config = arguments.Dequeue();
|
||||
if(Model.openvpnInstances.Keys.Contains(config)) { //schon in der Liste
|
||||
OpenVpnController o = Model.openvpnInstances[config];
|
||||
if(!o.HasExited) { //läuft noch! Fehler!
|
||||
this.sendMessage("Fehler! Openvpn Läuft noch! " + config + " Config");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
o.Start();
|
||||
} catch(Exception e) {
|
||||
this.sendMessage("Fehler beim Starten von OpenVPN: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
} else { //Noch nicht in der liste
|
||||
string ovpn = InIReader.getInstance("config.ini").getValue("program", "openvpn");
|
||||
ovpn = (ovpn.EndsWith("\\")) ? ovpn : ovpn + "\\";
|
||||
try {
|
||||
OpenVpnController o = new OpenVpnController(ovpn, config);
|
||||
Model.openvpnInstances.Add(config, o);
|
||||
o.Start();
|
||||
} catch(Exception e) {
|
||||
this.sendMessage("Fehler beim Starten von OpenVPN: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("true");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private bool getVpn(Queue<string> arguments) {
|
||||
if(arguments.Count != 1) {
|
||||
this.sendMessage("To less arguments for SetOpenVpnService.getVpn class");
|
||||
return false;
|
||||
}
|
||||
string config = arguments.Dequeue();
|
||||
string run = "";
|
||||
try {
|
||||
if(Model.openvpnInstances.Keys.Contains(config)) {
|
||||
OpenVpnController o = Model.openvpnInstances[config];
|
||||
if(o.HasExited) {
|
||||
run = "disconnected";
|
||||
} else if(o.Conntected) {
|
||||
run = "connected|" + o.Ip + "|" + o.Dhcp + "|" + o.DhcpLease + "|" + o.IpDate + "|" + o.SuccessDate;
|
||||
} else {
|
||||
run = "conntecting";
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
this.sendMessage(e.Message);
|
||||
return false;
|
||||
}
|
||||
if(run == "") {
|
||||
this.sendMessage("unknown");
|
||||
return false;
|
||||
}
|
||||
this.sendMessage("[" + run + "]");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user