我需要构建一个Windows服务来监控网络(IP)并相应地修改代理设置。
将安装该服务,并应观察IP以检测它是内部IP还是外部IP。
我已经基于互联网指南创建了一个基本的Windows服务,但我不确定从这里开始的最佳方式是什么。
从指南中我注意到WindowsService
对象有某种事件系统,我想知道是否有可能挂钩?
这是基本代码。
using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.ComponentModel;
using System.Configuration.Install;
namespace WindowsService
{
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer
{
public WindowsServiceInstaller()
{
ServiceProcessInstaller SPI = new ServiceProcessInstaller();
ServiceInstaller SI = new ServiceInstaller();
//# Service Account Information
SPI.Account = ServiceAccount.LocalSystem;
SPI.Username = null;
SPI.Password = null;
//# Service Information
SI.DisplayName = WindowsService._WindowsServiceName;
SI.StartType = ServiceStartMode.Automatic;
//# set in the constructor of WindowsService.cs
SI.ServiceName = WindowsService._WindowsServiceName;
Installers.Add(SPI);
Installers.Add(SI);
}
}
class WindowsService : ServiceBase
{
public static string _WindowsServiceName = "Serco External Proxy Manager";
public WindowsService()
{
ServiceName = _WindowsServiceName;
EventLog.Log = "Application";
// These Flags set whether or not to handle that specific
// type of event. Set to true if you need it, false otherwise.
CanHandlePowerEvent = true;
CanHandleSessionChangeEvent = true;
CanPauseAndContinue = true;
CanShutdown = true;
CanStop = true;
}
static void Main()
{
ServiceBase.Run(new WindowsService());
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
}
protected override void OnStop()
{
base.OnStop();
}
protected override void OnPause()
{
base.OnPause();
}
protected override void OnContinue()
{
base.OnContinue();
}
}
}
感谢任何帮助
答案 0 :(得分:1)
我也不清楚修改代理设置,但至于监控网络本身,我想我可以帮忙解决这个问题。
为了监控网络上的IP流量,您需要创建一个“原始”(或混杂)套接字。您必须拥有本地框的管理员权限才能创建这种套接字,但只要您的Windows服务在系统帐户下运行,您应该没问题(顺便说一句,这就是我在我的情况下所做的事情)
要创建原始套接字,请执行以下操作:
using System.Net.Sockets;
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
s.Bind(new IPEndPoint(IPAddress.Parse("192.168.0.1"), 0)); // use your local IP
byte[] incoming = BitConverter.GetBytes(1);
byte[] outgoing = BitConverter.GetBytes(1);
s.IOControl(IOControlCode.ReceiveAll, incoming, outgoing);
s.ReceiveBufferSize = 8 * 1024 * 1024; // 8MB
您现在可以使用此套接字接收指定本地IP地址上的所有传入和传出IP数据包。
在Windows服务中,添加以下字段:
using System.Threading;
private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
private Thread _thread;
private Socket _socket;
在Windows服务的OnStart()
回调中,创建将完成工作的线程:
protected override void OnStart(string[] args)
{
_thread = new Thread(delegate() {
// Initialize the socket here
while (!_shutdownEvent.WaitOne(0)) {
// Receive the next packet from the socket
// Process packet, e.g., extract source/destination IP addresses/ports
// Modify proxy settings?
}
// Close socket
});
_thread.Name = "Monitor Thread";
_thread.IsBackground = true;
_thread.Start();
}
在Windows服务的OnStop()
回调中,您需要通知线程关闭:
protected override void OnStop()
{
_shutdownEvent.Set();
if (!_thread.Join(3000)) { // give the thread 3 seconds to stop
_thread.Abort();
}
}
希望,这足以让你开始。
答案 1 :(得分:0)
不确定修改代理设置,但为了监控,您需要使用WMI。
答案 2 :(得分:0)
您需要定义您遇到问题的部分,并将其作为特定问题加以说明。
以下是您的TODO列表中的内容:
从您的问题中不清楚您已尝试过什么,也许您可以举例说明您遇到过的问题,您尝试做什么来解决问题,有人可以提供一些帮助