From 46ffce7adc5cb5d0edb052f652e9e0590b612429 Mon Sep 17 00:00:00 2001 From: dmkozin Date: Thu, 2 Oct 2025 21:01:35 +0300 Subject: [PATCH 1/3] Set previous proxy settings --- src/plugin.ts | 17 ++++++++++++++--- src/proxy.ts | 2 +- src/utils/adb.ts | 40 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index 9e3415d..eb9c22f 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -3,13 +3,17 @@ 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'; +import { ProxyOptions } from './proxy'; export class AppiumInterceptorPlugin extends BasePlugin { + + private previousGlobalProxy: ProxyOptions = {} as ProxyOptions + static executeMethodMap = { 'interceptor: addMock': { command: 'addMock', @@ -98,8 +102,13 @@ export class AppiumInterceptorPlugin extends BasePlugin { return response; } const realDevice = await isRealDevice(adb, deviceUDID); + + this.previousGlobalProxy = await getGlobalProxyValue(adb, deviceUDID) + log.info(`Current proxy: ${this.previousGlobalProxy}}`) + + const proxy = await setupProxyServer(sessionId, deviceUDID, realDevice); - await configureWifiProxy(adb, deviceUDID, realDevice, proxy); + await configureWifiProxy(adb, deviceUDID, realDevice, proxy.options); proxyCache.add(sessionId, proxy); } log.info("Creating session for appium interceptor"); @@ -110,7 +119,9 @@ export class AppiumInterceptorPlugin extends BasePlugin { const proxy = proxyCache.get(sessionId); if (proxy) { const adb = driver.sessions[sessionId]?.adb; - await configureWifiProxy(adb, proxy.deviceUDID, false); + + log.info(`Set previous proxy settings`) + await configureWifiProxy(adb, proxy.deviceUDID, false, this.previousGlobalProxy); await cleanUpProxyServer(proxy); } return next(); diff --git a/src/proxy.ts b/src/proxy.ts index 0ec3ce3..b2fd4c9 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -55,7 +55,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); diff --git a/src/utils/adb.ts b/src/utils/adb.ts index 83f625a..c5fc1ed 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,10 +29,10 @@ export async function configureWifiProxy( adb: ADBInstance, udid: UDID, realDevice: boolean, - proxy?: Proxy + proxy?: ProxyOptions ): Promise { try { - const host = proxy ? `${proxy.ip}:${proxy.port}` : ':0'; + const host = proxy?.ip ? `${proxy.ip}:${proxy.port}` : ':0'; if (realDevice && proxy) { await adbExecWithDevice(adb, udid, ['reverse', `tcp:${proxy.port}`, `tcp:${proxy.port}`]); @@ -51,6 +51,40 @@ 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', From 2e7d49e960c5b26c0f24439dedfd4f0af47d4ae5 Mon Sep 17 00:00:00 2001 From: dmkozin Date: Thu, 2 Oct 2025 21:54:17 +0300 Subject: [PATCH 2/3] Fixes --- src/plugin.ts | 2 +- src/scripts/test-connection.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index eb9c22f..bff46af 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -133,7 +133,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, this.previousGlobalProxy); await cleanUpProxyServer(proxy); } } 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); } From 292c66d7d0bd26e2d38521b15346a96b2b533344 Mon Sep 17 00:00:00 2001 From: dmkozin Date: Tue, 14 Oct 2025 14:54:51 +0300 Subject: [PATCH 3/3] old proxy in proxy object --- src/plugin.ts | 16 ++++------------ src/proxy.ts | 5 +++++ src/utils/adb.ts | 3 +-- src/utils/proxy.ts | 7 ++++--- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index bff46af..59c120d 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -8,12 +8,9 @@ import { cleanUpProxyServer, sanitizeMockConfig, setupProxyServer } from './util import proxyCache from './proxy-cache'; import logger from './logger'; import log from './logger'; -import { ProxyOptions } from './proxy'; export class AppiumInterceptorPlugin extends BasePlugin { - private previousGlobalProxy: ProxyOptions = {} as ProxyOptions - static executeMethodMap = { 'interceptor: addMock': { command: 'addMock', @@ -102,12 +99,9 @@ export class AppiumInterceptorPlugin extends BasePlugin { return response; } const realDevice = await isRealDevice(adb, deviceUDID); + const currentGlobalProxy = await getGlobalProxyValue(adb, deviceUDID) - this.previousGlobalProxy = await getGlobalProxyValue(adb, deviceUDID) - log.info(`Current proxy: ${this.previousGlobalProxy}}`) - - - const proxy = await setupProxyServer(sessionId, deviceUDID, realDevice); + const proxy = await setupProxyServer(sessionId, deviceUDID, realDevice, currentGlobalProxy); await configureWifiProxy(adb, deviceUDID, realDevice, proxy.options); proxyCache.add(sessionId, proxy); } @@ -119,9 +113,7 @@ export class AppiumInterceptorPlugin extends BasePlugin { const proxy = proxyCache.get(sessionId); if (proxy) { const adb = driver.sessions[sessionId]?.adb; - - log.info(`Set previous proxy settings`) - await configureWifiProxy(adb, proxy.deviceUDID, false, this.previousGlobalProxy); + await configureWifiProxy(adb, proxy.deviceUDID, false, proxy.previousGlobalProxy); await cleanUpProxyServer(proxy); } return next(); @@ -133,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, this.previousGlobalProxy); + await configureWifiProxy(adb, proxy.deviceUDID, false, proxy.previousGlobalProxy); await cleanUpProxyServer(proxy); } } diff --git a/src/proxy.ts b/src/proxy.ts index b2fd4c9..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 { @@ -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/utils/adb.ts b/src/utils/adb.ts index c5fc1ed..feda63f 100644 --- a/src/utils/adb.ts +++ b/src/utils/adb.ts @@ -32,7 +32,7 @@ export async function configureWifiProxy( proxy?: ProxyOptions ): Promise { try { - const host = proxy?.ip ? `${proxy.ip}:${proxy.port}` : ':0'; + const host = proxy ? `${proxy.ip}:${proxy.port}` : ':0'; if (realDevice && proxy) { await adbExecWithDevice(adb, udid, ['reverse', `tcp:${proxy.port}`, `tcp:${proxy.port}`]); @@ -56,7 +56,6 @@ export async function getGlobalProxyValue( udid: UDID ): Promise { try { - const proxy = await adbExecWithDevice(adb, udid, [ 'shell', 'settings', 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');