简单实现Http代理工具

作者: 手机游戏网站  发布:2019-11-26

理之当然希图是想写个工具放到服务器里代理一下晚秋秋的。

没悟出N台(邮电通讯/联通/移动卡塔 尔(英语:State of Qatar)的服务器都把端口全封了。

于是以下代码是在该地质度量试代理网页访谈通过。

 

说下思路再上代码及下载:

实际一句话便是:产生三个Soket,三个担负选取乞求和回发诉求,另二个担负中间转播的去央浼和摄取。

首要一句话正是:每一个央浼产生二个线程管理,管理时根本是的延时管理(不然数据选用不全卡塔尔。

 

现行反革命上代码,Proxy自定义类:

 

图片 1图片 2Proxy

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

//by 路过三秋
namespace TCPProxy
{
    public class Proxy
    {
        Socket clientSocket;//选取和重回
        Byte[] read = null;//存款和储蓄来自顾客端哀告数据包
        Byte[] recvBytes = null;//存款和储蓄中间转播恳求重返的数额

        public Proxy(Socket socket)
        {
            clientSocket = socket;
            recvBytes = new Byte[1024 * 1024];
            clientSocket.ReceiveBufferSize = recvBytes.Length;
            clientSocket.SendBufferSize = recvBytes.Length;
            clientSocket.SendTimeout = 10000;
            clientSocket.ReceiveTimeout = 10000;
        }
        public void Run()
        {
            #region 获取顾客端诉求数据
            string clientmessage = "";
            read = new byte[clientSocket.Available];
            int bytes = ReadMessage(read, ref clientSocket, ref clientmessage);
            if (bytes == 0)
            {
                Write("读取不到多少!");
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
                return;
            }
            int port = 80;
            string url = GetUrl(clientmessage, port);
            #endregion
            
            try
            {
                #region IP解析获取
                IPHostEntry IPHost= Dns.GetHostEntry(url);
                Write("-----------------------------央浼开头---------------------------");
                Write("http://" + IPHost.HostName);
                string[] aliases = IPHost.Aliases;
                IPAddress[] address = IPHost.AddressList;//分析出要寻访的服务器地址
                Write("IP地址:" + address[0]);
                #endregion                

                #region 创造中间转播Socket及建设构造连接
                IPEndPoint ipEndpoint = new IPEndPoint(address[0], port);
                Socket IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //创制连接Web服务器端的Socket对象
                IPsocket.Connect(ipEndpoint);
                //Socket连Web接服务器
                if (IPsocket.Connected)
                {
                    Write("Socket 精确连接!");
                }
                #endregion
                #region 发送中间转播需要
                IPsocket.Send(read, 0);
                Write("发送字节:" + read.Length);
                #endregion
                #region 接收中间转播央求数据
                int length = 0, count = 0;
                do
                {
                    length = IPsocket.Receive(recvBytes, count, IPsocket.Available, 0);
                    count = count + length;
                    Write("正在选择数据..." + length);
                    System.Threading.Thread.Sleep(100);//关键点,需要太快数据选用不全
                }
                while (IPsocket.Available > 0);
                #endregion
                #region 关闭转向央求Socket
                IPsocket.Shutdown(SocketShutdown.Both);
                IPsocket.Close();
                #endregion

                #region 将中间转播央求收到的多寡发回看客端
                clientSocket.Send(recvBytes, 0, count, 0);
                Write("正在重返数据..." + count);
                #endregion
                #region 截至央求,关闭客户端Socket
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();
                recvBytes = null;
                Write("完毕,已关闭连接...");
                Write("-----------------------------伏乞停止---------------------------");
                #endregion
            }
            catch (Exception err)
            {
                System.Console.WriteLine(err.Message + err.Source);
            }

        }
        //从央求头里深入深入分析出url和端口号
        private string GetUrl(string clientmessage, int port)
        {
            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).Trim();

            string url = string.Empty;
            if (!part1.Contains("http://"))
            {
                part1 = "http://" + part1;
            }
            Uri uri = new Uri(part1);
            url = uri.Host;
            port = uri.Port;
            return url;
        }
        //接受客商端的HTTP央浼数据
        private int ReadMessage(byte[] readByte, ref Socket s, ref string clientmessage)
        {
            int bytes = s.Receive(readByte, readByte.Length, 0);
            clientmessage = Encoding.ASCII.GetString(readByte);
            return bytes;
        }

        private void Write(string msg)
        {
            // return;
            System.Console.WriteLine(msg);
        }
    }
}

 

 

在决定台里调用:

 

图片 3图片 4代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Net.Sockets;
using System.Threading;
namespace TCPProxy
{
    class Program
    {
        static void Main(string[] args)
        {
                Listen(8989);
        }
        static void Write(string msg)
        {
            Console.WriteLine(msg);
        }

        static void Listen(int port)
        {
            Write("酌量监听端口:" + port);
            TcpListener tcplistener = new TcpListener(port);
            try
            {
                tcplistener.Start();
            }
            catch
            {
                Write("该端口已被并吞,请改换端口号!!!");
                ReListen(tcplistener);
            }
            Write("确认:y/n (yes or no):");
            string isOK = Console.ReadLine();
            if (isOK == "y")
            {
                Write("成功监听端口:" + port);
                //侦听端口号 
                Socket socket;
                while (true)
                {
                    socket = tcplistener.AcceptSocket();
                    //并拿走传送和选取数据的Scoket实例 
                    Proxy proxy = new Proxy(socket);
                    //Proxy类实例化 
                    Thread thread = new Thread(new ThreadStart(proxy.Run));
                    //创立线程 
                    thread.Start();
                    System.Threading.Thread.Sleep(10);
                    //运营线程 
                }
            }
            else
            {
                ReListen(tcplistener);
            }
        }
        static void ReListen(TcpListener listener)
        {
            if (listener != null)
            {
                listener.Stop();
                listener = null;
            }
            Write("请输入监听端口号:");
            string newPort = Console.ReadLine();
            int port;
            if (int.TryParse(newPort, out port))
            {
                Listen(port);
            }
            else
            {
                ReListen(listener);
            }
        }
    }
}

 

 

自己测量试验:

在展开IE-》工具->Internet选项->链接->局域网设置->代理连接-》输入IP和端口号

拜访网站,查看调节台出口语资源信息息,页面访谈平常,测量检验通过。

 

自然想测量检验下QQ代理,万般无奈本地已被封,服务器也没端口可用。有空归家再测了。

 

提供代码下载

 

本文由银河网站登录发布于手机游戏网站,转载请注明出处:简单实现Http代理工具

关键词:

上一篇:2012将与Hadoop无缝集成
下一篇:诗和远方