Golang 反向代理以避免 SOP

Golang Reverse Proxy to avoid SOP

我目前正在开发一个 Web 应用程序(在 golang 中),它将用作其他内部应用程序(运行 在 docker 容器中)的主要门户。此 Web 应用程序应该只提供一个 HTML-Page,其中导航栏位于顶部,页面的其余部分将是一个 IFrame。在导航栏上,我们有多个链接将更改 IFrame 的源。了解导航栏上的链接是动态创建的很重要。

我很快就遇到了 Iframe 无法显示其他内部应用程序的问题,因为同源策略会阻止所有内容。为了解决这个问题,我认为在 golang 中实现我自己的反向代理可能是个好主意。

package main

import (
    "fmt"
    "net/http"
    "net/http/httputil"
    "net/url"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "<html><body><iframe src=\"/\" width=\"100%\" height=\"100%\"/></body></html>")
}

func proxyHandle(r *http.Request) {
    r.Host = "google.com"
    r.URL.Host = r.Host
    r.URL.Scheme = "http"
}

func main() {
    proxy := httputil.NewSingleHostReverseProxy(&url.URL{
        Scheme: "http",
        Host:   "google.com",
    })
    proxy.Director = proxyHandle

    http.Handle("/", proxy)
    http.HandleFunc("/index", handler)
    http.ListenAndServe(":8080", nil)
}

我仍然收到 SOP 错误消息。我现在基本上有两个问题:

经过 fiddler I was able to find out that the specified address (http://google.com) 的一些调查后,发回重定向响应。交易基本上是这样的:

客户端 -> MyProxy -> RealWebsite

客户端 <- Myproxy <- RealWebsite:位置 https://www.google.com

客户端 -> https://www.google.com

因此,客户端试图在与同源策略冲突的 IFrame 中打开“https://www.google.com”。经过一些研究,我发现了 这个 github 问题 。似乎 golang 反向代理没有转换 "Location"-Header(这是一个重定向)。

我现在可以重写 Location-Header,但我决定使用现有的反向代理 (nginx)。