diff --git a/v2rayN/v2rayN/Handler/MainFormHandler.cs b/v2rayN/v2rayN/Handler/MainFormHandler.cs index c44f8a1b..b101aedc 100644 --- a/v2rayN/v2rayN/Handler/MainFormHandler.cs +++ b/v2rayN/v2rayN/Handler/MainFormHandler.cs @@ -1,7 +1,5 @@ -using System; +using System; using System.Drawing; -using System.Threading; -using System.Threading.Tasks; using System.Windows.Forms; using v2rayN.Base; using v2rayN.Mode; @@ -11,7 +9,7 @@ namespace v2rayN.Handler class MainFormHandler { private static MainFormHandler instance; - Action _updateUI; + Action updateUI; //private DownloadHandle downloadHandle2; //private Config _config; @@ -42,6 +40,27 @@ namespace v2rayN.Handler color = (new Color[] { Color.Red, Color.Purple, Color.DarkGreen, Color.Orange, Color.DarkSlateBlue, Color.RoyalBlue })[index - 1]; //color = ColorTranslator.FromHtml(new string[] { "#CC0066", "#CC6600", "#99CC99", "#666699" }[index - 1]); } + //代理 + if (config.sysProxyType.ToString() == "ForcedChange") + { + if (config.routings[config.routingIndex].remarks.IndexOf("全局") > -1) + { + color = ColorTranslator.FromHtml("#EA0000");//红色 + } + else if (config.routings[config.routingIndex].remarks.IndexOf("绕过") > -1) + { + color = ColorTranslator.FromHtml("#46A3FF");//蓝色 + } + else + { + color = ColorTranslator.FromHtml("#EA7500");//棕色 + } + } + else + { + //清除代理或不改变系统代理 + color = ColorTranslator.FromHtml("#BEBEBE");//灰色 + } int width = 128; int height = 128; @@ -168,82 +187,103 @@ namespace v2rayN.Handler return counter; } - public void BackupGuiNConfig(Config config, bool auto = false) + + public void UpdateSubscriptionProcess(Config config, Action update) { - string fileName = string.Empty; - if (auto) + updateUI = update; + + updateUI(false, UIRes.I18N("MsgUpdateSubscriptionStart")); + + if (config.subItem == null || config.subItem.Count <= 0) { - fileName = Utils.GetTempPath($"guiNConfig{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.json"); + updateUI(false, UIRes.I18N("MsgNoValidSubscription")); + return; } - else + + for (int k = 1; k <= config.subItem.Count; k++) { - SaveFileDialog fileDialog = new SaveFileDialog + 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) { - Filter = "guiNConfig|*.json", - FilterIndex = 2, - RestoreDirectory = true - }; - if (fileDialog.ShowDialog() != DialogResult.OK) - { - return; + continue; } - fileName = fileDialog.FileName; + 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); + } + }; + downloadHandle3.Error += (sender2, args) => + { + updateUI(false, args.GetException().Message); + }; + + downloadHandle3.WebDownloadString(url); + updateUI(false, $"{hashCode}{UIRes.I18N("MsgStartGettingSubscriptions")}"); } + + } + + 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; } - var ret = Utils.ToJsonFile(config, fileName); - if (!auto) + if (Utils.ToJsonFile(config, fileName) == 0) { - if (ret == 0) - { - - UI.Show(UIRes.I18N("OperationSuccess")); - } - else - { - UI.ShowWarning(UIRes.I18N("OperationFailed")); - } + 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); - } - } } -} \ No newline at end of file +}