3

如何创建一个透明地将连接转发到新 IP 地址/端口的程序?

例如,如果客户端启动 TCP 连接,IP 地址为 111.111.111.111,TCP 端口为 111,则程序可以配置为转发该 IP/端口 222.222.222.222:222 的数据包。

透明地,我的意思是:

  • 最小的性能损失
  • 无需调整客户端或最终目标软件/配置(就像透明 Web 代理不需要在浏览器中进行显式设置一样)
  • 处理尽可能低的操作系统网络层

我想用 .NET、C# 和 Windows 来做这件事。但出于各种但未指定的原因,我不想创建一个简单的 TCP 代理。如果无法做到这一点,那么在没有可能的解决方案的情况下,这可能是一个可以接受的答案。

4

1 回答 1

9

这是我几周前写的一个实验:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Globalization;
using System.Threading;

namespace TcpTunnel {

public static class Program {

    public static void Main(String[] args) {

        if( args.Length != 3 ) {

            Console.WriteLine("<remoteAddress> <remotePort> <listenPort>");
            return;
        }

        ServerConfiguration config;

        try {

            config = new ServerConfiguration( args[0], args[1], args[2] );

        } catch(ArgumentException aex) {

            Console.WriteLine("<remoteAddress> <remotePort> <listenPort>");
            Console.WriteLine( aex.Message );
            return;
        }

        Thread serverThread = new Thread( ServerThread );
        serverThread.Start( config );

        Console.WriteLine("Enter \"q<enter>\" to stop program.");

        String line;
        while( (line = Console.ReadLine()).ToUpperInvariant() != "Q" );

        config.ServerSocket.Shutdown(SocketShutdown.Both);
        config.ServerSocket.Close();
    }

    private class ServerConfiguration {

        public IPAddress RemoteAddress;
        public UInt16    RemotePort;
        public UInt16    ListenPort;

        public ServerConfiguration(String remoteAddress, String remotePort, String listenPort) {

            RemoteAddress = IPAddress.Parse( remoteAddress );
            RemotePort    = UInt16   .Parse( remotePort, NumberStyles.Integer, CultureInfo.InvariantCulture );
            ListenPort    = UInt16   .Parse( listenPort, NumberStyles.Integer, CultureInfo.InvariantCulture );
        }

        public Boolean RunServer = true;

        public Socket  ServerSocket;
    }

    private static void ServerThread(Object configObj) {

        ServerConfiguration config = (ServerConfiguration)configObj;

        ///////////////////////////////////////
        // Setup  



        ///////////////////////////////////////
        // Wait for client

        Socket serverSocket = config.ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Unspecified);

        serverSocket.Bind( new IPEndPoint( IPAddress.Any, config.ListenPort ) );
        serverSocket.Listen(1);

        Console.WriteLine("Now listening for port {0}, will forward to {1}.", config.ListenPort, config.RemotePort);

        while( config.RunServer ) {

            Socket client = serverSocket.Accept();

            Thread clientThread = new Thread( ClientThread );
            clientThread.Start( new ClientContext() { Config = config, Client = client } );
        }

    }

    private class ClientContext {

        public ServerConfiguration Config;
        public Socket              Client;
    }

    private static void ClientThread(Object contextObj) {

        ClientContext context = (ClientContext)contextObj;

        Socket              client = context.Client;
        ServerConfiguration config = context.Config;

        ///////////////////////////////////////
        // Connect to remote server.

        IPEndPoint remoteEndPoint = new IPEndPoint( config.RemoteAddress, config.RemotePort );

        Socket remote = new Socket( remoteEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);

        remote.Connect( remoteEndPoint );

        ///////////////////////////////////////
        // Run tunnel.

        Console.WriteLine("Running tunnel.");

        Byte[] buffer = new Byte[4096];

        for(;;) {

            if( client.Available > 0 ) {

                Int32 count = client.Receive( buffer );
                if( count == 0 ) return;

                remote.Send( buffer, count, SocketFlags.None );
            }

            if( remote.Available > 0 ) {

                Int32 count = remote.Receive( buffer );
                if( count == 0 ) return;

                client.Send( buffer, count, SocketFlags.None );
            }
        }
    }

}

}
于 2013-03-31T10:03:46.213 回答