Skip to content

Commit 2471cbd

Browse files
为 js request 新增 remote ip
1 parent b89c868 commit 2471cbd

File tree

3 files changed

+3
-4
lines changed

3 files changed

+3
-4
lines changed

global-types/globals.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ declare global {
3535
rawPath: string;
3636
host: string;
3737
remoteAddr: string;
38+
remoteIP: string;
3839
proto: string;
3940
httpVersion: string;
4041
path: string;

pkg/filters/goja/var_request.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/dop251/goja"
99
"gopkg.d7z.net/gitea-pages/pkg/core"
10+
"gopkg.d7z.net/gitea-pages/pkg/utils"
1011
)
1112

1213
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
3839
"rawPath": req.URL.Path,
3940
"host": req.Host,
4041
"remoteAddr": req.RemoteAddr,
42+
"remoteIP": utils.GetRemoteIP(req),
4143
"proto": req.Proto,
4244
"httpVersion": req.Proto,
4345
"path": url.Path,

pkg/utils/net.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import (
99
// 注意,相关 ip 获取未做反向代理安全判断,可能导致安全降级
1010

1111
func GetRemoteIP(r *http.Request) string {
12-
// 最先取 cloudflare 的头
13-
if ip := r.Header.Get("CF-Connecting-IP"); ip != "" {
14-
return ip
15-
}
1612
if forwardedFor := r.Header.Get("X-Forwarded-For"); forwardedFor != "" {
1713
ips := strings.Split(forwardedFor, ",")
1814
if len(ips) > 0 {

0 commit comments

Comments
 (0)