1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00
Teknik/ServerMaint/Program.cs
Uncled1023 d5c85add89 Added Server Maintainence program.
Fixed client side encrypted uploads that save the key from not being scanned.
2016-05-09 13:01:41 -07:00

121 lines
5.5 KiB
C#

using nClam;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Teknik.Areas.Upload.Models;
using Teknik.Configuration;
using Teknik.Helpers;
using Teknik.Models;
namespace ServerMaint
{
public class Program
{
public static event Action<string> OutputEvent;
public static void Main(string[] args)
{
string currentPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string parentPath = Directory.GetParent(currentPath).FullName;
string logFile = Path.Combine(currentPath, "virusLogs.txt");
string errorFile = Path.Combine(currentPath, "errorLogs.txt");
string configPath = Path.Combine(parentPath, "App_Data");
// Let's clean some stuff!!
try
{
Config config = Config.Load(configPath);
TeknikEntities db = new TeknikEntities();
// Scan all the uploads for viruses, and remove the bad ones
if (config.UploadConfig.VirusScanEnable)
{
List<Upload> uploads = db.Uploads.ToList();
int totalCount = uploads.Count();
int totalScans = 0;
int totalClean = 0;
int totalViruses = 0;
foreach (Upload upload in uploads)
{
totalScans++;
string subDir = upload.FileName[0].ToString();
string filePath = Path.Combine(config.UploadConfig.UploadDirectory, subDir, upload.FileName);
if (File.Exists(filePath))
{
// Read in the file
byte[] data = File.ReadAllBytes(filePath);
// If the IV is set, and Key is set, then decrypt it
if (!string.IsNullOrEmpty(upload.Key) && !string.IsNullOrEmpty(upload.IV))
{
// Decrypt the data
data = AES.Decrypt(data, upload.Key, upload.IV);
}
// We have the data, let's scan it
ClamClient clam = new ClamClient(config.UploadConfig.ClamServer, config.UploadConfig.ClamPort);
clam.MaxStreamSize = config.UploadConfig.MaxUploadSize;
ClamScanResult scanResult = clam.SendAndScanFile(data);
switch (scanResult.Result)
{
case ClamScanResults.Clean:
totalClean++;
string cleanMsg = string.Format("[{0}] Clean Scan: {1}/{2} Scanned | {3} - {4}", DateTime.Now, totalScans, totalCount, upload.Url, upload.FileName);
Output(cleanMsg);
break;
case ClamScanResults.VirusDetected:
totalViruses++;
string msg = string.Format("[{0}] Virus Detected: {1} - {2} - {3}", DateTime.Now, upload.Url, upload.FileName, scanResult.InfectedFiles.First().VirusName);
File.AppendAllLines(logFile, new List<string> { msg });
Output(msg);
//// Delete from the DB
//db.Uploads.Remove(upload);
//db.SaveChanges();
//// Delete the File
//if (File.Exists(filePath))
//{
// File.Delete(filePath);
//}
break;
case ClamScanResults.Error:
string errorMsg = string.Format("[{0}] Scan Error: {1}", DateTime.Now, scanResult.RawResult);
File.AppendAllLines(errorFile, new List<string> { errorMsg });
Output(errorMsg);
break;
case ClamScanResults.Unknown:
string unkMsg = string.Format("[{0}] Unknown Scan Result: {1}", DateTime.Now, scanResult.RawResult);
File.AppendAllLines(errorFile, new List<string> { unkMsg });
Output(unkMsg);
break;
}
}
}
Output(string.Format("Scanning Complete. {0} Scanned | {1} Viruses Found | {2} Total Files", totalScans, totalViruses, totalCount));
}
}
catch (Exception ex)
{
string msg = string.Format("[{0}] Exception: {1}", DateTime.Now, ex.Message);
File.AppendAllLines(errorFile, new List<string> { msg });
Output(msg);
}
}
public static void Output(string message)
{
Console.WriteLine(message);
if (OutputEvent != null)
{
OutputEvent(message);
}
}
}
}