package server import ( "net/http" "net/http/httputil" "net/url" "git.magicany.cc/black1552/gf-common/log" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/os/gctx" ) // BuildRequest 反向代理请求到指定主机 // 自动支持所有 HTTP 方法及 WebSocket 连接 func BuildRequest(r *ghttp.Request, host string) { targetURL, err := url.Parse(host) if err != nil { log.Error(gctx.New(), "parse target host error:", err) r.Response.WriteStatus(http.StatusInternalServerError) r.Response.Write([]byte("Invalid target host")) return } proxy := httputil.NewSingleHostReverseProxy(targetURL) // 自定义 Director 来设置目标 URL,保留原始请求的所有参数 proxy.Director = func(req *http.Request) { req.URL.Scheme = targetURL.Scheme req.URL.Host = targetURL.Host req.URL.Path = r.URL.Path req.URL.RawQuery = r.URL.RawQuery // 保留原始 Host 头 req.Host = r.Host } // 错误处理 proxy.ErrorHandler = func(w http.ResponseWriter, req *http.Request, err error) { log.Error(gctx.New(), "proxy error:", err) w.WriteHeader(http.StatusBadGateway) w.Write([]byte("Bad Gateway")) } // ServeHTTP 会自动处理 WebSocket 升级和所有 HTTP 方法 proxy.ServeHTTP(r.Response.Writer, r.Request) }