diff --git a/application.go b/application.go index e1f4cdc..1b6bfe8 100644 --- a/application.go +++ b/application.go @@ -744,6 +744,9 @@ func setMakeCodeListener(name string, appIns *Application) { } } + if context.RouterString[0] != name { + return isFinished + } if len(context.RouterString) < 2 || len(context.RouterString) > 3 || !(context.Router[context.RouterString[0]] != nil && context.Router[context.RouterString[0]][context.RouterString[1]] != nil) { diff --git a/code.go b/code.go index 47166dd..a3124ad 100644 --- a/code.go +++ b/code.go @@ -10,9 +10,12 @@ import ( "os" "sort" "strings" + "sync" "time" ) +var adminLoginRateLimiter sync.Map // key: ip,value: *[]int64(1分钟内登录时间戳列表) + // Project 管理端项目 var TptProject = Proj{ //"user": UserCtr, @@ -964,6 +967,26 @@ var TptProject = Proj{ that.Display(0, re) }, "login": func(that *Context) { + // 限速:同一 IP 1分钟内最多尝试10次 + ip := that.Req.Header.Get("X-Forwarded-For") + if ip == "" { + ip = that.Req.RemoteAddr + } + now := time.Now().Unix() + val, _ := adminLoginRateLimiter.LoadOrStore(ip, &[]int64{}) + times := val.(*[]int64) + filtered := (*times)[:0] + for _, t := range *times { + if now-t < 60 { + filtered = append(filtered, t) + } + } + *times = append(filtered, now) + if len(*times) > 10 { + that.Display(5, "登录尝试过于频繁,请稍后再试") + return + } + hotimeName := that.RouterString[0] fileConfig := that.MakeCodeRouter[hotimeName].FileConfig @@ -1017,15 +1040,15 @@ var TptProject = Proj{ that.Display(4, "找不到配置文件") return } - if that.Session(fileConfig.GetString("table")+"_id").Data == nil { - - conf := ObjToMap(string(btes)) - delete(conf, "menus") - delete(conf, "tables") - //没有登录只需要返回这些信息 - that.Display(0, conf) - return - } + if that.Session(fileConfig.GetString("table")+"_id").Data == nil { + conf := ObjToMap(string(btes)) + //未登录只返回前端登录页所需的最少字段,避免泄露表结构等敏感信息 + that.Display(0, Map{ + "label": conf["label"], + "name": conf["name"], + }) + return + } //可读写配置 conf := ObjToMap(string(btes)) menus := conf.GetSlice("menus")