From 2471cbd87bcfed99ed7ba4af8db0c7b4540d436c Mon Sep 17 00:00:00 2001 From: ExplodingDragon Date: Tue, 25 Nov 2025 21:22:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=20js=20request=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=20remote=20ip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- global-types/globals.d.ts | 1 + pkg/filters/goja/var_request.go | 2 ++ pkg/utils/net.go | 4 ---- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/global-types/globals.d.ts b/global-types/globals.d.ts index cc2d6f1..10f7630 100644 --- a/global-types/globals.d.ts +++ b/global-types/globals.d.ts @@ -35,6 +35,7 @@ declare global { rawPath: string; host: string; remoteAddr: string; + remoteIP: string; proto: string; httpVersion: string; path: string; diff --git a/pkg/filters/goja/var_request.go b/pkg/filters/goja/var_request.go index 73099e7..c8a9493 100644 --- a/pkg/filters/goja/var_request.go +++ b/pkg/filters/goja/var_request.go @@ -7,6 +7,7 @@ import ( "github.com/dop251/goja" "gopkg.d7z.net/gitea-pages/pkg/core" + "gopkg.d7z.net/gitea-pages/pkg/utils" ) func RequestInject(ctx core.FilterContext, jsCtx *goja.Runtime, req *http.Request) error { @@ -38,6 +39,7 @@ func RequestInject(ctx core.FilterContext, jsCtx *goja.Runtime, req *http.Reques "rawPath": req.URL.Path, "host": req.Host, "remoteAddr": req.RemoteAddr, + "remoteIP": utils.GetRemoteIP(req), "proto": req.Proto, "httpVersion": req.Proto, "path": url.Path, diff --git a/pkg/utils/net.go b/pkg/utils/net.go index 6bcb472..6f49235 100644 --- a/pkg/utils/net.go +++ b/pkg/utils/net.go @@ -9,10 +9,6 @@ import ( // 注意,相关 ip 获取未做反向代理安全判断,可能导致安全降级 func GetRemoteIP(r *http.Request) string { - // 最先取 cloudflare 的头 - if ip := r.Header.Get("CF-Connecting-IP"); ip != "" { - return ip - } if forwardedFor := r.Header.Get("X-Forwarded-For"); forwardedFor != "" { ips := strings.Split(forwardedFor, ",") if len(ips) > 0 {