From 1d80bc640d6ee38d7011b3cf148219629d64531c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Mon, 11 Sep 2023 16:05:59 +0800 Subject: [PATCH] =?UTF-8?q?WebP=E8=BD=AC=E6=8D=A2=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=AE=BD=E5=BA=A6=E5=92=8C=E9=AB=98=E5=BA=A6=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_writer.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/internal/nodes/http_writer.go b/internal/nodes/http_writer.go index 17fd0ab..c6f6998 100644 --- a/internal/nodes/http_writer.go +++ b/internal/nodes/http_writer.go @@ -16,6 +16,7 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils/fasttime" "github.com/TeaOSLab/EdgeNode/internal/utils/readers" + setutils "github.com/TeaOSLab/EdgeNode/internal/utils/sets" "github.com/TeaOSLab/EdgeNode/internal/utils/writers" _ "github.com/biessek/golang-ico" "github.com/iwind/TeaGo/lists" @@ -39,6 +40,7 @@ import ( var webpMaxBufferSize int64 = 1_000_000_000 var webpTotalBufferSize int64 = 0 +var webpIgnoreURLSet = setutils.NewFixedSet(131072) func init() { if !teaconst.IsMain { @@ -47,7 +49,7 @@ func init() { var systemMemory = utils.SystemMemoryGB() / 8 if systemMemory > 0 { - webpMaxBufferSize = int64(systemMemory) * 1024 * 1024 * 1024 + webpMaxBufferSize = int64(systemMemory) << 30 } } @@ -547,6 +549,11 @@ func (this *HTTPWriter) PrepareWebP(resp *http.Response, size int64) { this.req.web.WebP.IsOn && this.req.web.WebP.MatchResponse(contentType, size, filepath.Ext(this.req.Path()), this.req.Format) && this.req.web.WebP.MatchAccept(this.req.requestHeader("Accept")) { + // 检查是否已经因为尺寸过大而忽略 + if webpIgnoreURLSet.Has(this.req.URL()) { + return + } + // 如果已经是WebP不再重复处理 // TODO 考虑是否需要很严格的匹配 if strings.Contains(contentType, "image/webp") { @@ -1072,12 +1079,24 @@ func (this *HTTPWriter) finishWebP() { var err error if isGif { gifImage, err = gif.DecodeAll(reader) + if gifImage != nil && (gifImage.Config.Width > gowebp.WebPMaxDimension || gifImage.Config.Height > gowebp.WebPMaxDimension) { + webpIgnoreURLSet.Push(this.req.URL()) + return + } } else { imageData, _, err = image.Decode(reader) + if imageData != nil { + var bound = imageData.Bounds() + if bound.Max.X > gowebp.WebPMaxDimension || bound.Max.Y > gowebp.WebPMaxDimension { + webpIgnoreURLSet.Push(this.req.URL()) + return + } + } } if err != nil { // 发生了错误终止处理 + webpIgnoreURLSet.Push(this.req.URL()) return }