mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 16:41:54 +08:00
fix(internal/handler): add URL parsing and update headers for proxy requests
This commit is contained in:
parent
3ea86189b1
commit
d8e9b90105
@ -6,6 +6,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -59,6 +60,15 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
targetPath := strings.TrimPrefix(r.URL.Path, matchedPrefix)
|
targetPath := strings.TrimPrefix(r.URL.Path, matchedPrefix)
|
||||||
targetURL := targetBase + targetPath
|
targetURL := targetBase + targetPath
|
||||||
|
|
||||||
|
// 解析目标 URL 以获取 host
|
||||||
|
parsedURL, err := url.Parse(targetURL)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Error parsing target URL", http.StatusInternalServerError)
|
||||||
|
log.Printf("[%s] %s %s -> 500 (error parsing URL: %v) [%v]",
|
||||||
|
getClientIP(r), r.Method, r.URL.Path, err, time.Since(startTime))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 创建新的请求
|
// 创建新的请求
|
||||||
proxyReq, err := http.NewRequest(r.Method, targetURL, r.Body)
|
proxyReq, err := http.NewRequest(r.Method, targetURL, r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -71,9 +81,21 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// 复制原始请求的 header
|
// 复制原始请求的 header
|
||||||
copyHeader(proxyReq.Header, r.Header)
|
copyHeader(proxyReq.Header, r.Header)
|
||||||
|
|
||||||
// 设置一些必要的头部
|
// 设置必要的头部,使用目标站点的 Host
|
||||||
proxyReq.Header.Set("X-Forwarded-Host", r.Host)
|
proxyReq.Host = parsedURL.Host
|
||||||
|
proxyReq.Header.Set("Host", parsedURL.Host)
|
||||||
proxyReq.Header.Set("X-Real-IP", getClientIP(r))
|
proxyReq.Header.Set("X-Real-IP", getClientIP(r))
|
||||||
|
proxyReq.Header.Set("X-Forwarded-Host", r.Host)
|
||||||
|
proxyReq.Header.Set("X-Forwarded-Proto", r.URL.Scheme)
|
||||||
|
|
||||||
|
// 添加或更新 X-Forwarded-For
|
||||||
|
if clientIP := getClientIP(r); clientIP != "" {
|
||||||
|
if prior := proxyReq.Header.Get("X-Forwarded-For"); prior != "" {
|
||||||
|
proxyReq.Header.Set("X-Forwarded-For", prior+", "+clientIP)
|
||||||
|
} else {
|
||||||
|
proxyReq.Header.Set("X-Forwarded-For", clientIP)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 发送代理请求
|
// 发送代理请求
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user