Initial commit
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package srv_ipblack
|
||||
|
||||
import (
|
||||
"videoweb/pub/models/ipblackmod"
|
||||
"videoweb/pub/pubdef"
|
||||
)
|
||||
|
||||
type GetIPBlackListReq struct {
|
||||
pubdef.Page
|
||||
IP *string `json:"ip"`
|
||||
}
|
||||
|
||||
type GetIPBlackListResp struct {
|
||||
Total int64 `json:"total"`
|
||||
List []*ipblackmod.IPBlackModel `json:"list"`
|
||||
}
|
||||
|
||||
type AddIPBlackListReq struct {
|
||||
Ip string `json:"ip"`
|
||||
Desc string `json:"desc" bson:"desc"` // 备注
|
||||
}
|
||||
|
||||
type DelIpBlackListReq struct {
|
||||
Ip string `json:"ip"`
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package srv_ipblack
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"sync"
|
||||
"time"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/models/ipblackmod"
|
||||
)
|
||||
|
||||
var (
|
||||
ipBlackMap map[string]struct{}
|
||||
lock sync.RWMutex
|
||||
)
|
||||
|
||||
func Init() {
|
||||
ctx := context.Background()
|
||||
list, err := ipblackmod.FindManyIPBlackModel(ctx, bson.M{})
|
||||
if err != nil {
|
||||
log.Error("ipBlackInit FindManyIPBlackModel error", log.E(err))
|
||||
return
|
||||
}
|
||||
tempMaps := make(map[string]struct{})
|
||||
for _, v := range list {
|
||||
tempMaps[v.Ip] = struct{}{}
|
||||
}
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
ipBlackMap = tempMaps
|
||||
}
|
||||
|
||||
func CheckIp(ip string) bool {
|
||||
lock.RLock()
|
||||
defer lock.RUnlock()
|
||||
if _, ok := ipBlackMap[ip]; ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func GetIpBlackList(ctx context.Context, req *GetIPBlackListReq) (*GetIPBlackListResp, *srvdef.SrvErr) {
|
||||
filter := bson.M{}
|
||||
if req.IP != nil && len(*req.IP) > 0 {
|
||||
filter[ipblackmod.FN.IPBlackModel.Ip] = req.IP
|
||||
}
|
||||
count, err := ipblackmod.CountIPBlackModel(ctx, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if count == 0 {
|
||||
return &GetIPBlackListResp{
|
||||
Total: 0,
|
||||
List: make([]*ipblackmod.IPBlackModel, 0),
|
||||
}, nil
|
||||
}
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(bson.M{ipblackmod.FN.IPBlackModel.CreatedAt: -1})
|
||||
opts.SetLimit(req.PageSize)
|
||||
opts.SetSkip((req.PageNum - 1) * req.PageSize)
|
||||
list, err := ipblackmod.FindManyIPBlackModel(ctx, filter, &opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp := &GetIPBlackListResp{
|
||||
Total: count,
|
||||
List: list,
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func AddIpBlack(ctx context.Context, req *AddIPBlackListReq) *srvdef.SrvErr {
|
||||
err := ipblackmod.InsertOneIPBlackModel(ctx, &ipblackmod.IPBlackModel{
|
||||
ID: primitive.NewObjectID(),
|
||||
Ip: req.Ip,
|
||||
Desc: req.Desc,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
if err == nil {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
ipBlackMap[req.Ip] = struct{}{}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func DelIpBlack(ctx context.Context, req *DelIpBlackListReq) *srvdef.SrvErr {
|
||||
_, err := ipblackmod.DelOneIPBlackModel(ctx, bson.M{
|
||||
ipblackmod.FN.IPBlackModel.Ip: req.Ip,
|
||||
})
|
||||
if err != nil {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
delete(ipBlackMap, req.Ip)
|
||||
ipBlackMap[req.Ip] = struct{}{}
|
||||
}
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user