feat(proxy): 增强代理功能,支持HTTPS跳过验证并添加Host/X-Forwarded-For指令

This commit is contained in:
程广 2025-06-24 18:50:25 +08:00
parent b58e51e4f5
commit fdb2f5c238
3 changed files with 30 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package handler package handler
import ( import (
"crypto/tls"
"fmt" "fmt"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
@ -58,7 +59,12 @@ func (p *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// 返回值: // 返回值:
// httputil.ReverseProxy实例 // httputil.ReverseProxy实例
func makeProxy(upstream string, path *model.HttpPath, index int) *httputil.ReverseProxy { func makeProxy(upstream string, path *model.HttpPath, index int) *httputil.ReverseProxy {
l := gologger.GetLogger("Proxy")
p := &httputil.ReverseProxy{} p := &httputil.ReverseProxy{}
p.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
directiveHandlers := []func(r *http.Request){} directiveHandlers := []func(r *http.Request){}
if len(path.Directives) > 0 { if len(path.Directives) > 0 {
for _, directive := range path.Directives { 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) { p.Director = func(req *http.Request) {
for _, handler := range directiveHandlers { for _, handler := range directiveHandlers {
handler(req) handler(req)
l.Info(fmt.Sprintf("proxy %s to %s", req.URL.String(), upstream))
} }
} }
p.ModifyResponse = func(resp *http.Response) error { 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 hasSticky := false
for _, cookie := range resp.Cookies() { for _, cookie := range resp.Cookies() {
if cookie.Name == "s" { if cookie.Name == "s" {

View File

@ -11,6 +11,19 @@ import (
type ProxyRequestUpdater func(arg ...string) func(r *http.Request) type ProxyRequestUpdater func(arg ...string) func(r *http.Request)
var ProxyRequestUpdateMap = map[string]ProxyRequestUpdater{ 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) { "HostSchemas": func(arg ...string) func(r *http.Request) {
targetUrl := arg[0] targetUrl := arg[0]
return func(r *http.Request) { return func(r *http.Request) {

View File

@ -21,6 +21,15 @@
"path": "/", "path": "/",
"root": "./example", "root": "./example",
"default": "index.html" "default": "index.html"
},
{
"path":"/HPImageArchive.aspx",
"upstreams":["https://cn.bing.com"],
"directives":[
"Proxy_HostSchemas $target",
"Proxy_Host $target",
"Proxy_XForwardFor"
]
} }
] ]
} }