2010-07-07 43 views
1

我使用一個TcpListener來監聽一個小型Web代理程序,該程序在特定的端口上監聽Web調用。一旦它接收到一個調用,它將創建一個套接字,獲取調用的標題,將其轉發到所請求的網頁上,讀取響應並將其返回給執行請求的客戶端。代理工作可愛,但現在我想限制誰通過代理通過提示用戶名訪問網絡。我將如何將身份驗證合併到我的代理上?Web代理在C#中創建,需要提示進行身份驗證

這裏是我的代碼:

using System;使用System.Net的 ;使用System.Net.Sockets的 ; using System.Text;使用System.IO的 ;使用System.Threading的 ;使用System.Windows.Forms的 ; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;

命名空間WebProxy2 { 公共部分Form1類:形式 { 公共BOOL proxyOn = FALSE;

public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    public void log(string logText) 
    { 
     Application.DoEvents(); 
     textBox1.Text += logText + "\r\n"; 
     StreamWriter sw = new StreamWriter(@"c:\proxyLog.txt",true); 
     sw.WriteLine(logText); 
     sw.Close(); 
     Application.DoEvents(); 
    } 

    class WebProxy2 
    { 
     Socket clientSocket; 
     Form1 proxyGui; 
     Byte[] read = new byte[1024]; 
     Byte[] Buffer = null; 
     Encoding ASCII = Encoding.ASCII; 
     const string HTTP_VERSION = "HTTP/1.0"; 
     const string CRLF = "\r\n"; 
     Byte[] RecvBytes = new Byte[4096]; 

     public WebProxy2(Socket socket, Form1 form) 
     { 
      this.clientSocket = socket; 
      this.proxyGui = form; 
     } 

     public void run() 
     { 
      Application.DoEvents(); 
      String clientmessage = " ", sURL = " "; 

      int bytes = readmessage(read, ref clientSocket, ref clientmessage); 
      if (bytes == 0) 
      { 
       return; 
      } 
      int index1 = clientmessage.IndexOf(' '); 
      int index2 = clientmessage.IndexOf(' ', index1 + 1); 
      if ((index1 == -1) || (index2 == -1)) 
      { 
       throw new IOException(); 
      } 
      proxyGui.log("Connecting to Site: " + clientmessage.Substring(index1 + 1, index2 - index1)); 
      proxyGui.log("Connection from " + clientSocket.RemoteEndPoint); 
      string part1 = clientmessage.Substring(index1 + 1, index2 - index1); 
      int index3 = part1.IndexOf('/', index1 + 8); 
      int index4 = part1.IndexOf(' ', index1 + 8); 
      int index5 = index4 - index3; 
      sURL = part1.Substring(index1 + 4, (part1.Length - index5) - 8); 
      try 
      { 


       IPHostEntry IPHost = Dns.Resolve(sURL); 
       proxyGui.log("Request resolved: " + IPHost.HostName); 
       string[] aliases = IPHost.Aliases; 
       IPAddress[] address = IPHost.AddressList; 
       proxyGui.log(address[0].ToString()); 
       IPEndPoint sEndpoint = new IPEndPoint(address[0], 80); 
       Socket IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 

       IPsocket.Connect(sEndpoint); 
       if (IPsocket.Connected) 
        proxyGui.log("Socket connect OK"); 
       string GET = clientmessage; 
       Byte[] ByteGet = ASCII.GetBytes(GET); 
       IPsocket.Send(ByteGet, ByteGet.Length, 0); 
       Int32 rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0); 
       proxyGui.log("Recieved " + rBytes); 
       //Buffer = RecvBytes; 
       String strRetPage = null; 
       strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes); 
       while (rBytes > 0) 
       { 
        rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0); 
        strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, rBytes); 
       } 
       IPsocket.Shutdown(SocketShutdown.Both); 
       IPsocket.Close(); 
       sendmessage(clientSocket, strRetPage); 
      } 
      catch (Exception exc2) 
      { 
       proxyGui.log(exc2.ToString()); 
      } 
     } 

     private int readmessage(byte[] ByteArray, ref Socket s, ref String clientmessage) 
     { 

      int bytes = s.Receive(ByteArray, 1024, 0); 
      string messagefromclient = Encoding.ASCII.GetString(ByteArray); 
      clientmessage = (String)messagefromclient; 
      return bytes; 
     } 
     private void sendmessage(Socket s, string message) 
     { 
      Buffer = new Byte[message.Length + 1]; 
      int length = ASCII.GetBytes(message, 0, message.Length, Buffer, 0); 
      s.Send(Buffer, length, 0); 
     } 

    } 

    const int port = 8080; 
    TcpListener tcplistener = new TcpListener(port); 
    private void btnProxy_Click_1(object sender, EventArgs e) 
    { 
     proxyOn = true; 
     log("Listening on port " + port + " " + DateTime.Now.ToString()); 

     tcplistener.Start(); 
     while (proxyOn) 
     { 
      Application.DoEvents(); 
      if (tcplistener.Pending()) 
      { 
       Socket socket = tcplistener.AcceptSocket(); 
       WebProxy2 webproxy = new WebProxy2(socket, this); 
       webproxy.run(); 
      } 
     Application.DoEvents(); 
     } 
    } 

    private void btnStop_Click_1(object sender, EventArgs e) 
    { 
     Application.DoEvents(); 
     proxyOn = false; 
     tcplistener.Stop(); 
     log("Proxy stopped !!! " + DateTime.Now.ToString()); 
     Application.DoEvents(); 
    } 

} 

}

回答

1

目前還不清楚是否已實施自己的HTTP代理協議(更多或更少,基於Host頭一個簡單的TCP重定向),或者如果您正在使用標準的HTTP代理協議。

如果你去爲標準代理(這可能是更好的,除非你有一個很好的理由不這樣做),你應該能夠使用Proxy-AuthenticateProxy-AuthorizationProxy-Authentication-Info頭,而不是WWW-AuthenticateAuthorizationAuthentication-Info(例如分別用於HTTP基本或摘要式身份驗證。

+0

請看看我的代碼,以獲得我在做什麼的一些澄清。請告訴我更多您建議的標準代理 – 2010-07-07 16:35:25

+0

您是否至少在瀏覽器中將其用作常規HTTP代理(如此配置)? – Bruno 2010-07-07 16:49:38

+0

是的,我的瀏覽器被配置爲一個普通的http代理 – 2010-07-08 05:53:02

相關問題