-
Notifications
You must be signed in to change notification settings - Fork 1
/
fakeddns_handler.go
66 lines (61 loc) · 1.54 KB
/
fakeddns_handler.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
package app
import (
"github.com/dgoujard/rfc2136_web_api/util/rfc2136"
"net/http"
"strings"
)
// swagger:route GET /nic/update FakeDns FakeDDNSNoIP
// Return None
// responses:
// 200: HealthRespOk
// 404: DnsRecordRespNotFound
func (app *App) HandleFakeDDNSNoIP(w http.ResponseWriter, r *http.Request) {
myip := r.URL.Query().Get("myip")
ipRecordType := "A"
if(strings.Contains(myip, ":")) {
ipRecordType = "AAAA"
}
hostname := r.URL.Query().Get("hostname")
parts := strings.Split(hostname, ".")
zone := parts[len(parts)-2] + "." + parts[len(parts)-1]
recordArr := parts[:len(parts)-2]
recordFqdn := strings.Join(recordArr,".")+"."+zone+"."
var found = false
for _, item := range app.zones {
if item == zone {
found = true
}
}
if !found {
http.Error(w, http.StatusText(404), 404)
return
}
zoneRecords , err := app.rfc2136.GetRecordsForZone(zone)
if err != nil {
http.Error(w, err.Error(), 422)
return
}
var zoneRecordNeedUpdate = true;
for _, zoneRecord := range zoneRecords {
if recordFqdn == zoneRecord.Fqdn {
if zoneRecord.Values[0] == myip && zoneRecord.Type == ipRecordType{
zoneRecordNeedUpdate = false
}else{
app.rfc2136.DeleteRecordForZone(zone,&zoneRecord)
}
}
}
w.WriteHeader(http.StatusOK)
if zoneRecordNeedUpdate {
newDnsRecord := rfc2136.DnsRecord{
Fqdn: recordFqdn,
Type: ipRecordType,
Values: []string{myip},
TTL: 0,
}
app.rfc2136.AddRecordForZone(zone,&newDnsRecord)
w.Write([]byte("Updated"))
}else{
w.Write([]byte("No change"))
}
}