Lang nichts mehr passiert, refactoring ist geil
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
|
||||
namespace NetMonitorServer
|
||||
{
|
||||
abstract class ActionClass
|
||||
{
|
||||
protected UdpClient outputNetworkStream;
|
||||
protected IPEndPoint outputNetworkPort;
|
||||
internal void SetAnswerStream(UdpClient udpClient, IPEndPoint iPEndPoint)
|
||||
{
|
||||
this.outputNetworkStream = udpClient;
|
||||
this.outputNetworkPort = iPEndPoint;
|
||||
}
|
||||
|
||||
abstract internal bool Run(Queue<string> arguments);
|
||||
|
||||
protected void sendMessage(string message)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
byte[] answ = Encoding.UTF8.GetBytes(message);
|
||||
outputNetworkStream.Send(answ, answ.Length, outputNetworkPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using BlubbFish.Utils;
|
||||
|
||||
namespace NetMonitorServer
|
||||
{
|
||||
class Factory
|
||||
{
|
||||
internal static ActionClass getAction(string className)
|
||||
{
|
||||
if (className.Substring(0, 3).ToLower() == "net")
|
||||
return new SetNetworks();
|
||||
if (className.Substring(0, 7).ToLower() == "service")
|
||||
return new SetService();
|
||||
if (className.Substring(0, 7).ToLower() == "openvpn")
|
||||
return new SetOpenVPNService();
|
||||
return null;
|
||||
}
|
||||
internal static InIReader getSettings() {
|
||||
return InIReader.getInstance("config.ini");
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-5
@@ -1,6 +1,6 @@
|
||||
namespace NetMonitorServer
|
||||
{
|
||||
partial class Service1
|
||||
partial class MainService
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
@@ -28,10 +28,8 @@
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.components = new System.ComponentModel.Container();
|
||||
this.timer1 = new System.Windows.Forms.Timer(this.components);
|
||||
//
|
||||
// Service1
|
||||
// MainService
|
||||
//
|
||||
this.ServiceName = "NetMonitorServer";
|
||||
|
||||
@@ -39,6 +37,5 @@
|
||||
|
||||
#endregion
|
||||
|
||||
private System.Windows.Forms.Timer timer1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.ServiceProcess;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,12 +117,6 @@
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>143, 71</value>
|
||||
</metadata>
|
||||
<metadata name="serviceController1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>230, 71</value>
|
||||
</metadata>
|
||||
<metadata name="$this.TrayLargeIcon" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
|
||||
<value>False</value>
|
||||
</metadata>
|
||||
@@ -6,32 +6,42 @@
|
||||
<ProductVersion>8.0.30703</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{7813196F-D729-44A2-81C7-82BB83E207F5}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>NetMonitorServer</RootNamespace>
|
||||
<AssemblyName>NetMonitorServer</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<TargetFrameworkProfile>
|
||||
</TargetFrameworkProfile>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>E:\Programme\NetMonitor\</OutputPath>
|
||||
<OutputPath>..\bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<DebugType>none</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>E:\Programme\NetMonitor\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<OutputPath>..\bin\Release\</OutputPath>
|
||||
<DefineConstants>
|
||||
</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<ApplicationIcon>icon.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<StartupObject />
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
@@ -46,24 +56,43 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="SetService.cs" />
|
||||
<Compile Include="Service1.cs">
|
||||
<Compile Include="ActionClass.cs" />
|
||||
<Compile Include="Factory.cs" />
|
||||
<Compile Include="Actions\SetOpenVPNService.cs" />
|
||||
<Compile Include="Actions\SetService.cs" />
|
||||
<Compile Include="MainService.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Service1.Designer.cs">
|
||||
<DependentUpon>Service1.cs</DependentUpon>
|
||||
<Compile Include="MainService.Designer.cs">
|
||||
<DependentUpon>MainService.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Program.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SetNetworks.cs" />
|
||||
<Compile Include="Actions\SetNetworks.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Service1.resx">
|
||||
<DependentUpon>Service1.cs</DependentUpon>
|
||||
<EmbeddedResource Include="MainService.resx">
|
||||
<DependentUpon>MainService.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
<None Include="ClassDiagram1.cd" />
|
||||
<None Include="config.ini">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="icon.ico" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Utils\Utils\Utils.csproj">
|
||||
<Project>{fac8ce64-bf13-4ece-8097-aeb5dd060098}</Project>
|
||||
<Name>Utils</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
||||
+45
-45
@@ -8,52 +8,52 @@ using System.ComponentModel;
|
||||
|
||||
namespace NetMonitorServer
|
||||
{
|
||||
[RunInstaller(true)]
|
||||
public class TestWinInstaller : Installer
|
||||
{
|
||||
private ServiceInstaller m_ThisService;
|
||||
private ServiceProcessInstaller m_ThisServiceProcess;
|
||||
[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 Service1()
|
||||
};
|
||||
ServiceBase.Run(ServicesToRun);
|
||||
}
|
||||
else if (args.Length == 1 || args[0] == "-r")
|
||||
{
|
||||
Service1 s = new Service1();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Resources;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("NetMonitorServer")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyDescription("Server des NetMonitors")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyCompany("BlubbFish")]
|
||||
[assembly: AssemblyProduct("NetMonitorServer")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2010")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2014")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
@@ -32,5 +33,6 @@ using System.Runtime.InteropServices;
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
[assembly: AssemblyVersion("1.1.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.1.0.0")]
|
||||
[assembly: NeutralResourcesLanguageAttribute("de-DE")]
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.ServiceProcess;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
|
||||
namespace NetMonitorServer
|
||||
{
|
||||
public partial class Service1 : ServiceBase
|
||||
{
|
||||
private Thread ot;
|
||||
public Service1()
|
||||
{
|
||||
InitializeComponent();
|
||||
this.ot = new Thread(new ThreadStart(overwatch));
|
||||
this.ot.IsBackground = true;
|
||||
}
|
||||
private static void overwatch()
|
||||
{
|
||||
UdpClient net_gets = new UdpClient(34523);
|
||||
IPEndPoint net_gets_port = new IPEndPoint(IPAddress.Loopback, 0);
|
||||
while (true)
|
||||
{
|
||||
if (Thread.CurrentThread.ThreadState == System.Threading.ThreadState.AbortRequested)
|
||||
{
|
||||
net_gets.Close();
|
||||
break;
|
||||
}
|
||||
byte[] data = net_gets.Receive(ref net_gets_port);
|
||||
string text = Encoding.UTF8.GetString(data);
|
||||
switchCommand(text);
|
||||
}
|
||||
}
|
||||
|
||||
private static void switchCommand(string data)
|
||||
{
|
||||
Console.WriteLine(data);
|
||||
if (data.Substring(0, 3).ToLower() == "net")
|
||||
new SetNetworks(data);
|
||||
if (data.Substring(0, 7).ToLower() == "service")
|
||||
new SetService(data);
|
||||
}
|
||||
|
||||
protected override void OnStart(string[] args)
|
||||
{
|
||||
this.ot.Start();
|
||||
}
|
||||
|
||||
protected override void OnStop()
|
||||
{
|
||||
this.ot.Abort();
|
||||
}
|
||||
|
||||
public void StartServiceConsole(string[] args)
|
||||
{
|
||||
this.OnStart(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
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
|
||||
{
|
||||
UdpClient net_send = new UdpClient();
|
||||
IPEndPoint net_send_port = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 34524);
|
||||
|
||||
public SetNetworks(string data)
|
||||
{
|
||||
if (data.Substring(0, 8).ToLower() == "net auto")
|
||||
this.setNetworkAuto(data);
|
||||
if (data.Substring(0, 6).ToLower() == "net ip")
|
||||
this.setNetworkIp(data);
|
||||
if (data.Substring(0, 7).ToLower() == "net dns")
|
||||
this.setNetworkDns(data);
|
||||
if (data.Substring(0, 8).ToLower() == "net wins")
|
||||
this.setNetworkWins(data);
|
||||
}
|
||||
|
||||
private void setNetworkWins(string data)
|
||||
{
|
||||
String[] par = data.Split(' ');
|
||||
if (par.Length != 4)
|
||||
return;
|
||||
bool ret;
|
||||
if (par[2].ToLower() == "auto")
|
||||
ret = this.runProgramNetsh("interface ipv4 set winsservers name=\"" + par[3] + "\" source=dhcp");
|
||||
else
|
||||
ret = this.runProgramNetsh("interface ipv4 set winsservers \"" + par[3] + "\" static " + par[2]);
|
||||
byte[] answ = Encoding.UTF8.GetBytes(ret.ToString());
|
||||
net_send.Send(answ, answ.Length, net_send_port);
|
||||
}
|
||||
|
||||
private void setNetworkDns(string data)
|
||||
{
|
||||
String[] par = data.Split(' ');
|
||||
if (par.Length != 4)
|
||||
return;
|
||||
bool ret;
|
||||
if (par[2].ToLower() == "auto")
|
||||
ret = this.runProgramNetsh("interface ipv4 set dnsservers name=\"" + par[3] + "\" source=dhcp");
|
||||
else
|
||||
ret = this.runProgramNetsh("interface ipv4 set dnsservers \"" + par[3] + "\" static " + par[2] + " primary");
|
||||
byte[] answ = Encoding.UTF8.GetBytes(ret.ToString());
|
||||
net_send.Send(answ, answ.Length, net_send_port);
|
||||
}
|
||||
|
||||
private void setNetworkIp(string data)
|
||||
{
|
||||
String[] par = data.Split(' ');
|
||||
if (par.Length != 4 && par.Length != 6)
|
||||
return;
|
||||
bool ret;
|
||||
if (par[2].ToLower() == "auto")
|
||||
ret = this.runProgramNetsh("interface ipv4 set address name=\"" + par[3] + "\" source=dhcp");
|
||||
else
|
||||
ret = this.runProgramNetsh("interface ipv4 set address \"" + par[5] + "\" static " + par[2] + " " + par[3] + " " + par[4]);
|
||||
byte[] answ = Encoding.UTF8.GetBytes(ret.ToString());
|
||||
net_send.Send(answ, answ.Length, net_send_port);
|
||||
}
|
||||
|
||||
private void setNetworkAuto(string data)
|
||||
{
|
||||
String[] par = data.Split(' ');
|
||||
if (par.Length != 3)
|
||||
return;
|
||||
byte[] answ;
|
||||
if (this.runProgramNetsh("interface ipv4 set address name=\"" + par[2] + "\" source=dhcp") &&
|
||||
this.runProgramNetsh("interface ipv4 set dnsservers name=\"" + par[2] + "\" source=dhcp") &&
|
||||
this.runProgramNetsh("interface ipv4 set winsservers name=\"" + par[2] + "\" source=dhcp"))
|
||||
answ = Encoding.UTF8.GetBytes(true.ToString());
|
||||
else
|
||||
answ = Encoding.UTF8.GetBytes(false.ToString());
|
||||
net_send.Send(answ, answ.Length, net_send_port);
|
||||
}
|
||||
|
||||
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;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
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 SetService
|
||||
{
|
||||
UdpClient net_send = new UdpClient();
|
||||
IPEndPoint net_send_port = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 34524);
|
||||
System.ServiceProcess.ServiceController serviceController;
|
||||
|
||||
public SetService(string data)
|
||||
{
|
||||
this.serviceController = new System.ServiceProcess.ServiceController();
|
||||
if (data.Substring(0, 13).ToLower() == "service start")
|
||||
this.setServiceStart(data);
|
||||
if (data.Substring(0, 12).ToLower() == "service stop")
|
||||
this.setServiceStop(data);
|
||||
if (data.Substring(0, 14).ToLower() == "service status")
|
||||
this.getServiceStatus(data);
|
||||
}
|
||||
|
||||
private void setServiceStart(string data)
|
||||
{
|
||||
String[] par = data.Split(' ');
|
||||
if (par.Length != 3)
|
||||
return;
|
||||
this.serviceController.ServiceName = par[2];
|
||||
try
|
||||
{
|
||||
this.serviceController.Start();
|
||||
this.serviceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
this.sendStr("false");
|
||||
}
|
||||
if (this.serviceController.Status == System.ServiceProcess.ServiceControllerStatus.Running)
|
||||
this.sendStr("true");
|
||||
else
|
||||
this.sendStr("false");
|
||||
}
|
||||
|
||||
private void setServiceStop(string data)
|
||||
{
|
||||
String[] par = data.Split(' ');
|
||||
if (par.Length != 3)
|
||||
return;
|
||||
this.serviceController.ServiceName = par[2];
|
||||
try
|
||||
{
|
||||
this.serviceController.Stop();
|
||||
this.serviceController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
this.sendStr("false");
|
||||
}
|
||||
if (this.serviceController.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
|
||||
this.sendStr("true");
|
||||
else
|
||||
this.sendStr("false");
|
||||
}
|
||||
|
||||
private void getServiceStatus(string data)
|
||||
{
|
||||
String[] par = data.Split(' ');
|
||||
if (par.Length != 3)
|
||||
return;
|
||||
try
|
||||
{
|
||||
this.serviceController.ServiceName = par[2];
|
||||
if (this.serviceController.Status == System.ServiceProcess.ServiceControllerStatus.Running)
|
||||
this.sendStr("true");
|
||||
else
|
||||
this.sendStr("false");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
this.sendStr("false");
|
||||
}
|
||||
}
|
||||
|
||||
private void sendStr(string p)
|
||||
{
|
||||
byte[] answ = Encoding.UTF8.GetBytes(p);
|
||||
net_send.Send(answ, answ.Length, net_send_port);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
[ports]
|
||||
server=54323
|
||||
client_from=54330
|
||||
client_to=54340
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 5.3 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.0 KiB |
Reference in New Issue
Block a user