Skip to content

Commit

Permalink
Rename LazyConfig to AppHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 7, 2024
1 parent f40eb72 commit 3bdef4d
Show file tree
Hide file tree
Showing 59 changed files with 186 additions and 198 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
namespace ServiceLib.Handler
using Splat;

namespace ServiceLib.Handler
{
public sealed class LazyConfig
public sealed class AppHandler
{
private static readonly Lazy<LazyConfig> _instance = new(() => new());
private static readonly Lazy<AppHandler> _instance = new(() => new());
private Config _config;
private int? _statePort;
private int? _statePort2;

public static LazyConfig Instance => _instance.Value;
private Job? _processJob;
public static AppHandler Instance => _instance.Value;
public Config Config => _config;

public int StatePort
Expand All @@ -28,21 +30,50 @@ public int StatePort2
}
}

private Job? _processJob;
#region Init

public AppHandler()
{
}

public bool InitApp()
{
if (ConfigHandler.LoadConfig(ref _config) != 0)
{
return false;
}
Locator.CurrentMutable.RegisterLazySingleton(() => new NoticeHandler(), typeof(NoticeHandler));
Thread.CurrentThread.CurrentUICulture = new(_config.uiItem.currentLanguage);

//Under Win10
if (Utils.IsWindows() && Environment.OSVersion.Version.Major < 10)
{
Environment.SetEnvironmentVariable("DOTNET_EnableWriteXorExecute", "0", EnvironmentVariableTarget.User);
}
return true;
}

public LazyConfig()
public bool InitComponents()
{
Logging.Setup();
Logging.LoggingEnabled(true);
Logging.SaveLog($"v2rayN start up | {Utils.GetVersion()} | {Utils.GetExePath()}");
Logging.SaveLog($"{Environment.OSVersion} - {(Environment.Is64BitOperatingSystem ? 64 : 32)}");
Logging.ClearLogs();

SQLiteHelper.Instance.CreateTable<SubItem>();
SQLiteHelper.Instance.CreateTable<ProfileItem>();
SQLiteHelper.Instance.CreateTable<ServerStatItem>();
SQLiteHelper.Instance.CreateTable<RoutingItem>();
SQLiteHelper.Instance.CreateTable<ProfileExItem>();
SQLiteHelper.Instance.CreateTable<DNSItem>();

return true;
}

#region Config
#endregion Init

public void SetConfig(Config config) => _config = config;
#region Config

public int GetLocalPort(EInboundProtocol protocol)
{
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/ServiceLib/Handler/ClashApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void ClashProxiesDelayTest(bool blAll, List<ClashProxyModel> lstProxy, Ac
return;
}
var urlBase = $"{GetApiUrl()}/proxies";
urlBase += @"/{0}/delay?timeout=10000&url=" + LazyConfig.Instance.Config.speedTestItem.speedPingTestUrl;
urlBase += @"/{0}/delay?timeout=10000&url=" + AppHandler.Instance.Config.speedTestItem.speedPingTestUrl;

List<Task> tasks = new List<Task>();
foreach (var it in lstProxy)
Expand Down Expand Up @@ -200,7 +200,7 @@ public async void ClashConnectionClose(string id)

private string GetApiUrl()
{
return $"{Global.HttpProtocol}{Global.Loopback}:{LazyConfig.Instance.StatePort2}";
return $"{Global.HttpProtocol}{Global.Loopback}:{AppHandler.Instance.StatePort2}";
}
}
}
36 changes: 18 additions & 18 deletions v2rayN/ServiceLib/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private static void ToJsonFile(Config config)

