最近有需要用到 cloudflare 來做我的 DNS 代理,因為我不想一直進入登入進入後台,所以我就乾脆看一下他的 API ,看看能不能透過自己使用程式操控,有一些小結果就分享一下吧,以後遇到也避免跌坑..
1. 首先取得 Zone ID ,基本上登入 cloudeflare 後,在 overview 那邊就會看到

2.到這裡 https://dash.cloudflare.com/profile 取得你的 API TOKEN ,產生的時候記得選 All Zones,之後你就拿到了 API token



3.之後就是程式碼的部分,這邊我就直接分享出來,請注意 Authorization 的 value 得部分空白很重要,多一個少一個空白都會出現 {"code":10000,"message":"Authentication error"}] ,被這搞到一下子,我都想說我按照文件做都還不行..
Response Model:
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
public class Meta | |
{ | |
public bool auto_added { get; set; } | |
public bool managed_by_apps { get; set; } | |
public bool managed_by_argo_tunnel { get; set; } | |
public string source { get; set; } | |
} | |
public class Result | |
{ | |
public string id { get; set; } | |
public string zone_id { get; set; } | |
public string zone_name { get; set; } | |
public string name { get; set; } | |
public string type { get; set; } | |
public string content { get; set; } | |
public bool proxiable { get; set; } | |
public bool proxied { get; set; } | |
public int ttl { get; set; } | |
public bool locked { get; set; } | |
public Meta meta { get; set; } | |
public DateTime created_on { get; set; } | |
public DateTime modified_on { get; set; } | |
} | |
public class ResultInfo | |
{ | |
public int page { get; set; } | |
public int per_page { get; set; } | |
public int count { get; set; } | |
public int total_count { get; set; } | |
public int total_pages { get; set; } | |
} | |
public class DNSRecordResult | |
{ | |
public List<Result> result { get; set; } | |
public bool success { get; set; } | |
public List<object> errors { get; set; } | |
public List<object> messages { get; set; } | |
public ResultInfo result_info { get; set; } | |
} |
Get All DNS Records C# Code:
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
HttpClient clientGetZones = new HttpClient(); | |
clientGetZones.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
//clientGetZones.DefaultRequestHeaders.Add("Authorization", " Bearer [API TOKEN]"); | |
clientGetZones.DefaultRequestHeaders.Add("Authorization", " Bearer abcdedghijklmnopQRSTUV_ABCD_ggggPqCD"); | |
//var url1 = "https://api.cloudflare.com/client/v4/zones/[ZONE ID]/dns_records?type=A&page=1&per_page=1000&order=type&direction=desc&match=all"; | |
//取得 A 紀錄的,並且一次取1000 筆回來 :P | |
var url1 = "https://api.cloudflare.com/client/v4/zones/2836714271b942af9386532b00b5579c/dns_records?type=A&page=1&per_page=1000&order=type&direction=desc&match=all"; | |
var res = clientGetZones.GetAsync(url1).Result.Content.ReadAsStringAsync().Result; | |
var dnsRecords = JsonConvert.DeserializeObject<DNSRecordResult>(res); | |
foreach (var record in dnsRecords.result) | |
{ | |
ltlResult.Text += record.id + "- " + record.name + "(" + record.type + ")" + ":" + record.content + "," + record.created_on.ToString("yyyy-MM-dd") + ", is Proxied:" + record.proxied + "<br>"; | |
} | |
//Result | |
//76698b3a5a5572d16d7a55db3e081099- sample.com(A):163.205.119.3,2020-10-22, is Proxied:True | |
//3bdcb03125648d36f3751d58017ba920- wizard.sample.com(A):163.205.171.212,2020-10-23, is Proxied:True | |
//213c334d631e6dae116d7dff61e1e0dd- demo1.sample.com(A):163.13.202.11,2020-10-27, is Proxied:True |
reference:
https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records
---
請你暫時把你的勇氣給我 在夢想快消失的時候 讓我的 Code 用力的穿過天空 為愛我的人做一秒英雄 如果這篇文章有幫助到您,簡單留個言,或是幫我按個讚,讓我有寫下去的動力...