diff --git a/src/ST.Client.Desktop.Mac/Services/Implementation/MacDesktopPlatformServiceImpl.cs b/src/ST.Client.Desktop.Mac/Services/Implementation/MacDesktopPlatformServiceImpl.cs index ab7611158..3002fee0f 100644 --- a/src/ST.Client.Desktop.Mac/Services/Implementation/MacDesktopPlatformServiceImpl.cs +++ b/src/ST.Client.Desktop.Mac/Services/Implementation/MacDesktopPlatformServiceImpl.cs @@ -7,6 +7,7 @@ using System.Application.Models; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; +using System.Threading.Tasks; using System.Windows; namespace System.Application.Services.Implementation @@ -21,17 +22,42 @@ namespace System.Application.Services.Implementation { return string.Empty; } - bool IPlatformService.AdminShell(string shell) + async Task IPlatformService.AdminShell(string shell) { + + var file = new FileInfo(Path.Combine(IOPath.AppDataDirectory, $@"sudoShell.sh")); + int index = 0; + if (file.Exists) + file.Delete(); + var vm = new UI.ViewModels.PasswordWindowViewModel(); + await IShowWindowService.Instance.ShowDialog(CustomWindow.Password, vm, string.Empty, ResizeModeCompat.CanResize); + var scriptContent = new Text.StringBuilder(); + scriptContent.AppendLine($"echo \"{vm.Password}\" | sudo -S {shell}"); + using (var stream = file.CreateText()) + { + stream.Write(scriptContent); + stream.Flush(); + stream.Dispose(); + } var pInfo = new ProcessStartInfo { - FileName = "open", - Arguments = $"{Path.Combine(IOPath.AppDataDirectory, $@"hello")} -t=1 -p=\"{shell}\"", + FileName = $"/bin/bash", + Arguments = file.FullName }; pInfo.UseShellExecute = true; + //pInfo.UseShellExecute = true; var p = Process.Start(pInfo); if (p == null) throw new FileNotFoundException("Shell"); p.Close(); + p.Exited += (object? _, EventArgs _) => + { + if (file.Exists) + file.Delete(); + if (p.ExitCode != 0) + IPlatformService.Instance.AdminShell(shell); + }; + if (file.Exists) + file.Delete(); return true; #if MONO_MAC //return false; diff --git a/src/ST.Client/Services/IPlatformService.cs b/src/ST.Client/Services/IPlatformService.cs index 5f78941f8..5f6423c26 100644 --- a/src/ST.Client/Services/IPlatformService.cs +++ b/src/ST.Client/Services/IPlatformService.cs @@ -1,3 +1,5 @@ +using System.Threading.Tasks; + namespace System.Application.Services { public interface IPlatformService @@ -6,6 +8,6 @@ namespace System.Application.Services public static IPlatformService Instance => DI.Get(); - bool AdminShell(string str) => throw new PlatformNotSupportedException(); + Task< bool> AdminShell(string str) => throw new PlatformNotSupportedException(); } } \ No newline at end of file diff --git a/src/ST.Client/Services/Implementation/HttpProxyServiceImpl.cs b/src/ST.Client/Services/Implementation/HttpProxyServiceImpl.cs index 4f29c7fee..9405d47d1 100644 --- a/src/ST.Client/Services/Implementation/HttpProxyServiceImpl.cs +++ b/src/ST.Client/Services/Implementation/HttpProxyServiceImpl.cs @@ -345,7 +345,8 @@ namespace System.Application.Services.Implementation proxyServer.CertificateManager.EnsureRootCertificate(); if (DI.IsmacOS) { - IPlatformService.Instance.AdminShell(filePath); + //安装证书 + IPlatformService.Instance.AdminShell($"security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain {filePath}"); } return IsCertificateInstalled(proxyServer.CertificateManager.RootCertificate); }