swink-agent-plugin-web 0.11.0

Web browsing plugin for swink-agent: fetch, search, screenshot, extract
Documentation
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Playwright bridge script — communicates with Rust via JSON lines on stdin/stdout.
// Usage: node playwright_bridge.js

const net = require('net');
const dns = require('dns').promises;
const http = require('http');
const readline = require('readline');

let browser = null;
let chromium = null;

function respond(obj) {
  process.stdout.write(JSON.stringify(obj) + '\n');
}

function resolveChromium() {
  if (!chromium) {
    ({ chromium } = require('playwright'));
  }
  return chromium;
}

async function ensureBrowser() {
  if (!browser) {
    browser = await resolveChromium().launch({ headless: true });
  }
  return browser;
}

function normalizeHost(host) {
  const rawHost = String(host || '');
  if (rawHost.startsWith('[') && rawHost.endsWith(']')) {
    return rawHost.slice(1, -1);
  }
  return rawHost;
}

function hostMatches(list, host) {
  const lowerHost = (host || '').toLowerCase();
  return (list || []).some((entry) => lowerHost === String(entry).toLowerCase());
}

function parseIpv4Bytes(host) {
  const parts = host.split('.').map((part) => Number(part));
  if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) {
    return null;
  }
  return parts;
}

function isPrivateIpv4Bytes(parts) {
  return (
    parts[0] === 0 ||
    parts[0] === 10 ||
    parts[0] === 127 ||
    (parts[0] === 100 && parts[1] >= 64 && parts[1] <= 127) ||
    (parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) ||
    (parts[0] === 192 && parts[1] === 168) ||
    (parts[0] === 169 && parts[1] === 254) ||
    (parts[0] === 198 && parts[1] >= 18 && parts[1] <= 19) ||
    (parts[0] === 192 && parts[1] === 0 && parts[2] === 2) ||
    (parts[0] === 198 && parts[1] === 51 && parts[2] === 100) ||
    (parts[0] === 203 && parts[1] === 0 && parts[2] === 113) ||
    parts[0] >= 224
  );
}

function isPrivateIpv4(host) {
  const parts = parseIpv4Bytes(host);
  return parts !== null && isPrivateIpv4Bytes(parts);
}

function parseIpv6Segments(host) {
  let input = host.toLowerCase().split('%')[0];

  if (input.includes('.')) {
    const lastColon = input.lastIndexOf(':');
    if (lastColon === -1) {
      return null;
    }
    const v4 = parseIpv4Bytes(input.slice(lastColon + 1));
    if (v4 === null) {
      return null;
    }
    input = `${input.slice(0, lastColon)}:${((v4[0] << 8) | v4[1]).toString(16)}:${((v4[2] << 8) | v4[3]).toString(16)}`;
  }

  const halves = input.split('::');
  if (halves.length > 2) {
    return null;
  }

  const left = halves[0] ? halves[0].split(':') : [];
  const right = halves.length === 2 && halves[1] ? halves[1].split(':') : [];
  const fill = halves.length === 2 ? 8 - left.length - right.length : 0;
  if (fill < 0 || (halves.length === 1 && left.length !== 8)) {
    return null;
  }

  const segments = [...left, ...Array(fill).fill('0'), ...right];
  if (segments.length !== 8) {
    return null;
  }

  return segments.map((segment) => {
    if (!/^[0-9a-f]{1,4}$/.test(segment)) {
      return null;
    }
    return Number.parseInt(segment, 16);
  });
}

function isPrivateIpv6(host) {
  const segments = parseIpv6Segments(host);
  if (segments === null || segments.some((segment) => segment === null)) {
    return false;
  }

  const mappedV4 =
    segments.slice(0, 5).every((segment) => segment === 0) && segments[5] === 0xffff;
  const compatibleV4 =
    segments.slice(0, 6).every((segment) => segment === 0) &&
    (segments[6] !== 0 || segments[7] !== 0);
  if (mappedV4 || compatibleV4) {
    return isPrivateIpv4Bytes([
      segments[6] >> 8,
      segments[6] & 0xff,
      segments[7] >> 8,
      segments[7] & 0xff,
    ]);
  }

  const unspecified = segments.every((segment) => segment === 0);
  const loopback = segments.slice(0, 7).every((segment) => segment === 0) && segments[7] === 1;
  return (
    unspecified ||
    loopback ||
    (segments[0] & 0xfe00) === 0xfc00 ||
    (segments[0] & 0xffc0) === 0xfe80 ||
    (segments[0] & 0xff00) === 0xff00 ||
    (segments[0] === 0x2001 && segments[1] === 0x0db8)
  );
}

