|
8 | 8 | "net/http" |
9 | 9 | "os" |
10 | 10 | "runtime/debug" |
| 11 | + "strings" |
11 | 12 | "time" |
12 | 13 |
|
13 | 14 | "ghproxy/api" |
@@ -394,14 +395,40 @@ func main() { |
394 | 395 | setupPages(cfg, r) |
395 | 396 | r.SetRedirectTrailingSlash(false) |
396 | 397 |
|
397 | | - r.GET("/github.com/:user/:repo/releases/download/*filepath", func(c *touka.Context) { |
398 | | - c.Set("matcher", "releases") |
399 | | - proxy.RoutingHandler(cfg)(c) |
400 | | - }) |
| 398 | + r.GET("/github.com/:user/:repo/releases/*filepath", func(c *touka.Context) { |
| 399 | + // 规范化路径: 移除前导斜杠, 简化后续处理 |
| 400 | + filepath := c.Param("filepath") |
| 401 | + if len(filepath) > 0 && filepath[0] == '/' { |
| 402 | + filepath = filepath[1:] |
| 403 | + } |
401 | 404 |
|
402 | | - r.GET("/github.com/:user/:repo/releases/:tag/download/*filepath", func(c *touka.Context) { |
403 | | - c.Set("matcher", "releases") |
404 | | - proxy.RoutingHandler(cfg)(c) |
| 405 | + isValidDownload := false |
| 406 | + |
| 407 | + // 检查两种合法的下载链接格式 |
| 408 | + // 情况 A: "download/..." |
| 409 | + if strings.HasPrefix(filepath, "download/") { |
| 410 | + isValidDownload = true |
| 411 | + } else { |
| 412 | + // 情况 B: ":tag/download/..." |
| 413 | + slashIndex := strings.IndexByte(filepath, '/') |
| 414 | + // 确保 tag 部分存在 (slashIndex > 0) |
| 415 | + if slashIndex > 0 { |
| 416 | + pathAfterTag := filepath[slashIndex+1:] |
| 417 | + if strings.HasPrefix(pathAfterTag, "download/") { |
| 418 | + isValidDownload = true |
| 419 | + } |
| 420 | + } |
| 421 | + } |
| 422 | + |
| 423 | + // 根据匹配结果执行最终操作 |
| 424 | + if isValidDownload { |
| 425 | + c.Set("matcher", "releases") |
| 426 | + proxy.RoutingHandler(cfg)(c) |
| 427 | + } else { |
| 428 | + // 任何不符合下载链接格式的 'releases' 路径都被视为浏览页面并拒绝 |
| 429 | + proxy.ErrorPage(c, proxy.NewErrorWithStatusLookup(400, "unsupported releases page, only download links are allowed")) |
| 430 | + return |
| 431 | + } |
405 | 432 | }) |
406 | 433 |
|
407 | 434 | r.GET("/github.com/:user/:repo/archive/*filepath", func(c *touka.Context) { |
|
0 commit comments