Routing: process supports macOS as well#6447
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a macOS (Darwin) implementation of common/net.FindProcess so process-based routing rules can resolve PID/name/path on macOS without cgo or external commands.
Changes:
- Excludes
darwinfrom the generic “unsupported platform”FindProcessimplementation. - Introduces a Darwin
FindProcessimplementation using libproc (proc_pidinfo,proc_pidfdinfo,proc_pidpath) and socket endpoint matching/fallback levels. - Adds Darwin-only unit tests and per-arch assembly trampolines (amd64/arm64) to call libproc dynamically.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| common/net/find_process_others.go | Updates build tags so Darwin uses its dedicated implementation. |
| common/net/find_process_darwin.go | Implements Darwin process lookup via libproc and socket table matching. |
| common/net/find_process_darwin_test.go | Adds Darwin-only tests for TCP/UDP, non-local, and fallback matching behavior. |
| common/net/find_process_darwin_arm64.s | Adds arm64 trampoline symbols for libproc dynamic imports. |
| common/net/find_process_darwin_amd64.s | Adds amd64 trampoline symbols for libproc dynamic imports. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@yiguodev ready 了吗 |
|
Yes |
|
|
|
Fixed |
|
Darwin 就编译这俩架构应该不用加判断吧,话说 go 原生 syscall 没有那些能力吗 |
|
|
|
已简化为 Go 当前 Darwin 目标只有
|
|
上面这些就是 AI 直接回的。 |
|
|
|
是的,当前约束下基本只能这样做。 这里要区分两个东西:
目前
这个 PR 选的是第 3 种,和 |
|
.s 的必要性倒是没啥异议了,毕竟这是 c 函数而不是 number,需要动态获取 API 内存地址
|
|
Linux 这边不是同一类问题。 当前 Linux 实现是:
Darwin 这次用 trampoline 是因为需要调用 Linux 后续如果要优化,方向应该更像是单独评估 netlink/sock_diag、procfs 扫描缓存或 inode->pid 查找策略,而不是用 Darwin 这套动态导入方案。这个可以单独开 PR 处理,不建议混在这次 macOS 支持里。 |
|
我的意思是 Linux 上有没有系统函数提供了类似的功能,可以 syscall 调用,就不需要现在这种比较普通的方法了 |
|
Linux 没有一个等价于 “按 socket tuple 直接返回 PID/进程路径” 的系统调用。 能考虑的内核接口主要是 所以 Linux 上最多可以优化第一步:
但第二步 inode -> PID 仍绕不开 因此 Linux 可以单独研究 |
|
|
|
这是在和agent聊天吗 |
|
|
|
@Fangliding @yiguodev Linux 上的进程路由似乎有问题 2dust/v2rayN#9715 (comment) |
|
tun入站似乎不兼容 之前提过的 代码是对着socks5之类的常规入站写的 |
|
@Fangliding 我看了下 2dust/v2rayN#9715 (comment) 的日志,来源二元组是有的,可能 Linux 把 TUN 的连接记录放别处了? |
|
各位好,经过验证 Linux 进程路由还是有问题 Linux 会直接提示Unables to find local process name LinuxXray 26.7.11 (Xray, Penetrates Everything.) 50231ea (go1.26.5 linux/arm64)
A unified platform for anti-censorship.
2026/07/21 07:24:56.619257 [Info] infra/conf/serial: Reading config: &{Name:/home/Duhs/.local/share/v2rayN/binConfigs/config.json Format:json}
2026/07/21 07:24:56.997221 [Warning] core: Xray 26.7.11 started
2026/07/21 07:24:57.589095 [Error] app/router: Unables to find local process name: common/net: connection for 127.0.0.1:52250 not found in /proc/net/tcp
2026/07/21 07:24:57.589170 from tcp:127.0.0.1:52250 accepted tcp:www.google.com:443 [socks -> proxy]
2026/07/21 07:25:01.101767 [Error] app/router: Unables to find local process name: common/net: connection for 127.0.0.1:52254 not found in /proc/net/tcp
2026/07/21 07:25:01.101883 from tcp:127.0.0.1:52254 accepted tcp:www.google.com:443 [socks -> proxy]macOS会提示Unables to find local process name,之后日志正常,但实测macOS无法访问Google等被屏蔽的网站 macOSXray 26.7.11 (Xray, Penetrates Everything.) 50231ea (go1.26.5 darwin/arm64)
A unified platform for anti-censorship.
2026/07/21 07:32:32.886108 [Info] infra/conf/serial: Reading config: &{Name:/Users/Duhs/Library/Application Support/v2rayN/binConfigs/config.json Format:json}
2026/07/21 07:32:33.321964 [Warning] core: Xray 26.7.11 started
2026/07/21 07:32:33.467676 from tcp:172.18.0.2:49355 accepted tcp:17.57.145.133:5223 [tun >> proxy]
2026/07/21 07:32:33.963594 [Error] app/router: Unables to find local process name: common/net: process not found for tcp connection from 127.0.0.1:49357 to :0
2026/07/21 07:32:33.963629 from tcp:127.0.0.1:49357 accepted tcp:www.google.com:443 [socks -> proxy]
2026/07/21 07:32:34 当前延迟: 44 ms,none
2026/07/21 07:32:39.065710 from udp:172.18.0.2:54122 accepted udp:31.13.92.37:443 [tun -> block]
2026/07/21 07:32:39.170327 from udp:172.18.0.2:57597 accepted udp:185.45.5.35:443 [tun -> block]
2026/07/21 07:32:39.170810 from tcp:172.18.0.2:49359 accepted tcp:31.13.92.37:443 [tun >> proxy]
2026/07/21 07:32:41.177897 from tcp:172.18.0.2:49361 accepted tcp:185.45.5.35:443 [tun >> proxy]
2026/07/21 07:32:41.261557 from tcp:172.18.0.2:49363 accepted tcp:123.6.82.137:443 [tun -> direct]
2026/07/21 07:32:44.289165 from tcp:172.18.0.2:49365 accepted tcp:17.137.162.3:443 [tun >> proxy]
2026/07/21 07:32:45.897410 from udp:172.18.0.2:50739 accepted udp:31.13.92.37:443 [tun -> block]
2026/07/21 07:32:46.002885 from udp:172.18.0.2:65524 accepted udp:185.45.5.35:443 [tun -> block] |
Summary
Add a Darwin implementation of
common/net.FindProcessso routing rules withprocesscan work on macOS.The implementation uses libproc to inspect process file descriptors and socket endpoints without cgo or external commands. It supports TCP and UDP, returns PID/name/path, and keeps the existing
ErrNotLocal/ unsupported-network behavior.Notes
proc_pidinfo,proc_pidfdinfo, andproc_pidpathfrom/usr/lib/libproc.dylib.curl/ Chrome helper traffic matchedprocessProxyand routed through the proxy outbound.Tests
go test ./common/net -count=1CGO_ENABLED=0 go test ./common/net -count=1go test ./app/router ./proxy/tun -count=1GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go test -c ./common/net -o /tmp/common_net_darwin_amd64.testGOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go test -c ./common/net -o /tmp/common_net_darwin_arm64.testgit diff --checkAI Coding by GPT-5.5 Extra High.
Tested on macOS (Only Apple Silicon)