function isBlockedPrivateHost(host) {
  const normalizedHost = normalizeHost(host);
  const lowerHost = normalizedHost.toLowerCase();
  if (lowerHost === 'localhost' || lowerHost.endsWith('.localhost')) {
    return true;
  }

  const ipVersion = net.isIP(normalizedHost);
  if (ipVersion === 4) {
    return isPrivateIpv4(normalizedHost);
  }
  if (ipVersion === 6) {
    return isPrivateIpv6(normalizedHost);
  }
  return false;
}

async function resolvesToBlockedPrivateAddress(host, lookup = dns.lookup) {
  const normalizedHost = normalizeHost(host);
  if (net.isIP(normalizedHost)) {
    return isBlockedPrivateHost(normalizedHost) ? normalizedHost : null;
  }

  const addrs = await lookup(normalizedHost, { all: true, verbatim: true });
  const results = Array.isArray(addrs) ? addrs : [addrs];
  for (const result of results) {
    const address = typeof result === 'string' ? result : result.address;
    if (address && isBlockedPrivateHost(address)) {
      return address;
    }
  }
  return null;
}

async function blockedByFilter(rawUrl, filter, lookup = dns.lookup) {
  if (!filter) {
    return null;
  }

  let parsed;
  try {
    parsed = new URL(rawUrl);
  } catch (err) {
    return `Invalid URL '${rawUrl}': ${err.message}`;
  }

  const scheme = parsed.protocol.replace(':', '');
  if (scheme !== 'http' && scheme !== 'https') {
    return `URL scheme '${scheme}' is not allowed; only http and https are permitted`;
  }

  const host = normalizeHost(parsed.hostname);
  if ((filter.allowlist || []).length > 0 && !hostMatches(filter.allowlist, host)) {
    return `Domain '${host}' is not on the allow list`;
  }
  if (hostMatches(filter.denylist, host)) {
    return `Domain '${host}' is on the deny list`;
  }
  if (filter.blockPrivateIps && isBlockedPrivateHost(host)) {
    return `Address ${host} is a private/internal host and is blocked`;
  }
  if (filter.blockPrivateIps) {
    try {
      const blockedAddress = await resolvesToBlockedPrivateAddress(host, lookup);
      if (blockedAddress) {
        return `Address ${blockedAddress} is a private/internal host and is blocked`;
      }
    } catch (err) {
      return `DNS resolution failed for '${host}': ${err.message}`;
    }
  }

  return null;
}

function newContextOptions(req, proxy) {
  const options = { serviceWorkers: 'block' };
  if (req.viewport) {
    options.viewport = { width: req.viewport.width, height: req.viewport.height };
  }
  if (proxy) {
    options.proxy = { server: proxy.server };
  }
  return options;
}

function filterNeedsProxy(filter) {
  return Boolean(filter && filter.blockPrivateIps);
}

function defaultPortForScheme(scheme) {
  return scheme === 'https' ? 443 : 80;
}

async function resolveProxyTarget(parsed, filter, lookup = dns.lookup) {
  const scheme = parsed.protocol.replace(':', '');
  if (scheme !== 'http' && scheme !== 'https') {
    throw new Error(`URL scheme '${scheme}' is not allowed; only http and https are permitted`);
  }

  const host = normalizeHost(parsed.hostname);
  if ((filter.allowlist || []).length > 0 && !hostMatches(filter.allowlist, host)) {
    throw new Error(`Domain '${host}' is not on the allow list`);
  }
  if (hostMatches(filter.denylist, host)) {
    throw new Error(`Domain '${host}' is on the deny list`);
  }
  if (filter.blockPrivateIps && isBlockedPrivateHost(host)) {
    throw new Error(`Address ${host} is a private/internal host and is blocked`);
  }

  let address = host;
  if (filter.blockPrivateIps && !net.isIP(host)) {
    let addrs;
    try {
      addrs = await lookup(host, { all: true, verbatim: true });
    } catch (err) {
      throw new Error(`DNS resolution failed for '${host}': ${err.message}`);
    }

    const results = Array.isArray(addrs) ? addrs : [addrs];
    if (results.length === 0) {
      throw new Error(`DNS resolution failed for '${host}': no addresses found`);
    }

    address = null;
    for (const result of results) {
      const resolved = typeof result === 'string' ? result : result.address;
      if (resolved && isBlockedPrivateHost(resolved)) {
        throw new Error(`Address ${resolved} is a private/internal host and is blocked`);
      }
      if (!address && resolved) {
        address = resolved;
      }
    }
    if (!address) {
      throw new Error(`DNS resolution failed for '${host}': no addresses found`);
    }
  }

  return {
    address,
    host,
    hostHeader: parsed.host,
    port: Number(parsed.port || defaultPortForScheme(scheme)),
  };
}

