UNPKG

@nephelaiio/cloudflare-cli

Version:
160 lines (151 loc) 5.86 kB
#!/usr/bin/env node // src/main.ts import {Command} from "commander"; import {setInfo, setQuiet, setVerbose} from "@nephelaiio/logger"; // src/environment.ts import {error} from "@nephelaiio/logger"; import {api} from "@nephelaiio/cloudflare-api"; var apiToken = () => `${process.env.CLOUDFLARE_API_TOKEN}`; var accountId = () => `${process.env.CLOUDFLARE_ACCOUNT_ID}`; var init = async () => { if (!apiToken()) { error("CLOUDFLARE_API_TOKEN environment variable must be set"); process.exit(1); } const token = apiToken(); const path = `/accounts`; const accounts = await api({ token, path }); if (accounts.length === 1) { process.env.CLOUDFLARE_ACCOUNT_ID = accounts[0].id; } else if (!accountId()) { error("CLOUDFLARE_ACCOUNT_ID environment variable must be set"); process.exit(1); } }; // src/zone.ts import {zoneInfo} from "@nephelaiio/cloudflare-api"; // src/utils.ts import {debug, error as error2} from "@nephelaiio/logger"; async function attempt(fn) { try { await fn(); } catch (e) { error2("Aborting on unrecoverable error"); debug(e.message); process.exit(1); } } // src/zone.ts var token = apiToken(); var zone = (program) => { const command = program.command("zone"); command.command("list").action(async (_) => await attempt(async () => { const zones = await zoneInfo({ token }); console.log(JSON.stringify(zones)); })); command.command("info").argument("<zone>", "zone name").action(async (zone2) => await attempt(async () => { const zones = await zoneInfo({ token, zone: zone2 }); console.log(JSON.stringify(zones)); })); }; // src/account.ts import {api as api2} from "@nephelaiio/cloudflare-api"; var account = (program) => { const command = program.command("account"); command.command("list").action(async () => await attempt(async () => { const token2 = apiToken(); const path = `/accounts`; const zones = await api2({ token: token2, path }); console.log(JSON.stringify(zones)); })); }; // src/waf.ts import {debug as debug2, info} from "@nephelaiio/logger"; import {api as api3, zoneInfo as zoneInfo2} from "@nephelaiio/cloudflare-api"; var token2 = apiToken(); var wafRulesetList = async (zone2, ruleset) => { const zoneMessage = zone2 ? `zone ${zone2}` : "all zones"; info(`Fetching waf ruleset info for ${zoneMessage}`); const zones = await zoneInfo2({ token: token2, zone: zone2 }); const rulesetQuery = zones.map(async (z) => { const zoneId = (await z).id; const zoneName = (await z).name; const addZoneInfo = (p) => ({ ...p, ...{ zone_name: zoneName, zone_id: zoneId } }); const path = `/zones/${zoneId}/rulesets?per_page=50`; const result = (await api3({ token: token2, path })).result; return result.map(addZoneInfo); }); const rulesetList = (await Promise.all(rulesetQuery)).flat(); const rulesets = () => ruleset ? rulesetList.filter((r) => r.phase == ruleset) : rulesetList; return rulesets(); }; var wafRulesetRules = async (zone2, ruleset) => { const zoneMessage = zone2 ? `zone ${zone2}` : "all zones"; info(`Fetching waf ruleset rules for ${zoneMessage}`); const rulesets = await wafRulesetList(zone2, ruleset); const ruleQuery = rulesets.map(async (z) => { const zoneId = (await z).zone_id; const rulesetId = (await z).id; const zoneName = (await z).zone_name; const addZoneInfo = (p) => ({ ...p, ...{ zone_name: zoneName, zone_id: zoneId, ruleset_id: rulesetId } }); const path = `/zones/${zoneId}/rulesets/phases/${ruleset}/entrypoint?per_page=50`; const result = (await api3({ token: token2, path })).result; const rules2 = (x) => ("rules" in result) && result.rules || []; return rules2(result).map(addZoneInfo); }); const rules = (await Promise.all(ruleQuery)).flat(); return rules; }; var rulesetAction = (command, action) => { const verb = command.command(action); return verb.option("-z, --zone <string>", undefined); }; var waf = (program) => { const wafCommand = program.command("waf"); const rsCommand = wafCommand.command("ruleset"); rulesetAction(rsCommand, "list").option("-z, --zone <string>", undefined).option("-p, --phase <string>", undefined).action(async (options) => await attempt(async () => { const zone2 = options.zone; const phase = options.phase; debug2(`Fetching waf ruleset zone ${zone2}`); const result = await wafRulesetList(zone2, phase); console.log(JSON.stringify(result)); })); rulesetAction(rsCommand, "custom").option("-z, --zone <string>", undefined).action(async (options) => await attempt(async () => { const zone2 = options.zone; debug2(`Fetching waf custom rules for zone ${zone2}`); const result = await wafRulesetRules(zone2, "http_request_firewall_custom"); console.log(JSON.stringify(result)); })); rulesetAction(rsCommand, "rate-limit").option("-z, --zone <string>", undefined).action(async (options) => await attempt(async () => { const zone2 = options.zone; debug2(`Fetching waf custom rules for zone ${zone2}`); const result = await wafRulesetRules(zone2, "http_ratelimit"); console.log(JSON.stringify(result)); })); }; // src/main.ts async function main() { const program = new Command; const verbose = (_, v) => v + 1; program.version(0.1, "--version", "output the current version").description("cloudflare cli helpers").helpOption("-h, --help", "output usage information").option("-v, --verbose", "verbosity that can be increased", verbose, 0).hook("preAction", async (program2, _) => { if (program2.opts().verbose > 1) { setVerbose(); } else if (program2.opts().verbose == 1) { setInfo(); } else { setQuiet(); } await init(); }); zone(program); account(program); waf(program); program.parse(process.argv); } main();