forked from xiachufang/krakend-ipfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_factory.go
43 lines (33 loc) · 1.07 KB
/
handler_factory.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
package ipfilter
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
"github.com/luraproject/lura/v2/proxy"
krakendgin "github.com/luraproject/lura/v2/router/gin"
)
func IpFilterFactory(ipFilter IPFilter, handlerFunc gin.HandlerFunc, logger logging.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
ip := c.ClientIP()
if ipFilter.Deny(ip) {
logger.Error(fmt.Sprintf("krakend-ipfilter deny request from: %s", ip))
c.AbortWithStatus(http.StatusForbidden)
return
}
handlerFunc(c)
}
}
func HandlerFactory(next krakendgin.HandlerFactory, logger logging.Logger) krakendgin.HandlerFactory {
return func(remote *config.EndpointConfig, p proxy.Proxy) gin.HandlerFunc {
handlerFunc := next(remote, p)
cfg := ConfigGetter(remote.ExtraConfig)
if cfg == nil {
return handlerFunc
}
ipFilter := NewIPFilter(cfg)
logger.Info(fmt.Sprintf("ip-filter krakend-ipfilter: allow %v deny %v", cfg.Allow, cfg.Deny))
return IpFilterFactory(ipFilter, handlerFunc, logger)
}
}