Skip to content

Commit 1305816

Browse files
dimakozindmkozin
andauthored
fix: saving the previous proxy state (#75)
* Set previous proxy settings * Fixes * old proxy in proxy object --------- Co-authored-by: dmkozin <[email protected]>
1 parent 7ed5e05 commit 1305816

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

src/plugin.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import http from 'http';
33
import { Application } from 'express';
44
import { CliArg, ISessionCapability, MockConfig, RecordConfig, RequestInfo, ReplayConfig, SniffConfig } from './types';
55
import _ from 'lodash';
6-
import { configureWifiProxy, isRealDevice } from './utils/adb';
6+
import { configureWifiProxy, isRealDevice, getGlobalProxyValue } from './utils/adb';
77
import { cleanUpProxyServer, sanitizeMockConfig, setupProxyServer } from './utils/proxy';
88
import proxyCache from './proxy-cache';
99
import logger from './logger';
1010
import log from './logger';
1111

1212
export class AppiumInterceptorPlugin extends BasePlugin {
13+
1314
static executeMethodMap = {
1415
'interceptor: addMock': {
1516
command: 'addMock',
@@ -98,8 +99,10 @@ export class AppiumInterceptorPlugin extends BasePlugin {
9899
return response;
99100
}
100101
const realDevice = await isRealDevice(adb, deviceUDID);
101-
const proxy = await setupProxyServer(sessionId, deviceUDID, realDevice);
102-
await configureWifiProxy(adb, deviceUDID, realDevice, proxy);
102+
const currentGlobalProxy = await getGlobalProxyValue(adb, deviceUDID)
103+
104+
const proxy = await setupProxyServer(sessionId, deviceUDID, realDevice, currentGlobalProxy);
105+
await configureWifiProxy(adb, deviceUDID, realDevice, proxy.options);
103106
proxyCache.add(sessionId, proxy);
104107
}
105108
log.info("Creating session for appium interceptor");
@@ -110,7 +113,7 @@ export class AppiumInterceptorPlugin extends BasePlugin {
110113
const proxy = proxyCache.get(sessionId);
111114
if (proxy) {
112115
const adb = driver.sessions[sessionId]?.adb;
113-
await configureWifiProxy(adb, proxy.deviceUDID, false);
116+
await configureWifiProxy(adb, proxy.deviceUDID, false, proxy.previousGlobalProxy);
114117
await cleanUpProxyServer(proxy);
115118
}
116119
return next();
@@ -122,7 +125,7 @@ export class AppiumInterceptorPlugin extends BasePlugin {
122125
const proxy = proxyCache.get(sessionId);
123126
if (proxy) {
124127
const adb = driver.sessions[sessionId]?.adb;
125-
await configureWifiProxy(adb, proxy.deviceUDID, false);
128+
await configureWifiProxy(adb, proxy.deviceUDID, false, proxy.previousGlobalProxy);
126129
await cleanUpProxyServer(proxy);
127130
}
128131
}

src/proxy.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface ProxyOptions {
2929
certificatePath: string;
3030
port: number;
3131
ip: string;
32+
previousGlobalProxy?: ProxyOptions;
3233
}
3334

3435
export class Proxy {
@@ -55,7 +56,7 @@ export class Proxy {
5556
this._replayStarted = true;
5657
}
5758

58-
constructor(private readonly options: ProxyOptions) {
59+
constructor(public readonly options: ProxyOptions) {
5960
this.httpProxy = new HttpProxy();
6061
this.recordingManager = new RecordingManager(options);
6162
addDefaultMocks(this);
@@ -81,6 +82,10 @@ export class Proxy {
8182
return this.options.certificatePath;
8283
}
8384

85+
public get previousGlobalProxy(): ProxyOptions | undefined {
86+
return this.options.previousGlobalProxy ?? undefined
87+
}
88+
8489
public async start(): Promise<boolean> {
8590
if (this._started) return true;
8691

src/scripts/test-connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ async function verifyDeviceConnection(adb: ADBInstance, udid: UDID) {
8080
const realDevice = await isRealDevice(adb, udid);
8181
const proxy = await setupProxyServer(uuid(), udid, realDevice);
8282
addMock(proxy);
83-
await configureWifiProxy(adb, udid, realDevice, proxy);
83+
await configureWifiProxy(adb, udid, realDevice, proxy.options);
8484
await openUrl(adb, udid, MOCK_BACKEND_URL);
8585
}
8686

src/utils/adb.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ADB from 'appium-adb';
2-
import { Proxy } from '../proxy';
2+
import { Proxy, ProxyOptions } from '../proxy';
33

44
export type ADBInstance = ADB;
55
export type UDID = string;
@@ -29,7 +29,7 @@ export async function configureWifiProxy(
2929
adb: ADBInstance,
3030
udid: UDID,
3131
realDevice: boolean,
32-
proxy?: Proxy
32+
proxy?: ProxyOptions
3333
): Promise<string> {
3434
try {
3535
const host = proxy ? `${proxy.ip}:${proxy.port}` : ':0';
@@ -51,6 +51,39 @@ export async function configureWifiProxy(
5151
}
5252
}
5353

54+
export async function getGlobalProxyValue(
55+
adb: ADBInstance,
56+
udid: UDID
57+
): Promise<ProxyOptions> {
58+
try {
59+
const proxy = await adbExecWithDevice(adb, udid, [
60+
'shell',
61+
'settings',
62+
'get',
63+
'global',
64+
'http_proxy'
65+
])
66+
67+
if(proxy == ":0" || proxy == "null") {
68+
return {
69+
port: 0
70+
} as ProxyOptions
71+
}
72+
73+
const [ip, portStr] = proxy.split(":");
74+
const port = Number(portStr);
75+
76+
return {
77+
ip: ip,
78+
port: port
79+
} as ProxyOptions
80+
81+
} catch (error: any) {
82+
throw new Error(`Error get global proxy value ${udid}: ${error.message}`);
83+
}
84+
}
85+
86+
5487
export async function openUrl(adb: ADBInstance, udid: UDID, url: string) {
5588
await adbExecWithDevice(adb, udid, [
5689
'shell',

src/utils/proxy.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '../types';
1010
import _ from 'lodash';
1111
import getPort from 'get-port';
12-
import { Proxy } from '../proxy';
12+
import { Proxy, ProxyOptions } from '../proxy';
1313
import ip from 'ip';
1414
import os from 'os';
1515
import path from 'path';
@@ -112,12 +112,13 @@ export function modifyResponseBody(ctx: IContext, mockConfig: MockConfig) {
112112
export async function setupProxyServer(
113113
sessionId: string,
114114
deviceUDID: string,
115-
isRealDevice: boolean
115+
isRealDevice: boolean,
116+
currentGlobalProxy?: ProxyOptions
116117
) {
117118
const certificatePath = prepareCertificate(sessionId);
118119
const port = await getPort();
119120
const _ip = isRealDevice ? 'localhost' : ip.address('public', 'ipv4');
120-
const proxy = new Proxy({ deviceUDID, sessionId, certificatePath, port, ip: _ip });
121+
const proxy = new Proxy({ deviceUDID, sessionId, certificatePath, port, ip: _ip, previousGlobalProxy: currentGlobalProxy });
121122
await proxy.start();
122123
if (!proxy.isStarted()) {
123124
throw new Error('Unable to start the proxy server');

0 commit comments

Comments
 (0)