-
Notifications
You must be signed in to change notification settings - Fork 5
/
WebGuard.go
65 lines (61 loc) · 1.81 KB
/
WebGuard.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
package WebGuard
import (
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"net/http"
)
// WebGuard web卫兵配置
type WebGuard struct {
config string // 配置文件路径
handler func(w http.ResponseWriter, r *http.Request) // http处理函数
hotLoading bool // 热加载
logger func(in ...interface{})
}
func New(cfg string, hotLoading bool, logger func(in ...interface{}), handler func(w http.ResponseWriter, r *http.Request)) *WebGuard {
return &WebGuard{
config: cfg,
handler: handler,
hotLoading: hotLoading,
logger: logger,
}
}
func (g *WebGuard) RunGuard() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
cfg := viper.New()
cfg.SetConfigType("yaml")
cfg.SetConfigFile(g.config)
if err := cfg.ReadInConfig(); err != nil {
// 配置文件没有找到,不影响规则,我们直接走规则控制函数
g.handler(w, r)
return
}
if g.hotLoading == true {
// 配置热加载
go func() {
cfg.WatchConfig()
cfg.OnConfigChange(func(in fsnotify.Event) {})
}()
}
filter, err := WebGuardFilter(cfg, r)
if err != nil {
if g.logger != nil {
g.logger(err) // 调用debug的logger函数,这个函数可以自定义,但是希望采用func(in interface{})格式输出
}
}
// 规则满足
if filter == true && err == nil {
g.handler(w, r)
} else {
// 规则不满足,我们看看是丢包还是进行重定向
var (
requestDrop = cfg.GetBool("proxy-handler.request-drop")
redirect = cfg.GetString("proxy-handler.redirect")
)
// 判断重定向还是直接丢包
proxy, _ := NewProxy(redirect, requestDrop)
r.URL.Path = "/" // 重定向到根
proxy.ServeHTTP(w, r)
return
}
}
}