Windows服务观看网络
我需要构建一个 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();
}
}
}
任何帮助表示赞赏
I need to build a Windows Service to monitor the network (IP) and modify the proxy settings accordingly.
The service will be installed, and should watch the IPs to detect whether it's an internal or external IP.
I have created a basic Windows Service based on guides around the internet but I'm unsure what's the best way to go from here.
From the guides I noticed that the WindowsService
object has some kind of event system, and I'm wondering if it's possible to hook into that?
Here's the basic code.
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();
}
}
}
Any help is appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也不清楚修改代理设置,但至于监控网络本身,我想我可以提供帮助。
为了监视网络上的 IP 流量,您需要创建一个“原始”(或混杂)套接字。你必须在本地机器上拥有管理员权限才能创建这种套接字,但只要你的 Windows 服务在系统帐户下运行,你就应该没问题(顺便说一句,这就是我在我的例子中所做的) 。
要创建原始套接字,请执行以下操作:
您现在可以使用此套接字接收指定本地 IP 地址上的所有传入和传出 IP 数据包。
在 Windows 服务中,添加以下字段:
在 Windows 服务的
OnStart()
回调中,创建将执行该工作的线程:在
OnStop()
回调中对于 Windows 服务,您需要向线程发出关闭信号:希望这足以让您开始。
I, too, am unclear about modifying the proxy settings, but as for monitoring the network itself, I think I can help with that.
In order to monitor the IP traffic on the network, you'll want to create a "raw" (or promiscuous) socket. You have to have admin rights on the local box to create this kind of socket, but as long as your Windows Service is running under the System account, you should be ok (that's what I'm doing in my case, by the way).
To create a raw socket, do something like this:
You can now use this socket to receive all of the incoming and outgoing IP packets on the specified local IP address.
In your Windows Service, add the following fields:
In the
OnStart()
callback of your Windows Service, create the thread that will do the work:In the
OnStop()
callback of your Windows Service, you need to signal the thread to shutdown:Hopefully, that gives you enough to get started.
不确定是否修改代理设置,但为了进行监控,您需要使用 WMI。
Not sure about modifying the proxy settings, but for monitoring you'll need to use WMI.
您需要定义您遇到问题的部分,并将其表述为一个具体问题。
以下是您的 TODO 列表中的内容:
从你的问题中并不清楚你已经尝试过什么,也许你可以举一个你遇到的问题的例子,你试图做什么来解决它,有人将能够提供一些帮助。
You'll need to define the part you're having problems with and phrase this as a specific question.
Here's what's on your TODO list:
It's not clear from your question what you've already tried, perhaps you could give an example of a problem you've been having, what you've tried to do to solve it and someone will be able to provide some help.