0

任何能够将 Golang 的 ReverseProxy 功能与 Iris-Go Web 框架连接起来的人。我无法让它工作。我可以用常规的 net/http 连接它。

func MultiHostReverseProxy(targets map[string]utils.Service) *httputil.ReverseProxy {
    r := regexp.MustCompile(`\/proxy/(?P<Service>[a-zA-Z_-]*)(?P<Path>\/.*)`)
    director := func(req *http.Request) {
        if strings.HasPrefix(req.URL.Path, "/proxy/") {
            temp := r.FindStringSubmatch(req.URL.Path);
            if (len(temp) > 1) {
                system := temp[1]
                if val, ok := targets[system]; ok {
                    s := val.Host + ":" + val.Port
                    req.URL.Scheme = val.Scheme
                    req.URL.Host = s
                    req.URL.Path = temp[2]

                    if enc, ok := GetAxleHeader(req.Header); ok {
                        dec := utils.Decrypt(KEY, enc)
                        req.Header.Set(val.AuthHeader, dec)
                        req.Header.Set(AXLE_HEADER, "")
                    } else {
                        token, nq := utils.FindAxleToken(req.URL.RawQuery);
                        fmt.Printf("%s -> token : %s    newQuery: %s\n", req.URL.RawQuery, token, nq);
                        if token != "" {
                            req.URL.RawQuery = nq
                            dec := utils.Decrypt(KEY, token)
                            req.Header.Set(val.AuthHeader, dec)
                            req.Header.Set(AXLE_HEADER, "")
                        }
                    }
                }
            }
        }
    }
    return &httputil.ReverseProxy{Director: director}
}

如何将这个 ReverseProxy 对象与 iris 框架一起使用;

4

2 回答 2

0

使用 iris 你有两个选择,创建一个代理服务器并运行它:

import "github.com/kataras/iris/core/host"
[...]
target, _ := url.Parse("https://example.com")
go host.NewProxy("example.com:80", target).ListenAndServe()
// this will proxy all http://example.com to https://example.com
// you can use that proxy as you like.

创建一个新的代理处理程序并在您喜欢的任何地方使用它:

import "github.com/kataras/iris/core/host"
[...]
target, _ := url.Parse("https://example.com")
proxy := host.ProxyHandler(target)
http.ListenAndServe("example.com:80", proxy)
于 2017-06-10T11:57:15.140 回答
0

在 Iris 搜索和发布后,我在 iris 资源中得到了这个示例。可能对其他人有帮助

https://github.com/kataras/iris/blob/master/http.go#L1412

于 2016-12-15T17:29:06.327 回答