/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * `bxc recon ` — one-shot URL → docs. * * Probes a target URL and produces a Markdown reconnaissance report. */ import { Browser, type Page } from "../api/browser.ts"; import { detectFrameworks } from "../detect.ts"; import { EXIT, type CommonOptions, logger, parseCommonArgs } from "./shared.ts"; import { bxcFetch } from "../utils/bxc-fetch.ts"; import { extractStructuredData } from "../google/fetch.ts"; import { generateOpenApiSchema } from "../utils/openapi.ts"; import { getEmbedding } from "../utils/vector.ts"; // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- export type ReconProfile = "static" | "fast" | "http" | "stealth" | "max"; export interface ReconAsset { type: "stylesheet" | "script" | "image" | "font" | "iframe"; url: string; host: string; } export interface ReconHeaders { server?: string; xPoweredBy?: string; cdnRayId?: string; cdnVendor: string; cspHosts: string[]; cacheControl?: string; contentSecurityPolicy?: string; contentType?: string; } export const RECON_SCHEMA = "bxc-recon-v1"; export interface ReconResult { $schema: typeof RECON_SCHEMA; url: string; finalUrl: string; httpStatus: number; bytes: number; gotoMs: number; profile: ReconProfile; headers: ReconHeaders; frameworks: Array<{ name: string; categories?: string[]; version?: string }>; assets: ReconAsset[]; cssSelectors: string[]; screenshotPath?: string; screenshotBytes?: number; } interface ReconCliOptions extends CommonOptions { url: string; profile: ReconProfile; outputPath?: string; snapshotDir?: string; screenshot: boolean; plain: boolean; headless?: boolean; cookies?: string | any[]; userAgent?: string; viewport?: { width: number; height: number }; proxyAuth?: string; spawnOpts?: any; } // --------------------------------------------------------------------------- // CDN fingerprinting // --------------------------------------------------------------------------- function fingerprintCdn(headers: Record): string { const get = (k: string): string | undefined => headers[k.toLowerCase()]; const server = (get("server") ?? "").toLowerCase(); if (get("cf-ray") || server.includes("cloudflare")) return "Cloudflare"; if (get("x-amz-cf-id") || get("x-amz-cf-pop")) return "AWS CloudFront"; if (get("x-served-by") && get("x-cache")) return "Fastly"; if (server.includes("akamai") || get("x-akamai-edge")) return "Akamai"; if ( server === "google frontend" || server.includes("gws") || server.includes("esf") || get("x-cloud-trace-context") ) { return "Google Frontend"; } if (get("x-vercel-id")) return "Vercel"; if (server.includes("cloudfront")) return "AWS CloudFront"; if (server.includes("nginx")) return "nginx (origin or self-hosted)"; return server || "unknown"; } function extractCspHosts(csp: string): string[] { const out = new Set(); for (const directive of csp.split(";")) { const trimmed = directive.trim(); if (!trimmed) continue; for (const part of trimmed.split(/\s+/).slice(1)) { if (part.startsWith("http")) { try { out.add(new URL(part).hostname); } catch { // not a valid URL — skip } } } } return [...out].sort(); } async function fetchFull( url: string, opts: Partial, ): Promise<{ headers: Record; status: number; finalUrl: string; body: string; }> { const r = await bxcFetch(url, opts); const headers: Record = {}; r.headers.forEach((v, k) => { headers[k.toLowerCase()] = v; }); const body = await r.text(); return { headers, status: r.status, finalUrl: r.url, body }; } function reconHeaders(headers: Record): ReconHeaders { return { server: headers["server"], xPoweredBy: headers["x-powered-by"], cdnRayId: headers["cf-ray"] ?? headers["x-amz-cf-id"] ?? headers["x-served-by"] ?? headers["x-cloud-trace-context"], cdnVendor: fingerprintCdn(headers), cspHosts: headers["content-security-policy"] ? extractCspHosts(headers["content-security-policy"]) : [], cacheControl: headers["cache-control"], contentSecurityPolicy: headers["content-security-policy"], contentType: headers["content-type"], }; } // --------------------------------------------------------------------------- // Asset extraction // --------------------------------------------------------------------------- function extractAssets(html: string, base: string): ReconAsset[] { const assets: ReconAsset[] = []; const baseUrl = new URL(base); const resolve = (href: string): string | null => { try { return new URL(href, baseUrl).href; } catch { return null; } }; const push = (type: ReconAsset["type"], href: string): void => { const url = resolve(href); if (!url) return; try { assets.push({ type, url, host: new URL(url).hostname }); } catch { // invalid host — ignore } }; for (const m of html.matchAll( /]+rel=["']?(?:stylesheet|preload)["']?[^>]*href=["']([^"']+)["'][^>]*>/gi, )) { push("stylesheet", m[1]); } for (const m of html.matchAll(/]+src=["']([^"']+)["']/gi)) { push("image", m[1]); } for (const m of html.matchAll( /]+(?:as=["']?font["']?|rel=["']?font["']?)[^>]+href=["']([^"']+)["'][^>]*>/gi, )) { push("font", m[1]); } for (const m of html.matchAll(/]+src=["']([^"']+)["']/gi)) { push("iframe", m[1]); } return assets; } // --------------------------------------------------------------------------- // CSS selectors // --------------------------------------------------------------------------- async function extractCssSelectors( html: string, base: string, opts: Partial, maxStylesheets = 5, ): Promise { const allCss: string[] = []; // Inline