From 4ad6e9e7f47dbceb46acd9975d946040e24fc3c1 Mon Sep 17 00:00:00 2001 From: Samuel Tulach Date: Thu, 10 Oct 2024 21:25:35 +0200 Subject: [PATCH] Fixed permalink missing error, added functionality to allow whole folder upload --- uploader/uploader/App.config | 16 ++++- uploader/uploader/LocalizationBase.cs | 1 + uploader/uploader/UploadForm.cs | 94 +++++++++++++++++---------- uploader/uploader/packages.config | 8 +-- uploader/uploader/uploader.csproj | 19 +++--- 5 files changed, 90 insertions(+), 48 deletions(-) diff --git a/uploader/uploader/App.config b/uploader/uploader/App.config index 4bfa005..95d9d38 100644 --- a/uploader/uploader/App.config +++ b/uploader/uploader/App.config @@ -1,6 +1,18 @@ - + - + + + + + + + + + + + + + diff --git a/uploader/uploader/LocalizationBase.cs b/uploader/uploader/LocalizationBase.cs index 53d9f57..57240b9 100644 --- a/uploader/uploader/LocalizationBase.cs +++ b/uploader/uploader/LocalizationBase.cs @@ -27,6 +27,7 @@ namespace uploader public string UploadForm_NoApiKey = "You have not entered an API key. Please go to settings and add one."; public string UploadForm_InvalidLength = "Invalid API key length. The key must contain 64 characters."; public string UploadForm_InvalidKey = "Invalid API key"; + public string UploadForm_Error = "Error"; public string Message_Idle = "Idle."; public string Message_Init = "Initializing..."; diff --git a/uploader/uploader/UploadForm.cs b/uploader/uploader/UploadForm.cs index 32568a8..245180f 100644 --- a/uploader/uploader/UploadForm.cs +++ b/uploader/uploader/UploadForm.cs @@ -19,19 +19,22 @@ namespace uploader public partial class UploadForm : DarkForm { private readonly bool _reopen; - private readonly string _fileName; + private readonly string _path; private readonly MainForm _mainForm; private readonly Settings _settings; private Thread _uploadThread; private RestClient _client; + private bool _isFolder; + private List _filesToUpload; - public UploadForm(MainForm mainForm, Settings settings, bool reopen, string fileName) + public UploadForm(MainForm mainForm, Settings settings, bool reopen, string path) { - _fileName = fileName; + _path = path; _mainForm = mainForm; _settings = settings; _reopen = reopen; - + _isFolder = Directory.Exists(_path); + InitializeComponent(); } @@ -73,6 +76,12 @@ namespace uploader this.Close(); } + private void DisplayError(string error) + { + var messageBox = new DarkMessageBox(error, LocalizationHelper.Base.UploadForm_Error, DarkMessageBoxIcon.Error, DarkDialogButton.Ok); + messageBox.ShowDialog(); + } + private void Upload() { if (string.IsNullOrEmpty(_settings.ApiKey)) @@ -86,19 +95,40 @@ namespace uploader MessageBox.Show(LocalizationHelper.Base.UploadForm_InvalidLength, LocalizationHelper.Base.UploadForm_InvalidKey, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - + ChangeStatus(LocalizationHelper.Base.Message_Init); _client = new RestClient("https://www.virustotal.com"); - if (!File.Exists(_fileName)) + if (_isFolder) { - throw new FileNotFoundException(); + _filesToUpload = Directory.GetFiles(_path, "*.*", SearchOption.AllDirectories).ToList(); + } + else + { + _filesToUpload = new List { _path }; } - ChangeStatus(LocalizationHelper.Base.Message_Check); + foreach (var file in _filesToUpload) + { + UploadFile(file); + } + + Finish(true); + } + + private void UploadFile(string fullPath) + { + if (!File.Exists(fullPath)) + { + DisplayError($"File {fullPath} does not exist."); + return; + } + + var fileName = Path.GetFileName(fullPath); + ChangeStatus($"Checking {fileName}..."); var reportRequest = new RestRequest("vtapi/v2/file/report", Method.Post); reportRequest.AddParameter("apikey", _settings.ApiKey); - reportRequest.AddParameter("resource", Utils.GetMD5(_fileName)); + reportRequest.AddParameter("resource", Utils.GetMD5(fullPath)); var reportResponse = _client.Execute(reportRequest); var reportContent = reportResponse.Content; @@ -108,46 +138,33 @@ namespace uploader { var reportLink = reportJson.permalink.ToString(); Process.Start(reportLink); - - if (_settings.DirectUpload) CloseWindow(); } catch (RuntimeBinderException) { // Json does not contain permalink which means it's a new file (or the request failed) - ChangeStatus(LocalizationHelper.Base.Message_Upload); + ChangeStatus($"Uploading {fileName}..."); var scanRequest = new RestRequest("vtapi/v2/file/scan", Method.Post); scanRequest.AddParameter("apikey", _settings.ApiKey); - scanRequest.AddFile("file", _fileName); + scanRequest.AddFile("file", fullPath); var scanResponse = _client.Execute(scanRequest); var scanContent = scanResponse.Content; - // TODO: check for HTML (file too large) dynamic scanJson = JsonConvert.DeserializeObject(scanContent); try { - string scanLink = scanJson.permalink.ToString(); + string sha256 = scanJson.sha256.ToString(); + string scanId = scanJson.scan_id.ToString(); + + var scanLink = $"https://www.virustotal.com/gui/file/{sha256}/detection/{scanId}"; - // An example link can look like this: - // https://www.virustotal.com/gui/file//detection/ - // If we don't remove the the scanid, then it will fail on new files since the scan did not finish - // Removing it like this will show the analysis progress for new files - scanLink = scanLink.Remove(scanLink.IndexOf("/detection")); - Process.Start(scanLink); - - if (_settings.DirectUpload) CloseWindow(); } - catch (Exception) + catch (Exception ex) { - // Response does not contain permalink so it failed - ChangeStatus(LocalizationHelper.Base.Message_NoLink); - Finish(false); - return; + DisplayError($"Failed to get link for {fileName}. Error: {ex.Message}"); } } - - Finish(true); } private void StartUploadThread() @@ -166,9 +183,18 @@ namespace uploader private void UploadForm_Load(object sender, EventArgs e) { - mdTextbox.Text = Utils.GetMD5(_fileName); - shaTextbox.Text = Utils.GetSHA1(_fileName); - sha2Textbox.Text = Utils.GetSHA256(_fileName); + if (_isFolder) + { + mdTextbox.Text = "N/A (Folder)"; + shaTextbox.Text = "N/A (Folder)"; + sha2Textbox.Text = "N/A (Folder)"; + } + else + { + mdTextbox.Text = Utils.GetMD5(_path); + shaTextbox.Text = Utils.GetSHA1(_path); + sha2Textbox.Text = Utils.GetSHA256(_path); + } settingsGroup.Text = LocalizationHelper.Base.UploadForm_Info; uploadButton.Text = LocalizationHelper.Base.UploadForm_Upload; @@ -197,4 +223,4 @@ namespace uploader } } } -} +} \ No newline at end of file diff --git a/uploader/uploader/packages.config b/uploader/uploader/packages.config index 0fe8a78..ce7c87f 100644 --- a/uploader/uploader/packages.config +++ b/uploader/uploader/packages.config @@ -1,14 +1,14 @@  - + - + - - + + \ No newline at end of file diff --git a/uploader/uploader/uploader.csproj b/uploader/uploader/uploader.csproj index 193435c..9a33b86 100644 --- a/uploader/uploader/uploader.csproj +++ b/uploader/uploader/uploader.csproj @@ -63,20 +63,21 @@ False ..\..\darkui\DarkUI.dll - - ..\packages\Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll + + ..\packages\Microsoft.Bcl.AsyncInterfaces.8.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll ..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll - - ..\packages\RestSharp.110.2.0\lib\net471\RestSharp.dll + + ..\packages\RestSharp.112.1.0\lib\net48\RestSharp.dll ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + ..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll @@ -86,11 +87,11 @@ ..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll - - ..\packages\System.Text.Encodings.Web.7.0.0\lib\net462\System.Text.Encodings.Web.dll + + ..\packages\System.Text.Encodings.Web.8.0.0\lib\net462\System.Text.Encodings.Web.dll - - ..\packages\System.Text.Json.7.0.2\lib\net462\System.Text.Json.dll + + ..\packages\System.Text.Json.8.0.5\lib\net462\System.Text.Json.dll ..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll @@ -101,7 +102,9 @@ + +