-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprep.go
258 lines (210 loc) · 6.03 KB
/
prep.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package server
import (
"net"
"net/http"
"net/url"
"strconv"
"strings"
)
const (
ERR_NO_ERR = iota
ERR_BAD_URL
ERR_SERVER_OFFLINE
ERR_WEBSITE_OFFLINE
ERR_DOMAIN_NOT_FOUND
ERR_SUBDOMAIN_NOT_FOUND
)
func (route *Route) prep() {
route.prepRemoteAddress()
err := route.prepRequestURI()
if err != nil {
route.err = ERR_BAD_URL
return
}
route.prepLogRequestURI()
route.DomainName, route.SubdomainName = prepDomainAndSubdomainNames(route.R)
if route.IsInternalConn() {
route.prepDomainAndSubdomainLocal()
}
route.prepHost()
route.err = route.prepDomainAndSubdomain()
}
func (route *Route) prepRemoteAddress() {
var err error
route.RemoteAddress, _, err = net.SplitHostPort(route.R.RemoteAddr)
if err != nil {
route.RemoteAddress = route.R.RemoteAddr
}
}
// prepRequestURI parses the requestURI incoming; in particular:
// - it sanitizes the path of the url
// - it sanitizes the query part of the url
// - creates the query map looking both for keys with or without a value
func (route *Route) prepRequestURI() (err error) {
splitPath := strings.Split(route.RequestURI, "?")
route.RequestURI, err = url.PathUnescape(splitPath[0])
if err != nil {
return
}
var query string
route.QueryMap = make(map[string]string)
if len(splitPath) > 1 && splitPath[1] != "" {
query, err = url.QueryUnescape(splitPath[1])
if err != nil {
return
}
requestQueries := strings.Split(query, "&")
for _, x := range requestQueries {
if strings.Contains(x, "=") {
if strings.HasPrefix(x, "=") {
continue
}
queryParsed := strings.Split(x, "=")
route.QueryMap[queryParsed[0]] = queryParsed[1]
} else {
route.QueryMap[x] = ""
}
}
}
return
}
func (route *Route) prepLogRequestURI() {
route.logRequestURI = "\"" + route.RequestURI + "\""
if len(route.QueryMap) != 0 {
s := " ["
i := 0
for key, value := range route.QueryMap {
if i != 0 {
s += " "
}
s += "<" + key + ">"
if value != "" {
s += ":<" + value + ">"
}
i++
}
s += "]"
route.logRequestURI += s
}
}
// prepDomainAndSubdomainNames parses the incoming request and separates
// the domain part from the subdomain part, just from a "string" standpoint
func prepDomainAndSubdomainNames(r *http.Request) (string, string) {
host, _, err := net.SplitHostPort(r.Host)
if err != nil {
host, _, err = net.SplitHostPort(r.Host + ":0")
if err != nil {
return r.Host, ""
}
}
if strings.HasSuffix(host, "127.0.0.1") || strings.HasSuffix(host, "::1") {
return "localhost", ""
}
split := strings.Split(host, ".")
len := len(split)
switch len {
case 1:
return host, ""
default:
if _, err = strconv.Atoi(split[len-1]); err == nil {
return host, ""
}
if strings.HasSuffix(host, "localhost") {
return split[len-1], strings.Join(split[:len-1], ".") + "."
}
if len == 2 {
return split[len-2] + "." + split[len-1], strings.Join(split[:len-2], ".")
}
return split[len-2] + "." + split[len-1], strings.Join(split[:len-2], ".") + "."
}
}
func prepSubdomainName(name string) string {
if name != "" && name != "*" && !strings.HasSuffix(name, ".") {
name += "."
}
if name == "www." {
name = ""
}
return name
}
// prepDomainAndSubdomain uses the previously parsed domain and subdomain
// to find the effective Domain and Subdomain structures and link them to
// the Route
func (route *Route) prepDomainAndSubdomain() int {
route.Domain = route.Srv.domains[route.DomainName]
if route.Domain == nil {
route.Domain = route.Srv.domains[""]
if route.Domain == nil {
route.Domain = new(Domain)
route.Subdomain = &Subdomain { Name: "" }
route.Website = &Website { Name: "Not Found" }
if net.ParseIP(route.DomainName) == nil {
route.Domain.Name = "Domain NF"
} else {
route.Domain.Name = "DIPA"
}
return ERR_DOMAIN_NOT_FOUND
}
}
route.Subdomain = route.Domain.subdomains[route.SubdomainName]
if route.Subdomain == nil {
route.Subdomain = route.Domain.subdomains["*"]
if route.Subdomain == nil {
route.Subdomain = &Subdomain { Name: "Subdomain NF" }
route.Website = &Website { Name: "Not Found" }
return ERR_SUBDOMAIN_NOT_FOUND
}
}
route.Website = route.Subdomain.website
return ERR_NO_ERR
}
// prepDomainAndSubdomainLocal should be called only when the connection is local:
// this gives the capability of a local network user to access all the domains and
// subdomains served by the server from a local, insecure connection (for example
// via "http://localhost/?domain=mydomain.com&subdomain=mysubdomain").
// This feature is available for testing: an offline domain/subdomain can still be
// accessed from the local network
func (route *Route) prepDomainAndSubdomainLocal() {
host := route.DomainName
hostSD := route.SubdomainName
savedConfig, ok := route.Router.offlineClients[route.RemoteAddress]
if ok {
route.DomainName = savedConfig.domain
route.SubdomainName = savedConfig.subdomain
}
queryDomain, ok := route.QueryMap["domain"]
if ok {
route.DomainName = queryDomain
}
if route.DomainName == "" {
route.DomainName = host
}
querySubdomain, ok := route.QueryMap["subdomain"]
if ok {
route.SubdomainName = querySubdomain
}
if route.SubdomainName == "" {
route.SubdomainName = hostSD
}
route.SubdomainName = prepSubdomainName(route.SubdomainName)
route.Router.offlineClients[route.RemoteAddress] = offlineClient {
route.DomainName, route.SubdomainName,
}
}
// prepHost joins the previously parsed domain and subdomain to create the
// full host name
func (route *Route) prepHost() {
if route.SubdomainName != "" {
route.Host = route.SubdomainName
}
route.Host += route.DomainName
}
// IsInternalConn tells wheather the incoming connection should be treated
// as a local connection. The user can add a filter that can extend this
// selection to match their needs
func (route *Route) IsInternalConn() bool {
if strings.Contains(route.RemoteAddress, "localhost") || strings.Contains(route.RemoteAddress, "127.0.0.1") || strings.Contains(route.RemoteAddress, "::1") {
return true
}
return route.Router.isInternalConn(route.RemoteAddress)
}