代理服务器实现原理_U盘病毒专杀工具__supko's blog_百度空间

       可以作为代理服务器为其它主机进行代理服务,如果本机使用,你会发现有意想不到的功能,可以看到以前不能看到的东西= =。


主要文件有两个,一个Form1.cs,一个代理类:Proxy.cs。

(如果想本机使用意想不到的功能,在Proxy.cs中需要稍微修改几处代码)

主界面Form1.cs代码为:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using WebProxyto;
namespace WebProxyto
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
        private void RunStart()
        {
            const int port = 8000;

            setieproxy("127.0.0.1", port.ToString());

            TcpListener tcplistener = new TcpListener(port);

            label1.BeginInvoke(new System.EventHandler(MSG),"侦听端口号: " + port.ToString());

            tcplistener.Start();
           
            while (true)
            {
                Socket socket = tcplistener.AcceptSocket();
                //并获取传送和接收数据的Scoket实例
                Proxy proxy = new Proxy(socket,textBox1);
                //Proxy类实例化
                Thread thread = new Thread(new ThreadStart(proxy.Run));
                //创建线程
                thread.Start();
                //启动线程
            }
        }

        private void MSG(object sender, EventArgs e)
        {
            label1.Text = sender.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Thread RR = new Thread(new ThreadStart(RunStart));
            RR.Start();

        }
        private void setieproxy(string ProxyServer, string port)
        {
            Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);

            //设置代理可用
            rk.SetValue("ProxyEnable", 1);
            //设置代理IP和端口
            rk.SetValue("ProxyServer", ProxyServer + ":" + port);
            rk.Close();

        }

        private void stopproxy()
        {
            Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);

            //设置代理不可用
            rk.SetValue("ProxyEnable", 0);
            rk.Close();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           

        }


    }
}

代理类Proxy.cs代码为:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace WebProxyto
{
    class Proxy:Form
    {
        public Proxy(Socket socket,TextBox Te)
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            this.clientSocket = socket;

            this.TexInfo = Te;
        }

        TextBox TexInfo;

        Socket clientSocket;

        Byte[] read = new byte[1024];

      
        Byte[] Buffer = null;
       
        Encoding ASCII = Encoding.ASCII;
       
        Byte[] RecvBytes = new Byte[4096];
       
        public void Run()
        {
            string clientmessage = " ";
           
            string URL = " ";
           
            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();
            }
           
            string part1 = clientmessage.Substring(index1 + 1, index2 - index1);
            int index3 = part1.IndexOf('/', index1 + 8);
            int index4 = part1.IndexOf(' ', index1 + 8);
            int index5 = index4 - index3;
            URL = part1.Substring(index1 + 4, (part1.Length - index5) - 8);
            try
            {
                IPHostEntry IPHost = Dns.Resolve(URL);
                TexInfo.BeginInvoke(new System.EventHandler(MSS), "请求url: " + URL);

               
                TexInfo.BeginInvoke(new System.EventHandler(MSS), "远程主机名: " + IPHost.HostName);
                string[] aliases = IPHost.Aliases;
                IPAddress[] address = IPHost.AddressList;
             
                TexInfo.BeginInvoke(new System.EventHandler(MSS), "Web服务器IP地址:" +address[0]);
               
                IPEndPoint ipEndpoint = new IPEndPoint(address[0], 80);

                Socket IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
               
                IPsocket.Connect(ipEndpoint);
              
                if (IPsocket.Connected)
                   
                    TexInfo.BeginInvoke(new System.EventHandler(MSS), "Socket 正确连接!");

                string GET = clientmessage;

                Byte[] ByteGet = ASCII.GetBytes(GET);
                IPsocket.Send(ByteGet, ByteGet.Length, 0);
               
                Int32 rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
               
               
                TexInfo.BeginInvoke(new System.EventHandler(MSS), "接收字节数:" +rBytes.ToString());
                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 esx1)
            {
                TexInfo.BeginInvoke(new System.EventHandler(MSS), esx1.Message);
            }

        }

        private void LogWritebaidu(string txt)
        {
           
            TexInfo.AppendText(txt + Environment.NewLine);
            TexInfo.SelectionStart = TexInfo.Text.Length;

        }
        private void MSS(object sender, EventArgs e)
        {
            LogWritebaidu(sender.ToString());
        }
       
        private int ReadMessage(byte[] ByteArray, ref Socket s, ref String clientmessage)
        {
            int bytes=0;
            try
            {
                bytes = s.Receive(ByteArray, 1024, 0);
                string messagefromclient = Encoding.ASCII.GetString(ByteArray);
                clientmessage = (String)messagefromclient;
            }
            catch (Exception esx2)
            {
                TexInfo.BeginInvoke(new System.EventHandler(MSS), esx2.Message);
            }
            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);
           
            TexInfo.BeginInvoke(new System.EventHandler(MSS), "传送字节数:" + length.ToString());
            s.Send(Buffer, length, 0);
        }

      

    }
}



郑重声明:资讯 【代理服务器实现原理_U盘病毒专杀工具__supko's blog_百度空间】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——