import axios from "axios";
import fs from "fs";
import { fileURLToPath } from "url";
import path from "path";

export const cloudflare = async () => {
  const [{ data: ipv4 }, { data: ipv6 }] = await Promise.all([
    axios.get("https://www.cloudflare.com/ips-v4"),
    axios.get("https://www.cloudflare.com/ips-v6"),
  ]);
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
  const ips = [...ipv4.split("\n"), ...ipv6.split("\n")];
  await fs.promises.writeFile(
    path.join(__dirname, "../ip_ranges/CLOUDFLARE.json"),
    JSON.stringify({
      NN: ips.map((prefix) => ({ prefix })),
    })
  );
};
