From 889b9d063aa87e912e156fe676c770d6a9c9f32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sun, 23 May 2021 17:01:08 +0800 Subject: [PATCH] =?UTF-8?q?URL=E8=B7=B3=E8=BD=AC=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=AD=A3=E5=88=99=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request_host_redirect.go | 37 ++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/internal/nodes/http_request_host_redirect.go b/internal/nodes/http_request_host_redirect.go index a13ba69..e4f3b29 100644 --- a/internal/nodes/http_request_host_redirect.go +++ b/internal/nodes/http_request_host_redirect.go @@ -2,6 +2,7 @@ package nodes import ( "net/http" + "strconv" "strings" ) @@ -12,7 +13,7 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) { if !u.IsOn { continue } - if u.MatchPrefix { + if u.MatchPrefix { // 匹配前缀 if strings.HasPrefix(fullURL, u.BeforeURL) { afterURL := u.AfterURL if u.KeepRequestURI { @@ -25,7 +26,39 @@ func (this *HTTPRequest) doHostRedirect() (blocked bool) { } return true } - } else { + } else if u.MatchRegexp { // 正则匹配 + reg := u.BeforeURLRegexp() + if reg == nil { + continue + } + matches := reg.FindStringSubmatch(fullURL) + if len(matches) == 0 { + continue + } + afterURL := u.AfterURL + for i, match := range matches { + afterURL = strings.ReplaceAll(afterURL, "${"+strconv.Itoa(i)+"}", match) + } + + subNames := reg.SubexpNames() + if len(subNames) > 0 { + for _, subName := range subNames { + if len(subName) > 0 { + index := reg.SubexpIndex(subName) + if index > -1 { + afterURL = strings.ReplaceAll(afterURL, "${"+subName+"}", matches[index]) + } + } + } + } + + if u.Status <= 0 { + http.Redirect(this.RawWriter, this.RawReq, afterURL, http.StatusTemporaryRedirect) + } else { + http.Redirect(this.RawWriter, this.RawReq, afterURL, u.Status) + } + return true + } else { // 精准匹配 if fullURL == u.RealBeforeURL() { if u.Status <= 0 { http.Redirect(this.RawWriter, this.RawReq, u.AfterURL, http.StatusTemporaryRedirect)