-
Notifications
You must be signed in to change notification settings - Fork 0
/
registrar_cloudflare.go
225 lines (212 loc) · 7.48 KB
/
registrar_cloudflare.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package gd
import (
"encoding/json"
"fmt"
"log"
"net/http"
"reflect"
"strings"
)
const (
TableCloudflareZone = `cloudflareZone`
TableCloudflareRecord = `cloudflareRecord`
)
type CloudflareZone struct {
Result []CloudflareZoneView `json:"result"`
Errors []string `json:"errors"`
Messages []string `json:"messages"`
ResultInfo map[string]int `json:"result_info"`
Success bool `json:"success"`
}
type CloudflareZoneView struct {
Id string `json:"id"`
Name string `json:"name"`
OriginalRegistrar string `json:"original_registrar"`
Status string `json:"status"`
Type string `json:"type"`
ActivatedTime string `json:"activated_on"`
ModifiedTime string `json:"modified_on"`
CreatedTime string `json:"created_on"`
Paused bool `json:"paused"`
NameServers []string `json:"name_servers"`
OriginalNameServers []string `json:"original_name_servers"`
Account map[string]string `json:"account"`
Owner map[string]string `json:"owner"`
Plan map[string]interface{} `json:"plan"`
Meta map[string]interface{} `json:"meta"`
DevelopmentMode int `json:"development_mode"`
OriginalDnshost string `json:"original_dnshost"`
// Permissions []string `json:"permissions"`
}
func (cf *CloudflareZoneView) CreateTableQuery() string {
e := reflect.ValueOf(cf).Elem()
var tableFields string
for i := 0; i < e.NumField(); i++ {
var sqlType string
varName := e.Type().Field(i).Name
varType := e.Field(i).Type().String()
if varType == "int" {
sqlType = varType
} else {
sqlType = "varchar(256)"
}
tableFields = tableFields + varName + " " + sqlType + ","
}
tableFields = strings.TrimRight(tableFields, ",")
createQuery := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s(%s)`, TableCloudflareZone, tableFields)
return createQuery
}
func (cf *CloudflareZoneView) InsertData() (string, []interface{}) {
e := reflect.ValueOf(cf).Elem()
insertQuery := fmt.Sprintf(`INSERT %s SET `, TableCloudflareZone)
// updateQuery := fmt.Sprintf(`UPDATE %s SET `, tableCloudflareZone)
var insertValue []interface{}
// var updateValue []interface{}
// var temp interface{}
for i := 0; i < e.NumField(); i++ {
varName := e.Type().Field(i).Name
varValue := e.Field(i).Interface()
if i >= 8 && i <= 14 {
varValue = fmt.Sprintf(`%v`, varValue)
}
insertQuery = insertQuery + varName + "=?,"
insertValue = append(insertValue, varValue)
// if varName != "Id" {
// updateQuery = updateQuery + varName + "=?,"
// updateValue = append(updateValue, varValue)
// } else {
// temp = varValue
// }
}
insertQuery = strings.TrimRight(insertQuery, ",")
// updateQuery = strings.TrimRight(updateQuery, ",")
// updateQuery = updateQuery + " WHERE Id=?"
// updateValue = append(updateValue, temp)
// searchQuery := fmt.Sprintf(`SELECT Id FROM %s WHERE Id = "%s"`, tableCloudflareZone, temp)
// UpsertData(searchQuery, insertQuery, updateQuery, insertValue, updateValue)
return insertQuery, insertValue
}
func (cf *CloudflareZoneView) GetZones(api, key, mail string) ([]string, []CloudflareZoneView) {
var zone CloudflareZone
var id []string
content, err := DoRequest(CloudflareRequest(api, key, mail, "zone", ""))
if err != nil {
log.Println(err)
return nil, nil
}
err = json.Unmarshal(content, &zone)
if err != nil {
log.Println(err)
return nil, nil
}
for i := range zone.Result {
id = append(id, zone.Result[i].Id)
}
return id, zone.Result
}
type CloudflareRecord struct {
Result []CloudflareRecordView `json:"result"`
Errors []string `json:"errors"`
Messages []string `json:"messages"`
ResultInfo map[string]int `json:"result_info"`
Success bool `json:"success"`
}
type CloudflareRecordView struct {
Id string `json:"id"`
ZoneName string `json:"zone_name"`
Name string `json:"name"`
Type string `json:"type"`
Content string `json:"content"`
TTL int `json:"ttl"`
Proxiable bool `json:"proxiable"`
Proxied bool `json:"proxied"`
Locked bool `json:"locked"`
Meta map[string]interface{} `json:"meta"`
ModifiedTime string `json:"modified_on"`
CreatedTime string `json:"created_on"`
ZoneId string `json:"zone_id"`
}
func (cf *CloudflareRecordView) CreateTableQuery() string {
e := reflect.ValueOf(cf).Elem()
var tableFields string
for i := 0; i < e.NumField(); i++ {
var sqlType string
varName := e.Type().Field(i).Name
varType := e.Field(i).Type().String()
if varType == "int" {
sqlType = varType
} else {
sqlType = "varchar(256)"
}
tableFields = tableFields + varName + " " + sqlType + ","
}
tableFields = strings.TrimRight(tableFields, ",")
createQuery := fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s(%s)`, TableCloudflareRecord, tableFields)
return createQuery
}
func (cf *CloudflareRecordView) InsertData() (string, []interface{}) {
e := reflect.ValueOf(cf).Elem()
insertQuery := fmt.Sprintf(`INSERT %s SET `, TableCloudflareRecord)
// updateQuery := fmt.Sprintf(`UPDATE %s SET `, tableCloudflareRecord)
var insertValue []interface{}
// var updateValue []interface{}
// var temp interface{}
for i := 0; i < e.NumField(); i++ {
varName := e.Type().Field(i).Name
varValue := e.Field(i).Interface()
if i >= 5 && i <= 9 {
varValue = fmt.Sprintf(`%v`, varValue)
}
insertQuery = insertQuery + varName + "=?,"
insertValue = append(insertValue, varValue)
// if varName != "Id" {
// updateQuery = updateQuery + varName + "=?,"
// updateValue = append(updateValue, varValue)
// } else {
// temp = varValue
// }
}
insertQuery = strings.TrimRight(insertQuery, ",")
// updateQuery = strings.TrimRight(updateQuery, ",")
// updateQuery = updateQuery + " WHERE Id=?"
// updateValue = append(updateValue, temp)
// searchQuery := fmt.Sprintf(`SELECT Id FROM %s WHERE Id = "%s"`, tableCloudflareRecord, temp)
// UpsertData(searchQuery, insertQuery, updateQuery, insertValue, updateValue)
return insertQuery, insertValue
}
func (cf *CloudflareRecordView) GetRecords(api, key, mail, zoneId string) []CloudflareRecordView {
var record CloudflareRecord
content, err := DoRequest(CloudflareRequest(api, key, mail, "record", zoneId))
if err != nil {
log.Println(err)
return nil
}
err = json.Unmarshal(content, &record)
if err != nil {
log.Println(err)
return nil
}
return record.Result
}
func CloudflareRequest(api, key, mail, method, zoneId string) *http.Request {
var uri string
switch method {
case "zone":
args := "?per_page=200&direction=desc"
uri = api + args
case "record":
args := fmt.Sprintf("/%s/dns_records?per_page=200&direction=desc", zoneId)
uri = api + args
}
data := strings.NewReader(``)
req, err := http.NewRequest(http.MethodGet, uri, data)
if err != nil {
log.Println("Resquest error.")
log.Println(err)
return req
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("X-Auth-Key", key)
req.Header.Set("X-Auth-Email", mail)
return req
}