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

View file

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

View file

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

View file

@ -66,10 +66,10 @@
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)
{
strExceptions = $"<local>;{config.ConstItem.DefIEProxyExceptions};{config.SystemProxyItem.SystemProxyExceptions}";
strExceptions = $"<local>;{strExceptions}";
}
strProxy = string.Empty;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -26,7 +26,7 @@ namespace ServiceLib.Services.CoreConfig
ret.Msg = ResUI.CheckServerSettings;
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()}";
return ret;
@ -548,7 +548,7 @@ namespace ServiceLib.Services.CoreConfig
}
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.strict_route = _config.TunModeItem.StrictRoute;
tunInbound.stack = _config.TunModeItem.Stack;

View file

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

View file

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

View file

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

View file

@ -282,7 +282,6 @@ namespace ServiceLib.ViewModels
try
{
Logging.SaveLog("MyAppExit Begin");
//if (blWindowsShutDown)
await SysProxyHandler.UpdateSysProxy(_config, true);
await ConfigHandler.SaveConfig(_config);
@ -296,7 +295,10 @@ namespace ServiceLib.ViewModels
catch { }
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)
{
await AutoStartupHandler.UpdateTask(_config);
AppHandler.Instance.Reset();
NoticeHandler.Instance.Enqueue(needReboot ? ResUI.NeedRebootTips : ResUI.OperationSuccess);
_updateView?.Invoke(EViewAction.CloseWindow, null);
@ -405,4 +406,4 @@ namespace ServiceLib.ViewModels
}
}
}
}
}

View file

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