File size: 1,056 Bytes
f1bfa1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { Request, Response } from "express";
import crypto from "node:crypto";
import fetch from "node-fetch";
import cfg from "../config/gemini.config.json";

function allowedHost(url: string) {
  try {
    const host = new URL(url).hostname.replace(/^www\./, "").toLowerCase();
    return cfg.network.allowlist.some(d => host === d || host.endsWith("." + d));
  } catch { return false; }
}

export async function aegisFetchHandler(req: Request, res: Response) {
  const { url, purpose } = req.body ?? {};
  if (!cfg.network.allowWebFetch) return res.status(403).json({ error: "Web fetch disabled" });
  if (!url || !allowedHost(url)) return res.status(400).json({ error: "URL not on allowlist" });

  const r = await fetch(url, { headers: { "User-Agent": "Belel-Aegis/1.0" } });
  const text = await r.text();
  const sha256 = crypto.createHash("sha256").update(text).digest("hex");

  return res.status(200).json({
    url, status: r.status, purpose: purpose ?? "citation",
    content: text, sha256, retrieved_at: new Date().toISOString()
  });
}