async function createFilteringProxy(filter) {
  const server = http.createServer(async (clientReq, clientRes) => {
    try {
      const parsed = new URL(clientReq.url);
      const target = await resolveProxyTarget(parsed, filter);
      const headers = { ...clientReq.headers, host: target.hostHeader };
      const upstream = http.request(
        {
          host: target.address,
          port: target.port,
          method: clientReq.method,
          path: `${parsed.pathname}${parsed.search}`,
          headers,
        },
        (upstreamRes) => {
          clientRes.writeHead(upstreamRes.statusCode || 502, upstreamRes.headers);
          upstreamRes.pipe(clientRes);
        }
      );
      upstream.on('error', (err) => {
        if (!clientRes.headersSent) {
          clientRes.writeHead(502);
        }
        clientRes.end(err.message);
      });
      clientReq.pipe(upstream);
    } catch (err) {
      clientRes.writeHead(403, { 'content-type': 'text/plain; charset=utf-8' });
      clientRes.end(err.message);
    }
  });

  server.on('connect', async (req, clientSocket, head) => {
    try {
      const parsed = new URL(`https://${req.url}`);
      const target = await resolveProxyTarget(parsed, filter);
      const upstream = net.connect({ host: target.address, port: target.port }, () => {
        clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
        if (head && head.length > 0) {
          upstream.write(head);
        }
        upstream.pipe(clientSocket);
        clientSocket.pipe(upstream);
      });
      upstream.on('error', (err) => {
        clientSocket.end(`HTTP/1.1 502 Bad Gateway\r\n\r\n${err.message}`);
      });
      clientSocket.on('error', () => upstream.destroy());
    } catch (err) {
      clientSocket.end(`HTTP/1.1 403 Forbidden\r\n\r\n${err.message}`);
    }
  });

  await new Promise((resolve, reject) => {
    server.once('error', reject);
    server.listen(0, '127.0.0.1', () => {
      server.off('error', reject);
      resolve();
    });
  });

  const address = server.address();
  return {
    server: `http://127.0.0.1:${address.port}`,
    async close() {
      await new Promise((resolve) => server.close(resolve));
    },
  };
}

async function installNavigationFilter(context, filter) {
  let blockedReason = null;
  if (!filter) {
    return () => blockedReason;
  }

  await context.route('**/*', async (route) => {
    const reason = await blockedByFilter(route.request().url(), filter);
    if (reason) {
      blockedReason = reason;
      await route.abort('blockedbyclient');
      return;
    }

    await route.continue();
  });

  return () => blockedReason;
}

async function handleScreenshot(req) {
  const b = await ensureBrowser();
  const proxy = filterNeedsProxy(req.filter) ? await createFilteringProxy(req.filter) : null;
  let context = null;
  let blockedReason = () => null;
  try {
    context = await b.newContext(newContextOptions(req, proxy));
    blockedReason = await installNavigationFilter(context, req.filter);
    const page = await context.newPage();
    await page.goto(req.url, { waitUntil: 'load', timeout: 30000 });
    const buf = await page.screenshot({ type: 'png', fullPage: false });
    const base64 = buf.toString('base64');
    respond({ id: req.id, ok: true, data: { image: base64, finalUrl: page.url() } });
  } catch (err) {
    respond({ id: req.id, ok: false, error: blockedReason() || err.message });
  } finally {
    if (context) {
      await context.close();
    }
    if (proxy) {
      await proxy.close();
    }
  }
}

function buildExtractionPlan(req) {
  let selector = req.selector || null;

  if (req.preset) {
    switch (req.preset) {
      case 'links':
        selector = 'a[href]';
        break;
      case 'headings':
        selector = 'h1,h2,h3,h4,h5,h6';
        break;
      case 'tables':
        selector = 'table';
        break;
      default:
        return { error: `Unknown preset: ${req.preset}` };
    }
  }

  if (!selector) {
    return { error: 'No selector or preset provided' };
  }

  return { selector, preset: req.preset || null };
}

