| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #!/usr/bin/env node
- const { execSync } = require('child_process')
- const path = require('path')
- const fs = require('fs')
- // 根目录 = config 文件所在目录的上级(config 在 <根>/configs/config.js)
- const configPath = process.env.STATIC_ROOT
- ? path.join(path.dirname(process.env.STATIC_ROOT), 'configs', 'config.js')
- : path.join(__dirname, '..', '..', 'configs', 'config.js')
- const PROJECT_ROOT = path.dirname(path.dirname(path.resolve(configPath)))
- const TCPIP_PORT = 5555
- /** 从配置解析并返回 ADB 可执行文件路径 */
- function getAdbPath() {
- if (!fs.existsSync(configPath)) return path.join(PROJECT_ROOT, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
- const config = require(configPath)
- const p = config.adbPath?.path
- return p ? (path.isAbsolute(p) ? p : path.resolve(PROJECT_ROOT, p)) : path.join(PROJECT_ROOT, 'lib', 'scrcpy-adb', process.platform === 'win32' ? 'adb.exe' : 'adb')
- }
- /** 返回当前通过 USB 连接的设备 ID 列表 */
- function getConnectedDeviceIds(adbPath) {
- const out = execSync(`"${adbPath}" devices`, { encoding: 'utf-8' })
- return out
- .split('\n')
- .filter((line) => line.trim() && !line.startsWith('List'))
- .map((line) => line.trim().split('\t')[0])
- .filter((id) => id)
- }
- /** 通过 USB 打开系统「无线调试」开关,并读回确认;返回是否成功 */
- function enableWirelessDebuggingSetting(adbPath, deviceId) {
- execSync(`"${adbPath}" -s ${deviceId} shell settings put global adb_wifi_enabled 1`, { encoding: 'utf-8' })
- const out = execSync(`"${adbPath}" -s ${deviceId} shell settings get global adb_wifi_enabled`, { encoding: 'utf-8' })
- return out.trim() === '1'
- }
- /** 通过 USB 在该设备上开启 TCPIP 监听,用于后续无线连接 */
- function enableTcpipOnDevice(adbPath, deviceId, port) {
- return execSync(`"${adbPath}" -s ${deviceId} tcpip ${port}`, { encoding: 'utf-8' }).trim()
- }
- /** 获取设备 WLAN IPv4,用于无线连接提示(先 getprop,无则用 ip addr)*/
- function getDeviceWlanIp(adbPath, deviceId) {
- const prop = execSync(`"${adbPath}" -s ${deviceId} shell getprop dhcp.wlan0.ipaddress`, { encoding: 'utf-8' }).trim()
- if (prop && /^\d+\.\d+\.\d+\.\d+$/.test(prop)) return prop
- const out = execSync(`"${adbPath}" -s ${deviceId} shell ip -4 addr show wlan0`, { encoding: 'utf-8' })
- const m = out.match(/inet\s+(\d+\.\d+\.\d+\.\d+)/)
- return m ? m[1] : null
- }
- const adbPath = getAdbPath()
- const devices = getConnectedDeviceIds(adbPath)
- if (devices.length === 0) {
- process.stderr.write('No devices found. Please connect a device via USB.\n')
- process.exit(1)
- }
- const deviceId = devices[0]
- if (devices.length > 1) {
- process.stdout.write(`Multiple devices. Using: ${deviceId}\n`)
- }
- // 先打开系统「无线调试」、取 WLAN IP(USB 仍连接),再执行 tcpip
- const wirelessOk = enableWirelessDebuggingSetting(adbPath, deviceId)
- if (wirelessOk) {
- process.stdout.write('无线调试已开启\n')
- } else {
- process.stdout.write('无线调试开关可能不被本机支持,请到 设置 → 开发者选项 中手动打开\n')
- }
- const wlanIp = getDeviceWlanIp(adbPath, deviceId)
- const tcpipOut = enableTcpipOnDevice(adbPath, deviceId, TCPIP_PORT)
- process.stdout.write(tcpipOut + '\n')
- if (wlanIp) {
- process.stdout.write(`Wireless: adb connect ${wlanIp}:${TCPIP_PORT}\n`)
- }
|