-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
155 lines (133 loc) · 3.88 KB
/
client.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
package hexonet
import (
"context"
"fmt"
"io"
"io/ioutil"
"regexp"
"strconv"
"sync"
"time"
CL "github.com/hexonet/go-sdk/apiclient"
"github.com/libdns/libdns"
"github.com/tojjx/libdns-hexonet/txtsanitize"
)
type client struct {
client *CL.APIClient
mutex sync.Mutex
}
func newClient(username, password string, debug io.Writer) (*client, error) {
if debug == nil {
debug = ioutil.Discard
}
c := CL.NewAPIClient()
c.SetCredentials(username, password) //username, password
// c = c.EnableDebugMode()
resp := c.Login()
if !resp.IsSuccess() {
return nil, fmt.Errorf("login failed, response code:%d description:%s\n", resp.GetCode(), resp.GetDescription())
}
return &client{client: c}, nil
}
func (c *client) getDNSEntries(ctx context.Context, zone string) ([]libdns.Record, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
cmd := map[string]interface{}{
"COMMAND": "QueryDNSZoneRRList",
"dnszone": zone,
}
resp := c.client.Request(cmd)
if !resp.IsSuccess() {
return nil, fmt.Errorf("getDNSEntries failed, response code:%d description:%s\n", resp.GetCode(), resp.GetDescription())
}
records := resp.GetRecords()
recs := make([]libdns.Record, 0, len(records))
for i := range records {
rr := records[i].GetData()["RR"]
name, ttlStr, typ, value := ParseRR(rr)
ttl, _ := strconv.Atoi(ttlStr)
recs = append(recs, libdns.Record{
Type: typ,
Name: name,
Value: value,
TTL: time.Second * time.Duration(ttl),
})
}
return recs, nil
}
func (c *client) addDNSEntry(ctx context.Context, zone string, records []libdns.Record) error {
c.mutex.Lock()
defer c.mutex.Unlock()
cmd := map[string]interface{}{
"COMMAND": "UpdateDNSZone",
"dnszone": zone,
}
for i := range records {
key := fmt.Sprintf("addrr%d", i)
val := fmt.Sprintf("%s %d IN %s %s", records[i].Name, int(records[i].TTL.Seconds()), records[i].Type, TXTSanitize(records[i]))
cmd[key] = val
}
// fmt.Printf("cmd:%#v\n", cmd)
resp := c.client.Request(cmd)
if !resp.IsSuccess() {
return fmt.Errorf("addDNSEntry failed, response code:%d description:%s\n", resp.GetCode(), resp.GetDescription())
}
return nil
}
func (c *client) removeDNSEntry(ctx context.Context, zone string, records []libdns.Record) error {
c.mutex.Lock()
defer c.mutex.Unlock()
cmd := map[string]interface{}{
"COMMAND": "UpdateDNSZone",
"dnszone": zone,
}
for i := range records {
key := fmt.Sprintf("delrr%d", i)
val := fmt.Sprintf("%s %d IN %s %s", records[i].Name, int(records[i].TTL.Seconds()), records[i].Type, TXTSanitize(records[i]))
cmd[key] = val
}
// fmt.Printf("cmd:%#v\n", cmd)
resp := c.client.Request(cmd)
if !resp.IsSuccess() {
return fmt.Errorf("removeDNSEntry failed, response code:%d description:%s\n", resp.GetCode(), resp.GetDescription())
}
return nil
}
func (c *client) updateDNSEntry(ctx context.Context, zone string, records []libdns.Record) error {
c.mutex.Lock()
defer c.mutex.Unlock()
cmd := map[string]interface{}{
"COMMAND": "UpdateDNSZone",
"dnszone": zone,
}
for i := range records {
key := fmt.Sprintf("rr%d", i)
val := fmt.Sprintf("%s %d IN %s %s", records[i].Name, int(records[i].TTL.Seconds()), records[i].Type, TXTSanitize(records[i]))
cmd[key] = val
}
// fmt.Printf("cmd:%#v\n", cmd)
resp := c.client.Request(cmd)
if !resp.IsSuccess() {
return fmt.Errorf("updateDNSEntry failed, response code:%d description:%s\n", resp.GetCode(), resp.GetDescription())
}
return nil
}
// rr format "gomeing.com. 3600 IN NS ns1191.hexonet.net."
func ParseRR(rr string) (name, ttl, typ, value string) {
pattern := `^(.+\.)\s+(\d+)\s+IN\s+(\w+)\s+(.+)$`
regex := regexp.MustCompile(pattern)
matches := regex.FindStringSubmatch(rr)
// extra
name = matches[1]
ttl = matches[2]
typ = matches[3]
value = matches[4]
return
}
func TXTSanitize(record libdns.Record) (value string) {
value = record.Value
if record.Type == "TXT" {
value = txtsanitize.TXTSanitize(record.Value)
}
return
}