-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvhost.go
353 lines (311 loc) · 9.05 KB
/
vhost.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2021 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ship
import (
"errors"
"fmt"
"net/http"
"strings"
"sync"
)
// IsDomainName is used to check whether the domain name is valid or not.
// And you can reset it to a customized one.
//
// The default implementation has these limits as follow:
// - The maximum length of the full qualified domain name is equal to 253.
// - The maximum length of the sub-domain name is equal to 63.
// - The valid characters only contain "a-zA-z0-9_-.".
//
var IsDomainName func(domainName string) bool = isDomainName
// HostManager is used to manage the domain hosts.
type HostManager interface {
// Len returns the number of the hosts.
Len() int
// Range is used to traverse all the hosts.
Range(func(host string, handler http.Handler))
// AddHost adds the host and the handler, then returns the added handler.
//
// If the host has been added, return the added handler.
AddHost(host string, handler http.Handler) (http.Handler, error)
// DelHost deletes the host accurately and returns the handler.
//
// If the host does not exist, return nil.
DelHost(host string) http.Handler
// GetHost matches the host accurately and returns the handler.
//
// If the host does not exist, return nil.
GetHost(host string) http.Handler
// MatchHost matches the host by the implemented rules, such as the regular
// expression, and returns the corresponding handler.
//
// If there is no host to match it, return ("", nil).
MatchHost(host string) (matchedHost string, matchedHandler http.Handler)
}
type lockHostManager struct {
lock sync.RWMutex
hosts HostManager
}
// NewLockHostManager returns a new HostManager based on lock
// to manage the hosts safely.
func NewLockHostManager(hm HostManager) HostManager {
return &lockHostManager{hosts: hm}
}
func (h *lockHostManager) Len() int {
h.lock.RLock()
_len := h.hosts.Len()
h.lock.RUnlock()
return _len
}
func (h *lockHostManager) Range(f func(host string, handler http.Handler)) {
h.lock.RLock()
h.hosts.Range(f)
h.lock.RUnlock()
}
func (h *lockHostManager) AddHost(host string, handler http.Handler) (
http.Handler, error) {
h.lock.Lock()
handler, err := h.hosts.AddHost(host, handler)
h.lock.Unlock()
return handler, err
}
func (h *lockHostManager) DelHost(host string) http.Handler {
h.lock.Lock()
handler := h.hosts.DelHost(host)
h.lock.Unlock()
return handler
}
func (h *lockHostManager) GetHost(host string) http.Handler {
h.lock.RLock()
handler := h.hosts.GetHost(host)
h.lock.RUnlock()
return handler
}
func (h *lockHostManager) MatchHost(host string) (string, http.Handler) {
h.lock.RLock()
host, handler := h.hosts.MatchHost(host)
h.lock.RUnlock()
return host, handler
}
type hostManager struct {
total int
exacts map[string]http.Handler
prefixs map[string]http.Handler
suffixs map[string]http.Handler
regexps HostManager
}
// NewHostManager returns a new HostManager implementation,
// which uses IsDomainName to check whether a host name is the valid domain
// and supports three kinds of hosts:
//
// - Exact: a valid domain, such as "www.example.com".
// - Prefix: a valid domain with the suffix ".*", such as "www.example.*".
// - Suffix: a valid domain with the prefix "*.", such as "*.example.com".
// - Regexp: a valid regular expression defined by regexpHostManager.
//
// Notice: if the host name is not any of the exact, prefix and suffix formats,
// it will be regarded as the regexp host name.
//
// If regexpHostManager is nil, it is NewRegexpHostManager() by default.
func NewHostManager(regexpHostManager HostManager) HostManager {
if regexpHostManager == nil {
regexpHostManager = NewRegexpHostManager()
}
return &hostManager{
exacts: make(map[string]http.Handler, 4),
prefixs: make(map[string]http.Handler, 4),
suffixs: make(map[string]http.Handler, 4),
regexps: regexpHostManager,
}
}
func (h *hostManager) updateLen() {
h.total = h.regexps.Len() +
len(h.prefixs) +
len(h.suffixs) +
len(h.exacts)
}
func (h *hostManager) Len() int { return h.total }
func (h *hostManager) Range(f func(host string, handler http.Handler)) {
for host, router := range h.exacts {
f(host, router)
}
for host, router := range h.prefixs {
f(host, router)
}
for host, router := range h.suffixs {
f(host, router)
}
h.regexps.Range(f)
}
func (h *hostManager) AddHost(host string, handler http.Handler) (http.Handler, error) {
var err error
if host == "" {
return nil, errors.New("host must not be empty")
} else if strings.HasPrefix(host, "*.") { // Suffix Matching
if !IsDomainName(host[2:]) {
return nil, fmt.Errorf("invalid domain '%s'", host)
} else if addedHandler, ok := h.suffixs[host]; ok {
handler = addedHandler
} else {
h.suffixs[host] = handler
}
} else if strings.HasSuffix(host, ".*") { // Prefix Matching
if !IsDomainName(host[:len(host)-2]) {
return nil, fmt.Errorf("invalid domain '%s'", host)
} else if addedHandler, ok := h.prefixs[host]; ok {
handler = addedHandler
} else {
h.prefixs[host] = handler
}
} else if IsDomainName(host) { // Exact Matching
if addedHandler, ok := h.exacts[host]; ok {
handler = addedHandler
} else {
h.exacts[host] = handler
}
} else if handler, err = h.regexps.AddHost(host, handler); err != nil {
return nil, err
}
h.updateLen()
return handler, nil
}
func (h *hostManager) DelHost(host string) (handler http.Handler) {
var ok bool
if host == "" {
return nil
} else if strings.HasPrefix(host, "*.") {
if handler, ok = h.suffixs[host]; ok {
delete(h.suffixs, host)
}
} else if strings.HasSuffix(host, ".*") {
if handler, ok = h.prefixs[host]; ok {
delete(h.prefixs, host)
}
} else if IsDomainName(host) {
if handler, ok = h.exacts[host]; ok {
delete(h.exacts, host)
}
} else {
handler = h.regexps.DelHost(host)
ok = handler != nil
}
if ok {
h.updateLen()
}
return
}
func (h *hostManager) GetHost(host string) http.Handler {
if host == "" {
return nil
} else if handler, ok := h.exacts[host]; ok {
return handler
} else if handler, ok := h.suffixs[host]; ok {
return handler
} else if handler, ok := h.prefixs[host]; ok {
return handler
}
return h.regexps.GetHost(host)
}
func (h *hostManager) MatchHost(host string) (
matchedHost string, matchedHandler http.Handler) {
host = splitHost(host)
// Exact Matching
if len(h.exacts) != 0 {
if router, ok := h.exacts[host]; ok {
return host, router
}
}
// Suffix Matching
if len(h.suffixs) != 0 {
for matchedHost, matchedHandler = range h.suffixs {
if strings.HasSuffix(host, matchedHost[1:]) {
return
}
}
}
// Prefix Matching
if len(h.prefixs) != 0 {
for matchedHost, matchedHandler = range h.prefixs {
if strings.HasPrefix(host, matchedHost[:len(matchedHost)-1]) {
return
}
}
}
// Regexp Matching
return h.regexps.MatchHost(host)
}
// isDomainName checks if a string is a presentation-format domain name
// (currently restricted to hostname-compatible "preferred name" LDH labels and
// SRV-like "underscore labels"; see golang.org/issue/12421).
func isDomainName(s string) bool {
// See RFC 1035, RFC 3696.
// Presentation format has dots before every label except the first, and the
// terminal empty label is optional here because we assume fully-qualified
// (absolute) input. We must therefore reserve space for the first and last
// labels' length octets in wire format, where they are necessary and the
// maximum total length is 255.
// So our _effective_ maximum is 253, but 254 is not rejected if the last
// character is a dot.
l := len(s)
if l == 0 || l > 254 || l == 254 && s[l-1] != '.' {
return false
}
last := byte('.')
nonNumeric := false // true once we've seen a letter or hyphen
partlen := 0
for i := 0; i < len(s); i++ {
c := s[i]
switch {
default:
return false
case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_':
nonNumeric = true
partlen++
case '0' <= c && c <= '9':
// fine
partlen++
case c == '-':
// Byte before dash cannot be dot.
if last == '.' {
return false
}
partlen++
nonNumeric = true
case c == '.':
// Byte before dot cannot be dot, dash.
if last == '.' || last == '-' {
return false
}
if partlen > 63 || partlen == 0 {
return false
}
partlen = 0
}
last = c
}
if last == '-' || partlen > 63 {
return false
}
return nonNumeric
}
func splitHost(host string) string {
if host[0] == '[' { // For IPv6
if index := strings.IndexByte(host, ']'); index != -1 {
return host[1:index]
}
return ""
} else if index := strings.IndexByte(host, ':'); index != -1 {
return host[:index]
}
return host
}