diff --git a/src/plugin.ts b/src/plugin.ts index 9e3415d..59c120d 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3,13 +3,14 @@ import http from 'http'; import { Application } from 'express'; import { CliArg, ISessionCapability, MockConfig, RecordConfig, RequestInfo, ReplayConfig, SniffConfig } from './types'; import _ from 'lodash'; -import { configureWifiProxy, isRealDevice } from './utils/adb'; +import { configureWifiProxy, isRealDevice, getGlobalProxyValue } from './utils/adb'; import { cleanUpProxyServer, sanitizeMockConfig, setupProxyServer } from './utils/proxy'; import proxyCache from './proxy-cache'; import logger from './logger'; import log from './logger'; export class AppiumInterceptorPlugin extends BasePlugin { + static executeMethodMap = { 'interceptor: addMock': { command: 'addMock', @@ -98,8 +99,10 @@ export class AppiumInterceptorPlugin extends BasePlugin { return response; } const realDevice = await isRealDevice(adb, deviceUDID); - const proxy = await setupProxyServer(sessionId, deviceUDID, realDevice); - await configureWifiProxy(adb, deviceUDID, realDevice, proxy); + const currentGlobalProxy = await getGlobalProxyValue(adb, deviceUDID) + + const proxy = await setupProxyServer(sessionId, deviceUDID, realDevice, currentGlobalProxy); + await configureWifiProxy(adb, deviceUDID, realDevice, proxy.options); proxyCache.add(sessionId, proxy); } log.info("Creating session for appium interceptor"); @@ -110,7 +113,7 @@ export class AppiumInterceptorPlugin extends BasePlugin { const proxy = proxyCache.get(sessionId); if (proxy) { const adb = driver.sessions[sessionId]?.adb; - await configureWifiProxy(adb, proxy.deviceUDID, false); + await configureWifiProxy(adb, proxy.deviceUDID, false, proxy.previousGlobalProxy); await cleanUpProxyServer(proxy); } return next(); @@ -122,7 +125,7 @@ export class AppiumInterceptorPlugin extends BasePlugin { const proxy = proxyCache.get(sessionId); if (proxy) { const adb = driver.sessions[sessionId]?.adb; - await configureWifiProxy(adb, proxy.deviceUDID, false); + await configureWifiProxy(adb, proxy.deviceUDID, false, proxy.previousGlobalProxy); await cleanUpProxyServer(proxy); } } diff --git a/src/proxy.ts b/src/proxy.ts index 0ec3ce3..b09eef5 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -29,6 +29,7 @@ export interface ProxyOptions { certificatePath: string; port: number; ip: string; + previousGlobalProxy?: ProxyOptions; } export class Proxy { @@ -55,7 +56,7 @@ export class Proxy { this._replayStarted = true; } - constructor(private readonly options: ProxyOptions) { + constructor(public readonly options: ProxyOptions) { this.httpProxy = new HttpProxy(); this.recordingManager = new RecordingManager(options); addDefaultMocks(this); @@ -81,6 +82,10 @@ export class Proxy { return this.options.certificatePath; } + public get previousGlobalProxy(): ProxyOptions | undefined { + return this.options.previousGlobalProxy ?? undefined + } + public async start(): Promise { if (this._started) return true; diff --git a/src/scripts/test-connection.ts b/src/scripts/test-connection.ts index 8a93c88..e929a8f 100644 --- a/src/scripts/test-connection.ts +++ b/src/scripts/test-connection.ts @@ -80,7 +80,7 @@ async function verifyDeviceConnection(adb: ADBInstance, udid: UDID) { const realDevice = await isRealDevice(adb, udid); const proxy = await setupProxyServer(uuid(), udid, realDevice); addMock(proxy); - await configureWifiProxy(adb, udid, realDevice, proxy); + await configureWifiProxy(adb, udid, realDevice, proxy.options); await openUrl(adb, udid, MOCK_BACKEND_URL); } diff --git a/src/utils/adb.ts b/src/utils/adb.ts index 83f625a..feda63f 100644 --- a/src/utils/adb.ts +++ b/src/utils/adb.ts @@ -1,5 +1,5 @@ import ADB from 'appium-adb'; -import { Proxy } from '../proxy'; +import { Proxy, ProxyOptions } from '../proxy'; export type ADBInstance = ADB; export type UDID = string; @@ -29,7 +29,7 @@ export async function configureWifiProxy( adb: ADBInstance, udid: UDID, realDevice: boolean, - proxy?: Proxy + proxy?: ProxyOptions ): Promise { try { const host = proxy ? `${proxy.ip}:${proxy.port}` : ':0'; @@ -51,6 +51,39 @@ export async function configureWifiProxy( } } +export async function getGlobalProxyValue( + adb: ADBInstance, + udid: UDID +): Promise { + try { + const proxy = await adbExecWithDevice(adb, udid, [ + 'shell', + 'settings', + 'get', + 'global', + 'http_proxy' + ]) + + if(proxy == ":0" || proxy == "null") { + return { + port: 0 + } as ProxyOptions + } + + const [ip, portStr] = proxy.split(":"); + const port = Number(portStr); + + return { + ip: ip, + port: port + } as ProxyOptions + + } catch (error: any) { + throw new Error(`Error get global proxy value ${udid}: ${error.message}`); + } +} + + export async function openUrl(adb: ADBInstance, udid: UDID, url: string) { await adbExecWithDevice(adb, udid, [ 'shell', diff --git a/src/utils/proxy.ts b/src/utils/proxy.ts index d9cf5c3..9fee412 100644 --- a/src/utils/proxy.ts +++ b/src/utils/proxy.ts @@ -9,7 +9,7 @@ import { } from '../types'; import _ from 'lodash'; import getPort from 'get-port'; -import { Proxy } from '../proxy'; +import { Proxy, ProxyOptions } from '../proxy'; import ip from 'ip'; import os from 'os'; import path from 'path'; @@ -112,12 +112,13 @@ export function modifyResponseBody(ctx: IContext, mockConfig: MockConfig) { export async function setupProxyServer( sessionId: string, deviceUDID: string, - isRealDevice: boolean + isRealDevice: boolean, + currentGlobalProxy?: ProxyOptions ) { const certificatePath = prepareCertificate(sessionId); const port = await getPort(); const _ip = isRealDevice ? 'localhost' : ip.address('public', 'ipv4'); - const proxy = new Proxy({ deviceUDID, sessionId, certificatePath, port, ip: _ip }); + const proxy = new Proxy({ deviceUDID, sessionId, certificatePath, port, ip: _ip, previousGlobalProxy: currentGlobalProxy }); await proxy.start(); if (!proxy.isStarted()) { throw new Error('Unable to start the proxy server');