Changed a lot
This commit is contained in:
@@ -46,6 +46,7 @@
|
||||
<Compile Include="NetworkSetter.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ServiceControl.cs" />
|
||||
<Compile Include="OpenVpnSetter.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Utils\Utils\Utils.csproj">
|
||||
@@ -53,6 +54,17 @@
|
||||
<Name>Utils</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="config.ini">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="network.ini">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="vpn.ini">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</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.
|
||||
|
||||
@@ -29,8 +29,8 @@ namespace NetMonitorUtils
|
||||
}
|
||||
public bool setNetwork(string networkName)
|
||||
{
|
||||
this.addLog("setNetwork", "Setze Netzwerkeinstellungen: " + networkName, LogLevel.Info);
|
||||
this.addLog("setNetwork", "networkName: " + networkName + ";", LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork", "Setze Netzwerkeinstellungen: " + networkName, LogLevel.Info);
|
||||
this.addLog("NetworkSetter.setNetwork", "networkName: " + networkName + ";", LogLevel.Debug);
|
||||
String name = networkini.getValue(networkName, "Name");
|
||||
String ip = networkini.getValue(networkName, "Ip");
|
||||
String subnet = networkini.getValue(networkName, "Subnet");
|
||||
@@ -38,100 +38,100 @@ namespace NetMonitorUtils
|
||||
String dns = networkini.getValue(networkName, "Dns");
|
||||
String wins = networkini.getValue(networkName, "Wins");
|
||||
String adapter = networkini.getValue(networkName, "Adapter");
|
||||
this.addLog("setNetwork", "name: " + name, LogLevel.Debug);
|
||||
this.addLog("setNetwork", "ip: " + ip, LogLevel.Debug);
|
||||
this.addLog("setNetwork", "subnet: " + subnet, LogLevel.Debug);
|
||||
this.addLog("setNetwork", "gateway: " + gateway, LogLevel.Debug);
|
||||
this.addLog("setNetwork", "dns: " + dns, LogLevel.Debug);
|
||||
this.addLog("setNetwork", "wins: " + wins, LogLevel.Debug);
|
||||
this.addLog("setNetwork", "adapter: " + adapter, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork", "name: " + name, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork", "ip: " + ip, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork", "subnet: " + subnet, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork", "gateway: " + gateway, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork", "dns: " + dns, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork", "wins: " + wins, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork", "adapter: " + adapter, LogLevel.Debug);
|
||||
bool ret = false;
|
||||
if (ip.ToLower() == "auto" && dns.ToLower() == "auto" && wins.ToLower() == "auto")
|
||||
{
|
||||
ret = setNetworkAuto(adapter, name);
|
||||
this.addLog("setNetwork.setNetworkAuto", "return " + ret, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork.setNetworkAuto", "return " + ret, LogLevel.Debug);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ip.ToLower() == "auto")
|
||||
{
|
||||
ret = setNetworkIpAuto(adapter, name);
|
||||
this.addLog("setNetwork.setNetworkIpAuto", "return " + ret, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork.setNetworkIpAuto", "return " + ret, LogLevel.Debug);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = setNetworkIp(ip, subnet, gateway, adapter, name);
|
||||
this.addLog("setNetwork.setNetworkIp", "return " + ret, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork.setNetworkIp", "return " + ret, LogLevel.Debug);
|
||||
}
|
||||
if (dns.ToLower() == "auto")
|
||||
{
|
||||
ret = setNetworkDnsAuto(adapter, name);
|
||||
this.addLog("setNetwork.setNetworkDnsAuto", "return " + ret, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork.setNetworkDnsAuto", "return " + ret, LogLevel.Debug);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = setNetworkDns(dns, adapter, name);
|
||||
this.addLog("setNetwork.setNetworkDns", "return " + ret, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork.setNetworkDns", "return " + ret, LogLevel.Debug);
|
||||
}
|
||||
if (wins.ToLower() == "auto")
|
||||
{
|
||||
ret = setNetworkWinsAuto(adapter, name);
|
||||
this.addLog("setNetwork.setNetworkWinsAuto", "return " + ret, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork.setNetworkWinsAuto", "return " + ret, LogLevel.Debug);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = setNetworkWins(wins, adapter, name);
|
||||
this.addLog("setNetwork.setNetworkWins", "return " + ret, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork.setNetworkWins", "return " + ret, LogLevel.Debug);
|
||||
}
|
||||
}
|
||||
this.addLog("setNetwork", "return " + ret, LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork", "return " + ret, LogLevel.Debug);
|
||||
return ret;
|
||||
}
|
||||
public bool setNetworkAuto(string adapter, string name)
|
||||
{
|
||||
this.addLog("setNetworkAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Automatisch", LogLevel.Info);
|
||||
this.addLog("setNetworkAuto", "datapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetworkAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Automatisch", LogLevel.Info);
|
||||
this.addLog("NetworkSetter.setNetworkAuto", "datapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
return setNetwork(netsetterType.Auto, null, null, null, adapter, name, null, null);
|
||||
}
|
||||
public bool setNetworkIpAuto(string adapter, string name)
|
||||
{
|
||||
this.addLog("setNetworkIpAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf IP Automatisch", LogLevel.Info);
|
||||
this.addLog("setNetworkIpAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetworkIpAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf IP Automatisch", LogLevel.Info);
|
||||
this.addLog("NetworkSetter.setNetworkIpAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
return setNetwork(netsetterType.IpAuto, null, null, null, adapter, name, null, null);
|
||||
}
|
||||
public bool setNetworkIp(string ip, string subnet, string gateway, string adapter, string name)
|
||||
{
|
||||
this.addLog("setNetworkIp", "Setze Netzwerkeinstellungen von " + adapter + " auf IP " + ip + "/" + subnet + " GW " + gateway, LogLevel.Info);
|
||||
this.addLog("setNetworkIp", "ip: " + ip + "; subnet: " + subnet + "; gateway: " + gateway + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetworkIp", "Setze Netzwerkeinstellungen von " + adapter + " auf IP " + ip + "/" + subnet + " GW " + gateway, LogLevel.Info);
|
||||
this.addLog("NetworkSetter.setNetworkIp", "ip: " + ip + "; subnet: " + subnet + "; gateway: " + gateway + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
return setNetwork(netsetterType.Ip, ip, subnet, gateway, adapter, name, null, null);
|
||||
}
|
||||
public bool setNetworkDnsAuto(string adapter, string name)
|
||||
{
|
||||
this.addLog("setNetworkDnsAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Dns Automatisch", LogLevel.Info);
|
||||
this.addLog("setNetworkDnsAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetworkDnsAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Dns Automatisch", LogLevel.Info);
|
||||
this.addLog("NetworkSetter.setNetworkDnsAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
return setNetwork(netsetterType.DnsAuto, null, null, null, adapter, name, null, null);
|
||||
}
|
||||
public bool setNetworkDns(string dns, string adapter, string name)
|
||||
{
|
||||
this.addLog("setNetworkDns", "Setze Netzwerkeinstellungen von " + adapter + " auf Dns " + dns, LogLevel.Info);
|
||||
this.addLog("setNetworkDns", "dns: " + dns + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetworkDns", "Setze Netzwerkeinstellungen von " + adapter + " auf Dns " + dns, LogLevel.Info);
|
||||
this.addLog("NetworkSetter.setNetworkDns", "dns: " + dns + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
return setNetwork(netsetterType.Dns, null, null, null, adapter, name, dns, null);
|
||||
}
|
||||
public bool setNetworkWinsAuto(string adapter, string name)
|
||||
{
|
||||
this.addLog("setNetworkWinsAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Wins Automatisch", LogLevel.Info);
|
||||
this.addLog("setNetworkWinsAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetworkWinsAuto", "Setze Netzwerkeinstellungen von " + adapter + " auf Wins Automatisch", LogLevel.Info);
|
||||
this.addLog("NetworkSetter.setNetworkWinsAuto", "adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
return setNetwork(netsetterType.WinsAuto, null, null, null, adapter, name, null, null);
|
||||
}
|
||||
public bool setNetworkWins(string wins, string adapter, string name)
|
||||
{
|
||||
this.addLog("setNetworkWins", "Setze Netzwerkeinstellungen von " + adapter + " auf Wins " + wins, LogLevel.Info);
|
||||
this.addLog("setNetworkWins", "wins: " + wins + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetworkWins", "Setze Netzwerkeinstellungen von " + adapter + " auf Wins " + wins, LogLevel.Info);
|
||||
this.addLog("NetworkSetter.setNetworkWins", "wins: " + wins + "; adapter: " + adapter + "; name: " + name + ";", LogLevel.Debug);
|
||||
return setNetwork(netsetterType.Wins, null, null, null, adapter, name, null, wins);
|
||||
}
|
||||
private bool setNetwork(netsetterType option, string ip, string subnet, string gateway, string adapter, string name, string dns, string wins)
|
||||
{
|
||||
this.addLog("setNetwork", "option: " + option + "; ip: " + ip + "; subnet: " + subnet + "; gateway: " + gateway + "; adapter: " + adapter + "; name: " + name + "; dns: " + dns + "; wins: " + wins + ";", LogLevel.Debug);
|
||||
this.addLog("NetworkSetter.setNetwork", "option: " + option + "; ip: " + ip + "; subnet: " + subnet + "; gateway: " + gateway + "; adapter: " + adapter + "; name: " + name + "; dns: " + dns + "; wins: " + wins + ";", LogLevel.Debug);
|
||||
bool status = false;
|
||||
try
|
||||
{
|
||||
@@ -173,10 +173,80 @@ namespace NetMonitorUtils
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.addLog("setNetwork", "Fehler beim Ausführen des Befehls: " + e.Message, LogLevel.Error);
|
||||
this.addLog("NetworkSetter.setNetwork", "Fehler beim Ausführen des Befehls: " + e.Message, LogLevel.Error);
|
||||
throw e;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
public Dictionary<String, String> getNetwork(List<string> adapters)
|
||||
{
|
||||
this.addLog("NetworkSetter.getNetwork", "Hole Netzwerkeinstellungen für alle Adapter", LogLevel.Info);
|
||||
this.addLog("NetworkSetter.getNetwork", "adapter: "+adapters+";", LogLevel.Debug);
|
||||
Dictionary<String, String> ret = new Dictionary<String, String>();
|
||||
foreach (string adapter in adapters)
|
||||
{
|
||||
String data = this.serviceController.NetworkGetName(adapter);
|
||||
if (data == "unknown")
|
||||
{
|
||||
ret.Add(adapter, "unknown");
|
||||
}
|
||||
else if (data == "disconnected")
|
||||
{
|
||||
ret.Add(adapter, "disconnected");
|
||||
}
|
||||
else
|
||||
{
|
||||
string[] values = data.Substring(0, data.Length - 1).Substring(1).Split('|');
|
||||
List<string> networks = this.networkini.getSections();
|
||||
string selected_network = "";
|
||||
foreach (string network in networks)
|
||||
{
|
||||
if (networkini.getValue(network, "Ip") == values[0])
|
||||
{
|
||||
selected_network = network;
|
||||
}
|
||||
if (networkini.getValue(network, "Subnet") == values[1] || (values[1] == "" && networkini.getValue(network, "Subnet") == null))
|
||||
{
|
||||
if (selected_network != network)
|
||||
{
|
||||
selected_network = "";
|
||||
}
|
||||
}
|
||||
if (networkini.getValue(network, "Gateway") == values[2] || (values[2] == "" && networkini.getValue(network, "Gateway") == null))
|
||||
{
|
||||
if (selected_network != network)
|
||||
{
|
||||
selected_network = "";
|
||||
}
|
||||
}
|
||||
if (networkini.getValue(network, "Dns") == values[3])
|
||||
{
|
||||
if (selected_network != network)
|
||||
{
|
||||
selected_network = "";
|
||||
}
|
||||
}
|
||||
if (networkini.getValue(network, "Wins") == values[4])
|
||||
{
|
||||
if (selected_network != network)
|
||||
{
|
||||
selected_network = "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (selected_network == "")
|
||||
{
|
||||
ret.Add(adapter, "unknown");
|
||||
}
|
||||
else
|
||||
{
|
||||
ret.Add(adapter, selected_network);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using BlubbFish.Utils;
|
||||
|
||||
namespace NetMonitorUtils {
|
||||
public class OpenVpnSetter : OwnObject {
|
||||
private ServiceControl serviceController;
|
||||
private InIReader vpnini;
|
||||
public OpenVpnSetter(string vpnfile, string configfile) {
|
||||
this.addLog("NetworkSetter", "vpnfile: " + vpnfile + "; configfile: " + configfile + ";", LogLevel.Debug);
|
||||
this.vpnini = InIReader.getInstance(vpnfile);
|
||||
this.serviceController = ServiceControl.getInstance(configfile);
|
||||
}
|
||||
public bool setStart(string name) {
|
||||
this.addLog("ServiceSetter.setStart", "", LogLevel.Debug);
|
||||
this.addLog("ServiceSetter.setStart", "Starte Service", LogLevel.Info);
|
||||
return this.serviceController.VpnSetStart(name);
|
||||
}
|
||||
public bool setStop(string name) {
|
||||
this.addLog("ServiceSetter.setStop", "", LogLevel.Debug);
|
||||
this.addLog("ServiceSetter.setStop", "Stoppe Service", LogLevel.Info);
|
||||
return this.serviceController.VpnSetStop(name);
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns a list ("unknown";"connected";"disconnected","name")
|
||||
/// </summary>
|
||||
/// <param name="names"></param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<String, String> getStatus(List<string> vpns) {
|
||||
this.addLog("ServiceSetter.setStatus", "", LogLevel.Debug);
|
||||
this.addLog("ServiceSetter.setStatus", "Status von allen Services", LogLevel.Info);
|
||||
Dictionary<String, String> ret = new Dictionary<String, String>();
|
||||
foreach(string vpn in vpns) {
|
||||
String data = this.serviceController.VpnGetStatus(vpn);
|
||||
if(data == "unknown") {
|
||||
ret.Add(vpn, "unknown");
|
||||
} else if(data == "disconnected") {
|
||||
ret.Add(vpn, "disconnected");
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
/// <summary>
|
||||
/// Return a Status of one VPN
|
||||
/// </summary>
|
||||
/// <param name="config">Configfile</param>
|
||||
/// <returns>Statusustring</returns>
|
||||
public string getStatus(string config) {
|
||||
this.addLog("ServiceSetter.getStatus", "", LogLevel.Debug);
|
||||
this.addLog("ServiceSetter.getStatus", "Status vom Service", LogLevel.Info);
|
||||
return this.serviceController.VpnGetStatus(config);
|
||||
}
|
||||
}
|
||||
}
|
||||
+228
-215
@@ -8,220 +8,233 @@ using System.Net;
|
||||
using BlubbFish.Utils;
|
||||
using NetMonitorUtils.Execptions;
|
||||
|
||||
namespace NetMonitorUtils
|
||||
{
|
||||
public class ServiceControl
|
||||
{
|
||||
private ServiceController sc = new ServiceController();
|
||||
private static ServiceControl instance;
|
||||
private InIReader config;
|
||||
private ServiceControl(string configfile)
|
||||
{
|
||||
this.sc.ServiceName = "NetMonitorServer";
|
||||
this.config = InIReader.getInstance(configfile);
|
||||
}
|
||||
|
||||
public static ServiceControl getInstance(string configfile)
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new ServiceControl(configfile);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private bool ServiceSetStatus(ServiceControllerStatus status)
|
||||
{
|
||||
if (this.sc.Status == status)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
this.sc.Start();
|
||||
try
|
||||
{
|
||||
this.sc.WaitForStatus(status, new TimeSpan(0, 0, 30));
|
||||
}
|
||||
catch (System.ServiceProcess.TimeoutException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ServiceStart()
|
||||
{
|
||||
return this.ServiceSetStatus(ServiceControllerStatus.Running);
|
||||
}
|
||||
|
||||
public bool ServicePause()
|
||||
{
|
||||
return this.ServiceSetStatus(ServiceControllerStatus.Paused);
|
||||
}
|
||||
|
||||
public bool ServiceStop()
|
||||
{
|
||||
return this.ServiceSetStatus(ServiceControllerStatus.Stopped);
|
||||
}
|
||||
|
||||
public bool NetworkSetAuto(string adapter)
|
||||
{
|
||||
if (adapter == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET","AUTO "+adapter);
|
||||
}
|
||||
|
||||
private bool sendMessage(string method, string message)
|
||||
{
|
||||
IPEndPoint netRecieverPoint = new IPEndPoint(IPAddress.Loopback, 0);
|
||||
int startPort = Int32.Parse(this.config.getValue("ports", "client_from"));
|
||||
int rangePort = Int32.Parse(this.config.getValue("ports", "client_to"));
|
||||
UdpClient netReciver = null;
|
||||
int selectedPort = 0;
|
||||
for (int i = startPort; i < rangePort; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
netReciver = new UdpClient(i);
|
||||
}
|
||||
catch (System.Net.Sockets.SocketException) { }
|
||||
if (netReciver != null)
|
||||
{
|
||||
selectedPort = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (netReciver == null)
|
||||
{
|
||||
throw new ServiceControlException("No Free Port for Comunication");
|
||||
}
|
||||
byte[] data = Encoding.UTF8.GetBytes(method + " " + selectedPort + " " + message);
|
||||
|
||||
UdpClient netSender = new UdpClient();
|
||||
IPEndPoint netSenderPoint = new IPEndPoint(IPAddress.Loopback, Int32.Parse(this.config.getValue("ports", "server")));
|
||||
netSender.Send(data, data.Length, netSenderPoint);
|
||||
netReciver.Client.ReceiveTimeout = 60000;
|
||||
byte[] recivedData;
|
||||
try
|
||||
{
|
||||
recivedData = netReciver.Receive(ref netRecieverPoint);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new ServiceControlNetworkException(e.Message);
|
||||
}
|
||||
string recieveText = Encoding.UTF8.GetString(recivedData);
|
||||
if (recieveText.ToLower() == "true")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ServiceControlException(recieveText);
|
||||
}
|
||||
}
|
||||
|
||||
public bool NetworkSetIp(string ip, string subnet, string gateway, string adapter)
|
||||
{
|
||||
if (ip == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("IP address not Set!");
|
||||
}
|
||||
if (subnet == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Subnet address not Set!");
|
||||
}
|
||||
if (gateway == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Gateway address not Set!");
|
||||
}
|
||||
if (adapter == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "IP " + ip + " " + subnet + " " + gateway + " " + adapter);
|
||||
}
|
||||
|
||||
public bool NetworkSetDNS(string dns, string adapter)
|
||||
{
|
||||
if (dns == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("DNS address not Set!");
|
||||
}
|
||||
if (adapter == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "DNS " + dns + " " + adapter);
|
||||
}
|
||||
|
||||
public bool NetworkSetWINS(string wins, string adapter)
|
||||
{
|
||||
if (wins == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("WINS address not Set!");
|
||||
}
|
||||
if (adapter == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "WINS " + wins + " " + adapter);
|
||||
}
|
||||
|
||||
public bool NetworkSetIpAuto(string adapter)
|
||||
{
|
||||
if (adapter == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "IP AUTO "+adapter);
|
||||
}
|
||||
|
||||
|
||||
public bool NetworkSetDnsAuto(string adapter)
|
||||
{
|
||||
if (adapter == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "DNS AUTO " + adapter);
|
||||
}
|
||||
|
||||
public bool NetworkSetWinsAuto(string adapter)
|
||||
{
|
||||
if (adapter == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "WINS AUTO " + adapter);
|
||||
}
|
||||
|
||||
public bool ServiceStart(string name)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Service name ist not set!");
|
||||
}
|
||||
return this.sendMessage("SERVICE", "START " + name);
|
||||
}
|
||||
|
||||
public bool ServiceStop(string name)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Service name ist not set!");
|
||||
}
|
||||
return this.sendMessage("SERVICE", "STOP " + name);
|
||||
}
|
||||
|
||||
public bool ServiceStatus(string name)
|
||||
{
|
||||
if (name == null)
|
||||
{
|
||||
throw new ServiceControlArgumentException("Service name ist not set!");
|
||||
}
|
||||
return this.sendMessage("SERVICE", "STATUS " + name);
|
||||
}
|
||||
namespace NetMonitorUtils {
|
||||
public class ServiceControl {
|
||||
private ServiceController sc = new ServiceController();
|
||||
private static ServiceControl instance;
|
||||
private InIReader config;
|
||||
private ServiceControl(string configfile) {
|
||||
this.sc.ServiceName = "NetMonitorServer";
|
||||
this.config = InIReader.getInstance(configfile);
|
||||
}
|
||||
|
||||
public static ServiceControl getInstance(string configfile) {
|
||||
if(instance == null) {
|
||||
instance = new ServiceControl(configfile);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private bool ServiceSetStatus(ServiceControllerStatus status) {
|
||||
if(this.sc.Status == status) {
|
||||
return true;
|
||||
}
|
||||
this.sc.Start();
|
||||
try {
|
||||
this.sc.WaitForStatus(status, new TimeSpan(0, 0, 30));
|
||||
} catch(System.ServiceProcess.TimeoutException) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ServiceStart() {
|
||||
return this.ServiceSetStatus(ServiceControllerStatus.Running);
|
||||
}
|
||||
|
||||
public bool ServicePause() {
|
||||
return this.ServiceSetStatus(ServiceControllerStatus.Paused);
|
||||
}
|
||||
|
||||
public bool ServiceStop() {
|
||||
return this.ServiceSetStatus(ServiceControllerStatus.Stopped);
|
||||
}
|
||||
|
||||
public bool NetworkSetAuto(string adapter) {
|
||||
if(adapter == null) {
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "AUTO " + adapter);
|
||||
}
|
||||
|
||||
private bool sendMessage(string method, string message) {
|
||||
IPEndPoint netRecieverPoint = new IPEndPoint(IPAddress.Loopback, 0);
|
||||
int startPort = Int32.Parse(this.config.getValue("ports", "client_from"));
|
||||
int rangePort = Int32.Parse(this.config.getValue("ports", "client_to"));
|
||||
UdpClient netReciver = null;
|
||||
int selectedPort = 0;
|
||||
for(int i = startPort; i < rangePort; i++) {
|
||||
try {
|
||||
netReciver = new UdpClient(i);
|
||||
} catch(System.Net.Sockets.SocketException) { }
|
||||
if(netReciver != null) {
|
||||
selectedPort = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(netReciver == null) {
|
||||
throw new ServiceControlException("No Free Port for Comunication");
|
||||
}
|
||||
byte[] data = Encoding.UTF8.GetBytes(method + " " + selectedPort + " " + message);
|
||||
|
||||
UdpClient netSender = new UdpClient();
|
||||
IPEndPoint netSenderPoint = new IPEndPoint(IPAddress.Loopback, Int32.Parse(this.config.getValue("ports", "server")));
|
||||
netSender.Send(data, data.Length, netSenderPoint);
|
||||
netReciver.Client.ReceiveTimeout = 60000;
|
||||
byte[] recivedData;
|
||||
try {
|
||||
recivedData = netReciver.Receive(ref netRecieverPoint);
|
||||
} catch(Exception e) {
|
||||
throw new ServiceControlNetworkException(e.Message);
|
||||
}
|
||||
string recieveText = Encoding.UTF8.GetString(recivedData);
|
||||
if(recieveText.ToLower() == "true") {
|
||||
return true;
|
||||
} else {
|
||||
throw new ServiceControlException(recieveText);
|
||||
}
|
||||
}
|
||||
|
||||
public bool NetworkSetIp(string ip, string subnet, string gateway, string adapter) {
|
||||
if(ip == null) {
|
||||
throw new ServiceControlArgumentException("IP address not Set!");
|
||||
}
|
||||
if(subnet == null) {
|
||||
throw new ServiceControlArgumentException("Subnet address not Set!");
|
||||
}
|
||||
if(gateway == null) {
|
||||
throw new ServiceControlArgumentException("Gateway address not Set!");
|
||||
}
|
||||
if(adapter == null) {
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "IP " + ip + " " + subnet + " " + gateway + " " + adapter);
|
||||
}
|
||||
|
||||
public bool NetworkSetDNS(string dns, string adapter) {
|
||||
if(dns == null) {
|
||||
throw new ServiceControlArgumentException("DNS address not Set!");
|
||||
}
|
||||
if(adapter == null) {
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "DNS " + dns + " " + adapter);
|
||||
}
|
||||
|
||||
public bool NetworkSetWINS(string wins, string adapter) {
|
||||
if(wins == null) {
|
||||
throw new ServiceControlArgumentException("WINS address not Set!");
|
||||
}
|
||||
if(adapter == null) {
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "WINS " + wins + " " + adapter);
|
||||
}
|
||||
|
||||
public bool NetworkSetIpAuto(string adapter) {
|
||||
if(adapter == null) {
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "IP AUTO " + adapter);
|
||||
}
|
||||
|
||||
|
||||
public bool NetworkSetDnsAuto(string adapter) {
|
||||
if(adapter == null) {
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "DNS AUTO " + adapter);
|
||||
}
|
||||
|
||||
public bool NetworkSetWinsAuto(string adapter) {
|
||||
if(adapter == null) {
|
||||
throw new ServiceControlArgumentException("Adapter ist not Set!");
|
||||
}
|
||||
return this.sendMessage("NET", "WINS AUTO " + adapter);
|
||||
}
|
||||
|
||||
public bool ServiceStart(string name) {
|
||||
if(name == null) {
|
||||
throw new ServiceControlArgumentException("Service name ist not set!");
|
||||
}
|
||||
return this.sendMessage("SERVICE", "START " + name);
|
||||
}
|
||||
|
||||
public bool ServiceStop(string name) {
|
||||
if(name == null) {
|
||||
throw new ServiceControlArgumentException("Service name ist not set!");
|
||||
}
|
||||
return this.sendMessage("SERVICE", "STOP " + name);
|
||||
}
|
||||
|
||||
public bool ServiceStatus(string name) {
|
||||
if(name == null) {
|
||||
throw new ServiceControlArgumentException("Service name ist not set!");
|
||||
}
|
||||
try {
|
||||
return this.sendMessage("SERVICE", "STATUS " + name);
|
||||
} catch(ServiceControlException e) {
|
||||
if(e.Message.ToLower() == "stopped") {
|
||||
return false;
|
||||
}
|
||||
if(e.Message.ToLower() == "running") {
|
||||
return true;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public string NetworkGetName(string adapter) {
|
||||
if(adapter == null) {
|
||||
throw new ServiceControlArgumentException("Adaptername not set!");
|
||||
}
|
||||
try {
|
||||
return this.sendMessage("NET", "NETWORK " + adapter).ToString();
|
||||
} catch(ServiceControlException e) {
|
||||
if(e.Message.ToLower() == "unknown") {
|
||||
return "unknown";
|
||||
}
|
||||
if(e.Message.ToLower() == "disconnected") {
|
||||
return "disconnected";
|
||||
}
|
||||
if(e.Message.ToLower().Substring(0, 1) == "[") {
|
||||
return e.Message;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
public string VpnGetStatus(string vpn) {
|
||||
if(vpn == null) {
|
||||
throw new ServiceControlArgumentException("Configname not set!");
|
||||
}
|
||||
try {
|
||||
return this.sendMessage("OPENVPN", "STATUS " + vpn).ToString();
|
||||
} catch(ServiceControlException e) {
|
||||
if(e.Message.ToLower() == "unknown") {
|
||||
return "unknown";
|
||||
}
|
||||
if(e.Message.ToLower() == "disconnected") {
|
||||
return "disconnected";
|
||||
}
|
||||
if(e.Message.ToLower().Substring(0, 1) == "[") {
|
||||
return e.Message;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
internal bool VpnSetStart(string name) {
|
||||
if(name == null) {
|
||||
throw new ServiceControlArgumentException("OpenVPN Config name ist not set!");
|
||||
}
|
||||
return this.sendMessage("OPENVPN", "START " + name);
|
||||
}
|
||||
|
||||
internal bool VpnSetStop(string name) {
|
||||
if(name == null) {
|
||||
throw new ServiceControlArgumentException("OpenVPN Config name ist not set!");
|
||||
}
|
||||
return this.sendMessage("OPENVPN", "STOP " + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
[ports]
|
||||
server=54323
|
||||
client_from=54330
|
||||
client_to=54340
|
||||
|
||||
[program]
|
||||
openvpn=C:\Program Files\OpenVPN
|
||||
@@ -0,0 +1,40 @@
|
||||
[network1]
|
||||
Name=Home
|
||||
Ip=auto
|
||||
Dns=auto
|
||||
Wins=auto
|
||||
Adapter=LAN-Verbindung
|
||||
|
||||
[network2]
|
||||
Name=FHG
|
||||
Ip=129.26.164.188
|
||||
Subnet=255.255.248.0
|
||||
Gateway=129.26.160.1
|
||||
Dns=129.26.165.177
|
||||
Wins=129.26.165.167
|
||||
Adapter=LAN-Verbindung
|
||||
|
||||
[network3]
|
||||
Name=Test
|
||||
Ip=10.15.20.25
|
||||
Subnet=255.255.255.0
|
||||
Gateway=0.0.0.0
|
||||
Dns=none
|
||||
Wins=none
|
||||
Adapter=LAN-Verbindung
|
||||
|
||||
[network4]
|
||||
Name=Wlan Home
|
||||
Ip=10.15.20.106
|
||||
Subnet=255.255.0.0
|
||||
Gateway=10.15.20.200
|
||||
Dns=10.15.20.243
|
||||
Wins=10.15.20.243
|
||||
Adapter=Drahtlosnetzwerkverbindung
|
||||
|
||||
[network5]
|
||||
Name=Wlan Auto
|
||||
Ip=auto
|
||||
Dns=auto
|
||||
Wins=auto
|
||||
Adapter=Drahtlosnetzwerkverbindung
|
||||
@@ -0,0 +1,7 @@
|
||||
[openvpn1]
|
||||
Name=FH
|
||||
Config=wlanfb02.ovpn
|
||||
|
||||
[openvpn2]
|
||||
Name=Home
|
||||
Config=home.ovpn_
|
||||
Reference in New Issue
Block a user