2021-08-06 16:02:59 +08:00
|
|
|
// Package proxy...
|
|
|
|
//
|
|
|
|
// Description : 正向代理的实现
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2021-08-06 2:07 下午
|
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2021-08-06 20:13:02 +08:00
|
|
|
"compress/gzip"
|
2021-08-06 16:02:59 +08:00
|
|
|
"io"
|
2021-08-06 20:13:02 +08:00
|
|
|
"io/ioutil"
|
2021-08-06 16:02:59 +08:00
|
|
|
"net/http"
|
2021-12-26 13:55:10 +08:00
|
|
|
"net/http/httputil"
|
2021-08-06 16:02:59 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Forward 正向代理的实现
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2:08 下午 2021/8/6
|
|
|
|
func Forward(rw http.ResponseWriter, req *http.Request, serverConfig *Server) {
|
|
|
|
|
2021-12-26 13:55:10 +08:00
|
|
|
if !strings.HasPrefix(serverConfig.URI, "/") {
|
|
|
|
serverConfig.URI = "/" + serverConfig.URI
|
2021-08-06 16:02:59 +08:00
|
|
|
}
|
2021-12-26 13:55:10 +08:00
|
|
|
// 请求重写方法
|
|
|
|
director := func(req *http.Request) {
|
|
|
|
req.URL.Scheme = serverConfig.Scheme
|
|
|
|
// req.URL.Host = projectDetail.GetProjectDetail().Domain + ":" + fmt.Sprintf("%v", projectDetail.GetProjectDetail().Port)
|
|
|
|
// req.Host = projectDetail.GetProjectDetail().Domain + ":" + fmt.Sprintf("%v", projectDetail.GetProjectDetail().Port)
|
|
|
|
req.Host = serverConfig.Host
|
|
|
|
req.URL.Host = serverConfig.Host
|
|
|
|
req.URL.Path = serverConfig.URI
|
|
|
|
req.RequestURI = serverConfig.URI
|
|
|
|
// 写入重写的请求Header
|
|
|
|
for k, v := range serverConfig.RewriteRequestHeader {
|
|
|
|
req.Header.Set(k, v)
|
2021-08-06 16:02:59 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-26 13:55:10 +08:00
|
|
|
// TODO : 重写响应数据
|
|
|
|
modifyResponseFunc := func(rep *http.Response) error {
|
|
|
|
return nil
|
2021-08-06 20:13:02 +08:00
|
|
|
}
|
2021-12-26 13:55:10 +08:00
|
|
|
p := &httputil.ReverseProxy{Director: director, ModifyResponse: modifyResponseFunc}
|
|
|
|
p.ServeHTTP(rw, req)
|
2021-09-11 22:38:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// getResultCompressType 获取返回结果的压缩方式
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 9:11 下午 2021/9/11
|
|
|
|
func getResultCompressType(res *http.Response) string {
|
|
|
|
return strings.ToLower(res.Header.Get("Content-Encoding"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// getResponseData 解析响应数据
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 9:15 下午 2021/9/11
|
|
|
|
func getResponseData(res *http.Response) ([]byte, error) {
|
|
|
|
var (
|
|
|
|
responseData []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch getResultCompressType(res) {
|
|
|
|
case "gzip":
|
|
|
|
var gzipData io.Reader
|
|
|
|
if gzipData, err = gzip.NewReader(res.Body); nil == err {
|
|
|
|
// gzip 处理过的数据
|
|
|
|
responseData, err = ioutil.ReadAll(gzipData)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// 默认没有任何压缩
|
|
|
|
responseData, err = io.ReadAll(res.Body)
|
2021-08-06 20:49:42 +08:00
|
|
|
}
|
2021-09-11 22:38:45 +08:00
|
|
|
return responseData, err
|
2021-08-06 16:02:59 +08:00
|
|
|
}
|