From 114fd5dfcb95b03569244613e2113c04585b5df9 Mon Sep 17 00:00:00 2001 From: qufangtao Date: Tue, 16 Dec 2025 15:57:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=89=8B=E6=9C=BA?= =?UTF-8?q?=E7=AB=AF=E8=B0=83=E8=AF=95=E5=99=A8=E5=88=87=E6=8D=A2=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E3=80=81=E5=8D=A1=E7=89=87=E6=A8=A1=E5=BC=8F=E5=90=8E?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: qufangtao --- packages/hap-debugger/src/adb/module.js | 36 ++++++++++++++++++++- packages/hap-debugger/src/router/routes.js | 25 ++++++++------ packages/hap-debugger/src/router/service.js | 9 +++--- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/packages/hap-debugger/src/adb/module.js b/packages/hap-debugger/src/adb/module.js index 7a98e0ab..5463028e 100644 --- a/packages/hap-debugger/src/adb/module.js +++ b/packages/hap-debugger/src/adb/module.js @@ -162,16 +162,50 @@ class ADBModule { this.currentDeviceMap.set(sn, currentDevice) this.cachedDeviceMap.set(sn, currentDevice) // 记录发送update http请求需要的ip和端口 + const remote2local = {} + remote2local[currentDevice.upForwardPortPair[1]] = currentDevice.upForwardPortPair[0] await this._writeClientLogFile({ sn, ip: `127.0.0.1`, - port: currentDevice.upForwardPortPair[0] + port: currentDevice.upForwardPortPair[0], + remote2local }) debuglog(`onDeviceAdded():(${sn}) end`) // 增加设备连接检测 this.onCheckDeviceReverse(event) } + async getForwardPort(client, remotePort) { + let remote2local = client.remote2local + if (!remote2local) return client.port + + let port = remote2local[remotePort] + if (!port) { + // addForwardPort + port = await this.addForwardPort(client, remotePort) + } + return port + } + + async addForwardPort(client, remotePort) { + const port = this._getNextLocalForwardPort() + debuglog(`addForwardPort():(${client.sn}) (local port: ${port}, remote port: ${remotePort}) start`) + const upForwardResult = await this.establishADBProxyLink( + 'forward', + [client.sn].concat([port, remotePort]) + ) + if (upForwardResult.err) { + colorconsole.error( + `### App Server ### addForwardPort(): (${client.sn})建立adb forward失败(local port: ${port}, remote port: ${remotePort}) ` + ) + return + } + client.remote2local[remotePort] = port + await this._writeClientLogFile(client) + debuglog(`addForwardPort():(${client.sn}) (local port: ${port}, remote port: ${remotePort}) end`) + return port + } + /** * 设备连接检测,用于检查当前窗口端口是否连接手机调试器 */ diff --git a/packages/hap-debugger/src/router/routes.js b/packages/hap-debugger/src/router/routes.js index b6436a16..36dd06a5 100644 --- a/packages/hap-debugger/src/router/routes.js +++ b/packages/hap-debugger/src/router/routes.js @@ -116,19 +116,25 @@ export async function searchSn(context, next) { const recordData = getRecords(clientRecordPath) const clients = getProjectClients(recordData) if (clients.length > 0) { + const remotePort = context.request.query.port colorconsole.log('### App Loader ### 通知设备开始下发SN') - clients.forEach(function (client) { + for (const client of clients) { // 匹配hap-toolkit-client-records.json表里已存的通过ADB连接的设备信息 if (client.ip === '127.0.0.1') { - // 仅向ADB现在连接的且没有SN的设备下发SN请求 - getDeviceInfo(client, (err, data) => { - if (err || data.sn) { - return - } - callDeviceWithOwnSn(client) - }) + if (remotePort) { + const port = await context.adbDebugger.getForwardPort(client, remotePort) + callDeviceWithOwnSn(client, port) + } else { + // 仅向ADB现在连接的且没有SN的设备下发SN请求 + getDeviceInfo(client, (err, data) => { + if (err || data.sn) { + return + } + callDeviceWithOwnSn(client) + }) + } } - }) + } } } else { await next() @@ -154,6 +160,7 @@ export async function startDebug(context, next) { // ADB调试模式 if (linkMode === LINK_MODE.ADB) { const { localWsPort, err } = await context.adbDebugger.forwardForWsChannel(sn, devicePort) + console.error(`startDebug(): adb forward 端口映射: sn=${sn},devicePort=${devicePort}`) if (err) { console.error(`startDebug(): adb forward 端口映射失败: ${err.message}`) trackDebug(eventAlias.h_forward_err) diff --git a/packages/hap-debugger/src/router/service.js b/packages/hap-debugger/src/router/service.js index 1841411e..edd4510b 100644 --- a/packages/hap-debugger/src/router/service.js +++ b/packages/hap-debugger/src/router/service.js @@ -66,10 +66,11 @@ export function getDebugInfoFromRequest(request) { * 迫于device目前不支持POST,暂时把sn放在header中 * @param client 本地存储的设备信息(ip、port、sn) */ -export function callDeviceWithOwnSn(client) { +export function callDeviceWithOwnSn(client, port) { + if (!port) port = client.port const options = { host: client.ip, - port: client.port, + port, path: '/reportsn', headers: { 'device-serial-number': client.sn @@ -78,11 +79,11 @@ export function callDeviceWithOwnSn(client) { } const req = http .request(options, () => { - colorconsole.log(`### App Server ### 通知手机设备(${client.sn})下发SN成功`) + colorconsole.log(`### App Server ### 通知手机设备(${client.sn}) port ${port} 下发SN成功`) }) .on('error', (err) => { colorconsole.warn( - `### App Server ### 通知手机设备(${client.sn})下发SN失败 错误信息: ${err.message}` + `### App Server ### 通知手机设备(${client.sn}) port ${port} 下发SN失败 错误信息: ${err.message}` ) }) .on('timeout', function () {