Пример #1
0
        ///<summary>
        /// Process Individual client
        /// </summary>
        ///
        ///
        private async void HandleConnectionAsync(TcpClient tcpClient)
        {
            string clientInfo = tcpClient.Client.RemoteEndPoint.ToString();

            LogMessage(string.Format("Got connection request from {0}", clientInfo));
            try
            {
                using (var networkStream = tcpClient.GetStream())
                    using (var reader = new StreamReader(networkStream))
                        using (var writer = new StreamWriter(networkStream))
                        {
                            writer.AutoFlush = true;
                            while (true)
                            {
                                var dataFromServer = await reader.ReadLineAsync();

                                if (string.IsNullOrEmpty(dataFromServer))
                                {
                                    break;
                                }
                                LogMessage("RECEIVED:" + dataFromServer);


                                var m   = new genericXMessage();
                                var mec = new DecryptMessageInput(new DecompressMessageInput(m));
                                mec.MsgIn = dataFromServer;
                                await mec.process();

                                Console.WriteLine(mec.Debug);

                                var x = SXMessage.Parse(mec.MsgOut);

                                var xec = new EncryptMessageOutput(new CompressMessageOutput(x));
                                xec.MsgIn = mec.MsgOut;
                                await xec.process();

                                Console.WriteLine(xec.Debug);
                                LogMessage("SENT:" + xec.MsgOut);
                                await writer.WriteLineAsync(xec.MsgOut);

                                //await writer.WriteLineAsync(dataFromServer.ToUpper());
                                //LogMessage("SENT:" + dataFromServer.ToUpper());
                            }
                        }
            }
            catch (Exception exp)
            {
                LogMessage(exp.Message);
            }
            finally
            {
                LogMessage(string.Format("Closing the client connection - {0}",
                                         clientInfo));
                tcpClient.Close();
            }
        }
Пример #2
0
        //public static void ReadCallback(IAsyncResult ar)
        //{
        //    String content = String.Empty;
        //    String encryptedContent = "";
        //    // Retrieve the state object and the handler socket
        //    // from the asynchronous state object.
        //    StateObject state = (StateObject)ar.AsyncState;
        //    Socket handler = state.workSocket;

        //    // Read data from the client socket.
        //    int bytesRead = handler.EndReceive(ar);
        //    string _msgOut = "";
        //    if (bytesRead > 0)
        //    {
        //        // There  might be more data, so store the data received so far.
        //        state.sb.Append(Encoding.ASCII.GetString(
        //            state.buffer, 0, bytesRead));

        //        // Check for end-of-file tag. If it is not there, read
        //        // more data.


        //        try
        //        {
        //            encryptedContent = state.sb.ToString();
        //            bool _isComp;
        //            content = StringCipher.Decrypt(encryptedContent, out _isComp);
        //            Console.WriteLine("-ENCRYPTED-------------------------------\n- Client -> Server: {0} bytes\n-----------------------------------------\n{1}",
        //               encryptedContent.Length, encryptedContent);


        //            // All the data has been read from the
        //            // client. Display it on the console.

        //            Console.WriteLine("-DECRYPTED-------------------------------\n- Client -> Server: {0} bytes\n-----------------------------------------\n{1}",
        //                content.Length, content);

        //            if (content.IndexOf("</message>") > -1)
        //            {
        //                XEspackSocksMessage _message = new XEspackSocksMessage(content);
        //                switch (_message.Action)
        //                {
        //                    case "Start Session":
        //                        _msgOut = StartSession(_message.Data).ToString();
        //                        break;
        //                    case "Database Connection":
        //                        _msgOut = launchConnection(_message.Data).ToString();
        //                        break;
        //                    case "Stored Procedure":
        //                        try
        //                        {
        //                            _msgOut = launchProcedure(_message.Data).ToString();
        //                        }
        //                        catch { }
        //                        break;
        //                    case "Recordset":
        //                        try
        //                        {
        //                            _msgOut = lauchRecordset(_message.Data).ToString();
        //                        }
        //                        catch { }
        //                        break;
        //                }
        //            }


        //            // Return result value. Display it on the console.
        //            Console.WriteLine("-DECRYPTED-------------------------------\n- Server -> Client: {0} bytes\n-----------------------------------------\n{1}",
        //            _msgOut.Length, _msgOut);
        //            var _encMsgOut = StringCipher.Encrypt(_msgOut, true);
        //            Console.WriteLine("-ENCRYPTED-------------------------------\n- Server -> Client: {0} bytes\n-----------------------------------------\n{1}",
        //            _encMsgOut.Length, _encMsgOut);
        //            Send(handler, _encMsgOut);
        //        }
        //        catch
        //        {
        //            // Not all data received. Get more.
        //            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
        //            new AsyncCallback(ReadCallback), state);
        //        }
        //    }
        //}

        public async static void ReadCallback(IAsyncResult ar)
        {
            String content = String.Empty;
            String msgIn   = "";
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state   = (StateObject)ar.AsyncState;
            Socket      handler = state.workSocket;

            // Read data from the client socket.
            int    bytesRead = handler.EndReceive(ar);
            string _msgOut   = "";

            if (bytesRead > 0)
            {
                // There  might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(
                                    state.buffer, 0, bytesRead));

                // Check for end-of-file tag. If it is not there, read
                // more data.
                try
                {
                    var m   = new genericXMessage();
                    var mec = new DecryptMessageInput(new DecompressMessageInput(m));
                    mec.MsgIn = state.sb.ToString();
                    await mec.process();

                    Console.WriteLine(mec.Debug);

                    var x = SXMessage.Parse(mec.MsgOut);

                    var xec = new EncryptMessageOutput(new CompressMessageOutput(x));
                    xec.MsgIn = mec.MsgOut;
                    await xec.process();

                    Console.WriteLine(xec.Debug);

                    Send(handler, xec.MsgOut);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    // Not all data received. Get more.
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                         new AsyncCallback(ReadCallback), state);
                }
            }
        }