2011-01-31 123 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服務,或者使用後臺線程

這不是從你的問題不清楚是什麼,你已經嘗試過,也許ÿ你可以舉一個你遇到過的問題的例子,你試圖解決這個問題,有人可以提供一些幫助。