特殊页面中的URL抓取的内容也支持请求变量

This commit is contained in:
GoEdgeLab
2021-09-21 10:13:30 +08:00
parent 88b1d20dcd
commit 2a8658c436
4 changed files with 15 additions and 7 deletions

View File

@@ -21,7 +21,7 @@ func (this *HTTPRequest) doPage(status int) (shouldStop bool) {
for _, page := range this.web.Pages { for _, page := range this.web.Pages {
if page.Match(status) { if page.Match(status) {
if urlPrefixRegexp.MatchString(page.URL) { if urlPrefixRegexp.MatchString(page.URL) {
this.doURL(http.MethodGet, page.URL, "", page.NewStatus) this.doURL(http.MethodGet, page.URL, "", page.NewStatus, true)
return true return true
} else { } else {
file := Tea.Root + Tea.DS + page.URL file := Tea.Root + Tea.DS + page.URL

View File

@@ -19,7 +19,7 @@ func (this *HTTPRequest) doRewrite() (shouldShop bool) {
if len(this.rewriteRule.ProxyHost) > 0 { if len(this.rewriteRule.ProxyHost) > 0 {
host = this.rewriteRule.ProxyHost host = this.rewriteRule.ProxyHost
} }
this.doURL(this.RawReq.Method, this.rewriteReplace, host, 0) this.doURL(this.RawReq.Method, this.rewriteReplace, host, 0, false)
return true return true
} }

View File

@@ -17,7 +17,7 @@ func (this *HTTPRequest) doShutdown() {
} }
if urlPrefixRegexp.MatchString(shutdown.URL) { // URL if urlPrefixRegexp.MatchString(shutdown.URL) { // URL
this.doURL(http.MethodGet, shutdown.URL, "", shutdown.Status) this.doURL(http.MethodGet, shutdown.URL, "", shutdown.Status, true)
return return
} }

View File

@@ -1,7 +1,6 @@
package nodes package nodes
import ( import (
"errors"
"github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/remotelogs"
"github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/iwind/TeaGo/logs" "github.com/iwind/TeaGo/logs"
@@ -11,7 +10,7 @@ import (
) )
// 请求某个URL // 请求某个URL
func (this *HTTPRequest) doURL(method string, url string, host string, statusCode int) { func (this *HTTPRequest) doURL(method string, url string, host string, statusCode int, supportVariables bool) {
req, err := http.NewRequest(method, url, this.RawReq.Body) req, err := http.NewRequest(method, url, this.RawReq.Body)
if err != nil { if err != nil {
logs.Error(err) logs.Error(err)
@@ -35,7 +34,7 @@ func (this *HTTPRequest) doURL(method string, url string, host string, statusCod
var client = utils.SharedHttpClient(60 * time.Second) var client = utils.SharedHttpClient(60 * time.Second)
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
logs.Error(errors.New(req.URL.String() + ": " + err.Error())) remotelogs.Error("HTTP_REQUEST_URL", req.URL.String()+": "+err.Error())
this.write50x(err, http.StatusInternalServerError) this.write50x(err, http.StatusInternalServerError)
return return
} }
@@ -50,6 +49,9 @@ func (this *HTTPRequest) doURL(method string, url string, host string, statusCod
this.processResponseHeaders(statusCode) this.processResponseHeaders(statusCode)
} }
if supportVariables {
resp.Header.Del("Content-Length")
}
this.writer.AddHeaders(resp.Header) this.writer.AddHeaders(resp.Header)
if statusCode <= 0 { if statusCode <= 0 {
this.writer.Prepare(resp.ContentLength, resp.StatusCode) this.writer.Prepare(resp.ContentLength, resp.StatusCode)
@@ -67,7 +69,13 @@ func (this *HTTPRequest) doURL(method string, url string, host string, statusCod
// 输出内容 // 输出内容
pool := this.bytePool(resp.ContentLength) pool := this.bytePool(resp.ContentLength)
buf := pool.Get() buf := pool.Get()
if supportVariables {
_, err = utils.CopyWithFilter(this.writer, resp.Body, buf, func(p []byte) []byte {
return []byte(this.Format(string(p)))
})
} else {
_, err = io.CopyBuffer(this.writer, resp.Body, buf) _, err = io.CopyBuffer(this.writer, resp.Body, buf)
}
pool.Put(buf) pool.Put(buf)
if err != nil { if err != nil {