-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoddns.go
123 lines (101 loc) · 2.8 KB
/
goddns.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
package main
import (
"fmt"
"log"
"net/http"
"github.com/svenhertle/goddns/backends"
)
// GoDDNS is a dynamic DNS server/updater
type GoDDNS struct {
cfg *config
backend backends.Backend
}
// NewGoDDNS creates a new GoDDNS instance
func NewGoDDNS(configfile string) *GoDDNS {
goddns := GoDDNS{}
// read config
goddns.cfg = getConfig(configfile)
// initialize backend
goddns.backend = backends.GetBackend(goddns.cfg.BackendName, goddns.cfg.backendViperConfig,
goddns.cfg.DNS.Domain, goddns.cfg.DNS.TTL)
// start web server
goddns.startHTTP(goddns.cfg.HTTP.Listen, goddns.cfg.HTTP.Port)
return &goddns
}
// startHTTP configured and starts the web server
func (goddns *GoDDNS) startHTTP(listen string, port int) {
// routes
http.HandleFunc("/", goddns.handleBase)
http.HandleFunc("/v1/", goddns.handleVersion1)
// listening address and port
listenAddress := fmt.Sprintf("[%s]:%d", listen, port)
log.Println("Listening on", listenAddress)
// run
var err error
if goddns.cfg.HTTP.TLS.Cert != "" && goddns.cfg.HTTP.TLS.Key != "" {
// with TLS
err = http.ListenAndServeTLS(listenAddress, goddns.cfg.HTTP.TLS.Cert, goddns.cfg.HTTP.TLS.Key, nil)
} else {
// without TLS
err = http.ListenAndServe(listenAddress, nil)
}
if err != nil {
log.Fatal("Starting the web server failed: ", err)
}
}
// handleBase handles HTTP requests on "/"
func (goddns *GoDDNS) handleBase(w http.ResponseWriter, r *http.Request) {
http.Error(w, "", http.StatusNotFound)
}
// handleVersion1 handles HTTP requests by clients (v1)
func (goddns *GoDDNS) handleVersion1(w http.ResponseWriter, r *http.Request) {
// get key
key := r.URL.Query().Get("key")
ip := r.URL.Query().Get("ip")
// check if key is given
if key == "" {
http.Error(w, "key missing", http.StatusBadRequest)
return
}
// check if key is valid for a client
var name string
for _, client := range goddns.cfg.Clients {
if client.Key == key {
name = client.Name
break
}
}
if name == "" {
http.Error(w, "key unknown", http.StatusBadRequest)
return
}
// get remote ip if we did not get it
if ip == "" {
if goddns.cfg.HTTP.BehindProxy {
ip = GetIPBehindProxy(r)
} else {
ip = GetIPDirect(r)
}
if ip == "" {
http.Error(w, "internal error", http.StatusInternalServerError)
log.Println("Cannot determine IP address of client")
return
}
}
// validate ip
addressType, ipValid := CheckIP(ip)
if !ipValid {
http.Error(w, "ip invalid", http.StatusBadRequest)
log.Printf("Got invalid IP address %s for client %s\n", ip, name)
return
}
// run the update
log.Printf("New IP for %s: %s\n", name, ip)
err := goddns.backend.Update(name, ip, addressType)
if err != nil {
http.Error(w, "internal error", http.StatusInternalServerError)
log.Println("Error while updating an entry:", err)
return
}
fmt.Fprintf(w, "ok\n")
}