-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (55 loc) · 1.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
const (
apiBaseURL = "https://api.cloudflare.com/client/v4/zones"
)
type DNSRecord struct {
Type string `json:"type"`
Name string `json:"name"`
Content string `json:"content"` //hanya ip
}
func addDNSRecord(zoneID, apiKey string, record DNSRecord) error {
url := fmt.Sprintf("%s%s/dns_records", apiBaseURL, zoneID)
requestData := struct {
Record DNSRecord `json:"dns_record"`
}{Record: record}
requestBody, err := json.Marshal(requestData)
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
return err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Failed to add DNS Record: %s", resp.Status)
}
fmt.Println("DNS Berhasil ditambahkan untuk zone dengan ID:", zoneID)
return nil
}
func main() {
apiKey := "API_KEY"
zoneID := "ZONE_ID"
recordToAdd := DNSRecord{
Type: "A", //type A record
Name: "halo.bukaevent.com", //subdomain
Content: "0.0.0.0", //IP origin
}
err := addDNSRecord(zoneID, apiKey, recordToAdd)
if err != nil {
fmt.Println("Error", err)
}
}