完成正向代理的主干逻辑
This commit is contained in:
commit
691e6b82df
26
.gitignore
vendored
Normal file
26
.gitignore
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# IDE
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
# Binary
|
||||||
|
realsee-open-svc
|
||||||
|
preline.i.svc.open.realsee.com
|
||||||
|
|
||||||
|
__debug_bin
|
||||||
|
|
||||||
|
# Output
|
||||||
|
releases
|
||||||
|
*.tar.gz
|
||||||
|
|
||||||
|
# Log
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Vendor
|
||||||
|
vendor
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
realsee-open-svc-data-product
|
||||||
|
release
|
||||||
|
nodejs/node_modules/*
|
||||||
|
storage
|
||||||
|
new_api_debug.tar
|
||||||
|
*.tar
|
16
go.mod
Normal file
16
go.mod
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
module git.zhangdeman.cn/zhangdeman/proxy
|
||||||
|
|
||||||
|
go 1.17
|
||||||
|
|
||||||
|
require (
|
||||||
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220723131533-e95f609c0f73 // indirect
|
||||||
|
github.com/Jeffail/gabs v1.4.0 // indirect
|
||||||
|
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
|
||||||
|
github.com/go-ini/ini v1.67.0 // indirect
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||||
|
github.com/tidwall/gjson v1.14.3 // indirect
|
||||||
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
|
github.com/tidwall/pretty v1.2.1 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
22
go.sum
Normal file
22
go.sum
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220723131533-e95f609c0f73 h1:DVDjR7IpxeDKHX+2C8UbrtSwBIUX0JEN/BiZnWcHrUg=
|
||||||
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20220723131533-e95f609c0f73/go.mod h1:zTir/0IWdK3E7n0GiaogyWHADAQnBtTdl2I6Z2/OPqw=
|
||||||
|
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
|
||||||
|
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
|
||||||
|
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
|
||||||
|
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
|
||||||
|
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||||
|
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||||
|
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||||
|
github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw=
|
||||||
|
github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
|
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||||
|
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||||
|
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
|
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
|
||||||
|
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
55
proxy.go
Normal file
55
proxy.go
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
// Package proxy ...
|
||||||
|
//
|
||||||
|
// Description : 正向代理服务器
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2022-10-08 14:26
|
||||||
|
package proxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.zhangdeman.cn/zhangdeman/util"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Forward 正向代理主干逻辑
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 14:28 2022/10/8
|
||||||
|
func Forward(rw http.ResponseWriter, req *http.Request) {
|
||||||
|
// 复制请求
|
||||||
|
proxyRequest := new(http.Request)
|
||||||
|
*proxyRequest = *req
|
||||||
|
if clientIP, _, err := net.SplitHostPort(proxyRequest.RemoteAddr); err == nil {
|
||||||
|
if prior, ok := proxyRequest.Header["X-forwarded-For"]; ok {
|
||||||
|
clientIP = strings.Join(prior, ", ")+ ", "+clientIP
|
||||||
|
}
|
||||||
|
proxyRequest.Header.Set("X-Forwarded-For", clientIP)
|
||||||
|
}
|
||||||
|
// 请求代理机器IP
|
||||||
|
proxyIP := util.IP.GetHostIP()
|
||||||
|
proxyRequest.Header.Set("X-Forward-Proxy-IP", proxyIP)
|
||||||
|
|
||||||
|
// 请求目标服务
|
||||||
|
transport := http.DefaultTransport
|
||||||
|
res, err := transport.RoundTrip(proxyRequest)
|
||||||
|
if nil != err {
|
||||||
|
rw.WriteHeader(http.StatusBadGateway)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 回写客户端响应头
|
||||||
|
for key, values := range res.Header {
|
||||||
|
for _, v := range values {
|
||||||
|
rw.Header().Add(key, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 回写客户端状态码
|
||||||
|
rw.WriteHeader(res.StatusCode)
|
||||||
|
// 回写响应体
|
||||||
|
io.Copy(rw, res.Body)
|
||||||
|
res.Body.Close()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user