예제 #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 HandleConnectionRequest(Connection acceptedConnection)
        {
            Socks4Request request = Socks4Request.Build(Socks4Request.REQUEST_TYPE_BIND, _RedirectHost, _RedirectPort, "");

            Connection redirectConnection = null;

            try
            {
                redirectConnection = Connection.Connect(_SocksServerHost, _SocksServerPort);
            }
            catch (Exception exp)
            {
                acceptedConnection.Close();
                return;
            }

            redirectConnection.SetObserver(new PortRedirectOverSocksConnectionObserver(acceptedConnection));
            acceptedConnection.IsBeingTraced = ObservedServer.IsBeingTraced;
            redirectConnection.BeginReceiving();

            redirectConnection.Send(request.ToByteArray());
        }
예제 #3
0
        private static void TestRemoteServer(string remoteHost, int remotePort, string remoteSocksHost, int remoteSocksPort, ref int statuscode, ref string statusmessage)
        {
            Connection testConnection;

            try
            {
                testConnection = Connection.Connect(remoteSocksHost, remoteSocksPort);
            }
            catch (Exception exp)
            {
                statuscode    = 505;
                statusmessage = "Could not connect to the Socks Server: " + exp.Message;
                return;
            }

            try
            {
                testConnection.Send(Socks4Request.Build(Socks4Request.REQUEST_TYPE_CONNECT, remoteHost, remotePort, "").ToByteArray());
                testConnection.BeginReceivingSync();

                byte[] socksRawResponse = testConnection.GetNextSyncReceiveBuffer();
                testConnection.Close();

                Socks4Response socksResponse = Socks4Response.Parse(socksRawResponse);

                if (socksResponse == null || !socksResponse.Success)
                {
                    statuscode    = 504;
                    statusmessage = "Socks server reports that it failed to connect to the remote host";
                }
            }
            catch (Exception exp)
            {
                statuscode    = 504;
                statusmessage = "Failed while trying to connect to the remote host through socks: " + exp.Message;
            }
        }
        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);
        }
예제 #5
0
        public static TcpClient ConnectTcpClientTo(IPAddress proxyAddress, short proxyPort, IPAddress destAddress, short destPort, string asciiUserId, int timeout)
        {
            // create the socket
            var socket = new TcpClient(AddressFamily.InterNetwork);
            socket.ReceiveTimeout = timeout;
            socket.SendTimeout = timeout;

            // connect to the proxy
            socket.Connect(new IPEndPoint(proxyAddress, proxyPort));

            // create the request
            var requestPacket = new Socks4Request(1, destPort, destAddress, asciiUserId);

            // Get the request
            var requestData = requestPacket.GetBytes();

            // Send the request
            socket.GetStream().Write(requestData, 0, requestData.Length);

            // Get the response from teh socket
            var response = new Socks4Response(socket);

            if (response.Version != 0)
            {
                // Unexpected version (must be 0)
                throw new Socks4Exception(string.Format("Invalid version. Expected 0, got {0}.", response.Version));
            }

            if (response.Command < (byte)Socks4ResponseCommand.MinValue || response.Command > (byte)Socks4ResponseCommand.MaxValue)
            {
                // Invalid response command
                throw new Socks4Exception(string.Format("Invalid reply command. Got {0}.", response.Command));
            }

            if (response.Command != (byte)Socks4ResponseCommand.Granted)
            {
                // Request failed
                throw new Socks4Exception((Socks4ResponseCommand)response.Command);
            }

            // Connection made! Return the socket.
            return socket;
        }
예제 #6
0
        public static System.Net.Sockets.Socket ConnectSocketTo(IPAddress proxyAddress, short proxyPort, IPAddress destAddress, short destPort, string asciiUserId)
        {
            // create the socket
            var socket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // connect to the proxy
            socket.Connect(new IPEndPoint(proxyAddress, proxyPort));

            // create the request
            var requestPacket = new Socks4Request(1, destPort, destAddress, asciiUserId);

            // Send the request
            socket.Send(requestPacket.GetBytes());

            // Get the response from teh socket
            var response = new Socks4Response(socket);

            if (response.Version != 0)
            {
                // Unexpected version (must be 0)
                throw new Socks4Exception(string.Format("Invalid version. Expected 0, got {0}.", response.Version));
            }

            if (response.Command < (byte)Socks4ResponseCommand.MinValue || response.Command > (byte)Socks4ResponseCommand.MaxValue)
            {
                // Invalid response command
                throw new Socks4Exception(string.Format("Invalid reply command. Got {0}.", response.Command));
            }

            if (response.Command != (byte)Socks4ResponseCommand.Granted)
            {
                // Request failed
                throw new Socks4Exception((Socks4ResponseCommand)response.Command);
            }

            // Connection made! Return the socket.
            return socket;
        }