Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
341 lines (279 loc) · 9.11 KB
/
Copy pathindex.js
File metadata and controls
341 lines (279 loc) · 9.11 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
import { lookup } from "node:dns/promises";
import { isIP } from "node:net";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { fetch, ProxyAgent } from "undici";
import {
buildProxyUsername,
checkExitIpInputShape,
checkExitIpInputSchema,
exitIpResponseSchema,
failure,
fetchPageInputShape,
fetchPageInputSchema,
htmlToText,
readResponseText,
ResponseBodyTooLargeError,
statusFailure,
success,
truncate,
validationFailure
} from "./lib/helpers.js";
const PROXY_HOST = "gw.dataimpulse.com";
const PROXY_PORT = 823;
const REQUEST_TIMEOUT_MS = 45_000;
const MAX_REDIRECTS = 10;
const EXIT_IP_ENDPOINT = "https://api.ipify.org?format=json";
const BROWSER_HEADERS = {
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
"accept-language": "en-US,en;q=0.9"
};
class RequestValidationError extends Error {}
const credentials = loadCredentials();
const server = new McpServer({
name: "dataimpulse-mcp",
version: "0.1.0"
});
server.registerTool(
"fetch_page",
{
title: "Fetch Page",
description: "Fetch a public HTTP(S) page through a DataImpulse residential proxy.",
inputSchema: fetchPageInputShape
},
async (input) => {
const parsed = fetchPageInputSchema.safeParse(input);
if (!parsed.success) {
return validationFailure(parsed.error);
}
return fetchPage(parsed.data);
}
);
server.registerTool(
"check_exit_ip",
{
title: "Check Exit IP",
description: "Return the public IP assigned by the current DataImpulse proxy targeting.",
inputSchema: checkExitIpInputShape
},
async (input) => {
const parsed = checkExitIpInputSchema.safeParse(input);
if (!parsed.success) {
return validationFailure(parsed.error);
}
return checkExitIp(parsed.data);
}
);
const transport = new StdioServerTransport();
await server.connect(transport);
function loadCredentials() {
const username = process.env.DI_USER;
const password = process.env.DI_PASS;
if (!username || !password) {
console.error("DataImpulse MCP requires both DI_USER and DI_PASS environment variables.");
process.exit(1);
}
return { username, password };
}
async function fetchPage({ url, country, city, session, raw = false }) {
try {
const destination = await assertPublicDestination(url);
return await withProxy({ country, city, session }, async (dispatcher) => {
const response = await fetchFollowingPublicRedirects(destination, dispatcher, BROWSER_HEADERS);
const body = await readResponseText(response);
if (!response.ok) {
return statusFailure(response.status, body);
}
return success(raw ? truncate(body) : truncate(htmlToText(body)));
});
} catch (error) {
return requestFailure(error);
}
}
async function checkExitIp({ country, session }) {
try {
const endpoint = await assertPublicDestination(EXIT_IP_ENDPOINT);
return await withProxy({ country, session }, async (dispatcher) => {
const response = await fetchFollowingPublicRedirects(endpoint, dispatcher, {
...BROWSER_HEADERS,
accept: "application/json, text/plain, */*"
});
const body = await readResponseText(response);
if (!response.ok) {
return statusFailure(response.status, body);
}
let parsedBody;
try {
parsedBody = JSON.parse(body);
} catch {
return failure("The exit-IP service returned invalid JSON.");
}
const payload = exitIpResponseSchema.safeParse(parsedBody);
if (!payload.success) {
return failure("The exit-IP service returned an invalid response.");
}
return success(`Exit IP: ${payload.data.ip}`);
});
} catch (error) {
return requestFailure(error);
}
}
async function withProxy(targeting, action) {
const dispatcher = createProxyAgent(targeting);
try {
return await action(dispatcher);
} finally {
// Each request owns its agent so its pooled sockets cannot outlive the call.
dispatcher.destroy();
}
}
function createProxyAgent({ country, city, session }) {
const proxyUrl = new URL(`http://${PROXY_HOST}:${PROXY_PORT}`);
proxyUrl.username = buildProxyUsername(credentials.username, { country, city, session });
proxyUrl.password = credentials.password;
return new ProxyAgent(proxyUrl.toString());
}
async function fetchFollowingPublicRedirects(initialUrl, dispatcher, headers) {
const signal = AbortSignal.timeout(REQUEST_TIMEOUT_MS);
let currentUrl = initialUrl;
for (let redirects = 0; redirects <= MAX_REDIRECTS; redirects += 1) {
const response = await fetch(currentUrl, {
dispatcher,
headers,
redirect: "manual",
signal
});
const location = response.headers.get("location");
if (!isRedirect(response.status) || !location) {
return response;
}
if (redirects === MAX_REDIRECTS) {
await response.body?.cancel();
throw new RequestValidationError("Too many redirects.");
}
await response.body?.cancel();
currentUrl = await assertPublicDestination(new URL(location, currentUrl).toString());
}
throw new RequestValidationError("Too many redirects.");
}
function isRedirect(status) {
return status === 301 || status === 302 || status === 303 || status === 307 || status === 308;
}
async function assertPublicDestination(value) {
let url;
try {
url = new URL(value);
} catch {
throw new RequestValidationError("The URL is invalid.");
}
if (url.protocol !== "http:" && url.protocol !== "https:") {
throw new RequestValidationError("Only public HTTP and HTTPS URLs are allowed.");
}
if (url.username || url.password) {
throw new RequestValidationError("URLs with credentials are not allowed.");
}
const hostname = url.hostname.replace(/^\[|\]$/g, "").toLowerCase();
if (isForbiddenHostname(hostname)) {
throw new RequestValidationError("The destination must not use a local, private, or metadata hostname.");
}
if (isIP(hostname) !== 0) {
if (!isPublicAddress(hostname)) {
throw new RequestValidationError("The destination must resolve to a public address.");
}
return url;
}
let addresses;
try {
addresses = await lookup(hostname, { all: true, verbatim: true });
} catch {
throw new RequestValidationError("The destination host could not be resolved.");
}
if (addresses.length === 0 || addresses.some(({ address }) => !isPublicAddress(address))) {
throw new RequestValidationError("The destination must resolve only to public addresses.");
}
return url;
}
function isForbiddenHostname(hostname) {
return (
hostname === "localhost" ||
hostname.endsWith(".localhost") ||
hostname.endsWith(".local") ||
hostname === "metadata" ||
hostname === "instance-data" ||
hostname === "metadata.google.internal" ||
hostname.endsWith(".metadata.google.internal")
);
}
function isPublicAddress(address) {
if (isIP(address) === 4) {
return isPublicIpv4(address);
}
if (isIP(address) === 6) {
return isPublicIpv6(address);
}
return false;
}
function isPublicIpv4(address) {
const [first, second] = address.split(".").map(Number);
if (first === 0 || first === 10 || first === 127 || first >= 224) {
return false;
}
if (first === 100 && second >= 64 && second <= 127) {
return false;
}
if (first === 169 && second === 254) {
return false;
}
if (first === 172 && second >= 16 && second <= 31) {
return false;
}
if (first === 192 && (second === 0 || second === 168)) {
return false;
}
if (first === 198 && (second === 18 || second === 19 || second === 51)) {
return false;
}
return !(first === 203 && second === 0);
}
function isPublicIpv6(address) {
const normalized = address.toLowerCase();
const firstHextet = Number.parseInt(normalized.split(":")[0] || "0", 16);
if (
normalized === "::" ||
normalized === "::1" ||
normalized.startsWith("::ffff:") ||
normalized.startsWith("100:") ||
normalized.startsWith("2001:0:") ||
normalized.startsWith("2001:db8:") ||
normalized.startsWith("2002:") ||
normalized.startsWith("3fff:") ||
normalized.startsWith("64:ff9b:")
) {
return false;
}
return (
(firstHextet & 0xfe00) !== 0xfc00 &&
(firstHextet & 0xffc0) !== 0xfe80 &&
(firstHextet & 0xff00) !== 0xff00
);
}
function requestFailure(error) {
if (error instanceof RequestValidationError) {
return failure(error.message);
}
if (error instanceof ResponseBodyTooLargeError) {
return failure("Response body exceeds the 1 MiB limit. Request a smaller resource and retry.");
}
if (
error?.name === "AbortError" ||
error?.name === "TimeoutError" ||
error?.code === "UND_ERR_CONNECT_TIMEOUT" ||
error?.cause?.code === "UND_ERR_CONNECT_TIMEOUT"
) {
return failure("Request timed out after 45 seconds.");
}
return failure("Network request failed. Check the destination and proxy availability, then retry.");
}