亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 httputil.ReverseProxy 設置 X-Forwarded-For

如何使用 httputil.ReverseProxy 設置 X-Forwarded-For

Go
守候你守候我 2022-11-28 10:15:24
我為什么要這樣做我已經兩次遇到這個問題。第一次是使用位于僅 IPv6 服務器網絡內的反向代理。客戶端請求通過 NAT46 傳入。請求的源 IP 變為 [固定的 96 位前綴] + [32 位客戶端 IPv4 地址]。這意味著反向代理始終可以識別真實的客戶端 IP。不過,我找不到將X-Forwarded-For標頭設置為該地址的方法。我通過修改后端服務器解決了這個問題。這次我有一個將在 Google App Engine 上運行的反向代理。請求首先到達 Google 的負載均衡器,它添加X-Forwarded-For標頭并將請求轉發到我的應用程序。我想稍微修改一下請求,然后將其傳遞給后端服務器,我無法修改。后端需要原始客戶端IP,并且可以通過X-Forwarded-For(已通過身份驗證,不用擔心)接受它。在這種情況下,我想X-Forwarded-For通過未修改的方式傳遞來自 Google 負載均衡器的標頭。問題似乎無法設置X-Forwarded-For為我在使用時選擇的值httputil.ReverseProxy。如果我設置它(下面的選項 1),將附加來自 TCP 連接的客戶端地址。如果我將它設置為nil(下面的選項 2),它會像文檔中建議的那樣被省略,但這也不是我想要的。package mainimport (    "log"    "net/http"    "net/http/httputil")func director(req *http.Request) {    // currently running a PHP script that just records headers    host := "bsweb02.bentonvillek12.org"    // who we dial    req.URL.Scheme = "https"    req.URL.Host = host    // host header    req.Host = host    // proof that most headers can be passed through fine    req.Header["Foo"] = []string{"Bar"}    req.Header["X-Forwarded-For"] = []string{"1.2.3.4"} // option 1    //req.Header["X-Forwarded-For"] = nil               // option 2}func main() {    http.ListenAndServe(":80", &httputil.ReverseProxy{        Director: director,    })}選項1array(9) {  ["Content-Type"]=>  string(0) ""  ["Content-Length"]=>  string(1) "0"  ["Foo"]=>  string(3) "Bar"  ["X-Forwarded-For"]=>  string(18) "1.2.3.4, 127.0.0.1"  ["User-Agent"]=>  string(11) "curl/7.81.0"  ["Host"]=>  string(26) "bsweb02.bentonvillek12.org"  ["Accept-Encoding"]=>  string(4) "gzip"  ["Accept"]=>  string(3) "*/*"  ["Connection"]=>  string(5) "close"}選項 2array(8) {  ["Content-Type"]=>  string(0) ""  ["Content-Length"]=>  string(1) "0"  ["Foo"]=>  string(3) "Bar"  ["User-Agent"]=>  string(11) "curl/7.81.0"  ["Host"]=>  string(26) "bsweb02.bentonvillek12.org"  ["Accept-Encoding"]=>  string(4) "gzip"  ["Accept"]=>  string(3) "*/*"  ["Connection"]=>  string(5) "close"}
查看完整描述

1 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

我相信你有兩個選擇。


1.實施http.RoundTripper

您實現自己的RoundTripper并在那里重新設置X-Forwarded-For。(示范)


type MyRoundTripper struct{}


func (t *MyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {

    req.Header["X-Forwarded-For"] = []string{"1.2.3.4"}

    return http.DefaultTransport.RoundTrip(req)

}


func main() {

    http.ListenAndServe(":80", &httputil.ReverseProxy{

        Director: director,

        Transport: &MyRoundTripper{},

    })

}

當Transport未設置該字段時,httputil.ReverseProxy它會回退到http.DefaultTransport,因此您也可以在自定義代碼后回退到它。


2.取消設置req.RemoteAddr

RemoteAddr在調用反向代理之前重置原始請求的字段。該字段由 HTTP 服務器設置,如果存在,將觸發X-Forwarded-For反向代理實現中的替換。(示范)


func main() {

    http.ListenAndServe(":80", func(w http.ResponseWriter, r *http.Request) {

        r.RemoteAddr = ""

        proxy := &httputil.ReverseProxy{ Director: director } 

        proxy.ServeHTTP(w, r)    

    })

}

但是,此行為依賴于實現細節,將來可能會或可能不會更改。我建議改用選項 1。


查看完整回答
反對 回復 2022-11-28
  • 1 回答
  • 0 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號