尝试修复无限重定向

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

View File

@ -61,35 +61,53 @@ 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)
if !isImageFile(filename) { // 非图片清况下302到源文件
log.Infof("Non-image file requested: %s, redirecting to original URL", filename) //
var redirectURL string //
//
reqURI := c.Path()
filename := path.Base(reqURI)
// 检查是否存在匹配的 IMG_MAP // 处理根路径请求
for prefix, target := range config.Config.ImageMap { if reqURI == "/" {
if strings.HasPrefix(reqURI, prefix) { // 重定向到一个适当的页面或返回一个默认响应
// 构造重定向 URL return c.SendString("Welcome to WebP Server")
redirectURL = target + strings.TrimPrefix(reqURI, prefix) }
break
}
}
// 如果没有找到匹配的 IMG_MAP使用默认的处理方式 if !isImageFile(filename) {
if redirectURL == "" { log.Infof("Non-image file requested: %s, redirecting to original URL", reqURI)
if proxyMode { var redirectURL string
redirectURL = realRemoteAddr
} else {
redirectURL = path.Join(config.Config.ImgPath, reqURI)
}
}
// 移除查询参数 // 检查是否存在匹配的 IMG_MAP
redirectURL = strings.Split(redirectURL, "?")[0] for prefix, target := range config.Config.ImageMap {
if strings.HasPrefix(reqURI, prefix) {
// 构造重定向 URL
redirectURL = target + strings.TrimPrefix(reqURI, prefix)
break
}
}
log.Infof("Redirecting to: %s", redirectURL) // 如果没有找到匹配的 IMG_MAP使用默认的处理方式
return c.Redirect(redirectURL, 302) if redirectURL == "" {
} if proxyMode {
redirectURL = realRemoteAddr
} else {
// 使用完整的请求路径,而不是追加到 ImgPath
redirectURL = path.Join("/", reqURI)
}
}
// 确保重定向 URL 不会导致循环
if redirectURL == reqURI || redirectURL == path.Join("/", reqURI) {
return c.SendStatus(fiber.StatusNotFound)
}
log.Infof("Redirecting to: %s", redirectURL)
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)
} }