32 lines
952 B
TypeScript
32 lines
952 B
TypeScript
import pino from "pino";
|
|
import { CloudflareConfig } from "./config.js";
|
|
|
|
const API_BASE = "https://api.cloudflare.com/client/v4";
|
|
|
|
export class CloudflareListClient {
|
|
config: CloudflareConfig;
|
|
logger = pino({ name: "WhitelisterEternal.CloudflareListClient" });
|
|
constructor(config: CloudflareConfig) {
|
|
this.config = config;
|
|
}
|
|
|
|
async AddIP(ip: string, comment: string) {
|
|
let res = await fetch(
|
|
`${API_BASE}/accounts/${this.config.accountID}/rules/lists/${this.config.listID}/items`,
|
|
{
|
|
method: "POST",
|
|
body: JSON.stringify([{ ip, comment }]),
|
|
headers: {
|
|
Authorization: `Bearer ${this.config.apiKey}`,
|
|
},
|
|
},
|
|
);
|
|
let json = await res.json();
|
|
if (!res.ok || !json.success) {
|
|
let error = json?.errors?.[0]?.message ?? res.statusText;
|
|
this.logger.error(`Failed to add IP ${ip} to Cloudflare list: ${error}`);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|