Created
February 21, 2024 11:39
-
-
Save ertugrulturan/4e8723e760414cc580a92fb8006ce0e9 to your computer and use it in GitHub Desktop.
CloudFlare Bulk Record Proxy Golang
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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
) | |
const ( | |
email = "cf mail" | |
apiKey = "cf api orj" | |
newIP = "proxy edeceğimiz ip adres" | |
baseURL = "https://api.cloudflare.com/client/v4" | |
) | |
type ZoneResponse struct { | |
Result []Zone `json:"result"` | |
ResultInfo struct { | |
TotalPages int `json:"total_pages"` | |
Page int `json:"page"` | |
} `json:"result_info"` | |
} | |
type Zone struct { | |
ID string `json:"id"` | |
} | |
type DNSRecord struct { | |
ID string `json:"id"` | |
Type string `json:"type"` | |
Name string `json:"name"` | |
Content string `json:"content"` | |
Proxied bool `json:"proxied"` | |
} | |
func main() { | |
zones := listZones() | |
for _, zone := range zones { | |
updateDNSRecords(zone.ID) | |
} | |
} | |
func listZones() []Zone { | |
var zones []Zone | |
url := fmt.Sprintf("%s/zones", baseURL) | |
for { | |
req, err := http.NewRequest("GET", url, nil) | |
if err != nil { | |
fmt.Println("Error creating request:", err) | |
return zones | |
} | |
req.Header.Set("X-Auth-Email", email) | |
req.Header.Set("X-Auth-Key", apiKey) | |
req.Header.Set("Content-Type", "application/json") | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
fmt.Println("Error making request:", err) | |
return zones | |
} | |
var zoneResp ZoneResponse | |
err = json.NewDecoder(resp.Body).Decode(&zoneResp) | |
if err != nil { | |
fmt.Println("Error decoding response:", err) | |
return zones | |
} | |
zones = append(zones, zoneResp.Result...) | |
if zoneResp.ResultInfo.Page < zoneResp.ResultInfo.TotalPages { | |
url = fmt.Sprintf("%s/zones?page=%d", baseURL, zoneResp.ResultInfo.Page+1) | |
} else { | |
break | |
} | |
} | |
return zones | |
} | |
func updateDNSRecords(zoneID string) { | |
url := fmt.Sprintf("%s/zones/%s/dns_records?type=A&content=%s&proxied=false", baseURL, zoneID, newIP) | |
req, err := http.NewRequest("GET", url, nil) | |
if err != nil { | |
fmt.Println("Error creating request:", err) | |
return | |
} | |
req.Header.Set("X-Auth-Email", email) | |
req.Header.Set("X-Auth-Key", apiKey) | |
req.Header.Set("Content-Type", "application/json") | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
fmt.Println("Error making request:", err) | |
return | |
} | |
var dnsRecords []DNSRecord | |
err = json.NewDecoder(resp.Body).Decode(&dnsRecords) | |
if err != nil { | |
fmt.Println("Error decoding response:", err) | |
return | |
} | |
for _, record := range dnsRecords { | |
recordURL := fmt.Sprintf("%s/zones/%s/dns_records/%s", baseURL, zoneID, record.ID) | |
data := map[string]interface{}{ | |
"type": "A", | |
"name": record.Name, | |
"content": newIP, | |
"proxied": true, | |
} | |
jsonData, err := json.Marshal(data) | |
if err != nil { | |
fmt.Println("Error encoding data:", err) | |
return | |
} | |
req, err := http.NewRequest("PUT", recordURL, bytes.NewBuffer(jsonData)) | |
if err != nil { | |
fmt.Println("Error creating request:", err) | |
return | |
} | |
req.Header.Set("X-Auth-Email", email) | |
req.Header.Set("X-Auth-Key", apiKey) | |
req.Header.Set("Content-Type", "application/json") | |
resp, err := client.Do(req) | |
if err != nil { | |
fmt.Println("Error making request:", err) | |
return | |
} | |
if resp.StatusCode == http.StatusOK { | |
fmt.Printf("Proxy enabled for %s in zone %s\n", record.Name, zoneID) | |
} else { | |
fmt.Printf("Failed to enable proxy for %s in zone %s\n", record.Name, zoneID) | |
// Handle error response if needed | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment