Skip to content

Commit

Permalink
macOS user friendly tweaks (#35)
Browse files Browse the repository at this point in the history
* Upgrade to .net8

* Cleanup Path seeker

* Load correct locale for macOS

* make macOS permission less confusing
  • Loading branch information
fakeboboliu authored Nov 22, 2023
1 parent c43161a commit 6b1b8b4
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 57 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
runtime: [linux-x64]
include:
- runtime: linux-x64
artifact_path: ./bin/Gtk/Release/net6.0/linux-x64
artifact_path: ./bin/Gtk/Release/net8.0/linux-x64
self_contained: "--self-contained"

runs-on: ubuntu-latest
Expand All @@ -38,11 +38,11 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 8

- name: Build for ${{ matrix.runtime }}
run: |
dotnet build OpenTrace.csproj --runtime ${{ matrix.runtime }} --configuration Release ${{ matrix.self_contained }} -f net6.0
dotnet build OpenTrace.csproj --runtime ${{ matrix.runtime }} --configuration Release ${{ matrix.self_contained }} -f net8.0
- name: Make tarball
run: cd ${{ matrix.artifact_path }} && chmod +x ./OpenTrace && tar -cvzf ../${{ matrix.runtime }}.tar.gz ./
Expand All @@ -52,5 +52,5 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.runtime }}.tar.gz
path: ./bin/Gtk/Release/net6.0/${{ matrix.runtime }}.tar.gz
path: ./bin/Gtk/Release/net8.0/${{ matrix.runtime }}.tar.gz

8 changes: 4 additions & 4 deletions .github/workflows/build-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 8

- name: Build for ${{ matrix.runtime }}
run: |
dotnet build OpenTrace.csproj --runtime ${{ matrix.runtime }} --configuration Release --self-contained -f net6.0
dotnet build OpenTrace.csproj --runtime ${{ matrix.runtime }} --configuration Release --self-contained -f net8.0
- name: Make tarball
run: cd ./bin/Mac64/Release/net6.0/${{ matrix.runtime }} && tar -cvzf OpenTrace_${{ matrix.runtime }}.app.tar.gz OpenTrace.app
run: cd ./bin/Mac64/Release/net8.0/${{ matrix.runtime }} && tar -cvzf OpenTrace_${{ matrix.runtime }}.app.tar.gz OpenTrace.app

- name: Upload artifact for ${{ matrix.runtime }}
uses: actions/upload-artifact@v3
with:
name: OpenTrace_${{ matrix.runtime }}.app.tar.gz
path: ./bin/Mac64/Release/net6.0/${{ matrix.runtime }}/OpenTrace_${{ matrix.runtime }}.app.tar.gz
path: ./bin/Mac64/Release/net8.0/${{ matrix.runtime }}/OpenTrace_${{ matrix.runtime }}.app.tar.gz
10 changes: 5 additions & 5 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ jobs:
archive_command: "zip -r"
archive_extension: zip
- runtime: linux-x64
artifact_path: ./bin/Gtk/Release/net6.0/linux-x64
artifact_path: ./bin/Gtk/Release/net8.0/linux-x64
self_contained: "--self-contained"
framework: net6.0
framework: net8.0
nt_file: nexttrace_linux_amd64
nt_fn: nexttrace
archive_command: "tar -cvzf"
archive_extension: tar.gz
- runtime: osx-x64
artifact_path: ./bin/Mac64/Release/net6.0/osx-x64
artifact_path: ./bin/Mac64/Release/net8.0/osx-x64
self_contained: "--self-contained"
framework: net6.0
framework: net8.0
nt_file: nexttrace_darwin_amd64
nt_fn: OpenTrace.app/Contents/MacOS/nexttrace
pack_target: OpenTrace.app
Expand All @@ -49,7 +49,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6
dotnet-version: 8

- name: Build
run: dotnet build OpenTrace.csproj --runtime ${{ matrix.runtime }} --configuration Release ${{ matrix.self_contained }} -f ${{ matrix.framework }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 8

- name: Build for ${{ matrix.runtime }}
run: |
Expand Down
19 changes: 17 additions & 2 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ public MainForm()
ResetMap();
};

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && UserSettings.hideAddICMPFirewallRule != true) tryAddICMPFirewallRule();

platformChecks();
// 绑定窗口事件
SizeChanged += MainForm_SizeChanged;
MouseDown += Dragging_MouseDown;
Expand Down Expand Up @@ -261,6 +261,21 @@ private void LoadDNSResolvers()
dnsResolverSelection.SelectedIndex = 0;
}

// 初始化期间进行平台特定检查
private void platformChecks()
{

// macOS 被隔离,请求释放
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && AppDomain.CurrentDomain.BaseDirectory.StartsWith("/private/var/folders"))
{
App.app.Invoke(() => {
MessageBox.Show(Resources.MACOS_QUARANTINE);
});
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && UserSettings.hideAddICMPFirewallRule != true) tryAddICMPFirewallRule();
}

