|
1 | 1 | import axios, { AxiosResponse, Method } from 'axios'; |
| 2 | +import { load as loadHtml } from 'cheerio'; |
2 | 3 |
|
3 | 4 | export interface RedirectResult { |
4 | 5 | chain: string[]; // every hop including the start |
@@ -75,6 +76,22 @@ export class RedirectResolver { |
75 | 76 |
|
76 | 77 | // If we get here, this UA did not produce a redirect. |
77 | 78 |
|
| 79 | + // Only for known interstitial hosts (e.g., LinkedIn), inspect small HTML snippet |
| 80 | + // to extract the intended external destination. Avoid doing this for arbitrary hosts |
| 81 | + // to prevent accidentally following asset links (e.g., fonts, images, svgs). |
| 82 | + const ct = String((getResp.headers as any)['content-type'] || ''); |
| 83 | + const snippet = (getResp as any).bodySnippet as string | undefined; |
| 84 | + const currentHost = (() => { try { return new URL(currentUrl).hostname.toLowerCase(); } catch { return ''; } })(); |
| 85 | + if (snippet && _isLikelyHtml(ct) && isLinkedInHost(currentHost)) { |
| 86 | + const found = extractExternalUrlFromHtml(snippet) || extractUrlFromText(snippet); |
| 87 | + if (found) { |
| 88 | + nextUrlFromAnyUA = resolveLocation(found, currentUrl); |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // (Intentionally no Pinterest-specific HTML parsing here; path rules handle normalization) |
| 94 | + |
78 | 95 | // Bail early if content-length is huge (treat as final to avoid heavy download). |
79 | 96 | const cl = headerInt(getResp.headers['content-length']); |
80 | 97 | if (cl !== null && cl > runtimeOpts.maxBodyBytes) { |
@@ -133,15 +150,52 @@ export class RedirectResolver { |
133 | 150 | validateStatus: () => true, |
134 | 151 | }); |
135 | 152 |
|
136 | | - // For GET/stream, we don't read the stream; headers are enough to check Location. |
137 | | - // Axios will keep the socket open briefly; caller quickly discards response object. |
138 | | - try { |
139 | | - if (isGet && resp.data?.destroy && typeof resp.data.destroy === 'function') { |
140 | | - // Proactively destroy stream to free resources |
141 | | - resp.data.destroy(); |
142 | | - } |
143 | | - } catch { |
144 | | - // ignore |
| 153 | + // For GET/stream, capture a small snippet so we can parse HTML-based redirects |
| 154 | + // like LinkedIn interstitial pages. Keep it bounded by maxBodyBytes, then destroy stream. |
| 155 | + if (isGet && resp.data && typeof (resp.data as any).on === 'function') { |
| 156 | + await new Promise<void>((resolve) => { |
| 157 | + try { |
| 158 | + const stream: any = resp.data; |
| 159 | + const chunks: Buffer[] = []; |
| 160 | + let received = 0; |
| 161 | + const maxBytes = opts.maxBodyBytes; |
| 162 | + |
| 163 | + const onData = (chunk: Buffer) => { |
| 164 | + if (!Buffer.isBuffer(chunk)) chunk = Buffer.from(String(chunk)); |
| 165 | + const remaining = Math.max(0, maxBytes - received); |
| 166 | + if (remaining > 0) { |
| 167 | + const toPush = chunk.length > remaining ? chunk.subarray(0, remaining) : chunk; |
| 168 | + chunks.push(toPush); |
| 169 | + received += toPush.length; |
| 170 | + } |
| 171 | + if (received >= maxBytes) { |
| 172 | + cleanup(); |
| 173 | + } |
| 174 | + }; |
| 175 | + |
| 176 | + const onEnd = () => cleanup(); |
| 177 | + const onError = () => cleanup(); |
| 178 | + |
| 179 | + const cleanup = () => { |
| 180 | + try { stream.off('data', onData); } catch { /* ignore: best-effort detach */ } |
| 181 | + try { stream.off('end', onEnd); } catch { /* ignore: best-effort detach */ } |
| 182 | + try { stream.off('error', onError); } catch { /* ignore: best-effort detach */ } |
| 183 | + try { if (typeof stream.destroy === 'function') stream.destroy(); } catch { /* ignore: best-effort destroy */ } |
| 184 | + try { (resp as any).bodySnippet = Buffer.concat(chunks).toString('utf8'); } catch { /* ignore: best-effort capture */ } |
| 185 | + resolve(); |
| 186 | + }; |
| 187 | + |
| 188 | + stream.on('data', onData); |
| 189 | + stream.on('end', onEnd); |
| 190 | + stream.on('error', onError); |
| 191 | + } catch { |
| 192 | + // Best-effort; ensure resolution |
| 193 | + try { if (resp.data?.destroy) resp.data.destroy(); } catch { /* ignore: destroy may fail */ } |
| 194 | + resolve(); |
| 195 | + } |
| 196 | + }); |
| 197 | + } else { |
| 198 | + // Non-stream case or no data; nothing to capture |
145 | 199 | } |
146 | 200 |
|
147 | 201 | return resp; |
@@ -189,3 +243,69 @@ function dedupeUA(list: UAOption[]): UAOption[] { |
189 | 243 | } |
190 | 244 | return out; |
191 | 245 | } |
| 246 | + |
| 247 | +// Heuristics |
| 248 | +function _isLikelyHtml(contentType: string): boolean { |
| 249 | + const ct = contentType.toLowerCase(); |
| 250 | + return ct.includes('text/html') || ct.includes('application/xhtml'); |
| 251 | +} |
| 252 | + |
| 253 | +// Extract first external href from HTML (skip LinkedIn/lnkd.in domains) using a DOM parser for reliability |
| 254 | +function extractExternalUrlFromHtml(html: string): string | null { |
| 255 | + try { |
| 256 | + const $ = loadHtml(html); |
| 257 | + // Prefer LinkedIn interstitial button selector if present |
| 258 | + const preferred = $('a[data-tracking-control-name="external_url_click"][href]') |
| 259 | + .map((_, el) => ($(el).attr('href') || '').trim()) |
| 260 | + .get(); |
| 261 | + const anchors = preferred.length > 0 |
| 262 | + ? preferred |
| 263 | + : $('a[href]') |
| 264 | + .map((_, el) => ($(el).attr('href') || '').trim()) |
| 265 | + .get(); |
| 266 | + |
| 267 | + for (const href of anchors) { |
| 268 | + if (!href) continue; |
| 269 | + try { |
| 270 | + const u = new URL(href); |
| 271 | + const host = u.hostname.toLowerCase(); |
| 272 | + if (!isLinkedInHost(host)) return href; |
| 273 | + } catch { |
| 274 | + // skip invalid |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + // As a last resort, scan visible text nodes for absolute URLs |
| 279 | + const text = $('body').text(); |
| 280 | + const fromText = extractUrlFromText(text); |
| 281 | + if (fromText) return fromText; |
| 282 | + } catch { |
| 283 | + // ignore parser failures |
| 284 | + } |
| 285 | + return null; |
| 286 | +} |
| 287 | + |
| 288 | +function isLinkedInHost(host: string): boolean { |
| 289 | + return host === 'lnkd.in' || host === 'linkedin.com' || host.endsWith('.linkedin.com'); |
| 290 | +} |
| 291 | + |
| 292 | +// Note: no Pinterest-specific parsing required; keep resolver generic |
| 293 | + |
| 294 | +// Plain text URL extraction (fallback) |
| 295 | +function extractUrlFromText(text?: string): string | null { |
| 296 | + if (!text) return null; |
| 297 | + const re = /(https?:\/\/[^\s"'<>]+)/gim; |
| 298 | + let m: RegExpExecArray | null; |
| 299 | + while ((m = re.exec(text))) { |
| 300 | + const candidate = m[1]; |
| 301 | + try { |
| 302 | + const host = new URL(candidate).hostname.toLowerCase(); |
| 303 | + if (!isLinkedInHost(host)) return candidate; |
| 304 | + } catch { |
| 305 | + // ignore |
| 306 | + } |
| 307 | + } |
| 308 | + return null; |
| 309 | +} |
| 310 | + |
| 311 | +// (No site-specific HTML canonical extraction to keep resolver generic) |
0 commit comments