diff --git a/index.js b/index.js index 45bb7a8..247eb47 100644 --- a/index.js +++ b/index.js @@ -15,13 +15,13 @@ const REPORTED_IP_COOLDOWN_MS = 7 * 60 * 60 * 1000; // 7h const fetchBlockedIPs = async () => { try { - const response = await axios.post('https://api.cloudflare.com/client/v4/graphql', PAYLOAD, { headers: headers.CLOUDFLARE }); - if (response.data?.data) { - const events = response.data.data.viewer.zones[0].firewallEventsAdaptive; + const res = await axios.post('https://api.cloudflare.com/client/v4/graphql', PAYLOAD(), { headers: headers.CLOUDFLARE }); + if (res.data?.data) { + const events = res.data.data.viewer.zones[0].firewallEventsAdaptive; log('info', `Fetched ${events.length} events from Cloudflare`); return events; } else { - throw new Error(`Failed to retrieve data from Cloudflare. Status: ${response.status}`); + throw new Error(`Failed to retrieve data from Cloudflare. Status: ${res.status}`); } } catch (err) { log('error', err.response ? `${err.response.status} HTTP ERROR (Cloudflare)\n${JSON.stringify(err.response.data, null, 2)}` : `Unknown error with Cloudflare: ${err.message}`); diff --git a/scripts/payload.js b/scripts/payload.js index cde94bd..8090328 100644 --- a/scripts/payload.js +++ b/scripts/payload.js @@ -1,5 +1,4 @@ -module.exports = { - query: `query ListFirewallEvents($zoneTag: string, $filter: FirewallEventsAdaptiveFilter_InputObject) { +const query = `query ListFirewallEvents($zoneTag: string, $filter: FirewallEventsAdaptiveFilter_InputObject) { viewer { zones(filter: { zoneTag: $zoneTag }) { firewallEventsAdaptive( @@ -25,8 +24,10 @@ module.exports = { } } } - }`, - variables: { + }`; + +module.exports = () => { + const variables = { zoneTag: process.env.CLOUDFLARE_ZONE_ID, filter: { datetime_geq: new Date(Date.now() - (60 * 60 * 10.5 * 1000)).toISOString(), @@ -46,5 +47,7 @@ module.exports = { { action_neq: 'managed_challenge_bypassed' } ] } - } + }; + + return { query, variables }; }; \ No newline at end of file