v2rayN/v2rayN/ServiceLib/Handler/AppHandler.cs

240 lines
6.7 KiB
C#
Raw Normal View History

namespace ServiceLib.Handler;
public sealed class AppHandler
2022-02-24 12:45:24 +00:00
{
#region Property
private static readonly Lazy<AppHandler> _instance = new(() => new());
private Config _config;
private int? _statePort;
private int? _statePort2;
private Job? _processJob;
public static AppHandler Instance => _instance.Value;
public Config Config => _config;
public int StatePort
2022-02-24 12:45:24 +00:00
{
get
{
_statePort ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api));
return _statePort.Value;
}
}
public int StatePort2
{
get
{
_statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2));
return _statePort2.Value + (_config.TunModeItem.EnableTun ? 1 : 0);
}
}
public string LinuxSudoPwd { get; set; }
#endregion Property
#region Init
2024-10-07 01:51:41 +00:00
public bool InitApp()
{
if (Utils.HasWritePermission() == false)
2024-10-07 01:51:41 +00:00
{
Environment.SetEnvironmentVariable(Global.LocalAppData, "1", EnvironmentVariableTarget.Process);
2024-10-07 01:51:41 +00:00
}
2024-02-08 06:01:33 +00:00
Logging.Setup();
var config = ConfigHandler.LoadConfig();
if (config == null)
2023-01-01 11:42:01 +00:00
{
return false;
2023-01-01 11:42:01 +00:00
}
_config = config;
Thread.CurrentThread.CurrentUICulture = new(_config.UiItem.CurrentLanguage);
2023-01-01 11:42:01 +00:00
//Under Win10
if (Utils.IsWindows() && Environment.OSVersion.Version.Major < 10)
{
Environment.SetEnvironmentVariable("DOTNET_EnableWriteXorExecute", "0", EnvironmentVariableTarget.User);
}
SQLiteHelper.Instance.CreateTable<SubItem>();
SQLiteHelper.Instance.CreateTable<ProfileItem>();
SQLiteHelper.Instance.CreateTable<ServerStatItem>();
SQLiteHelper.Instance.CreateTable<RoutingItem>();
SQLiteHelper.Instance.CreateTable<ProfileExItem>();
2025-07-13 09:22:10 +00:00
SQLiteHelper.Instance.CreateTable<DNSItem>();
return true;
}
2022-03-21 12:20:29 +00:00
public bool InitComponents()
{
Logging.SaveLog($"v2rayN start up | {Utils.GetRuntimeInfo()}");
Logging.LoggingEnabled(_config.GuiItem.EnableLog);
//First determine the port value
_ = StatePort;
_ = StatePort2;
return true;
}
2024-02-08 06:01:33 +00:00
public bool Reset()
{
_statePort = null;
_statePort2 = null;
return true;
}
2024-02-08 06:01:33 +00:00
#endregion Init
2023-01-01 11:42:01 +00:00
#region Config
2023-04-14 12:49:36 +00:00
public int GetLocalPort(EInboundProtocol protocol)
{
var localPort = _config.Inbound.FirstOrDefault(t => t.Protocol == nameof(EInboundProtocol.socks))?.LocalPort ?? 10808;
return localPort + (int)protocol;
}
2023-01-01 11:42:01 +00:00
public void AddProcess(IntPtr processHandle)
{
if (Utils.IsWindows())
2023-01-01 11:42:01 +00:00
{
_processJob ??= new();
try
2023-01-01 11:42:01 +00:00
{
_processJob?.AddProcess(processHandle);
2023-01-01 11:42:01 +00:00
}
catch
2023-01-01 11:42:01 +00:00
{
}
}
}
2023-04-14 12:49:36 +00:00
#endregion Config
#region SqliteHelper
public async Task<List<SubItem>?> SubItems()
{
return await SQLiteHelper.Instance.TableAsync<SubItem>().OrderBy(t => t.Sort).ToListAsync();
}
public async Task<SubItem?> GetSubItem(string? subid)
{
return await SQLiteHelper.Instance.TableAsync<SubItem>().FirstOrDefaultAsync(t => t.Id == subid);
}
public async Task<List<ProfileItem>?> ProfileItems(string subid)
{
if (subid.IsNullOrEmpty())
{
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().ToListAsync();
}
else
2022-03-21 12:20:29 +00:00
{
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().Where(t => t.Subid == subid).ToListAsync();
}
}
public async Task<List<string>?> ProfileItemIndexes(string subid)
{
return (await ProfileItems(subid))?.Select(t => t.IndexId)?.ToList();
}
public async Task<List<ProfileItemModel>?> ProfileItems(string subid, string filter)
{
var sql = @$"select a.*
2023-04-14 12:49:36 +00:00
,b.remarks subRemarks
2023-01-01 11:42:01 +00:00
from ProfileItem a
2023-04-14 12:49:36 +00:00
left join SubItem b on a.subid = b.id
2023-01-01 11:42:01 +00:00
where 1=1 ";
if (subid.IsNotEmpty())
2023-01-01 11:42:01 +00:00
{
sql += $" and a.subid = '{subid}'";
2023-01-01 11:42:01 +00:00
}
if (filter.IsNotEmpty())
2023-12-23 12:57:31 +00:00
{
if (filter.Contains('\''))
2023-12-23 12:57:31 +00:00
{
filter = filter.Replace("'", "");
2023-12-23 12:57:31 +00:00
}
sql += string.Format(" and (a.remarks like '%{0}%' or a.address like '%{0}%') ", filter);
2023-12-23 12:57:31 +00:00
}
return await SQLiteHelper.Instance.QueryAsync<ProfileItemModel>(sql);
}
2023-04-14 12:49:36 +00:00
public async Task<ProfileItem?> GetProfileItem(string indexId)
{
if (indexId.IsNullOrEmpty())
2023-01-01 11:42:01 +00:00
{
return null;
2023-01-01 11:42:01 +00:00
}
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(it => it.IndexId == indexId);
}
2023-01-01 11:42:01 +00:00
public async Task<ProfileItem?> GetProfileItemViaRemarks(string? remarks)
{
if (remarks.IsNullOrEmpty())
{
return null;
}
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(it => it.Remarks == remarks);
}
public async Task<List<RoutingItem>?> RoutingItems()
{
return await SQLiteHelper.Instance.TableAsync<RoutingItem>().OrderBy(t => t.Sort).ToListAsync();
}
public async Task<RoutingItem?> GetRoutingItem(string id)
{
return await SQLiteHelper.Instance.TableAsync<RoutingItem>().FirstOrDefaultAsync(it => it.Id == id);
}
2023-01-01 11:42:01 +00:00
2025-07-13 09:22:10 +00:00
public async Task<List<DNSItem>?> DNSItems()
{
return await SQLiteHelper.Instance.TableAsync<DNSItem>().ToListAsync();
}
public async Task<DNSItem?> GetDNSItem(ECoreType eCoreType)
{
return await SQLiteHelper.Instance.TableAsync<DNSItem>().FirstOrDefaultAsync(it => it.CoreType == eCoreType);
}
#endregion SqliteHelper
#region Core Type
public List<string> GetShadowsocksSecurities(ProfileItem profileItem)
{
var coreType = GetCoreType(profileItem, EConfigType.Shadowsocks);
switch (coreType)
2023-01-01 11:42:01 +00:00
{
case ECoreType.v2fly:
return Global.SsSecurities;
2022-03-21 12:20:29 +00:00
case ECoreType.Xray:
return Global.SsSecuritiesInXray;
2024-01-23 04:30:11 +00:00
case ECoreType.sing_box:
return Global.SsSecuritiesInSingbox;
2022-03-21 12:20:29 +00:00
}
return Global.SsSecuritiesInSingbox;
}
2022-03-21 12:20:29 +00:00
public ECoreType GetCoreType(ProfileItem profileItem, EConfigType eConfigType)
{
if (profileItem?.CoreType != null)
2022-03-21 12:20:29 +00:00
{
return (ECoreType)profileItem.CoreType;
2022-03-21 12:20:29 +00:00
}
2022-03-28 10:54:05 +00:00
var item = _config.CoreTypeItem?.FirstOrDefault(it => it.ConfigType == eConfigType);
return item?.CoreType ?? ECoreType.Xray;
2022-02-24 12:45:24 +00:00
}
#endregion Core Type
}