Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 8 additions & 5 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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");
Expand All @@ -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();
Expand All @@ -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);
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface ProxyOptions {
certificatePath: string;
port: number;
ip: string;
previousGlobalProxy?: ProxyOptions;
}

export class Proxy {
Expand All @@ -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);
Expand All @@ -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<boolean> {
if (this._started) return true;

Expand Down
2 changes: 1 addition & 1 deletion src/scripts/test-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
37 changes: 35 additions & 2 deletions src/utils/adb.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,7 +29,7 @@ export async function configureWifiProxy(
adb: ADBInstance,
udid: UDID,
realDevice: boolean,
proxy?: Proxy
proxy?: ProxyOptions
): Promise<string> {
try {
const host = proxy ? `${proxy.ip}:${proxy.port}` : ':0';
Expand All @@ -51,6 +51,39 @@ export async function configureWifiProxy(
}
}

export async function getGlobalProxyValue(
adb: ADBInstance,
udid: UDID
): Promise<ProxyOptions> {
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',
Expand Down
7 changes: 4 additions & 3 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
Expand Down