public static dynamic GetCatalogFileByHash(dynamic serverMsg) { dynamic response = null; string sha256 = serverMsg.Arguments.Sha256; Log.Info("Command GetCatalogFileByHash: Hash {0}", sha256); var sessionFactory = Database.getSessionFactory(); using (var session = sessionFactory.OpenSession()) { var catalogs = session.QueryOver <CatalogFile>() .Where(e => e.Sha256 == Helpers.HexStringToByteArray(sha256)) .List <CatalogFile>(); if (catalogs.Count() != 0) { CatalogFile catalog = catalogs[0]; Log.Info("Found file {0}", catalog.FilePath); response = UploadFile(catalog.Sha256, catalog.FilePath, "catalog"); } else { // TODO Need to throw error Log.Error("GetCatalogFileByHash requested unknown hash {0}", sha256); } } return(response); }
public static void LogCatalogFile(string catalogFilePath) { Log.Info("Catalog file used: {0}", catalogFilePath); var sessionFactory = Database.getSessionFactory(); using (var session = sessionFactory.OpenSession()) { // Check if this catalog already exists in our DB var catalogs = session.QueryOver <CatalogFile>() .Where(e => e.FilePath == catalogFilePath) .List <CatalogFile>(); if (catalogs.Count() == 0) { // Does not exist, so add it using (var transaction = session.BeginTransaction()) { Log.Info("Catalog file never seen before, so adding it to the DB"); // TODO I should not be keeping track of the catalogs by filename instead of using the hash byte[] md5Hash, sha1Hash, sha256Hash; Helpers.ComputeHashes(catalogFilePath, out md5Hash, out sha1Hash, out sha256Hash); var catalogFile = new CatalogFile { FilePath = catalogFilePath, Sha256 = sha256Hash, Size = (int)(new System.IO.FileInfo(catalogFilePath).Length), FirstAccessTime = DateTime.UtcNow }; Log.Info("Recorded catalog file of size {0}", catalogFile.Size); session.Save(catalogFile); transaction.Commit(); } } else { // Already exists in the DB, so pass } } }
public static bool PostCatalogFile(CatalogFile catalogFile) { CatalogFilePost catalogFilePost = new CatalogFilePost(); catalogFilePost.TimeOfEvent = Helpers.ConvertToUnixTime(catalogFile.FirstAccessTime); catalogFilePost.Path = catalogFile.FilePath; catalogFilePost.Size = catalogFile.Size; catalogFilePost.Sha256 = Helpers.ByteArrayToHexString(catalogFile.Sha256); string postMessage = Helpers.SerializeToJson(catalogFilePost, typeof(CatalogFilePost)); string response = Beacon.PostToServer(postMessage, "/api/v1/CatalogFileEvent"); if (response == "") { // Remote server could not be reached or encountered an error return(false); } CatalogFileResponse processEventResponse = (CatalogFileResponse)Helpers.DeserializeFromJson(response, typeof(CatalogFileResponse)); return(true); }