Web代理在C#中创建,需要提示进行身份验证

时间:2010-07-07 15:12:52

标签: c# authentication sockets proxy

我想要一个使用TcpListener的小型Web代理程序,该程序侦听特定端口以进行Web调用。收到呼叫后,它会创建一个套接字,获取呼叫的标头,将其转发到请求的网页上,读取响应并将其返回给执行请求的客户端。代理工作很可爱,但现在我想限制谁可以通过代理用户名来访问网络。我如何将身份验证纳入我的代理?

这是我的代码:

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

命名空间WebProxy2 {     公共部分类Form1:表格     {         public 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 个答案:

答案 0 :(得分:1)

目前尚不清楚您是否实施了自己的HTTP代理协议(或多或少是基于Host标头的简单TCP重定向),或者您是否使用标准HTTP代理协议。

如果您选择标准代理(可能更好,除非您有充分的理由不这样做),您应该可以使用Proxy-AuthenticateProxy-Authorization和{{1例如,标头而不是Proxy-Authentication-InfoWWW-AuthenticateAuthorization用于HTTP基本或摘要式身份验证。