private void tryAddICMPFirewallRule()
{
// 提示 Windows 用户添加防火墙规则放行 ICMP
Expand Down
112 changes: 76 additions & 36 deletions NextTraceWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,37 @@ internal class NextTraceWrapper
public event EventHandler<AppQuitEventArgs> AppQuit;
public event EventHandler<ExceptionalOutputEventArgs> ExceptionalOutput;
private string nexttracePath;
private bool builtinNT = false;

Check warning on line 49 in NextTraceWrapper.cs

View workflow job for this annotation

GitHub Actions / build (win-x64)

The field 'NextTraceWrapper.builtinNT' is assigned but its value is never used

Check warning on line 49 in NextTraceWrapper.cs

View workflow job for this annotation

GitHub Actions / build (win-x64)

The field 'NextTraceWrapper.builtinNT' is assigned but its value is never used
private int errorOutputCount = 0;
public ObservableCollection<TracerouteResult> Output { get; } = new ObservableCollection<TracerouteResult>();

public NextTraceWrapper()
{
string curDir = AppDomain.CurrentDomain.BaseDirectory;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// 检查 Windows 平台可执行文件
List<string> winBinaryList = new List<string> { "nexttrace.exe", "nexttrace_windows_amd64.exe", "nexttrace_windows_arm64.exe", "nexttrace_windows_armv7.exe", "nexttrace_windows_386.exe" };
foreach (string winBinaryName in winBinaryList)
{
if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, winBinaryName)))
if (File.Exists(Path.Combine(curDir, winBinaryName)))
{
// 先检查根目录
nexttracePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, winBinaryName);
nexttracePath = Path.Combine(curDir, winBinaryName);
break;
} else {
// 再检查PATH变量
string pathVar = Environment.GetEnvironmentVariable("PATH");
string[] pathDirs = pathVar.Split(Path.PathSeparator);
foreach (string pathDir in pathDirs)
}
// 再检查PATH变量
string pathVar = Environment.GetEnvironmentVariable("PATH");
string[] pathDirs = pathVar.Split(Path.PathSeparator);
foreach (string pathDir in pathDirs)
{
if (File.Exists(Path.Combine(pathDir, winBinaryName)))
{
if (File.Exists(Path.Combine(pathDir, winBinaryName)))
{
nexttracePath = Path.Combine(pathDir, winBinaryName);
break;
}
nexttracePath = Path.Combine(pathDir, winBinaryName);
break;
}
if (nexttracePath != null) break;
}
if (nexttracePath != null) break;
}
}
else
Expand All @@ -84,35 +85,33 @@ public NextTraceWrapper()
List<string> otherBinaryList = new List<string> { "nexttrace", "nexttrace_android_arm64", "nexttrace_darwin_amd64", "nexttrace_darwin_arm64", "nexttrace_dragonfly_amd64", "nexttrace_freebsd_386", "nexttrace_freebsd_amd64", "nexttrace_freebsd_arm64", "nexttrace_freebsd_armv7", "nexttrace_linux_386", "nexttrace_linux_amd64", "nexttrace_linux_arm64", "nexttrace_linux_armv5", "nexttrace_linux_armv6", "nexttrace_linux_armv7", "nexttrace_linux_mips", "nexttrace_linux_mips64", "nexttrace_linux_mips64le", "nexttrace_linux_mipsle", "nexttrace_linux_ppc64", "nexttrace_linux_ppc64le", "nexttrace_linux_riscv64", "nexttrace_linux_s390x", "nexttrace_openbsd_386", "nexttrace_openbsd_amd64", "nexttrace_openbsd_arm64", "nexttrace_openbsd_armv7" };
foreach (string otherBinaryName in otherBinaryList)
{
if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, otherBinaryName)))
if (File.Exists(Path.Combine(curDir, "OpenTrace.app/Contents/MacOS", otherBinaryName)))
{
// 先检查根目录
nexttracePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, otherBinaryName);
nexttracePath = Path.Combine(curDir, "OpenTrace.app/Contents/MacOS", otherBinaryName);
builtinNT = true;
break;
}
else
if (File.Exists(Path.Combine(curDir, otherBinaryName)))
{
// 再检查PATH变量
string pathVar = Environment.GetEnvironmentVariable("PATH");
string[] pathDirs = pathVar.Split(Path.PathSeparator);
foreach (string pathDir in pathDirs)
{
if (File.Exists(Path.Combine(pathDir, otherBinaryName)))
{
nexttracePath = Path.Combine(pathDir, otherBinaryName);
break;
}
}
if (nexttracePath != null) break;
else if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "OpenTrace.app/Contents/MacOS", otherBinaryName)))
nexttracePath = Path.Combine(curDir, otherBinaryName);
builtinNT = true;
break;
}

