mirror of https://github.com/2dust/v2rayN
Refactor
parent
05e424f805
commit
e2c836794b
|
@ -27,10 +27,10 @@ namespace v2rayN
|
|||
/// <param name="e"></param>
|
||||
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);
|
||||
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, Global.ExePathKey, out bool bCreatedNew);
|
||||
ProgramStarted = new EventWaitHandle(false, EventResetMode.AutoReset, exePathKey, out bool bCreatedNew);
|
||||
if (!rebootas && !bCreatedNew)
|
||||
{
|
||||
ProgramStarted.Set();
|
||||
|
@ -39,8 +39,6 @@ namespace v2rayN
|
|||
return;
|
||||
}
|
||||
|
||||
Global.ProcessJob = new Job();
|
||||
|
||||
Logging.Setup();
|
||||
Init();
|
||||
Logging.LoggingEnabled(_config.guiItem.enableLog);
|
||||
|
|
|
@ -576,6 +576,28 @@ namespace v2rayN
|
|||
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 测速
|
||||
|
||||
#region 杂项
|
||||
|
|
|
@ -118,6 +118,7 @@ namespace v2rayN
|
|||
@"http://cachefly.cachefly.net/100mb.test",
|
||||
@"http://cachefly.cachefly.net/10mb.test"
|
||||
};
|
||||
|
||||
public static readonly List<string> SpeedPingTestUrls = new() {
|
||||
@"https://www.google.com/generate_204",
|
||||
};
|
||||
|
@ -187,14 +188,5 @@ namespace v2rayN
|
|||
public static readonly List<string> TuicCongestionControls = new() { "cubic", "new_reno", "bbr" };
|
||||
|
||||
#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
|
||||
}
|
||||
}
|
|
@ -811,7 +811,7 @@ namespace v2rayN.Handler
|
|||
//},
|
||||
clash_api = new Clash_Api4Sbox()
|
||||
{
|
||||
external_controller = $"{Global.Loopback}:{Global.StatePort}",
|
||||
external_controller = $"{Global.Loopback}:{LazyConfig.Instance.StatePort}",
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -788,7 +788,7 @@ namespace v2rayN.Handler
|
|||
Inboundsettings4Ray apiInboundSettings = new();
|
||||
apiInbound.tag = tag;
|
||||
apiInbound.listen = Global.Loopback;
|
||||
apiInbound.port = Global.StatePort;
|
||||
apiInbound.port = LazyConfig.Instance.StatePort;
|
||||
apiInbound.protocol = Global.InboundAPIProtocal;
|
||||
apiInboundSettings.address = Global.Loopback;
|
||||
apiInbound.settings = apiInboundSettings;
|
||||
|
|
|
@ -316,7 +316,7 @@ namespace v2rayN.Handler
|
|||
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;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
|
@ -11,8 +11,14 @@ namespace v2rayN.Handler
|
|||
|
||||
public static LazyConfig Instance => _instance.Value;
|
||||
|
||||
private int _statePort;
|
||||
public int StatePort { get => _statePort; }
|
||||
private Job _processJob = new();
|
||||
|
||||
public LazyConfig()
|
||||
{
|
||||
_statePort = Utils.GetFreePort();
|
||||
|
||||
SqliteHelper.Instance.CreateTable<SubItem>();
|
||||
SqliteHelper.Instance.CreateTable<ProfileItem>();
|
||||
SqliteHelper.Instance.CreateTable<ServerStatItem>();
|
||||
|
@ -62,6 +68,15 @@ namespace v2rayN.Handler
|
|||
}
|
||||
return localPort;
|
||||
}
|
||||
|
||||
public void AddProcess(IntPtr processHandle)
|
||||
{
|
||||
_processJob.AddProcess(processHandle);
|
||||
}
|
||||
|
||||
#endregion Config
|
||||
|
||||
#region SqliteHelper
|
||||
|
||||
public List<SubItem> SubItems()
|
||||
{
|
||||
|
@ -158,7 +173,7 @@ namespace v2rayN.Handler
|
|||
return SqliteHelper.Instance.Table<DNSItem>().FirstOrDefault(it => it.coreType == eCoreType);
|
||||
}
|
||||
|
||||
#endregion Config
|
||||
#endregion SqliteHelper
|
||||
|
||||
#region Core Type
|
||||
|
||||
|
|
|
@ -28,7 +28,6 @@ namespace v2rayN.Handler
|
|||
_updateFunc = update;
|
||||
|
||||
Init();
|
||||
Global.StatePort = GetFreePort();
|
||||
|
||||
_statisticsV2Ray = new StatisticsV2ray(config, UpdateServerStat);
|
||||
_statisticsSingbox = new StatisticsSingbox(config, UpdateServerStat);
|
||||
|
@ -80,6 +79,10 @@ namespace v2rayN.Handler
|
|||
{
|
||||
GetServerStatItem(_config.indexId);
|
||||
|
||||
if (_serverStatItem is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (server.proxyUp != 0 || server.proxyDown != 0)
|
||||
{
|
||||
_serverStatItem.todayUp += server.proxyUp;
|
||||
|
@ -87,15 +90,13 @@ namespace v2rayN.Handler
|
|||
_serverStatItem.totalUp += server.proxyUp;
|
||||
_serverStatItem.totalDown += server.proxyDown;
|
||||
}
|
||||
if (Global.ShowInTaskbar)
|
||||
{
|
||||
server.indexId = _config.indexId;
|
||||
server.todayUp = _serverStatItem.todayUp;
|
||||
server.todayDown = _serverStatItem.todayDown;
|
||||
server.totalUp = _serverStatItem.totalUp;
|
||||
server.totalDown = _serverStatItem.totalDown;
|
||||
_updateFunc(server);
|
||||
}
|
||||
|
||||
server.indexId = _config.indexId;
|
||||
server.todayUp = _serverStatItem.todayUp;
|
||||
server.todayDown = _serverStatItem.todayDown;
|
||||
server.totalUp = _serverStatItem.totalUp;
|
||||
server.totalDown = _serverStatItem.totalDown;
|
||||
_updateFunc(server);
|
||||
}
|
||||
|
||||
private void GetServerStatItem(string indexId)
|
||||
|
|
|
@ -27,7 +27,7 @@ namespace v2rayN.Handler
|
|||
|
||||
try
|
||||
{
|
||||
url = $"ws://{Global.Loopback}:{Global.StatePort}/traffic";
|
||||
url = $"ws://{Global.Loopback}:{LazyConfig.Instance.StatePort}/traffic";
|
||||
|
||||
if (webSocket == null)
|
||||
{
|
||||
|
|
|
@ -30,7 +30,7 @@ namespace v2rayN.Handler
|
|||
{
|
||||
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);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
|
@ -35,6 +35,7 @@ namespace v2rayN.ViewModels
|
|||
private readonly PaletteHelper _paletteHelper = new();
|
||||
private Dictionary<string, bool> _dicHeaderSort = new();
|
||||
private Action<EViewAction> _updateView;
|
||||
private bool _showInTaskbar;
|
||||
|
||||
#endregion private prop
|
||||
|
||||
|
@ -568,7 +569,7 @@ namespace v2rayN.ViewModels
|
|||
|
||||
AutoHideStartup();
|
||||
|
||||
Global.ShowInTaskbar = true;
|
||||
_showInTaskbar = true;
|
||||
}
|
||||
|
||||
private void Init()
|
||||
|
@ -635,7 +636,7 @@ namespace v2rayN.ViewModels
|
|||
{
|
||||
Application.Current.Dispatcher.Invoke((Action)(() =>
|
||||
{
|
||||
if (!Global.ShowInTaskbar)
|
||||
if (!_showInTaskbar)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
@ -1183,7 +1184,7 @@ namespace v2rayN.ViewModels
|
|||
_noticeHandler?.SendMessage(msg, true);
|
||||
Application.Current.Dispatcher.Invoke((Action)(() =>
|
||||
{
|
||||
if (!Global.ShowInTaskbar)
|
||||
if (!_showInTaskbar)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
@ -1658,7 +1659,7 @@ namespace v2rayN.ViewModels
|
|||
|
||||
public void ShowHideWindow(bool? blShow)
|
||||
{
|
||||
var bl = blShow ?? !Global.ShowInTaskbar;
|
||||
var bl = blShow ?? !_showInTaskbar;
|
||||
if (bl)
|
||||
{
|
||||
//Application.Current.MainWindow.ShowInTaskbar = true;
|
||||
|
@ -1677,7 +1678,7 @@ namespace v2rayN.ViewModels
|
|||
//IntPtr windowHandle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
|
||||
//Utils.RegWriteValue(Global.MyRegPath, Utils.WindowHwndKey, Convert.ToString((long)windowHandle));
|
||||
}
|
||||
Global.ShowInTaskbar = bl;
|
||||
_showInTaskbar = bl;
|
||||
}
|
||||
|
||||
private void RestoreUI()
|
||||
|
|
Loading…
Reference in New Issue