Пример #1
0
        public void HandleConnectionRequest(Connection acceptedConnection)
        {
            Connection remoteSocksServerConnection = null;

            try
            {
                remoteSocksServerConnection = Connection.ConnectOverHttpProxy(_RemoteSocksServerHost, _RemoteSocksServerPort);
            }
            catch
            {
                acceptedConnection.Close();
                return;
            }

            remoteSocksServerConnection.SetObserver(this);

            remoteSocksServerConnection.Send(Socks4Request.Build(Socks4Request.REQUEST_TYPE_CONNECT, _RedirectHost, _RedirectPort, "").ToByteArray());

            _WaitHandle.WaitOne();

            if (_RemoteSocksServerResponse == null || !_RemoteSocksServerResponse.Success)
            {
                SocksCommon.SendSocksError(acceptedConnection);
                return;
            }

            SocksCommon.SendSocksSuccess(acceptedConnection, _RemoteSocksServerResponse.Port, IPAddress.Parse(_RemoteSocksServerResponse.ResolvedIP));

            acceptedConnection.SetObserver(new TransmissionRedirectConnectionObserver(remoteSocksServerConnection, true));
            remoteSocksServerConnection.SetObserver(new TransmissionRedirectConnectionObserver(acceptedConnection, false));
            acceptedConnection.BeginReceiving();
        }
        public override void HandleReceive(byte[] receiveBuffer, int offset, int count)
        {
            byte[] requestASCII = new byte[count];
            Array.ConstrainedCopy(receiveBuffer, offset, requestASCII, 0, count);
            Console.WriteLine("Socks request received: {0}\r\n{1}", Encoding.ASCII.GetString(requestASCII), Log.ByteArrayToHexString(requestASCII));

            Socks4Request request = Socks4Request.Parse(receiveBuffer);

            if (request == null)
            {
                Console.WriteLine("Could not parse socks request");
                SocksCommon.SendSocksError(_Client);
                return;
            }

            Connection requestedConnection = null;

            try
            {
                requestedConnection = Connection.Connect(request.RemoteHostIP, request.RemotePort);
                Console.WriteLine("{0} --> Socks Request successful, connected to {1}:{2}", _Client, request.RemoteHostIP, request.RemotePort);
            }
            catch
            {
                SocksCommon.SendSocksError(_Client);
                return;
            }

            requestedConnection.SetObserver(new TransmissionRedirectConnectionObserver(_Client, false));
            _Client.SetObserver(new TransmissionRedirectConnectionObserver(requestedConnection, true));

            // _Client will continue to receive, but destination needs to start receiveing.
            requestedConnection.BeginReceiving();

            SocksCommon.SendSocksSuccess(_Client, request.RemotePort, request.RemoteHostIP);
        }