From 34092a41b060bb498dd3f456d852a11a438ac677 Mon Sep 17 00:00:00 2001 From: wood chen Date: Sun, 29 Sep 2024 00:47:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9D=9E=E5=9B=BE=E7=89=87=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=BC=9A302=E9=87=8D=E5=AE=9A=E5=90=91=E5=88=B0=E5=8E=9F?= =?UTF-8?q?=E5=A7=8B=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/router.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/handler/router.go b/handler/router.go index f115bde..adaf368 100644 --- a/handler/router.go +++ b/handler/router.go @@ -16,6 +16,8 @@ import ( log "github.com/sirupsen/logrus" ) + + func Convert(c *fiber.Ctx) error { // this function need to do: // 1. get request path, query string @@ -59,6 +61,17 @@ func Convert(c *fiber.Ctx) error { log.Debugf("Incoming connection from %s %s %s", c.IP(), reqHostname, reqURIwithQuery) + // 新增:检查是否为图片文件 + if !isImageFile(filename) { + log.Infof("Non-image file requested: %s, redirecting to original URL", filename) + if proxyMode { + return c.Redirect(realRemoteAddr, 302) + } else { + return c.Redirect(reqURIwithQuery, 302) + } + } + + if !helper.CheckAllowedType(filename) { msg := "File extension not allowed! " + filename log.Warn(msg) @@ -186,3 +199,15 @@ func Convert(c *fiber.Ctx) error { c.Set("X-Compression-Rate", helper.GetCompressionRate(rawImageAbs, finalFilename)) return c.SendFile(finalFilename) } + +// 新增:检查文件是否为图片的辅助函数 +func isImageFile(filename string) bool { + ext := strings.ToLower(path.Ext(filename)) + allowedExtensions := []string{"jpg","png","jpeg","gif","bmp","svg","heic","nef",".webp",".tiff"} + for _, allowedExt := range allowedExtensions { + if ext == allowedExt { + return true + } + } + return false +}