package ipwhitectrl import ( "encoding/json" "91porn-server/common" "91porn-server/common/constant" "91porn-server/common/stderr" "91porn-server/models/l/operatorlgmod" "91porn-server/models/v/ipwhitemod" "91porn-server/web/webg" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" ) // IPWhiteAdd doc // @Summary 添加IP白名单 // @Description 添加IP白名单 // @Tags WEB-IPBlock IP白名单 // @Accept mpfd,json // @Produce json,html // @Param ip formData array true "ip" // @Param remark formData string false "备注" // @Success 200 {string} json "{"msg": "操作成功"}" // @Failure 400 {string} json "{"msg": "操作失败"}" // @Router /web/admin/ipwhite/add [post] func IPWhiteAdd(ctx *gin.Context) { manager, err := common.GetAdminAct(ctx) if err != nil { common.ServeJSON(ctx, stderr.AdminIDErr, err.Error()) return } p := ipwhitemod.IPWhite{} if err = ctx.ShouldBind(&p); err != nil { common.ServeJSON(ctx, stderr.ErrParamError, err.Error()) return } p.CreateUser = manager if err = ipwhitemod.Insert(&p); err != nil { common.ServeJSON(ctx, stderr.ErrDbInsertError, err.Error()) return } common.Go(func() { //加入到redis中 _, _ = webg.Redis.SAdd(constant.IPWhiteRedisKey, p.IP) }) log, _ := json.Marshal(p) _ = operatorlgmod.RecordOperation(manager, constant.IPWhiteList, constant.Add, string(log), ctx.Request.URL.RequestURI()) common.ServeJSON(ctx, stderr.Success, "") } // IPWhiteList 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/ipwhite/list [get] func IpWhiteList(ctx *gin.Context) { param := ipwhitemod.ListParam{} if err := ctx.ShouldBind(¶m); err != nil { common.ServeJSON(ctx, stderr.ErrParamError, "web ipwhite List arg error "+err.Error()) return } result, err := ipwhitemod.List(param) if err != nil && err != mongo.ErrNoDocuments { common.ServeJSON(ctx, stderr.ErrDbQueryError, err.Error()) return } common.ServeJSON(ctx, stderr.Success, result) } // IPWhiteDel doc // @Summary ip白名单删除 // @Description 删除ip白名单 // @Tags WEB-IPWhite 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/ipwhite/del [delete] func IPWhiteDel(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"` IP string `form:"ip" json:"ip"` } 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 } filter := bson.M{"_id": bson.M{"$in": objIds}} ipWhiteList, _ := ipwhitemod.FindMany(filter) if err = ipwhitemod.DeleteMany(objIds); err != nil { common.ServeJSON(ctx, stderr.ErrDbDeleteError, err.Error()) return } common.Go(func() { for _, v := range ipWhiteList { _, _ = webg.Redis.SRem(constant.IPWhiteRedisKey, v.IP) } }) log, _ := json.Marshal(args) _ = operatorlgmod.RecordOperation(manager, constant.IPWhiteList, constant.Delete, string(log), ctx.Request.URL.RequestURI()) common.ServeJSON(ctx, stderr.Success, "") } // IPWhiteEdit doc // @Summary ip白名单修改 // @Description IP白名单修改 // @Tags WEB-IPWhite 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/ipwhite/edit [post] func IPWhiteEdit(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"` ipwhitemod.IPWhiteEdit } if err = ctx.ShouldBind(&args); err != nil { common.ServeJSON(ctx, stderr.ErrParamError, err.Error()) return } if err = ipwhitemod.Update(args.ID, args.IPWhiteEdit); err != nil { common.ServeJSON(ctx, stderr.ErrDbUpdateError, "") return } if args.IP != nil { common.Go(func() { _, _ = webg.Redis.SAdd(constant.IPWhiteRedisKey, *args.IP) }) } log, _ := json.Marshal(args) _ = operatorlgmod.RecordOperation(manager, constant.IPWhiteList, constant.Modify, string(log), ctx.Request.URL.RequestURI()) common.ServeJSON(ctx, stderr.Success, nil) }