2022-10-13 03:09:52 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IpLimitMap struct {
|
2022-10-14 09:15:43 +00:00
|
|
|
mux *sync.RWMutex
|
2022-10-13 03:09:52 +00:00
|
|
|
m map[string]*int64
|
2022-11-16 03:09:25 +00:00
|
|
|
limitNum *int64
|
2022-10-13 03:09:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 03:09:25 +00:00
|
|
|
func IpLimit(num int64) (func(ctx *gin.Context), func(int64)) {
|
2022-10-13 03:09:52 +00:00
|
|
|
m := IpLimitMap{
|
2022-10-14 09:15:43 +00:00
|
|
|
mux: &sync.RWMutex{},
|
2022-10-13 03:09:52 +00:00
|
|
|
m: make(map[string]*int64),
|
2022-11-16 03:09:25 +00:00
|
|
|
limitNum: new(int64),
|
2022-10-13 03:09:52 +00:00
|
|
|
}
|
2022-11-16 03:09:25 +00:00
|
|
|
fn := func(num int64) {
|
|
|
|
atomic.StoreInt64(m.limitNum, num)
|
|
|
|
}
|
|
|
|
fn(num)
|
2022-10-13 03:09:52 +00:00
|
|
|
|
|
|
|
return func(c *gin.Context) {
|
2024-01-18 15:29:50 +00:00
|
|
|
if atomic.LoadInt64(m.limitNum) <= 0 {
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
2022-10-13 03:09:52 +00:00
|
|
|
ip := c.ClientIP()
|
2022-10-14 09:15:43 +00:00
|
|
|
m.mux.RLock()
|
|
|
|
i, ok := m.m[ip]
|
|
|
|
m.mux.RUnlock()
|
2024-01-18 15:29:50 +00:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
m.mux.Lock()
|
|
|
|
i = new(int64)
|
|
|
|
m.m[ip] = i
|
|
|
|
m.mux.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-10-13 03:09:52 +00:00
|
|
|
defer func() {
|
2022-10-14 09:15:43 +00:00
|
|
|
ii := atomic.LoadInt64(i)
|
2024-01-18 15:29:50 +00:00
|
|
|
if ii > 0 {
|
2022-10-13 03:09:52 +00:00
|
|
|
atomic.AddInt64(i, -1)
|
2022-10-14 09:15:43 +00:00
|
|
|
if atomic.LoadInt64(i) == 0 {
|
2022-10-13 03:09:52 +00:00
|
|
|
m.mux.Lock()
|
|
|
|
delete(m.m, ip)
|
|
|
|
m.mux.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2022-10-14 09:15:43 +00:00
|
|
|
|
2024-01-18 15:29:50 +00:00
|
|
|
if atomic.LoadInt64(i) >= atomic.LoadInt64(m.limitNum) {
|
2022-10-13 03:09:52 +00:00
|
|
|
c.Status(http.StatusForbidden)
|
|
|
|
c.Abort()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
atomic.AddInt64(i, 1)
|
2022-10-14 09:15:43 +00:00
|
|
|
c.Next()
|
2022-11-16 03:09:25 +00:00
|
|
|
}, fn
|
2022-10-13 03:09:52 +00:00
|
|
|
}
|