public static int AddServer(Config config, ProfileItem profileItem)
{
var item = LazyConfig.Instance.GetProfileItem(profileItem.indexId);
var item = AppHandler.Instance.GetProfileItem(profileItem.indexId);
if (item is null)
{
item = profileItem;
Expand Down Expand Up @@ -476,7 +476,7 @@ public static int CopyServer(Config config, List<ProfileItem> indexes)
{
foreach (var it in indexes)
{
var item = LazyConfig.Instance.GetProfileItem(it.indexId);
var item = AppHandler.Instance.GetProfileItem(it.indexId);
if (item is null)
{
continue;
Expand Down Expand Up @@ -541,7 +541,7 @@ public static int SetDefaultServer(Config config, List<ProfileItemModel> lstProf

public static ProfileItem? GetDefaultServer(Config config)
{
var item = LazyConfig.Instance.GetProfileItem(config.indexId);
var item = AppHandler.Instance.GetProfileItem(config.indexId);
if (item is null)
{
var item2 = SQLiteHelper.Instance.Table<ProfileItem>().FirstOrDefault();
Expand Down Expand Up @@ -677,7 +677,7 @@ public static int AddCustomServer(Config config, ProfileItem profileItem, bool b
/// <returns></returns>
public static int EditCustomServer(Config config, ProfileItem profileItem)
{
var item = LazyConfig.Instance.GetProfileItem(profileItem.indexId);
var item = AppHandler.Instance.GetProfileItem(profileItem.indexId);
if (item is null)
{
item = profileItem;
Expand Down Expand Up @@ -717,7 +717,7 @@ public static int AddShadowsocksServer(Config config, ProfileItem profileItem, b
profileItem.id = profileItem.id.TrimEx();
profileItem.security = profileItem.security.TrimEx();

if (!LazyConfig.Instance.GetShadowsocksSecurities(profileItem).Contains(profileItem.security))
if (!AppHandler.Instance.GetShadowsocksSecurities(profileItem).Contains(profileItem.security))
{
return -1;
}
Expand Down Expand Up @@ -894,7 +894,7 @@ public static int AddWireguardServer(Config config, ProfileItem profileItem, boo

public static int SortServers(Config config, string subId, string colName, bool asc)
{
var lstModel = LazyConfig.Instance.ProfileItems(subId, "");
var lstModel = AppHandler.Instance.ProfileItems(subId, "");
if (lstModel.Count <= 0)
{
return -1;
Expand Down Expand Up @@ -1026,7 +1026,7 @@ public static int AddVlessServer(Config config, ProfileItem profileItem, bool to

public static Tuple<int, int> DedupServerList(Config config, string subId)
{
var lstProfile = LazyConfig.Instance.ProfileItems(subId);
var lstProfile = AppHandler.Instance.ProfileItems(subId);

List<ProfileItem> lstKeep = new();
List<ProfileItem> lstRemove = new();
Expand Down Expand Up @@ -1126,7 +1126,7 @@ private static int RemoveProfileItem(Config config, string indexId)
{
try
{
var item = LazyConfig.Instance.GetProfileItem(indexId);
var item = AppHandler.Instance.GetProfileItem(indexId);
if (item == null)
{
return 0;
Expand Down Expand Up @@ -1161,7 +1161,7 @@ public static int AddCustomServer4Multiple(Config config, List<ProfileItem> sele
return -1;
}

var profileItem = LazyConfig.Instance.GetProfileItem(indexId) ?? new();
var profileItem = AppHandler.Instance.GetProfileItem(indexId) ?? new();
profileItem.indexId = indexId;
profileItem.remarks = coreType == ECoreType.sing_box ? ResUI.menuSetDefaultMultipleServer : ResUI.menuSetDefaultLoadBalanceServer;
profileItem.address = Global.CoreMultipleLoadConfigFileName;
Expand Down Expand Up @@ -1196,7 +1196,7 @@ private static int AddBatchServers(Config config, string strData, string subid,
if (isSub && Utils.IsNotEmpty(subid))
{
RemoveServerViaSubid(config, subid, isSub);
subFilter = LazyConfig.Instance.GetSubItem(subid)?.filter ?? "";
subFilter = AppHandler.Instance.GetSubItem(subid)?.filter ?? "";
}

int countServers = 0;
Expand Down Expand Up @@ -1235,7 +1235,7 @@ private static int AddBatchServers(Config config, string strData, string subid,
//Check for duplicate indexId
if (lstDbIndexId is null)
{
lstDbIndexId = LazyConfig.Instance.ProfileItemIndexes("");
lstDbIndexId = AppHandler.Instance.ProfileItemIndexes("");
}
if (lstAdd.Any(t => t.indexId == existItem.indexId)
|| lstDbIndexId.Any(t => t == existItem.indexId))
Expand Down Expand Up @@ -1295,7 +1295,7 @@ private static int AddBatchServers4Custom(Config config, string strData, string
return -1;
}

var subItem = LazyConfig.Instance.GetSubItem(subid);
var subItem = AppHandler.Instance.GetSubItem(subid);
var subRemarks = subItem?.remarks;
var preSocksPort = subItem?.preSocksPort;

Expand Down Expand Up @@ -1430,7 +1430,7 @@ public static int AddBatchServers(Config config, string strData, string subid, b
List<ProfileItem>? lstOriSub = null;
if (isSub && Utils.IsNotEmpty(subid))
{
lstOriSub = LazyConfig.Instance.ProfileItems(subid);
lstOriSub = AppHandler.Instance.ProfileItems(subid);
}

var counter = 0;
Expand Down Expand Up @@ -1500,7 +1500,7 @@ public static int AddSubItem(Config config, string url)

public static int AddSubItem(Config config, SubItem subItem)
{
var item = LazyConfig.Instance.GetSubItem(subItem.id);
var item = AppHandler.Instance.GetSubItem(subItem.id);
if (item is null)
{
item = subItem;
Expand Down Expand Up @@ -1577,7 +1577,7 @@ public static int RemoveServerViaSubid(Config config, string subid, bool isSub)

public static int DeleteSubItem(Config config, string id)
{
var item = LazyConfig.Instance.GetSubItem(id);
var item = AppHandler.Instance.GetSubItem(id);
if (item is null)
{
return 0;
Expand Down Expand Up @@ -1752,7 +1752,7 @@ public static int SetDefaultRouting(Config config, RoutingItem routingItem)

public static RoutingItem GetDefaultRouting(Config config)
{
var item = LazyConfig.Instance.GetRoutingItem(config.routingBasicItem.routingIndexId);
var item = AppHandler.Instance.GetRoutingItem(config.routingBasicItem.routingIndexId);
if (item is null)
{
var item2 = SQLiteHelper.Instance.Table<RoutingItem>().FirstOrDefault(t => t.locked == false);
Expand All @@ -1766,7 +1766,7 @@ public static RoutingItem GetDefaultRouting(Config config)
public static int InitBuiltinRouting(Config config, bool blImportAdvancedRules = false)
{
var ver = "V3-";
var items = LazyConfig.Instance.RoutingItems();
var items = AppHandler.Instance.RoutingItems();
if (blImportAdvancedRules || items.Where(t => t.remarks.StartsWith(ver)).ToList().Count <= 0)
{
var maxSort = items.Count;
Expand Down Expand Up @@ -1832,7 +1832,7 @@ public static void RemoveRoutingItem(RoutingItem routingItem)

public static int InitBuiltinDNS(Config config)
{
var items = LazyConfig.Instance.DNSItems();
var items = AppHandler.Instance.DNSItems();
if (items.Count <= 0)
{
var item = new DNSItem()
Expand Down
6 changes: 3 additions & 3 deletions v2rayN/ServiceLib/Handler/CoreConfig/CoreConfigClash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ public int GenerateClientCustomConfig(ProfileItem node, string? fileName, out st
}

//port
fileContent["port"] = LazyConfig.Instance.GetLocalPort(EInboundProtocol.http);
fileContent["port"] = AppHandler.Instance.GetLocalPort(EInboundProtocol.http);
//socks-port
fileContent["socks-port"] = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks);
fileContent["socks-port"] = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks);
//log-level
fileContent["log-level"] = GetLogLevel(_config.coreBasicItem.loglevel);

//external-controller
fileContent["external-controller"] = $"{Global.Loopback}:{LazyConfig.Instance.StatePort2}";
fileContent["external-controller"] = $"{Global.Loopback}:{AppHandler.Instance.StatePort2}";
//allow-lan
if (_config.inbound[0].allowLANConn)
{
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/ServiceLib/Handler/CoreConfig/CoreConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static int GenerateClientConfig(ProfileItem node, string? fileName, out s
msg = ResUI.CheckServerSettings;
return -1;
}
var config = LazyConfig.Instance.Config;
var config = AppHandler.Instance.Config;

msg = ResUI.InitialConfiguration;
if (node.configType == EConfigType.Custom)
Expand All @@ -35,7 +35,7 @@ public static int GenerateClientConfig(ProfileItem node, string? fileName, out s
return GenerateClientCustomConfig(node, fileName, out msg);
}
}
else if (LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box)
else if (AppHandler.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box)
{
var configGenSingbox = new CoreConfigSingbox(config);
if (configGenSingbox.GenerateClientConfigContent(node, out SingboxConfig? singboxConfig, out msg) != 0)
Expand Down
20 changes: 10 additions & 10 deletions v2rayN/ServiceLib/Handler/CoreConfig/CoreConfigSingbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public int GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds, out Sin
singboxConfig.inbounds.Clear(); // Remove "proxy" service for speedtest, avoiding port conflicts.
singboxConfig.outbounds.RemoveAt(0);

int httpPort = LazyConfig.Instance.GetLocalPort(EInboundProtocol.speedtest);
int httpPort = AppHandler.Instance.GetLocalPort(EInboundProtocol.speedtest);

foreach (var it in selecteds)
{
Expand All @@ -132,7 +132,7 @@ public int GenerateClientSpeedtestConfig(List<ServerTestItem> selecteds, out Sin
{
continue;
}
var item = LazyConfig.Instance.GetProfileItem(it.indexId);
var item = AppHandler.Instance.GetProfileItem(it.indexId);
if (it.configType is EConfigType.VMess or EConfigType.VLESS)
{
if (item is null || Utils.IsNullOrEmpty(item.id) || !Utils.IsGuidByParse(item.id))
Expand Down Expand Up @@ -282,7 +282,7 @@ public int GenerateClientMultipleLoadConfig(List<ProfileItem> selecteds, out Sin
{
continue;
}
var item = LazyConfig.Instance.GetProfileItem(it.indexId);
var item = AppHandler.Instance.GetProfileItem(it.indexId);
if (item is null)
{
continue;
Expand Down Expand Up @@ -486,7 +486,7 @@ private int GenInbounds(SingboxConfig singboxConfig)
};
singboxConfig.inbounds.Add(inbound);

inbound.listen_port = LazyConfig.Instance.GetLocalPort(EInboundProtocol.socks);
inbound.listen_port = AppHandler.Instance.GetLocalPort(EInboundProtocol.socks);
inbound.sniff = _config.inbound[0].sniffingEnabled;
inbound.sniff_override_destination = _config.inbound[0].routeOnly ? false : _config.inbound[0].sniffingEnabled;
inbound.domain_strategy = Utils.IsNullOrEmpty(_config.routingBasicItem.domainStrategy4Singbox) ? null : _config.routingBasicItem.domainStrategy4Singbox;
Expand Down Expand Up @@ -600,7 +600,7 @@ private int GenOutbound(ProfileItem node, Outbound4Sbox outbound)
}
case EConfigType.Shadowsocks:
{
outbound.method = LazyConfig.Instance.GetShadowsocksSecurities(node).Contains(node.security) ? node.security : Global.None;
outbound.method = AppHandler.Instance.GetShadowsocksSecurities(node).Contains(node.security) ? node.security : Global.None;
outbound.password = node.id;

GenOutboundMux(node, outbound);
Expand Down Expand Up @@ -854,7 +854,7 @@ private int GenMoreOutbounds(ProfileItem node, SingboxConfig singboxConfig)
}
try
{
var subItem = LazyConfig.Instance.GetSubItem(node.subid);
var subItem = AppHandler.Instance.GetSubItem(node.subid);
if (subItem is null)
{
return 0;
Expand All @@ -865,7 +865,7 @@ private int GenMoreOutbounds(ProfileItem node, SingboxConfig singboxConfig)
var txtOutbound = Utils.GetEmbedText(Global.SingboxSampleOutbound);

//Previous proxy
var prevNode = LazyConfig.Instance.GetProfileItemViaRemarks(subItem.prevProfile);
var prevNode = AppHandler.Instance.GetProfileItemViaRemarks(subItem.prevProfile);
if (prevNode is not null
&& prevNode.configType != EConfigType.Custom)
{
Expand All @@ -878,7 +878,7 @@ private int GenMoreOutbounds(ProfileItem node, SingboxConfig singboxConfig)
}

//Next proxy
var nextNode = LazyConfig.Instance.GetProfileItemViaRemarks(subItem.nextProfile);
var nextNode = AppHandler.Instance.GetProfileItemViaRemarks(subItem.nextProfile);
if (nextNode is not null
&& nextNode.configType != EConfigType.Custom)
{
Expand Down Expand Up @@ -1173,7 +1173,7 @@ private int GenDns(ProfileItem? node, SingboxConfig singboxConfig)
{
try
{
var item = LazyConfig.Instance.GetDNSItem(ECoreType.sing_box);
var item = AppHandler.Instance.GetDNSItem(ECoreType.sing_box);
var strDNS = string.Empty;
if (_config.tunModeItem.enableTun)
{
Expand Down Expand Up @@ -1260,7 +1260,7 @@ private int GenExperimental(SingboxConfig singboxConfig)
singboxConfig.experimental ??= new Experimental4Sbox();
singboxConfig.experimental.clash_api = new Clash_Api4Sbox()
{
external_controller = $"{Global.Loopback}:{LazyConfig.Instance.StatePort2}",
external_controller = $"{Global.Loopback}:{AppHandler.Instance.StatePort2}",
};
}

Expand Down
Loading

0 comments on commit 3bdef4d

Please sign in to comment.