-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
407 lines (326 loc) · 10.2 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package main
import (
"database/sql"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
_ "github.com/go-sql-driver/mysql"
"golang.org/x/crypto/bcrypt"
)
/*
/api?ip=xxxxx (optional, use request ip if not submitted)
host=foo
domain=example.com
key=apikey
*/
// DNSRecord is a struct to collect the data collected by request to update the database
type DNSRecord struct {
host string
domain string
accessKey string
ip string
}
// GetIP gets a requests IP address by reading off the forwarded-for
// header (for proxies) and falls back to use the remote address.
// check x-forwarded for later...
func GetIP(r *http.Request) (string, error) {
forwarded := r.Header.Get("X-FORWARDED-FOR")
if forwarded != "" {
parsedIP := net.ParseIP(forwarded)
if parsedIP != nil {
return net.IP.String(parsedIP), nil
}
ip, _, err := net.SplitHostPort(forwarded)
if err != nil {
log.Printf("forwarded for: %s is not IP:port\n", forwarded)
return "", errors.New(err.Error())
}
return ip, nil
}
forwarded = r.Header.Get("X-Real-Ip")
if forwarded != "" {
parsedIP := net.ParseIP(forwarded)
if parsedIP != nil {
return net.IP.String(parsedIP), nil
}
ip, _, err := net.SplitHostPort(forwarded)
if err != nil {
log.Printf("X-Real-Ip: %s is not IP:port\n", forwarded)
return "", errors.New(err.Error())
}
return ip, nil
}
parsedIP := net.ParseIP(r.RemoteAddr)
if parsedIP != nil {
return net.IP.String(parsedIP), nil
}
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Printf("userip: %q is not IP:port\n", r.RemoteAddr)
return "", errors.New(err.Error())
}
return ip, nil
}
// Handle all requests which do not match
func handleEverything(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Unknown request")
ip, err := GetIP(r)
if err != nil {
log.Printf("Unknown request from unknown IP address")
return
}
log.Printf("Unknown request from IP " + ip)
return
}
// Validate domain and access key
func validateRequest(dnsrecord DNSRecord) (bool, error) {
//log.Println(dnsrecord)
db := dbConn()
var count int
// 1. check api key and domain
var hashedPassword string
err := db.QueryRow("SELECT d.access_key FROM domains d WHERE d.domainname=?", dnsrecord.domain).Scan(&hashedPassword)
defer db.Close()
if err != nil {
log.Println("Database query problem: " + err.Error())
return false, errors.New("Database query problem: " + err.Error())
}
accessKeyBytes := []byte(dnsrecord.accessKey) // convert submitted api key into []byte
hashedPasswordBytes := []byte(hashedPassword) // the hashed password from database has to be converted, too
err = bcrypt.CompareHashAndPassword(hashedPasswordBytes, accessKeyBytes)
if err != nil {
log.Println("Wrong API key")
return false, nil // no error, but wrong API key
}
// 2. check if host entry exists
err = db.QueryRow("SELECT count(*) as cnt FROM dynrecords r, domains d WHERE d.id=r.domain_id AND r.hostname=? AND d.domainname=? AND d.access_key=?", dnsrecord.host, dnsrecord.domain, hashedPassword).Scan(&count)
if err != nil {
log.Println("Database query problem: " + err.Error())
return false, errors.New("Database query problem: " + err.Error())
}
if count != 1 {
return false, errors.New("Wrong number of hosts found")
}
return true, nil
}
// Update SOA entry in PowerDNS records table
func updateSoa(dnsrecord DNSRecord) (bool, error) {
// get SOA entry
log.Printf("get SOA entry from records")
db := dbConnPdns()
var content string
err := db.QueryRow("SELECT content FROM records WHERE type=? AND name=?", "SOA", dnsrecord.domain).Scan(&content)
if err != nil {
log.Println("Database problem: " + err.Error())
return false, errors.New(err.Error())
}
defer db.Close()
// split content into its parts, increase serial to perform AXFR
var contentParts = strings.Split(content, " ")
//log.Println(contentParts)
//log.Println(contentParts[2])
var serial, _ = strconv.ParseInt(contentParts[2], 10, 64)
serial = serial + 2
//log.Println(serial)
var serialString = strconv.FormatInt(int64(serial), 10)
//log.Println(serialString)
contentParts[2] = serialString
//log.Println(contentParts)
var contentModified = strings.Join(contentParts, " ")
//log.Println(contentModified)
updateStmt, err := db.Prepare("UPDATE records SET content=? WHERE type=? AND name=?")
if err != nil {
log.Println("Update problem: " + err.Error())
return false, errors.New(err.Error())
//os.Exit(1)
}
_, err = updateStmt.Exec(contentModified, "SOA", dnsrecord.domain)
if err != nil {
log.Println("Update problem: " + err.Error())
return false, errors.New(err.Error())
}
return true, nil
}
// Update host in PowerDNS record table with new IP address
func updateRecord(dnsrecord DNSRecord) (bool, error) {
// get SOA entry
db := dbConnPdns()
defer db.Close()
updateStmt, err := db.Prepare("UPDATE records SET content=? WHERE type=? AND name=?")
if err != nil {
log.Println("Prepare record update problem: " + err.Error())
return false, errors.New("Prepare record update problem: " + err.Error())
}
_, err = updateStmt.Exec(dnsrecord.ip, "A", dnsrecord.host+"."+dnsrecord.domain)
if err != nil {
log.Println("Update problem: " + err.Error())
return false, errors.New("Update record problem: " + err.Error())
}
return true, nil
}
// Update dynrecords table with new timestamp of last update
func updateDynRecords(dnsrecord DNSRecord) (bool, error) {
db := dbConn()
defer db.Close()
updateStmt, err := db.Prepare("UPDATE dynrecords r, domains d SET r.host_updated=now() WHERE r.hostname=? AND r.domain_id=d.id AND d.domainname=?")
if err != nil {
log.Println("Prepare update problem: " + err.Error())
return false, errors.New("Prepare update problem: " + err.Error())
//os.Exit(1)
}
_, err = updateStmt.Exec(dnsrecord.host, dnsrecord.domain)
if err != nil {
log.Println("Update problem: " + err.Error())
return false, errors.New("Update problem: " + err.Error())
}
return true, nil
}
// Update PowerDNS and dynpower database after validating request
func updateEntry(dnsrecord DNSRecord) (bool, error) {
//log.Println(dnsrecord)
var err error
requestValid, err := validateRequest(dnsrecord)
if err != nil {
log.Println("Error by validating request data " + err.Error())
return false, errors.New("Error by validating request data " + err.Error())
}
if requestValid == false {
log.Println("Invalid request data, please check host, domain and key")
return false, errors.New("Invalid request data, please check host, domain and key")
}
log.Println("Data valid, now update!")
_, err = updateRecord(dnsrecord)
if err != nil {
log.Println("Error by updating records entry")
return false, errors.New("Error by updating records entry")
}
_, err = updateSoa(dnsrecord)
if err != nil {
log.Println("Error by updating SOA entry")
return false, errors.New("Error by updating SOA entry")
}
_, err = updateDynRecords(dnsrecord)
if err != nil {
log.Println("Error by updating dynrecords entry")
return false, errors.New("Error by updating dynrecords entry")
}
log.Println("All records updated")
return true, nil
}
// Handle the update request: get request data and call update function in case of no error
func handleUpdate(w http.ResponseWriter, r *http.Request) {
var err error
var record DNSRecord
q := r.URL.Query()
//fmt.Println(q["key"])
key := q.Get("key")
if len(key) < 1 {
log.Println("Missing API key")
fmt.Fprintf(w, "error")
return
}
//log.Println(key)
host := q.Get("host")
if len(host) < 1 {
log.Println("Host entry is missing")
fmt.Fprintf(w, "error")
return
}
//fmt.Println(host)
domain := q.Get("domain")
if len(domain) < 1 {
log.Println("Domain entry is missing")
fmt.Fprintf(w, "error")
return
}
// get ip from query string or from request
record.host = host
record.domain = domain
record.accessKey = key
// todo maybe: use HTTP error codes
ip := q.Get("ip") // prefer submitted IP over request IP
if len(ip) < 1 {
ip, err = GetIP(r)
if err != nil {
log.Println("Could not get IP address, exiting...")
fmt.Fprintf(w, "An error occurred, see log entry.")
return
}
} else if net.ParseIP(ip) == nil {
log.Println("Invalid IP, exiting...")
fmt.Fprintf(w, "An error occurred, see log entry.")
return
}
record.ip = ip
log.Println("Try to update record...")
_, err = updateEntry(record)
if err != nil {
log.Println("Error: " + err.Error())
fmt.Fprintf(w, "An error occurred, see log entry.")
return
}
fmt.Fprintf(w, "ok")
return
}
// Create connection to dynpower database
func dbConn() (db *sql.DB) {
dbname := os.Getenv("DBNAME")
dbhost := os.Getenv("DBHOST")
dbuser := os.Getenv("DBUSER")
dbpassword := os.Getenv("DBPASSWORD")
db, err := sql.Open("mysql", dbuser+":"+dbpassword+"@tcp("+dbhost+":3306)/"+dbname)
if err != nil {
log.Println("Error by connecting database.")
panic(err.Error())
}
return db
}
// Create connection to PowerDNS database
func dbConnPdns() (db *sql.DB) {
dbname := os.Getenv("PDNS_DBNAME")
dbhost := os.Getenv("PDNS_DBHOST")
dbuser := os.Getenv("PDNS_DBUSER")
dbpassword := os.Getenv("PDNS_DBPASSWORD")
db, err := sql.Open("mysql", dbuser+":"+dbpassword+"@tcp("+dbhost+":3306)/"+dbname)
if err != nil {
log.Println("Error by connecting database.")
panic(err.Error())
}
return db
}
// Check database connection by performing a query, exit in error case
func checkDb() {
db := dbConn()
_, err := db.Query("SELECT r.hostname, d.domainname, d.access_key FROM dynrecords r, domains d WHERE d.id=r.domain_id")
if err != nil {
log.Println("Database problem: " + err.Error())
os.Exit(1)
//panic(err.Error()) // proper error handling instead of panic in your app
}
return
}
// Check PowerDNS database connection by performing a query, exit in error case
func checkDbPdns() {
db := dbConnPdns()
_, err := db.Query("SELECT * FROM domains")
if err != nil {
log.Println("PowerDNS database problem: " + err.Error())
os.Exit(1)
//panic(err.Error()) // proper error handling instead of panic in your app
}
return
}
func main() {
checkDb()
checkDbPdns()
log.Println("dynpower server started...")
http.HandleFunc("/", handleEverything)
http.HandleFunc("/api", handleEverything)
http.HandleFunc("/api/update", handleUpdate)
http.ListenAndServe(":8080", nil)
}