diff options
author | Mike Crute <mike@crute.us> | 2022-11-25 14:48:48 -0800 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2022-11-25 14:48:48 -0800 |
commit | f7e0bb4d4775b2f77a95d1f982337fdfd1f0d3da (patch) | |
tree | 8660bf43efaf34c633ad133a01148cec9e7bff1a /clients | |
parent | f02ba5d62b864bfd1c9a4736d9ca1f5571b68a3e (diff) | |
download | golib-f7e0bb4d4775b2f77a95d1f982337fdfd1f0d3da.tar.bz2 golib-f7e0bb4d4775b2f77a95d1f982337fdfd1f0d3da.tar.xz golib-f7e0bb4d4775b2f77a95d1f982337fdfd1f0d3da.zip |
clients/dns: add acme dns clientclients/dns/v0.1.0
Diffstat (limited to 'clients')
-rw-r--r-- | clients/dns/acme.go | 55 | ||||
-rw-r--r-- | clients/dns/go.mod | 3 |
2 files changed, 58 insertions, 0 deletions
diff --git a/clients/dns/acme.go b/clients/dns/acme.go new file mode 100644 index 0000000..a827807 --- /dev/null +++ b/clients/dns/acme.go | |||
@@ -0,0 +1,55 @@ | |||
1 | package dns | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "context" | ||
6 | "encoding/json" | ||
7 | "fmt" | ||
8 | "io/ioutil" | ||
9 | "net/http" | ||
10 | "time" | ||
11 | ) | ||
12 | |||
13 | type AcmeDNSServiceClient struct { | ||
14 | URL string | ||
15 | ApiKey string | ||
16 | } | ||
17 | |||
18 | func (c *AcmeDNSServiceClient) makeRequest(cin context.Context, method, zone, chal string) error { | ||
19 | out, err := json.Marshal(map[string]string{"challenge": chal, "zone": zone}) | ||
20 | if err != nil { | ||
21 | return err | ||
22 | } | ||
23 | |||
24 | // Autocert has an overall timeout of 5 minutes | ||
25 | ctx, cancel := context.WithTimeout(cin, 3*time.Minute) | ||
26 | defer cancel() | ||
27 | |||
28 | r, err := http.NewRequestWithContext(ctx, method, c.URL, bytes.NewBuffer(out)) | ||
29 | if err != nil { | ||
30 | return err | ||
31 | } | ||
32 | r.Header.Set("Content-Type", "application/json") | ||
33 | r.SetBasicAuth("u", c.ApiKey) | ||
34 | |||
35 | res, err := http.DefaultClient.Do(r) | ||
36 | if err != nil { | ||
37 | return err | ||
38 | } | ||
39 | |||
40 | // The service will only return one of these codes on success | ||
41 | if res.StatusCode != http.StatusCreated && res.StatusCode != http.StatusNoContent { | ||
42 | rb, _ := ioutil.ReadAll(res.Body) | ||
43 | return fmt.Errorf("DNS service HTTP error: %s", rb) | ||
44 | } | ||
45 | |||
46 | return nil | ||
47 | } | ||
48 | |||
49 | func (c *AcmeDNSServiceClient) Fulfill(ctx context.Context, domain string, record string) error { | ||
50 | return c.makeRequest(ctx, http.MethodPost, domain, record) | ||
51 | } | ||
52 | |||
53 | func (c *AcmeDNSServiceClient) Cleanup(ctx context.Context, domain string, record string) { | ||
54 | c.makeRequest(ctx, http.MethodDelete, domain, record) | ||
55 | } | ||
diff --git a/clients/dns/go.mod b/clients/dns/go.mod new file mode 100644 index 0000000..c4cef7d --- /dev/null +++ b/clients/dns/go.mod | |||
@@ -0,0 +1,3 @@ | |||
1 | module code.crute.us/mcrute/golib/clients/dns | ||
2 | |||
3 | go 1.18 | ||