-
Notifications
You must be signed in to change notification settings - Fork 2
/
caddy-dyndns.go
226 lines (208 loc) · 4.49 KB
/
caddy-dyndns.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package dyndns
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"github.com/caddyserver/caddy"
"github.com/robfig/cron"
)
type Authentification struct {
Apikey string
Email string
}
type Config struct {
Provider string
Ipaddress string
Auth Authentification
Domains []string
Period string
Ipupdate string
}
var debug bool
func init() {
debug = os.Getenv("DDNS_DEBUG") == "on"
caddy.RegisterPlugin("dyndns", caddy.Plugin{Action: startup})
}
func startup(c *caddy.Controller) error {
return registerCallback(c, c.OnFirstStartup)
}
func getExternalIP(url string) (string, error) {
var addr string
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return addr, err
}
resp, err := client.Do(req)
if err != nil {
return addr, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return addr, err
}
defer resp.Body.Close()
addr = strings.Trim(string(body), " \r\n")
if addr != "" {
ip := net.ParseIP(addr)
if ip == nil {
return addr, err
}
}
return addr, nil
}
func getIP(intf string) string {
myip := intf
if intf == "remote" {
list, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, iface := range list {
//fmt.Printf("%d name=%s %v\n", i, iface.Name, iface)
addrs, err := iface.Addrs()
if err != nil {
panic(err)
}
for _, addr := range addrs {
fmt.Println(iface.Name, "->", addr.(*net.IPNet).IP)
if ipnet, ok := addr.(*net.IPNet); ok && isPublicIP(ipnet.IP) {
myip = ipnet.IP.String()
}
}
}
}
if intf == "local" {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err == nil {
ipnet := conn.LocalAddr().(*net.UDPAddr)
myip = ipnet.IP.String()
}
defer conn.Close()
}
if strings.Contains(intf, "http") {
var err error
myip, err = getExternalIP(intf)
if err != nil {
myip = ""
}
}
return myip
}
func isPublicIP(IP net.IP) bool {
if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
return false
}
if ip4 := IP.To4(); ip4 != nil {
switch true {
case ip4[0] == 10:
return false
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
return false
case ip4[0] == 192 && ip4[1] == 168:
return false
default:
return true
}
}
return false
}
func registerCallback(c *caddy.Controller, registerFunc func(func() error)) error {
var funcs []func() error
configs, err := parse(c)
if err != nil {
return err
}
for _, conf := range configs {
fn := func() error {
//<-------Cron begin
cr := cron.New()
cr.AddFunc("@every "+conf.Period, func() {
//Update record DNS
conf.Ipupdate = ""
if conf.Ipaddress != "" {
conf.Ipupdate = getIP(conf.Ipaddress)
}
switch conf.Provider {
case "cloudflare":
err = cloudflareupd(conf)
case "yandex":
err = yandexupd(conf)
case "dnspod":
err = dnspodupd(conf)
case "digitalocean":
err = digitaloceanupd(conf)
default:
err = cloudflareupd(conf)
}
})
cr.Start()
//Cron end------->
if err != nil {
return err
}
return nil
}
funcs = append(funcs, fn)
}
return c.OncePerServerBlock(func() error {
for _, fn := range funcs {
registerFunc(fn)
}
return nil
})
}
func parse(c *caddy.Controller) ([]Config, error) {
var configs []Config
for c.Next() { // skip the directive name
conf := Config{}
//No extra args expected
if len(c.RemainingArgs()) > 0 {
return configs, c.ArgErr()
}
for c.NextBlock() {
switch c.Val() {
case "provider":
if !c.NextArg() {
return configs, c.ArgErr()
}
conf.Provider = c.Val()
case "ipaddress":
if !c.NextArg() {
return configs, c.ArgErr()
}
conf.Ipaddress = c.Val()
case "auth":
args := c.RemainingArgs()
if len(args) > 2 {
return configs, c.ArgErr()
}
if len(args) == 0 {
return configs, c.ArgErr()
}
conf.Auth.Apikey = args[0]
if len(args) == 2 {
conf.Auth.Email = args[1]
}
case "domains":
args := c.RemainingArgs()
if len(args) == 0 {
return configs, c.ArgErr()
}
conf.Domains = args
case "period":
if !c.NextArg() {
return configs, c.ArgErr()
}
conf.Period = c.Val()
default:
return configs, c.ArgErr()
}
}
configs = append(configs, conf)
}
return configs, nil
}