From 4156b64ac6f6a7296843e78fe8e984e557951a2d Mon Sep 17 00:00:00 2001 From: wood chen Date: Thu, 1 May 2025 08:42:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=85=8D=E7=BD=AE=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A1=AE=E4=BF=9D=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E9=85=8D=E7=BD=AE=E7=9A=84=E6=89=A9=E5=B1=95=E5=90=8D?= =?UTF-8?q?=E8=A7=84=E5=88=99=E5=9C=A8=E6=9B=B4=E6=96=B0=E6=97=B6=E5=BE=97?= =?UTF-8?q?=E5=88=B0=E6=AD=A3=E7=A1=AE=E5=A4=84=E7=90=86=EF=BC=8C=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=86=97=E4=BD=99=E4=BB=A3=E7=A0=81=E4=BB=A5=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E5=9B=9E=E8=B0=83=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 - data/config.example.json | 61 +++++++++++++++++++++++++++++++++++++++ internal/config/config.go | 20 ++++++++----- internal/handler/proxy.go | 6 +--- 4 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 data/config.example.json diff --git a/.gitignore b/.gitignore index 9033da3..914d6e6 100644 --- a/.gitignore +++ b/.gitignore @@ -33,7 +33,6 @@ data/metrics/metrics.json data/metrics/path_stats.json data/metrics/referer_stats.json data/metrics/status_codes.json -data/config.example.json data/config.json .env data/cache diff --git a/data/config.example.json b/data/config.example.json new file mode 100644 index 0000000..e780c8e --- /dev/null +++ b/data/config.example.json @@ -0,0 +1,61 @@ +{ + "MAP": { + "/path1": { + "DefaultTarget": "https://path1.com/path/path/path", + "ExtensionMap": [ + { + "Extensions": "jpg,png,avif", + "Target": "https://path1-img.com/path/path/path", + "SizeThreshold": 204800, + "MaxSize": 5242880 + }, + { + "Extensions": "mp4,webm", + "Target": "https://path1-video.com/path/path/path", + "SizeThreshold": 204800, + "MaxSize": 5242880 + }, + { + "Extensions": "*", + "Target": "https://path1-wildcard.com/path/path/path", + "SizeThreshold": 204800, + "MaxSize": 5242880 + } + ] + }, + "/path2": "https://path2.com", + "/path3": { + "DefaultTarget": "https://path3.com", + "ExtensionMap": [ + { + "Extensions": "*", + "Target": "https://path3-wildcard.com", + "SizeThreshold": 512000, + "MaxSize": 10485760 + } + ], + "SizeThreshold": 512000 + }, + "/wildcard-no-limits": { + "DefaultTarget": "https://default.example.com", + "ExtensionMap": [ + { + "Extensions": "*", + "Target": "https://unlimited.example.com", + "SizeThreshold": 0, + "MaxSize": 0 + } + ] + } + }, + "Compression": { + "Gzip": { + "Enabled": false, + "Level": 6 + }, + "Brotli": { + "Enabled": false, + "Level": 4 + } + } +} \ No newline at end of file diff --git a/internal/config/config.go b/internal/config/config.go index ac0fecf..5057d4c 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -41,8 +41,9 @@ func NewConfigManager(configPath string) (*ConfigManager, error) { } // 确保所有路径配置的扩展名规则都已更新 - for _, pathConfig := range config.MAP { - pathConfig.ProcessExtensionMap() + for path, pc := range config.MAP { + pc.ProcessExtensionMap() + config.MAP[path] = pc // 更新回原始map } cm.config.Store(config) @@ -131,8 +132,9 @@ func RegisterUpdateCallback(callback func(*Config)) { // TriggerCallbacks 触发所有回调 func TriggerCallbacks(cfg *Config) { // 确保所有路径配置的扩展名规则都已更新 - for _, pathConfig := range cfg.MAP { - pathConfig.ProcessExtensionMap() + for path, pc := range cfg.MAP { + pc.ProcessExtensionMap() + cfg.MAP[path] = pc // 更新回原始map } callbackMutex.RLock() @@ -151,8 +153,9 @@ func (c *configImpl) Update(newConfig *Config) { defer c.Unlock() // 确保所有路径配置的扩展名规则都已更新 - for _, pathConfig := range newConfig.MAP { - pathConfig.ProcessExtensionMap() + for path, pc := range newConfig.MAP { + pc.ProcessExtensionMap() + newConfig.MAP[path] = pc // 更新回原始map } // 更新配置 @@ -191,8 +194,9 @@ func (cm *ConfigManager) loadConfig() error { } // 确保所有路径配置的扩展名规则都已更新 - for _, pathConfig := range config.MAP { - pathConfig.ProcessExtensionMap() + for path, pc := range config.MAP { + pc.ProcessExtensionMap() + config.MAP[path] = pc // 更新回原始map } cm.config.Store(config) diff --git a/internal/handler/proxy.go b/internal/handler/proxy.go index 3573235..6e468ca 100644 --- a/internal/handler/proxy.go +++ b/internal/handler/proxy.go @@ -179,11 +179,7 @@ func NewProxyHandler(cfg *config.Config) *ProxyHandler { // 注册配置更新回调 config.RegisterUpdateCallback(func(newCfg *config.Config) { - // 确保所有路径配置的processedExtMap都已更新 - for _, pathConfig := range newCfg.MAP { - pathConfig.ProcessExtensionMap() - } - + // 注意:config包已经在回调触发前处理了所有ExtRules,这里无需再次处理 handler.pathMap = newCfg.MAP handler.prefixTree.update(newCfg.MAP) // 更新前缀匹配树 handler.config = newCfg