-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (94 loc) · 2.05 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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/razorcorp/dyndns/cloudflare"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
type ipv4 struct {
IP string `json:"ip"`
}
func logger(msg interface{}) {
log.Printf("%#v", msg)
}
func runner() {
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
os.Exit(2)
}
}()
logger("Initiating IP update")
logger("Cloudflare DNS host found")
var cf = cloudflare.Cloudflare{}
cf.Proxied = true
if token, ok := os.LookupEnv("CF_token"); !ok {
panic(errors.New("cloudflare Token not set"))
} else {
logger("Cloudflare token found")
cf.Token = token
}
if domain, ok := os.LookupEnv("Domain"); !ok {
panic(errors.New("domain name not set"))
} else {
logger("Domain: " + domain)
cf.Domain = domain
}
if hostname, ok := os.LookupEnv("Subdomain"); !ok {
logger(hostname)
panic(errors.New("subdomain not set"))
} else {
cf.Hostname = hostname
}
if _, ok := os.LookupEnv("CF_proxy_disabled"); !ok {
logger("CF_proxy_disabled not set")
} else {
cf.Proxied = false
}
logger("Fetching new IP address")
ip := new(ipv4)
client := new(http.Client)
if req, err := http.NewRequest(http.MethodGet, "https://api.ipify.org?format=json", nil); err != nil {
panic(err)
} else {
if resp, err := client.Do(req); err != nil {
panic(err)
} else {
defer resp.Body.Close()
if resp.StatusCode != 200 {
panic(resp.Status)
} else if err := json.NewDecoder(resp.Body).Decode(&ip); err != nil {
panic(err)
}
}
}
logger(ip.IP)
cf.IpAddress = ip.IP
logger("Updating DNS record")
if err := cf.UpdateRecordSet(); err != nil {
panic(err)
}
logger("Update completed")
}
func main() {
interval := flag.Int64("freq", 2, "IP update frequency in hours. Default 2h")
flag.Parse()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
s := <-sigs
log.Printf("RECEIVED SIGNAL: %s", s)
os.Exit(1)
}()
for {
runner()
time.Sleep(time.Hour * time.Duration(*interval))
}
}