Lang nichts mehr passiert, refactoring ist geil

This commit is contained in:
2014-07-11 14:38:10 +00:00
parent f019b4e493
commit cd21034b41
62 changed files with 2132 additions and 561 deletions
+151
View File
@@ -0,0 +1,151 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
namespace NetMonitorServer
{
class SetNetworks : ActionClass
{
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;
}
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.setNetworkDns 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 runProgramNetsh(string 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();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output.Trim());
if (p.ExitCode == 0)
{
return true;
}
throw new Exception(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);
default: this.sendMessage("Wrong arguments for service on SetNetwork class"); return false;
}
}
}
}
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NetMonitorServer
{
class SetOpenVPNService : ActionClass
{
internal override bool Run(Queue<string> arguments)
{
throw new NotImplementedException();
}
}
}
+115
View File
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.ServiceProcess;
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;
}
}
}
}