mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 08:11:11 +08:00
Compare commits
No commits in common. "5731418822fd37e3e6c81da6992797a2868b790b" and "5dde21a403320031b43cd4b38189542e66ac11f0" have entirely different histories.
5731418822
...
5dde21a403
4 changed files with 98 additions and 129 deletions
|
|
@ -1,9 +1,5 @@
|
|||
# 更新日志
|
||||
|
||||
3.5.1 - 2025-06-09
|
||||
---
|
||||
- CHANGE: 大幅优化`Matcher`的性能, 实现零分配, 大幅提升性能; 单次操作时间: `254.3 ns/op` => `29.59 ns/op`
|
||||
|
||||
25w45a - 2025-06-09
|
||||
---
|
||||
- PRE-RELEASE: 此版本是v3.5.1预发布版本,请勿在生产环境中使用;
|
||||
|
|
|
|||
2
VERSION
2
VERSION
|
|
@ -1 +1 @@
|
|||
3.5.1
|
||||
3.5.0
|
||||
|
|
@ -9,25 +9,6 @@ import (
|
|||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
githubPrefix = "https://github.com/"
|
||||
rawPrefix = "https://raw.githubusercontent.com/"
|
||||
gistPrefix = "https://gist.github.com/"
|
||||
apiPrefix = "https://api.github.com/"
|
||||
githubPrefixLen int
|
||||
rawPrefixLen int
|
||||
gistPrefixLen int
|
||||
apiPrefixLen int
|
||||
)
|
||||
|
||||
func init() {
|
||||
githubPrefixLen = len(githubPrefix)
|
||||
rawPrefixLen = len(rawPrefix)
|
||||
gistPrefixLen = len(gistPrefix)
|
||||
apiPrefixLen = len(apiPrefix)
|
||||
//log.Printf("githubPrefixLen: %d, rawPrefixLen: %d, gistPrefixLen: %d, apiPrefixLen: %d", githubPrefixLen, rawPrefixLen, gistPrefixLen, apiPrefixLen)
|
||||
}
|
||||
|
||||
// Matcher 从原始URL路径中高效地解析并匹配代理规则.
|
||||
func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHProxyErrors) {
|
||||
if len(rawPath) < 18 {
|
||||
|
|
@ -35,8 +16,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
|
|||
}
|
||||
|
||||
// 匹配 "https://github.com/"
|
||||
if strings.HasPrefix(rawPath, githubPrefix) {
|
||||
remaining := rawPath[githubPrefixLen:]
|
||||
if strings.HasPrefix(rawPath, "https://github.com/") {
|
||||
remaining := rawPath[19:]
|
||||
i := strings.IndexByte(remaining, '/')
|
||||
if i <= 0 {
|
||||
return "", "", "", NewErrorWithStatusLookup(400, "malformed github path: missing user")
|
||||
|
|
@ -74,8 +55,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
|
|||
}
|
||||
|
||||
// 匹配 "https://raw.githubusercontent.com/"
|
||||
if strings.HasPrefix(rawPath, rawPrefix) {
|
||||
remaining := rawPath[rawPrefixLen:]
|
||||
if strings.HasPrefix(rawPath, "https://raw.githubusercontent.com/") {
|
||||
remaining := rawPath[34:]
|
||||
// 这里的逻辑与 github.com 的类似, 需要提取 user, repo, branch, file...
|
||||
// 我们只需要 user 和 repo
|
||||
i := strings.IndexByte(remaining, '/')
|
||||
|
|
@ -98,8 +79,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
|
|||
}
|
||||
|
||||
// 匹配 "https://gist.github.com/"
|
||||
if strings.HasPrefix(rawPath, gistPrefix) {
|
||||
remaining := rawPath[gistPrefixLen:]
|
||||
if strings.HasPrefix(rawPath, "https://gist.github.com/") {
|
||||
remaining := rawPath[24:]
|
||||
i := strings.IndexByte(remaining, '/')
|
||||
if i <= 0 {
|
||||
// case: https://gist.github.com/user
|
||||
|
|
@ -115,11 +96,11 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
|
|||
}
|
||||
|
||||
// 匹配 "https://api.github.com/"
|
||||
if strings.HasPrefix(rawPath, apiPrefix) {
|
||||
if strings.HasPrefix(rawPath, "https://api.github.com/") {
|
||||
if !cfg.Auth.ForceAllowApi && (cfg.Auth.Method != "header" || !cfg.Auth.Enabled) {
|
||||
return "", "", "", NewErrorWithStatusLookup(403, "API proxy requires header authentication")
|
||||
}
|
||||
remaining := rawPath[apiPrefixLen:]
|
||||
remaining := rawPath[23:]
|
||||
var user, repo string
|
||||
if strings.HasPrefix(remaining, "repos/") {
|
||||
parts := strings.SplitN(remaining[6:], "/", 3)
|
||||
|
|
@ -139,9 +120,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
|
|||
return "", "", "", NewErrorWithStatusLookup(404, "no matcher found for the given path")
|
||||
}
|
||||
|
||||
// 原实现
|
||||
/*
|
||||
func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHProxyErrors) {
|
||||
func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHProxyErrors) {
|
||||
var (
|
||||
user string
|
||||
repo string
|
||||
|
|
@ -235,9 +215,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
|
|||
//return "", "", "", ErrNotFound
|
||||
errMsg := "Didn't match any matcher"
|
||||
return "", "", "", NewErrorWithStatusLookup(404, errMsg)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
var (
|
||||
proxyableMatchersMap map[string]struct{}
|
||||
initMatchersOnce sync.Once
|
||||
|
|
|
|||
|
|
@ -68,12 +68,6 @@ func TestMatcher_Compatibility(t *testing.T) {
|
|||
config: cfgWithAuth,
|
||||
expectedUser: "owner", expectedRepo: "repo", expectedMatcher: "clone",
|
||||
},
|
||||
{
|
||||
name: "Girhub Broken Path",
|
||||
rawPath: "https://github.com/owner",
|
||||
config: cfgWithAuth,
|
||||
expectError: true, expectedErrCode: 400,
|
||||
},
|
||||
|
||||
{
|
||||
name: "RawGHUserContent Path",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue