-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
dns.go
85 lines (70 loc) · 2.13 KB
/
dns.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
package main
import (
"net/http"
"encoding/json"
"net"
"strings"
"github.com/azukaar/cosmos-server/src/utils"
)
func CheckDNSRoute(w http.ResponseWriter, req *http.Request) {
if utils.LoggedInOnly(w, req) != nil {
return
}
if(req.Method == "GET") {
url := utils.SanitizeSafe(req.URL.Query().Get("url"))
url = strings.Split(url, ":")[0]
if url == "" {
utils.Error("CheckDNS", nil)
utils.HTTPError(w, "Internal server error: No URL requested", http.StatusInternalServerError, "DNS001")
return
}
errDNS := utils.CheckDNS(url)
if errDNS != nil {
utils.Error("CheckDNS", errDNS)
utils.HTTPError(w, "DNS Check error: " + errDNS.Error(), http.StatusInternalServerError, "DNS002")
return
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
})
} else {
utils.Error("CheckDNS: Method not allowed" + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}
func GetDNSRoute(w http.ResponseWriter, req *http.Request) {
if !utils.GetMainConfig().NewInstall && (utils.LoggedInOnly(w, req) != nil) {
return
}
if(req.Method == "GET") {
url := utils.SanitizeSafe(req.URL.Query().Get("url"))
url = strings.Split(url, ":")[0]
if url == "" {
utils.Error("CheckDNS", nil)
utils.HTTPError(w, "Internal server error: No URL requested", http.StatusInternalServerError, "DNS001")
return
}
ips, err := net.LookupIP(url)
if err != nil {
utils.Error("CheckDNS", err)
utils.HTTPError(w, "Internal server error: " + err.Error(), http.StatusInternalServerError, "DNS001")
return
}
for _, ip := range ips {
if ip.To4() != nil {
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
"data": ip.String(),
})
return
}
}
utils.Error("CheckDNS: No DNS entry found. Did you point the domain to your server?", nil)
utils.HTTPError(w, "Internal server error: " + err.Error(), http.StatusInternalServerError, "DNS001")
} else {
utils.Error("CheckDNS: Method not allowed" + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}