Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go Proxy #1

Open
CharLemAznable opened this issue Mar 19, 2019 · 1 comment
Open

Go Proxy #1

CharLemAznable opened this issue Mar 19, 2019 · 1 comment
Labels

Comments

@CharLemAznable
Copy link
Owner

首先是因为有墙

$ export GOPROXY="https://goproxy.io"

的确好用, golang.org/x/...这些包都能go get到了.

然后提交了自己的工具包

release了新版本, 却怎么都go get不到

$ go: get ...... unexpected end of JSON input

见了鬼了, 辣鸡modules

各种尝试, 一顿搜索, 最后:

$ export GOPROXY=""

终于能go get到了.

墙才是最终的辣鸡;
@CharLemAznable
Copy link
Owner Author

Go语言内置的反向代理

func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy {
	targetQuery := target.RawQuery
	director := func(req *http.Request) {
		req.URL.Scheme = target.Scheme
		req.URL.Host = target.Host
		req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
		if targetQuery == "" || req.URL.RawQuery == "" {
			req.URL.RawQuery = targetQuery + req.URL.RawQuery
		} else {
			req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
		}
		if _, ok := req.Header["User-Agent"]; !ok {
			// explicitly disable User-Agent so it's not set to default value
			req.Header.Set("User-Agent", "")
		}
	}
	return &ReverseProxy{Director: director}
}

原本代理本地的服务没有问题, 代理到微信服务就404.
参考了一篇博客:
Go 语言实现 HTTP 层面的反向代理
加了一行代码:

func reverseProxy(target *url.URL) *httputil.ReverseProxy {
    targetQuery := target.RawQuery
    director := func(req *http.Request) {
        req.Host = target.Host // Different from the default NewSingleHostReverseProxy()
        req.URL.Scheme = target.Scheme
        req.URL.Host = target.Host
        req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
        if targetQuery == "" || req.URL.RawQuery == "" {
            req.URL.RawQuery = targetQuery + req.URL.RawQuery
        } else {
            req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
        }
        if _, ok := req.Header["User-Agent"]; !ok {
            req.Header.Set("User-Agent", "")
        }
    }
    return &httputil.ReverseProxy{Director: director}
}

反向代理成功.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant