-
Notifications
You must be signed in to change notification settings - Fork 56
/
restrictAccess.go
83 lines (68 loc) · 1.86 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
76
77
78
79
80
81
82
83
package serverHandler
import (
"mjpclab.dev/ghfs/src/util"
"net/http"
"os"
"strings"
)
func newRestrictAccesses(pathHostsList [][]string) []pathStrings {
restricts := make([]pathStrings, 0, len(pathHostsList))
for _, pathHosts := range pathHostsList {
if len(pathHosts) == 0 {
continue
}
restricts = append(restricts, pathStrings{pathHosts[0], pathHosts[1:]})
}
return restricts
}
func hasRestrictAccess(globalRestrictAccesses []string, restrictAccessUrls, restrictAccessDirs []pathStrings) bool {
return globalRestrictAccesses != nil || len(restrictAccessUrls) > 0 || len(restrictAccessDirs) > 0
}
func (h *aliasHandler) isAllowAccess(r *http.Request, reqUrlPath, reqFsPath string, file *os.File, item os.FileInfo) bool {
if !h.restrictAccess {
return 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
}
sourceHost = util.ExtractHostFromUrl(sourceHost)
selfHost := strings.ToLower(r.Host)
if sourceHost == selfHost {
return true
}
if util.Contains(h.globalRestrictAccess, sourceHost) {
return 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].strings, sourceHost) {
return 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].strings, sourceHost) {
return true
}
}
if h.globalRestrictAccess == nil && !urlMatched && !dirMatched {
return true
}
return false
}
func restrictAccess(w http.ResponseWriter, status int) {
w.WriteHeader(status)
}