Ejemplo n.º 1
0
        private static void Read(object target)
        {
            TcpClient     client  = (TcpClient)target;
            NetworkStream stream  = client.GetStream();
            Crypter       c       = new Crypto.Crypter(new DefaultConfigurationKey());
            StringBuilder decrypt = new StringBuilder();

            byte[] read;
            int    readInt;

            while (true)
            {
                if (!client.Connected)
                {
                    break;
                }

                read = new byte[client.ReceiveBufferSize];
                try
                {
                    readInt = stream.Read(read, 0, read.Length);
                    decrypt.Append(c.Decrypt(Encoding.GetEncoding("ibm850").GetString(read).Replace("\0", "")));

                    Console.WriteLine("From server : {0}", decrypt.Replace("\0", ""));
                    Console.ForegroundColor = ConsoleColor.Gray;
                    decrypt.Clear();
                }
                catch
                {
                }
            }
        }
Ejemplo n.º 2
0
 protected string Encrypt(string data)
 {
     if (PublicKey != null)
         this.crypter = new Crypter(PublicKey);
     else
         throw new ArgumentNullException("PublicKey");
     
     return crypter.Encrypt(data);
 }
Ejemplo n.º 3
0
        public BaseGatewayService()
        {
            string signature = ConfigurationManager.AppSettings["Signature"];
            header = new Header(signature, "texto-app", "");
            Request = new Request(null, header, null);
            Request.QueueWorkItem = new QueueWorkItem();

            crypter = new Crypto.Crypter(new ApplicationSettingKeySym());
        }
Ejemplo n.º 4
0
        protected string Encrypt(string data)
        {
            if (PublicKey != null)
            {
                this.crypter = new Crypter(PublicKey);
            }
            else
            {
                throw new ArgumentNullException("PublicKey");
            }

            return(crypter.Encrypt(data));
        }
Ejemplo n.º 5
0
 public Client(string ip, int port, IKeySym privateKey)
 {
     this.ip = ip;
     this.port = port;
     if (privateKey == null)
     {
         throw new ArgumentNullException("privateKey");
     }
     this.privateKey = privateKey;
     tcpClient = new TcpClient();
     crypter = new Crypter(privateKey);
     encode = Encoding.GetEncoding("ibm850");
     parser = new CommandParser(); 
 }
Ejemplo n.º 6
0
        protected string Encrypt()
        {
            if (PublicKey != null)
                this.crypter = new Crypter(PublicKey);
            else
                throw new ArgumentNullException("PublicKey");

            if (IsRequestValid())
            {
                string command = Newtonsoft.Json.JsonConvert.SerializeObject(this.Request, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings
                {
                    TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects,
                    TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
                });
                return crypter.Encrypt(command);
            }
            return null;
        }
Ejemplo n.º 7
0
        protected string Encrypt()
        {
            if (PublicKey != null)
            {
                this.crypter = new Crypter(PublicKey);
            }
            else
            {
                throw new ArgumentNullException("PublicKey");
            }

            if (IsRequestValid())
            {
                string command = Newtonsoft.Json.JsonConvert.SerializeObject(this.Request, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings
                {
                    TypeNameHandling       = Newtonsoft.Json.TypeNameHandling.Objects,
                    TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple
                });
                return(crypter.Encrypt(command));
            }
            return(null);
        }
Ejemplo n.º 8
0
        public static string CreateCommand(string signature, string input, IKeySym privateKey)
        {
            IParser<ParamArgs> parser = new CommandParser(); 
            ParamArgs output = parser.Parse(input);
            if (output.Count == 0) throw new OperationCanceledException("Could not parse command.");

            Header header = new Header(signature, "", output.Get<string>("Command"));
            string jsonHeader = Newtonsoft.Json.JsonConvert.SerializeObject(header);

            DynamicData detail = new DynamicData();
            foreach (string key in output.ToDictionary().Keys)
            {
                if (!key.Equals("Command"))
                {
                    detail.Add(key, output.Get<object>(key));
                }
            }
            string jsonDetail = Newtonsoft.Json.JsonConvert.SerializeObject(detail.GetDictionary());
            Crypter crypter = new Crypter(privateKey);

            string final = string.Format(Format, jsonHeader, jsonDetail);
            return crypter.Encrypt(final); 
        }
