-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynSetup.go
132 lines (110 loc) · 3.58 KB
/
dynSetup.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
package main
import (
"encoding/json"
"log"
"os"
"time"
"github.com/monaco-io/request"
)
// Token struct
type Token struct {
Status string `json:"status"`
Data struct {
Token string `json:"token"`
Version string `json:"version"`
} `json:"data"`
JobID int64 `json:"job_id"`
Msgs []struct {
INFO string `json:"INFO"`
SOURCE string `json:"SOURCE"`
ERRCD interface{} `json:"ERR_CD"`
LVL string `json:"LVL"`
} `json:"msgs"`
}
// function to generate a token
func getToken() string {
client := request.Client{
URL: "https://api.dynect.net/REST/Session/",
Method: "POST",
Body: []byte(`{
"customer_name": "CUSTOMER_NAME",
"user_name": "USER_NAME",
"password": "PASSWORD"
}`),
}
resp, err := client.Do()
if err != nil {
log.Println(err)
}
var tokenData Token
json.Unmarshal([]byte(resp.Data), &tokenData)
var token string
token = tokenData.Data.Token
return token
}
func deleteTxt(token string, targetDomain string, recordName string) {
deleteURL := "https://api.dynect.net/REST/TXTRecord/" + targetDomain + "/" + recordName
client := request.Client{
URL: deleteURL,
Method: "DELETE",
Header: map[string]string{"Auth-Token": token, "Content-Type": "application/json"},
// ContentType: request.ApplicationJSON,
}
resp, err := client.Do()
if err != nil {
log.Println(resp.Code, string(resp.Data), err)
}
}
func updateZone(token string, targetDomain string, recordName string) {
updateZoneURL := "https://api.dynect.net/REST/Zone/" + targetDomain
client := request.Client{
URL: updateZoneURL,
Method: "PUT",
Header: map[string]string{"Auth-Token": token, "Content-Type": "application/json"},
Body: []byte(`{"publish": true}`),
}
resp, err := client.Do()
if err != nil {
log.Println(resp.Code, string(resp.Data), err)
}
}
func addTxt(token string, targetDomain string, recordName string, recordValue string) {
addURL := "https://api.dynect.net/REST/TXTRecord/" + targetDomain + "/" + recordName
recordValue = `"` + recordValue + `"`
body := `{"rdata":{"txtdata": ` + recordValue + `},"ttl":"0"}`
client := request.Client{
URL: addURL,
Method: "POST",
Header: map[string]string{"Auth-Token": token, "Content-Type": "application/json"},
Body: []byte(body),
}
resp, err := client.Do()
if err != nil {
log.Println(resp.Code, string(resp.Data), err)
}
}
func main() {
token := getToken()
targetDomain := os.Args[1]
recordName := os.Args[2]
recordValue := os.Args[3]
runType := os.Args[4]
// Certify the Web passes *.example.com, but we need the zone
targetDomain = targetDomain[2:]
if runType == "setup" {
//make sure there are no existing _acme-challege records
deleteTxt(token, targetDomain, recordName)
updateZone(token, targetDomain, recordName)
//create new record
addTxt(token, targetDomain, recordName, recordValue)
updateZone(token, targetDomain, recordName)
//sleep for ten seconds so dns changes can be propegated
time.Sleep(10 * time.Second)
} else if runType == "cleanup" {
//cleanup the records
deleteTxt(token, targetDomain, recordName)
updateZone(token, targetDomain, recordName)
} else {
log.Println("There was an error, please check the arguments")
}
}