mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 16:41:54 +08:00
feat(config, middleware): add fixed paths configuration and proxy middleware
This commit is contained in:
parent
830eb864ef
commit
8f2c035e93
@ -12,5 +12,17 @@
|
|||||||
"Enabled": false,
|
"Enabled": false,
|
||||||
"Level": 4
|
"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"
|
||||||
}
|
}
|
||||||
|
]
|
||||||
}
|
}
|
@ -3,6 +3,7 @@ package config
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
MAP map[string]string `json:"MAP"`
|
MAP map[string]string `json:"MAP"`
|
||||||
Compression CompressionConfig `json:"Compression"`
|
Compression CompressionConfig `json:"Compression"`
|
||||||
|
FixedPaths []FixedPathConfig `json:"FixedPaths"` // 新增
|
||||||
}
|
}
|
||||||
|
|
||||||
type CompressionConfig struct {
|
type CompressionConfig struct {
|
||||||
@ -14,3 +15,9 @@ type CompressorConfig struct {
|
|||||||
Enabled bool `json:"Enabled"`
|
Enabled bool `json:"Enabled"`
|
||||||
Level int `json:"Level"`
|
Level int `json:"Level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FixedPathConfig struct {
|
||||||
|
Path string `json:"Path"`
|
||||||
|
TargetHost string `json:"TargetHost"`
|
||||||
|
TargetURL string `json:"TargetURL"`
|
||||||
|
}
|
||||||
|
@ -5,17 +5,17 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"proxy-go/internal/config"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CDNJSConfig struct {
|
type FixedPathConfig struct {
|
||||||
Path string // 固定路径,例如 "/cdnjs"
|
Path string `json:"Path"`
|
||||||
TargetHost string // 目标主机,例如 "cdnjs.cloudflare.com"
|
TargetHost string `json:"TargetHost"`
|
||||||
TargetURL string // 目标URL,例如 "https://cdnjs.cloudflare.com"
|
TargetURL string `json:"TargetURL"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CDNJSMiddleware 处理固定路径的代理
|
func FixedPathProxyMiddleware(configs []config.FixedPathConfig) func(http.Handler) http.Handler {
|
||||||
func CDNJSMiddleware(configs []CDNJSConfig) func(http.Handler) http.Handler {
|
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// 检查是否匹配任何固定路径
|
// 检查是否匹配任何固定路径
|
39
main.go
39
main.go
@ -29,37 +29,40 @@ func main() {
|
|||||||
// 创建代理处理器
|
// 创建代理处理器
|
||||||
proxyHandler := handler.NewProxyHandler(cfg.MAP)
|
proxyHandler := handler.NewProxyHandler(cfg.MAP)
|
||||||
|
|
||||||
// 创建 CDNJS 中间件配置
|
// 创建处理器链
|
||||||
cdnjsConfigs := []middleware.CDNJSConfig{
|
handlers := []struct {
|
||||||
|
pathPrefix string
|
||||||
|
handler http.Handler
|
||||||
|
}{
|
||||||
|
// 固定路径处理器
|
||||||
{
|
{
|
||||||
Path: "/cdnjs",
|
pathPrefix: "", // 空字符串表示检查所有 FixedPaths 配置
|
||||||
TargetHost: "cdnjs.cloudflare.com",
|
handler: middleware.FixedPathProxyMiddleware(cfg.FixedPaths)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})),
|
||||||
TargetURL: "https://cdnjs.cloudflare.com",
|
|
||||||
},
|
},
|
||||||
|
// 可以在这里添加其他固定路径处理器
|
||||||
|
// {
|
||||||
|
// pathPrefix: "/something",
|
||||||
|
// handler: someOtherMiddleware(config)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})),
|
||||||
|
// },
|
||||||
|
// 默认代理处理器放在最后
|
||||||
{
|
{
|
||||||
Path: "/jsdelivr",
|
pathPrefix: "",
|
||||||
TargetHost: "cdn.jsdelivr.net",
|
handler: proxyHandler,
|
||||||
TargetURL: "https://cdn.jsdelivr.net",
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建主处理器
|
// 创建主处理器
|
||||||
mainHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
mainHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// 检查是否匹配任何固定路径配置
|
// 遍历所有处理器
|
||||||
for _, cfg := range cdnjsConfigs {
|
for _, h := range handlers {
|
||||||
if strings.HasPrefix(r.URL.Path, cfg.Path) {
|
if h.pathPrefix == "" || strings.HasPrefix(r.URL.Path, h.pathPrefix) {
|
||||||
// 使用 CDNJS 中间件处理
|
h.handler.ServeHTTP(w, r)
|
||||||
handler := middleware.CDNJSMiddleware(cdnjsConfigs)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
|
||||||
handler.ServeHTTP(w, r)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果没有匹配的固定路径,使用普通代理处理器
|
|
||||||
proxyHandler.ServeHTTP(w, r)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// 对非 CDNJS 请求添加压缩中间件
|
// 添加压缩中间件
|
||||||
var handler http.Handler = mainHandler
|
var handler http.Handler = mainHandler
|
||||||
if cfg.Compression.Gzip.Enabled || cfg.Compression.Brotli.Enabled {
|
if cfg.Compression.Gzip.Enabled || cfg.Compression.Brotli.Enabled {
|
||||||
handler = middleware.CompressionMiddleware(compManager)(handler)
|
handler = middleware.CompressionMiddleware(compManager)(handler)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user