-
Notifications
You must be signed in to change notification settings - Fork 0
/
goaway.go
56 lines (46 loc) · 967 Bytes
/
goaway.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
package middlewares
import (
"net/http"
"regexp"
)
var goAwayPaths = []string{
"/wp-login.php",
"/.git/config",
"/.env",
}
var goAwayPatterns = []string{
`.php$`,
`\/wp-includes\/`,
`\/wp-admin\/`,
`\/wp-content\/`,
`wlwmanifest.xml$`,
`^\/laravel\/`,
`^\/.vscode\/`,
`^\/.remote\/`,
`^\/uploads\/`,
}
func BuildGoAwayMiddleware() func(http.Handler) http.Handler {
pathMap := make(map[string]bool)
for _, path := range goAwayPaths {
pathMap[path] = true
}
var patterns []regexp.Regexp
for _, pattern := range goAwayPatterns {
patterns = append(patterns, *regexp.MustCompile(pattern))
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if pathMap[r.URL.Path] {
w.WriteHeader(404)
return
}
for _, pattern := range patterns {
if pattern.MatchString(r.URL.Path) {
w.WriteHeader(404)
return
}
}
next.ServeHTTP(w, r)
})
}
}