2011-01-31 107 views
0

我需要构建一个Windows服务来监视网络(IP)并相应地修改代理设置。Windows服务观看网络

服务将被安装,并且应该监视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

我意识到这并不直接回答你的问题。但它可能有帮助...而不是创建自己的代码来更改代理设置,也许你可以使用http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol? – 2011-01-31 16:36:42

+0

谢谢,但似乎有一定的局限性,我认为会造成问题与我们的企业网络,比如我们保持浏览器亲自到用户 – RobertPitt 2011-01-31 18:12:03

回答

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; 

OnStart()回调您的Windows服务,创建线程,将做的工作:

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

但愿,这足以让你开始。

0

不知道有关修改代理服务器设置,但对于监控你需要使用WMI

+1

System.Net命名空间处理此,太 - 不知道WMI ..? – 2011-01-31 16:03:31

+0

我宁愿事件,而不是使用System.Net进行轮询,因为这需要很小的资源。 – RobertPitt 2011-01-31 16:19:47

0

您需要定义您遇到问题的部分,并将此短语定义为特定问题。

这里是你的待办事项清单:(?想必Internet Explorer代理设置)

  1. 确定机器的IP地址(可以有很多),并就他们的一些判断
  2. 修改代理设置
  3. 集成这一功能为Windows服务,或者使用后台线程

这不是从你的问题不清楚是什么,你已经尝试过,也许ÿ你可以举一个你遇到过的问题的例子,你试图解决这个问题,有人可以提供一些帮助。