enable-wirless-connect.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env node
  2. const { execSync } = require('child_process')
  3. const path = require('path')
  4. // 项目根 = 脚本所在目录的上一级(nodejs -> 项目根)
  5. // 打包后 nodejs 在 app.asar 内执行,但 lib/configs 在 app.asar.unpacked,需用 unpacked 路径才能找到 adb
  6. let PROJECT_ROOT = path.resolve(__dirname, '..')
  7. if (PROJECT_ROOT.includes('app.asar') && !PROJECT_ROOT.includes('app.asar.unpacked')) {
  8. PROJECT_ROOT = PROJECT_ROOT.replace('app.asar', 'app.asar.unpacked')
  9. }
  10. const TCPIP_PORT = 5555
  11. /** 从配置解析并返回 ADB 可执行文件路径 */
  12. function getAdbPath() {
  13. const configPath = path.join(PROJECT_ROOT, 'configs', 'config.js')
  14. const config = require(configPath)
  15. return config.adbPath?.path
  16. ? path.resolve(PROJECT_ROOT, config.adbPath.path)
  17. : path.join(PROJECT_ROOT, 'lib', 'scrcpy-adb', 'adb.exe')
  18. }
  19. /** 返回当前通过 USB 连接的设备 ID 列表(仅 status 为 device 且非 IP:port,排除无线设备) */
  20. function getConnectedDeviceIds(adbPath) {
  21. const out = execSync(`"${adbPath}" devices`, { encoding: 'utf-8' })
  22. return out
  23. .split('\n')
  24. .filter((line) => line.trim() && !line.startsWith('List') && line.includes('\tdevice'))
  25. .map((line) => line.trim().split('\t')[0])
  26. .filter((id) => id && !id.includes(':'))
  27. }
  28. /** 操作一:在 USB 设备上开启无线调试(settings adb_wifi_enabled 1) */
  29. function enableWirelessSetting(adbPath, deviceId) {
  30. execSync(`"${adbPath}" -s ${deviceId} shell settings put global adb_wifi_enabled 1`, { encoding: 'utf-8' })
  31. }
  32. /** 操作二:在 USB 设备上激活 5555 端口(tcpip),用于无线连接 */
  33. function enableTcpipPort(adbPath, deviceId, port) {
  34. return execSync(`"${adbPath}" -s ${deviceId} tcpip ${port}`, { encoding: 'utf-8' }).trim()
  35. }
  36. /** 从 adb shell 输出中匹配 inet 后的 IPv4(支持 inet 192.168.1.1/24 格式),可排除回环 */
  37. function parseInetFromOutput(out, skipLoopback = false) {
  38. const re = /inet\s+(\d+\.\d+\.\d+\.\d+)/g
  39. const ips = []
  40. let m
  41. while ((m = re.exec(out || '')) !== null) ips.push(m[1])
  42. if (ips.length === 0) return null
  43. if (skipLoopback) {
  44. const nonLoop = ips.find((ip) => ip !== '127.0.0.1')
  45. return nonLoop || null
  46. }
  47. return ips[0]
  48. }
  49. /** 安全拼 adb -s 参数(deviceId 含空格时需引号) */
  50. function adbShellCmd(adbPath, deviceId, shellCmd) {
  51. const id = deviceId.indexOf(' ') >= 0 ? `"${deviceId}"` : deviceId
  52. return `"${adbPath}" -s ${id} shell ${shellCmd}`
  53. }
  54. /** 获取设备当前 WiFi IP:多接口 + 多命令兼容不同机型 */
  55. function getDeviceIp(adbPath, deviceId) {
  56. const run = (shellCmd) => {
  57. try {
  58. return execSync(adbShellCmd(adbPath, deviceId, shellCmd), { encoding: 'utf-8' })
  59. } catch (e) {
  60. return ''
  61. }
  62. }
  63. // 1) wlan0 inet(最常见)
  64. let out = run('ip -4 addr show wlan0')
  65. let ip = parseInetFromOutput(out)
  66. if (ip) return ip
  67. // 2) eth0(部分机型/平板)
  68. out = run('ip -4 addr show eth0')
  69. ip = parseInetFromOutput(out)
  70. if (ip) return ip
  71. // 3) ip route get:src 或 from
  72. out = run('ip route get 1.1.1.1')
  73. let m = out.match(/\bsrc\s+(\d+\.\d+\.\d+\.\d+)\b/)
  74. if (m) return m[1]
  75. m = out.match(/\bfrom\s+(\d+\.\d+\.\d+\.\d+)\b/)
  76. if (m) return m[1]
  77. // 4) getprop
  78. for (const prop of ['dhcp.wlan0.ipaddress', 'net.wlan0.ipaddress', 'dhcp.eth0.ipaddress']) {
  79. out = run('getprop ' + prop)
  80. ip = (out || '').trim()
  81. if (/^\d+\.\d+\.\d+\.\d+$/.test(ip)) return ip
  82. }
  83. // 5) ip -4 addr show 全量,取第一个非回环 IP(避免 lo 的 127.0.0.1)
  84. out = run('ip -4 addr show')
  85. return parseInetFromOutput(out, true)
  86. }
  87. /** 主流程:取 USB 设备,先取 IP(与 getip.js 一致,此时 USB 稳定),再开无线与 tcpip,最后输出 */
  88. function run() {
  89. const adbPath = getAdbPath()
  90. const devices = getConnectedDeviceIds(adbPath)
  91. if (devices.length === 0) {
  92. process.stderr.write('No devices found. Please connect a device via USB.\n')
  93. process.exit(1)
  94. }
  95. const deviceId = devices[0]
  96. // 先取 IP(在 tcpip 之前,USB 稳定时取,与 bat-tool/getip 行为一致)
  97. const ip = getDeviceIp(adbPath, deviceId)
  98. enableWirelessSetting(adbPath, deviceId)
  99. const tcpipOut = enableTcpipPort(adbPath, deviceId, TCPIP_PORT)
  100. process.stdout.write(tcpipOut + '\n')
  101. if (ip) {
  102. process.stdout.write('DEVICE_IP:' + ip + '\n')
  103. }
  104. process.exit(0)
  105. }
  106. run()