Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
)
Expand Down Expand Up @@ -45,6 +44,20 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
newPath := strings.Replace(r.URL.Path, "/release", "", 1)
newPath = strings.Replace(newPath, "/test", "", 1)

// 提取 X-Forwarded-For 和 X-Real-IP 头部
xForwardedFor := r.Header.Get("X-Forwarded-For")
xRealIP := r.Header.Get("X-Real-IP")

// 确定使用者IP
userIP := ""
if xForwardedFor != "" {
userIP = xForwardedFor
} else if xRealIP != "" {
userIP = xRealIP
} else {
userIP = r.RemoteAddr // 如果没有这些头部,使用连接来源地址
}

// 拼接目标URL(带上查询字符串,如果有的话)
// 如果请求中包含 X-Target-Host 头,则使用该头作为目标域名
// 优先级 header > args > default
Expand All @@ -58,10 +71,8 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
targetURL += "?" + r.URL.RawQuery
}

// 本地打印代理请求完整URL用于调试
if os.Getenv("ENV") == "local" {
fmt.Printf("Proxying request to: %s\n", targetURL)
}
// 打印代理请求信息
fmt.Printf("Proxying [%s] request for [%s]\n", targetURL, userIP)

// 创建代理HTTP请求
proxyReq, err := http.NewRequest(r.Method, targetURL, r.Body)
Expand All @@ -71,6 +82,10 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
return
}

// 删除原始请求中的 X-Forwarded-For 和 X-Real-IP 头部
r.Header.Del("X-Forwarded-For")
r.Header.Del("X-Real-IP")

// 将原始请求头复制到新请求中
for headerKey, headerValues := range r.Header {
for _, headerValue := range headerValues {
Expand Down