forked from lastlogin-net/obligator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomains.go
185 lines (151 loc) · 3.26 KB
/
domains.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package obligator
import (
"errors"
"fmt"
"html/template"
"io"
"net"
"net/http"
"net/url"
)
type DomainHandler struct {
mux *http.ServeMux
}
func (h *DomainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mux.ServeHTTP(w, r)
}
func NewDomainHandler(db Database, tmpl *template.Template, cluster *Cluster, proxy Proxy, jose *JOSE) *DomainHandler {
mux := http.NewServeMux()
mux.HandleFunc("/domains", func(w http.ResponseWriter, r *http.Request) {
// TODO: can probably be done once at startup
ips, err := net.LookupIP(r.Host)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
var ipv4 net.IP
var ipv6 net.IP
for _, ip := range ips {
if ip.To4() != nil {
ipv4 = ip
} else {
ipv6 = ip
}
}
data := struct {
*commonData
Host string
Ipv4 string
Ipv6 string
}{
commonData: newCommonData(nil, db, r),
Host: r.Host,
Ipv4: ipv4.String(),
Ipv6: ipv6.String(),
}
err = tmpl.ExecuteTemplate(w, "domains.html", data)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
})
mux.HandleFunc("/add-domain", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
// TODO: sanitize domain
domain := r.Form.Get("domain")
if domain == "" {
w.WriteHeader(400)
io.WriteString(w, "Missing domain")
return
}
_, err := url.Parse(domain)
if err != nil {
w.WriteHeader(400)
io.WriteString(w, err.Error())
return
}
ownerId := r.Form.Get("owner_id")
idents, _ := getIdentities(db, r)
match := false
for _, ident := range idents {
if ident.Id == ownerId {
match = true
break
}
}
if !match {
w.WriteHeader(403)
io.WriteString(w, "You don't own that ID")
return
}
err = verifyIpsMatch(domain, r.Host)
if err != nil {
w.WriteHeader(400)
io.WriteString(w, err.Error())
return
}
//cname, err := getAuthoritativeCNAME(r.Context(), domain)
//if err != nil {
// w.WriteHeader(500)
// io.WriteString(w, err.Error())
// return
//}
//if cname != rootUrl.Host {
// fmt.Println(cname, rootUrl.Host)
// w.WriteHeader(400)
// io.WriteString(w, "CNAME != host")
// return
//}
primaryHost, err := cluster.PrimaryHost()
if err != nil {
// I *am* the primary
} else {
done := cluster.RedirectOrForward(primaryHost, w, r)
if done {
return
}
}
err = proxy.AddDomain(domain)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
err = db.AddDomain(domain, ownerId)
if err != nil {
w.WriteHeader(500)
io.WriteString(w, err.Error())
return
}
http.Redirect(w, r, fmt.Sprintf("https://%s/login", domain), 303)
})
h := &DomainHandler{
mux: mux,
}
return h
}
func verifyIpsMatch(domainA, domainB string) error {
aIps, err := net.LookupHost(domainA)
if err != nil {
return err
}
bIps, err := net.LookupHost(domainB)
if err != nil {
return err
}
for _, aIp := range aIps {
match := false
for _, bIp := range bIps {
if aIp == bIp {
match = true
break
}
}
if !match {
return errors.New("No matching IP. Make sure you either have either a CNAME or BOTH A and AAAA records set up.")
}
}
return nil
}