-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
210 lines (179 loc) · 5.83 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
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
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
logging "log"
"os"
"strings"
dns "github.com/dnsimple/dnsimple-go/dnsimple"
)
var log *logging.Logger
type Args struct {
Token string
Domain string
RecName string
Link string
Verbose bool
TTL int
}
const (
DnslinkPrefix = "dnslink="
DefaultTTL = 60
TXT = "TXT"
)
var Usage = `
USAGE
DNSIMPLE_TOKEN=trustno1 dnslink-dnsimple -d <domain-name> -r <record-name> -l <dnslink-value>
OPTIONS
-l, --link <string> dnslink value, eg. ipfs path (required)
-d, --domain <string> dnsimple domain name (required)
-r, --record <string> domain record name (required)
-v, --verbose show logging output
-h, --help show this documentation
--ttl <int> set the ttl of the record (default: 60)
EXAMPLES
DNSIMPLE_TOKEN=$(cat dnsimple-token) dnslink-dnsimple -d domain.net -r _dnslink -l /ipns/ipfs.io
`
func parseArgs() (Args, error) {
var a Args
flag.StringVar(&a.Domain, "d", "", "")
flag.StringVar(&a.Domain, "domain", "", "")
flag.StringVar(&a.RecName, "r", "", "")
flag.StringVar(&a.RecName, "record", "", "")
flag.StringVar(&a.Link, "l", "", "")
flag.StringVar(&a.Link, "link", "", "")
flag.BoolVar(&a.Verbose, "v", false, "")
flag.BoolVar(&a.Verbose, "verbose", false, "")
flag.IntVar(&a.TTL, "ttl", DefaultTTL, "")
a.Token = os.Getenv("DNSIMPLE_TOKEN")
flag.Usage = func() {
fmt.Fprint(os.Stderr, Usage)
}
flag.Parse()
if a.Token == "" || a.Domain == "" || a.RecName == "" || a.Link == "" {
return a, errors.New("token, domain, record, and link arguments required")
}
return a, nil
}
func main() {
args, err := parseArgs()
if err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
fmt.Fprintln(os.Stderr, Usage)
os.Exit(-1)
}
if args.Verbose {
log = logging.New(os.Stderr, "", 0)
} else {
log = logging.New(io.Discard, "", 0)
}
if err := errMain(args); err != nil {
fmt.Fprintln(os.Stderr, "error:", sanitizeErr(args.Token, err))
os.Exit(-1)
}
}
func sanitizeErr(token string, err error) string {
return strings.Replace(fmt.Sprintf("%s", err), token, "", -1)
}
func errMain(args Args) error {
tc := dns.StaticTokenHTTPClient(context.Background(), args.Token)
client := dns.NewClient(tc)
// get the account responsible for zone, and the dnslink record if there is one.
acc, oldR, err := findAccountAndRecord(client, args)
if err != nil {
return err
}
// Create or update the _dnslink record.
var newR *dns.ZoneRecord
if oldR == nil {
newR, err = createRecord(client, args, acc)
} else { // got an old record to update
newR, err = updateRecord(client, args, acc, oldR)
}
if err != nil {
return err
}
fmt.Printf("updated TXT %s.%s to %s\n", newR.Name, args.Domain, newR.Content)
return nil
}
// findAccountAndRecord finds the right account that can update the desired record
// if we find the specific record to update, return it too.
func findAccountAndRecord(c *dns.Client, args Args) (acc string, rec *dns.ZoneRecord, err error) {
acc, recs, err := findAccountForZone(c, args)
if err != nil {
return "", nil, err
}
// find record to replace, if any
var oldR *dns.ZoneRecord
for _, r := range recs {
if strings.HasPrefix(r.Content, "dnslink") || strings.HasPrefix(r.Content, "\"dnslink") {
oldR = &r
}
}
if oldR == nil {
log.Println("existing dnslink record: not found")
} else {
log.Println("existing dnslink record:", oldR.ZoneID, oldR.Type, oldR.Name, oldR.Content)
}
return acc, oldR, nil
}
func findAccountForZone(c *dns.Client, args Args) (acc string, recs []dns.ZoneRecord, err error) {
// Loop over all accounts to find the one containing the relevant zone.
accopts := &dns.ListOptions{}
accounts, err := c.Accounts.ListAccounts(context.Background(), accopts)
if err != nil {
return "", nil, err
}
log.Printf("found %d accounts for token: %s\n", len(accounts.Data), accounts.Data[0].Email)
for _, a := range accounts.Data {
acc := fmt.Sprintf("%d", a.ID)
zropts := &dns.ZoneRecordListOptions{Name: dns.String(args.RecName), Type: dns.String(TXT)}
recs, err := c.Zones.ListRecords(context.Background(), acc, args.Domain, zropts)
if err != nil {
log.Printf("error listing records of account %s: %s", acc, err)
continue
}
records := recs.Data
log.Printf("found domain %s in account %s with %d records\n",
args.Domain, acc, len(records))
return acc, records, nil
}
return "", nil, fmt.Errorf("did not find account for: %s", args.Domain)
}
func createRecord(c *dns.Client, args Args, acc string) (*dns.ZoneRecord, error) {
newR := newRecord(args)
log.Println("will CreateRecord:", newR.ZoneID, newR.Type, *newR.Name, newR.Content)
res, err := c.Zones.CreateRecord(context.Background(), acc, args.Domain, *newR)
if err != nil {
return nil, fmt.Errorf("CreateRecord: %v", err)
}
log.Println("did CreateRecord:", res.Data.ZoneID, res.Data.Type, res.Data.Name, res.Data.Content)
return res.Data, nil
}
func updateRecord(c *dns.Client, args Args, acc string, oldR *dns.ZoneRecord) (*dns.ZoneRecord, error) {
// just update value
oldR.Content = DnslinkPrefix + args.Link
// we only want to change the value.
// looking at the API, it should only update what we ask it to update.
// (i was getting "regions not available in your plan")
newR := newRecord(args)
newR.ZoneID = oldR.ZoneID
log.Println("will UpdateRecord:", newR.ZoneID, newR.Type, *newR.Name, newR.Content)
res, err := c.Zones.UpdateRecord(context.Background(), acc, args.Domain, oldR.ID, *newR)
if err != nil {
return nil, fmt.Errorf("UpdateRecord: %v", err)
}
log.Println("did UpdateRecord:", res.Data.ZoneID, res.Data.Type, res.Data.Name, res.Data.Content)
return res.Data, nil
}
func newRecord(args Args) *dns.ZoneRecordAttributes {
return &dns.ZoneRecordAttributes{
Type: TXT,
Name: dns.String(args.RecName),
Content: DnslinkPrefix + args.Link,
TTL: args.TTL,
}
}