feat(proxy): 增强代理功能,支持HTTPS跳过验证并添加Host/X-Forwarded-For指令
This commit is contained in:
parent
b58e51e4f5
commit
fdb2f5c238
|
@ -1,6 +1,7 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
|
@ -58,7 +59,12 @@ func (p *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
// 返回值:
|
||||
// httputil.ReverseProxy实例
|
||||
func makeProxy(upstream string, path *model.HttpPath, index int) *httputil.ReverseProxy {
|
||||
l := gologger.GetLogger("Proxy")
|
||||
p := &httputil.ReverseProxy{}
|
||||
p.Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
|
||||
directiveHandlers := []func(r *http.Request){}
|
||||
if len(path.Directives) > 0 {
|
||||
for _, directive := range path.Directives {
|
||||
|
@ -76,10 +82,12 @@ func makeProxy(upstream string, path *model.HttpPath, index int) *httputil.Rever
|
|||
p.Director = func(req *http.Request) {
|
||||
for _, handler := range directiveHandlers {
|
||||
handler(req)
|
||||
l.Info(fmt.Sprintf("proxy %s to %s", req.URL.String(), upstream))
|
||||
}
|
||||
}
|
||||
|
||||
p.ModifyResponse = func(resp *http.Response) error {
|
||||
l.Info(fmt.Sprintf("proxy %s to %s, with status %d", resp.Request.URL.String(), upstream, resp.StatusCode))
|
||||
hasSticky := false
|
||||
for _, cookie := range resp.Cookies() {
|
||||
if cookie.Name == "s" {
|
||||
|
|
|
@ -11,6 +11,19 @@ import (
|
|||
type ProxyRequestUpdater func(arg ...string) func(r *http.Request)
|
||||
|
||||
var ProxyRequestUpdateMap = map[string]ProxyRequestUpdater{
|
||||
"Host": func(arg ...string) func(r *http.Request) {
|
||||
targetUrl := arg[0]
|
||||
return func(r *http.Request) {
|
||||
turl, _ := url.Parse(targetUrl)
|
||||
r.Host = turl.Host
|
||||
}
|
||||
},
|
||||
"XForwardFor": func(arg ...string) func(r *http.Request) {
|
||||
return func(r *http.Request) {
|
||||
r.Header.Set("X-Forwarded-For", r.RemoteAddr)
|
||||
|
||||
}
|
||||
},
|
||||
"HostSchemas": func(arg ...string) func(r *http.Request) {
|
||||
targetUrl := arg[0]
|
||||
return func(r *http.Request) {
|
||||
|
|
|
@ -21,6 +21,15 @@
|
|||
"path": "/",
|
||||
"root": "./example",
|
||||
"default": "index.html"
|
||||
},
|
||||
{
|
||||
"path":"/HPImageArchive.aspx",
|
||||
"upstreams":["https://cn.bing.com"],
|
||||
"directives":[
|
||||
"Proxy_HostSchemas $target",
|
||||
"Proxy_Host $target",
|
||||
"Proxy_XForwardFor"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue