-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_add.go
65 lines (61 loc) · 1.41 KB
/
api_add.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
// Copyright 2021 Sean.ZH
package main
import (
"log"
"net/http"
"github.com/miekg/dns"
)
func (a *ApiHandler) AddRecord(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Write(a.BadMethodMsg)
log.Println("bad method for add record:", r.Method)
return
}
var record DNSRecord
err := a.UnjsonRequest(r, &record)
if err != nil {
log.Println("unjson req error:", err)
w.Write(a.BadRequestMsg)
return
}
record.Name = dns.Fqdn(record.Name)
cf, ok := TypeHandlerList[record.Type]
if !ok {
log.Println("not supported type:", record.Type)
w.Write(a.NotSupportedType)
return
}
err = CommonCheck(&record)
if err != nil {
log.Println("failed of common check:", err)
w.Write(a.BadRecordValue)
return
}
err = cf.CheckRecord(&record)
if err != nil {
w.Write(a.BadRecordValue)
log.Println("check record:", err)
return
}
conflictList := []uint16{dns.TypeNS, dns.TypeCNAME}
for _, tp := range conflictList {
// more than one ns records does not conflict
if tp == dns.TypeNS && record.Type == dns.TypeNS {
continue
}
ret, err := a.DB.Find(record.Name, tp)
if err == nil && len(ret) > 0 {
log.Println("conflict", dns.Type(record.Type), "with", dns.Type(tp))
w.Write(a.TypeConflictMsg)
return
}
}
err = a.DB.Insert(record)
if err != nil {
log.Println("db insert error:", err)
w.Write(a.DBErrMsg)
return
}
record.Msg = "ok"
a.JsonResponse(w, record)
}