Update MainFormHandler.cs

This commit is contained in:
passerby-b 2021-11-30 14:07:52 +08:00 committed by GitHub
parent c29ccf76d5
commit 53bc6af2f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<bool, string> _updateUI;
Action<bool, string> 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<bool, string> 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<bool, string> 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);
}
}
}
}