From 88b1d20dcd6089261f90fe2c6ef798826706ecd4 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Tue, 21 Sep 2021 09:29:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=B9=E6=AE=8A=E9=A1=B5=E9=9D=A2=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=AF=B7=E6=B1=82=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/pages/403.html | 2 +- build/pages/404.html | 2 +- build/pages/50x.html | 2 +- build/pages/shutdown_en.html | 2 +- build/pages/shutdown_upgrade_zh.html | 2 +- build/pages/shutdown_zh.html | 2 +- internal/nodes/http_request_page.go | 6 ++++-- internal/nodes/http_request_shutdown.go | 6 ++++-- internal/utils/reader_utils.go | 25 +++++++++++++++++++++++++ 9 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 internal/utils/reader_utils.go diff --git a/build/pages/403.html b/build/pages/403.html index af62447..bae67a5 100644 --- a/build/pages/403.html +++ b/build/pages/403.html @@ -9,7 +9,7 @@

403 Forbidden

Sorry, your access to the page has been denied. Please try again later.

- + \ No newline at end of file diff --git a/build/pages/404.html b/build/pages/404.html index 299f3d8..5dd80df 100644 --- a/build/pages/404.html +++ b/build/pages/404.html @@ -9,7 +9,7 @@

404 Not Found

Sorry, the page you are looking for is not found. Please try again later.

- + \ No newline at end of file diff --git a/build/pages/50x.html b/build/pages/50x.html index b9236d6..221a072 100644 --- a/build/pages/50x.html +++ b/build/pages/50x.html @@ -9,7 +9,7 @@

An error occurred.

Sorry, the page you are looking for is currently unavailable. Please try again later.

- + \ No newline at end of file diff --git a/build/pages/shutdown_en.html b/build/pages/shutdown_en.html index 22d7a1d..308741a 100644 --- a/build/pages/shutdown_en.html +++ b/build/pages/shutdown_en.html @@ -9,7 +9,7 @@

The website is shutdown.

Sorry, the page you are looking for is currently unavailable. Please try again later.

- + \ No newline at end of file diff --git a/build/pages/shutdown_upgrade_zh.html b/build/pages/shutdown_upgrade_zh.html index cc6106f..0beea52 100644 --- a/build/pages/shutdown_upgrade_zh.html +++ b/build/pages/shutdown_upgrade_zh.html @@ -9,7 +9,7 @@

网站升级中

为了给您提供更好的服务,我们正在升级网站,请稍后重新访问。

- + \ No newline at end of file diff --git a/build/pages/shutdown_zh.html b/build/pages/shutdown_zh.html index a04f2d0..ba158cb 100644 --- a/build/pages/shutdown_zh.html +++ b/build/pages/shutdown_zh.html @@ -9,7 +9,7 @@

网站暂时关闭

网站已被暂时关闭,请耐心等待我们的重新开通通知。

- + \ No newline at end of file diff --git a/internal/nodes/http_request_page.go b/internal/nodes/http_request_page.go index 5ca1638..aaa5d69 100644 --- a/internal/nodes/http_request_page.go +++ b/internal/nodes/http_request_page.go @@ -2,9 +2,9 @@ package nodes import ( "github.com/TeaOSLab/EdgeNode/internal/remotelogs" + "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/logs" - "io" "net/http" "os" "regexp" @@ -48,7 +48,9 @@ func (this *HTTPRequest) doPage(status int) (shouldStop bool) { this.writer.WriteHeader(status) } buf := bytePool1k.Get() - _, err = io.CopyBuffer(this.writer, fp, buf) + _, err = utils.CopyWithFilter(this.writer, fp, buf, func(p []byte) []byte { + return []byte(this.Format(string(p))) + }) bytePool1k.Put(buf) if err != nil { if !this.canIgnore(err) { diff --git a/internal/nodes/http_request_shutdown.go b/internal/nodes/http_request_shutdown.go index 832c992..8c41f27 100644 --- a/internal/nodes/http_request_shutdown.go +++ b/internal/nodes/http_request_shutdown.go @@ -2,9 +2,9 @@ package nodes import ( "github.com/TeaOSLab/EdgeNode/internal/remotelogs" + "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/logs" - "io" "net/http" "os" ) @@ -64,7 +64,9 @@ func (this *HTTPRequest) doShutdown() { this.writer.WriteHeader(http.StatusOK) } buf := bytePool1k.Get() - _, err = io.CopyBuffer(this.writer, fp, buf) + _, err = utils.CopyWithFilter(this.writer, fp, buf, func(p []byte) []byte { + return []byte(this.Format(string(p))) + }) bytePool1k.Put(buf) if err != nil { if !this.canIgnore(err) { diff --git a/internal/utils/reader_utils.go b/internal/utils/reader_utils.go new file mode 100644 index 0000000..d10704d --- /dev/null +++ b/internal/utils/reader_utils.go @@ -0,0 +1,25 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package utils + +import "io" + +func CopyWithFilter(writer io.Writer, reader io.Reader, buf []byte, filter func(p []byte) []byte) (written int64, err error) { + for { + n, err := reader.Read(buf) + if n > 0 { + n2, err := writer.Write(filter(buf[:n])) + written += int64(n2) + if err != nil { + return written, err + } + } + if err != nil { + if err == io.EOF { + break + } + return written, err + } + } + return written, nil +}