/
main.go
127 lines (107 loc) · 2.94 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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/miekg/dns"
"log"
"net/http"
"os"
)
var authuser, authpassword string
func main() {
port := envStringOrDefault("PORT", "5000")
authuser = envStringOrDefault("AUTH_USER", "user")
authpassword = envStringOrDefault("AUTH_PASSWORD", "changeme")
r := mux.NewRouter()
r.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
r.HandleFunc("/lookup/{hostname}", LookupHandler).Methods("GET")
r.HandleFunc("/verify_target/{hostname1}/{hostname2}", VerifyTargetHandler).
Methods("GET")
http.Handle("/", r)
err := loadRootConfig()
if err != nil {
log.Fatal("Error loading root config:", err.Error())
}
err = http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func envStringOrDefault(name, defaultvalue string) string {
val := os.Getenv(name)
if val == "" {
return defaultvalue
}
return val
}
func getBasicAuth(r *http.Request) (username string, password string) {
authString := r.Header.Get("Authorization")
if len(authString) > 6 && string(authString[0:5]) == "Basic" {
creds, err := base64.StdEncoding.DecodeString(authString[6:])
if err != nil {
log.Printf("level=error type=credential_decode_error message=%q", err.Error())
return "", ""
}
username, password = split(string(creds), ':', true)
}
return
}
func split(s string, c byte, cutc bool) (string, string) {
for i := 0; i < len(s); i++ {
if s[i] == c {
if cutc {
return s[0:i], s[i+1:]
}
return s[0:i], s[i:]
}
}
return s, ""
}
func checkAuth(r *http.Request, username string, password string) bool {
u, p := getBasicAuth(r)
return u == username && p == password
}
func UnauthorizedResponse(w http.ResponseWriter) {
http.Error(w, "{\"error\": \"unauthorized\"}", 401)
}
func NotFoundHandler(w http.ResponseWriter, req *http.Request) {
http.Error(w, "{\"error\": \"not found\"}", 404)
}
func LookupHandler(w http.ResponseWriter, req *http.Request) {
if !checkAuth(req, authuser, authpassword) {
UnauthorizedResponse(w)
return
}
domainname := dns.Fqdn(mux.Vars(req)["hostname"])
nocache := req.URL.Query().Get("nocache") != ""
cr, err := LookupDNS(domainname, nocache)
if err != nil {
http.Error(w, fmt.Sprintf("{\"error\": %q}", err.Error()), 500)
return
}
json.NewEncoder(w).Encode(cr)
return
}
func VerifyTargetHandler(w http.ResponseWriter, req *http.Request) {
if !checkAuth(req, authuser, authpassword) {
UnauthorizedResponse(w)
return
}
hostname1 := dns.Fqdn(mux.Vars(req)["hostname1"])
hostname2 := dns.Fqdn(mux.Vars(req)["hostname2"])
nocache := req.URL.Query().Get("nocache") != ""
target_alias := req.URL.Query().Get("target_alias")
if target_alias != "" {
target_alias = dns.Fqdn(target_alias)
}
vr, err := VerifyTarget(hostname1, hostname2, target_alias, nocache)
if err != nil {
w.WriteHeader(500)
json.NewEncoder(w).Encode(vr.Error)
return
}
json.NewEncoder(w).Encode(vr)
return
}