@@ -162,16 +162,50 @@ class ADBModule {
162162 this . currentDeviceMap . set ( sn , currentDevice )
163163 this . cachedDeviceMap . set ( sn , currentDevice )
164164 // 记录发送update http请求需要的ip和端口
165+ const remote2local = { }
166+ remote2local [ currentDevice . upForwardPortPair [ 1 ] ] = currentDevice . upForwardPortPair [ 0 ]
165167 await this . _writeClientLogFile ( {
166168 sn,
167169 ip : `127.0.0.1` ,
168- port : currentDevice . upForwardPortPair [ 0 ]
170+ port : currentDevice . upForwardPortPair [ 0 ] ,
171+ remote2local
169172 } )
170173 debuglog ( `onDeviceAdded():(${ sn } ) end` )
171174 // 增加设备连接检测
172175 this . onCheckDeviceReverse ( event )
173176 }
174177
178+ async getForwardPort ( client , remotePort ) {
179+ let remote2local = client . remote2local
180+ if ( ! remote2local ) return client . port
181+
182+ let port = remote2local [ remotePort ]
183+ if ( ! port ) {
184+ // addForwardPort
185+ port = await this . addForwardPort ( client , remotePort )
186+ }
187+ return port
188+ }
189+
190+ async addForwardPort ( client , remotePort ) {
191+ const port = this . _getNextLocalForwardPort ( )
192+ debuglog ( `addForwardPort():(${ client . sn } ) (local port: ${ port } , remote port: ${ remotePort } ) start` )
193+ const upForwardResult = await this . establishADBProxyLink (
194+ 'forward' ,
195+ [ client . sn ] . concat ( [ port , remotePort ] )
196+ )
197+ if ( upForwardResult . err ) {
198+ colorconsole . error (
199+ `### App Server ### addForwardPort(): (${ client . sn } )建立adb forward失败(local port: ${ port } , remote port: ${ remotePort } ) `
200+ )
201+ return
202+ }
203+ client . remote2local [ remotePort ] = port
204+ await this . _writeClientLogFile ( client )
205+ debuglog ( `addForwardPort():(${ client . sn } ) (local port: ${ port } , remote port: ${ remotePort } ) end` )
206+ return port
207+ }
208+
175209 /**
176210 * 设备连接检测,用于检查当前窗口端口是否连接手机调试器
177211 */
0 commit comments