话说上一次做完http代理工具后,没有测试QQ代理,回家试了一下,做了一下简单补充,并测试通过。

 

上次的文章提到,所有公司的服务器端口都封了,只剩下几个通讯的已经正在便用的。

于是,在本地XP下试了一下端口劫持,也称端口复用。抢占80端口。

关键代码就一句:tcplistener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

在本地实现抢占80端口后,所有的http请求本地iis的,就出现访问不了的情况,所以我又实现了给IIS中转请求

即收到消息后,分析是否IIS请求,如果是,则转发请求到IIS,其它的就转发给相应其它的网站。

由于转发给本机的IIS请求速度过快,这时候需要适当延时,不然问题又出来了。

端口复用在win2003下是不支持的,我也没办法。

但是可通过不同IP监听同一端口如:192.168.1.48 8000 和192.168.1.49 8000(此时需要为主机分配多个IP)

接下来继续上代码,并提供代码下载:

Proxy.cs

Proxy代理类
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Net.Sockets;
using System.Threading;
namespace TcpProxy
{
    
/// <summary>
    
/// by 路过秋天
    
/// http://www.cnblogs.com/cyq1162
    
/// </summary>
    class Program
    {
      
        
static void Main(string[] args)
        {
                Listen(
80);
        }
        
static void Write(string msg)
        {
            Console.WriteLine(msg);
        }

        
static void Listen(int port)
        {
            Write(
"准备监听端口:" + port);
            System.Net.IPAddress[] ips 
= System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());
            
foreach (System.Net.IPAddress ip in ips)
            {
                Write(ip.ToString());
            }
            System.Net.IPAddress ipp 
= System.Net.IPAddress.Parse("0.0.0.0");
            TcpListener tcplistener 
= new TcpListener(ipp,port);
            
//tcplistener.ExclusiveAddressUse = false;
           tcplistener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
           
//tcplistener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.,ProtocolType.IP);
            try
            {
                tcplistener.Start();
            }
            
catch(Exception err)
            {
                Write(err.Message);
                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(
200);
                    
//启动线程 
                }
            }
            
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);
            }
        }
    }
}

 

Program.cs

 

Main函数入口
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Net.Sockets;
using System.Threading;
namespace TcpProxy
{
    
/// <summary>
    
/// by 路过秋天
    
/// http://www.cnblogs.com/cyq1162
    
/// </summary>
    class Program
    {
      
        
static void Main(string[] args)
        {
                Listen(
80);
        }
        
static void Write(string msg)
        {
            Console.WriteLine(msg);
        }

        
static void Listen(int port)
        {
            Write(
"准备监听端口:" + port);
            System.Net.IPAddress[] ips 
= System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());
            
foreach (System.Net.IPAddress ip in ips)
            {
                Write(ip.ToString());
            }
            System.Net.IPAddress ipp 
= System.Net.IPAddress.Parse("0.0.0.0");
            TcpListener tcplistener 
= new TcpListener(ipp,port);
            
//tcplistener.ExclusiveAddressUse = false;
           tcplistener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
           
//tcplistener.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.,ProtocolType.IP);
            try
            {
                tcplistener.Start();
            }
            
catch(Exception err)
            {
                Write(err.Message);
                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(
200);
                    
//启动线程 
                }
            }
            
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);
            }
        }
    }
}

 

 

OK,打完收下。

点击此处下载代码

posted on 2010-06-07 21:49  路过秋天  阅读(4090)  评论(4编辑  收藏  举报
路过秋天