package ipblockctrl import ( "encoding/json" "91porn-server/common" "91porn-server/common/constant" "91porn-server/common/constant/redisconst" "91porn-server/common/stderr" "91porn-server/models/l/operatorlgmod" "91porn-server/models/v/ipblockmod" "91porn-server/web/webg" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson/primitive" ) // IPBlockAdd doc // @Summary 添加IP限制 类型 // @Description 添加IP限制 // @Tags WEB-IPBlock IP限制 // @Accept mpfd,json // @Produce json,html // @Param ip formData array true "ip" // @Param type formData string true "限制的类型" // @Param remark formData string false "备注" // @Success 200 {string} json "{"msg": "操作成功"}" // @Failure 400 {string} json "{"msg": "操作失败"}" // @Router /web/admin/ipblock/add [post] func IPBlockAdd(ctx *gin.Context) { manager, err := common.GetAdminAct(ctx) if err != nil { common.ServeJSON(ctx, stderr.AdminIDErr, err.Error()) return } p := ipblockmod.IPBlock{} if err = ctx.ShouldBind(&p); err != nil { common.ServeJSON(ctx, stderr.ErrParamError, err.Error()) return } if err = ipblockmod.Insert(&p); err != nil { common.ServeJSON(ctx, stderr.ErrDbInsertError, err.Error()) return } common.Go(func() { //加入到redis中 _, _ = webg.Redis.SAdd(redisconst.IPBlockKey(string(p.Type)), p.IP) _, _ = webg.Redis.ExpireKey(redisconst.IPBlockKey(string(p.Type)), redisconst.IPBlockExpire) }) log, _ := json.Marshal(p) _ = operatorlgmod.RecordOperation(manager, constant.IPBlockList, constant.Add, string(log), ctx.Request.URL.RequestURI()) common.ServeJSON(ctx, stderr.Success, nil) } // IPBlockList doc // @Summary ip限制列表 // @Description ip限制列表 // @Tags WEB-IPBlock IP限制 // @Accept mpfd,json // @Produce json,html // @Param type query string false "限制类型" // @Param pageNumber query integer true "当前页" mininum(1) // @Param pageSize query integer true "每页条数" mininum(1) // @Success 200 {string} json "{"msg": "操作成功"}" // @Failure 400 {string} json "{"msg": "操作失败"}" // @Router /web/admin/ipblock/list [get] func IpBlockList(ctx *gin.Context) { param := ipblockmod.ListParam{} if err := ctx.ShouldBind(¶m); err != nil { common.ServeJSON(ctx, stderr.ErrParamError, "web ipblock List arg error "+err.Error()) return } result, err := ipblockmod.List(param) if err != nil { common.ServeJSON(ctx, stderr.ErrDbQueryError, err.Error()) return } common.ServeJSON(ctx, stderr.Success, result) } // IPBlockDel doc // @Summary ip限制删除 // @Description 删除ip限制 // @Tags WEB-IPBlock IP限制 // @Accept mpfd,json // @Produce json,html // @Param id formData string true "id字符串" // @Param ip formData string true "ip地址" // @Param type formData string true "操作类型" // @Success 200 {string} json "{"msg": "操作成功"}" // @Failure 400 {string} json "{"msg": "操作失败"}" // @Router /web/admin/ipblock/del [delete] func IPBlockDel(ctx *gin.Context) { manager, err := common.GetAdminAct(ctx) if err != nil { common.ServeJSON(ctx, stderr.AdminIDErr, err.Error()) return } type Param struct { ID primitive.ObjectID `form:"id" json:"id" binding:"required"` Type string `form:"type" json:"type" binding:"required"` IP string `form:"ip" json:"ip" binding:"required"` } var args struct { Params []Param `form:"params" json:"params" binding:"required"` } if err = ctx.ShouldBind(&args); err != nil { common.ServeJSON(ctx, stderr.ErrParamError, err.Error()) return } objIds := make([]primitive.ObjectID, len(args.Params)) for i, v := range args.Params { objIds[i] = v.ID } if err = ipblockmod.DeleteMany(objIds); err != nil { common.ServeJSON(ctx, stderr.ErrDbDeleteError, err.Error()) return } common.Go(func() { for _, v := range args.Params { _, _ = webg.Redis.SRem(redisconst.IPBlockKey(v.Type), v.IP) } }) log, _ := json.Marshal(args) _ = operatorlgmod.RecordOperation(manager, constant.IPBlockList, constant.Delete, string(log), ctx.Request.URL.RequestURI()) common.ServeJSON(ctx, stderr.Success, nil) } // IPBlockEdit doc // @Summary ip限制修改 // @Description IP限制修改 // @Tags WEB-IPBlock IP限制 // @Accept mpfd,json // @Produce json,html // @Param id formData string true "记录id" // @Param ip formData string false "ip" // @Param type formData string true "限制的类型" // @Param remark formData string false "备注" // @Success 200 {string} json "{"msg": "操作成功"}" // @Failure 400 {string} json "{"msg": "操作失败"}" // @Router /web/admin/ipblock/edit [post] func IPBlockEdit(ctx *gin.Context) { manager, err := common.GetAdminAct(ctx) if err != nil { common.ServeJSON(ctx, stderr.AdminIDErr, err.Error()) return } var args struct { ID primitive.ObjectID `form:"id" json:"id" binding:"required"` ipblockmod.IPBlockEdit } if err = ctx.ShouldBind(&args); err != nil { common.ServeJSON(ctx, stderr.ErrParamError, err.Error()) return } if err = ipblockmod.Update(args.ID, args.IPBlockEdit); err != nil { common.ServeJSON(ctx, stderr.ErrDbUpdateError, "") return } if args.IP != nil { common.Go(func() { _, _ = webg.Redis.SAdd(redisconst.IPBlockKey(string(*args.Type)), *args.IP) _, _ = webg.Redis.ExpireKey(redisconst.IPBlockKey(string(*args.Type)), redisconst.IPBlockExpire) }) } log, _ := json.Marshal(args) _ = operatorlgmod.RecordOperation(manager, constant.IPBlockList, constant.Modify, string(log), ctx.Request.URL.RequestURI()) common.ServeJSON(ctx, stderr.Success, nil) }