From 7424f27cf30065a1308aa3ba4d75ea82c0af4af9 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 10 May 2024 20:07:01 +0800 Subject: Check if reverse proxy is correctly configured (#30890) Follow #27011 Follow #30885 --------- Co-authored-by: silverwind Co-authored-by: Giteabot --- routers/web/admin/admin.go | 12 ++++++++++++ routers/web/admin/admin_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) (limited to 'routers/web/admin') diff --git a/routers/web/admin/admin.go b/routers/web/admin/admin.go index 3dd3c9670f..dee1650b5a 100644 --- a/routers/web/admin/admin.go +++ b/routers/web/admin/admin.go @@ -9,12 +9,14 @@ import ( "net/http" "runtime" "sort" + "strings" "time" activities_model "code.gitea.io/gitea/models/activities" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/httplib" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -223,6 +225,16 @@ func SelfCheck(ctx *context.Context) { ctx.HTML(http.StatusOK, tplSelfCheck) } +func SelfCheckPost(ctx *context.Context) { + var problems []string + frontendAppURL := ctx.FormString("location_origin") + setting.AppSubURL + "/" + ctxAppURL := httplib.GuessCurrentAppURL(ctx) + if !strings.HasPrefix(ctxAppURL, frontendAppURL) { + problems = append(problems, ctx.Locale.TrString("admin.self_check.location_origin_mismatch", frontendAppURL, ctxAppURL)) + } + ctx.JSON(http.StatusOK, map[string]any{"problems": problems}) +} + func CronTasks(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("admin.monitor.cron") ctx.Data["PageIsAdminMonitorCron"] = true diff --git a/routers/web/admin/admin_test.go b/routers/web/admin/admin_test.go index 2b65ab3ea3..782126adf5 100644 --- a/routers/web/admin/admin_test.go +++ b/routers/web/admin/admin_test.go @@ -4,8 +4,14 @@ package admin import ( + "net/http" "testing" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/test" + "code.gitea.io/gitea/services/contexttest" + "github.com/stretchr/testify/assert" ) @@ -66,3 +72,21 @@ func TestShadowPassword(t *testing.T) { assert.EqualValues(t, k.Result, shadowPassword(k.Provider, k.CfgItem)) } } + +func TestSelfCheckPost(t *testing.T) { + defer test.MockVariableValue(&setting.AppURL, "http://config/sub/")() + defer test.MockVariableValue(&setting.AppSubURL, "/sub")() + + ctx, resp := contexttest.MockContext(t, "GET http://host/sub/admin/self_check?location_origin=http://frontend") + SelfCheckPost(ctx) + assert.EqualValues(t, http.StatusOK, resp.Code) + + data := struct { + Problems []string `json:"problems"` + }{} + err := json.Unmarshal(resp.Body.Bytes(), &data) + assert.NoError(t, err) + assert.Equal(t, []string{ + ctx.Locale.TrString("admin.self_check.location_origin_mismatch", "http://frontend/sub/", "http://host/sub/"), + }, data.Problems) +} -- cgit v1.2.3