From 03f21308279cce4abe060b1fdce84a0c64d25f9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Fri, 7 Jul 2023 11:50:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E4=B8=AD=E5=8F=AA=E5=85=81=E8=AE=B8=E4=BD=BF=E7=94=A8pages/?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E4=B8=8B=E6=96=87=E4=BB=B6=EF=BC=88=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E4=BB=A5=E5=BE=80=E7=89=88=E6=9C=AC=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request_page.go | 43 +++++++++++------------- internal/nodes/http_request_shutdown.go | 44 ++++++++++++------------- internal/nodes/http_request_url.go | 4 +-- 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/internal/nodes/http_request_page.go b/internal/nodes/http_request_page.go index e578a5f..23372aa 100644 --- a/internal/nodes/http_request_page.go +++ b/internal/nodes/http_request_page.go @@ -6,9 +6,10 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/iwind/TeaGo/Tea" - "github.com/iwind/TeaGo/logs" "net/http" "os" + "path" + "strings" ) // 请求特殊页面 @@ -54,30 +55,30 @@ func (this *HTTPRequest) doPageLookup(pages []*serverconfigs.HTTPPageConfig, sta this.doURL(http.MethodGet, page.URL, "", newStatus, true) return true } else { - file := Tea.Root + Tea.DS + page.URL - fp, err := os.Open(file) - if err != nil { - logs.Error(err) - msg := "404 page not found: '" + page.URL + "'" - + var realpath = path.Clean(page.URL) + if !strings.HasPrefix(realpath, "/pages/") && !strings.HasPrefix(realpath, "pages/") { // only files under "/pages/" can be used + var msg = "404 page not found: '" + page.URL + "'" this.writer.WriteHeader(http.StatusNotFound) - _, err := this.writer.Write([]byte(msg)) - if err != nil { - logs.Error(err) - } + _, _ = this.writer.Write([]byte(msg)) return true } + var file = Tea.Root + Tea.DS + realpath + fp, err := os.Open(file) + if err != nil { + var msg = "404 page not found: '" + page.URL + "'" + this.writer.WriteHeader(http.StatusNotFound) + _, _ = this.writer.Write([]byte(msg)) + return true + } + defer func() { + _ = fp.Close() + }() stat, err := fp.Stat() if err != nil { - logs.Error(err) - msg := "404 could not read page content: '" + page.URL + "'" - + var msg = "404 could not read page content: '" + page.URL + "'" this.writer.WriteHeader(http.StatusNotFound) - _, err := this.writer.Write([]byte(msg)) - if err != nil { - logs.Error(err) - } + _, _ = this.writer.Write([]byte(msg)) return true } @@ -92,7 +93,7 @@ func (this *HTTPRequest) doPageLookup(pages []*serverconfigs.HTTPPageConfig, sta this.writer.Prepare(nil, stat.Size(), status, true) this.writer.WriteHeader(status) } - buf := utils.BytePool1k.Get() + var buf = utils.BytePool1k.Get() _, err = utils.CopyWithFilter(this.writer, fp, buf, func(p []byte) []byte { return []byte(this.Format(string(p))) }) @@ -104,10 +105,6 @@ func (this *HTTPRequest) doPageLookup(pages []*serverconfigs.HTTPPageConfig, sta } else { this.writer.SetOk() } - err = fp.Close() - if err != nil { - logs.Error(err) - } } return true diff --git a/internal/nodes/http_request_shutdown.go b/internal/nodes/http_request_shutdown.go index 693aff8..96b9383 100644 --- a/internal/nodes/http_request_shutdown.go +++ b/internal/nodes/http_request_shutdown.go @@ -5,14 +5,15 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/iwind/TeaGo/Tea" - "github.com/iwind/TeaGo/logs" "net/http" "os" + "path" + "strings" ) // 调用临时关闭页面 func (this *HTTPRequest) doShutdown() { - shutdown := this.web.Shutdown + var shutdown = this.web.Shutdown if shutdown == nil { return } @@ -34,28 +35,30 @@ func (this *HTTPRequest) doShutdown() { this.ProcessResponseHeaders(this.writer.Header(), http.StatusOK) this.writer.WriteHeader(http.StatusOK) } - _, err := this.writer.WriteString("The site have been shutdown.") - if err != nil { - logs.Error(err) - } - + _, _ = this.writer.WriteString("The site have been shutdown.") return } // 从本地文件中读取 - file := Tea.Root + Tea.DS + shutdown.URL - fp, err := os.Open(file) - if err != nil { - logs.Error(err) - msg := "404 page not found: '" + shutdown.URL + "'" - + var realpath = path.Clean(shutdown.URL) + if !strings.HasPrefix(realpath, "/pages/") && !strings.HasPrefix(realpath, "pages/") { // only files under "/pages/" can be used + var msg = "404 page not found: '" + shutdown.URL + "'" this.writer.WriteHeader(http.StatusNotFound) - _, err = this.writer.Write([]byte(msg)) - if err != nil { - logs.Error(err) - } + _, _ = this.writer.Write([]byte(msg)) return } + var file = Tea.Root + Tea.DS + shutdown.URL + fp, err := os.Open(file) + if err != nil { + var msg = "404 page not found: '" + shutdown.URL + "'" + this.writer.WriteHeader(http.StatusNotFound) + _, _ = this.writer.Write([]byte(msg)) + return + } + + defer func() { + _ = fp.Close() + }() // 自定义响应Headers if shutdown.Status > 0 { @@ -65,7 +68,7 @@ func (this *HTTPRequest) doShutdown() { this.ProcessResponseHeaders(this.writer.Header(), http.StatusOK) this.writer.WriteHeader(http.StatusOK) } - buf := utils.BytePool1k.Get() + var buf = utils.BytePool1k.Get() _, err = utils.CopyWithFilter(this.writer, fp, buf, func(p []byte) []byte { return []byte(this.Format(string(p))) }) @@ -77,11 +80,6 @@ func (this *HTTPRequest) doShutdown() { } else { this.writer.SetOk() } - - err = fp.Close() - if err != nil { - remotelogs.Warn("HTTP_REQUEST_SHUTDOWN", "close file failed: "+err.Error()) - } } else if shutdown.BodyType == shared.BodyTypeHTML { // 自定义响应Headers if shutdown.Status > 0 { diff --git a/internal/nodes/http_request_url.go b/internal/nodes/http_request_url.go index 0765021..6baea82 100644 --- a/internal/nodes/http_request_url.go +++ b/internal/nodes/http_request_url.go @@ -67,8 +67,8 @@ func (this *HTTPRequest) doURL(method string, url string, host string, statusCod } // 输出内容 - pool := this.bytePool(resp.ContentLength) - buf := pool.Get() + var pool = this.bytePool(resp.ContentLength) + var buf = pool.Get() if supportVariables { _, err = utils.CopyWithFilter(this.writer, resp.Body, buf, func(p []byte) []byte { return []byte(this.Format(string(p)))