示例#1
0
        public void Connect(EndPoint remoteEndpoint, Socket destination)
        {
            var state = new ProxyState(_mainSocket, destination);

            _mainSocket.Connect(remoteEndpoint);
            _mainSocket.BeginReceive(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, OnDataReceive, state);
        }
示例#2
0
        private void Loop()
        {
            var listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            listenerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            listenerSocket.Bind(_listenEndPoint);
            listenerSocket.Listen(10);

            while (true)
            {
                var source      = listenerSocket.Accept();
                var destination = new ProxyForwarder();
                var state       = new ProxyState(source, destination.Socket);
                var port        = _onConnection();
                var remote      = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
                destination.Connect(remote, source);
                source.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, ProxyForwarder.OnDataReceive, state);
            }
        }