mirror of
https://github.com/2dust/v2rayN.git
synced 2025-08-25 12:26:55 +00:00
Update MainFormHandler.cs
This commit is contained in:
parent
53bc6af2f0
commit
fd00485e91
1 changed files with 64 additions and 83 deletions
|
@ -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<bool, string> updateUI;
|
||||
Action<bool, string> _updateUI;
|
||||
|
||||
//private DownloadHandle downloadHandle2;
|
||||
//private Config _config;
|
||||
|
@ -187,78 +189,14 @@ namespace v2rayN.Handler
|
|||
return counter;
|
||||
}
|
||||
|
||||
|
||||
public void UpdateSubscriptionProcess(Config config, Action<bool, string> 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;
|
||||
}
|
||||
|
||||
for (int k = 1; k <= config.subItem.Count; k++)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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();
|
||||
fileName = Utils.GetTempPath($"guiNConfig{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.json");
|
||||
}
|
||||
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
|
||||
{
|
||||
|
@ -270,13 +208,18 @@ namespace v2rayN.Handler
|
|||
{
|
||||
return;
|
||||
}
|
||||
string fileName = fileDialog.FileName;
|
||||
fileName = fileDialog.FileName;
|
||||
}
|
||||
if (Utils.IsNullOrEmpty(fileName))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (Utils.ToJsonFile(config, fileName) == 0)
|
||||
var ret = Utils.ToJsonFile(config, fileName);
|
||||
if (!auto)
|
||||
{
|
||||
if (ret == 0)
|
||||
{
|
||||
|
||||
UI.Show(UIRes.I18N("OperationSuccess"));
|
||||
}
|
||||
else
|
||||
|
@ -284,6 +227,44 @@ namespace v2rayN.Handler
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue