向诚

向诚

巷子里的猫很自由,却没有归宿。
telegram
email

CloudFlare Preferred IP Round Robin + Bulk Import of Resolutions (Flawed Solution)

The previous server and CDN are not going to be renewed, so the front end has been moved to Vercel, and the back end has been moved to another server. After the CloudFlare Partners plan's optional IP feature became invalid, I couldn't select IP addresses manually. Now, CloudFlare allows for the selection of optimal IP addresses through SaaS, and the temptation of freeloading power is starting to emerge 🤣

There are many tutorials online about selecting IP addresses manually. I learned from this article. PayPal can also be used with a domestic identity, only requiring bank card verification without any deductions. First, I used the CloudflareSpeedTest project to test the fastest optimal IP address in my region. Since CloudFlare supports A record load balancing, I had no problem adding multiple IP addresses for resolution. However, the problem is that manually deleting and modifying the resolution every time is troublesome, and it takes more than ten or twenty minutes to speed up the test. I was thinking if there is a better solution to quickly meet my needs.

After consulting the documentation of the CloudflareSpeedTest project, I found a tutorial on automatically updating domain name resolution in Cloudflare to the fastest IP (Windows/Linux script + manual tutorial). Unfortunately, it can only modify a single resolution. Continuous trial and error led me to realize that it seems unrealistic to modify resolutions in bulk through the API. So, in the end, I achieved my goal through the following steps:

Optimal IP speed test => Convert the exported results into DNS import text that CloudFlare can recognize => Import into CloudFlare

After the first import, all you need to do is add a request to delete DNS resolutions before each command. The following are the implementation processes of various functions:

Quick Import Analysis#

CloudFlare's DNS management panel looks like this, with a quick import/export DNS records feature.

image-20240723221516797

The content is as follows:

image-20240723221934997

After testing, as long as the text document has a partially similar format, the resolution can be quickly imported.

Optimal Results Conversion#

Please modify the script according to your own domain name to suit your situation!!!

In the same directory as CloudflareSpeedTest, create a command prompt script.

@echo off

setlocal EnableDelayedExpansion

REM Get the directory where the current script is located
set "scriptPath=%~dp0"

REM Set the input and output file paths
set "inputFile=%scriptPath%result.csv"
REM Get the current user's desktop path
set "desktopPath=C:\Users\YourUsername\Desktop"

REM Set the output file path
set "outputFile=%desktopPath%\output.txt"

REM Clear the output file
type nul > "%outputFile%"

REM Add ";; A Records" at the beginning of the output file
echo ;; A Records >> "%outputFile%"

REM Read each line of the result.csv file (skip the header)
for /f "usebackq skip=1 tokens=1-6 delims=," %%A in ("%inputFile%") do (
    REM Get the IP address in each line (first column)
    set "ip=%%A"
    REM Add "arey.tools.tf. 1 IN A" and the IP address to the text document
    echo arey.tools.tf.	1	IN	A	!ip! >> "%outputFile%"
)

This script can quickly convert the output results of CloudflareSpeedTest on the Windows platform into the format shown in the following figure. If you need support for other platforms, please ask ChatGPT.

image-20240723222707016

Bulk Resolution Deletion#

Create a PowerShell script (usually with a .ps1 extension) with the following content:

$API_TOKEN = "<API TOKEN>"
$ZONE_ID   = "<ZONE ID>"

$baseUrl = "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"

$headers = @{
  'Authorization' = "Bearer $API_TOKEN"
  'Content-Type'  = "application/json"
}

$listUrl = $baseUrl + '?per_page=500'
Write-Host $listUrl
$records = Invoke-RestMethod -Uri $listUrl -Method 'GET' -Headers $headers
$records = $records | Select-Object -ExpandProperty result

foreach ($record in $records) {
  Write-Host "Deleting $($record.name) that points to $($record.content)"

  $deleteUrl = $baseUrl + '/' + $record.id
  Invoke-RestMethod -Uri $deleteUrl -Method 'DELETE' -Headers $headers
  Write-Host $deleteUrl
}

If you have other important resolutions, please do not mistakenly delete them! You can back up your resolutions and add the pre-backed up resolution text segments to output.txt before importing!

Obtaining the API Token#

Link: API Tokens

image-20240723223733682

image-20240723223744745

image-20240723223814629 Select the domain name for which you want to perform the operation and obtain the API Token.

Obtaining the Zone ID#

Open the CloudFlare domain panel management page.

image-20240723224014617

Finally, we use the following command to run the PowerShell script in the command prompt.

powershell.exe -ExecutionPolicy Bypass -File <name>.ps1

Slightly Integrated#

@echo off

del result.csv ::Delete the previously exported results
CloudflareST.exe ::Perform optimization
powershell.exe -ExecutionPolicy Bypass -File <name>.ps1 ::Run the script to delete previous resolutions

setlocal EnableDelayedExpansion

REM Get the directory where the current script is located
set "scriptPath=%~dp0"

REM Set the input and output file paths
set "inputFile=%scriptPath%result.csv"
REM Get the current user's desktop path
set "desktopPath=C:\Users\YourUsername\Desktop"

REM Set the output file path
set "outputFile=%desktopPath%\output.txt"

REM Clear the output file
type nul > "%outputFile%"

REM Add ";; A Records" at the beginning of the output file
echo ;; A Records >> "%outputFile%"

REM Read each line of the result.csv file (skip the header)
for /f "usebackq skip=1 tokens=1-6 delims=," %%A in ("%inputFile%") do (
    REM Get the IP address in each line (first column)
    set "ip=%%A"
    REM Add "解析域. 1 IN A" and the IP address to the text document
    echo 解析域.	1	IN	A	!ip! >> "%outputFile%"
)

After that, import it into the DNS resolution panel.

Rice Cooker Stuffed into Your Mouth#

If you find it troublesome, you can directly resolve it to my optimal selection. The CNAME is arey.tools.tf.

I can't guarantee that it will work, but this is how I use it. The update time is uncertain, but it will definitely be updated at least once a week. You can advance further and use server Crontab to run it, but my server is overseas, and specifying the URL for selecting IP addresses affects my network situation, so I can only run it on my own computer.

This article is synchronized and updated to xLog by Mix Space.
The original link is https://de3ay.com/posts/tech/cloudflare-fastly-ips-import


Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.