dsh-miniwin 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +28 -0
- package/README.en.md +83 -0
- package/README.md +304 -2
- package/cordis.patch.yml +10 -0
- package/lib/client.js +532 -0
- package/lib/discover.js +244 -0
- package/lib/index.js +2290 -0
- package/lib/qr.js +313 -0
- package/package.json +57 -6
- package/runtime/adb-control.js +470 -0
- package/runtime/apk/miniwin.apk +0 -0
- package/runtime/assets/icon.ico +0 -0
- package/runtime/assets/icon.png +0 -0
- package/runtime/assets/tray-16.png +0 -0
- package/runtime/assets/tray-32.png +0 -0
- package/runtime/au.js +131 -0
- package/runtime/audio-bridge.js +267 -0
- package/runtime/audio-healer.js +369 -0
- package/runtime/audio-router.js +175 -0
- package/runtime/avcc.js +186 -0
- package/runtime/device.js +307 -0
- package/runtime/main.js +3309 -0
- package/runtime/mkv-reader.js +326 -0
- package/runtime/package.json +5 -0
- package/runtime/preload.js +122 -0
- package/runtime/scrcpy-capture.js +623 -0
- package/runtime/streamer.js +192 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/AdbWinApi.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/AdbWinUsbApi.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/LICENSE.txt +204 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/SDL3.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/adb.exe +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/avcodec-63.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/avformat-63.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/avutil-61.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/disconnected.png +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/libusb-1.0.dll +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/open_a_terminal_here.bat +1 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/scrcpy-noconsole.vbs +7 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/scrcpy-server +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/scrcpy.exe +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/scrcpy.png +0 -0
- package/runtime/tools/scrcpy/scrcpy-win64-v5.0.1/swresample-7.dll +0 -0
- package/runtime/ui.html +3166 -0
- package/runtime/usb-hotplug.js +194 -0
- package/runtime/wifi-adb-keeper.js +395 -0
- package/runtime/wifi-discovery.js +213 -0
- package/runtime/wifi-receiver.js +733 -0
- package/screenshots.json +5 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* USB 热插拔快速重连 —— 用 adb 的事件流,不用轮询。
|
|
3
|
+
*
|
|
4
|
+
* ══════════ 为什么放弃轮询 ══════════
|
|
5
|
+
*
|
|
6
|
+
* 最初想每 250ms 跑一次 `adb devices`。实测单次 38~152ms(抖动大),
|
|
7
|
+
* 250ms 间隔意味着 adb 进程几乎一直在 fork,CPU 白烧,而且
|
|
8
|
+
* 仍然有最多 250ms 的发现延迟。
|
|
9
|
+
*
|
|
10
|
+
* ══════════ adb track-devices ══════════
|
|
11
|
+
*
|
|
12
|
+
* adb 自带一个长连接命令:`adb track-devices`
|
|
13
|
+
*
|
|
14
|
+
* - 它是一条只会往前流的**事件流**,设备有任何变化立刻推一行
|
|
15
|
+
* - 零轮询、零 fork,一个进程常驻
|
|
16
|
+
* - 实测格式:
|
|
17
|
+
* 00352MN0224829001893<TAB>device
|
|
18
|
+
* 00192.168.172.118:5555<TAB>offline
|
|
19
|
+
* 00 ← 空列表(前面 00 是长度的十六进制)
|
|
20
|
+
*
|
|
21
|
+
* 所以设备一插上,这里几乎是「立刻」收到,不用等任何轮询周期。
|
|
22
|
+
*
|
|
23
|
+
* ══════════ 触发策略 ══════════
|
|
24
|
+
*
|
|
25
|
+
* 只在「无 → 有」的边沿触发一次重连,且加 1.5 秒冷却 ——
|
|
26
|
+
* USB 枚举过程中 adb 可能连推好几次事件(offline → device),
|
|
27
|
+
* 不去重会发起多次并发重连。
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
const { spawn } = require('node:child_process')
|
|
31
|
+
|
|
32
|
+
class UsbHotplugWatcher {
|
|
33
|
+
/**
|
|
34
|
+
* @param {object} o
|
|
35
|
+
* @param {string} o.adb
|
|
36
|
+
* @param {(serial:string)=>void} o.onDeviceBack 设备回来(边沿触发)
|
|
37
|
+
* @param {(serial:string)=>void} [o.onDeviceLost]
|
|
38
|
+
* @param {(m:string)=>void} [o.onLog]
|
|
39
|
+
*/
|
|
40
|
+
constructor (o) {
|
|
41
|
+
this.adb = o.adb
|
|
42
|
+
this.onDeviceBack = o.onDeviceBack
|
|
43
|
+
this.onDeviceLost = o.onDeviceLost || (() => {})
|
|
44
|
+
this.onLog = o.onLog || (() => {})
|
|
45
|
+
|
|
46
|
+
this.proc = null
|
|
47
|
+
this.online = null // 当前已知的唯一在线设备
|
|
48
|
+
this.recovering = false
|
|
49
|
+
this._cooldown = null
|
|
50
|
+
this._restartTimer = null
|
|
51
|
+
this.stopped = false
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
log (m) { this.onLog('[usb] ' + m) }
|
|
55
|
+
|
|
56
|
+
start () {
|
|
57
|
+
this.stopped = false
|
|
58
|
+
this._spawn()
|
|
59
|
+
this.log('热插拔监听已启动(adb track-devices 事件流)')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
_spawn () {
|
|
63
|
+
if (this.stopped) return
|
|
64
|
+
try {
|
|
65
|
+
this.proc = spawn(this.adb, ['track-devices'], { windowsHide: true })
|
|
66
|
+
} catch (e) {
|
|
67
|
+
this.log('启动失败: ' + e.message)
|
|
68
|
+
this._scheduleRestart()
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let buf = ''
|
|
73
|
+
|
|
74
|
+
this.proc.stdout.on('data', (d) => {
|
|
75
|
+
buf += d.toString('utf8')
|
|
76
|
+
/**
|
|
77
|
+
* track-devices 的输出是「4 位十六进制长度 + 内容」分帧,
|
|
78
|
+
* 但实际解析不需要那么严格 —— 直接按行取「serial<TAB>state」
|
|
79
|
+
* 更稳,因为长度前缀里可能夹着换行。
|
|
80
|
+
*/
|
|
81
|
+
let idx
|
|
82
|
+
while ((idx = buf.indexOf('\n')) >= 0) {
|
|
83
|
+
const line = buf.slice(0, idx)
|
|
84
|
+
buf = buf.slice(idx + 1)
|
|
85
|
+
this._handleLine(line)
|
|
86
|
+
}
|
|
87
|
+
// 防止异常输入把 buffer 撑爆
|
|
88
|
+
if (buf.length > 65536) buf = ''
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
this.proc.stderr.on('data', () => {})
|
|
92
|
+
|
|
93
|
+
this.proc.on('exit', () => {
|
|
94
|
+
if (this.stopped) return
|
|
95
|
+
this.log('监听进程退出,1 秒后重启')
|
|
96
|
+
this._scheduleRestart()
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
this.proc.on('error', (e) => {
|
|
100
|
+
this.log('监听进程错误: ' + e.message)
|
|
101
|
+
this._scheduleRestart()
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
_scheduleRestart () {
|
|
106
|
+
if (this.stopped || this._restartTimer) return
|
|
107
|
+
this._restartTimer = setTimeout(() => {
|
|
108
|
+
this._restartTimer = null
|
|
109
|
+
this._spawn()
|
|
110
|
+
}, 1000)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 解析一行:
|
|
115
|
+
* "00122MN0224829001893\tdevice" ← 正常(前 4 位是长度)
|
|
116
|
+
* "00122MN0224829001893\toffline"
|
|
117
|
+
* "" 或 "00" ← 设备列表空
|
|
118
|
+
*/
|
|
119
|
+
_handleLine (raw) {
|
|
120
|
+
const line = raw.replace(/\r/g, '').trim()
|
|
121
|
+
if (!line) {
|
|
122
|
+
// 空行在部分版本里代表「列表空了」
|
|
123
|
+
this._onEmpty()
|
|
124
|
+
return
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 去掉可能存在的长度前缀
|
|
128
|
+
let s = line
|
|
129
|
+
if (/^[0-9a-f]{4}/.test(s) && s.includes('\t')) {
|
|
130
|
+
s = s.slice(4)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const parts = s.split('\t')
|
|
134
|
+
if (parts.length < 2) {
|
|
135
|
+
if (/^[0-9a-f]+$/i.test(s)) this._onEmpty()
|
|
136
|
+
return
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const serial = parts[0].trim()
|
|
140
|
+
const state = parts[1].trim()
|
|
141
|
+
if (!serial) return
|
|
142
|
+
|
|
143
|
+
if (state === 'device') {
|
|
144
|
+
this._onOnline(serial)
|
|
145
|
+
} else if (state === 'offline' || state === 'unauthorized') {
|
|
146
|
+
// offline → 不算在线,但如果之前是我们用的那个,视为掉线
|
|
147
|
+
if (this.online === serial) {
|
|
148
|
+
this.log(`设备转为 ${state}:${serial}`)
|
|
149
|
+
this.online = null
|
|
150
|
+
try { this.onDeviceLost(serial) } catch {}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
_onEmpty () {
|
|
156
|
+
if (this.online) {
|
|
157
|
+
this.log(`设备列表已空(原 ${this.online})`)
|
|
158
|
+
const lost = this.online
|
|
159
|
+
this.online = null
|
|
160
|
+
try { this.onDeviceLost(lost) } catch {}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
_onOnline (serial) {
|
|
165
|
+
if (this.online === serial) return // 已经是这个了,不重复触发
|
|
166
|
+
|
|
167
|
+
const isNew = !this.online
|
|
168
|
+
this.online = serial
|
|
169
|
+
this.log(`设备在线:${serial}`)
|
|
170
|
+
|
|
171
|
+
// 只处理「从无到有」;如果是不同设备切换,也当作新的
|
|
172
|
+
if (!isNew && this.recovering) return
|
|
173
|
+
if (this.recovering) return
|
|
174
|
+
|
|
175
|
+
this.recovering = true
|
|
176
|
+
try { this.onDeviceBack(serial) } catch {}
|
|
177
|
+
this._cooldown = setTimeout(() => {
|
|
178
|
+
this.recovering = false
|
|
179
|
+
this._cooldown = null
|
|
180
|
+
}, 1500)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
stop () {
|
|
184
|
+
this.stopped = true
|
|
185
|
+
if (this._cooldown) { clearTimeout(this._cooldown); this._cooldown = null }
|
|
186
|
+
if (this._restartTimer) { clearTimeout(this._restartTimer); this._restartTimer = null }
|
|
187
|
+
if (this.proc) {
|
|
188
|
+
try { this.proc.kill() } catch {}
|
|
189
|
+
this.proc = null
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
module.exports = { UsbHotplugWatcher }
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WiFi adb 自愈 —— 让投屏在「手机重启后」自动恢复,不需要插 USB。
|
|
3
|
+
*
|
|
4
|
+
* ══════════ 要解决什么 ══════════
|
|
5
|
+
*
|
|
6
|
+
* adb 的 TCP 模式(`adb tcpip 5555`)本质上是**运行时状态**:
|
|
7
|
+
* adbd 进程重启就回到 USB 模式,所以手机一重启就失效。
|
|
8
|
+
* 而 `adb tcpip` 本身只能通过 USB 通道下发,无法自己通过网络执行 ——
|
|
9
|
+
* 鸡生蛋的问题。
|
|
10
|
+
*
|
|
11
|
+
* 华为把这台机器的「无线调试」设置页裁掉了(Settings 里没有
|
|
12
|
+
* 对应的 Activity,adb_wifi_enabled 是只读的),所以 Android 11
|
|
13
|
+
* 原生的「无线调试配对」也用不了。只能靠下面的策略绕过去。
|
|
14
|
+
*
|
|
15
|
+
* ══════════ 三层自愈策略 ══════════
|
|
16
|
+
*
|
|
17
|
+
* ① 平时:直接用 WiFi serial 连接(TTL 内一直有效)
|
|
18
|
+
*
|
|
19
|
+
* ② 掉线时:扫描局域网的 5555 端口,找到就重连
|
|
20
|
+
* —— 处理「手机换了 IP」「DHCP 重新分配」这类情况
|
|
21
|
+
*
|
|
22
|
+
* ③ 都不行时:等 USB 出现,自动跑一次 `adb tcpip` 然后切回 WiFi
|
|
23
|
+
* —— 处理「手机重启」这种情况。用户只需插线 3 秒,
|
|
24
|
+
* 程序提示「可以拔线了」就拔掉,全程自动化
|
|
25
|
+
*
|
|
26
|
+
* ══════════ 为什么不做「无线调试配对」 ══════════
|
|
27
|
+
*
|
|
28
|
+
* Android 11+ 的原生无线调试需要:
|
|
29
|
+
* 开发者选项 → 无线调试 → 使用配对码配对设备
|
|
30
|
+
* 华为在这台机器的 ROM 上把这个入口去掉了(实测 Settings 里
|
|
31
|
+
* 搜不到任何 WirelessDebuggingActivity,adb_wifi_enabled 永远返回 0
|
|
32
|
+
* 且不可写)。所以只能走上面的三层策略。
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
const net = require('node:net')
|
|
36
|
+
const { execFile } = require('node:child_process')
|
|
37
|
+
|
|
38
|
+
class WifiAdbKeeper {
|
|
39
|
+
/**
|
|
40
|
+
* @param {object} o
|
|
41
|
+
* @param {string} o.adb adb.exe 路径
|
|
42
|
+
* @param {string} [o.preferIp] 优先尝试的手机 IP
|
|
43
|
+
* @param {()=>void} [o.onLog]
|
|
44
|
+
* @param {(serial:string)=>void} [o.onOnline] 拿到可用 serial 时回调
|
|
45
|
+
* @param {(needUsb:boolean)=>void} [o.onNeedUsb] 需要用户插 USB 时回调
|
|
46
|
+
*/
|
|
47
|
+
constructor (o) {
|
|
48
|
+
this.adb = o.adb
|
|
49
|
+
/**
|
|
50
|
+
* 优先 IP:显式传入 > 上次成功连接落盘的缓存。
|
|
51
|
+
*
|
|
52
|
+
* 缓存来自 wifi-discovery.js(%APPDATA%\miniwin-stream\wifi-cache.json)。
|
|
53
|
+
* 之前 preferIp 只在内存里,程序一重启就丢,每次都要重新扫整个 /24;
|
|
54
|
+
* 有了落盘缓存,冷启动也能 200ms 内直连。
|
|
55
|
+
*/
|
|
56
|
+
this.preferIp = o.preferIp || (() => {
|
|
57
|
+
try { const c = require('./wifi-discovery.js').readCache(); return c ? c.host : null } catch { return null }
|
|
58
|
+
})()
|
|
59
|
+
this.onLog = o.onLog || (() => {})
|
|
60
|
+
this.onOnline = o.onOnline || (() => {})
|
|
61
|
+
this.onNeedUsb = o.onNeedUsb || (() => {})
|
|
62
|
+
|
|
63
|
+
this.serial = null // 当前可用的 serial(wifi 或 usb)
|
|
64
|
+
this.transport = null // 'wifi' | 'usb'
|
|
65
|
+
this.timer = null
|
|
66
|
+
this.scanning = false
|
|
67
|
+
this._lastUsbPrepare = 0
|
|
68
|
+
/**
|
|
69
|
+
* 「现在是否正在投屏采集」——上层注入。
|
|
70
|
+
* 采集期间禁止 `adb tcpip`(那会重启 adbd 掐断 scrcpy),
|
|
71
|
+
* 详见 _check() 里的注释。
|
|
72
|
+
*/
|
|
73
|
+
this.isStreaming = o.isStreaming || null
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
log (m) { this.onLog('[wifi-adb] ' + m) }
|
|
77
|
+
|
|
78
|
+
_run (args, timeout = 8000) {
|
|
79
|
+
return new Promise(resolve => {
|
|
80
|
+
execFile(this.adb, args, { timeout, windowsHide: true },
|
|
81
|
+
(err, stdout, stderr) => resolve({
|
|
82
|
+
ok: !err,
|
|
83
|
+
out: String(stdout || ''),
|
|
84
|
+
err: String(stderr || (err && err.message) || '')
|
|
85
|
+
}))
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 列出当前 adb 设备 */
|
|
90
|
+
async devices () {
|
|
91
|
+
const r = await this._run(['devices'])
|
|
92
|
+
const list = []
|
|
93
|
+
/**
|
|
94
|
+
* 按行扫,注意 Windows 的 \r。
|
|
95
|
+
*
|
|
96
|
+
* ── 踩过的坑(这个 bug 藏得很深)──
|
|
97
|
+
*
|
|
98
|
+
* adb.exe 在 Windows 上输出的是 CRLF:
|
|
99
|
+
* "List of devices attached\r\n2MN0224829001893\tdevice\r\n\r\n"
|
|
100
|
+
*
|
|
101
|
+
* 原来按 \n 切之后,每行末尾还挂着 \r。而正则写成:
|
|
102
|
+
* /^(\S+)\s+(\S+)$/
|
|
103
|
+
* `\S` 不匹配 \r、`$` 只匹配字符串末尾(不含 \r),于是
|
|
104
|
+
* **整行匹配失败**,list 永远是空数组。
|
|
105
|
+
*
|
|
106
|
+
* 后果:手机明明插着 USB、adb devices 也看得到,程序却判定
|
|
107
|
+
* 「没有 adb 设备」→ 直接跳去扫局域网 → 扫不到(WiFi adb 没开)
|
|
108
|
+
* → 卡在「正在等手机上线」3 分钟。而 USB 那条路根本没被尝试。
|
|
109
|
+
*
|
|
110
|
+
* 修法:先 trim 掉行尾空白再匹配;同时按 \r?\n 切行,
|
|
111
|
+
* 两种换行都能吃。
|
|
112
|
+
*/
|
|
113
|
+
for (const raw of r.out.split(/\r?\n/)) {
|
|
114
|
+
const line = raw.trim() // ← 关键:吃掉行尾的 \r
|
|
115
|
+
if (!line) continue
|
|
116
|
+
const m = line.match(/^(\S+)\s+(\S+)$/)
|
|
117
|
+
if (m && m[2] === 'device') {
|
|
118
|
+
list.push({
|
|
119
|
+
serial: m[1],
|
|
120
|
+
isWifi: /:\d+$/.test(m[1])
|
|
121
|
+
})
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return list
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 启动守护循环。
|
|
129
|
+
*
|
|
130
|
+
* 每 5 秒检查一次:
|
|
131
|
+
* - 有 WiFi 设备 → 直接用,标记 online
|
|
132
|
+
* - 只有 USB → 跑一次 tcpip 切到 WiFi,然后提示用户拔线
|
|
133
|
+
* - 都没有 → 扫描局域网
|
|
134
|
+
*/
|
|
135
|
+
start () {
|
|
136
|
+
this.stop()
|
|
137
|
+
const tick = async () => {
|
|
138
|
+
try { await this._check() } catch (e) { this.log('检查失败: ' + e.message) }
|
|
139
|
+
this.timer = setTimeout(tick, 5000)
|
|
140
|
+
}
|
|
141
|
+
tick()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
stop () {
|
|
145
|
+
if (this.timer) { clearTimeout(this.timer); this.timer = null }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async _check () {
|
|
149
|
+
const devs = await this.devices()
|
|
150
|
+
const wifi = devs.find(d => d.isWifi)
|
|
151
|
+
const usb = devs.find(d => !d.isWifi)
|
|
152
|
+
|
|
153
|
+
// ① WiFi 已连上 → 一切正常
|
|
154
|
+
if (wifi) {
|
|
155
|
+
if (this.serial !== wifi.serial || this.transport !== 'wifi') {
|
|
156
|
+
this.serial = wifi.serial
|
|
157
|
+
this.transport = 'wifi'
|
|
158
|
+
this.log(`WiFi 设备在线: ${wifi.serial}`)
|
|
159
|
+
this.onOnline(wifi.serial)
|
|
160
|
+
}
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ② 只有 USB → 切换一次(限流:30 秒内不重复)
|
|
165
|
+
if (usb && Date.now() - this._lastUsbPrepare > 30000) {
|
|
166
|
+
this._lastUsbPrepare = Date.now()
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* ══════════ tcpip 会重启 adbd,采集期间必须绕开 ══════════
|
|
170
|
+
*
|
|
171
|
+
* `adb tcpip 5555` 的真实语义是 "restarting in TCP mode",
|
|
172
|
+
* adbd 一重启,正在跑的 scrcpy 当场 `Device disconnected`。
|
|
173
|
+
*
|
|
174
|
+
* 所以:
|
|
175
|
+
* ① 设备已经是 TCP 模式 → 只 adb connect,不重新下发
|
|
176
|
+
* ② 上层说正在采集 → 什么都不做
|
|
177
|
+
*/
|
|
178
|
+
let alreadyTcp = false
|
|
179
|
+
try {
|
|
180
|
+
const g = await this._run([
|
|
181
|
+
'-s', usb.serial, 'shell', 'getprop', 'service.adb.tcp.port'
|
|
182
|
+
], 6000)
|
|
183
|
+
alreadyTcp = String(g.out || '').trim() === '5555'
|
|
184
|
+
} catch {}
|
|
185
|
+
|
|
186
|
+
if (!alreadyTcp) {
|
|
187
|
+
if (this.isStreaming && this.isStreaming()) {
|
|
188
|
+
this.log('采集进行中,跳过 tcpip(避免重启 adbd 断流)')
|
|
189
|
+
return
|
|
190
|
+
}
|
|
191
|
+
this.log(`检测到 USB(${usb.serial}),正在开启 WiFi 通道…`)
|
|
192
|
+
const r = await this._run(['-s', usb.serial, 'tcpip', '5555'], 15000)
|
|
193
|
+
if (!r.ok) {
|
|
194
|
+
this.log('tcpip 失败: ' + r.err.slice(0, 100))
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
this.log('tcpip 已开启,等待设备上线…')
|
|
198
|
+
await new Promise(r2 => setTimeout(r2, 3000))
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// 拿手机 IP
|
|
202
|
+
const ip = await this._phoneIp(usb.serial)
|
|
203
|
+
if (!ip) { this.log('拿不到手机 WiFi IP'); return }
|
|
204
|
+
this.log(`手机 WiFi IP: ${ip}`)
|
|
205
|
+
|
|
206
|
+
const c = await this._run(['connect', `${ip}:5555`], 10000)
|
|
207
|
+
if (c.out.includes('connected')) {
|
|
208
|
+
this.log(`✓ WiFi 通道就绪: ${ip}:5555 —— 现在可以拔掉 USB`)
|
|
209
|
+
this.preferIp = ip
|
|
210
|
+
this.serial = `${ip}:5555`
|
|
211
|
+
this.transport = 'wifi'
|
|
212
|
+
this.onOnline(this.serial)
|
|
213
|
+
} else {
|
|
214
|
+
this.log('连接失败: ' + c.out.slice(0, 100))
|
|
215
|
+
}
|
|
216
|
+
return
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ③ 什么都没有 → 扫局域网 + 提示插 USB
|
|
220
|
+
if (!this.scanning) {
|
|
221
|
+
this.scanning = true
|
|
222
|
+
const found = await this.scanLan()
|
|
223
|
+
this.scanning = false
|
|
224
|
+
if (found) {
|
|
225
|
+
this.log(`扫描到设备: ${found}`)
|
|
226
|
+
this.serial = found
|
|
227
|
+
this.transport = 'wifi'
|
|
228
|
+
this.onOnline(found)
|
|
229
|
+
return
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
this.onNeedUsb(true)
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** 读手机 WiFi IP */
|
|
236
|
+
async _phoneIp (serial) {
|
|
237
|
+
const r = await this._run(['-s', serial, 'shell',
|
|
238
|
+
"ip -o addr show wlan0 2>/dev/null | grep 'inet ' | head -1"], 8000)
|
|
239
|
+
const m = r.out.match(/inet (\d+\.\d+\.\d+\.\d+)/)
|
|
240
|
+
return m ? m[1] : null
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 扫局域网 5555 端口。
|
|
245
|
+
*
|
|
246
|
+
* 只在「已知网段 + 优先 IP 附近」扫,全 /24 段扫一遍约 254 个连接,
|
|
247
|
+
* 并发 32 的话 2 秒左右,不会拖慢主流程。
|
|
248
|
+
*/
|
|
249
|
+
async scanLan () {
|
|
250
|
+
const pcIp = await this._pcIp()
|
|
251
|
+
if (!pcIp) return null
|
|
252
|
+
|
|
253
|
+
// 优先试上次的 IP(内存或落盘缓存)
|
|
254
|
+
if (this.preferIp) {
|
|
255
|
+
if (await this._tryConnect(this.preferIp)) return `${this.preferIp}:5555`
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* 交给 wifi-discovery 扫:多网卡多网段、并发 96、260ms 超时,
|
|
260
|
+
* 比这里原来的「单网段 + 批 32 串行」快一倍以上,
|
|
261
|
+
* 而且会顺带用 mDNS 找 Android 11+ 无线调试广播的端口。
|
|
262
|
+
*/
|
|
263
|
+
try {
|
|
264
|
+
const disc = require('./wifi-discovery.js')
|
|
265
|
+
const r = await disc.discover({ adbPath: this.adb, port: 5555 })
|
|
266
|
+
for (const c of r.candidates) {
|
|
267
|
+
// connect() 内部会用 get-state 复核,避免把「端口开着但不是 adb」的主机写进缓存
|
|
268
|
+
const ok = await disc.connect(this.adb, c.host, c.port)
|
|
269
|
+
if (ok.ok) {
|
|
270
|
+
this.preferIp = c.host
|
|
271
|
+
return `${c.host}:${c.port}`
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return null
|
|
275
|
+
} catch (e) {
|
|
276
|
+
this.log('扫描失败,回退到内置扫描: ' + e.message)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const base = pcIp.split('.').slice(0, 3).join('.')
|
|
280
|
+
const hosts = []
|
|
281
|
+
for (let i = 1; i <= 254; i++) hosts.push(`${base}.${i}`)
|
|
282
|
+
|
|
283
|
+
const CONCURRENCY = 32
|
|
284
|
+
for (let i = 0; i < hosts.length; i += CONCURRENCY) {
|
|
285
|
+
const batch = hosts.slice(i, i + CONCURRENCY)
|
|
286
|
+
const results = await Promise.all(batch.map(h => this._probe(h)))
|
|
287
|
+
const hit = results.find(Boolean)
|
|
288
|
+
if (hit) {
|
|
289
|
+
this.preferIp = hit
|
|
290
|
+
return `${hit}:5555`
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return null
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* 拿本机局域网 IP —— 用 Node 原生 os.networkInterfaces()。
|
|
298
|
+
*
|
|
299
|
+
* 为什么不用 PowerShell:在 Electron 主进程里 spawn powershell
|
|
300
|
+
* 有启动延迟(几百 ms)且容易被安全软件拦,而 os 模块是同步的、
|
|
301
|
+
* 零开销。实测 PowerShell 版在打包环境里经常返回空。
|
|
302
|
+
*
|
|
303
|
+
* 挑选顺序:
|
|
304
|
+
* 1. 优先 172.16~31 / 192.168 / 10 网段的私有地址(局域网)
|
|
305
|
+
* 2. 排除 127.x、169.254.x(回环 / 自动分配失败)
|
|
306
|
+
* 3. 排除 WSL / Hyper-V 虚拟网卡(它们的网段扫不到手机)
|
|
307
|
+
*/
|
|
308
|
+
/**
|
|
309
|
+
* 拿本机「WiFi 网卡」的局域网 IP。
|
|
310
|
+
*
|
|
311
|
+
* ── 为什么必须挑网卡而不是随便取一个 ──
|
|
312
|
+
*
|
|
313
|
+
* 这台机器上有 7 个 IPv4 地址:
|
|
314
|
+
* 10.8.1.58 本地连接(代理软件的虚拟网卡)
|
|
315
|
+
* 172.26.176.1 vEthernet (Default Switch) Hyper-V
|
|
316
|
+
* 172.25.16.1 vEthernet (WSL) WSL
|
|
317
|
+
* 169.254.229.164 Npcap Loopback 抓包工具
|
|
318
|
+
* 192.168.174.62 以太网 2 外接网卡
|
|
319
|
+
* 172.31.100.174 WLAN ← 手机在这个网段
|
|
320
|
+
* 127.0.0.1 Loopback
|
|
321
|
+
*
|
|
322
|
+
* 早期按「私有网段优先」挑,结果选中 10.8.1.58(代理网卡),
|
|
323
|
+
* 扫了整个 10.8.1.0/24 也找不到手机。
|
|
324
|
+
*
|
|
325
|
+
* ── 现在的策略 ──
|
|
326
|
+
*
|
|
327
|
+
* 按网卡名前缀分级打分:
|
|
328
|
+
* 1. WLAN / Wi-Fi / 无线 ← 最高优先级(手机连的就是这个)
|
|
329
|
+
* 2. 以太网 / Ethernet ← 有线也常见
|
|
330
|
+
* 3. 其他(排除虚拟网卡)
|
|
331
|
+
* 同分时优先 172.16~31 / 192.168 / 10 网段。
|
|
332
|
+
*/
|
|
333
|
+
_pcIp () {
|
|
334
|
+
const os = require('node:os')
|
|
335
|
+
const ifaces = os.networkInterfaces()
|
|
336
|
+
|
|
337
|
+
const VIRTUAL = /vEthernet|VirtualBox|VMware|WSL|Loopback|Npcap|Hyper-V|虚拟|TAP|代理/i
|
|
338
|
+
const WIFI_NAME = /^(WLAN|Wi-?Fi|无线)/i
|
|
339
|
+
const ETH_NAME = /^(以太网|Ethernet|本地连接 \d|Local Area)/i
|
|
340
|
+
|
|
341
|
+
const scored = []
|
|
342
|
+
for (const [name, addrs] of Object.entries(ifaces)) {
|
|
343
|
+
if (!addrs) continue
|
|
344
|
+
if (VIRTUAL.test(name)) continue
|
|
345
|
+
for (const a of addrs) {
|
|
346
|
+
if (a.family !== 'IPv4' && a.family !== 4) continue
|
|
347
|
+
const ip = a.address
|
|
348
|
+
if (ip.startsWith('127.') || ip.startsWith('169.254.')) continue
|
|
349
|
+
|
|
350
|
+
let score = 0
|
|
351
|
+
if (WIFI_NAME.test(name)) score += 100
|
|
352
|
+
else if (ETH_NAME.test(name)) score += 50
|
|
353
|
+
// 私有网段加分
|
|
354
|
+
if (/^172\.(1[6-9]|2\d|3[01])\./.test(ip)) score += 10
|
|
355
|
+
else if (/^192\.168\./.test(ip)) score += 8
|
|
356
|
+
else if (/^10\./.test(ip)) score += 3
|
|
357
|
+
|
|
358
|
+
scored.push({ name, ip, score })
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
scored.sort((a, b) => b.score - a.score)
|
|
363
|
+
if (scored.length) {
|
|
364
|
+
this.log(`本机 IP: ${scored[0].ip}(${scored[0].name})`)
|
|
365
|
+
return scored[0].ip
|
|
366
|
+
}
|
|
367
|
+
return null
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** 探测主机 5555 端口是否开着 */
|
|
371
|
+
_probe (host) {
|
|
372
|
+
return new Promise(resolve => {
|
|
373
|
+
const sock = new net.Socket()
|
|
374
|
+
let done = false
|
|
375
|
+
const finish = (v) => {
|
|
376
|
+
if (done) return
|
|
377
|
+
done = true
|
|
378
|
+
try { sock.destroy() } catch {}
|
|
379
|
+
resolve(v)
|
|
380
|
+
}
|
|
381
|
+
sock.setTimeout(600)
|
|
382
|
+
sock.once('connect', () => finish(host))
|
|
383
|
+
sock.once('timeout', () => finish(null))
|
|
384
|
+
sock.once('error', () => finish(null))
|
|
385
|
+
sock.connect(5555, host)
|
|
386
|
+
})
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
async _tryConnect (ip) {
|
|
390
|
+
const r = await this._run(['connect', `${ip}:5555`], 6000)
|
|
391
|
+
return r.out.includes('connected')
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
module.exports = { WifiAdbKeeper }
|