Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Respect adb remote host setting while collecting CDP info #890

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions lib/helpers/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ async function webviewsFromProcs(
* @param socketName - The remote Unix socket name
* @param webviewDevtoolsPort - The local port number or null to apply
* autodetection
* @returns The local port number if the remote socket has been forwarded
* successfully or `null` otherwise
* @returns The host name and the port number to connect to if the
* remote socket has been forwarded successfully
* @throws {Error} If there was an error while allocating the local port
*/
async function allocateDevtoolsPort(
async function allocateDevtoolsChannel(
adb: any,
socketName: string,
webviewDevtoolsPort: number | null = null,
): Promise<number> {
): Promise<[string, number]> {
// socket names come with '@', but this should not be a part of the abstract
// remote port, so remove it
const remotePort = socketName.replace(/^@/, '');
Expand All @@ -252,7 +252,7 @@ async function allocateDevtoolsPort(
`the starting port number`,
);
}
return (await DEVTOOLS_PORT_ALLOCATION_GUARD(async () => {
const port = (await DEVTOOLS_PORT_ALLOCATION_GUARD(async () => {
let localPort: number;
try {
localPort = await findAPortNotInUse(startPort, endPort);
Expand All @@ -266,6 +266,7 @@ async function allocateDevtoolsPort(
await adb.adbExec(['forward', `tcp:${localPort}`, `localabstract:${remotePort}`]);
return localPort;
})) as number;
return [adb.adbHost ?? '127.0.0.1', port];
}

/**
Expand Down Expand Up @@ -322,21 +323,22 @@ async function collectWebviewsDetails(
for (const item of webviewsMapping) {
detailCollectors.push(
(async () => {
let localPort: number | undefined;
let port: number|undefined;
let host: string|undefined;
try {
localPort = await allocateDevtoolsPort(adb, item.proc, webviewDevtoolsPort);
[host, port] = await allocateDevtoolsChannel(adb, item.proc, webviewDevtoolsPort);
if (enableWebviewDetailsCollection) {
item.info = await cdpInfo(localPort);
item.info = await cdpInfo(host, port);
}
if (ensureWebviewsHavePages) {
item.pages = await cdpList(localPort);
item.pages = await cdpList(host, port);
}
} catch (e) {
logger.debug(e);
} finally {
if (localPort) {
if (port) {
try {
await adb.removePortForward(localPort);
await adb.removePortForward(port);
} catch (e) {
logger.debug(e);
}
Expand All @@ -350,20 +352,20 @@ async function collectWebviewsDetails(
}

// https://chromedevtools.github.io/devtools-protocol/
async function cdpList(localPort: number): Promise<object[]> {
async function cdpList(host: string, port: number): Promise<object[]> {
return (
await axios({
url: `http://127.0.0.1:${localPort}/json/list`,
url: `http://${host}:${port}/json/list`,
timeout: CDP_REQ_TIMEOUT,
})
).data;
}

// https://chromedevtools.github.io/devtools-protocol/
async function cdpInfo(localPort: number): Promise<object[]> {
async function cdpInfo(host: string, port: number): Promise<object[]> {
return (
await axios({
url: `http://127.0.0.1:${localPort}/json/version`,
url: `http://${host}:${port}/json/version`,
timeout: CDP_REQ_TIMEOUT,
})
).data;
Expand Down
Loading