mirror of
https://github.com/2dust/v2rayN.git
synced 2025-04-19 21:52:25 +00:00
[macOS] feat: auto run. https://github.com/2dust/v2rayN/issues/6383
This commit is contained in:
parent
f71125d8f3
commit
8c157251e5
2 changed files with 81 additions and 10 deletions
|
@ -1,4 +1,5 @@
|
|||
using System.Security.Principal;
|
||||
using System.Diagnostics;
|
||||
using System.Security.Principal;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ServiceLib.Handler
|
||||
|
@ -29,7 +30,12 @@ namespace ServiceLib.Handler
|
|||
}
|
||||
else if (Utils.IsOSX())
|
||||
{
|
||||
//TODO
|
||||
await ClearTaskOSX();
|
||||
|
||||
if (config.GuiItem.AutoRun)
|
||||
{
|
||||
await SetTaskOSX();
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -161,5 +167,77 @@ namespace ServiceLib.Handler
|
|||
}
|
||||
|
||||
#endregion Linux
|
||||
|
||||
#region macOS
|
||||
|
||||
private static async Task ClearTaskOSX()
|
||||
{
|
||||
try
|
||||
{
|
||||
var launchAgentPath = GetLaunchAgentPathMacOS();
|
||||
if (File.Exists(launchAgentPath))
|
||||
{
|
||||
var args = new[] { "-c", $"launchctl unload -w \"{launchAgentPath}\"" };
|
||||
await Utils.GetCliWrapOutput("/bin/bash", args);
|
||||
|
||||
File.Delete(launchAgentPath);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task SetTaskOSX()
|
||||
{
|
||||
try
|
||||
{
|
||||
var plistContent = GenerateLaunchAgentPlist();
|
||||
var launchAgentPath = GetLaunchAgentPathMacOS();
|
||||
await File.WriteAllTextAsync(launchAgentPath, plistContent);
|
||||
|
||||
var args = new[] { "-c", $"launchctl load -w \"{launchAgentPath}\"" };
|
||||
await Utils.GetCliWrapOutput("/bin/bash", args);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.SaveLog(_tag, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetLaunchAgentPathMacOS()
|
||||
{
|
||||
var homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
||||
var launchAgentPath = Path.Combine(homePath, "Library", "LaunchAgents", $"{Global.AppName}-LaunchAgent.plist");
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(launchAgentPath));
|
||||
return launchAgentPath;
|
||||
}
|
||||
|
||||
private static string GenerateLaunchAgentPlist()
|
||||
{
|
||||
var exePath = Utils.GetExePath();
|
||||
var appName = Path.GetFileNameWithoutExtension(exePath);
|
||||
return $@"<?xml version=""1.0"" encoding=""UTF-8""?>
|
||||
<!DOCTYPE plist PUBLIC ""-//Apple//DTD PLIST 1.0//EN"" ""http://www.apple.com/DTDs/PropertyList-1.0.dtd"">
|
||||
<plist version=""1.0"">
|
||||
<dict>
|
||||
<key>Label</key>
|
||||
<string>{Global.AppName}-LaunchAgent</string>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>/bin/sh</string>
|
||||
<string>-c</string>
|
||||
<string>if ! pgrep -x ""{appName}"" > /dev/null; then ""{exePath}""; fi</string>
|
||||
</array>
|
||||
<key>RunAtLoad</key>
|
||||
<true/>
|
||||
<key>KeepAlive</key>
|
||||
<false/>
|
||||
</dict>
|
||||
</plist>";
|
||||
}
|
||||
|
||||
#endregion macOS
|
||||
}
|
||||
}
|
|
@ -176,13 +176,6 @@ namespace v2rayN.Desktop.Views
|
|||
txbSettingsExceptionTip.IsVisible = false;
|
||||
panSystemProxyAdvanced.IsVisible = false;
|
||||
}
|
||||
|
||||
if (Utils.IsOSX())
|
||||
{
|
||||
tbAutoRun.IsVisible = false;
|
||||
togAutoRun.IsVisible = false;
|
||||
//TODO
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
|
||||
|
|
Loading…
Reference in a new issue