function collectAttributes(el) {
  if (!el.attributes) {
    return {};
  }

  const attributes = Array.isArray(el.attributes) ? el.attributes : Array.from(el.attributes);
  return Object.fromEntries(attributes.map((attr) => [attr.name, attr.value]));
}

function extractElementData(el, preset) {
  const tag = (el.tagName || '').toLowerCase();
  const text = (el.textContent || '').trim().slice(0, 500);

  switch (preset) {
    case 'links':
      return {
        tag,
        text,
        attributes: { href: el.getAttribute('href') || '' },
      };
    case 'headings':
      return {
        tag,
        text,
        attributes: {},
      };
    case 'tables':
      return {
        tag: 'table',
        text: (el.innerHTML || '').slice(0, 2000),
        attributes: {},
      };
    default:
      return {
        tag,
        text,
        attributes: collectAttributes(el),
      };
  }
}

async function handleExtract(req) {
  const b = await ensureBrowser();
  const proxy = filterNeedsProxy(req.filter) ? await createFilteringProxy(req.filter) : null;
  let context = null;
  let blockedReason = () => null;
  try {
    context = await b.newContext(newContextOptions(req, proxy));
    blockedReason = await installNavigationFilter(context, req.filter);
    const page = await context.newPage();
    await page.goto(req.url, { waitUntil: 'load', timeout: 30000 });

    const plan = buildExtractionPlan(req);
    if (plan.error) {
      respond({ id: req.id, ok: false, error: plan.error });
      return;
    }

    const elements = await page.evaluate(
      ({ selector, preset }) => {
        function collectAttributes(el) {
          return Object.fromEntries(Array.from(el.attributes).map((attr) => [attr.name, attr.value]));
        }

        function extractElementData(el, activePreset) {
          const tag = el.tagName.toLowerCase();
          const text = (el.textContent || '').trim().slice(0, 500);

          switch (activePreset) {
            case 'links':
              return {
                tag,
                text,
                attributes: { href: el.getAttribute('href') || '' },
              };
            case 'headings':
              return {
                tag,
                text,
                attributes: {},
              };
            case 'tables':
              return {
                tag: 'table',
                text: el.innerHTML.slice(0, 2000),
                attributes: {},
              };
            default:
              return {
                tag,
                text,
                attributes: collectAttributes(el),
              };
          }
        }

        const elements = document.querySelectorAll(selector);
        return Array.from(elements).slice(0, 200).map((el) => extractElementData(el, preset));
      },
      plan
    );

    respond({ id: req.id, ok: true, data: { elements, finalUrl: page.url() } });
  } catch (err) {
    respond({ id: req.id, ok: false, error: blockedReason() || err.message });
  } finally {
    if (context) {
      await context.close();
    }
    if (proxy) {
      await proxy.close();
    }
  }
}

async function handleRequest(line) {
  let req;
  try {
    req = JSON.parse(line);
  } catch (err) {
    respond({ id: 0, ok: false, error: `Invalid JSON: ${err.message}` });
    return;
  }

  const action = req.action;
  try {
    switch (action) {
      case 'ping':
        respond({ id: req.id, ok: true });
        break;
      case 'screenshot':
        await handleScreenshot(req);
        break;
      case 'extract':
        await handleExtract(req);
        break;
      case 'shutdown':
        respond({ id: req.id, ok: true });
        if (browser) {
          await browser.close();
          browser = null;
        }
        process.exit(0);
        break;
      default:
        respond({ id: req.id, ok: false, error: `Unknown action: ${action}` });
    }
  } catch (err) {
    respond({ id: req.id || 0, ok: false, error: err.message });
  }
}

async function handleClose() {
  if (browser) {
    await browser.close();
    browser = null;
  }
  process.exit(0);
}

if (require.main === module) {
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false,
  });

  rl.on('line', (line) => {
    void handleRequest(line);
  });

  rl.on('close', () => {
    void handleClose();
  });
}

module.exports = {
  blockedByFilter,
  buildExtractionPlan,
  collectAttributes,
  createFilteringProxy,
  extractElementData,
  filterNeedsProxy,
  installNavigationFilter,
  isBlockedPrivateHost,
  newContextOptions,
  resolveProxyTarget,
};