Code optimization, function asynchrony

This commit is contained in:
2dust
2024-10-21 09:45:33 +08:00
parent 394b657fc9
commit a866017b4c
21 changed files with 518 additions and 514 deletions
+31 -27
View File
@@ -102,41 +102,45 @@
#region SqliteHelper
public List<SubItem> SubItems()
public async Task<List<SubItem>> SubItems()
{
return SQLiteHelper.Instance.Table<SubItem>().ToList();
return await SQLiteHelper.Instance.TableAsync<SubItem>().OrderBy(t => t.sort).ToListAsync();
}
public SubItem GetSubItem(string subid)
public async Task<SubItem> GetSubItem(string subid)
{
return SQLiteHelper.Instance.Table<SubItem>().FirstOrDefault(t => t.id == subid);
return await SQLiteHelper.Instance.TableAsync<SubItem>().FirstOrDefaultAsync(t => t.id == subid);
}
public List<ProfileItem> ProfileItems(string subid)
public async Task<List<ProfileItem>> ProfileItems(string subid)
{
if (Utils.IsNullOrEmpty(subid))
{
return SQLiteHelper.Instance.Table<ProfileItem>().ToList();
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().ToListAsync();
}
else
{
return SQLiteHelper.Instance.Table<ProfileItem>().Where(t => t.subid == subid).ToList();
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().Where(t => t.subid == subid).ToListAsync();
}
}
public List<string> ProfileItemIndexes(string subid)
public async Task<List<string>> ProfileItemIndexes(string subid)
{
if (Utils.IsNullOrEmpty(subid))
{
return SQLiteHelper.Instance.Table<ProfileItem>().Select(t => t.indexId).ToList();
return (await SQLiteHelper.Instance.TableAsync<ProfileItem>().ToListAsync())
.Select(t => t.indexId)
.ToList();
}
else
{
return SQLiteHelper.Instance.Table<ProfileItem>().Where(t => t.subid == subid).Select(t => t.indexId).ToList();
return (await SQLiteHelper.Instance.TableAsync<ProfileItem>().Where(t => t.subid == subid).ToListAsync())
.Select(t => t.indexId)
.ToList();
}
}
public List<ProfileItemModel> ProfileItems(string subid, string filter)
public async Task<List<ProfileItemModel>> ProfileItems(string subid, string filter)
{
var sql = @$"select a.*
,b.remarks subRemarks
@@ -156,14 +160,14 @@
sql += string.Format(" and (a.remarks like '%{0}%' or a.address like '%{0}%') ", filter);
}
return SQLiteHelper.Instance.Query<ProfileItemModel>(sql).ToList();
return await SQLiteHelper.Instance.QueryAsync<ProfileItemModel>(sql);
}
public List<ProfileItemModel> ProfileItemsEx(string subid, string filter)
public async Task<List<ProfileItemModel>> ProfileItemsEx(string subid, string filter)
{
var lstModel = ProfileItems(_config.subIndexId, filter);
var lstModel = await ProfileItems(_config.subIndexId, filter);
ConfigHandler.SetDefaultServer(_config, lstModel);
await ConfigHandler.SetDefaultServer(_config, lstModel);
var lstServerStat = (_config.guiItem.enableStatistics ? StatisticsHandler.Instance.ServerStat : null) ?? [];
var lstProfileExs = ProfileExHandler.Instance.ProfileExs;
@@ -198,42 +202,42 @@
return lstModel;
}
public ProfileItem? GetProfileItem(string indexId)
public async Task<ProfileItem?> GetProfileItem(string indexId)
{
if (Utils.IsNullOrEmpty(indexId))
{
return null;
}
return SQLiteHelper.Instance.Table<ProfileItem>().FirstOrDefault(it => it.indexId == indexId);
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(it => it.indexId == indexId);
}
public ProfileItem? GetProfileItemViaRemarks(string? remarks)
public async Task<ProfileItem?> GetProfileItemViaRemarks(string? remarks)
{
if (Utils.IsNullOrEmpty(remarks))
{
return null;
}
return SQLiteHelper.Instance.Table<ProfileItem>().FirstOrDefault(it => it.remarks == remarks);
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(it => it.remarks == remarks);
}
public List<RoutingItem> RoutingItems()
public async Task<List<RoutingItem>> RoutingItems()
{
return SQLiteHelper.Instance.Table<RoutingItem>().Where(it => it.locked == false).OrderBy(t => t.sort).ToList();
return await SQLiteHelper.Instance.TableAsync<RoutingItem>().Where(it => it.locked == false).OrderBy(t => t.sort).ToListAsync();
}
public RoutingItem GetRoutingItem(string id)
public async Task<RoutingItem> GetRoutingItem(string id)
{
return SQLiteHelper.Instance.Table<RoutingItem>().FirstOrDefault(it => it.locked == false && it.id == id);
return await SQLiteHelper.Instance.TableAsync<RoutingItem>().FirstOrDefaultAsync(it => it.locked == false && it.id == id);
}
public List<DNSItem> DNSItems()
public async Task<List<DNSItem>> DNSItems()
{
return SQLiteHelper.Instance.Table<DNSItem>().ToList();
return await SQLiteHelper.Instance.TableAsync<DNSItem>().ToListAsync();
}
public DNSItem GetDNSItem(ECoreType eCoreType)
public async Task<DNSItem> GetDNSItem(ECoreType eCoreType)
{
return SQLiteHelper.Instance.Table<DNSItem>().FirstOrDefault(it => it.coreType == eCoreType);
return await SQLiteHelper.Instance.TableAsync<DNSItem>().FirstOrDefaultAsync(it => it.coreType == eCoreType);
}
#endregion SqliteHelper
+48 -40
View File
@@ -218,7 +218,7 @@ namespace ServiceLib.Handler
public static async Task<int> AddServer(Config config, ProfileItem profileItem)
{
var item = AppHandler.Instance.GetProfileItem(profileItem.indexId);
var item = await AppHandler.Instance.GetProfileItem(profileItem.indexId);
if (item is null)
{
item = profileItem;
@@ -330,7 +330,7 @@ namespace ServiceLib.Handler
{
foreach (var it in indexes)
{
var item = AppHandler.Instance.GetProfileItem(it.indexId);
var item = await AppHandler.Instance.GetProfileItem(it.indexId);
if (item is null)
{
continue;
@@ -382,23 +382,26 @@ namespace ServiceLib.Handler
{
return 0;
}
if (SQLiteHelper.Instance.Table<ProfileItem>().Where(t => t.indexId == config.indexId).Any())
var count = await SQLiteHelper.Instance.TableAsync<ProfileItem>().CountAsync(t => t.indexId == config.indexId);
if (count > 0)
{
return 0;
}
if (lstProfile.Count > 0)
{
return await SetDefaultServerIndex(config, lstProfile.Where(t => t.port > 0).FirstOrDefault()?.indexId);
return await SetDefaultServerIndex(config, lstProfile.FirstOrDefault(t => t.port > 0)?.indexId);
}
return await SetDefaultServerIndex(config, SQLiteHelper.Instance.Table<ProfileItem>().Where(t => t.port > 0).Select(t => t.indexId).FirstOrDefault());
var item = await SQLiteHelper.Instance.TableAsync<ProfileItem>().Where(t => t.port > 0).FirstOrDefaultAsync();
return await SetDefaultServerIndex(config, item.indexId);
}
public static async Task<ProfileItem?> GetDefaultServer(Config config)
{
var item = AppHandler.Instance.GetProfileItem(config.indexId);
var item = await AppHandler.Instance.GetProfileItem(config.indexId);
if (item is null)
{
var item2 = SQLiteHelper.Instance.Table<ProfileItem>().FirstOrDefault();
var item2 = await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync();
await SetDefaultServerIndex(config, item2?.indexId);
return item2;
}
@@ -531,7 +534,7 @@ namespace ServiceLib.Handler
/// <returns></returns>
public static async Task<int> EditCustomServer(Config config, ProfileItem profileItem)
{
var item = AppHandler.Instance.GetProfileItem(profileItem.indexId);
var item = await AppHandler.Instance.GetProfileItem(profileItem.indexId);
if (item is null)
{
item = profileItem;
@@ -748,7 +751,7 @@ namespace ServiceLib.Handler
public static async Task<int> SortServers(Config config, string subId, string colName, bool asc)
{
var lstModel = AppHandler.Instance.ProfileItems(subId, "");
var lstModel = await AppHandler.Instance.ProfileItems(subId, "");
if (lstModel.Count <= 0)
{
return -1;
@@ -880,7 +883,7 @@ namespace ServiceLib.Handler
public static async Task<Tuple<int, int>> DedupServerList(Config config, string subId)
{
var lstProfile = AppHandler.Instance.ProfileItems(subId);
var lstProfile = await AppHandler.Instance.ProfileItems(subId);
List<ProfileItem> lstKeep = new();
List<ProfileItem> lstRemove = new();
@@ -980,7 +983,7 @@ namespace ServiceLib.Handler
{
try
{
var item = AppHandler.Instance.GetProfileItem(indexId);
var item = await AppHandler.Instance.GetProfileItem(indexId);
if (item == null)
{
return 0;
@@ -1000,22 +1003,24 @@ namespace ServiceLib.Handler
return 0;
}
public static async Task<Tuple<int, string>> AddCustomServer4Multiple(Config config, List<ProfileItem> selecteds, ECoreType coreType)
public static async Task<RetResult> AddCustomServer4Multiple(Config config, List<ProfileItem> selecteds, ECoreType coreType)
{
var indexId = Utils.GetMd5(Global.CoreMultipleLoadConfigFileName);
string configPath = Utils.GetConfigPath(Global.CoreMultipleLoadConfigFileName);
if (CoreConfigHandler.GenerateClientMultipleLoadConfig(config, configPath, selecteds, coreType, out string msg) != 0)
var configPath = Utils.GetConfigPath(Global.CoreMultipleLoadConfigFileName);
var result = await CoreConfigHandler.GenerateClientMultipleLoadConfig(config, configPath, selecteds, coreType);
if (result.Code != 0)
{
return new Tuple<int, string>(-1, "");
return result;
}
var fileName = configPath;
if (!File.Exists(fileName))
{
return new Tuple<int, string>(-1, "");
return result;
}
var profileItem = AppHandler.Instance.GetProfileItem(indexId) ?? new();
var profileItem = await AppHandler.Instance.GetProfileItem(indexId) ?? new();
profileItem.indexId = indexId;
profileItem.remarks = coreType == ECoreType.sing_box ? ResUI.menuSetDefaultMultipleServer : ResUI.menuSetDefaultLoadBalanceServer;
profileItem.address = Global.CoreMultipleLoadConfigFileName;
@@ -1024,7 +1029,8 @@ namespace ServiceLib.Handler
await AddServerCommon(config, profileItem, true);
return new Tuple<int, string>(0, indexId);
result.Data = indexId;
return result;
}
#endregion Server
@@ -1050,7 +1056,7 @@ namespace ServiceLib.Handler
if (isSub && Utils.IsNotEmpty(subid))
{
await RemoveServerViaSubid(config, subid, isSub);
subFilter = AppHandler.Instance.GetSubItem(subid)?.filter ?? "";
subFilter = (await AppHandler.Instance.GetSubItem(subid))?.filter ?? "";
}
int countServers = 0;
@@ -1089,7 +1095,7 @@ namespace ServiceLib.Handler
//Check for duplicate indexId
if (lstDbIndexId is null)
{
lstDbIndexId = AppHandler.Instance.ProfileItemIndexes("");
lstDbIndexId = await AppHandler.Instance.ProfileItemIndexes("");
}
if (lstAdd.Any(t => t.indexId == existItem.indexId)
|| lstDbIndexId.Any(t => t == existItem.indexId))
@@ -1149,7 +1155,7 @@ namespace ServiceLib.Handler
return -1;
}
var subItem = AppHandler.Instance.GetSubItem(subid);
var subItem = await AppHandler.Instance.GetSubItem(subid);
var subRemarks = subItem?.remarks;
var preSocksPort = subItem?.preSocksPort;
@@ -1284,7 +1290,7 @@ namespace ServiceLib.Handler
List<ProfileItem>? lstOriSub = null;
if (isSub && Utils.IsNotEmpty(subid))
{
lstOriSub = AppHandler.Instance.ProfileItems(subid);
lstOriSub = await AppHandler.Instance.ProfileItems(subid);
}
var counter = 0;
@@ -1328,7 +1334,8 @@ namespace ServiceLib.Handler
public static async Task<int> AddSubItem(Config config, string url)
{
//already exists
if (SQLiteHelper.Instance.Table<SubItem>().Any(e => e.url == url))
var count = await SQLiteHelper.Instance.TableAsync<SubItem>().CountAsync(e => e.url == url);
if (count > 0)
{
return 0;
}
@@ -1354,7 +1361,7 @@ namespace ServiceLib.Handler
public static async Task<int> AddSubItem(Config config, SubItem subItem)
{
var item = AppHandler.Instance.GetSubItem(subItem.id);
var item = await AppHandler.Instance.GetSubItem(subItem.id);
if (item is null)
{
item = subItem;
@@ -1383,9 +1390,10 @@ namespace ServiceLib.Handler
if (item.sort <= 0)
{
var maxSort = 0;
if (SQLiteHelper.Instance.Table<SubItem>().Count() > 0)
if (await SQLiteHelper.Instance.TableAsync<SubItem>().CountAsync() > 0)
{
maxSort = SQLiteHelper.Instance.Table<SubItem>().Max(t => t == null ? 0 : t.sort);
var lstSubs = (await AppHandler.Instance.SubItems());
maxSort = lstSubs.LastOrDefault()?.sort ?? 0;
}
item.sort = maxSort + 1;
}
@@ -1412,7 +1420,7 @@ namespace ServiceLib.Handler
{
return -1;
}
var customProfile = SQLiteHelper.Instance.Table<ProfileItem>().Where(t => t.subid == subid && t.configType == EConfigType.Custom).ToList();
var customProfile = await SQLiteHelper.Instance.TableAsync<ProfileItem>().Where(t => t.subid == subid && t.configType == EConfigType.Custom).ToListAsync();
if (isSub)
{
await SQLiteHelper.Instance.ExecuteAsync($"delete from ProfileItem where isSub = 1 and subid = '{subid}'");
@@ -1431,7 +1439,7 @@ namespace ServiceLib.Handler
public static async Task<int> DeleteSubItem(Config config, string id)
{
var item = AppHandler.Instance.GetSubItem(id);
var item = await AppHandler.Instance.GetSubItem(id);
if (item is null)
{
return 0;
@@ -1594,7 +1602,7 @@ namespace ServiceLib.Handler
public static async Task<int> SetDefaultRouting(Config config, RoutingItem routingItem)
{
if (SQLiteHelper.Instance.Table<RoutingItem>().Where(t => t.id == routingItem.id).Count() > 0)
if (await SQLiteHelper.Instance.TableAsync<RoutingItem>().Where(t => t.id == routingItem.id).CountAsync() > 0)
{
config.routingBasicItem.routingIndexId = routingItem.id;
}
@@ -1606,10 +1614,10 @@ namespace ServiceLib.Handler
public static async Task<RoutingItem> GetDefaultRouting(Config config)
{
var item = AppHandler.Instance.GetRoutingItem(config.routingBasicItem.routingIndexId);
var item = await AppHandler.Instance.GetRoutingItem(config.routingBasicItem.routingIndexId);
if (item is null)
{
var item2 = SQLiteHelper.Instance.Table<RoutingItem>().FirstOrDefault(t => t.locked == false);
var item2 = await SQLiteHelper.Instance.TableAsync<RoutingItem>().FirstOrDefaultAsync(t => t.locked == false);
await SetDefaultRouting(config, item2);
return item2;
}
@@ -1642,7 +1650,7 @@ namespace ServiceLib.Handler
if (template == null)
return await InitBuiltinRouting(config, blImportAdvancedRules); // fallback
var items = AppHandler.Instance.RoutingItems();
var items = await AppHandler.Instance.RoutingItems();
var maxSort = items.Count;
if (!blImportAdvancedRules && items.Where(t => t.remarks.StartsWith(template.version)).ToList().Count > 0)
{
@@ -1682,7 +1690,7 @@ namespace ServiceLib.Handler
public static async Task<int> InitBuiltinRouting(Config config, bool blImportAdvancedRules = false)
{
var ver = "V3-";
var items = AppHandler.Instance.RoutingItems();
var items = await AppHandler.Instance.RoutingItems();
if (!blImportAdvancedRules && items.Where(t => t.remarks.StartsWith(ver)).ToList().Count > 0)
{
return 0;
@@ -1723,9 +1731,9 @@ namespace ServiceLib.Handler
return 0;
}
public static RoutingItem? GetLockedRoutingItem(Config config)
public static async Task<RoutingItem?> GetLockedRoutingItem(Config config)
{
return SQLiteHelper.Instance.Table<RoutingItem>().FirstOrDefault(it => it.locked == true);
return await SQLiteHelper.Instance.TableAsync<RoutingItem>().FirstOrDefaultAsync(it => it.locked == true);
}
public static async Task RemoveRoutingItem(RoutingItem routingItem)
@@ -1739,7 +1747,7 @@ namespace ServiceLib.Handler
public static async Task<int> InitBuiltinDNS(Config config)
{
var items = AppHandler.Instance.DNSItems();
var items = await AppHandler.Instance.DNSItems();
if (items.Count <= 0)
{
var item = new DNSItem()
@@ -1782,9 +1790,9 @@ namespace ServiceLib.Handler
}
}
public static DNSItem GetExternalDNSItem(ECoreType type, string url)
public static async Task<DNSItem> GetExternalDNSItem(ECoreType type, string url)
{
var currentItem = AppHandler.Instance.GetDNSItem(type);
var currentItem = await AppHandler.Instance.GetDNSItem(type);
var downloadHandle = new DownloadService();
var templateContent = Task.Run(() => downloadHandle.TryDownloadString(url, true, "")).Result;
@@ -1832,8 +1840,8 @@ namespace ServiceLib.Handler
config.constItem.srsSourceUrl = Global.SingboxRulesetSources[1];
config.constItem.routeRulesTemplateSourceUrl = Global.RoutingRulesSources[1];
await SaveDNSItems(config, GetExternalDNSItem(ECoreType.Xray, Global.DNSTemplateSources[1] + "v2ray.json"));
await SaveDNSItems(config, GetExternalDNSItem(ECoreType.sing_box, Global.DNSTemplateSources[1] + "sing_box.json"));
await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.Xray, Global.DNSTemplateSources[1] + "v2ray.json"));
await SaveDNSItems(config, await GetExternalDNSItem(ECoreType.sing_box, Global.DNSTemplateSources[1] + "sing_box.json"));
return true;
}
+68 -103
View File
@@ -5,86 +5,55 @@
/// </summary>
public class CoreConfigHandler
{
public static int GenerateClientConfig(ProfileItem node, string? fileName, out string msg, out string content)
public static async Task<RetResult> GenerateClientConfig(ProfileItem node, string? fileName)
{
content = string.Empty;
try
{
if (node == null)
{
msg = ResUI.CheckServerSettings;
return -1;
}
var config = AppHandler.Instance.Config;
var config = AppHandler.Instance.Config;
var result = new RetResult(-1);
msg = ResUI.InitialConfiguration;
if (node.configType == EConfigType.Custom)
if (node.configType == EConfigType.Custom)
{
if (node.coreType is ECoreType.mihomo)
{
if (node.coreType is ECoreType.mihomo)
{
var configGenClash = new CoreConfigClashService(config);
return configGenClash.GenerateClientCustomConfig(node, fileName, out msg);
}
if (node.coreType is ECoreType.sing_box)
{
var configGenSingbox = new CoreConfigSingboxService(config);
return configGenSingbox.GenerateClientCustomConfig(node, fileName, out msg);
}
else
{
return GenerateClientCustomConfig(node, fileName, out msg);
}
result = await new CoreConfigClashService(config).GenerateClientCustomConfig(node, fileName);
}
else if (AppHandler.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box)
if (node.coreType is ECoreType.sing_box)
{
var configGenSingbox = new CoreConfigSingboxService(config);
if (configGenSingbox.GenerateClientConfigContent(node, out SingboxConfig? singboxConfig, out msg) != 0)
{
return -1;
}
if (Utils.IsNullOrEmpty(fileName))
{
content = JsonUtils.Serialize(singboxConfig);
}
else
{
JsonUtils.ToFile(singboxConfig, fileName, false);
}
result = await new CoreConfigSingboxService(config).GenerateClientCustomConfig(node, fileName);
}
else
{
var coreConfigV2ray = new CoreConfigV2rayService(config);
if (coreConfigV2ray.GenerateClientConfigContent(node, out V2rayConfig? v2rayConfig, out msg) != 0)
{
return -1;
}
if (Utils.IsNullOrEmpty(fileName))
{
content = JsonUtils.Serialize(v2rayConfig);
}
else
{
JsonUtils.ToFile(v2rayConfig, fileName, false);
}
result = await GenerateClientCustomConfig(node, fileName);
}
}
catch (Exception ex)
else if (AppHandler.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box)
{
Logging.SaveLog("GenerateClientConfig", ex);
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
result = await new CoreConfigSingboxService(config).GenerateClientConfigContent(node);
}
return 0;
else
{
result = await new CoreConfigV2rayService(config).GenerateClientConfigContent(node);
}
if (result.Code != 0)
{
return result;
}
if (Utils.IsNotEmpty(fileName) && result.Data != null)
{
await File.WriteAllTextAsync(fileName, result.Data.ToString());
}
return result;
}
private static int GenerateClientCustomConfig(ProfileItem node, string? fileName, out string msg)
private static async Task<RetResult> GenerateClientCustomConfig(ProfileItem node, string? fileName)
{
var ret = new RetResult(-1);
try
{
if (node == null || fileName is null)
{
msg = ResUI.CheckServerSettings;
return -1;
ret.Msg = ResUI.CheckServerSettings;
return ret;
}
if (File.Exists(fileName))
@@ -100,8 +69,8 @@
}
if (!File.Exists(addressFileName))
{
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
File.Copy(addressFileName, fileName);
File.SetAttributes(fileName, FileAttributes.Normal); //Copy will keep the attributes of addressFileName, so we need to add write permissions to fileName just in case of addressFileName is a read-only file.
@@ -109,63 +78,59 @@
//check again
if (!File.Exists(fileName))
{
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Msg = string.Format(ResUI.SuccessfulConfiguration, "");
ret.Code = 0;
return ret;
}
catch (Exception ex)
{
Logging.SaveLog("GenerateClientCustomConfig", ex);
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
ret.Msg = ResUI.FailedGenDefaultConfiguration;
return ret;
}
return 0;
}
public static int GenerateClientSpeedtestConfig(Config config, string fileName, List<ServerTestItem> selecteds, ECoreType coreType, out string msg)
public static async Task<RetResult> GenerateClientSpeedtestConfig(Config config, string fileName, List<ServerTestItem> selecteds, ECoreType coreType)
{
var result = new RetResult(-1);
if (coreType == ECoreType.sing_box)
{
if (new CoreConfigSingboxService(config).GenerateClientSpeedtestConfig(selecteds, out SingboxConfig? singboxConfig, out msg) != 0)
{
return -1;
}
JsonUtils.ToFile(singboxConfig, fileName, false);
}
else
{
if (new CoreConfigV2rayService(config).GenerateClientSpeedtestConfig(selecteds, out V2rayConfig? v2rayConfig, out msg) != 0)
{
return -1;
}
JsonUtils.ToFile(v2rayConfig, fileName, false);
}
return 0;
}
public static int GenerateClientMultipleLoadConfig(Config config, string fileName, List<ProfileItem> selecteds, ECoreType coreType, out string msg)
{
msg = ResUI.CheckServerSettings;
if (coreType == ECoreType.sing_box)
{
if (new CoreConfigSingboxService(config).GenerateClientMultipleLoadConfig(selecteds, out SingboxConfig? singboxConfig, out msg) != 0)
{
return -1;
}
JsonUtils.ToFile(singboxConfig, fileName, false);
result = await new CoreConfigSingboxService(config).GenerateClientSpeedtestConfig(selecteds);
}
else if (coreType == ECoreType.Xray)
{
if (new CoreConfigV2rayService(config).GenerateClientMultipleLoadConfig(selecteds, out V2rayConfig? v2rayConfig, out msg) != 0)
{
return -1;
}
JsonUtils.ToFile(v2rayConfig, fileName, false);
result = await new CoreConfigV2rayService(config).GenerateClientSpeedtestConfig(selecteds);
}
if (result.Code != 0)
{
return result;
}
await File.WriteAllTextAsync(fileName, result.Data.ToString());
return result;
}
public static async Task<RetResult> GenerateClientMultipleLoadConfig(Config config, string fileName, List<ProfileItem> selecteds, ECoreType coreType)
{
var result = new RetResult(-1);
if (coreType == ECoreType.sing_box)
{
result = await new CoreConfigSingboxService(config).GenerateClientMultipleLoadConfig(selecteds);
}
else if (coreType == ECoreType.Xray)
{
result = await new CoreConfigV2rayService(config).GenerateClientMultipleLoadConfig(selecteds);
}
return 0;
if (result.Code != 0)
{
return result;
}
await File.WriteAllTextAsync(fileName, result.Data.ToString());
return result;
}
}
}
+16 -18
View File
@@ -24,7 +24,7 @@ namespace ServiceLib.Handler
Environment.SetEnvironmentVariable("xray.location.asset", Utils.GetBinPath(""), EnvironmentVariableTarget.Process);
}
public void LoadCore(ProfileItem? node)
public async Task LoadCore(ProfileItem? node)
{
if (node == null)
{
@@ -32,18 +32,18 @@ namespace ServiceLib.Handler
return;
}
string fileName = Utils.GetConfigPath(Global.CoreConfigFileName);
if (CoreConfigHandler.GenerateClientConfig(node, fileName, out string msg, out string content) != 0)
var fileName = Utils.GetConfigPath(Global.CoreConfigFileName);
var result = await CoreConfigHandler.GenerateClientConfig(node, fileName);
ShowMsg(false, result.Msg);
if (result.Code != 0)
{
ShowMsg(false, msg);
return;
}
else
{
ShowMsg(false, msg);
ShowMsg(true, $"{node.GetSummary()}");
CoreStop();
CoreStart(node);
await CoreStart(node);
//In tun mode, do a delay check and restart the core
//if (_config.tunModeItem.enableTun)
@@ -65,18 +65,15 @@ namespace ServiceLib.Handler
}
}
public int LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
public async Task<int> LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
{
int pid = -1;
var coreType = selecteds.Exists(t => t.ConfigType == EConfigType.Hysteria2 || t.ConfigType == EConfigType.TUIC || t.ConfigType == EConfigType.WireGuard) ? ECoreType.sing_box : ECoreType.Xray;
string configPath = Utils.GetConfigPath(Global.CoreSpeedtestConfigFileName);
if (CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType, out string msg) != 0)
var pid = -1;
var coreType = selecteds.Exists(t => t.ConfigType is EConfigType.Hysteria2 or EConfigType.TUIC or EConfigType.WireGuard) ? ECoreType.sing_box : ECoreType.Xray;
var configPath = Utils.GetConfigPath(Global.CoreSpeedtestConfigFileName);
var result = await CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType);
ShowMsg(false, result.Msg);
if (result.Code == 0)
{
ShowMsg(false, msg);
}
else
{
ShowMsg(false, msg);
pid = CoreStartSpeedtest(configPath, coreType);
}
return pid;
@@ -170,7 +167,7 @@ namespace ServiceLib.Handler
return fileName;
}
private void CoreStart(ProfileItem node)
private async Task CoreStart(ProfileItem node)
{
ShowMsg(false, $"{Environment.OSVersion} - {(Environment.Is64BitOperatingSystem ? 64 : 32)}");
ShowMsg(false, string.Format(ResUI.StartService, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
@@ -227,7 +224,8 @@ namespace ServiceLib.Handler
if (itemSocks != null)
{
string fileName2 = Utils.GetConfigPath(Global.CorePreConfigFileName);
if (CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2, out string msg2, out string configStr) == 0)
var result = await CoreConfigHandler.GenerateClientConfig(itemSocks, fileName2);
if (result.Code == 0)
{
var coreInfo2 = CoreInfoHandler.Instance.GetCoreInfo(preCoreType);
var proc2 = RunProcess(node, coreInfo2, $" -c {Global.CorePreConfigFileName}", true);
@@ -29,7 +29,7 @@ namespace ServiceLib.Handler
{
await SQLiteHelper.Instance.ExecuteAsync($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )");
_lstProfileEx = new(SQLiteHelper.Instance.Table<ProfileExItem>());
_lstProfileEx = new(await SQLiteHelper.Instance.TableAsync<ProfileExItem>().ToListAsync());
}
private void IndexIdEnqueue(string indexId)
@@ -45,7 +45,7 @@ namespace ServiceLib.Handler
var cnt = _queIndexIds.Count;
if (cnt > 0)
{
var lstExists = SQLiteHelper.Instance.Table<ProfileExItem>();
var lstExists = await SQLiteHelper.Instance.TableAsync<ProfileExItem>().ToListAsync();
List<ProfileExItem> lstInserts = [];
List<ProfileExItem> lstUpdates = [];
@@ -71,7 +71,7 @@
long ticks = DateTime.Now.Date.Ticks;
await SQLiteHelper.Instance.ExecuteAsync($"update ServerStatItem set todayUp = 0,todayDown=0,dateNow={ticks} where dateNow<>{ticks}");
_lstServerStat = SQLiteHelper.Instance.Table<ServerStatItem>().ToList();
_lstServerStat = await SQLiteHelper.Instance.TableAsync<ServerStatItem>().ToListAsync();
}
private void UpdateServerStatHandler(ServerSpeedItem server)
+1 -1
View File
@@ -24,7 +24,7 @@
while (true)
{
var updateTime = ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds();
var lstSubs = AppHandler.Instance.SubItems()
var lstSubs = (await AppHandler.Instance.SubItems())
.Where(t => t.autoUpdateInterval > 0)
.Where(t => updateTime - t.updateTime >= t.autoUpdateInterval * 60)
.ToList();