This commit is contained in:
2dust 2024-02-08 14:01:33 +08:00
parent 05e424f805
commit e2c836794b
11 changed files with 63 additions and 34 deletions

View file

@ -27,10 +27,10 @@ namespace v2rayN
/// <param name="e"></param> /// <param name="e"></param>
protected override void OnStartup(StartupEventArgs e) protected override void OnStartup(StartupEventArgs e)
{ {
Global.ExePathKey = Utils.GetMD5(Utils.GetExePath()); var exePathKey = Utils.GetMD5(Utils.GetExePath());
var rebootas = (e.Args ?? new string[] { }).Any(t => t == Global.RebootAs); var rebootas = (e.Args ?? new string[] { }).Any(t => t == Global.RebootAs);
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, Global.ExePathKey, out bool bCreatedNew); ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, exePathKey, out bool bCreatedNew);
if (!rebootas && !bCreatedNew) if (!rebootas && !bCreatedNew)
{ {
ProgramStarted.Set(); ProgramStarted.Set();
@ -39,8 +39,6 @@ namespace v2rayN
return; return;
} }
Global.ProcessJob = new Job();
Logging.Setup(); Logging.Setup();
Init(); Init();
Logging.LoggingEnabled(_config.guiItem.enableLog); Logging.LoggingEnabled(_config.guiItem.enableLog);

View file

@ -576,6 +576,28 @@ namespace v2rayN
return inUse; return inUse;
} }
public static int GetFreePort()
{
try
{
int defaultPort = 9090;
if (!Utils.PortInUse(defaultPort))
{
return defaultPort;
}
TcpListener l = new(IPAddress.Loopback, 0);
l.Start();
int port = ((IPEndPoint)l.LocalEndpoint).Port;
l.Stop();
return port;
}
catch
{
}
return 69090;
}
#endregion #endregion
#region #region

View file

@ -118,6 +118,7 @@ namespace v2rayN
@"http://cachefly.cachefly.net/100mb.test", @"http://cachefly.cachefly.net/100mb.test",
@"http://cachefly.cachefly.net/10mb.test" @"http://cachefly.cachefly.net/10mb.test"
}; };
public static readonly List<string> SpeedPingTestUrls = new() { public static readonly List<string> SpeedPingTestUrls = new() {
@"https://www.google.com/generate_204", @"https://www.google.com/generate_204",
}; };
@ -187,14 +188,5 @@ namespace v2rayN
public static readonly List<string> TuicCongestionControls = new() { "cubic", "new_reno", "bbr" }; public static readonly List<string> TuicCongestionControls = new() { "cubic", "new_reno", "bbr" };
#endregion const #endregion const
#region global variable
public static int StatePort { get; set; }
public static Job ProcessJob { get; set; }
public static bool ShowInTaskbar { get; set; }
public static string ExePathKey { get; set; }
#endregion global variable
} }
} }

View file

@ -811,7 +811,7 @@ namespace v2rayN.Handler
//}, //},
clash_api = new Clash_Api4Sbox() clash_api = new Clash_Api4Sbox()
{ {
external_controller = $"{Global.Loopback}:{Global.StatePort}", external_controller = $"{Global.Loopback}:{LazyConfig.Instance.StatePort}",
} }
}; };
} }

View file

@ -788,7 +788,7 @@ namespace v2rayN.Handler
Inboundsettings4Ray apiInboundSettings = new(); Inboundsettings4Ray apiInboundSettings = new();
apiInbound.tag = tag; apiInbound.tag = tag;
apiInbound.listen = Global.Loopback; apiInbound.listen = Global.Loopback;
apiInbound.port = Global.StatePort; apiInbound.port = LazyConfig.Instance.StatePort;
apiInbound.protocol = Global.InboundAPIProtocal; apiInbound.protocol = Global.InboundAPIProtocal;
apiInboundSettings.address = Global.Loopback; apiInboundSettings.address = Global.Loopback;
apiInbound.settings = apiInboundSettings; apiInbound.settings = apiInboundSettings;

View file

@ -316,7 +316,7 @@ namespace v2rayN.Handler
throw new Exception(displayLog ? proc.StandardError.ReadToEnd() : "启动进程失败并退出 (Failed to start the process and exited)"); throw new Exception(displayLog ? proc.StandardError.ReadToEnd() : "启动进程失败并退出 (Failed to start the process and exited)");
} }
Global.ProcessJob.AddProcess(proc.Handle); LazyConfig.Instance.AddProcess(proc.Handle);
return proc; return proc;
} }
catch (Exception ex) catch (Exception ex)

View file

@ -11,8 +11,14 @@ namespace v2rayN.Handler
public static LazyConfig Instance => _instance.Value; public static LazyConfig Instance => _instance.Value;
private int _statePort;
public int StatePort { get => _statePort; }
private Job _processJob = new();
public LazyConfig() public LazyConfig()
{ {
_statePort = Utils.GetFreePort();
SqliteHelper.Instance.CreateTable<SubItem>(); SqliteHelper.Instance.CreateTable<SubItem>();
SqliteHelper.Instance.CreateTable<ProfileItem>(); SqliteHelper.Instance.CreateTable<ProfileItem>();
SqliteHelper.Instance.CreateTable<ServerStatItem>(); SqliteHelper.Instance.CreateTable<ServerStatItem>();
@ -62,6 +68,15 @@ namespace v2rayN.Handler
} }
return localPort; return localPort;
} }
public void AddProcess(IntPtr processHandle)
{
_processJob.AddProcess(processHandle);
}
#endregion Config
#region SqliteHelper
public List<SubItem> SubItems() public List<SubItem> SubItems()
{ {
@ -158,7 +173,7 @@ namespace v2rayN.Handler
return SqliteHelper.Instance.Table<DNSItem>().FirstOrDefault(it => it.coreType == eCoreType); return SqliteHelper.Instance.Table<DNSItem>().FirstOrDefault(it => it.coreType == eCoreType);
} }
#endregion Config #endregion SqliteHelper
#region Core Type #region Core Type

View file

@ -28,7 +28,6 @@ namespace v2rayN.Handler
_updateFunc = update; _updateFunc = update;
Init(); Init();
Global.StatePort = GetFreePort();
_statisticsV2Ray = new StatisticsV2ray(config, UpdateServerStat); _statisticsV2Ray = new StatisticsV2ray(config, UpdateServerStat);
_statisticsSingbox = new StatisticsSingbox(config, UpdateServerStat); _statisticsSingbox = new StatisticsSingbox(config, UpdateServerStat);
@ -80,6 +79,10 @@ namespace v2rayN.Handler
{ {
GetServerStatItem(_config.indexId); GetServerStatItem(_config.indexId);
if (_serverStatItem is null)
{
return;
}
if (server.proxyUp != 0 || server.proxyDown != 0) if (server.proxyUp != 0 || server.proxyDown != 0)
{ {
_serverStatItem.todayUp += server.proxyUp; _serverStatItem.todayUp += server.proxyUp;
@ -87,15 +90,13 @@ namespace v2rayN.Handler
_serverStatItem.totalUp += server.proxyUp; _serverStatItem.totalUp += server.proxyUp;
_serverStatItem.totalDown += server.proxyDown; _serverStatItem.totalDown += server.proxyDown;
} }
if (Global.ShowInTaskbar)
{ server.indexId = _config.indexId;
server.indexId = _config.indexId; server.todayUp = _serverStatItem.todayUp;
server.todayUp = _serverStatItem.todayUp; server.todayDown = _serverStatItem.todayDown;
server.todayDown = _serverStatItem.todayDown; server.totalUp = _serverStatItem.totalUp;
server.totalUp = _serverStatItem.totalUp; server.totalDown = _serverStatItem.totalDown;
server.totalDown = _serverStatItem.totalDown; _updateFunc(server);
_updateFunc(server);
}
} }
private void GetServerStatItem(string indexId) private void GetServerStatItem(string indexId)

View file

@ -27,7 +27,7 @@ namespace v2rayN.Handler
try try
{ {
url = $"ws://{Global.Loopback}:{Global.StatePort}/traffic"; url = $"ws://{Global.Loopback}:{LazyConfig.Instance.StatePort}/traffic";
if (webSocket == null) if (webSocket == null)
{ {

View file

@ -30,7 +30,7 @@ namespace v2rayN.Handler
{ {
try try
{ {
_channel = GrpcChannel.ForAddress($"{Global.HttpProtocol}{Global.Loopback}:{Global.StatePort}"); _channel = GrpcChannel.ForAddress($"{Global.HttpProtocol}{Global.Loopback}:{LazyConfig.Instance.StatePort}");
_client = new StatsService.StatsServiceClient(_channel); _client = new StatsService.StatsServiceClient(_channel);
} }
catch (Exception ex) catch (Exception ex)

View file

@ -35,6 +35,7 @@ namespace v2rayN.ViewModels
private readonly PaletteHelper _paletteHelper = new(); private readonly PaletteHelper _paletteHelper = new();
private Dictionary<string, bool> _dicHeaderSort = new(); private Dictionary<string, bool> _dicHeaderSort = new();
private Action<EViewAction> _updateView; private Action<EViewAction> _updateView;
private bool _showInTaskbar;
#endregion private prop #endregion private prop
@ -568,7 +569,7 @@ namespace v2rayN.ViewModels
AutoHideStartup(); AutoHideStartup();
Global.ShowInTaskbar = true; _showInTaskbar = true;
} }
private void Init() private void Init()
@ -635,7 +636,7 @@ namespace v2rayN.ViewModels
{ {
Application.Current.Dispatcher.Invoke((Action)(() => Application.Current.Dispatcher.Invoke((Action)(() =>
{ {
if (!Global.ShowInTaskbar) if (!_showInTaskbar)
{ {
return; return;
} }
@ -1183,7 +1184,7 @@ namespace v2rayN.ViewModels
_noticeHandler?.SendMessage(msg, true); _noticeHandler?.SendMessage(msg, true);
Application.Current.Dispatcher.Invoke((Action)(() => Application.Current.Dispatcher.Invoke((Action)(() =>
{ {
if (!Global.ShowInTaskbar) if (!_showInTaskbar)
{ {
return; return;
} }
@ -1658,7 +1659,7 @@ namespace v2rayN.ViewModels
public void ShowHideWindow(bool? blShow) public void ShowHideWindow(bool? blShow)
{ {
var bl = blShow ?? !Global.ShowInTaskbar; var bl = blShow ?? !_showInTaskbar;
if (bl) if (bl)
{ {
//Application.Current.MainWindow.ShowInTaskbar = true; //Application.Current.MainWindow.ShowInTaskbar = true;
@ -1677,7 +1678,7 @@ namespace v2rayN.ViewModels
//IntPtr windowHandle = new WindowInteropHelper(Application.Current.MainWindow).Handle; //IntPtr windowHandle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
//Utils.RegWriteValue(Global.MyRegPath, Utils.WindowHwndKey, Convert.ToString((long)windowHandle)); //Utils.RegWriteValue(Global.MyRegPath, Utils.WindowHwndKey, Convert.ToString((long)windowHandle));
} }
Global.ShowInTaskbar = bl; _showInTaskbar = bl;
} }
private void RestoreUI() private void RestoreUI()