Esempio n. 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();
            }
        }
Esempio n. 2
0
        public async Task <XDocument> Transmit(XDataMessageRequest x)
        {
            try
            {
                Status = CommStatus.CONNECTED;
                if (_session == null)
                {
                    await getSocksConnection();
                }
                var msgOut = new Message();
                var encryptedCompressedMsgOut = new EncryptMessageOutput(new CompressMessageOutput(msgOut))
                {
                    MsgIn = x.ToString()
                };
                await encryptedCompressedMsgOut.process();

                var Socket = new AsynchronousSocketClient()
                {
                    ServerIP = IP, ServerPort = Port
                };
                var _result = await Socket.AsyncConversation(encryptedCompressedMsgOut.MsgOut);

                if (_result == "" || _result.Substring(0, 5) == "ERROR")
                {
                    throw new Exception(_result);
                }
                var msgIn = new Message();
                var decryptedUncompressedMsgIn = new DecryptMessageInput(new DecompressMessageInput(msgIn))
                {
                    MsgIn = _result
                };
                await decryptedUncompressedMsgIn.process();

                //Thread.Sleep(2000);
                //var m = new TransmitEntcryptedCompressedMessage() { ServerIP = IP, Port = Port };
                //m.MsgIn = x.ToString();
                //await m.process();
                //var _res= XDocument.Parse(m.MsgOut);
                var _res = XDocument.Parse(decryptedUncompressedMsgIn.MsgOut);
                Status = CommStatus.OFFLINE;
                return(_res);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Status = CommStatus.ERROR;
                throw e;
            }
        }
Esempio n. 3
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);
                }
            }
        }
Esempio n. 4
0
        public async Task <string> Transmit(string s)
        {
            var msgOut = new Message();
            var encryptedCompressedMsgOut = new EncryptMessageOutput(new CompressMessageOutput(msgOut))
            {
                MsgIn = s
            };
            await encryptedCompressedMsgOut.process();

            var Socket = new AsynchronousSocketClient()
            {
                ServerIP = IP, ServerPort = Port
            };
            var _result = Socket.SyncConversation(encryptedCompressedMsgOut.MsgOut);
            var msgIn   = new Message();
            var decryptedUncompressedMsgIn = new DecompressMessageInput(new DecryptMessageInput(msgIn))
            {
                MsgIn = _result
            };
            await decryptedUncompressedMsgIn.process();

            return(decryptedUncompressedMsgIn.MsgOut);
        }