-
Notifications
You must be signed in to change notification settings - Fork 56
/
restrictAccess.go
75 lines (62 loc) · 1.71 KB
/
restrictAccess.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
package serverHandler
import (
"mjpclab.dev/ghfs/src/util"
"net/http"
"os"
"strings"
)
func newRestrictAccesses(pathHostsList [][]string) pathStringsList {
restricts := make(pathStringsList, 0, len(pathHostsList))
for _, pathHosts := range pathHostsList {
if len(pathHosts) == 0 {
continue
}
restricts = append(restricts, pathStrings{pathHosts[0], pathHosts[1:]})
}
return restricts
}
func (h *aliasHandler) isAllowAccess(r *http.Request, reqUrlPath, reqFsPath string, file *os.File, item os.FileInfo) (restrict, allow bool) {
if h.globalRestrictAccess == nil && len(h.restrictAccessUrls) == 0 && len(h.restrictAccessDirs) == 0 {
return false, true
}
reqHeader := r.Header
sourceHost := reqHeader.Get("Referer")
if len(sourceHost) == 0 {
sourceHost = reqHeader.Get("Origin")
}
if len(sourceHost) == 0 && !shouldServeAsContent(file, item) {
return true, true
}
sourceHost = util.ExtractHostFromUrl(sourceHost)
selfHost := strings.ToLower(r.Host)
if sourceHost == selfHost {
return true, true
}
if util.Contains(h.globalRestrictAccess, sourceHost) {
return true, true
}
urlMatched := false
for i := range h.restrictAccessUrls {
if !util.HasUrlPrefixDir(reqUrlPath, h.restrictAccessUrls[i].path) {
continue
}
urlMatched = true
if util.Contains(h.restrictAccessUrls[i].values, sourceHost) {
return true, true
}
}
dirMatched := false
for i := range h.restrictAccessDirs {
if !util.HasFsPrefixDir(reqFsPath, h.restrictAccessDirs[i].path) {
continue
}
dirMatched = true
if util.Contains(h.restrictAccessDirs[i].values, sourceHost) {
return true, true
}
}
if h.globalRestrictAccess == nil && !urlMatched && !dirMatched {
return true, true
}
return true, false
}