-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathplugin.ts
More file actions
366 lines (326 loc) · 12.9 KB
/
plugin.ts
File metadata and controls
366 lines (326 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { BasePlugin } from 'appium/plugin';
import http from 'http';
import { Application } from 'express';
import _ from 'lodash';
import {
CliArg,
ISessionCapability,
MockConfig,
RecordConfig,
RequestInfo,
ReplayConfig,
SniffConfig,
} from './types';
import { DefaultPluginArgs, IPluginArgs } from './interfaces';
import {
configureWifiProxy,
isRealDevice,
getAdbReverseTunnels,
getCurrentWifiProxyConfig,
ADBInstance,
UDID,
} from './utils/adb';
import { cleanUpProxyServer, parseJson, sanitizeMockConfig, setupProxyServer } from './utils/proxy';
import proxyCache from './proxy-cache';
import log from './logger';
export class AppiumInterceptorPlugin extends BasePlugin {
private pluginArgs: IPluginArgs = Object.assign({}, DefaultPluginArgs);
static executeMethodMap = {
'interceptor: addMock': {
command: 'addMock',
params: { required: ['config'] },
},
'interceptor: removeMock': {
command: 'removeMock',
params: { required: ['id'] },
},
'interceptor: disableMock': {
command: 'disableMock',
params: { required: ['id'] },
},
'interceptor: enableMock': {
command: 'enableMock',
params: { required: ['id'] },
},
'interceptor: startListening': {
command: 'startListening',
params: { optional: ['config'] },
},
'interceptor: getInterceptedData': {
command: 'getInterceptedData',
params: { optional: ['id'] },
},
'interceptor: stopListening': {
command: 'stopListening',
params: { optional: ['id'] },
},
'interceptor: startRecording': {
command: 'startRecording',
params: { optional: ['config'] },
},
'interceptor: stopRecording': {
command: 'stopRecording',
params: { optional: ['id'] },
},
'interceptor: startReplaying': {
command: 'startReplaying',
params: { required: ['replayConfig'] },
},
'interceptor: stopReplaying': {
command: 'stopReplaying',
params: { optional: ['id'] },
},
'interceptor: getProxyState': {
command: 'getProxyState',
},
'interceptor: startProxy': {
command: 'startProxy',
},
'interceptor: stopProxy': {
command: 'stopProxy',
},
};
constructor(name: string, cliArgs: CliArg) {
super(name, cliArgs);
log.debug(`📱 Initializing plugin with CLI args: ${JSON.stringify(cliArgs)}`);
this.pluginArgs = Object.assign({}, DefaultPluginArgs, cliArgs as unknown as IPluginArgs);
}
/**
* Static method called by Appium at server startup.
* Can be used to extend the Express server with new routes.
*/
static async updateServer(expressApp: Application, httpServer: http.Server, cliArgs: CliArg) {}
async createSession(
next: () => any,
driver: any,
jwpDesCaps: any,
jwpReqCaps: any,
caps: ISessionCapability,
) {
const response = await next();
// Early return if session creation failed at driver level
if ((response.value && response.value.error) || response.error) {
log.warn('Session creation failed. Skipping interceptor setup.');
return response;
}
const mergedCaps = { ...caps.alwaysMatch, ..._.get(caps, 'firstMatch[0]', {}) };
const startProxyAutomatically = mergedCaps['appium:startProxyAutomatically'] === true;
const [sessionId, sessionCaps] = response.value;
const { deviceUDID, platformName } = sessionCaps;
const adb = driver.sessions[sessionId]?.adb;
// Platform validation (Android only)
if (platformName?.toLowerCase().trim() !== 'android') {
log.warn(
`Platform '${platformName}' is not supported. Appium interceptor plugin only supports Android. Skipping interceptor setup.`,
);
return response;
}
if (!adb) {
throw log.errorWithException(
`[${sessionId}] Unable to find ADB instance. API interception cannot be initialized.`,
);
}
if (startProxyAutomatically) {
log.debug(
`[${sessionId}] Capability 'startProxyAutomatically' is enabled. Initializing proxy setup...`,
);
await this.setupProxy(adb, sessionId, deviceUDID);
} else {
log.debug(
`[${sessionId}] Capability 'startProxyAutomatically' is disabled. Use command 'startProxy' to start proxy.`,
);
}
return response;
}
async deleteSession(next: () => any, driver: any, sessionId: string) {
log.debug(`[${sessionId}] Deleting session. Starting proxy cleanup...`);
const adb = driver.sessions[sessionId]?.adb;
await this.clearProxy(adb, sessionId);
return next();
}
async onUnexpectedShutdown(driver: any, cause: any) {
log.error(
`Unexpected shutdown detected (Cause: ${cause}). Cleaning up all active proxy sessions...`,
);
const sessions = Object.keys(driver.sessions || {});
for (const sessionId of sessions) {
const adb = driver.sessions[sessionId]?.adb;
await this.clearProxy(adb, sessionId);
}
}
async addMock(_next: any, driver: any, config: MockConfig) {
const proxy = this.getSessionProxy(driver.sessionId);
log.debug(`[${driver.sessionId}] Registering new mock rule (config=${JSON.stringify(config)})`);
sanitizeMockConfig(config);
return proxy.addMock(config);
}
async removeMock(_next: any, driver: any, id: string) {
const proxy = this.getSessionProxy(driver.sessionId);
log.debug(`[${driver.sessionId}] Removing mock rule with ID: ${id}`);
proxy.removeMock(id);
}
async disableMock(_next: any, driver: any, id: string) {
const proxy = this.getSessionProxy(driver.sessionId);
log.debug(`[${driver.sessionId}] Disabling mock rule with ID: ${id}`);
proxy.disableMock(id);
}
async enableMock(_next: any, driver: any, id: string) {
const proxy = this.getSessionProxy(driver.sessionId);
log.debug(`[${driver.sessionId}] Enabling mock rule with ID: ${id}`);
proxy.enableMock(id);
}
async startListening(_next: any, driver: any, config: SniffConfig): Promise<string> {
const proxy = this.getSessionProxy(driver.sessionId);
log.debug(`[${driver.sessionId}] Starting network listener (config=${JSON.stringify(config)})`);
return proxy.addSniffer(config);
}
async getInterceptedData(_next: any, driver: any, id: string): Promise<RequestInfo[]> {
const proxy = this.getSessionProxy(driver.sessionId);
log.debug(`[${driver.sessionId}] Fetching intercepted data for listener with ID: ${id}`);
return proxy.getInterceptedData(false, id);
}
async stopListening(_next: any, driver: any, id: string): Promise<RequestInfo[]> {
const proxy = this.getSessionProxy(driver.sessionId);
log.debug(`[${driver.sessionId}] Stopping network listener with ID: ${id}`);
return proxy.removeSniffer(false, id);
}
async startRecording(_next: any, driver: any, config: SniffConfig): Promise<string> {
const proxy = this.getSessionProxy(driver.sessionId);
log.debug(`[${driver.sessionId}] Starting traffic recording`);
return proxy.addSniffer(config);
}
async stopRecording(_next: any, driver: any, id: string): Promise<RecordConfig[]> {
const proxy = this.getSessionProxy(driver.sessionId);
log.debug(`[${driver.sessionId}] Stopping traffic recording for listener with ID: ${id}`);
return proxy.removeSniffer(true, id);
}
async startReplaying(_next: any, driver: any, replayConfig: ReplayConfig) {
const proxy = this.getSessionProxy(driver.sessionId);
log.debug(`[${driver.sessionId}] Starting traffic replay`);
proxy.startReplaying();
return proxy.getRecordingManager().replayTraffic(replayConfig);
}
async stopReplaying(_next: any, driver: any, id: string) {
const proxy = this.getSessionProxy(driver.sessionId);
log.debug(`[${driver.sessionId}] Stopping traffic replay`);
proxy.getRecordingManager().stopReplay(id);
}
async execute(next: any, driver: any, script: string, args: any) {
return await this.executeMethod(next, driver, script, args);
}
async startProxy(_next: any, driver: any) {
await this.setupProxy(driver.adb, driver.sessionId, driver.adb?.curDeviceId);
}
async stopProxy(_next: any, driver: any) {
await this.clearProxy(driver.adb, driver.sessionId);
}
private getSessionProxy(sessionId: string) {
log.debug(`getSessionProxy(sessionId=${sessionId})`);
const proxy = proxyCache.get(sessionId);
if (!proxy) {
throw log.errorWithException(
`No active proxy found for session ${sessionId}. Please call 'startProxy' first.`,
);
}
return proxy;
}
private async setupProxy(adb: ADBInstance, sessionId: string, deviceUDID: UDID) {
log.debug(`setupProxy(sessionId=${sessionId}, deviceUDID:${deviceUDID})`);
if (proxyCache.get(sessionId)) {
log.warn(`[${sessionId}] A proxy is already active for this session. Skipping setup.`);
return;
}
if (!adb) throw log.errorWithException('Proxy setup failed: ADB instance is missing.');
if (!sessionId) throw log.errorWithException('Proxy setup failed: Session ID is missing.');
if (!deviceUDID) throw log.errorWithException('Proxy setup failed: Device UDID is missing.');
try {
const realDevice = await isRealDevice(adb, deviceUDID);
const currentGlobalProxy = await getCurrentWifiProxyConfig(adb, deviceUDID);
const whitelistedDomains = ((domains) =>
Array.isArray(domains) ? domains : typeof domains === 'string' ? [domains] : [])(
typeof this.pluginArgs.whitelisteddomains === 'string'
? parseJson(this.pluginArgs.whitelisteddomains)
: this.pluginArgs.whitelisteddomains,
);
const blacklistedDomains = ((domains) =>
Array.isArray(domains) ? domains : typeof domains === 'string' ? [domains] : [])(
typeof this.pluginArgs.blacklisteddomains === 'string'
? this.pluginArgs.blacklisteddomains
: parseJson(this.pluginArgs.blacklisteddomains),
);
const upstreamProxy =
typeof this.pluginArgs.upstreamproxy === 'string' &&
this.pluginArgs.upstreamproxy.trim().length > 0
? this.pluginArgs.upstreamproxy.trim()
: null;
const proxy = await setupProxyServer(
sessionId,
deviceUDID,
realDevice,
this.pluginArgs.certdirectory,
currentGlobalProxy,
whitelistedDomains,
blacklistedDomains,
upstreamProxy,
);
await configureWifiProxy(adb, deviceUDID, realDevice, proxy.options);
proxyCache.add(sessionId, proxy);
log.debug(
`[${sessionId}] Proxy successfully registered (ip=${proxy.options.ip}, port=${proxy.options.port}).`,
);
} catch (err: any) {
throw log.errorWithException(`[${sessionId}] Failed to initialize proxy: ${err.message}`);
}
}
private async clearProxy(adb: ADBInstance, sessionId: string) {
const proxy = proxyCache.get(sessionId);
if (!proxy) {
log.debug(`[${sessionId}] No proxy registered for this session. Nothing to clear.`);
return;
}
log.debug(`[${sessionId}] Reverting device settings and cleaning up proxy resources...`);
try {
// Revert WiFi settings to previous state or off
await configureWifiProxy(adb, proxy.options.deviceUDID, false, proxy.previousGlobalProxy);
// Shutdown the local proxy server
await cleanUpProxyServer(proxy);
proxyCache.remove(sessionId);
log.debug(`[${sessionId}] Proxy cleanup successful.`);
} catch (err: any) {
// Log the error but do not block the session deletion process
log.error(`[${sessionId}] Critical error during proxy cleanup: ${err.message}`);
}
}
/**
* Aggregates the current health and configuration state of the proxy system.
* * This method performs a dual-layer diagnostic:
* 1. Host Layer: Checks if the proxy instance exists in the cache and verifies its execution status.
* 2. Transport Layer: Queries the physical device via ADB to list active reverse tunnels.
* * It is primarily used to determine if a connectivity issue originates from the
* Node.js server (Host) or the ADB bridge/USB connection (Device).
*
* @param next - The next middleware or handler in the execution chain (if applicable).
* @param driver - The Appium driver instance containing the ADB controller and session ID.
* @returns A Promise resolving to a JSON string representing the combined state of the proxy and ADB tunnels.
*/
async getProxyState(next: any, driver: any): Promise<string> {
const adb = driver.adb;
const udid = adb.curDeviceId;
const adbReverseTunnels = await getAdbReverseTunnels(adb, udid);
const proxy = proxyCache.get(driver.sessionId);
const proxyServerStatus = {
isRegistered: !!proxy,
isStarted: proxy ? proxy.isStarted() : false,
...proxy?.options,
};
const adbDeviceStatus = {
udid: udid,
activeAdbReverseTunnels: adbReverseTunnels,
};
const proxyState = {
proxyServerStatus: proxyServerStatus,
adbDeviceStatus: adbDeviceStatus,
};
return JSON.stringify(proxyState);
}
}