diff --git a/data/config.json b/data/config.json index a98a944..03920f7 100644 --- a/data/config.json +++ b/data/config.json @@ -12,5 +12,17 @@ "Enabled": false, "Level": 4 } - } + }, + "FixedPaths": [ + { + "Path": "/cdnjs", + "TargetHost": "cdnjs.cloudflare.com", + "TargetURL": "https://cdnjs.cloudflare.com" + }, + { + "Path": "/jsdelivr", + "TargetHost": "cdn.jsdelivr.net", + "TargetURL": "https://cdn.jsdelivr.net" + } + ] } \ No newline at end of file diff --git a/internal/config/types.go b/internal/config/types.go index e8ad6f4..9f68271 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -3,6 +3,7 @@ package config type Config struct { MAP map[string]string `json:"MAP"` Compression CompressionConfig `json:"Compression"` + FixedPaths []FixedPathConfig `json:"FixedPaths"` // 新增 } type CompressionConfig struct { @@ -14,3 +15,9 @@ type CompressorConfig struct { Enabled bool `json:"Enabled"` Level int `json:"Level"` } + +type FixedPathConfig struct { + Path string `json:"Path"` + TargetHost string `json:"TargetHost"` + TargetURL string `json:"TargetURL"` +} diff --git a/internal/middleware/cdnjs.go b/internal/middleware/fixed_path_proxy.go similarity index 86% rename from internal/middleware/cdnjs.go rename to internal/middleware/fixed_path_proxy.go index 9e5f241..9767c0b 100644 --- a/internal/middleware/cdnjs.go +++ b/internal/middleware/fixed_path_proxy.go @@ -5,17 +5,17 @@ import ( "log" "net" "net/http" + "proxy-go/internal/config" "strings" ) -type CDNJSConfig struct { - Path string // 固定路径,例如 "/cdnjs" - TargetHost string // 目标主机,例如 "cdnjs.cloudflare.com" - TargetURL string // 目标URL,例如 "https://cdnjs.cloudflare.com" +type FixedPathConfig struct { + Path string `json:"Path"` + TargetHost string `json:"TargetHost"` + TargetURL string `json:"TargetURL"` } -// CDNJSMiddleware 处理固定路径的代理 -func CDNJSMiddleware(configs []CDNJSConfig) func(http.Handler) http.Handler { +func FixedPathProxyMiddleware(configs []config.FixedPathConfig) func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // 检查是否匹配任何固定路径 diff --git a/main.go b/main.go index 04e1f13..3d4075d 100644 --- a/main.go +++ b/main.go @@ -29,37 +29,40 @@ func main() { // 创建代理处理器 proxyHandler := handler.NewProxyHandler(cfg.MAP) - // 创建 CDNJS 中间件配置 - cdnjsConfigs := []middleware.CDNJSConfig{ + // 创建处理器链 + handlers := []struct { + pathPrefix string + handler http.Handler + }{ + // 固定路径处理器 { - Path: "/cdnjs", - TargetHost: "cdnjs.cloudflare.com", - TargetURL: "https://cdnjs.cloudflare.com", + pathPrefix: "", // 空字符串表示检查所有 FixedPaths 配置 + handler: middleware.FixedPathProxyMiddleware(cfg.FixedPaths)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})), }, + // 可以在这里添加其他固定路径处理器 + // { + // pathPrefix: "/something", + // handler: someOtherMiddleware(config)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})), + // }, + // 默认代理处理器放在最后 { - Path: "/jsdelivr", - TargetHost: "cdn.jsdelivr.net", - TargetURL: "https://cdn.jsdelivr.net", + pathPrefix: "", + handler: proxyHandler, }, } // 创建主处理器 mainHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // 检查是否匹配任何固定路径配置 - for _, cfg := range cdnjsConfigs { - if strings.HasPrefix(r.URL.Path, cfg.Path) { - // 使用 CDNJS 中间件处理 - handler := middleware.CDNJSMiddleware(cdnjsConfigs)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) - handler.ServeHTTP(w, r) + // 遍历所有处理器 + for _, h := range handlers { + if h.pathPrefix == "" || strings.HasPrefix(r.URL.Path, h.pathPrefix) { + h.handler.ServeHTTP(w, r) return } } - - // 如果没有匹配的固定路径,使用普通代理处理器 - proxyHandler.ServeHTTP(w, r) }) - // 对非 CDNJS 请求添加压缩中间件 + // 添加压缩中间件 var handler http.Handler = mainHandler if cfg.Compression.Gzip.Enabled || cfg.Compression.Brotli.Enabled { handler = middleware.CompressionMiddleware(compManager)(handler)