From fd00485e913e18b9b41de457e73e2c1ddb8987a0 Mon Sep 17 00:00:00 2001 From: passerby-b <40885345+passerby-b@users.noreply.github.com> Date: Tue, 30 Nov 2021 15:30:19 +0800 Subject: [PATCH] Update MainFormHandler.cs --- v2rayN/v2rayN/Handler/MainFormHandler.cs | 147 ++++++++++------------- 1 file changed, 64 insertions(+), 83 deletions(-) diff --git a/v2rayN/v2rayN/Handler/MainFormHandler.cs b/v2rayN/v2rayN/Handler/MainFormHandler.cs index b101aedc..70f2ab94 100644 --- a/v2rayN/v2rayN/Handler/MainFormHandler.cs +++ b/v2rayN/v2rayN/Handler/MainFormHandler.cs @@ -1,5 +1,7 @@ using System; using System.Drawing; +using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms; using v2rayN.Base; using v2rayN.Mode; @@ -9,7 +11,7 @@ namespace v2rayN.Handler class MainFormHandler { private static MainFormHandler instance; - Action updateUI; + Action _updateUI; //private DownloadHandle downloadHandle2; //private Config _config; @@ -187,103 +189,82 @@ namespace v2rayN.Handler return counter; } - - public void UpdateSubscriptionProcess(Config config, Action update) + public void BackupGuiNConfig(Config config, bool auto = false) { - updateUI = update; - - updateUI(false, UIRes.I18N("MsgUpdateSubscriptionStart")); - - if (config.subItem == null || config.subItem.Count <= 0) + string fileName = string.Empty; + if (auto) { - updateUI(false, UIRes.I18N("MsgNoValidSubscription")); - return; + fileName = Utils.GetTempPath($"guiNConfig{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.json"); } - - for (int k = 1; k <= config.subItem.Count; k++) + else { - string id = config.subItem[k - 1].id.TrimEx(); - string url = config.subItem[k - 1].url.TrimEx(); - string hashCode = $"{k}->"; - if (config.subItem[k - 1].enabled == false) + SaveFileDialog fileDialog = new SaveFileDialog { - continue; - } - if (Utils.IsNullOrEmpty(id) || Utils.IsNullOrEmpty(url)) - { - updateUI(false, $"{hashCode}{UIRes.I18N("MsgNoValidSubscription")}"); - continue; - } - - DownloadHandle downloadHandle3 = new DownloadHandle(); - downloadHandle3.UpdateCompleted += (sender2, args) => - { - if (args.Success) - { - updateUI(false, $"{hashCode}{UIRes.I18N("MsgGetSubscriptionSuccessfully")}"); - string result = Utils.Base64Decode(args.Msg); - if (Utils.IsNullOrEmpty(result)) - { - updateUI(false, $"{hashCode}{UIRes.I18N("MsgSubscriptionDecodingFailed")}"); - return; - } - - ConfigHandler.RemoveServerViaSubid(ref config, id); - updateUI(false, $"{hashCode}{UIRes.I18N("MsgClearSubscription")}"); - // RefreshServers(); - int ret = MainFormHandler.Instance.AddBatchServers(config, result, id); - if (ret > 0) - { - // RefreshServers(); - } - else - { - updateUI(false, $"{hashCode}{UIRes.I18N("MsgFailedImportSubscription")}"); - } - updateUI(true, $"{hashCode}{UIRes.I18N("MsgUpdateSubscriptionEnd")}"); - } - else - { - updateUI(false, args.Msg); - } + Filter = "guiNConfig|*.json", + FilterIndex = 2, + RestoreDirectory = true }; - downloadHandle3.Error += (sender2, args) => + if (fileDialog.ShowDialog() != DialogResult.OK) { - updateUI(false, args.GetException().Message); - }; - - downloadHandle3.WebDownloadString(url); - updateUI(false, $"{hashCode}{UIRes.I18N("MsgStartGettingSubscriptions")}"); + return; + } + fileName = fileDialog.FileName; } - - } - - public void BackupGuiNConfig(Config config) - { - SaveFileDialog fileDialog = new SaveFileDialog - { - Filter = "guiNConfig|*.json", - FilterIndex = 2, - RestoreDirectory = true - }; - if (fileDialog.ShowDialog() != DialogResult.OK) - { - return; - } - string fileName = fileDialog.FileName; if (Utils.IsNullOrEmpty(fileName)) { return; } - if (Utils.ToJsonFile(config, fileName) == 0) + var ret = Utils.ToJsonFile(config, fileName); + if (!auto) { - UI.Show(UIRes.I18N("OperationSuccess")); - } - else - { - UI.ShowWarning(UIRes.I18N("OperationFailed")); + if (ret == 0) + { + + UI.Show(UIRes.I18N("OperationSuccess")); + } + else + { + UI.ShowWarning(UIRes.I18N("OperationFailed")); + } } } + public void UpdateTask(Config config, Action update) + { + _updateUI = update; + Task.Run(() => UpdateTaskRun(config)); + } + + private void UpdateTaskRun(Config config) + { + var updateHandle = new UpdateHandle(); + while (true) + { + Utils.SaveLog("UpdateTaskRun"); + Thread.Sleep(60000); + if (config.autoUpdateInterval <= 0) + { + continue; + } + + updateHandle.UpdateGeoFile("geosite", config, (bool success, string msg) => + { + _updateUI(false, msg); + if (success) + Utils.SaveLog("geosite" + msg); + }); + + Thread.Sleep(60000); + + updateHandle.UpdateGeoFile("geoip", config, (bool success, string msg) => + { + _updateUI(false, msg); + if (success) + Utils.SaveLog("geoip" + msg); + }); + + Thread.Sleep(1000 * 3600 * config.autoUpdateInterval); + } + } } }