summaryrefslogtreecommitdiff
path: root/examples/cloudflare/dns.go
blob: eb8de23e054a4b456d4bf1204a7a02e0be2f4423 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// This is a simple example
package main

import 	(
	"os"
	"log"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"
)

// Define a struct to match the JSON structure of the response.
// This structure should be adjusted based on the actual format of the response.
type DNSRecords struct {
	Result []struct {
		ID     string `json:"id"`
		Type   string `json:"type"`
		Name   string `json:"name"`
		Content string `json:"content"`
		Proxied bool `json:"proxied"`
		Proxiable bool `json:"proxiable"`
		TTL int `json:"ttl"`
	} `json:"result"`
}

// var domain string = "wit.org"
// var os.Getenv("CLOUDFLARE_DOMAIN")

func loadDNS(hostname string) {
	log.Println("adding DNS record")

	newt := mainWindow.NewTab(hostname)
	newg := newt.NewGroup("more")
	grid := newg.NewGrid("gridnuts", 5, gridH)

//	grid.NewButton("Type", func () {
//		log.Println("sort by Type")
//	})
	typedrop := grid.NewDropdown("type")
	typedrop.AddText("A")
	typedrop.AddText("AAAA")
	typedrop.AddText("CNAME")
	typedrop.Custom = func () {
		log.Println("custom dropdown() a =", typedrop.Name, typedrop.S)
	}
	grid.NewButton("Name", func () {
		log.Println("sort by Name")
	})
	grid.NewButton("Protection", func () {
		log.Println("sort proxied")
	})
	grid.NewButton("TTL", func () {
		log.Println("sort by TTL")
	})
	grid.NewButton("Value", func () {
		log.Println("sort by Value")
	})

	newt.NewButton("Save", func () {
		log.Println("save stuff to cloudflare")
	})

	records := getRecords()
	for _, record := range records.Result {
		grid.NewLabel(record.Type)
		textbox := grid.NewTextbox(record.Name)
		textbox.SetText(record.Name)
		if (record.Proxied) {
			grid.NewLabel("Proxied")
		} else {
			grid.NewLabel("DNS")
		}
		var ttl, short  string
		if (record.TTL == 1) {
			ttl = "Auto"
		} else {
			ttl = strconv.Itoa(record.TTL)
		}
		grid.NewLabel(ttl)
		// short = fmt.Sprintf("%80s", record.Content)
		short = record.Content
		if len(short) > 40 {
			short = short[:40] // Slice the first 20 characters
		}

		namebox := grid.NewTextbox(short)
		namebox.SetText(short)

		fmt.Printf("ID: %s, Type: %s, Name: %s, short Content: %s\n", record.ID, record.Type, record.Name, short)
		fmt.Printf("\tproxied: %b, %b, string TTL: %i\n", record.Proxied, record.Proxiable, ttl)
	}
}

func getRecords() *DNSRecords {
	var url string = os.Getenv("CLOUDFLARE_URL")
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Println(err)
		return nil
	}

	var authKey string = os.Getenv("CLOUDFLARE_AUTHKEY")
	var email string = os.Getenv("CLOUDFLARE_EMAIL")

	// Set headers
	req.Header.Set("X-Auth-Key", authKey)
	req.Header.Set("X-Auth-Email", email)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return nil
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
		return nil
	}

	var records DNSRecords
	if err := json.Unmarshal(body, &records); err != nil {
		fmt.Println(err)
		return nil
	}

	return &records
}