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
|
package controllers
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
godns "github.com/miekg/dns"
"code.crute.me/mcrute/go_ddns_manager/dns"
"code.crute.me/mcrute/go_ddns_manager/util"
"code.crute.me/mcrute/go_ddns_manager/web/middleware"
)
func UpdateDynamicDNS(c *gin.Context) {
cfg := middleware.GetServerConfig(c)
res := middleware.GetDDNSAuthKey(c)
if res == "" {
log.Println("ddns: Unable to get auth key")
c.AbortWithStatus(http.StatusForbidden)
return
}
zone, part := cfg.BindConfig.FindClosestZone(res, cfg.DynamicDnsView)
if zone == nil {
log.Println("ddns: Unable to locate zone")
c.AbortWithStatus(http.StatusNotFound)
return
}
inip := util.GetRequestIP(c)
if inip == nil {
log.Println("ddns: Unable to parse IP")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
txn := cfg.DNSClient.QueryRecursive(zone, res, godns.TypeA)
cur, err := cfg.DNSClient.SendQuery(txn)
if err == nil && len(cur) == 1 {
if cur[0].(*godns.A).A.Equal(inip) {
log.Printf("ddns: Update not required for '%s', IP %s", res, inip)
c.String(http.StatusNotModified, "")
return
}
}
txn = cfg.DNSClient.StartUpdate(zone).Upsert(&dns.A{
Name: part,
Ttl: 60,
A: inip,
})
if err := cfg.DNSClient.SendUpdate(txn); err != nil {
log.Printf("ddns: %s", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.String(http.StatusAccepted, "")
}
|