增加检查是否为网页类型

This commit is contained in:
starry
2025-09-01 12:05:16 +08:00
committed by GitHub
parent 30bc88ed93
commit 40f5b597ab

View File

@@ -29,6 +29,14 @@ var (
} }
) )
// 全局变量:被阻止的内容类型
var blockedContentTypes = map[string]bool{
"text/html": true,
"application/xhtml+xml": true,
"text/xml": true,
"application/xml": true,
}
// GitHubProxyHandler GitHub代理处理器 // GitHubProxyHandler GitHub代理处理器
func GitHubProxyHandler(c *gin.Context) { func GitHubProxyHandler(c *gin.Context) {
rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/") rawPath := strings.TrimPrefix(c.Request.URL.RequestURI(), "/")
@@ -122,12 +130,23 @@ func proxyGitHubWithRedirect(c *gin.Context, u string, redirectCount int) {
} }
}() }()
// 如果Github上游404则返回错误信息 // 检查并处理被阻止的内容类型
if resp.StatusCode == http.StatusNotFound { if c.Request.Method == "GET" {
c.String(http.StatusForbidden, "无效的GitHub地址") if contentType := resp.Header.Get("Content-Type"); blockedContentTypes[strings.ToLower(strings.Split(contentType, ";")[0])] {
return c.JSON(http.StatusForbidden, map[string]string{
"error": "Content type not allowed",
"message": "检测到网页内容,本服务不支持加速网页,仅支持加速资源下载。",
})
return
}
} }
// 如果Github上游404则返回错误信息。这样处理是否会更好点暂时先用检查内容类型的方式吧防止返回网页内容。欢迎大佬们提供更好的方案。
// if resp.StatusCode == http.StatusNotFound {
// c.String(http.StatusForbidden, "无效的GitHub地址")
// return
// }
// 检查文件大小限制 // 检查文件大小限制
cfg := config.GetConfig() cfg := config.GetConfig()
if contentLength := resp.Header.Get("Content-Length"); contentLength != "" { if contentLength := resp.Header.Get("Content-Length"); contentLength != "" {