masscheckin ganz viel kram

This commit is contained in:
2015-11-16 00:10:59 +00:00
parent 8264cbfa7b
commit dfc14d9c36
45 changed files with 1862 additions and 834 deletions
+3 -2
View File
@@ -4,9 +4,10 @@ using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using BlubbFish.Utils;
namespace NetMonitorServer {
abstract class ActionClass {
abstract class ActionClass : OwnObject {
protected UdpClient outputNetworkStream;
protected IPEndPoint outputNetworkPort;
internal void SetAnswerStream(UdpClient udpClient, IPEndPoint iPEndPoint) {
@@ -17,7 +18,7 @@ namespace NetMonitorServer {
abstract internal bool Run(Queue<string> arguments);
protected void sendMessage(string message) {
Console.WriteLine(message);
this.addLog("ActionClass.sendMessage", "Nachricht geantwortet: "+message, LogLevel.Notice);
byte[] answ = Encoding.UTF8.GetBytes(message);
outputNetworkStream.Send(answ, answ.Length, outputNetworkPort);
}
+278 -291
View File
@@ -7,297 +7,284 @@ 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)
{
this.sendMessage("To less arguments for SetNetworks.setNetworkWins class");
return false;
}
string ip = data.Dequeue();
string name = data.Dequeue();
string netsh = "interface ipv4 set winsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip);
try
{
this.runProgramNetsh(netsh);
}
catch (Exception e)
{
this.sendMessage(e.Message);
return false;
}
this.sendMessage("true");
return true;
}
namespace NetMonitorServer {
class SetNetworks : ActionClass {
private string netsh_output = "";
private bool setNetworkDns(Queue<string> data)
{
if (data.Count != 2)
{
this.sendMessage("To less arguments for SetNetworks.setNetworkDns class");
return false;
}
string ip = data.Dequeue();
string name = data.Dequeue();
string netsh = "interface ipv4 set dnsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip + " primary");
try
{
this.runProgramNetsh(netsh);
}
catch (Exception e)
{
this.sendMessage(e.Message);
return false;
}
this.sendMessage("true");
return true;
}
private bool setNetworkIp(Queue<string> data)
{
if (data.Count != 2 && data.Count != 4)
{
this.sendMessage("To less arguments for SetNetworks.setNetworkIp class");
return false;
}
string ip = data.Dequeue();
string netsh = "";
if (ip == "auto")
{
string name = data.Dequeue();
netsh = "interface ipv4 set address name=\"" + name + "\" source=dhcp";
}
else
{
string subnet = data.Dequeue();
string gw = data.Dequeue();
string name = data.Dequeue();
netsh = "interface ipv4 set address \"" + name + "\" static " + ip + " " + subnet + " " + gw;
}
try
{
this.runProgramNetsh(netsh);
}
catch (Exception e)
{
this.sendMessage(e.Message);
return false;
}
this.sendMessage("true");
return true;
}
private bool setNetworkAuto(Queue<string> data)
{
if (data.Count != 1)
{
this.sendMessage("To less arguments for SetNetworks.setNetworkAuto class");
return false;
}
string name = data.Dequeue();
try
{
this.runProgramNetsh("interface ipv4 set address name=\"" + name + "\" source=dhcp");
this.runProgramNetsh("interface ipv4 set dnsservers name=\"" + name + "\" source=dhcp");
this.runProgramNetsh("interface ipv4 set winsservers name=\"" + name + "\" source=dhcp");
}
catch (Exception e)
{
this.sendMessage(e.Message);
return false;
}
this.sendMessage("true");
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";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
this.netsh_output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
//Console.WriteLine(this.netsh_output.Trim());
if (p.ExitCode == 0)
{
return true;
}
throw new Exception(this.netsh_output.Trim());
}
internal override bool Run(Queue<string> arguments)
{
if (arguments.Count < 2)
{
this.sendMessage("To less arguments for SetNetwork class");
return false;
}
switch (arguments.Dequeue().ToLower())
{
case "auto": return this.setNetworkAuto(arguments);
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;
}
}
internal override bool Run(Queue<string> arguments) {
this.addLog("SetNetworks.Run", "Setze Netzwerk Befehl", LogLevel.Notice);
if(arguments.Count < 1) {
this.sendMessage("To less arguments for SetNetwork class");
return false;
}
switch(arguments.Dequeue().ToLower()) {
case "auto":
return this.setNetworkAuto(arguments);
case "ip":
return this.setNetworkIp(arguments);
case "dns":
return this.setNetworkDns(arguments);
case "wins":
return this.setNetworkWins(arguments);
case "network":
return this.getNetwork(arguments);
case "adapters":
return this.getAdapters(arguments);
default:
this.sendMessage("Wrong arguments for service on SetNetwork class");
return false;
}
}
private bool getAdapters(Queue<string> arguments) {
if(arguments.Count != 0) {
this.sendMessage("To less arguments for SetNetworks.getAdapters class");
return false;
}
this.addLog("SetNetworks.getNetwork", "Lese Liste Adapter", LogLevel.Notice);
List<string> ret = new List<string>();
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|connected) [ ]+([^ ].*)$", RegexOptions.IgnoreCase)) {
ret.Add(Regex.Match(l, "(disconnected|connected) [ ]+([^ ].*)$").Groups[2].ToString());
}
}
} catch(Exception e) {
this.sendMessage(e.Message);
return false;
}
if(ret.Count == 0) {
this.sendMessage("unknown");
return false;
}
this.sendMessage("[" + String.Join("|", ret.ToArray()) + "]");
return true;
}
private bool setNetworkWins(Queue<string> data) {
if(data.Count != 2) {
this.sendMessage("To less arguments for SetNetworks.setNetworkWins class");
return false;
}
string ip = data.Dequeue();
string name = data.Dequeue();
this.addLog("SetNetworks.setNetworkWins", "Setzte Wins auf " + name + " mit " + ip, LogLevel.Notice);
string netsh = "interface ipv4 set winsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip);
try {
this.runProgramNetsh(netsh);
} catch(Exception e) {
this.sendMessage(e.Message);
return false;
}
this.sendMessage("true");
return true;
}
private bool setNetworkDns(Queue<string> data) {
if(data.Count != 2) {
this.sendMessage("To less arguments for SetNetworks.setNetworkDns class");
return false;
}
string ip = data.Dequeue();
string name = data.Dequeue();
this.addLog("SetNetworks.setNetworkDns", "Setzte Dns auf " + name + " mit " + ip, LogLevel.Notice);
string netsh = "interface ipv4 set dnsservers name=\"" + name + (ip == "auto" ? "\" source=dhcp" : "\" static " + ip + " primary");
try {
this.runProgramNetsh(netsh);
} catch(Exception e) {
this.sendMessage(e.Message);
return false;
}
this.sendMessage("true");
return true;
}
private bool setNetworkIp(Queue<string> data) {
if(data.Count != 2 && data.Count != 4) {
this.sendMessage("To less arguments for SetNetworks.setNetworkIp class");
return false;
}
string ip = data.Dequeue();
string netsh = "";
if(ip == "auto") {
string name = data.Dequeue();
this.addLog("SetNetworks.setNetworkIp", "Setzte Ip auf " + name + " mit " + ip, LogLevel.Notice);
netsh = "interface ipv4 set address name=\"" + name + "\" source=dhcp";
} else {
string subnet = data.Dequeue();
string gw = data.Dequeue();
string name = data.Dequeue();
this.addLog("SetNetworks.setNetworkIp", "Setzte Ip auf " + name + " mit " + ip, LogLevel.Notice);
netsh = "interface ipv4 set address \"" + name + "\" static " + ip + " " + subnet + " " + gw;
}
try {
this.runProgramNetsh(netsh);
} catch(Exception e) {
this.sendMessage(e.Message);
return false;
}
this.sendMessage("true");
return true;
}
private bool setNetworkAuto(Queue<string> data) {
if(data.Count != 1) {
this.sendMessage("To less arguments for SetNetworks.setNetworkAuto class");
return false;
}
string name = data.Dequeue();
this.addLog("SetNetworks.setNetworkAuto", "Setzte DHCP auf " + name, LogLevel.Notice);
try {
this.runProgramNetsh("interface ipv4 set address name=\"" + name + "\" source=dhcp");
this.runProgramNetsh("interface ipv4 set dnsservers name=\"" + name + "\" source=dhcp");
this.runProgramNetsh("interface ipv4 set winsservers name=\"" + name + "\" source=dhcp");
} catch(Exception e) {
this.sendMessage(e.Message);
return false;
}
this.sendMessage("true");
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();
this.addLog("SetNetworks.getNetwork", "Lese Netzwerk auf " + adapter, LogLevel.Notice);
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) {
this.addLog("SetNetworks.runProgramNetsh", "Netsh: " + args, LogLevel.Info);
//Console.WriteLine("netsh args: "+args);
Process p = new Process();
p.StartInfo.Arguments = args;
p.StartInfo.FileName = "netsh";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
this.netsh_output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
//Console.WriteLine(this.netsh_output.Trim());
if(p.ExitCode == 0) {
return true;
}
throw new Exception(this.netsh_output.Trim());
}
}
}
+20 -11
View File
@@ -10,13 +10,14 @@ namespace NetMonitorServer {
class SetOpenVPNService : ActionClass {
internal override bool Run(Queue<string> arguments) {
this.addLog("SetOpenVPNService.Run", "Starte OpenVpn Befehl", LogLevel.Notice);
if(arguments.Count < 2) {
this.sendMessage("To less arguments for SetOpenVpnService class");
return false;
}
switch(arguments.Dequeue().ToLower()) {
case "status":
return this.getVpn(arguments);
return this.getVpnStatus(arguments);
case"start":
return this.getVpnStart(arguments);
case"stop":
@@ -28,7 +29,12 @@ namespace NetMonitorServer {
}
private bool getVpnStop(Queue<string> arguments) {
if(arguments.Count != 1) {
this.sendMessage("To less arguments for SetOpenVpnService.getVpnStop");
return false;
}
string config = arguments.Dequeue();
this.addLog("SetOpenVPNService.getVpnStop", "Stoppe OpenVPN: "+config, LogLevel.Notice);
if(Model.openvpnInstances.Keys.Contains(config)) { //schon in der Liste
OpenVpnController o = Model.openvpnInstances[config];
if(o.HasExited) { //läuft nicht! Fehler!
@@ -41,11 +47,16 @@ namespace NetMonitorServer {
return false;
}
this.sendMessage("true");
return false;
return true;
}
private bool getVpnStart(Queue<string> arguments) {
if(arguments.Count != 1) {
this.sendMessage("To less arguments for SetOpenVpnService.getVpnStart");
return false;
}
string config = arguments.Dequeue();
this.addLog("SetOpenVPNService.getVpnStart", "Starte OpenVPN: " + config, LogLevel.Notice);
if(Model.openvpnInstances.Keys.Contains(config)) { //schon in der Liste
OpenVpnController o = Model.openvpnInstances[config];
if(!o.HasExited) { //läuft noch! Fehler!
@@ -58,32 +69,30 @@ namespace NetMonitorServer {
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");
string ovpn = Factory.getSettings().getValue("program", "openvpn");
ovpn = (ovpn.EndsWith("\\")) ? ovpn : ovpn + "\\";
try {
OpenVpnController o = new OpenVpnController(ovpn, config);
o.EventLog += this.addLog;
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;
}
this.sendMessage("true");
return true;
}
private bool getVpn(Queue<string> arguments) {
private bool getVpnStatus(Queue<string> arguments) {
if(arguments.Count != 1) {
this.sendMessage("To less arguments for SetOpenVpnService.getVpn class");
this.sendMessage("To less arguments for SetOpenVpnService.getVpnStatus");
return false;
}
string config = arguments.Dequeue();
this.addLog("SetOpenVPNService.getVpnStatus", "Status des OpenVPN: " + config, LogLevel.Notice);
string run = "";
try {
if(Model.openvpnInstances.Keys.Contains(config)) {
+87 -103
View File
@@ -7,109 +7,93 @@ using System.Net;
using System.Net.Sockets;
using System.ServiceProcess;
namespace NetMonitorServer
{
class SetService : ActionClass
{
ServiceController serviceController;
namespace NetMonitorServer {
class SetService : ActionClass {
ServiceController serviceController;
private bool setServiceStart(Queue<string> data)
{
if (data.Count != 1)
{
this.sendMessage("To less arguments for SetService.setServiceStart class");
return false;
}
this.serviceController.ServiceName = data.Dequeue();
try
{
this.serviceController.Start();
this.serviceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
}
catch (Exception e)
{
this.sendMessage(e.Message);
return false;
}
if (this.serviceController.Status == ServiceControllerStatus.Running)
{
this.sendMessage("true");
}
else
{
this.sendMessage("The service isn't started");
return false;
}
return true;
}
private bool setServiceStop(Queue<string> data)
{
if (data.Count != 1)
{
this.sendMessage("To less arguments for SetService.setServiceStop class");
return false;
}
this.serviceController.ServiceName = data.Dequeue();
try
{
this.serviceController.Stop();
this.serviceController.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));
}
catch (Exception e)
{
this.sendMessage(e.Message);
return false;
}
if (this.serviceController.Status == ServiceControllerStatus.Stopped)
{
this.sendMessage("true");
}
else
{
this.sendMessage("The service isn't stopped");
return false;
}
return true;
}
private bool getServiceStatus(Queue<string> data)
{
if (data.Count != 1)
{
this.sendMessage("To less arguments for SetService.setServiceStatus class");
return false;
}
try
{
this.serviceController.ServiceName = data.Dequeue();
if (this.serviceController.Status == ServiceControllerStatus.Running)
this.sendMessage("running");
else
this.sendMessage("stopped");
}
catch (Exception e)
{
this.sendMessage(e.Message);
return false;
}
return true;
}
internal override bool Run(Queue<string> arguments)
{
if (arguments.Count != 2)
{
this.sendMessage("To less arguments for SetService class");
return false;
}
this.serviceController = new ServiceController();
switch (arguments.Dequeue().ToLower())
{
case "start": return this.setServiceStart(arguments);
case "stop": return this.setServiceStop(arguments);
case "status": return this.getServiceStatus(arguments);
default: this.sendMessage("Wrong arguments for service on SetService class"); return false;
}
}
internal override bool Run(Queue<string> arguments) {
this.addLog("SetService.Run", "Starte Service Befehl", LogLevel.Notice);
if(arguments.Count != 2) {
this.sendMessage("To less arguments for SetService class");
return false;
}
this.serviceController = new ServiceController();
switch(arguments.Dequeue().ToLower()) {
case "start":
return this.setServiceStart(arguments);
case "stop":
return this.setServiceStop(arguments);
case "status":
return this.getServiceStatus(arguments);
default:
this.sendMessage("Wrong arguments for service on SetService class");
return false;
}
}
private bool setServiceStart(Queue<string> data) {
if(data.Count != 1) {
this.sendMessage("To less arguments for SetService.setServiceStart class");
return false;
}
this.serviceController.ServiceName = data.Dequeue();
this.addLog("SetService.setServiceStart", "Starte Service: " + this.serviceController.ServiceName, LogLevel.Notice);
try {
this.serviceController.Start();
this.serviceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
} catch(Exception e) {
this.sendMessage(e.Message);
return false;
}
if(this.serviceController.Status == ServiceControllerStatus.Running) {
this.sendMessage("true");
} else {
this.sendMessage("The service isn't started");
return false;
}
return true;
}
private bool setServiceStop(Queue<string> data) {
if(data.Count != 1) {
this.sendMessage("To less arguments for SetService.setServiceStop class");
return false;
}
this.serviceController.ServiceName = data.Dequeue();
this.addLog("SetService.setServiceStop", "Stoppe Service: " + this.serviceController.ServiceName, LogLevel.Notice);
try {
this.serviceController.Stop();
this.serviceController.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));
} catch(Exception e) {
this.sendMessage(e.Message);
return false;
}
if(this.serviceController.Status == ServiceControllerStatus.Stopped) {
this.sendMessage("true");
} else {
this.sendMessage("The service isn't stopped");
return false;
}
return true;
}
private bool getServiceStatus(Queue<string> data) {
if(data.Count != 1) {
this.sendMessage("To less arguments for SetService.setServiceStatus class");
return false;
}
try {
this.serviceController.ServiceName = data.Dequeue();
this.addLog("SetService.setServiceStatus", "Status des Service: " + this.serviceController.ServiceName, LogLevel.Notice);
if(this.serviceController.Status == ServiceControllerStatus.Running)
this.sendMessage("running");
else
this.sendMessage("stopped");
} catch(Exception e) {
this.sendMessage(e.Message);
return false;
}
return true;
}
}
}
@@ -6,9 +6,10 @@ using System.Threading.Tasks;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.IO;
using BlubbFish.Utils;
namespace NetMonitorServer.Controller {
class OpenVpnController {
class OpenVpnController : OwnObject {
private Process proc;
public bool HasExited { get; private set; }
@@ -20,8 +21,10 @@ namespace NetMonitorServer.Controller {
public string IpDate { get; private set; }
public string SuccessDate { get; private set; }
private string config;
public OpenVpnController(string path, string config) {
// TODO: Complete member initialization
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";
@@ -29,8 +32,9 @@ namespace NetMonitorServer.Controller {
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]*)";
@@ -47,16 +51,19 @@ namespace NetMonitorServer.Controller {
this.SuccessDate = match.Groups[1].Value;
this.Conntected = true;
}
//Console.WriteLine("L: |" + outLine.Data + "|");
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;
@@ -75,6 +82,7 @@ namespace NetMonitorServer.Controller {
}
internal bool Kill() {
this.addLog("NetMonitorServer.Kill", "Stoppe OpenVPN " + this.config, LogLevel.Notice);
this.proc.CancelErrorRead();
this.proc.CancelOutputRead();
this.proc.Kill();
+4 -1
View File
@@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BlubbFish.Utils;
using System.Reflection;
namespace NetMonitorServer {
class Factory {
@@ -16,7 +18,8 @@ namespace NetMonitorServer {
return null;
}
internal static InIReader getSettings() {
return InIReader.getInstance("config.ini");
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
return InIReader.getInstance(path + "\\config.ini");
}
}
}
+88 -69
View File
@@ -11,74 +11,93 @@ using System.Net.Sockets;
using System.Threading;
using BlubbFish.Utils;
namespace NetMonitorServer
{
public partial class MainService : ServiceBase
{
private Thread serverThread = new Thread(new ThreadStart(overwatch));
private static UdpClient receiveStream;
public MainService()
{
InitializeComponent();
this.serverThread.IsBackground = true;
}
private static void overwatch()
{
IPEndPoint networkStreamAddress = new IPEndPoint(IPAddress.Loopback, 0);
while (true)
{
if (Thread.CurrentThread.ThreadState == System.Threading.ThreadState.AbortRequested)
{
receiveStream.Close();
break;
}
byte[] data = receiveStream.Receive(ref networkStreamAddress);
Thread queryThread = new Thread(new ParameterizedThreadStart(ServerCommandProcessor));
queryThread.Start(data);
}
}
private static void ServerCommandProcessor(object threadParam)
{
string data = Encoding.UTF8.GetString((byte[])threadParam);
Console.WriteLine(data);
Queue<string> arguments = new Queue<string>(data.Split(' '));
if (arguments.Count < 2)
{
Console.WriteLine("Zu wenig Argumente");
return;
}
ActionClass a = Factory.getAction(arguments.Dequeue());
if (a == null)
{
Console.WriteLine("Falsche Angabe der ActionClass");
return;
}
a.SetAnswerStream(new UdpClient(), new IPEndPoint(IPAddress.Loopback, Int32.Parse(arguments.Dequeue())));
a.Run(arguments);
}
protected override void OnStart(string[] args)
{
this.initServerThread();
this.serverThread.Start();
}
private void initServerThread()
{
receiveStream = new UdpClient(Int32.Parse(Factory.getSettings().getValue("ports", "server")));
}
protected override void OnStop()
{
this.serverThread.Abort();
try { receiveStream.Close(); } catch { };
}
public void StartServiceConsole(string[] args)
{
this.OnStart(args);
}
namespace NetMonitorServer {
public partial class MainService : ServiceBase {
private Thread serverThread = new Thread(new ThreadStart(overwatch));
private static UdpClient receiveStream;
private static FileLogger log;
private static OwnObject.LogLevel loglevel;
public MainService() {
string folder = Factory.getSettings().getValue("program", "logfolder");
folder = folder.EndsWith("\\") ? folder : folder + "\\";
log = FileLogger.getInstance(folder + "server.log", false);
loglevel = (OwnObject.LogLevel)Enum.Parse(typeof(OwnObject.LogLevel), Factory.getSettings().getValue("program", "loglevel"));
LogEvents("MainService", "Server Starten...", OwnObject.LogLevel.Notice);
InitializeComponent();
this.serverThread.IsBackground = true;
}
private static void overwatch() {
LogEvents("MainService.overwatch", "Empfangsthread gestartet.", OwnObject.LogLevel.Notice);
IPEndPoint networkStreamAddress = new IPEndPoint(IPAddress.Loopback, 0);
while(true) {
if(Thread.CurrentThread.ThreadState == System.Threading.ThreadState.AbortRequested) {
receiveStream.Close();
break;
}
byte[] data = receiveStream.Receive(ref networkStreamAddress);
Thread queryThread = new Thread(new ParameterizedThreadStart(ServerCommandProcessor));
queryThread.Start(data);
}
}
private static void ServerCommandProcessor(object threadParam) {
string data = Encoding.UTF8.GetString((byte[])threadParam);
LogEvents("MainService.ServerCommandProcessor", "Befehl empfangen: "+data, OwnObject.LogLevel.Notice);
Queue<string> arguments = new Queue<string>(data.Split(' '));
if(arguments.Count < 2) {
LogEvents("MainService.ServerCommandProcessor", "Zu wenig Argumente", OwnObject.LogLevel.Error);
return;
}
ActionClass a = Factory.getAction(arguments.Dequeue());
a.EventLog += LogEvents;
if(a == null) {
LogEvents("MainService.ServerCommandProcessor", "Falsche Angabe der ActionClass", OwnObject.LogLevel.Error);
return;
}
a.SetAnswerStream(new UdpClient(), new IPEndPoint(IPAddress.Loopback, Int32.Parse(arguments.Dequeue())));
a.Run(arguments);
}
static void LogEvents(string location, string message, OwnObject.LogLevel level) {
LogEvents(location, message, level, DateTime.Now);
}
static void LogEvents(string location, string message, OwnObject.LogLevel level, DateTime date) {
string msg = OwnObject.logToString(location, message, level, date, true, true);
//if(level >= loglevel) {
log.setLine(msg);
//}
Console.WriteLine(msg);
}
protected override void OnStart(string[] args) {
LogEvents("MainService.OnStart", "Server gestartet", OwnObject.LogLevel.Notice);
this.initServerThread();
this.serverThread.Start();
}
private void initServerThread() {
int port = Int32.Parse(Factory.getSettings().getValue("ports", "server"));
try {
receiveStream = new UdpClient(port);
} catch(Exception e) {
LogEvents("MainService.initServerThread", "Fehler beim Initialisieren des Ports "+port+": "+e.Message, OwnObject.LogLevel.Error);
throw e;
}
}
protected override void OnStop() {
LogEvents("MainService.OnStop", "Server gestoppt", OwnObject.LogLevel.Notice);
this.serverThread.Abort();
try { receiveStream.Close(); } catch { };
}
public void StartServiceConsole(string[] args) {
loglevel = OwnObject.LogLevel.Debug;
LogEvents("MainService.StartServiceConsole", "Server über die Konsole gestartet", OwnObject.LogLevel.Notice);
this.OnStart(args);
}
}
}
+38 -47
View File
@@ -6,54 +6,45 @@ using System.Text;
using System.Configuration.Install;
using System.ComponentModel;
namespace NetMonitorServer
{
[RunInstaller(true)]
public class TestWinInstaller : Installer
{
private ServiceInstaller m_ThisService;
private ServiceProcessInstaller m_ThisServiceProcess;
namespace NetMonitorServer {
[RunInstaller(true)]
public class TestWinInstaller : Installer {
private ServiceInstaller m_ThisService;
private ServiceProcessInstaller m_ThisServiceProcess;
public TestWinInstaller()
{
m_ThisService = new ServiceInstaller();
m_ThisServiceProcess = new ServiceProcessInstaller();
public TestWinInstaller() {
m_ThisService = new ServiceInstaller();
m_ThisServiceProcess = new ServiceProcessInstaller();
m_ThisServiceProcess.Account = ServiceAccount.LocalSystem;
m_ThisService.ServiceName = "NetMonitorServer";
m_ThisService.StartType = ServiceStartMode.Automatic;
m_ThisService.Description = "Teil von NetMonitor der die Systemnahen Befele ausführt";
m_ThisService.DisplayName = "Netzwerk Settings Tool";
m_ThisServiceProcess.Account = ServiceAccount.LocalSystem;
m_ThisService.ServiceName = "NetMonitorServer";
m_ThisService.StartType = ServiceStartMode.Automatic;
m_ThisService.Description = "Teil von NetMonitor der die Systemnahen Befele ausführt";
m_ThisService.DisplayName = "Netzwerk Settings Tool";
Installers.Add(m_ThisService);
Installers.Add(m_ThisServiceProcess);
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
if (args.Length == 0)
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MainService()
};
ServiceBase.Run(ServicesToRun);
}
else if (args.Length == 1 || args[0] == "-r")
{
MainService s = new MainService();
s.StartServiceConsole(args);
while (true)
{
System.Threading.Thread.Sleep(100);
}
}
}
}
Installers.Add(m_ThisService);
Installers.Add(m_ThisServiceProcess);
}
}
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args) {
if(args.Length == 0) {
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MainService()
};
ServiceBase.Run(ServicesToRun);
} else if(args.Length == 1 && args[0] == "-r") {
MainService s = new MainService();
s.StartServiceConsole(args);
while(true) {
System.Threading.Thread.Sleep(100);
}
}
}
}
}