-
Notifications
You must be signed in to change notification settings - Fork 2
/
http_auth_method_base.go
73 lines (61 loc) · 1.55 KB
/
http_auth_method_base.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
// Copyright 2022 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package serverconfigs
import (
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/iwind/TeaGo/lists"
"net/http"
"path/filepath"
"regexp"
"strings"
)
var httpAuthTimestampRegexp = regexp.MustCompile(`^\d{10}$`)
type HTTPAuthBaseMethod struct {
Exts []string `json:"exts"`
Domains []string `json:"domains"`
}
func (this *HTTPAuthBaseMethod) SetExts(exts []string) {
this.Exts = exts
}
func (this *HTTPAuthBaseMethod) SetDomains(domains []string) {
this.Domains = domains
}
func (this *HTTPAuthBaseMethod) removeQueryArgs(query string, args []string) string {
var pieces = strings.Split(query, "&")
var result = []string{}
Loop:
for _, piece := range pieces {
for _, arg := range args {
if strings.HasPrefix(piece, arg+"=") {
continue Loop
}
}
result = append(result, piece)
}
return strings.Join(result, "&")
}
func (this *HTTPAuthBaseMethod) matchTimestamp(timestamp string) bool {
return httpAuthTimestampRegexp.MatchString(timestamp)
}
func (this *HTTPAuthBaseMethod) MatchRequest(req *http.Request) bool {
if len(this.Exts) > 0 {
var ext = filepath.Ext(req.URL.Path)
if len(ext) == 0 {
return false
}
// ext中包含点符号
ext = strings.ToLower(ext)
if !lists.ContainsString(this.Exts, ext) {
return false
}
}
if len(this.Domains) > 0 {
var domain = req.Host
if len(domain) == 0 {
return false
}
if !configutils.MatchDomains(this.Domains, domain) {
return false
}
}
return true
}