enable-port5555.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env node
  2. const { execSync } = require('child_process')
  3. const path = require('path')
  4. const fs = require('fs')
  5. // 根目录 = config 文件所在目录的上级(config 在 <根>/configs/config.js)
  6. const configPath = process.env.STATIC_ROOT
  7. ? path.join(path.dirname(process.env.STATIC_ROOT), 'configs', 'config.js')
  8. : path.join(__dirname, '..', '..', 'configs', 'config.js')
  9. const PROJECT_ROOT = path.dirname(path.dirname(path.resolve(configPath)))
  10. const TCPIP_PORT = 5555
  11. /** 从配置解析并返回 ADB 可执行文件路径 */
  12. function getAdbPath() {
  13. if (!fs.existsSync(configPath)) return path.join(PROJECT_ROOT, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
  14. const config = require(configPath)
  15. const p = config.adbPath?.path
  16. return p ? (path.isAbsolute(p) ? p : path.resolve(PROJECT_ROOT, p)) : path.join(PROJECT_ROOT, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
  17. }
  18. /** 返回当前通过 USB 连接的设备 ID 列表 */
  19. function getConnectedDeviceIds(adbPath) {
  20. const out = execSync(`"${adbPath}" devices`, { encoding: 'utf-8' })
  21. return out
  22. .split('\n')
  23. .filter((line) => line.trim() && !line.startsWith('List'))
  24. .map((line) => line.trim().split('\t')[0])
  25. .filter((id) => id)
  26. }
  27. /** 通过 USB 打开系统「无线调试」开关,并读回确认;返回是否成功 */
  28. function enableWirelessDebuggingSetting(adbPath, deviceId) {
  29. execSync(`"${adbPath}" -s ${deviceId} shell settings put global adb_wifi_enabled 1`, { encoding: 'utf-8' })
  30. const out = execSync(`"${adbPath}" -s ${deviceId} shell settings get global adb_wifi_enabled`, { encoding: 'utf-8' })
  31. return out.trim() === '1'
  32. }
  33. /** 通过 USB 在该设备上开启 TCPIP 监听,用于后续无线连接 */
  34. function enableTcpipOnDevice(adbPath, deviceId, port) {
  35. return execSync(`"${adbPath}" -s ${deviceId} tcpip ${port}`, { encoding: 'utf-8' }).trim()
  36. }
  37. /** 获取设备 WLAN IPv4,用于无线连接提示(先 getprop,无则用 ip addr)*/
  38. function getDeviceWlanIp(adbPath, deviceId) {
  39. const prop = execSync(`"${adbPath}" -s ${deviceId} shell getprop dhcp.wlan0.ipaddress`, { encoding: 'utf-8' }).trim()
  40. if (prop && /^\d+\.\d+\.\d+\.\d+$/.test(prop)) return prop
  41. const out = execSync(`"${adbPath}" -s ${deviceId} shell ip -4 addr show wlan0`, { encoding: 'utf-8' })
  42. const m = out.match(/inet\s+(\d+\.\d+\.\d+\.\d+)/)
  43. return m ? m[1] : null
  44. }
  45. const adbPath = getAdbPath()
  46. const devices = getConnectedDeviceIds(adbPath)
  47. if (devices.length === 0) {
  48. process.stderr.write('No devices found. Please connect a device via USB.\n')
  49. process.exit(1)
  50. }
  51. const deviceId = devices[0]
  52. if (devices.length > 1) {
  53. process.stdout.write(`Multiple devices. Using: ${deviceId}\n`)
  54. }
  55. // 先打开系统「无线调试」、取 WLAN IP(USB 仍连接),再执行 tcpip
  56. const wirelessOk = enableWirelessDebuggingSetting(adbPath, deviceId)
  57. if (wirelessOk) {
  58. process.stdout.write('无线调试已开启\n')
  59. } else {
  60. process.stdout.write('无线调试开关可能不被本机支持,请到 设置 → 开发者选项 中手动打开\n')
  61. }
  62. const wlanIp = getDeviceWlanIp(adbPath, deviceId)
  63. const tcpipOut = enableTcpipOnDevice(adbPath, deviceId, TCPIP_PORT)
  64. process.stdout.write(tcpipOut + '\n')
  65. if (wlanIp) {
  66. process.stdout.write(`Wireless: adb connect ${wlanIp}:${TCPIP_PORT}\n`)
  67. }