string pathVar = Environment.GetEnvironmentVariable("PATH");
string[] pathDirs = pathVar.Split(Path.PathSeparator);
foreach (string pathDir in pathDirs)
{
if (File.Exists(Path.Combine(pathDir, otherBinaryName)))
{
nexttracePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "OpenTrace.app/Contents/MacOS", otherBinaryName);
nexttracePath = Path.Combine(pathDir, otherBinaryName);
break;
}
}
if (nexttracePath != null) break;
}
}

// 检查是否手动指定了可执行文件
if (UserSettings.executablePath != "")
{
Expand All @@ -132,10 +131,11 @@ public NextTraceWrapper()
}
}

public void Run(string host,bool MTRMode, params string[] extraArgs)
public void Run(string host, bool MTRMode, params string[] extraArgs)
{
Task.Run(() =>
{
Console.WriteLine($"Using NextTrace: {nexttracePath}");
string arguments;
if (MTRMode)
{
Expand All @@ -145,6 +145,45 @@ public void Run(string host,bool MTRMode, params string[] extraArgs)
{
arguments = ArgumentBuilder(host, extraArgs);
}
#if NET8_0_OR_GREATER
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) &&
Array.Find(extraArgs, e => e == "-T" || e == "-U") != null &&
Environment.UserName != "root")
{
FileSystemInfo fa = new FileInfo(nexttracePath);
if ((fa.UnixFileMode & UnixFileMode.SetUser) == 0)
{
if (!builtinNT)
App.app.Invoke(() => {
Eto.Forms.MessageBox.Show(Resources.MISSING_COMP_PRIV_MACOS);
});
else
{
var elvp = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "/usr/bin/osascript",
ArgumentList = {
"-e",
$"do shell script \"chown root:admin '{nexttracePath}' && chmod +sx '{nexttracePath}'\" with administrator privileges with prompt \"{Resources.MISSING_PRIV_MACOS}\"",
},
UseShellExecute = false,
RedirectStandardOutput = false,
RedirectStandardError = false,
CreateNoWindow = true
}
};
elvp.Start();
elvp.WaitForExit();
}
Status = AppStatus.Quit;
AppQuit?.Invoke(this, new AppQuitEventArgs(0));
return;
}
}
#endif
_process = new Process
{
StartInfo = new ProcessStartInfo
Expand All @@ -158,6 +197,7 @@ public void Run(string host,bool MTRMode, params string[] extraArgs)
CreateNoWindow = true
}
};
if (UserSettings.IPInsightToken != "") _process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_IPINSIGHT_TOKEN", UserSettings.IPInsightToken);
if (UserSettings.IPInfoToken != "") _process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_IPINFO_TOKEN", UserSettings.IPInfoToken);
if (UserSettings.ChunZhenEndpoint != "") _process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_CHUNZHENURL", UserSettings.ChunZhenEndpoint);
Expand All @@ -167,7 +207,7 @@ public void Run(string host,bool MTRMode, params string[] extraArgs)
if (MTRMode) // 添加环境变量让NextTrace进入持续追踪模式
_process.StartInfo.EnvironmentVariables.Add("NEXTTRACE_UNINTERRUPTED", "1");
Regex match1stLine = new Regex(@"^\d{1,2}\|");
_process.OutputDataReceived += (sender, e) =>
{
Expand Down Expand Up @@ -295,9 +335,9 @@ private string ArgumentBuilder(string host, string[] extraArgs, string[] ignoreU
UserSettings userSettings = new UserSettings();
foreach (var setting in userSettings.GetType().GetProperties())
{
if(checkArgsFromConfList.Contains(setting.Name) && (ignoreUserArgs == null || !ignoreUserArgs.Contains(setting.Name)))
if (checkArgsFromConfList.Contains(setting.Name) && (ignoreUserArgs == null || !ignoreUserArgs.Contains(setting.Name)))
{
if((string)setting.GetValue(userSettings, null) != "")
if ((string)setting.GetValue(userSettings, null) != "")
finalArgs.Add("--" + setting.Name.Replace('_', '-') + " " + (string)setting.GetValue(userSettings, null));
}
}
Expand Down
2 changes: 1 addition & 1 deletion OpenTrace.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
-->

<PropertyGroup>
<TargetFrameworks>;net48;net6.0</TargetFrameworks>
<TargetFrameworks>;net48;net8.0</TargetFrameworks>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<OutputType>Exe</OutputType>
<PackageProjectUrl>https://github.com/Archeb/opentrace</PackageProjectUrl>
Expand Down
Loading

0 comments on commit 6b1b8b4

Please sign in to comment.