xray的IPv4优先 IPv6优先行为受 gai.conf 控制?
通过进一步的交流, 群友的意思是 sing-box 向外访问, 无法通过 gai.conf 来设置是 IPv4优先 还是 IPv6优先
群友用的是fakeip的模式, 翻墙客户端发给梯子的数据包中只有域名, 没有IP.
上一篇我们测试了sing-box, 这次我们再测试一下 xray
技术基础
编译xray
准备测试环境
搭建一个简单的xray服务端
官方安装脚本
bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install
把service自动启动的xray关掉
systemctl stop xray
配置文件
nano /usr/local/etc/xray/test-config.json
{"log": {"access": "/var/log/xray/test-access.log","error": "/var/log/xray/test-error.log","loglevel": "debug"},"inbounds": [{"listen":"127.0.0.1","port":1080,"protocol":"socks"}],"outbounds": [{"protocol": "freedom","tag": "direct"}]}
命令行前台运行xray (不能Ctrl+C中断, 就这么放着)
/usr/local/bin/xray run -config /usr/local/etc/xray/test-config.json
新开一个SSH终端窗口.
curl -x socks5h://127.0.0.1:1080 api.myip.la
curl的结果正常.
查看xray的日志, 与 之前的测试记录一致
说明xray正常工作, DNS解析了域名.
给 Xray 挂钩子 指定使用系统 getaddrinfo
GODEBUG=netdns=cgo LD_PRELOAD=/root/hook_getaddrinfo.so /usr/local/bin/xray run -config /usr/local/etc/xray/test-config.json
* 上面这是1整行
另一个SSH窗口用curl测试
curl -x socks5h://127.0.0.1:1080 api.myip.la
发现xray的窗口没有钩子的打印
检查 xray 的日志 也没有钩子的打印
调整 CGO_ENABLED 编译参数
注意到 Xray 的官方编译操作有 CGO_ENABLED=0 参数
结合我们之前钩Go程序的经验, 猜测这个编译参数应该影响Go程序是否使用系统的getaddrinfo
让我们试试分别将这个编译参数设置为 0 和 1 的结果.
下载 xray 源码
编译xray
CGO_ENABLED=0 go build -o xray-cgo0 -trimpath -buildvcs=false -ldflags="-s -w -buildid=" -v ./main
CGO_ENABLED=1 go build -o xray-cgo1 -trimpath -buildvcs=false -ldflags="-s -w -buildid=" -v ./main
用 CGO_ENABLED=0编译的xray-cgo0进行测试
GODEBUG=netdns=cgo LD_PRELOAD=/root/hook_getaddrinfo.so /root/Xray-core-25.10.15/xray-cgo0 run -config /usr/local/etc/xray/test-config.json
* 上面是很长的1整行
在另一个SSH窗口用curl socks5h进行测试
xray-cgo0的窗口没有钩子打印日志
用 CGO_ENABLED=1编译的xray-cgo1进行测试
GODEBUG=netdns=cgo LD_PRELOAD=/root/hook_getaddrinfo.so /root/Xray-core-25.10.15/xray-cgo1 run -config /usr/local/etc/xray/test-config.json
* 上面是很长的1整行
在另一个SSH窗口用curl socks5h进行测试
xray-cgo1的窗口出现了熟悉的钩子打印日志
发现xray的行为是受gai.conf控制的.
不过需要注意的是, 当修改了gai.conf后, xray需要重新启动, 否则xray还是按启动时的gai.conf的设置.
总结
当使用xray原始程序时, 不受gai.conf的影响.
用 CGO_ENABLED=1 参数编译出xray, 并加上 GODEBUG=netdns=cgo 的运行参数后, 可以让 xray 调用系统 getaddrinfo 接口, 进而受到 gai.conf 影响, 作出 IPv4优先 或 IPv6优先 的行为.







评论
发表评论