Windows服务观看网络

发布于 2024-10-15 06:30:05 字数 2337 浏览 1 评论 0原文

我需要构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余罪 2024-10-22 06:30:05

我也不清楚修改代理设置,但至于监控网络本身,我想我可以提供帮助。

为了监视网络上的 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();
}

OnStop() 回调中对于 Windows 服务,您需要向线程发出关闭信号:

protected override void OnStop()
{
    _shutdownEvent.Set();
    if (!_thread.Join(3000)) { // give the thread 3 seconds to stop
        _thread.Abort();
    }
}

希望这足以让您开始。

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:

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

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:

using System.Threading;
private ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
private Thread _thread;
private Socket _socket;

In the OnStart() callback of your Windows Service, create the thread that will do the work:

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();
}

In the OnStop() callback of your Windows Service, you need to signal the thread to shutdown:

protected override void OnStop()
{
    _shutdownEvent.Set();
    if (!_thread.Join(3000)) { // give the thread 3 seconds to stop
        _thread.Abort();
    }
}

Hopefully, that gives you enough to get started.

趁微风不噪 2024-10-22 06:30:05

不确定是否修改代理设置,但为了进行监控,您需要使用 WMI

Not sure about modifying the proxy settings, but for monitoring you'll need to use WMI.

倾听心声的旋律 2024-10-22 06:30:05

您需要定义您遇到问题的部分,并将其表述为一个具体问题。

以下是您的 TODO 列表中的内容:

  1. 确定计算机的 IP 地址(可以有很多),并对它们做出一些判断
  2. 修改代理设置(大概是 Internet Explorer 代理设置?)
  3. 将此功能集成到 Windows 服务中,也许使用后台线程

从你的问题中并不清楚你已经尝试过什么,也许你可以举一个你遇到的问题的例子,你试图做什么来解决它,有人将能够提供一些帮助。

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:

  1. Determine IP addresses of the machine (there can be many), and make some judgement on them
  2. Modify proxy settings (presumably Internet Explorer proxy settings?)
  3. Integrate this functionality into a Windows Service, perhaps using a background thread

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文