-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
138 lines (107 loc) · 3.29 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
package main
import (
"context"
"github.com/digitalocean/godo"
"github.com/sirupsen/logrus"
"github.com/weppos/publicsuffix-go/publicsuffix"
"golang.org/x/oauth2"
"net/http"
"os"
)
// --------------------------------------------------------------------------------------------------------------------
type TokenSource struct {
AccessToken string
}
// --------------------------------------------------------------------------------------------------------------------
func main() {
if len(os.Getenv("GODOHOST")) == 0 {
if err := os.Setenv("GODOHOST", ":80"); err != nil {
logrus.WithError(err).Error("setenv default GODOHOST")
}
}
http.HandleFunc("/update", update)
logrus.Info("Start.. listen on " + os.Getenv("GODOHOST"))
logrus.WithError(http.ListenAndServe(os.Getenv("GODOHOST"), nil)).Error("listenAndServe")
}
// --------------------------------------------------------------------------------------------------------------------
func (t *TokenSource) Token() (*oauth2.Token, error) {
token := &oauth2.Token{
AccessToken: t.AccessToken,
}
return token, nil
}
// --------------------------------------------------------------------------------------------------------------------
func update(w http.ResponseWriter, r *http.Request) {
host := r.URL.Query().Get("h")
parsedHost, err := publicsuffix.Parse(host)
if err != nil {
logrus.WithField("host", host).Error("host error")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`notfqdn`))
return
}
ip := r.URL.Query().Get("ip")
if len(r.URL.Query().Get("ip")) < 7 {
ip = r.RemoteAddr
}
tokenSource := &TokenSource{
AccessToken: r.URL.Query().Get("p"),
}
client := godo.NewClient(oauth2.NewClient(context.Background(), tokenSource))
ctx := context.TODO()
logrus.WithFields(logrus.Fields{"host": host, "ip": ip}).Info("new update")
domains, err := domainList(ctx, client, parsedHost.SLD+"."+parsedHost.TLD)
if err != nil {
logrus.WithError(err).Error("get domains")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`badauth`))
return
}
for _, d := range domains {
if d.Name == parsedHost.TRD {
if d.Data == ip {
w.Write([]byte(`nochg ` + ip))
return
}
editRequest := &godo.DomainRecordEditRequest{
Data: ip,
}
domainRecord, _, err := client.Domains.EditRecord(ctx, parsedHost.SLD+"."+parsedHost.TLD, d.ID, editRequest)
if err != nil {
logrus.WithError(err).Error("get domains")
w.WriteHeader(http.StatusInternalServerError)
return
}
if domainRecord.Data == ip {
w.Write([]byte(`good ` + domainRecord.Data))
return
}
w.Write([]byte(`dnserr`))
return
}
}
w.Write([]byte(`nohost`))
}
// --------------------------------------------------------------------------------------------------------------------
func domainList(ctx context.Context, client *godo.Client, domain string) ([]godo.DomainRecord, error) {
list := []godo.DomainRecord{}
opt := &godo.ListOptions{}
for {
domains, resp, err := client.Domains.Records(ctx, domain, opt)
if err != nil {
return nil, err
}
for _, d := range domains {
list = append(list, d)
}
if resp.Links == nil || resp.Links.IsLastPage() {
break
}
page, err := resp.Links.CurrentPage()
if err != nil {
return nil, err
}
opt.Page = page + 1
}
return list, nil
}