69 lines
2.2 KiB
Go
69 lines
2.2 KiB
Go
//go:build darwin
|
||
|
||
package device
|
||
|
||
import (
|
||
"fmt"
|
||
"net"
|
||
"os/exec"
|
||
"time"
|
||
)
|
||
|
||
// ============================================================
|
||
// [macOS] 触发「本地网络」权限弹窗
|
||
// 直接跑 Contents/MacOS 二进制时系统往往不登记;需:
|
||
// 1) 用 open 启动 .app(LaunchServices)
|
||
// 2) 进程内发 mDNS 组播 / Bonjour 浏览
|
||
// ============================================================
|
||
|
||
func triggerLocalNetworkAccess() ActionResult {
|
||
// 1) mDNS 组播(224.0.0.251:5353)—— 系统用此判断本地网络访问
|
||
addr, err := net.ResolveUDPAddr("udp4", "224.0.0.251:5353")
|
||
if err == nil {
|
||
conn, err := net.DialUDP("udp4", nil, addr)
|
||
if err == nil {
|
||
// 最小 DNS 查询头,足够让 TCC 记一笔
|
||
_, _ = conn.Write([]byte{
|
||
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||
})
|
||
_ = conn.Close()
|
||
}
|
||
}
|
||
|
||
// 2) 浏览 ADB 配对服务(Bonjour)
|
||
cmd := exec.Command("dns-sd", "-B", "_adb-tls-pairing._tcp", "local.")
|
||
_ = cmd.Start()
|
||
time.Sleep(1500 * time.Millisecond)
|
||
if cmd.Process != nil {
|
||
_ = cmd.Process.Kill()
|
||
_, _ = cmd.Process.Wait()
|
||
}
|
||
|
||
// 3) 打开系统设置对应页,方便用户手动打开开关
|
||
_ = exec.Command("open",
|
||
"x-apple.systempreferences:com.apple.preference.security?Privacy_LocalNetwork",
|
||
).Start()
|
||
// Sequoia / 更新系统备用 URL
|
||
_ = exec.Command("open",
|
||
"x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension?Privacy_LocalNetwork",
|
||
).Start()
|
||
|
||
return ActionResult{
|
||
OK: true,
|
||
Message: "已请求本地网络访问。若弹出授权请点「允许」;若列表暂无 iTuoke,请稍等几秒后刷新该页,并确认本应用是用「打开.app」方式启动的。",
|
||
}
|
||
}
|
||
|
||
func openLocalNetworkSettings() ActionResult {
|
||
err1 := exec.Command("open",
|
||
"x-apple.systempreferences:com.apple.settings.PrivacySecurity.extension?Privacy_LocalNetwork",
|
||
).Start()
|
||
err2 := exec.Command("open",
|
||
"x-apple.systempreferences:com.apple.preference.security?Privacy_LocalNetwork",
|
||
).Start()
|
||
if err1 != nil && err2 != nil {
|
||
return ActionResult{OK: false, Message: fmt.Sprintf("无法打开系统设置:%v", err1)}
|
||
}
|
||
return ActionResult{OK: true, Message: "已打开「本地网络」设置"}
|
||
}
|