-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
136 lines (107 loc) · 2.7 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
)
func main() {
domainPtr := flag.String("domain", os.Getenv("CERTBOT_DOMAIN"), "The domain being authenticated")
validationPtr := flag.String("validation", os.Getenv("CERTBOT_VALIDATION"), "The validation string")
configFilePtr := flag.String("config", "config.json", "The config file location")
debugPtr := flag.Bool("debug", false, "Specify to include debug logging")
flag.Parse()
domain := *domainPtr
validation := *validationPtr
if domain == "" || validation == "" {
flag.Usage()
os.Exit(1)
}
configFile, err := ioutil.ReadFile(*configFilePtr)
if err != nil {
log.Fatal(err)
}
config := &config{}
if err := json.Unmarshal(configFile, config); err != nil {
log.Fatal(err)
}
domainCfg, ok := config.Domains[domain]
if !ok {
log.Fatalf("domain %s is not in config file", domain)
}
if domainCfg.AcmeDNSURL == "" {
domainCfg.AcmeDNSURL = config.AcmeDNSURL
}
if domainCfg.AcmeDNSURL == "" {
log.Fatalf("missing acme_dns_url for domain %s", domain)
}
requestURL, err := resolveURL(domainCfg.AcmeDNSURL, "update")
if err != nil {
log.Fatal(err)
}
body := &updateBody{
Subdomain: domainCfg.Subdomain,
Txt: validation,
}
data, err := json.Marshal(body)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest(http.MethodPost, requestURL.String(), bytes.NewBuffer(data))
if err != nil {
log.Fatal(err)
}
req.Header.Add("X-Api-User", domainCfg.Username)
req.Header.Add("X-Api-Key", domainCfg.Password)
if *debugPtr {
fmt.Printf("Executing %+v\n", req)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
log.Fatalf("response status code was not 200: %d: %s", res.StatusCode, string(body))
}
if *debugPtr {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("Response received, unable to read body: %v\n", err)
} else {
fmt.Printf("Response received: %s\n", string(body))
}
}
propagationDuration := domainCfg.PropagationDuration
if propagationDuration == 0 {
propagationDuration = config.PropagationDuration
}
if *debugPtr {
fmt.Printf("Waiting for %v for DNS to propagate\n", time.Duration(propagationDuration))
}
time.Sleep(time.Duration(propagationDuration))
if *debugPtr {
fmt.Println("Done")
}
}
func resolveURL(base, path string) (*url.URL, error) {
baseURL, err := url.Parse(base)
if err != nil {
return nil, err
}
pathURL, err := url.Parse(path)
if err != nil {
return nil, err
}
return baseURL.ResolveReference(pathURL), nil
}