2022-09-29 09:23:02 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github/fthvgb1/wp-go/vars"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IpLimitMap struct {
|
|
|
|
mux *sync.Mutex
|
2022-10-02 02:58:48 +00:00
|
|
|
m map[string]*int64
|
2022-09-29 09:23:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FlowLimit() func(ctx *gin.Context) {
|
|
|
|
var flow int64
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
randFn := func(start, end time.Duration) time.Duration {
|
|
|
|
end++
|
|
|
|
return time.Duration(rand.Intn(int(end-start)) + int(start))
|
|
|
|
}
|
|
|
|
m := IpLimitMap{
|
|
|
|
mux: &sync.Mutex{},
|
2022-10-02 02:58:48 +00:00
|
|
|
m: make(map[string]*int64),
|
2022-09-29 09:23:02 +00:00
|
|
|
}
|
|
|
|
statPath := map[string]struct{}{
|
|
|
|
"wp-includes": {},
|
|
|
|
"wp-content": {},
|
|
|
|
"favicon.ico": {},
|
|
|
|
}
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
f := strings.Split(strings.TrimLeft(c.FullPath(), "/"), "/")
|
|
|
|
_, ok := statPath[f[0]]
|
|
|
|
if len(f) > 0 && ok {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
2022-10-02 02:58:48 +00:00
|
|
|
s := false
|
2022-09-29 09:25:15 +00:00
|
|
|
ip := c.ClientIP()
|
2022-10-02 02:58:48 +00:00
|
|
|
defer m.searchLimit(false, c, ip, f, &s)
|
|
|
|
if m.searchLimit(true, c, ip, f, &s) {
|
2022-09-29 09:23:02 +00:00
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
atomic.AddInt64(&flow, 1)
|
2022-10-02 02:58:48 +00:00
|
|
|
defer func() {
|
|
|
|
atomic.AddInt64(&flow, -1)
|
|
|
|
}()
|
2022-09-29 09:23:02 +00:00
|
|
|
if flow >= vars.Conf.MaxRequestSleepNum && flow <= vars.Conf.MaxRequestNum {
|
|
|
|
t := randFn(vars.Conf.SleepTime[0], vars.Conf.SleepTime[1])
|
|
|
|
time.Sleep(t)
|
|
|
|
} else if flow > vars.Conf.MaxRequestNum {
|
|
|
|
c.String(http.StatusForbidden, "请求太多了,服务器君压力山大中==!, 请稍后访问")
|
|
|
|
c.Abort()
|
2022-10-02 02:58:48 +00:00
|
|
|
|
2022-09-29 09:23:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Next()
|
|
|
|
|
2022-10-02 02:58:48 +00:00
|
|
|
}
|
2022-09-29 09:23:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-02 02:58:48 +00:00
|
|
|
func (m *IpLimitMap) searchLimit(start bool, c *gin.Context, ip string, f []string, s *bool) (isForbid bool) {
|
2022-09-29 09:23:02 +00:00
|
|
|
if f[0] == "" && c.Query("s") != "" {
|
2022-10-02 02:58:48 +00:00
|
|
|
if start {
|
2022-09-29 09:23:02 +00:00
|
|
|
i, ok := m.m[ip]
|
2022-10-02 02:58:48 +00:00
|
|
|
num := vars.Conf.SingleIpSearchNum
|
|
|
|
if !ok {
|
|
|
|
m.mux.Lock()
|
|
|
|
i = new(int64)
|
|
|
|
m.m[ip] = i
|
|
|
|
m.mux.Unlock()
|
2022-09-29 09:23:02 +00:00
|
|
|
}
|
2022-10-02 02:58:48 +00:00
|
|
|
if num > 0 && *i >= num {
|
|
|
|
isForbid = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*s = true
|
|
|
|
atomic.AddInt64(i, 1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i, ok := m.m[ip]
|
|
|
|
if ok && *s && *i > 0 {
|
|
|
|
atomic.AddInt64(i, -1)
|
|
|
|
if *i == 0 {
|
2022-09-29 09:23:02 +00:00
|
|
|
m.mux.Lock()
|
|
|
|
delete(m.m, ip)
|
|
|
|
m.mux.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|