-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
104 lines (82 loc) · 2.6 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
package main
import (
"errors"
"log"
"net"
"os"
"github.com/dacruz/dns_updater/godaddy"
"github.com/dacruz/dns_updater/ipify"
)
const (
DNS_UPDATER_GO_DADDY_API_URL = "DNS_UPDATER_GO_DADDY_API_URL"
DNS_UPDATER_GO_DADDY_API_KEY = "DNS_UPDATER_GO_DADDY_API_KEY"
DNS_UPDATER_HOST = "DNS_UPDATER_HOST"
DNS_UPDATER_DOMAIN = "DNS_UPDATER_DOMAIN"
DNS_UPDATER_IPIFY_URL = "DNS_UPDATER_IPIFY_URL"
)
type configuration struct {
GoDaddyAPIUrl string
GoDaddyAPIKey string
Host string
Domain string
IpifyAPIUrl string
}
func main() {
log.SetPrefix("dns updater: ")
err := run()
if err != nil {
log.Fatal(err)
}
}
func run() error {
conf, err := loadConf()
if err != nil {
return err
}
currentIpChannel := make(chan net.IP)
go ipify.FetchCurrentIp(currentIpChannel, conf.IpifyAPIUrl)
currentDnsValueChannel := make(chan net.IP)
go godaddy.FetchCurrentRecordValue(currentDnsValueChannel, conf.GoDaddyAPIUrl, conf.Domain, conf.Host, conf.GoDaddyAPIKey)
currentIp, ok := <-currentIpChannel
if !ok {
return errors.New("could not fetch currect ip")
}
currentDnsValue, ok := <-currentDnsValueChannel
if !ok {
return errors.New("could not fetch currect dns value")
}
log.Printf("dns value: %s", currentDnsValue.String())
log.Printf("current ip: %s", currentIp.String())
if !currentDnsValue.Equal(currentIp) {
updatedDnsValue, err := godaddy.UpdateRecordValue(currentIp, conf.GoDaddyAPIUrl, conf.Domain, conf.Host, conf.GoDaddyAPIKey)
if err != nil {
return err
}
log.Printf("updatedDnsValue: %s", updatedDnsValue.String())
} else {
log.Println("no update required")
}
return nil
}
func loadConf() (configuration, error) {
config := configuration{}
goDaddyAPIUrl, goDaddyAPIUrlExists := os.LookupEnv(DNS_UPDATER_GO_DADDY_API_URL)
config.GoDaddyAPIUrl = goDaddyAPIUrl
goDaddyAPIKey, goDaddyAPIKeyExists := os.LookupEnv(DNS_UPDATER_GO_DADDY_API_KEY)
config.GoDaddyAPIKey = goDaddyAPIKey
host, hostExists := os.LookupEnv(DNS_UPDATER_HOST)
config.Host = host
domain, domainExists := os.LookupEnv(DNS_UPDATER_DOMAIN)
config.Domain = domain
ipifyAPIUrl, ipifyAPIUrlExists := os.LookupEnv(DNS_UPDATER_IPIFY_URL)
config.IpifyAPIUrl = ipifyAPIUrl
if !goDaddyAPIUrlExists || !goDaddyAPIKeyExists || !hostExists || !domainExists || !ipifyAPIUrlExists {
return config, errors.New("environment variable missing. e.g: " +
"export DNS_UPDATER_GO_DADDY_API_URL=FOO " +
"export DNS_UPDATER_GO_DADDY_API_KEY=BAR " +
"export DNS_UPDATER_HOST=BAZ " +
"export DNS_UPDATER_DOMAIN=OPA " +
"export DNS_UPDATER_IPIFY_URL=OMA")
}
return config, nil
}