v2rayN/v2rayN/ServiceLib/Handler/AppHandler.cs

273 lines
9.6 KiB
C#
Raw Normal View History

2024-10-07 02:59:13 +00:00
namespace ServiceLib.Handler
2022-02-24 12:45:24 +00:00
{
2024-10-07 01:51:41 +00:00
public sealed class AppHandler
2022-02-24 12:45:24 +00:00
{
#region Property
2024-10-07 01:51:41 +00:00
private static readonly Lazy<AppHandler> _instance = new(() => new());
2022-02-24 12:45:24 +00:00
private Config _config;
private int? _statePort;
private int? _statePort2;
2024-10-07 01:51:41 +00:00
private Job? _processJob;
private bool? _isAdministrator;
2024-10-07 01:51:41 +00:00
public static AppHandler Instance => _instance.Value;
2024-08-15 13:03:00 +00:00
public Config Config => _config;
public int StatePort
{
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 bool IsAdministrator
{
get
{
_isAdministrator ??= Utils.IsAdministrator();
return _isAdministrator.Value;
}
}
#endregion Property
2024-10-07 01:51:41 +00:00
#region Init
public bool InitApp()
{
2024-10-20 03:51:05 +00:00
_config = ConfigHandler.LoadConfig();
if (_config == null)
2024-10-07 01:51:41 +00:00
{
return false;
}
Thread.CurrentThread.CurrentUICulture = new(_config.UiItem.CurrentLanguage);
2024-10-07 01:51:41 +00:00
//Under Win10
if (Utils.IsWindows() && Environment.OSVersion.Version.Major < 10)
{
Environment.SetEnvironmentVariable("DOTNET_EnableWriteXorExecute", "0", EnvironmentVariableTarget.User);
}
2024-10-16 02:52:45 +00:00
SQLiteHelper.Instance.CreateTable<SubItem>();
SQLiteHelper.Instance.CreateTable<ProfileItem>();
SQLiteHelper.Instance.CreateTable<ServerStatItem>();
SQLiteHelper.Instance.CreateTable<RoutingItem>();
SQLiteHelper.Instance.CreateTable<ProfileExItem>();
SQLiteHelper.Instance.CreateTable<DNSItem>();
2024-10-07 01:51:41 +00:00
return true;
}
2024-02-08 06:01:33 +00:00
2024-10-07 01:51:41 +00:00
public bool InitComponents()
2023-01-01 11:42:01 +00:00
{
2024-10-07 01:51:41 +00:00
Logging.Setup();
Logging.LoggingEnabled(_config.GuiItem.EnableLog);
2024-10-07 01:51:41 +00:00
Logging.SaveLog($"v2rayN start up | {Utils.GetVersion()} | {Utils.GetExePath()}");
Logging.SaveLog($"{Environment.OSVersion} - {(Environment.Is64BitOperatingSystem ? 64 : 32)}");
Logging.ClearLogs();
return true;
2023-01-01 11:42:01 +00:00
}
public bool Reset()
{
_statePort = null;
_statePort2 = null;
return true;
}
2024-10-07 01:51:41 +00:00
#endregion Init
2023-01-01 11:42:01 +00:00
2024-10-07 01:51:41 +00:00
#region Config
2022-03-21 12:20:29 +00:00
2024-03-09 01:27:55 +00:00
public int GetLocalPort(EInboundProtocol protocol)
2023-01-01 11:42:01 +00:00
{
var localPort = _config.Inbound.FirstOrDefault(t => t.Protocol == nameof(EInboundProtocol.socks))?.LocalPort ?? 10808;
2024-03-09 01:27:55 +00:00
return localPort + (int)protocol;
2023-01-01 11:42:01 +00:00
}
2024-02-08 06:01:33 +00:00
public void AddProcess(IntPtr processHandle)
{
2024-08-27 11:44:50 +00:00
if (Utils.IsWindows())
2024-08-20 06:15:29 +00:00
{
_processJob ??= new();
_processJob?.AddProcess(processHandle);
2024-08-20 06:30:45 +00:00
}
2024-02-08 06:01:33 +00:00
}
#endregion Config
#region SqliteHelper
2023-01-01 11:42:01 +00:00
2024-10-24 07:35:37 +00:00
public async Task<List<SubItem>?> SubItems()
2023-01-01 11:42:01 +00:00
{
return await SQLiteHelper.Instance.TableAsync<SubItem>().OrderBy(t => t.Sort).ToListAsync();
2023-01-01 11:42:01 +00:00
}
2023-04-14 12:49:36 +00:00
2024-10-24 07:35:37 +00:00
public async Task<SubItem?> GetSubItem(string subid)
2023-01-01 11:42:01 +00:00
{
return await SQLiteHelper.Instance.TableAsync<SubItem>().FirstOrDefaultAsync(t => t.Id == subid);
2023-01-01 11:42:01 +00:00
}
2024-10-24 07:35:37 +00:00
public async Task<List<ProfileItem>?> ProfileItems(string subid)
2023-01-01 11:42:01 +00:00
{
2024-03-26 06:26:03 +00:00
if (Utils.IsNullOrEmpty(subid))
2023-01-01 11:42:01 +00:00
{
2024-10-21 01:45:33 +00:00
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().ToListAsync();
2023-01-01 11:42:01 +00:00
}
else
{
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().Where(t => t.Subid == subid).ToListAsync();
2023-01-01 11:42:01 +00:00
}
}
2023-04-14 12:49:36 +00:00
2024-10-24 07:35:37 +00:00
public async Task<List<string>?> ProfileItemIndexes(string subid)
{
2024-10-24 07:35:37 +00:00
return (await ProfileItems(subid))?.Select(t => t.IndexId)?.ToList();
}
2023-01-01 11:42:01 +00:00
2024-10-24 07:35:37 +00:00
public async Task<List<ProfileItemModel>?> ProfileItems(string subid, string filter)
2022-03-21 12:20:29 +00:00
{
2023-04-14 12:49:36 +00:00
var sql = @$"select a.*
,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 ";
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(subid))
2023-01-01 11:42:01 +00:00
{
2023-01-04 07:47:08 +00:00
sql += $" and a.subid = '{subid}'";
2023-01-01 11:42:01 +00:00
}
2024-09-17 08:52:41 +00:00
if (Utils.IsNotEmpty(filter))
2023-01-01 11:42:01 +00:00
{
2023-02-20 10:16:30 +00:00
if (filter.Contains('\''))
2023-01-31 06:13:25 +00:00
{
filter = filter.Replace("'", "");
}
2024-10-20 03:51:05 +00:00
sql += string.Format(" and (a.remarks like '%{0}%' or a.address like '%{0}%') ", filter);
2023-01-01 11:42:01 +00:00
}
2024-10-21 01:45:33 +00:00
return await SQLiteHelper.Instance.QueryAsync<ProfileItemModel>(sql);
2023-01-01 11:42:01 +00:00
}
2024-10-24 07:35:37 +00:00
public async Task<List<ProfileItemModel>?> ProfileItemsEx(string subid, string filter)
{
var lstModel = await ProfileItems(_config.SubIndexId, filter);
2024-10-21 01:45:33 +00:00
await ConfigHandler.SetDefaultServer(_config, lstModel);
var lstServerStat = (_config.GuiItem.EnableStatistics ? StatisticsHandler.Instance.ServerStat : null) ?? [];
var lstProfileExs = await ProfileExHandler.Instance.GetProfileExs();
lstModel = (from t in lstModel
join t2 in lstServerStat on t.IndexId equals t2.IndexId into t2b
from t22 in t2b.DefaultIfEmpty()
join t3 in lstProfileExs on t.IndexId equals t3.IndexId into t3b
from t33 in t3b.DefaultIfEmpty()
select new ProfileItemModel
{
IndexId = t.IndexId,
ConfigType = t.ConfigType,
Remarks = t.Remarks,
Address = t.Address,
Port = t.Port,
Security = t.Security,
Network = t.Network,
StreamSecurity = t.StreamSecurity,
Subid = t.Subid,
SubRemarks = t.SubRemarks,
IsActive = t.IndexId == _config.IndexId,
Sort = t33 == null ? 0 : t33.Sort,
Delay = t33 == null ? 0 : t33.Delay,
DelayVal = t33?.Delay != 0 ? $"{t33?.Delay} {Global.DelayUnit}" : string.Empty,
SpeedVal = t33?.Speed != 0 ? $"{t33?.Speed} {Global.SpeedUnit}" : string.Empty,
TodayDown = t22 == null ? "" : Utils.HumanFy(t22.TodayDown),
TodayUp = t22 == null ? "" : Utils.HumanFy(t22.TodayUp),
TotalDown = t22 == null ? "" : Utils.HumanFy(t22.TotalDown),
TotalUp = t22 == null ? "" : Utils.HumanFy(t22.TotalUp)
}).OrderBy(t => t.Sort).ToList();
return lstModel;
}
2024-10-21 01:45:33 +00:00
public async Task<ProfileItem?> GetProfileItem(string indexId)
2023-01-01 11:42:01 +00:00
{
2024-03-26 06:26:03 +00:00
if (Utils.IsNullOrEmpty(indexId))
2023-01-01 11:42:01 +00:00
{
return null;
}
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(it => it.IndexId == indexId);
2023-01-01 11:42:01 +00:00
}
2024-10-21 01:45:33 +00:00
public async Task<ProfileItem?> GetProfileItemViaRemarks(string? remarks)
2023-12-23 12:57:31 +00:00
{
2024-03-26 06:26:03 +00:00
if (Utils.IsNullOrEmpty(remarks))
2023-12-23 12:57:31 +00:00
{
return null;
}
return await SQLiteHelper.Instance.TableAsync<ProfileItem>().FirstOrDefaultAsync(it => it.Remarks == remarks);
2023-12-23 12:57:31 +00:00
}
2024-10-24 07:35:37 +00:00
public async Task<List<RoutingItem>?> RoutingItems()
2023-01-01 11:42:01 +00:00
{
2024-11-20 04:01:04 +00:00
return await SQLiteHelper.Instance.TableAsync<RoutingItem>().OrderBy(t => t.Sort).ToListAsync();
2023-01-01 11:42:01 +00:00
}
2023-04-14 12:49:36 +00:00
2024-10-24 07:35:37 +00:00
public async Task<RoutingItem?> GetRoutingItem(string id)
2023-01-01 11:42:01 +00:00
{
2024-11-20 04:01:04 +00:00
return await SQLiteHelper.Instance.TableAsync<RoutingItem>().FirstOrDefaultAsync(it => it.Id == id);
2023-01-01 11:42:01 +00:00
}
2024-10-24 07:35:37 +00:00
public async Task<List<DNSItem>?> DNSItems()
{
2024-10-21 01:45:33 +00:00
return await SQLiteHelper.Instance.TableAsync<DNSItem>().ToListAsync();
}
2024-10-24 07:35:37 +00:00
public async Task<DNSItem?> GetDNSItem(ECoreType eCoreType)
{
return await SQLiteHelper.Instance.TableAsync<DNSItem>().FirstOrDefaultAsync(it => it.CoreType == eCoreType);
}
2024-02-08 06:01:33 +00:00
#endregion SqliteHelper
2023-01-01 11:42:01 +00:00
#region Core Type
2024-02-19 09:43:36 +00:00
public List<string> GetShadowsocksSecurities(ProfileItem profileItem)
2023-01-01 11:42:01 +00:00
{
2024-01-23 04:30:11 +00:00
var coreType = GetCoreType(profileItem, EConfigType.Shadowsocks);
switch (coreType)
2022-03-21 12:20:29 +00:00
{
2024-01-23 04:30:11 +00:00
case ECoreType.v2fly:
2024-02-19 09:43:36 +00:00
return Global.SsSecurities;
2022-03-21 12:20:29 +00:00
2024-01-23 04:30:11 +00:00
case ECoreType.Xray:
2024-02-19 09:43:36 +00:00
return Global.SsSecuritiesInXray;
2024-01-23 04:30:11 +00:00
case ECoreType.sing_box:
2024-02-19 09:43:36 +00:00
return Global.SsSecuritiesInSingbox;
2024-01-23 04:30:11 +00:00
}
2024-11-13 10:00:31 +00:00
return Global.SsSecuritiesInSingbox;
2022-03-21 12:20:29 +00:00
}
2023-01-01 11:42:01 +00:00
public ECoreType GetCoreType(ProfileItem profileItem, EConfigType eConfigType)
2022-03-21 12:20:29 +00:00
{
if (profileItem?.CoreType != null)
2022-03-21 12:20:29 +00:00
{
return (ECoreType)profileItem.CoreType;
2022-03-21 12:20:29 +00:00
}
2024-12-03 05:58:56 +00:00
var item = _config.CoreTypeItem?.FirstOrDefault(it => it.ConfigType == eConfigType);
return item?.CoreType ?? ECoreType.Xray;
2022-03-21 12:20:29 +00:00
}
2022-03-28 10:54:05 +00:00
2023-04-14 12:49:36 +00:00
#endregion Core Type
2022-02-24 12:45:24 +00:00
}
2023-11-28 08:07:53 +00:00
}