From 443b98ce567ef9d02fcbebb5b8f88d54f978c604 Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Wed, 15 Nov 2023 09:36:23 +0800 Subject: [PATCH 1/5] fix-4893 --- modules/context/repo.go | 26 ++++++++++++++++++++++++++ modules/setting/ratelimit.go | 23 +++++++++++++++++++++++ modules/setting/setting.go | 1 + options/locale/locale_en-US.ini | 2 ++ options/locale/locale_zh-CN.ini | 2 ++ routers/routes/routes.go | 12 ++++++------ templates/status/429.tmpl | 12 ++++++++++++ 7 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 modules/setting/ratelimit.go create mode 100644 templates/status/429.tmpl diff --git a/modules/context/repo.go b/modules/context/repo.go index 963ff849dc..00c25ff019 100755 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -8,6 +8,7 @@ package context import ( "fmt" "io/ioutil" + "net/http" "net/url" "path" "strings" @@ -22,6 +23,7 @@ import ( "gitea.com/macaron/macaron" "github.com/editorconfig/editorconfig-core-go/v2" "github.com/unknwon/com" + "golang.org/x/time/rate" ) // PullRequest contains informations to make a pull request @@ -371,6 +373,30 @@ func RepoIDAssignment() macaron.Handler { } } +func LowLimiter() macaron.Handler { + return limiterHandler(setting.RateLimitConfig.LowRate, setting.RateLimitConfig.LowBurst) +} + +func limiterHandler(rateLimit int, burst int) macaron.Handler { + var limiter *rate.Limiter + if setting.RateLimitConfig.Enabled { + limiter = rate.NewLimiter(rate.Limit(rateLimit), burst) + } + return func(ctx *Context) { + if !ctx.IsSigned { + if setting.RateLimitConfig.Enabled && limiter != nil && !limiter.Allow() { + ctx.HTML(http.StatusTooManyRequests, "status/429") + return + } + } + ctx.Next() + } +} + +func HighLimiter() macaron.Handler { + return limiterHandler(setting.RateLimitConfig.HighRate, setting.RateLimitConfig.HighBurst) +} + // RepoAssignment returns a macaron to handle repository assignment func RepoAssignment() macaron.Handler { return func(ctx *Context) { diff --git a/modules/setting/ratelimit.go b/modules/setting/ratelimit.go new file mode 100644 index 0000000000..1d31f6a433 --- /dev/null +++ b/modules/setting/ratelimit.go @@ -0,0 +1,23 @@ +package setting + +var RateLimitConfig *RateLimit + +type RateLimit struct { + Enabled bool + LowRate int + LowBurst int + HighRate int + HighBurst int +} + +func initRateLimitConfig() { + sec := Cfg.Section("rate_limit") + RateLimitConfig = &RateLimit{ + Enabled: sec.Key("Enabled").MustBool(true), + LowRate: sec.Key("LowRate").MustInt(10), + LowBurst: sec.Key("LowBurst").MustInt(20), + HighRate: sec.Key("HighRate").MustInt(20), + HighBurst: sec.Key("HighBurst").MustInt(30), + } + +} diff --git a/modules/setting/setting.go b/modules/setting/setting.go index b5bc7a1e7e..dc3e773c8d 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -2122,4 +2122,5 @@ func NewServices() { newTaskService() NewQueueService() newPhoneService() + initRateLimitConfig() } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index e3784634ac..187540938b 100755 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -99,8 +99,10 @@ loading = Loading… error404_index = Request forbidden by administrative rules error500_index = Internal Server Error +error429_index = Request forbidden by administrative rules! error404 = The page you are trying to reach either does not exist or you are not authorized to view it. error500= Sorry, the site has encountered some problems, we are trying to fix the page, please try again later. +error429 = Too many requests, please try again later. [error] occurred = An error has occurred report_message = An error has occurred diff --git a/options/locale/locale_zh-CN.ini b/options/locale/locale_zh-CN.ini index acb847ed5b..806cc3595d 100755 --- a/options/locale/locale_zh-CN.ini +++ b/options/locale/locale_zh-CN.ini @@ -99,8 +99,10 @@ loading=正在加载... error404_index = 您的访问受限! error500_index = 抱歉!您指定的网页无法访问。 +error429_index = 您的访问受限! error404=您正尝试访问的页面 不存在您尚未被授权 查看该页面。 error500=抱歉,站点遇到一些问题,我们正尝试修复网页,请您稍后再试。 +error429 = 您访问的接口访问量超过限制,请稍后再试。 [error] occurred=发生错误 diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 3938eb4ba2..dd64099b3d 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -966,7 +966,7 @@ func RegisterRoutes(m *macaron.Macaron) { }, reqSignIn) // ***** Release Attachment Download without Signin - m.Get("/:username/:reponame/releases/download/:vTag/:fileName", ignSignIn, context.RepoAssignment(), repo.MustBeNotEmpty, repo.RedirectDownload) + m.Get("/:username/:reponame/releases/download/:vTag/:fileName", ignSignIn, context.LowLimiter(), context.RepoAssignment(), repo.MustBeNotEmpty, repo.RedirectDownload) m.Group("/:username/:reponame", func() { m.Group("/settings", func() { @@ -1060,7 +1060,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists). Get(repo.SetDiffViewStyle, repo.CompareDiff). Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(auth.CreateIssueForm{}), repo.CompareAndPullRequestPost) - }, context.RepoAssignment(), context.UnitTypes()) + }, context.LowLimiter(), context.RepoAssignment(), context.UnitTypes()) // Grouping for those endpoints that do require authentication m.Group("/:username/:reponame", func() { @@ -1187,7 +1187,7 @@ func RegisterRoutes(m *macaron.Macaron) { } ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount }) - }, ignSignIn, context.RepoAssignment(), context.UnitTypes(), reqRepoReleaseReader) + }, ignSignIn, context.LowLimiter(), context.RepoAssignment(), context.UnitTypes(), reqRepoReleaseReader) m.Group("/:username/:reponame", func() { m.Post("/topics", repo.TopicsPost) @@ -1583,18 +1583,18 @@ func RegisterRoutes(m *macaron.Macaron) { }, context.RepoRef(), reqRepoCodeReader) m.Get("/commit/:sha([a-f0-9]{7,40})\\.:ext(patch|diff)", repo.MustBeNotEmpty, reqRepoCodeReader, repo.RawDiff) - }, ignSignIn, context.RepoAssignment(), context.UnitTypes()) + }, ignSignIn, context.LowLimiter(), context.RepoAssignment(), context.UnitTypes()) m.Group("/:username/:reponame", func() { m.Get("/stars", repo.Stars) m.Get("/watchers", repo.Watchers) m.Get("/search", reqRepoCodeReader, repo.Search) - }, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes()) + }, ignSignIn, context.LowLimiter(), context.RepoAssignment(), context.RepoRef(), context.UnitTypes()) m.Group("/:username", func() { m.Group("/:reponame", func() { m.Get("", repo.SetEditorconfigIfExists, repo.Home) m.Get("\\.git$", repo.SetEditorconfigIfExists, repo.Home) - }, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes()) + }, ignSignIn, context.HighLimiter(), context.RepoAssignment(), context.RepoRef(), context.UnitTypes()) m.Group("/:reponame", func() { m.Group("\\.git/info/lfs", func() { diff --git a/templates/status/429.tmpl b/templates/status/429.tmpl new file mode 100644 index 0000000000..ce2e8d989a --- /dev/null +++ b/templates/status/429.tmpl @@ -0,0 +1,12 @@ +{{template "base/head" .}} +{{if .IsRepo}}
{{template "repo/header" .}}
{{end}} + +
+
+ +

{{.i18n.Tr "error429_index"}}

+

{{.i18n.Tr "error429" | Safe}}

+
+
+ +{{template "base/footer" .}} -- 2.34.1 From 0831cb9a18722e2804e1dbffc673d28781ad46e0 Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Wed, 15 Nov 2023 10:29:57 +0800 Subject: [PATCH 2/5] fix-doc-107 --- routers/repo/view.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index e025c64306..54d747482e 100755 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -971,7 +971,7 @@ func ContributorsAPI(ctx *context.Context) { } else { // new committer info var newContributor = &ContributorInfo{ - user, user.RelAvatarLink(), user.Name, user.Email, c.CommitCnt, + nil, user.RelAvatarLink(), user.Name, user.Email, c.CommitCnt, } count++ contributorInfos = append(contributorInfos, newContributor) @@ -984,7 +984,7 @@ func ContributorsAPI(ctx *context.Context) { existedContributorInfo.CommitCnt += c.CommitCnt } else { var newContributor = &ContributorInfo{ - user, "", "", c.Email, c.CommitCnt, + nil, "", "", c.Email, c.CommitCnt, } count++ contributorInfos = append(contributorInfos, newContributor) -- 2.34.1 From d92f26ac638f3659e0389255dcfed9fed28e5530 Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Thu, 16 Nov 2023 15:53:17 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=99=BD=E5=90=8D?= =?UTF-8?q?=E5=8D=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/context/repo.go | 14 +++++++++++++- modules/setting/ratelimit.go | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/modules/context/repo.go b/modules/context/repo.go index 00c25ff019..c83d715676 100755 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -384,7 +384,19 @@ func limiterHandler(rateLimit int, burst int) macaron.Handler { } return func(ctx *Context) { if !ctx.IsSigned { - if setting.RateLimitConfig.Enabled && limiter != nil && !limiter.Allow() { + isInWhiteList := false + userName := ctx.Params(":username") + repoName := ctx.Params(":reponame") + + if userName != "" && repoName != "" { + for _, v := range setting.RateLimitConfig.WhiteList { + if v == userName+"/"+repoName { + isInWhiteList = true + } + } + } + + if !isInWhiteList && setting.RateLimitConfig.Enabled && limiter != nil && !limiter.Allow() { ctx.HTML(http.StatusTooManyRequests, "status/429") return } diff --git a/modules/setting/ratelimit.go b/modules/setting/ratelimit.go index 1d31f6a433..010e9d0cff 100644 --- a/modules/setting/ratelimit.go +++ b/modules/setting/ratelimit.go @@ -1,5 +1,7 @@ package setting +import "strings" + var RateLimitConfig *RateLimit type RateLimit struct { @@ -8,6 +10,7 @@ type RateLimit struct { LowBurst int HighRate int HighBurst int + WhiteList []string } func initRateLimitConfig() { @@ -18,6 +21,7 @@ func initRateLimitConfig() { LowBurst: sec.Key("LowBurst").MustInt(20), HighRate: sec.Key("HighRate").MustInt(20), HighBurst: sec.Key("HighBurst").MustInt(30), + WhiteList: strings.Split(sec.Key("WhiteList").MustString(""), ","), } } -- 2.34.1 From ef21a4b129afdd68e64aa6d02b37d254e83c9871 Mon Sep 17 00:00:00 2001 From: chenshihai Date: Mon, 20 Nov 2023 09:12:05 +0800 Subject: [PATCH 4/5] update mind page --- web_src/vuepages/pages/model/mind/index.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_src/vuepages/pages/model/mind/index.vue b/web_src/vuepages/pages/model/mind/index.vue index db8c6a6ce6..a2b73d7824 100644 --- a/web_src/vuepages/pages/model/mind/index.vue +++ b/web_src/vuepages/pages/model/mind/index.vue @@ -108,7 +108,7 @@

{{ descr }}

-
成长生命期阶段版本
+
成长生命期阶段版本(当前最新版本——进阶版1.5T Tokens)
您提交的申请体验通过审核后,我们将提供中间 checkpoints 供您研究使用;随着训练的推进,我们将提供对应阶段的 checkpoints 。
-- 2.34.1 From 78989c6e90b6c578e8c04ec7fb83f4aa30151ecf Mon Sep 17 00:00:00 2001 From: chenshihai Date: Thu, 23 Nov 2023 15:54:54 +0800 Subject: [PATCH 5/5] update menu and logo --- custom/public/img/logo-w.svg | 168 +++++++++++++++++++++----- templates/base/head_navbar.tmpl | 4 +- templates/base/head_navbar_fluid.tmpl | 4 +- templates/base/head_navbar_home.tmpl | 4 +- templates/base/head_navbar_pro.tmpl | 4 +- 5 files changed, 144 insertions(+), 40 deletions(-) diff --git a/custom/public/img/logo-w.svg b/custom/public/img/logo-w.svg index 133f63d232..bd6de37c61 100644 --- a/custom/public/img/logo-w.svg +++ b/custom/public/img/logo-w.svg @@ -1,5 +1,5 @@ - + - - - - - - + + + + + + + l3.8,2c0.8,0.4,0.8,1,0.8,1.9v0.1c0.1,1.6,1.8,3.5,4.2,3.5c2.3,0,4.3-1.9,4.4-4.2c0-1.6-0.8-3.1-2.3-3.9c-0.6-0.3-1.7-0.5-2.9-0.4 + c-0.9,0.1-1,0.6-2.8-0.5L18.3,46c-0.8-0.5-1.7-1.3-1.6-2.3V22c0-1.8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0.6,1.1,1.5,1.1,2.5l-0.5,6.8c0,1.9,0.3,2.8,1.8,3.5l11.8,6.3c1,0.6,1.6,1.7,1.6,2.8v3.1 + c0,3.1,4.3,5.7,8.7,3.3l16.4-9.8c1.9-1,3.2-3,3.4-5.2V20.7C65,18.5,63.8,16.5,61.9,15.4z M24.2,50.7c1.1,0,2,0.9,2,2s-0.9,2-2,2 + s-2-0.9-2-2C22.2,51.7,23.1,50.7,24.2,50.7z M48.7,21.5c0-1.1,0.9-2,2-2s2,0.9,2,2s-0.9,2-2,2S48.7,22.6,48.7,21.5z M30.6,23.8 + c-1.1,0-2-0.9-2-2s0.9-2,2-2s2,0.9,2,2S31.8,23.8,30.6,23.8z M22.2,26.4c0-1.1,0.9-2,2-2s2,0.9,2,2s-0.9,2-2,2S22.2,27.5,22.2,26.4z + M40,17.9c-1.1,0-2-0.9-2-2s0.9-2,2-2s2,0.9,2,2S41.1,17.9,40,17.9z"/> + + + + + + + diff --git a/templates/base/head_navbar.tmpl b/templates/base/head_navbar.tmpl index 8ade9f4f91..5a83cdec2d 100755 --- a/templates/base/head_navbar.tmpl +++ b/templates/base/head_navbar.tmpl @@ -61,7 +61,7 @@ {{end}} {{.i18n.Tr "custom.head.course"}} {{.i18n.Tr "custom.head.openi.repo"}} - 科技2030项目 +
@@ -110,7 +110,7 @@ {{end}} {{.i18n.Tr "custom.head.course"}} {{.i18n.Tr "custom.head.openi.repo"}} - 科技2030项目 +
diff --git a/templates/base/head_navbar_fluid.tmpl b/templates/base/head_navbar_fluid.tmpl index ca7bcbc787..fc905b9d6e 100644 --- a/templates/base/head_navbar_fluid.tmpl +++ b/templates/base/head_navbar_fluid.tmpl @@ -58,7 +58,7 @@ {{end}} {{.i18n.Tr "custom.head.course"}} {{.i18n.Tr "custom.head.openi.repo"}} - 科技2030项目 + {{.i18n.Tr "custom.head.forum"}} @@ -104,7 +104,7 @@ {{end}} {{.i18n.Tr "custom.head.course"}} {{.i18n.Tr "custom.head.openi.repo"}} - 科技2030项目 + diff --git a/templates/base/head_navbar_home.tmpl b/templates/base/head_navbar_home.tmpl index 130d0bba16..06fd088806 100644 --- a/templates/base/head_navbar_home.tmpl +++ b/templates/base/head_navbar_home.tmpl @@ -50,7 +50,7 @@ {{end}} {{.i18n.Tr "custom.head.course"}} {{.i18n.Tr "custom.head.openi.repo"}} - 科技2030项目 + @@ -98,7 +98,7 @@ {{end}} {{.i18n.Tr "custom.head.course"}} {{.i18n.Tr "custom.head.openi.repo"}} - 科技2030项目 + diff --git a/templates/base/head_navbar_pro.tmpl b/templates/base/head_navbar_pro.tmpl index e55bc05ac6..325b0be92c 100644 --- a/templates/base/head_navbar_pro.tmpl +++ b/templates/base/head_navbar_pro.tmpl @@ -60,7 +60,7 @@ {{end}} {{.i18n.Tr "custom.head.course"}} {{.i18n.Tr "custom.head.openi.repo"}} - 科技2030项目 + @@ -108,7 +108,7 @@ {{end}} {{.i18n.Tr "custom.head.course"}} {{.i18n.Tr "custom.head.openi.repo"}} - 科技2030项目 + -- 2.34.1