【问题标题】:Double reverse TCP proxy in C#?C#中的双重反向TCP代理?
【发布时间】:2016-12-27 22:18:24
【问题描述】:

编辑:意识到我需要在 C# 上实现它: https://en.wikipedia.org/wiki/Traversal_Using_Relays_around_NAT

我会努力找一个,如果有人有的话,我会很感激的,谢谢!

我可能只是当场编造了这个名字。

我想要做的是让一台计算机位于 NAT 之后,并且需要提供连接到服务器的服务。 然后,我将使用第三台计算机连接到该服务器,并与第一台计算机启动的 TCP 流进行交互,就像我直接连接到它一样。

找到了一种创建转发代理的方法,效果很好: blog.brunogarcia.com/2012/10/simple-tcp-forwarder-in-c.html

using System;
using System.Net;
using System.Net.Sockets;

namespace TcpProxy
{
    class Program
    {
        static void Main(string[] args)
        {
            new TcpForwarderSlim().Start(
    new IPEndPoint(IPAddress.Parse("127.0.0.1"), int.Parse("69")),
    new IPEndPoint(IPAddress.Parse("91.198.174.192"), int.Parse("80")));
        }

    }
    public class TcpForwarderSlim
    {
        private readonly Socket _mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        public void Start(IPEndPoint local, IPEndPoint remote)
        {
            _mainSocket.Bind(local);
            _mainSocket.Listen(10);

            while (true)
            {
                var source = _mainSocket.Accept();
                var destination = new TcpForwarderSlim();
                var state = new State(source, destination._mainSocket);
                destination.Connect(remote, source);
                source.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, OnDataReceive, state);
            }
        }

        private void Connect(EndPoint remoteEndpoint, Socket destination)
        {
            var state = new State(_mainSocket, destination);
            _mainSocket.Connect(remoteEndpoint);
            _mainSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, OnDataReceive, state);
        }

        private static void OnDataReceive(IAsyncResult result)
        {
            var state = (State)result.AsyncState;
            try
            {
                var bytesRead = state.SourceSocket.EndReceive(result);
                if (bytesRead > 0)
                {
                    state.DestinationSocket.Send(state.Buffer, bytesRead, SocketFlags.None);
                    state.SourceSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, OnDataReceive, state);
                }
            }
            catch
            {
                state.DestinationSocket.Close();
                state.SourceSocket.Close();
            }
        }

        private class State
        {
            public Socket SourceSocket { get; private set; }
            public Socket DestinationSocket { get; private set; }
            public byte[] Buffer { get; private set; }

            public State(Socket source, Socket destination)
            {
                SourceSocket = source;
                DestinationSocket = destination;
                Buffer = new byte[8192];
            }
        }
    }
}

谁能指出我正确的方向?

【问题讨论】:

    标签: c# tcp proxy


    【解决方案1】:

    UPNP 可以在正确配置的路由器上打开端口。

    STUN、ICE 和TURN 在端口无法打开时使用中间服务器进行连接。有开源的server implementations

    UDP 可以使用UDP Hole Punching

    This answer 提供了更详细的解释,但没有太多 C# 帮助。

    【讨论】:

      猜你喜欢
      • 2019-03-14
      • 2017-05-31
      • 2018-06-16
      • 2013-02-17
      • 2017-12-03
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      相关资源
      最近更新 更多