更新 gotemplate 的参数注入
This commit is contained in:
30
pkg/utils/net.go
Normal file
30
pkg/utils/net.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 注意,相关 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 {
|
||||
return strings.TrimSpace(ips[0])
|
||||
}
|
||||
}
|
||||
if realIP := r.Header.Get("X-Real-IP"); realIP != "" {
|
||||
return realIP
|
||||
}
|
||||
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
return r.RemoteAddr
|
||||
}
|
||||
return ip
|
||||
}
|
||||
25
pkg/utils/template.go
Normal file
25
pkg/utils/template.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NewTemplateInject(r *http.Request, def map[string]any) map[string]any {
|
||||
if def == nil {
|
||||
def = make(map[string]any)
|
||||
}
|
||||
headers := make(map[string]string)
|
||||
for k, vs := range r.Header {
|
||||
headers[k] = strings.Join(vs, ",")
|
||||
}
|
||||
def["Request"] = map[string]any{
|
||||
"Headers": headers,
|
||||
"Path": r.URL.Path,
|
||||
"Method": r.Method,
|
||||
"RequestURI": r.RequestURI,
|
||||
"RemoteAddr": r.RemoteAddr,
|
||||
"RemoteIP": GetRemoteIP(r),
|
||||
}
|
||||
return def
|
||||
}
|
||||
Reference in New Issue
Block a user