尝试修复无限重定向

This commit is contained in:
wood chen 2024-09-29 15:04:43 +08:00
parent 68108a3946
commit 0b494e08ac

View File

@ -61,8 +61,21 @@ func Convert(c *fiber.Ctx) error {
log.Debugf("Incoming connection from %s %s %s", c.IP(), reqHostname, reqURIwithQuery) log.Debugf("Incoming connection from %s %s %s", c.IP(), reqHostname, reqURIwithQuery)
// 非图片清况下302到源文件
//
//
//
reqURI := c.Path()
filename := path.Base(reqURI)
// 处理根路径请求
if reqURI == "/" {
// 重定向到一个适当的页面或返回一个默认响应
return c.SendString("Welcome to WebP Server")
}
if !isImageFile(filename) { if !isImageFile(filename) {
log.Infof("Non-image file requested: %s, redirecting to original URL", filename) log.Infof("Non-image file requested: %s, redirecting to original URL", reqURI)
var redirectURL string var redirectURL string
// 检查是否存在匹配的 IMG_MAP // 检查是否存在匹配的 IMG_MAP
@ -79,17 +92,22 @@ func Convert(c *fiber.Ctx) error {
if proxyMode { if proxyMode {
redirectURL = realRemoteAddr redirectURL = realRemoteAddr
} else { } else {
redirectURL = path.Join(config.Config.ImgPath, reqURI) // 使用完整的请求路径,而不是追加到 ImgPath
redirectURL = path.Join("/", reqURI)
} }
} }
// 移除查询参数 // 确保重定向 URL 不会导致循环
redirectURL = strings.Split(redirectURL, "?")[0] if redirectURL == reqURI || redirectURL == path.Join("/", reqURI) {
return c.SendStatus(fiber.StatusNotFound)
}
log.Infof("Redirecting to: %s", redirectURL) log.Infof("Redirecting to: %s", redirectURL)
return c.Redirect(redirectURL, 302) return c.Redirect(redirectURL, fiber.StatusFound)
} }
//
//
if !helper.CheckAllowedType(filename) { if !helper.CheckAllowedType(filename) {
msg := "File extension not allowed! " + filename msg := "File extension not allowed! " + filename
@ -227,17 +245,11 @@ func isImageFile(filename string) bool {
} }
ext = ext[1:] // 移除开头的点 ext = ext[1:] // 移除开头的点
// 使用 config.go 中定义的默认 AllowedTypes
defaultImageTypes := config.NewWebPConfig().AllowedTypes
// 检查配置中的 ALLOWED_TYPES
allowedTypes := config.Config.AllowedTypes allowedTypes := config.Config.AllowedTypes
if len(allowedTypes) == 1 && allowedTypes[0] == "*" {
// 如果 ALLOWED_TYPES 包含 "*",使用默认列表 // 如果允许所有类型,则使用默认的图片类型列表
if slices.Contains(allowedTypes, "*") { allowedTypes = config.NewWebPConfig().AllowedTypes
allowedTypes = defaultImageTypes
} }
// 检查文件扩展名是否在允许的类型列表中
return slices.Contains(allowedTypes, ext) return slices.Contains(allowedTypes, ext)
} }