为 js request 新增 remote ip
This commit is contained in:
1
global-types/globals.d.ts
vendored
1
global-types/globals.d.ts
vendored
@@ -35,6 +35,7 @@ declare global {
|
|||||||
rawPath: string;
|
rawPath: string;
|
||||||
host: string;
|
host: string;
|
||||||
remoteAddr: string;
|
remoteAddr: string;
|
||||||
|
remoteIP: string;
|
||||||
proto: string;
|
proto: string;
|
||||||
httpVersion: string;
|
httpVersion: string;
|
||||||
path: string;
|
path: string;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/dop251/goja"
|
"github.com/dop251/goja"
|
||||||
"gopkg.d7z.net/gitea-pages/pkg/core"
|
"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 {
|
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,
|
"rawPath": req.URL.Path,
|
||||||
"host": req.Host,
|
"host": req.Host,
|
||||||
"remoteAddr": req.RemoteAddr,
|
"remoteAddr": req.RemoteAddr,
|
||||||
|
"remoteIP": utils.GetRemoteIP(req),
|
||||||
"proto": req.Proto,
|
"proto": req.Proto,
|
||||||
"httpVersion": req.Proto,
|
"httpVersion": req.Proto,
|
||||||
"path": url.Path,
|
"path": url.Path,
|
||||||
|
|||||||
@@ -9,10 +9,6 @@ import (
|
|||||||
// 注意,相关 ip 获取未做反向代理安全判断,可能导致安全降级
|
// 注意,相关 ip 获取未做反向代理安全判断,可能导致安全降级
|
||||||
|
|
||||||
func GetRemoteIP(r *http.Request) string {
|
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 != "" {
|
if forwardedFor := r.Header.Get("X-Forwarded-For"); forwardedFor != "" {
|
||||||
ips := strings.Split(forwardedFor, ",")
|
ips := strings.Split(forwardedFor, ",")
|
||||||
if len(ips) > 0 {
|
if len(ips) > 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user