From 4fa91b67cffc81bf798a0b802d4c8a71e6624c84 Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Tue, 25 Mar 2025 20:22:23 +0800 Subject: [PATCH] update match --- config/config.go | 4 +++- config/config.toml | 1 + proxy/match.go | 60 ++++++++++++++++++++++++---------------------- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/config/config.go b/config/config.go index a338ec8..58540fa 100644 --- a/config/config.go +++ b/config/config.go @@ -64,9 +64,11 @@ type GitCloneConfig struct { /* [shell] editor = true +rewriteAPI = false */ type ShellConfig struct { - Editor bool `toml:"editor"` + Editor bool `toml:"editor"` + RewriteAPI bool `toml:"rewriteAPI"` } /* diff --git a/config/config.toml b/config/config.toml index 64ad1da..9ae7000 100644 --- a/config/config.toml +++ b/config/config.toml @@ -19,6 +19,7 @@ ForceH2C = false [shell] editor = false +rewriteAPI = false [pages] mode = "internal" # "internal" or "external" diff --git a/proxy/match.go b/proxy/match.go index 37f4aa4..0b3e946 100644 --- a/proxy/match.go +++ b/proxy/match.go @@ -46,6 +46,19 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, error) repo string matcher string ) + // 匹配 "https://raw"开头的链接 + if strings.HasPrefix(rawPath, "https://raw") { + remainingPath := strings.TrimPrefix(rawPath, "https://") + parts := strings.Split(remainingPath, "/") + if len(parts) <= 3 { + return "", "", "", ErrInvalidURL + } + user = parts[1] + repo = parts[2] + matcher = "raw" + + return user, repo, matcher, nil + } // 匹配 "https://github.com"开头的链接 if strings.HasPrefix(rawPath, "https://github.com") { remainingPath := strings.TrimPrefix(rawPath, "https://github.com") @@ -75,19 +88,6 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, error) } return user, repo, matcher, nil } - // 匹配 "https://raw"开头的链接 - if strings.HasPrefix(rawPath, "https://raw") { - remainingPath := strings.TrimPrefix(rawPath, "https://") - parts := strings.Split(remainingPath, "/") - if len(parts) <= 3 { - return "", "", "", ErrInvalidURL - } - 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://") @@ -100,25 +100,27 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, error) 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/") + if cfg.Shell.RewriteAPI { + // 匹配 "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.AuthMethod != "header" || !cfg.Auth.Enabled { - return "", "", "", ErrAuthHeaderUnavailable + 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.AuthMethod != "header" || !cfg.Auth.Enabled { + return "", "", "", ErrAuthHeaderUnavailable + } + } + return user, repo, matcher, nil } - return user, repo, matcher, nil } return "", "", "", ErrInvalidURL }