golang实现http/https反向代理示例代码
代码语言:golang
所属分类:其他
代码描述:golang实现http/https反向代理示例代码
代码标签: golang http https 反向 代理 示例 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
package main import ( "log" "net/http" "net/http/httputil" "net/url" ) // 创建一个反向代理 func NewProxy(target string) (*httputil.ReverseProxy, error) { url, err := url.Parse(target) if err != nil { return nil, err } proxy := httputil.NewSingleHostReverseProxy(url) originalDirector := proxy.Director proxy.Director = func(req *http.Request) { originalDirector(req) req.Header.Set("X-Forwarded-Host", req.Host) req.Host = url.Host } return proxy, nil } func main() { target := "https://www.baidu.com" // 替换为你的目标服务器 proxy, err := NewProxy(target) if err != nil { log.Fatalf("Could not create proxy: %v", err) } // 设置 HTTP/HTTPS 服务器 http.HandleFunc(&qu.........完整代码请登录后点击上方下载按钮下载查看
网友评论0