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
|
25w45a - 2025-06-09
|
||||||
---
|
---
|
||||||
- PRE-RELEASE: 此版本是v3.5.1预发布版本,请勿在生产环境中使用;
|
- PRE-RELEASE: 此版本是v3.5.1预发布版本,请勿在生产环境中使用;
|
||||||
|
|
|
||||||
2
VERSION
2
VERSION
|
|
@ -1 +1 @@
|
||||||
3.5.1
|
3.5.0
|
||||||
215
proxy/match.go
215
proxy/match.go
|
|
@ -9,25 +9,6 @@ import (
|
||||||
"sync"
|
"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路径中高效地解析并匹配代理规则.
|
// Matcher 从原始URL路径中高效地解析并匹配代理规则.
|
||||||
func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHProxyErrors) {
|
func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHProxyErrors) {
|
||||||
if len(rawPath) < 18 {
|
if len(rawPath) < 18 {
|
||||||
|
|
@ -35,8 +16,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
|
||||||
}
|
}
|
||||||
|
|
||||||
// 匹配 "https://github.com/"
|
// 匹配 "https://github.com/"
|
||||||
if strings.HasPrefix(rawPath, githubPrefix) {
|
if strings.HasPrefix(rawPath, "https://github.com/") {
|
||||||
remaining := rawPath[githubPrefixLen:]
|
remaining := rawPath[19:]
|
||||||
i := strings.IndexByte(remaining, '/')
|
i := strings.IndexByte(remaining, '/')
|
||||||
if i <= 0 {
|
if i <= 0 {
|
||||||
return "", "", "", NewErrorWithStatusLookup(400, "malformed github path: missing user")
|
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/"
|
// 匹配 "https://raw.githubusercontent.com/"
|
||||||
if strings.HasPrefix(rawPath, rawPrefix) {
|
if strings.HasPrefix(rawPath, "https://raw.githubusercontent.com/") {
|
||||||
remaining := rawPath[rawPrefixLen:]
|
remaining := rawPath[34:]
|
||||||
// 这里的逻辑与 github.com 的类似, 需要提取 user, repo, branch, file...
|
// 这里的逻辑与 github.com 的类似, 需要提取 user, repo, branch, file...
|
||||||
// 我们只需要 user 和 repo
|
// 我们只需要 user 和 repo
|
||||||
i := strings.IndexByte(remaining, '/')
|
i := strings.IndexByte(remaining, '/')
|
||||||
|
|
@ -98,8 +79,8 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
|
||||||
}
|
}
|
||||||
|
|
||||||
// 匹配 "https://gist.github.com/"
|
// 匹配 "https://gist.github.com/"
|
||||||
if strings.HasPrefix(rawPath, gistPrefix) {
|
if strings.HasPrefix(rawPath, "https://gist.github.com/") {
|
||||||
remaining := rawPath[gistPrefixLen:]
|
remaining := rawPath[24:]
|
||||||
i := strings.IndexByte(remaining, '/')
|
i := strings.IndexByte(remaining, '/')
|
||||||
if i <= 0 {
|
if i <= 0 {
|
||||||
// case: https://gist.github.com/user
|
// 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/"
|
// 匹配 "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) {
|
if !cfg.Auth.ForceAllowApi && (cfg.Auth.Method != "header" || !cfg.Auth.Enabled) {
|
||||||
return "", "", "", NewErrorWithStatusLookup(403, "API proxy requires header authentication")
|
return "", "", "", NewErrorWithStatusLookup(403, "API proxy requires header authentication")
|
||||||
}
|
}
|
||||||
remaining := rawPath[apiPrefixLen:]
|
remaining := rawPath[23:]
|
||||||
var user, repo string
|
var user, repo string
|
||||||
if strings.HasPrefix(remaining, "repos/") {
|
if strings.HasPrefix(remaining, "repos/") {
|
||||||
parts := strings.SplitN(remaining[6:], "/", 3)
|
parts := strings.SplitN(remaining[6:], "/", 3)
|
||||||
|
|
@ -139,105 +120,103 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
|
||||||
return "", "", "", NewErrorWithStatusLookup(404, "no matcher found for the given path")
|
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 (
|
var (
|
||||||
user string
|
user string
|
||||||
repo string
|
repo string
|
||||||
matcher string
|
matcher string
|
||||||
)
|
)
|
||||||
// 匹配 "https://github.com"开头的链接
|
|
||||||
if strings.HasPrefix(rawPath, "https://github.com") {
|
|
||||||
remainingPath := strings.TrimPrefix(rawPath, "https://github.com")
|
|
||||||
|
|
||||||
//if strings.HasPrefix(remainingPath, "/") {
|
|
||||||
// remainingPath = strings.TrimPrefix(remainingPath, "/")
|
|
||||||
//}
|
|
||||||
|
|
||||||
remainingPath = strings.TrimPrefix(remainingPath, "/")
|
|
||||||
// 预期格式/user/repo/more...
|
|
||||||
// 取出user和repo和最后部分
|
|
||||||
parts := strings.Split(remainingPath, "/")
|
|
||||||
if len(parts) <= 2 {
|
|
||||||
errMsg := "Not enough parts in path after matching 'https://github.com*'"
|
|
||||||
return "", "", "", NewErrorWithStatusLookup(400, errMsg)
|
|
||||||
}
|
|
||||||
user = parts[0]
|
|
||||||
repo = parts[1]
|
|
||||||
// 匹配 "https://github.com"开头的链接
|
// 匹配 "https://github.com"开头的链接
|
||||||
if len(parts) >= 3 {
|
if strings.HasPrefix(rawPath, "https://github.com") {
|
||||||
switch parts[2] {
|
remainingPath := strings.TrimPrefix(rawPath, "https://github.com")
|
||||||
case "releases", "archive":
|
|
||||||
matcher = "releases"
|
//if strings.HasPrefix(remainingPath, "/") {
|
||||||
case "blob":
|
// remainingPath = strings.TrimPrefix(remainingPath, "/")
|
||||||
matcher = "blob"
|
//}
|
||||||
case "raw":
|
|
||||||
matcher = "raw"
|
remainingPath = strings.TrimPrefix(remainingPath, "/")
|
||||||
case "info", "git-upload-pack":
|
// 预期格式/user/repo/more...
|
||||||
matcher = "clone"
|
// 取出user和repo和最后部分
|
||||||
default:
|
parts := strings.Split(remainingPath, "/")
|
||||||
errMsg := "Url Matched 'https://github.com*', but didn't match the next matcher"
|
if len(parts) <= 2 {
|
||||||
|
errMsg := "Not enough parts in path after matching 'https://github.com*'"
|
||||||
return "", "", "", NewErrorWithStatusLookup(400, errMsg)
|
return "", "", "", NewErrorWithStatusLookup(400, errMsg)
|
||||||
}
|
}
|
||||||
|
user = parts[0]
|
||||||
|
repo = parts[1]
|
||||||
|
// 匹配 "https://github.com"开头的链接
|
||||||
|
if len(parts) >= 3 {
|
||||||
|
switch parts[2] {
|
||||||
|
case "releases", "archive":
|
||||||
|
matcher = "releases"
|
||||||
|
case "blob":
|
||||||
|
matcher = "blob"
|
||||||
|
case "raw":
|
||||||
|
matcher = "raw"
|
||||||
|
case "info", "git-upload-pack":
|
||||||
|
matcher = "clone"
|
||||||
|
default:
|
||||||
|
errMsg := "Url Matched 'https://github.com*', but didn't match the next matcher"
|
||||||
|
return "", "", "", NewErrorWithStatusLookup(400, errMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return user, repo, matcher, nil
|
||||||
}
|
}
|
||||||
return user, repo, matcher, nil
|
// 匹配 "https://raw"开头的链接
|
||||||
}
|
if strings.HasPrefix(rawPath, "https://raw") {
|
||||||
// 匹配 "https://raw"开头的链接
|
remainingPath := strings.TrimPrefix(rawPath, "https://")
|
||||||
if strings.HasPrefix(rawPath, "https://raw") {
|
parts := strings.Split(remainingPath, "/")
|
||||||
remainingPath := strings.TrimPrefix(rawPath, "https://")
|
if len(parts) <= 3 {
|
||||||
parts := strings.Split(remainingPath, "/")
|
errMsg := "URL after matched 'https://raw*' should have at least 4 parts (user/repo/branch/file)."
|
||||||
if len(parts) <= 3 {
|
return "", "", "", NewErrorWithStatusLookup(400, errMsg)
|
||||||
errMsg := "URL after matched 'https://raw*' should have at least 4 parts (user/repo/branch/file)."
|
}
|
||||||
return "", "", "", NewErrorWithStatusLookup(400, errMsg)
|
|
||||||
}
|
|
||||||
user = parts[1]
|
|
||||||
repo = parts[2]
|
|
||||||
matcher = "raw"
|
|
||||||
|
|
||||||
return user, repo, matcher, nil
|
|
||||||
}
|
|
||||||
// 匹配 "https://gist"开头的链接
|
|
||||||
if strings.HasPrefix(rawPath, "https://gist") {
|
|
||||||
remainingPath := strings.TrimPrefix(rawPath, "https://")
|
|
||||||
parts := strings.Split(remainingPath, "/")
|
|
||||||
if len(parts) <= 3 {
|
|
||||||
errMsg := "URL after matched 'https://gist*' should have at least 4 parts (user/gist_id)."
|
|
||||||
return "", "", "", NewErrorWithStatusLookup(400, errMsg)
|
|
||||||
}
|
|
||||||
user = parts[1]
|
|
||||||
repo = ""
|
|
||||||
matcher = "gist"
|
|
||||||
return user, repo, matcher, nil
|
|
||||||
}
|
|
||||||
// 匹配 "https://api.github.com/"开头的链接
|
|
||||||
if strings.HasPrefix(rawPath, "https://api.github.com/") {
|
|
||||||
matcher = "api"
|
|
||||||
remainingPath := strings.TrimPrefix(rawPath, "https://api.github.com/")
|
|
||||||
|
|
||||||
parts := strings.Split(remainingPath, "/")
|
|
||||||
if parts[0] == "repos" {
|
|
||||||
user = parts[1]
|
user = parts[1]
|
||||||
repo = parts[2]
|
repo = parts[2]
|
||||||
}
|
matcher = "raw"
|
||||||
if parts[0] == "users" {
|
|
||||||
user = parts[1]
|
|
||||||
}
|
|
||||||
if !cfg.Auth.ForceAllowApi {
|
|
||||||
if cfg.Auth.Method != "header" || !cfg.Auth.Enabled {
|
|
||||||
//return "", "", "", ErrAuthHeaderUnavailable
|
|
||||||
errMsg := "AuthHeader Unavailable, Need to open header auth to enable api proxy"
|
|
||||||
return "", "", "", NewErrorWithStatusLookup(403, errMsg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return user, repo, matcher, nil
|
|
||||||
}
|
|
||||||
//return "", "", "", ErrNotFound
|
|
||||||
errMsg := "Didn't match any matcher"
|
|
||||||
return "", "", "", NewErrorWithStatusLookup(404, errMsg)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
return user, repo, matcher, nil
|
||||||
|
}
|
||||||
|
// 匹配 "https://gist"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://gist") {
|
||||||
|
remainingPath := strings.TrimPrefix(rawPath, "https://")
|
||||||
|
parts := strings.Split(remainingPath, "/")
|
||||||
|
if len(parts) <= 3 {
|
||||||
|
errMsg := "URL after matched 'https://gist*' should have at least 4 parts (user/gist_id)."
|
||||||
|
return "", "", "", NewErrorWithStatusLookup(400, errMsg)
|
||||||
|
}
|
||||||
|
user = parts[1]
|
||||||
|
repo = ""
|
||||||
|
matcher = "gist"
|
||||||
|
return user, repo, matcher, nil
|
||||||
|
}
|
||||||
|
// 匹配 "https://api.github.com/"开头的链接
|
||||||
|
if strings.HasPrefix(rawPath, "https://api.github.com/") {
|
||||||
|
matcher = "api"
|
||||||
|
remainingPath := strings.TrimPrefix(rawPath, "https://api.github.com/")
|
||||||
|
|
||||||
|
parts := strings.Split(remainingPath, "/")
|
||||||
|
if parts[0] == "repos" {
|
||||||
|
user = parts[1]
|
||||||
|
repo = parts[2]
|
||||||
|
}
|
||||||
|
if parts[0] == "users" {
|
||||||
|
user = parts[1]
|
||||||
|
}
|
||||||
|
if !cfg.Auth.ForceAllowApi {
|
||||||
|
if cfg.Auth.Method != "header" || !cfg.Auth.Enabled {
|
||||||
|
//return "", "", "", ErrAuthHeaderUnavailable
|
||||||
|
errMsg := "AuthHeader Unavailable, Need to open header auth to enable api proxy"
|
||||||
|
return "", "", "", NewErrorWithStatusLookup(403, errMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return user, repo, matcher, nil
|
||||||
|
}
|
||||||
|
//return "", "", "", ErrNotFound
|
||||||
|
errMsg := "Didn't match any matcher"
|
||||||
|
return "", "", "", NewErrorWithStatusLookup(404, errMsg)
|
||||||
|
}
|
||||||
|
*/
|
||||||
var (
|
var (
|
||||||
proxyableMatchersMap map[string]struct{}
|
proxyableMatchersMap map[string]struct{}
|
||||||
initMatchersOnce sync.Once
|
initMatchersOnce sync.Once
|
||||||
|
|
|
||||||
|
|
@ -68,12 +68,6 @@ func TestMatcher_Compatibility(t *testing.T) {
|
||||||
config: cfgWithAuth,
|
config: cfgWithAuth,
|
||||||
expectedUser: "owner", expectedRepo: "repo", expectedMatcher: "clone",
|
expectedUser: "owner", expectedRepo: "repo", expectedMatcher: "clone",
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "Girhub Broken Path",
|
|
||||||
rawPath: "https://github.com/owner",
|
|
||||||
config: cfgWithAuth,
|
|
||||||
expectError: true, expectedErrCode: 400,
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
{
|
||||||
name: "RawGHUserContent Path",
|
name: "RawGHUserContent Path",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue