The previous servers 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. Since the CloudFlare Partners plan for selecting IP addresses manually is no longer available, I have been unable to select IP addresses manually. Now, CloudFlare allows for the selection of IP addresses through SaaS, and the temptation to freeload is starting to emerge 🤣
There are many tutorials online about selecting IP addresses manually, and I learned from this article. PayPal can also be used with a domestic identity, only requiring bank card verification without any charges. First, I used the CloudflareSpeedTest project to test the fastest preferred 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 test the speed. I was thinking if there is a better solution to meet my needs more quickly.
After consulting the documentation of the CloudflareSpeedTest project, I found a tutorial on automatically updating domain name resolution in Cloudflare to the fastest IP address (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. Therefore, in the end, I achieved my goal through the following steps:
Speed test for preferred IP address => Convert the exported results into DNS import text recognized by CloudFlare => Import into CloudFlare
After the first import, it is only necessary to add a request to delete DNS resolutions before each command. The following are the implementation processes for each function.
Analysis of Quick Import#
CloudFlare's DNS management panel looks like this, with a quick import/export DNS records feature.
The content is as follows:
After testing, as long as the text document has a partially similar format, the resolution can be imported quickly.
Conversion of Preferred Results#
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 desktop path of the current user
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 data 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.
Batch Deletion of Resolutions#
Create a new PowerShell script (usually with the .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
Select the domain name for which you want to perform the operation, and finally obtain the API Token.
Obtaining the Zone ID#
Open the CloudFlare domain panel management page.
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 the 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 desktop path of the current user
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 data to the text document
echo 解析域. 1 IN A !ip! >> "%outputFile%"
)
Then import it into the DNS resolution panel.
Rice Cooker Feeding You#
If you find it troublesome, you can directly resolve it to my preferred location. The CNAME is arey.tools.tf
.
No guarantee that it will work, but this is how I use it. The update time is uncertain, but it is definitely 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 manually affects my network situation, so I can only run it on my own computer.
This article is synchronized to xLog by Mix Space
The original link is https://de3ay.com/posts/tech/cloudflare-fastly-ips-import