Beispiel #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            Request.InputStream.Position = 0;

            try
            {
                JSONRequest req = JSON.GetRequest(Request.InputStream);

                using (IAMDatabase database = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                {
                    ProxyConfig config = new ProxyConfig(true);
                    config.GetDBConfig(database.Connection, ((EnterpriseData)Page.Session["enterprise_data"]).Id, req.host);

                    if (config.fqdn != null) //Encontrou o proxy
                    {
                        DirectoryInfo inDir = null;

                        using (ServerDBConfig c = new ServerDBConfig(IAMDatabase.GetWebConnection()))
                            inDir = new DirectoryInfo(c.GetItem("inboundFiles"));

                        if (!inDir.Exists)
                        {
                            inDir.Create();
                        }

                        req.enterpriseid = ((EnterpriseData)Page.Session["enterprise_data"]).Id.ToString();

                        String filename = config.proxy_name + "-" + DateTime.Now.ToString("yyyyMMddHHmmss-ffffff") + ".iamreq";


                        if (String.IsNullOrEmpty(req.filename))
                        {
                            req.filename = "Empty";
                        }

                        StringBuilder trackData = new StringBuilder();
                        trackData.AppendLine("Proxy: " + req.host);
                        trackData.AppendLine("Enterprise ID: " + req.enterpriseid);
                        trackData.AppendLine("Proxy filename: " + req.filename);
                        trackData.AppendLine("Saved filename: " + filename);

                        UserLogLevel level = UserLogLevel.Info;

                        trackData.AppendLine("");
                        trackData.AppendLine("Checking package...");

                        if (String.IsNullOrEmpty(req.data))
                        {
                            throw new Exception("Request data is empty");
                        }

                        Byte[] rData = Convert.FromBase64String(req.data);

                        if (!String.IsNullOrEmpty(req.sha1hash))
                        {
                            if (!CATools.SHA1CheckHash(rData, req.sha1hash))
                            {
                                throw new Exception("SHA1 Checksum is not equal");
                            }
                        }

                        String type = "";
                        try
                        {
                            JsonGeneric jData = new JsonGeneric();
                            try
                            {
                                String certPass = CATools.SHA1Checksum(Encoding.UTF8.GetBytes(config.fqdn));
                                if (String.IsNullOrEmpty(config.server_pkcs12_cert))
                                {
                                    throw new Exception("Server PKCS12 from proxy config is empty");
                                }

                                using (CryptApi cApi = CryptApi.ParsePackage(CATools.LoadCert(Convert.FromBase64String(config.server_pkcs12_cert), certPass), rData))
                                    jData.FromJsonBytes(cApi.clearData);
                            }
                            catch (Exception ex)
                            {
                                jData = null;
                                trackData.AppendLine("Error decrypting package data for enterprise " + req.enterpriseid + " and proxy " + req.host + ", " + ex.Message);

#if DEBUG
                                trackData.AppendLine(ex.StackTrace);
#endif
                            }

                            if (jData != null)
                            {
#if DEBUG
                                trackData.AppendLine("");
                                trackData.AppendLine("Request data:");
                                trackData.AppendLine(jData.ToJsonString());

                                trackData.AppendLine("");
#endif

                                type = jData.function;

                                trackData.AppendLine("Type: " + type);
                                trackData.AppendLine("Data array length: " + (jData.data == null ? "0" : jData.data.Count.ToString()));

                                if (type.ToLower() == "processimportv2")
                                {
                                    Int32 d = 1;
                                    foreach (String[] dr in jData.data)
                                    {
                                        try
                                        {
                                            Int32 resourcePluginCol = jData.GetKeyIndex("resource_plugin");
                                            Int32 pkgCol            = jData.GetKeyIndex("package");

                                            if (resourcePluginCol == -1)
                                            {
                                                trackData.AppendLine("[Package data " + d + "] Erro finding column 'resource_plugin'");
                                            }

                                            if (pkgCol == -1)
                                            {
                                                trackData.AppendLine("[Package data " + d + "] Erro finding column 'package'");
                                            }

                                            if ((resourcePluginCol != -1) && (pkgCol != -1))
                                            {
                                                PluginConnectorBaseImportPackageUser pkg = JSON.DeserializeFromBase64 <PluginConnectorBaseImportPackageUser>(dr[pkgCol]);
                                                trackData.AppendLine("[Package data " + d + "] Import id: " + pkg.importId);
                                                trackData.AppendLine("[Package data " + d + "] Package id: " + pkg.pkgId);

                                                Int64 trackId = 0;
                                                try
                                                {
                                                    String tpkg = JSON.Serialize2(pkg);

                                                    DbParameterCollection par = new DbParameterCollection();
                                                    par.Add("@entity_id", typeof(Int64)).Value = 0;
                                                    par.Add("@date", typeof(DateTime)).Value   = pkg.GetBuildDate();
                                                    par.Add("@flow", typeof(String)).Value     = "inbound";
                                                    par.Add("@package_id", typeof(String), pkg.pkgId.Length).Value = pkg.pkgId;
                                                    par.Add("@filename", typeof(String)).Value             = req.filename;
                                                    par.Add("@package", typeof(String), tpkg.Length).Value = tpkg;

                                                    trackId = database.ExecuteScalar <Int64>("sp_new_package_track", System.Data.CommandType.StoredProcedure, par, null);

                                                    trackData.AppendLine("[Package data " + d + "] Package track id: " + trackId);

                                                    tpkg = null;

                                                    if (trackId > 0)
                                                    {
                                                        database.AddPackageTrack(trackId, "ProxyAPI", "Package received from proxy and saved at " + filename);
                                                    }
                                                }
                                                catch (Exception ex3) {
                                                    trackData.AppendLine("[Package data " + d + "] Erro generating package track: " + ex3.Message);
                                                }


                                                pkg.Dispose();
                                                pkg = null;
                                            }
                                        }
                                        catch (Exception ex2)
                                        {
                                            trackData.AppendLine("[Package data " + d + "] Erro parsing package data " + ex2.Message);
                                        }
                                        d++;
                                    }
                                }
                            }
                        }
                        catch (Exception ex1) {
                            trackData.AppendLine("Erro parsing package " + ex1.Message);
                            level = UserLogLevel.Error;
                        }

                        database.AddUserLog(LogKey.API_Log, DateTime.Now, "ProxyAPI", level, 0, ((EnterpriseData)Page.Session["enterprise_data"]).Id, 0, 0, 0, 0, 0, "File received from proxy " + req.host + (String.IsNullOrEmpty(type) ? "" : " (" + type + ")"), trackData.ToString());


                        File.WriteAllBytes(Path.Combine(inDir.FullName, filename), Encoding.UTF8.GetBytes(JSON.Serialize <JSONRequest>(req)));

                        ReturnHolder.Controls.Add(new LiteralControl(JSON.GetResponse(true, "", "Request received and proxy finded (" + (req.data != null ? req.data.Length.ToString() : "0") + ")")));
                    }
                }
            }
            catch (Exception ex) {
                Tools.Tool.notifyException(ex);
                throw ex;
            }
        }
Beispiel #2
0
        private void ImportRegistersV2(ProxyConfig config, JsonGeneric jData, FileInfo f, JSONRequest req, IAMDatabase db)
        {
            Int32 resourcePluginCol = jData.GetKeyIndex("resource_plugin");
            Int32 pkgCol            = jData.GetKeyIndex("package");


            if (resourcePluginCol == -1)
            {
                TextLog.Log("Inbound", "\t[ImportRegistersV2] Erro on find column 'resource_plugin' in " + f.Name + " enterprise " + req.enterpriseid + " and proxy " + req.host);
                return;
            }


            if (pkgCol == -1)
            {
                TextLog.Log("Inbound", "\t[ImportRegistersV2] Erro on find column 'package' in " + f.Name + " enterprise " + req.enterpriseid + " and proxy " + req.host);
                return;
            }

            //Realiza a importação no modelo BulkInsert por melhor desempenho do banco
            DataTable dtBulk = new DataTable();

            dtBulk.Columns.Add(new DataColumn("date", typeof(DateTime)));
            dtBulk.Columns.Add(new DataColumn("file_name", typeof(String)));
            dtBulk.Columns.Add(new DataColumn("resource_plugin", typeof(Int64)));
            dtBulk.Columns.Add(new DataColumn("import_id", typeof(String)));
            dtBulk.Columns.Add(new DataColumn("package_id", typeof(String)));
            dtBulk.Columns.Add(new DataColumn("package", typeof(String)));
            dtBulk.Columns.Add(new DataColumn("status", typeof(String)));

            foreach (String[] dr in jData.data)
            {
                PluginConnectorBaseImportPackageUser pkg = JSON.DeserializeFromBase64 <PluginConnectorBaseImportPackageUser>(dr[pkgCol]);
                dtBulk.Rows.Add(new Object[] { DateTime.Now, f.Name, dr[resourcePluginCol], pkg.importId, pkg.pkgId, JSON.Serialize2(pkg), 'F' });

                try
                {
                    DbParameterCollection par = new DbParameterCollection();

                    par.Add("@date", typeof(DateTime)).Value = pkg.GetBuildDate();
                    par.Add("@package_id", typeof(String), pkg.pkgId.Length).Value = pkg.pkgId;

                    Int64 trackId = db.ExecuteScalar <Int64>("select id from st_package_track where flow = 'inbound' and date = @date and package_id = @package_id", System.Data.CommandType.Text, par, null);

                    db.AddPackageTrack(trackId, "inbound", "Package imported to process queue");
                }
                catch { }
            }

            db.BulkCopy(dtBulk, "collector_imports");

            //Apaga todos os registros da tabela temporaria

            /*
             * Procedimento desabiliato em 2018-08-29 por suspeita de problema
             * db.ExecuteNonQuery("delete from collector_imports_temp", System.Data.CommandType.Text, null, null);
             *
             * db.BulkCopy(dtBulk, "collector_imports_temp");
             *
             * //Proteção contra reimportação de pacotes (loop)
             * db.ExecuteNonQuery("delete from collector_imports_temp where exists (select 1 from collector_imports_old o where o.date >= dateadd(day,-1,getdate()) and o.file_name = file_name and o.resource_plugin_id = resource_plugin_id and o.import_id = import_id and o.package_id = package_id)", System.Data.CommandType.Text, null, null);
             * db.ExecuteNonQuery("delete from collector_imports_temp where exists (select 1 from collector_imports o where o.date >= dateadd(day,-1,getdate()) and o.file_name = file_name and o.resource_plugin_id = resource_plugin_id and o.import_id = import_id and o.package_id = package_id)", System.Data.CommandType.Text, null, null);
             *
             * db.ExecuteNonQuery("insert into collector_imports select * from collector_imports_temp", System.Data.CommandType.Text, null, null);
             * db.ExecuteNonQuery("delete from collector_imports_temp", System.Data.CommandType.Text, null, null);
             * */

            //Atualiza os registros importados deste arquivo para liberar o processamento
            //Isso avisa o sistema que estes registros estão livres para processamento
            //*** Desabilitado essa funç~~ao em 2018-03-08, e colocado o registro para ser importado diretamente com o Status 'F'
            //db.ExecuteNonQuery("update collector_imports set status = 'F' where [file_name] = '" + f.Name + "'", CommandType.Text, null);

            //Realiza o rebuild do indice desta tabela para agilizar no engine
            //Este processo será executado somente uma vez pelo objeto pai
            //db.ExecuteNonQuery("sp_reindex_imports", CommandType.StoredProcedure, null);

#if DEBUG
            TextLog.Log("Inbound", "\t[ImportRegistersV2] Imported " + dtBulk.Rows.Count + " registers for enterprise " + req.enterpriseid + " and proxy " + req.host);
#endif

            dtBulk.Dispose();
            dtBulk = null;

            jData = null;
        }