update geo files for sing-box

pull/3786/head
2dust 2023-04-28 15:54:51 +08:00
parent 59d397a3a0
commit ec8f6478df
5 changed files with 107 additions and 67 deletions

View File

@ -20,6 +20,7 @@
public const string tuicCoreUrl = "https://github.com/EAimTY/tuic/releases"; public const string tuicCoreUrl = "https://github.com/EAimTY/tuic/releases";
public const string singboxCoreUrl = "https://github.com/SagerNet/sing-box/releases"; public const string singboxCoreUrl = "https://github.com/SagerNet/sing-box/releases";
public const string geoUrl = "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/{0}.dat"; public const string geoUrl = "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/{0}.dat";
public const string singboxGeoUrl = "https://github.com/soffchen/sing-{0}/releases/latest/download/{0}.db";
public const string SpeedPingTestUrl = @"https://www.google.com/generate_204"; public const string SpeedPingTestUrl = @"https://www.google.com/generate_204";
public const string CustomRoutingListUrl = @"https://raw.githubusercontent.com/2dust/v2rayCustomRoutingList/master/"; public const string CustomRoutingListUrl = @"https://raw.githubusercontent.com/2dust/v2rayCustomRoutingList/master/";

View File

@ -62,12 +62,12 @@ namespace v2rayN.Handler
return 0; return 0;
} }
public void DownloadFileAsync(string url, bool blProxy, int downloadTimeout) public async Task DownloadFileAsync(string url, bool blProxy, int downloadTimeout)
{ {
try try
{ {
Utils.SetSecurityProtocol(LazyConfig.Instance.GetConfig().guiItem.enableSecurityProtocolTls13); Utils.SetSecurityProtocol(LazyConfig.Instance.GetConfig().guiItem.enableSecurityProtocolTls13);
UpdateCompleted?.Invoke(this, new ResultEventArgs(false, ResUI.Downloading)); UpdateCompleted?.Invoke(this, new ResultEventArgs(false, $"{ResUI.Downloading} {url}"));
var progress = new Progress<double>(); var progress = new Progress<double>();
progress.ProgressChanged += (sender, value) => progress.ProgressChanged += (sender, value) =>
@ -76,7 +76,7 @@ namespace v2rayN.Handler
}; };
var webProxy = GetWebProxy(blProxy); var webProxy = GetWebProxy(blProxy);
_ = DownloaderHelper.Instance.DownloadFileAsync(webProxy, await DownloaderHelper.Instance.DownloadFileAsync(webProxy,
url, url,
Utils.GetTempPath(Utils.GetDownloadFileName(url)), Utils.GetTempPath(Utils.GetDownloadFileName(url)),
progress, progress,

View File

@ -206,19 +206,10 @@ namespace v2rayN.Handler
{ {
if ((dtNow - autoUpdateGeoTime).Hours % config.guiItem.autoUpdateInterval == 0) if ((dtNow - autoUpdateGeoTime).Hours % config.guiItem.autoUpdateInterval == 0)
{ {
updateHandle.UpdateGeoFile("geosite", config, (bool success, string msg) => updateHandle.UpdateGeoFileAll(config, (bool success, string msg) =>
{ {
update(false, msg); update(false, msg);
if (success) });
Utils.SaveLog("geosite" + msg);
});
updateHandle.UpdateGeoFile("geoip", config, (bool success, string msg) =>
{
update(false, msg);
if (success)
Utils.SaveLog("geoip" + msg);
});
autoUpdateGeoTime = dtNow; autoUpdateGeoTime = dtNow;
} }
} }

View File

@ -84,7 +84,7 @@ namespace v2rayN.Handler
_updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, "v2rayN")); _updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, "v2rayN"));
url = args.Msg; url = args.Msg;
askToDownload(downloadHandle, url, true); _ = askToDownload(downloadHandle, url, true);
} }
else else
{ {
@ -135,7 +135,7 @@ namespace v2rayN.Handler
{ {
_updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, "Core")); _updateFunc(false, string.Format(ResUI.MsgParsingSuccessfully, "Core"));
url = args.Msg; url = args.Msg;
askToDownload(downloadHandle, url, true); _ = askToDownload(downloadHandle, url, true);
} }
else else
{ {
@ -279,51 +279,16 @@ namespace v2rayN.Handler
}); });
} }
public void UpdateGeoFile(string geoName, Config config, Action<bool, string> update) public void UpdateGeoFileAll(Config config, Action<bool, string> update)
{ {
_config = config; Task.Run(async () =>
_updateFunc = update;
var url = string.Format(Global.geoUrl, geoName);
DownloadHandle downloadHandle = new();
downloadHandle.UpdateCompleted += (sender2, args) =>
{ {
if (args.Success) await UpdateGeoFile("geosite", _config, update);
{ await UpdateGeoFile("geoip", _config, update);
_updateFunc(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, geoName));
try await UpdateGeoFile4Singbox("geosite", _config, update);
{ await UpdateGeoFile4Singbox("geoip", _config, update);
string fileName = Utils.GetTempPath(Utils.GetDownloadFileName(url)); });
if (File.Exists(fileName))
{
//Global.coreTypes.ForEach(it =>
//{
// string targetPath = Utils.GetBinPath($"{geoName}.dat", (ECoreType)Enum.Parse(typeof(ECoreType), it));
// File.Copy(fileName, targetPath, true);
//});
string targetPath = Utils.GetBinPath($"{geoName}.dat");
File.Copy(fileName, targetPath, true);
File.Delete(fileName);
//_updateFunc(true, "");
}
}
catch (Exception ex)
{
_updateFunc(false, ex.Message);
}
}
else
{
_updateFunc(false, args.Msg);
}
};
downloadHandle.Error += (sender2, args) =>
{
_updateFunc(false, args.GetException().Message);
};
askToDownload(downloadHandle, url, false);
} }
public void RunAvailabilityCheck(Action<bool, string> update) public void RunAvailabilityCheck(Action<bool, string> update)
@ -570,7 +535,7 @@ namespace v2rayN.Handler
} }
} }
private void askToDownload(DownloadHandle downloadHandle, string url, bool blAsk) private async Task askToDownload(DownloadHandle downloadHandle, string url, bool blAsk)
{ {
bool blDownload = false; bool blDownload = false;
if (blAsk) if (blAsk)
@ -586,10 +551,98 @@ namespace v2rayN.Handler
} }
if (blDownload) if (blDownload)
{ {
downloadHandle.DownloadFileAsync(url, true, 600); await downloadHandle.DownloadFileAsync(url, true, 600);
} }
} }
private async Task UpdateGeoFile(string geoName, Config config, Action<bool, string> update)
{
_config = config;
_updateFunc = update;
var url = string.Format(Global.geoUrl, geoName);
DownloadHandle downloadHandle = new();
downloadHandle.UpdateCompleted += (sender2, args) =>
{
if (args.Success)
{
_updateFunc(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, geoName));
try
{
string fileName = Utils.GetTempPath(Utils.GetDownloadFileName(url));
if (File.Exists(fileName))
{
//Global.coreTypes.ForEach(it =>
//{
// string targetPath = Utils.GetBinPath($"{geoName}.dat", (ECoreType)Enum.Parse(typeof(ECoreType), it));
// File.Copy(fileName, targetPath, true);
//});
string targetPath = Utils.GetBinPath($"{geoName}.dat");
File.Copy(fileName, targetPath, true);
File.Delete(fileName);
//_updateFunc(true, "");
}
}
catch (Exception ex)
{
_updateFunc(false, ex.Message);
}
}
else
{
_updateFunc(false, args.Msg);
}
};
downloadHandle.Error += (sender2, args) =>
{
_updateFunc(false, args.GetException().Message);
};
await askToDownload(downloadHandle, url, false);
}
private async Task UpdateGeoFile4Singbox(string geoName, Config config, Action<bool, string> update)
{
_config = config;
_updateFunc = update;
var url = string.Format(Global.singboxGeoUrl, geoName);
DownloadHandle downloadHandle = new();
downloadHandle.UpdateCompleted += (sender2, args) =>
{
if (args.Success)
{
_updateFunc(false, string.Format(ResUI.MsgDownloadGeoFileSuccessfully, geoName));
try
{
string fileName = Utils.GetTempPath(Utils.GetDownloadFileName(url));
if (File.Exists(fileName))
{
string targetPath = Utils.GetConfigPath($"{geoName}.db");
File.Copy(fileName, targetPath, true);
File.Delete(fileName);
}
}
catch (Exception ex)
{
_updateFunc(false, ex.Message);
}
}
else
{
_updateFunc(false, args.Msg);
}
};
downloadHandle.Error += (sender2, args) =>
{
_updateFunc(false, args.GetException().Message);
};
await askToDownload(downloadHandle, url, false);
}
#endregion private #endregion private
} }
} }

View File

@ -1460,12 +1460,7 @@ namespace v2rayN.ViewModels
private void CheckUpdateGeo() private void CheckUpdateGeo()
{ {
Task.Run(() => (new UpdateHandle()).UpdateGeoFileAll(_config, UpdateTaskHandler);
{
var updateHandle = new UpdateHandle();
updateHandle.UpdateGeoFile("geosite", _config, UpdateTaskHandler);
updateHandle.UpdateGeoFile("geoip", _config, UpdateTaskHandler);
});
} }
#endregion CheckUpdate #endregion CheckUpdate