This commit is contained in:
fonaix 2024-12-01 00:00:44 +08:00
commit 394ca09732
17 changed files with 71 additions and 64 deletions

View file

@ -27,7 +27,7 @@
get get
{ {
_statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2)); _statePort2 ??= Utils.GetFreePort(GetLocalPort(EInboundProtocol.api2));
return _statePort2.Value; return _statePort2.Value + (_config.TunModeItem.EnableTun ? 1 : 0);
} }
} }
@ -79,6 +79,13 @@
return true; return true;
} }
public bool Reset()
{
_statePort = null;
_statePort2 = null;
return true;
}
#endregion Init #endregion Init
#region Config #region Config

View file

@ -25,7 +25,7 @@ namespace ServiceLib.Handler
await SetTaskLinux(); await SetTaskLinux();
} }
} }
else if(Utils.IsOSX()) else if (Utils.IsOSX())
{ {
//TODO //TODO
} }

View file

@ -97,16 +97,12 @@ namespace ServiceLib.Handler
{ {
if (_process != null) if (_process != null)
{ {
await KillProcess(_process); _process = await KillProcess(_process);
_process.Dispose();
_process = null;
} }
if (_processPre != null) if (_processPre != null)
{ {
await KillProcess(_processPre); _processPre = await KillProcess(_processPre);
_processPre.Dispose();
_processPre = null;
} }
if (_linuxSudoPid > 0) if (_linuxSudoPid > 0)
@ -125,8 +121,7 @@ namespace ServiceLib.Handler
{ {
try try
{ {
var _p = Process.GetProcessById(pid); await KillProcess(Process.GetProcessById(pid));
await KillProcess(_p);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -326,32 +321,18 @@ namespace ServiceLib.Handler
} }
} }
private async Task KillProcess(Process? proc) private async Task<Process?> KillProcess(Process? proc)
{ {
if (proc is null) if (proc is null)
{ {
return; return null;
}
try
{
proc?.Kill();
}
catch (Exception)
{
// ignored
} }
try { proc?.Kill(true); } catch { }
try { proc?.Close(); } catch { }
try { proc?.Dispose(); } catch { }
await Task.Delay(100); await Task.Delay(100);
if (proc?.HasExited == false) return null;
{
try
{
proc?.Kill();
}
catch (Exception)
{
// ignored
}
}
} }
#endregion Process #endregion Process
@ -375,7 +356,7 @@ namespace ServiceLib.Handler
private async Task KillProcessAsLinuxSudo() private async Task KillProcessAsLinuxSudo()
{ {
var cmdLine = $"kill -9 {_linuxSudoPid}"; var cmdLine = $"kill {_linuxSudoPid}";
var shFilePath = await CreateLinuxShellFile(cmdLine, "kill_as_sudo.sh"); var shFilePath = await CreateLinuxShellFile(cmdLine, "kill_as_sudo.sh");
Process proc = new() Process proc = new()
{ {
@ -392,26 +373,37 @@ namespace ServiceLib.Handler
if (_config.TunModeItem.LinuxSudoPwd.IsNotEmpty()) if (_config.TunModeItem.LinuxSudoPwd.IsNotEmpty())
{ {
var pwd = DesUtils.Decrypt(_config.TunModeItem.LinuxSudoPwd); try
await Task.Delay(10); {
await proc.StandardInput.WriteLineAsync(pwd); var pwd = DesUtils.Decrypt(_config.TunModeItem.LinuxSudoPwd);
await Task.Delay(10); await Task.Delay(10);
await proc.StandardInput.WriteLineAsync(pwd); await proc.StandardInput.WriteLineAsync(pwd);
await Task.Delay(10);
await proc.StandardInput.WriteLineAsync(pwd);
}
catch (Exception)
{
// ignored
}
} }
var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10)); var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(10));
await proc.WaitForExitAsync(timeout.Token); await proc.WaitForExitAsync(timeout.Token);
await Task.Delay(1000); await Task.Delay(3000);
} }
private async Task<string> CreateLinuxShellFile(string cmdLine, string fileName) private async Task<string> CreateLinuxShellFile(string cmdLine, string fileName)
{ {
//Shell scripts //Shell scripts
var shFilePath = Utils.GetBinPath(fileName); var shFilePath = Utils.GetBinPath(AppHandler.Instance.IsAdministrator ? "root_" + fileName : fileName);
File.Delete(shFilePath); File.Delete(shFilePath);
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendLine("#!/bin/sh"); sb.AppendLine("#!/bin/sh");
if (_config.TunModeItem.LinuxSudoPwd.IsNullOrEmpty()) if (AppHandler.Instance.IsAdministrator)
{
sb.AppendLine($"{cmdLine}");
}
else if (_config.TunModeItem.LinuxSudoPwd.IsNullOrEmpty())
{ {
sb.AppendLine($"pkexec {cmdLine}"); sb.AppendLine($"pkexec {cmdLine}");
} }

View file

@ -66,10 +66,10 @@
private static void GetWindowsProxyString(Config config, int port, int portSocks, out string strProxy, out string strExceptions) private static void GetWindowsProxyString(Config config, int port, int portSocks, out string strProxy, out string strExceptions)
{ {
strExceptions = ""; strExceptions = $"{config.ConstItem.DefIEProxyExceptions};{config.SystemProxyItem.SystemProxyExceptions}";
if (config.SystemProxyItem.NotProxyLocalAddress) if (config.SystemProxyItem.NotProxyLocalAddress)
{ {
strExceptions = $"<local>;{config.ConstItem.DefIEProxyExceptions};{config.SystemProxyItem.SystemProxyExceptions}"; strExceptions = $"<local>;{strExceptions}";
} }
strProxy = string.Empty; strProxy = string.Empty;

View file

@ -3140,7 +3140,7 @@ namespace ServiceLib.Resx {
} }
/// <summary> /// <summary>
/// 查找类似 Linux system sudo password 的本地化字符串。 /// 查找类似 System sudo password 的本地化字符串。
/// </summary> /// </summary>
public static string TbSettingsLinuxSudoPassword { public static string TbSettingsLinuxSudoPassword {
get { get {

View file

@ -1364,7 +1364,7 @@
<value>(Domain or IP or ProcName) and Port and Protocol and InboundTag =&gt; OutboundTag</value> <value>(Domain or IP or ProcName) and Port and Protocol and InboundTag =&gt; OutboundTag</value>
</data> </data>
<data name="TbSettingsLinuxSudoPassword" xml:space="preserve"> <data name="TbSettingsLinuxSudoPassword" xml:space="preserve">
<value>Linux system sudo password</value> <value>System sudo password</value>
</data> </data>
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve"> <data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
<value>The password is encrypted and stored only in local files.</value> <value>The password is encrypted and stored only in local files.</value>

View file

@ -1364,7 +1364,7 @@
<value>Remarks Memo</value> <value>Remarks Memo</value>
</data> </data>
<data name="TbSettingsLinuxSudoPassword" xml:space="preserve"> <data name="TbSettingsLinuxSudoPassword" xml:space="preserve">
<value>Linux system sudo password</value> <value>System sudo password</value>
</data> </data>
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve"> <data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
<value>The password is encrypted and stored only in local files.</value> <value>The password is encrypted and stored only in local files.</value>

View file

@ -1364,7 +1364,7 @@
<value>Remote (WebDAV)</value> <value>Remote (WebDAV)</value>
</data> </data>
<data name="TbSettingsLinuxSudoPassword" xml:space="preserve"> <data name="TbSettingsLinuxSudoPassword" xml:space="preserve">
<value>Linux system sudo password</value> <value>System sudo password</value>
</data> </data>
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve"> <data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
<value>The password is encrypted and stored only in local files.</value> <value>The password is encrypted and stored only in local files.</value>

View file

@ -1361,7 +1361,7 @@
<value>备注备忘</value> <value>备注备忘</value>
</data> </data>
<data name="TbSettingsLinuxSudoPassword" xml:space="preserve"> <data name="TbSettingsLinuxSudoPassword" xml:space="preserve">
<value>Linux系统的sudo密码</value> <value>系统的sudo密码</value>
</data> </data>
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve"> <data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
<value>密码已加密且只存储在本地文件中,无密码则每次都要输入</value> <value>密码已加密且只存储在本地文件中,无密码则每次都要输入</value>

View file

@ -1361,7 +1361,7 @@
<value>混淆密碼(obfs password)</value> <value>混淆密碼(obfs password)</value>
</data> </data>
<data name="TbSettingsLinuxSudoPassword" xml:space="preserve"> <data name="TbSettingsLinuxSudoPassword" xml:space="preserve">
<value>Linux系統的sudo密碼</value> <value>系統的sudo密碼</value>
</data> </data>
<data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve"> <data name="TbSettingsLinuxSudoPasswordTip" xml:space="preserve">
<value>密碼已加密且只儲存在本機檔案中,無密碼則每次都要輸入</value> <value>密碼已加密且只儲存在本機檔案中,無密碼則每次都要輸入</value>

View file

@ -26,7 +26,7 @@ namespace ServiceLib.Services.CoreConfig
ret.Msg = ResUI.CheckServerSettings; ret.Msg = ResUI.CheckServerSettings;
return ret; return ret;
} }
if (node.GetNetwork() is nameof(ETransport.kcp) or nameof(ETransport.xhttp)) if (node.GetNetwork() is nameof(ETransport.kcp) or nameof(ETransport.xhttp))
{ {
ret.Msg = ResUI.Incorrectconfiguration + $" - {node.GetNetwork()}"; ret.Msg = ResUI.Incorrectconfiguration + $" - {node.GetNetwork()}";
return ret; return ret;
@ -548,7 +548,7 @@ namespace ServiceLib.Services.CoreConfig
} }
var tunInbound = JsonUtils.Deserialize<Inbound4Sbox>(Utils.GetEmbedText(Global.TunSingboxInboundFileName)) ?? new Inbound4Sbox { }; var tunInbound = JsonUtils.Deserialize<Inbound4Sbox>(Utils.GetEmbedText(Global.TunSingboxInboundFileName)) ?? new Inbound4Sbox { };
tunInbound.interface_name = Utils.IsOSX()? $"utun{new Random().Next(99)}": "singbox_tun"; tunInbound.interface_name = Utils.IsOSX() ? $"utun{new Random().Next(99)}" : "singbox_tun";
tunInbound.mtu = _config.TunModeItem.Mtu; tunInbound.mtu = _config.TunModeItem.Mtu;
tunInbound.strict_route = _config.TunModeItem.StrictRoute; tunInbound.strict_route = _config.TunModeItem.StrictRoute;
tunInbound.stack = _config.TunModeItem.Stack; tunInbound.stack = _config.TunModeItem.Stack;

View file

@ -185,7 +185,7 @@ namespace ServiceLib.Services
{ {
if (pid > 0) if (pid > 0)
{ {
CoreHandler.Instance.CoreStopPid(pid); await CoreHandler.Instance.CoreStopPid(pid);
} }
await ProfileExHandler.Instance.SaveTo(); await ProfileExHandler.Instance.SaveTo();
} }
@ -252,7 +252,7 @@ namespace ServiceLib.Services
if (pid > 0) if (pid > 0)
{ {
CoreHandler.Instance.CoreStopPid(pid); await CoreHandler.Instance.CoreStopPid(pid);
} }
UpdateFunc("", ResUI.SpeedtestingCompleted); UpdateFunc("", ResUI.SpeedtestingCompleted);
await ProfileExHandler.Instance.SaveTo(); await ProfileExHandler.Instance.SaveTo();
@ -317,7 +317,7 @@ namespace ServiceLib.Services
if (pid > 0) if (pid > 0)
{ {
CoreHandler.Instance.CoreStopPid(pid); await CoreHandler.Instance.CoreStopPid(pid);
} }
UpdateFunc("", ResUI.SpeedtestingCompleted); UpdateFunc("", ResUI.SpeedtestingCompleted);
await ProfileExHandler.Instance.SaveTo(); await ProfileExHandler.Instance.SaveTo();

View file

@ -8,8 +8,8 @@ namespace ServiceLib.Services.Statistics
private Config _config; private Config _config;
private bool _exitFlag; private bool _exitFlag;
private ClientWebSocket? webSocket; private ClientWebSocket? webSocket;
private string url = string.Empty;
private Action<ServerSpeedItem>? _updateFunc; private Action<ServerSpeedItem>? _updateFunc;
private string Url => $"ws://{Global.Loopback}:{AppHandler.Instance.StatePort2}/traffic";
public StatisticsSingboxService(Config config, Action<ServerSpeedItem> updateFunc) public StatisticsSingboxService(Config config, Action<ServerSpeedItem> updateFunc)
{ {
@ -26,12 +26,10 @@ namespace ServiceLib.Services.Statistics
try try
{ {
url = $"ws://{Global.Loopback}:{AppHandler.Instance.StatePort2}/traffic";
if (webSocket == null) if (webSocket == null)
{ {
webSocket = new ClientWebSocket(); webSocket = new ClientWebSocket();
await webSocket.ConnectAsync(new Uri(url), CancellationToken.None); await webSocket.ConnectAsync(new Uri(Url), CancellationToken.None);
} }
} }
catch { } catch { }

View file

@ -3,18 +3,17 @@
public class StatisticsXrayService public class StatisticsXrayService
{ {
private const long linkBase = 1024; private const long linkBase = 1024;
private string _url;
private ServerSpeedItem _serverSpeedItem = new(); private ServerSpeedItem _serverSpeedItem = new();
private Config _config; private Config _config;
private bool _exitFlag; private bool _exitFlag;
private Action<ServerSpeedItem>? _updateFunc; private Action<ServerSpeedItem>? _updateFunc;
private string Url => $"{Global.HttpProtocol}{Global.Loopback}:{AppHandler.Instance.StatePort}/debug/vars";
public StatisticsXrayService(Config config, Action<ServerSpeedItem> updateFunc) public StatisticsXrayService(Config config, Action<ServerSpeedItem> updateFunc)
{ {
_config = config; _config = config;
_updateFunc = updateFunc; _updateFunc = updateFunc;
_exitFlag = false; _exitFlag = false;
_url = $"{Global.HttpProtocol}{Global.Loopback}:{AppHandler.Instance.StatePort}/debug/vars";
Task.Run(Run); Task.Run(Run);
} }
@ -36,7 +35,7 @@
continue; continue;
} }
var result = await HttpClientHelper.Instance.TryGetAsync(_url); var result = await HttpClientHelper.Instance.TryGetAsync(Url);
if (result != null) if (result != null)
{ {
var server = ParseOutput(result) ?? new ServerSpeedItem(); var server = ParseOutput(result) ?? new ServerSpeedItem();

View file

@ -282,7 +282,6 @@ namespace ServiceLib.ViewModels
try try
{ {
Logging.SaveLog("MyAppExit Begin"); Logging.SaveLog("MyAppExit Begin");
//if (blWindowsShutDown)
await SysProxyHandler.UpdateSysProxy(_config, true); await SysProxyHandler.UpdateSysProxy(_config, true);
await ConfigHandler.SaveConfig(_config); await ConfigHandler.SaveConfig(_config);
@ -296,7 +295,10 @@ namespace ServiceLib.ViewModels
catch { } catch { }
finally finally
{ {
_updateView?.Invoke(EViewAction.Shutdown, null); if (!blWindowsShutDown)
{
_updateView?.Invoke(EViewAction.Shutdown, null);
}
} }
} }

View file

@ -356,6 +356,7 @@ namespace ServiceLib.ViewModels
if (await ConfigHandler.SaveConfig(_config) == 0) if (await ConfigHandler.SaveConfig(_config) == 0)
{ {
await AutoStartupHandler.UpdateTask(_config); await AutoStartupHandler.UpdateTask(_config);
AppHandler.Instance.Reset();
NoticeHandler.Instance.Enqueue(needReboot ? ResUI.NeedRebootTips : ResUI.OperationSuccess); NoticeHandler.Instance.Enqueue(needReboot ? ResUI.NeedRebootTips : ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow, null); _updateView?.Invoke(EViewAction.CloseWindow, null);

View file

@ -21,6 +21,7 @@ namespace v2rayN.Desktop.Views
private WindowNotificationManager? _manager; private WindowNotificationManager? _manager;
private CheckUpdateView? _checkUpdateView; private CheckUpdateView? _checkUpdateView;
private BackupAndRestoreView? _backupAndRestoreView; private BackupAndRestoreView? _backupAndRestoreView;
private bool _blCloseByUser = false;
public MainWindow() public MainWindow()
{ {
@ -284,6 +285,11 @@ namespace v2rayN.Desktop.Views
protected override async void OnClosing(WindowClosingEventArgs e) protected override async void OnClosing(WindowClosingEventArgs e)
{ {
if (_blCloseByUser)
{
return;
}
Logging.SaveLog("OnClosing -> " + e.CloseReason.ToString()); Logging.SaveLog("OnClosing -> " + e.CloseReason.ToString());
switch (e.CloseReason) switch (e.CloseReason)
@ -379,6 +385,8 @@ namespace v2rayN.Desktop.Views
{ {
return; return;
} }
_blCloseByUser = true;
StorageUI(); StorageUI();
await ViewModel?.MyAppExitAsync(false); await ViewModel?.MyAppExitAsync(false);