From fa87584316af8ea2165c537bdcf9a957a8cb5438 Mon Sep 17 00:00:00 2001 From: mh-swift Date: Fri, 23 Aug 2024 12:22:17 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=BC=E5=AE=B9hostname+host=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/internal/controller/admin/common/site.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/server/internal/controller/admin/common/site.go b/server/internal/controller/admin/common/site.go index 57bb909..da3462d 100644 --- a/server/internal/controller/admin/common/site.go +++ b/server/internal/controller/admin/common/site.go @@ -45,7 +45,12 @@ func (c *cSite) Config(ctx context.Context, _ *common.SiteConfigReq) (res *commo func (c *cSite) getWsAddr(ctx context.Context, request *ghttp.Request) string { // 如果是本地IP访问,则认为是调试模式,走实际请求地址,否则走配置中的地址 - ip := ghttp.RequestFromCtx(ctx).GetHost() + // 尝试读取hostname,兼容本地运行模式 + ip := ghttp.RequestFromCtx(ctx).GetHeader("hostname") + if len(ip) == 0 { + ip = ghttp.RequestFromCtx(ctx).GetHost() + } + if validate.IsLocalIPAddr(ip) { return "ws://" + ip + ":" + gstr.StrEx(request.Host, ":") + g.Cfg().MustGet(ctx, "router.websocket.prefix").String() } @@ -59,7 +64,12 @@ func (c *cSite) getWsAddr(ctx context.Context, request *ghttp.Request) string { func (c *cSite) getDomain(ctx context.Context, request *ghttp.Request) string { // 如果是本地IP访问,则认为是调试模式,走实际请求地址,否则走配置中的地址 - ip := request.GetHost() + // 尝试读取hostname,兼容本地运行模式 + ip := ghttp.RequestFromCtx(ctx).GetHeader("hostname") + if len(ip) == 0 { + ip = ghttp.RequestFromCtx(ctx).GetHost() + } + if validate.IsLocalIPAddr(ip) { return "http://" + ip + ":" + gstr.StrEx(request.Host, ":") }