From a1416722439700299d13b03dda73046588f6b29c Mon Sep 17 00:00:00 2001 From: wood chen Date: Sat, 22 Mar 2025 19:35:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=86=8D=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E5=92=8C?= =?UTF-8?q?=E4=BF=A1=E5=8F=B7=E5=A4=84=E7=90=86=E6=A8=A1=E5=9D=97=EF=BC=8C?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=89=A9=E5=B1=95=E5=90=8D=E8=A7=84=E5=88=99?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E9=98=88=E5=80=BC=E6=B3=A8=E9=87=8A=E4=BB=A5?= =?UTF-8?q?=E7=AE=80=E5=8C=96=E7=90=86=E8=A7=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/config.json | 27 -------------------------- internal/config/types.go | 8 ++++---- internal/utils/signal.go | 32 ------------------------------- internal/utils/utils.go | 16 ++++++++++------ web/app/dashboard/config/page.tsx | 10 +++++----- 5 files changed, 19 insertions(+), 74 deletions(-) delete mode 100644 data/config.json delete mode 100644 internal/utils/signal.go diff --git a/data/config.json b/data/config.json deleted file mode 100644 index e2e6be0..0000000 --- a/data/config.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "MAP": { - "/path1": { - "DefaultTarget": "https://path1.com/path/path/path", - "ExtensionMap": { - "jpg,png,avif": "https://path1-img.com/path/path/path", - "mp4,webm": "https://path1-video.com/path/path/path" - }, - "SizeThreshold": 204800 - }, - "/path2": "https://path2.com", - "/path3": { - "DefaultTarget": "https://path3.com", - "SizeThreshold": 512000 - } - }, - "Compression": { - "Gzip": { - "Enabled": false, - "Level": 6 - }, - "Brotli": { - "Enabled": false, - "Level": 4 - } - } -} \ No newline at end of file diff --git a/internal/config/types.go b/internal/config/types.go index 3962573..9255fc0 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -19,8 +19,8 @@ type PathConfig struct { type ExtensionRule struct { Extensions []string // 支持的扩展名列表 Target string // 目标服务器 - SizeThreshold int64 // 最小文件大小阈值 - MaxSize int64 // 最大文件大小阈值 + SizeThreshold int64 // 最小阈值 + MaxSize int64 // 最大阈值 } type CompressionConfig struct { @@ -37,8 +37,8 @@ type CompressorConfig struct { type ExtRuleConfig struct { Extensions string `json:"Extensions"` // 逗号分隔的扩展名 Target string `json:"Target"` // 目标服务器 - SizeThreshold int64 `json:"SizeThreshold"` // 最小文件大小阈值 - MaxSize int64 `json:"MaxSize"` // 最大文件大小阈值 + SizeThreshold int64 `json:"SizeThreshold"` // 最小阈值 + MaxSize int64 `json:"MaxSize"` // 最大阈值 } // 处理扩展名映射的方法 diff --git a/internal/utils/signal.go b/internal/utils/signal.go deleted file mode 100644 index 95e3a32..0000000 --- a/internal/utils/signal.go +++ /dev/null @@ -1,32 +0,0 @@ -package utils - -import ( - "os" - "os/signal" - "sync" - "syscall" -) - -func SetupCloseHandler(callback func()) { - c := make(chan os.Signal, 1) - done := make(chan bool, 1) - var once sync.Once - - signal.Notify(c, os.Interrupt, syscall.SIGTERM) - go func() { - <-c - once.Do(func() { - callback() - done <- true - }) - }() - - go func() { - select { - case <-done: - os.Exit(0) - case <-c: - os.Exit(1) - } - }() -} diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 0c119c4..624ac30 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "fmt" "log" + "math" "net" "net/http" neturl "net/url" @@ -219,11 +220,12 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo ext = strings.ToLower(ext) for _, rule := range pathConfig.ExtRules { // 处理阈值默认值 - if rule.SizeThreshold <= 0 { - rule.SizeThreshold = 500 * 1024 // 默认最小阈值 500KB + if rule.SizeThreshold < 0 { + rule.SizeThreshold = 0 // 默认不限制 } + if rule.MaxSize <= 0 { - rule.MaxSize = 10 * 1024 * 1024 // 默认最大阈值 10MB + rule.MaxSize = math.MaxInt64 // 设置为最大值表示不限制 } // 检查是否包含通配符 @@ -241,10 +243,9 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo // 如果没有找到匹配的具体扩展名规则,使用通配符规则 if len(matchingRules) == 0 { if len(wildcardRules) > 0 { - log.Printf("[Route] %s -> 使用通配符规则 (扩展名: %s)", path, ext) + log.Printf("[Route] %s -> 使用通配符规则", path) matchingRules = wildcardRules } else { - log.Printf("[Route] %s -> %s (没有找到扩展名 %s 的规则)", path, targetBase, ext) return targetBase, false } } @@ -265,8 +266,11 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo rule := &matchingRules[i] // 检查文件大小是否在阈值范围内 - if contentLength > rule.SizeThreshold && contentLength <= rule.MaxSize { + if contentLength >= rule.SizeThreshold && contentLength <= rule.MaxSize { // 找到匹配的规则 + log.Printf("[Route] %s -> %s (文件大小: %s, 在区间 %s 到 %s 之间)", + path, rule.Target, FormatBytes(contentLength), + FormatBytes(rule.SizeThreshold), FormatBytes(rule.MaxSize)) bestRule = rule break } diff --git a/web/app/dashboard/config/page.tsx b/web/app/dashboard/config/page.tsx index 442fe13..80e0152 100644 --- a/web/app/dashboard/config/page.tsx +++ b/web/app/dashboard/config/page.tsx @@ -32,8 +32,8 @@ import { interface ExtRuleConfig { Extensions: string; // 逗号分隔的扩展名 Target: string; // 目标服务器 - SizeThreshold: number; // 最小文件大小阈值(字节) - MaxSize: number; // 最大文件大小阈值(字节) + SizeThreshold: number; // 最小阈值(字节) + MaxSize: number; // 最大阈值(字节) } interface PathMapping { @@ -610,7 +610,7 @@ export default function ConfigPage() { if (maxSizeBytes > 0 && sizeThresholdBytes >= maxSizeBytes) { toast({ title: "错误", - description: "最大文件大小阈值必须大于最小文件大小阈值", + description: "最大阈值必须大于最小阈值", variant: "destructive", }); return; @@ -981,7 +981,7 @@ export default function ConfigPage() {
- +
- +