Repository navigation
Expand file tree
/
Copy pathinterceptor.js
More file actions
274 lines (228 loc) · 7.48 KB
/
Copy pathinterceptor.js
File metadata and controls
274 lines (228 loc) · 7.48 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
const puppeteer = require("puppeteer");
const fs = require("fs");
const path = require("path");
class RequestInterceptor {
constructor() {
this.requests = [];
this.responses = [];
this.outputFile = "intercepted_requests.json";
this.logFile = "intercepted_requests.log";
this.browser = null;
this.page = null;
}
async init() {
console.log("🚀 Starting Puppeteer Request Interceptor...");
// Initialize log file
this.initLogFile();
// Launch browser with GUI (non-headless)
this.browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
args: [
"--start-maximized",
"--disable-web-security",
"--disable-features=VizDisplayCompositor",
],
});
this.page = await this.browser.newPage();
// Set up request/response interceptors
await this.setupInterceptors();
console.log("✅ Browser launched and interceptors set up!");
console.log("📝 All HTTP requests will be logged to:", this.outputFile);
console.log("📄 Real-time logs will be written to:", this.logFile);
console.log("🌐 Navigate to any website and interact with it...");
console.log("⏹️ Press Ctrl+C to stop and save the logs");
}
initLogFile() {
const logHeader = `[${new Date().toISOString()}] ===== Puppeteer Request Interceptor Started =====\n`;
fs.writeFileSync(this.logFile, logHeader);
}
writeToLog(message) {
const timestamp = new Date().toISOString();
const logEntry = `[${timestamp}] ${message}\n`;
fs.appendFileSync(this.logFile, logEntry);
}
async setupInterceptors() {
// Intercept requests
await this.page.setRequestInterception(true);
this.page.on("request", (request) => {
const requestData = {
timestamp: new Date().toISOString(),
type: "REQUEST",
method: request.method(),
url: request.url(),
headers: request.headers(),
postData: request.postData(),
resourceType: request.resourceType(),
};
this.requests.push(requestData);
// Log to console for real-time monitoring
console.log(`📤 ${request.method()} ${request.url()}`);
if (request.postData()) {
console.log(` 📝 POST Data: ${request.postData()}`);
}
// Write to log file
this.writeToLog(`REQUEST ${request.method()} ${request.url()}`);
if (request.postData()) {
this.writeToLog(`POST_DATA ${request.postData()}`);
}
// Continue with the request
request.continue();
});
// Intercept responses
this.page.on("response", async (response) => {
const request = response.request();
let responseBody = null;
try {
// Only capture response body for certain content types
const contentType = response.headers()["content-type"] || "";
if (
contentType.includes("application/json") ||
contentType.includes("text/") ||
contentType.includes("application/x-www-form-urlencoded")
) {
responseBody = await response.text();
}
} catch (error) {
responseBody = `Error reading response: ${error.message}`;
}
const responseData = {
timestamp: new Date().toISOString(),
type: "RESPONSE",
method: request.method(),
url: response.url(),
status: response.status(),
statusText: response.statusText(),
headers: response.headers(),
body: responseBody,
fromCache: response.fromCache(),
};
this.responses.push(responseData);
// Log to console for real-time monitoring
console.log(
`📥 ${response.status()} ${request.method()} ${response.url()}`,
);
// Write to log file
this.writeToLog(
`RESPONSE ${response.status()} ${request.method()} ${response.url()}`,
);
if (responseBody && responseBody.length < 500) {
this.writeToLog(`RESPONSE_BODY ${responseBody}`);
}
});
}
async navigateTo(url) {
if (!url.startsWith("http")) {
url = "https://" + url;
}
console.log(`🔗 Navigating to: ${url}`);
await this.page.goto(url, { waitUntil: "networkidle2" });
}
async saveInterceptedData() {
const allData = {
session: {
startTime: new Date().toISOString(),
totalRequests: this.requests.length,
totalResponses: this.responses.length,
},
requests: this.requests,
responses: this.responses,
combined: this.combineRequestsAndResponses(),
};
// Save to JSON file
fs.writeFileSync(this.outputFile, JSON.stringify(allData, null, 2));
// Also create a simplified curl commands file
this.generateCurlCommands();
// Write session end to log file
this.writeToLog(
`===== Session Ended - ${this.requests.length} requests, ${this.responses.length} responses =====`,
);
console.log(
`\n💾 Saved ${this.requests.length} requests and ${this.responses.length} responses to ${this.outputFile}`,
);
console.log(`📄 Real-time log saved to ${this.logFile}`);
console.log(`📋 Curl commands saved to curl_commands.sh`);
}
combineRequestsAndResponses() {
const combined = [];
this.requests.forEach((req) => {
const matchingResponse = this.responses.find(
(res) =>
res.url === req.url &&
res.method === req.method &&
new Date(res.timestamp) >= new Date(req.timestamp),
);
combined.push({
request: req,
response: matchingResponse || null,
});
});
return combined;
}
generateCurlCommands() {
const curlCommands = [];
this.requests.forEach((req) => {
if (
req.resourceType === "document" ||
req.resourceType === "xhr" ||
req.resourceType === "fetch"
) {
let curl = `curl -X ${req.method} '${req.url}'`;
// Add headers
Object.entries(req.headers).forEach(([key, value]) => {
if (!["host", "content-length"].includes(key.toLowerCase())) {
curl += ` \\\n -H '${key}: ${value}'`;
}
});
// Add POST data
if (req.postData) {
curl += ` \\\n -d '${req.postData}'`;
}
curlCommands.push(curl);
}
});
fs.writeFileSync("curl_commands.sh", curlCommands.join("\n\n") + "\n");
}
async close() {
if (this.browser) {
await this.browser.close();
}
}
}
// Main execution
async function main() {
const interceptor = new RequestInterceptor();
// Handle graceful shutdown
process.on("SIGINT", async () => {
console.log("\n🛑 Stopping interceptor...");
await interceptor.saveInterceptedData();
await interceptor.close();
console.log("👋 Goodbye!");
process.exit(0);
});
try {
await interceptor.init();
// Optional: Navigate to a starting URL
const startUrl = process.argv[2];
if (startUrl) {
await interceptor.navigateTo(startUrl);
} else {
// Navigate to a blank page to start
await interceptor.page.goto("about:blank");
console.log(
"💡 Tip: You can also run with a URL: node interceptor.js example.com",
);
}
// Keep the script running until user stops it
console.log(
"🔄 Script is running... Interact with the browser and press Ctrl+C when done.",
);
// Prevent the script from exiting
await new Promise(() => {});
} catch (error) {
console.error("❌ Error:", error);
await interceptor.close();
process.exit(1);
}
}
main();