-
Notifications
You must be signed in to change notification settings - Fork 0
/
registrar_west.go
198 lines (185 loc) · 5.18 KB
/
registrar_west.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
package gd
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"time"
)
const (
TableWestZone = `westZone`
TableWestRecord = `westRecord`
)
type WestZone struct {
Result int `json:"result"`
ClientId string `json:"clientid"`
Data struct {
Items []WestZoneView `json:"items"`
} `json:"data"`
Limit string `json:"limit"`
Total string `json:"total"`
PageNo string `json:"pageno"`
TotalPage string `json:"totalpage"`
}
type WestZoneView struct {
Domain string `json:"domain"`
RegistryDate string `json:"regdate"`
ExpiryDate string `json:"expdate"`
Dns1 string `json:"dns1"`
Dns2 string `json:"dns2"`
Dns3 string `json:"dns3"`
Dns4 string `json:"dns4"`
Dns5 string `json:"dns5"`
Dns6 string `json:"dns6"`
Year string `json:"year"`
Version string `json:"version"`
Hold string `json:"clienthold"`
}
func (w *WestZoneView) CreateTableQuery() string {
e := reflect.ValueOf(w).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)`, TableWestZone, tableFields)
return createQuery
}
func (w *WestZoneView) InsertData() (string, []interface{}) {
e := reflect.ValueOf(w).Elem()
insertQuery := fmt.Sprintf(`INSERT %s SET `, TableWestZone)
var insertValue []interface{}
for i := 0; i < e.NumField(); i++ {
varName := e.Type().Field(i).Name
varValue := e.Field(i).Interface()
insertQuery = insertQuery + varName + "=?,"
insertValue = append(insertValue, varValue)
}
insertQuery = strings.TrimRight(insertQuery, ",")
return insertQuery, insertValue
}
func (w *WestZoneView) GetZones(api, account, key string) ([]string, []WestZoneView) {
var zone WestZone
var id []string
content, err := DoRequest(WestRequest(api, account, key, "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.Data.Items {
id = append(id, zone.Data.Items[i].Domain)
}
return id, zone.Data.Items
}
type WestRecord struct {
Result int `json:"result"`
ClientId string `json:"clientid"`
Data struct {
PageNo int `json:"pageno"`
Limit int `json:"limit"`
Total int `json:"total"`
PageCount int `json:"pagecount"`
Items []WestRecordView `json:"items"`
} `json:"data"`
TotalPage string `json:"totalpages"`
}
type WestRecordView struct {
Id int `json:"id"`
Item string `json:"item"`
Value string `json:"value"`
Type string `json:"type"`
Level int `json:"level"`
TTL int `json:"ttl"`
Line string `json:"line"`
Pause int `json:"pause"`
Zone string
}
func (w *WestRecordView) CreateTableQuery() string {
e := reflect.ValueOf(w).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)`, TableWestRecord, tableFields)
return createQuery
}
func (w *WestRecordView) InsertData() (string, []interface{}) {
e := reflect.ValueOf(w).Elem()
insertQuery := fmt.Sprintf(`INSERT %s SET `, TableWestRecord)
var insertValue []interface{}
for i := 0; i < e.NumField(); i++ {
varName := e.Type().Field(i).Name
varValue := e.Field(i).Interface()
insertQuery = insertQuery + varName + "=?,"
insertValue = append(insertValue, varValue)
}
insertQuery = strings.TrimRight(insertQuery, ",")
return insertQuery, insertValue
}
func (w *WestRecordView) GetRecords(api, account, key, zoneId string) []WestRecordView {
var record WestRecord
content, err := DoRequest(WestRequest(api, account, key, "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.Data.Items
}
func WestRequest(api, account, key, method, zoneId string) *http.Request {
var uri string
now := strconv.FormatInt(time.Now().Local().UnixMilli(), 10)
hashData := account + key + now
data := url.Values{}
data.Set("username", account)
data.Set("limit", "1000")
data.Set("time", now)
data.Set("token", Md5encode(hashData))
switch method {
case "zone":
args := "/domain/?act=getdomains"
uri = api + args
case "record":
args := "/domain/?act=getdnsrecord"
uri = api + args
data.Set("domain", zoneId)
}
req, err := http.NewRequest(http.MethodGet, uri, strings.NewReader(data.Encode()))
if err != nil {
log.Println("Resquest error.")
log.Println(err)
return req
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req
}