Ejemplo n.º 9
0
 private static void Read(object target)
 {
     TcpClient client = (TcpClient)target;
     NetworkStream stream = client.GetStream();
     Crypter c = new Crypto.Crypter(new DefaultConfigurationKey());
     StringBuilder decrypt = new StringBuilder();
     byte[] read;
     int readInt;
       
     while (true)
     {
         if (!client.Connected)
             break;
         
         read = new byte[client.ReceiveBufferSize];  
         try
         {
             readInt = stream.Read(read, 0, read.Length);
             decrypt.Append(c.Decrypt(Encoding.GetEncoding("ibm850").GetString(read).Replace("\0", "")));
              
             Console.WriteLine("From server : {0}", decrypt.Replace("\0", ""));
             Console.ForegroundColor = ConsoleColor.Gray;
             decrypt.Clear();
         }
         catch 
         { 
         }
     }
 }
 public AuthorizationRequestFilterAttribute()
 {
     c = new Crypter(new DefaultConfigurationKey()); 
 }
Ejemplo n.º 11
0
        private void TimerProcessingRequest_Elapsed(object sender, EventArgs e)
        {  
            ////ensures that one thread enter a processing request section, in the same time reading sms section may will be occured.
            ////If another thread tries to enter a locked code, it will wait, block, until the object is released.
            lock(portColletion)
            {
                PacketEventArgs workItem = workerPoolManager.GetJob();
                if (workItem != null)
                { 
                    BasicInformation currentConnection = portColletion.Get();
                    if (currentConnection != null)
                    {
                        List<string> command = new List<string>();
                        ParameterizedMap parsingCommand;
                        try
                        {
                            string dataEncode = Encoding.GetEncoding("ibm850").GetString(workItem.Data);

                            Request request = Newtonsoft.Json.JsonConvert.DeserializeObject<Request>(dataEncode, new Newtonsoft.Json.JsonSerializerSettings
                            {
                                TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects
                            });  
                            ICommandParser<ParameterizedMap> parser = new CommandParser();
                            parsingCommand = parser.Parse(request.QueueWorkItem.Command);

                            DynamicData parameters = new DynamicData();
                            foreach (string key in parsingCommand.ToDictionary().Keys)
                            {
                                if (!key.Equals("Command"))
                                {
                                    parameters.Add(key, parsingCommand.TryGet<object>(key));
                                }
                            }
                            parameters.Add("id", request.QueueWorkItem.SeqNbr == null ? "" : request.QueueWorkItem.SeqNbr);
                            command = parsingCommand.TryGet<string>("Command").Split('.').ToList();

                            ParameterizedMap param = new ParameterizedMap();
                            param.Add("base", currentConnection);
                            param.Add("packet", workItem);
                            param.Add("command", command);
                            param.Add("parameters", parameters); 

                            string responseOut;
                            string encrypted;

                            Worker.IPipeline pipe = ObjectPool.Instance.Resolve<Worker.IPipeline>();

                            pipe.BeforePush(param);
                            responseOut = pipe.Push(param); 
                            OnDataSent(responseOut.ToString());
                            pipe.AfterPush(param);

                            // send back to client if request workItem is send from tcp
                            if (workItem.Client != null)
                            {
                                Crypter crypter = new Crypter(new DefaultConfigurationKey());
                                encrypted = crypter.Encrypt(responseOut.ToString());
                                try
                                {
                                    this.Send(((IPEndPoint)workItem.Client.RemoteEndPoint).Port, ASCIIEncoding.ASCII.GetBytes(encrypted.ToString()));
                                }
                                catch (System.Net.Sockets.SocketException)
                                { 
                                    // client was closed, dont raise error. 
                                }
                                catch (ObjectDisposedException)
                                { 
                                    // client was closed, dont raise error.  
                                }  
                            }   
                        }
                        catch (Exception ex)
                        {
                            RaiseError(currentConnection, ex, "process request");
                        }  
                    }
                    else
                    {
                        workerPoolManager.CurrentWorker.Add(workItem);
                    }
                }
            } 
        }