Skip to content

Commit

Permalink
[macOS] feat: auto run. #6383 (#6597)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaRinne authored Jan 30, 2025
1 parent 6c5011a commit b2feaf3
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
84 changes: 81 additions & 3 deletions v2rayN/ServiceLib/Handler/AutoStartupHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Principal;
using System.Diagnostics;
using System.Security.Principal;
using System.Text.RegularExpressions;

namespace ServiceLib.Handler
Expand Down Expand Up @@ -29,7 +30,12 @@ public static async Task<bool> UpdateTask(Config config)
}
else if (Utils.IsOSX())
{
//TODO
await ClearTaskOSX();

if (config.GuiItem.AutoRun)
{
await SetTaskOSX();
}
}

return true;
Expand Down Expand Up @@ -161,5 +167,77 @@ private static string GetHomePathLinux()
}

#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
}
}
}
7 changes: 0 additions & 7 deletions v2rayN/v2rayN.Desktop/Views/OptionSettingWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,6 @@ public OptionSettingWindow()
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)
Expand Down

0 comments on commit b2feaf3

Please sign in to comment.