Created
November 14, 2023 06:31
-
-
Save simshaun/84eb3bc72229e5ae2cb8200877ed0cfa to your computer and use it in GitHub Desktop.
PowerShell script to set up CloudFlare redirect domains in bulk. Use at own risk.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cloudflare API credentials | |
$email = "AAAAAAAA" | |
$apiKey = "BBBBBBBB" | |
# List of domains to be created | |
$domains = @( | |
"example.com", | |
"example2.com" | |
) | |
# Destination URL for redirection | |
$destinationUrl = "https://www.google.com/" | |
# API endpoint URLs | |
$apiBaseUrl = "https://api.cloudflare.com/client/v4" | |
$zonesUrl = "$apiBaseUrl/zones" | |
# Function to make API requests | |
function Invoke-CloudflareApi { | |
param ( | |
[string]$url, | |
[string]$method = "GET", | |
[hashtable]$headers = @{}, | |
[object]$body | |
) | |
$headers["X-Auth-Email"] = $email | |
$headers["X-Auth-Key"] = $apiKey | |
$response = Invoke-RestMethod -Uri $url -Method $method -Headers $headers -Body ($body | ConvertTo-Json -Depth 10) -ContentType "application/json" | |
return $response | |
} | |
# Function to check if a zone exists | |
function Get-CloudflareZone { | |
param ( | |
[string]$domain | |
) | |
$zonesUrlWithName = $zonesUrl + "?name=$domain" | |
$zones = Invoke-CloudflareApi -url $zonesUrlWithName | |
return $zones.result | Where-Object { $_.name -eq $domain } | |
} | |
# Function to check if a DNS record exists | |
function Get-CloudflareDnsRecord { | |
param ( | |
[string]$zoneId, | |
[string]$recordName | |
) | |
$dnsRecordsUrl = "$zonesUrl/$zoneId/dns_records?name=$name" | |
$dnsRecords = Invoke-CloudflareApi -url $dnsRecordsUrl | |
return $dnsRecords.result | |
} | |
# Function to check if a ruleset rule exists | |
function Get-CloudflareRuleset { | |
param ( | |
[string]$zoneId, | |
[string]$rulesetId | |
) | |
$ruleset = Invoke-CloudflareApi -url "$zonesUrl/$zoneId/rulesets/$rulesetId" | |
return $ruleset.result | |
} | |
# Function to find a ruleset by phase name | |
function Get-RulesetIdByPhase { | |
param ( | |
[string]$zoneId, | |
[string]$phase | |
) | |
$existingRulesets = (Invoke-CloudflareApi -url "$zonesUrl/$zoneId/rulesets").result | |
$ruleset = $existingRulesets | Where-Object { $_.phase -eq $phase } | |
if ($ruleset -ne $null) { | |
return $ruleset.id | |
} else { | |
return $null | |
} | |
} | |
# | |
# | |
# | |
# | |
# | |
foreach ($domain in $domains) { | |
# Check if the zone already exists | |
$existingZone = Get-CloudflareZone -domain $domain | |
if ($existingZone -eq $null) { | |
# Create a new zone for the domain | |
$newZone = @{ | |
name = $domain | |
jump_start = $false | |
} | |
$zoneResponse = Invoke-CloudflareApi -url $zonesUrl -method "POST" -body $newZone | |
$zoneId = $zoneResponse.result.id | |
Write-Host "Zone for domain '$domain' created successfully." | |
} | |
else { | |
Write-Host "Zone for domain '$domain' already exists. Proceeding with other configurations." | |
$zoneId = $existingZone.id | |
} | |
# Create new DNS records for the domain | |
$dnsRecordName = $domain | |
$existingDnsRecord = Get-CloudflareDnsRecord -zoneId $zoneId -recordName $dnsRecordName | |
if ($existingDnsRecord -eq $null) { | |
$newDnsRecord = @{ | |
type = "A" | |
name = $dnsRecordName | |
content = "192.168.0.1" | |
ttl = 1 | |
proxied = $true | |
} | |
Invoke-CloudflareApi -url "$zonesUrl/$zoneId/dns_records" -method "POST" -body $newDnsRecord | |
Write-Host "DNS record for '$dnsRecordName' created successfully." | |
} | |
else { | |
Write-Host "DNS record for '$dnsRecordName' already exists. Skipping creation." | |
} | |
# Create a www CNAME record pointing to the root if it doesn't exist | |
$wwwDnsRecordName = "www" | |
$existingCnameRecord = Get-CloudflareDnsRecord -zoneId $zoneId -recordName $wwwDnsRecordName | |
if ($existingCnameRecord -eq $null) { | |
$newCnameRecord = @{ | |
type = "CNAME" | |
name = $wwwDnsRecordName | |
content = $domain | |
ttl = 1 | |
proxied = $true | |
} | |
Invoke-CloudflareApi -url "$zonesUrl/$zoneId/dns_records" -method "POST" -body $newCnameRecord | |
Write-Host "CNAME record for '$wwwDnsRecordName' created successfully." | |
} | |
else { | |
Write-Host "CNAME record for '$wwwDnsRecordName' already exists. Skipping creation." | |
} | |
# Create a redirect rule if it doesn't exist | |
$rulesetId = Get-RulesetIdByPhase -zoneId $zoneId -phase "http_request_dynamic_redirect" | |
if ($rulesetId -eq $null) { | |
$newRuleset = @{ | |
kind = "zone" | |
name = "default" | |
phase = "http_request_dynamic_redirect" | |
rules = @() | |
} | |
$rulesetResponse = Invoke-CloudflareApi -url "$zonesUrl/$zoneId/rulesets" -method "POST" -body $newRuleset | |
$rulesetId = $rulesetResponse.result.id | |
} | |
$ruleset = Get-CloudflareRuleset -zoneId $zoneId -rulesetId $rulesetId | |
$rulesUrl = "$zonesUrl/$zoneId/rulesets/$rulesetId/rules" | |
$redirectRule = $ruleset.rules | Where-Object { $_.action -eq "redirect" } | |
if ($redirectRule -eq $null) { | |
$newRule = @{ | |
description = "Redirect" | |
expression = "true" | |
action = "redirect" | |
action_parameters = @{ | |
from_value = @{ | |
target_url = @{ | |
value = $destinationUrl | |
} | |
status_code = 301 | |
preserve_query_string = $false | |
} | |
} | |
} | |
# Create the rule | |
Invoke-CloudflareApi -url $rulesUrl -method "POST" -body $newRule | |
Write-Host "Redirect rule created successfully." | |
} else { | |
Write-Host "Redirect rule already exists. Skipping creation." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment