feat: HH-773 合集重命名接入 egress geo 缓存、geo 失败降级与每小时补探测
Build and Publish Docker Image / build-and-push (pull_request) Successful in 15m54s
Build and Publish Docker Image / build-and-push (pull_request) Successful in 15m54s
- service: EgressCacheKey 从 handler 下沉复用;MergeCachedEgressGeo 在合集 重命名前合并缓存 geo 字段(纯缓存读、零网络 I/O);StripEgressGeoFields 防止 geo 字段泄漏进订阅输出 - filter: geo 检测失败的节点保留 '[别名] 原名'(或纯原名)参与编号/排序, 不再丢弃 - handler: 每小时 StartEgressRefresher 对启用源补探测缺失/过期的 egress 缓存(含 TTL 内 error 结果一律跳过);RegisterRoutes 返回 *Deps; cmd/server.go 启动定时任务并在 shutdown 时取消 - tests: service 缓存 key 稳定性/合并/管道测试、handler 补探测跳过/TTL/ 过期/取消测试、filter 降级测试;修复 2 个过时测试(mihomo YAML 配置、 缓存命中路径)
This commit is contained in:
@@ -14,11 +14,13 @@ import (
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"gopkg.in/yaml.v3"
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"github.com/peterqiu0516/sub-store/internal/config"
|
||||
"github.com/peterqiu0516/sub-store/internal/database"
|
||||
"github.com/peterqiu0516/sub-store/internal/model"
|
||||
"github.com/peterqiu0516/sub-store/internal/service"
|
||||
"github.com/peterqiu0516/sub-store/internal/template"
|
||||
)
|
||||
|
||||
@@ -2127,25 +2129,42 @@ func TestBuildEgressProbeConfig(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("buildEgressProbeConfig returned error: %v", err)
|
||||
}
|
||||
// The probe config is a minimal mihomo (Clash Meta) YAML.
|
||||
var doc map[string]any
|
||||
if err := json.Unmarshal(data, &doc); err != nil {
|
||||
t.Fatalf("invalid config JSON: %v", err)
|
||||
if err := yaml.Unmarshal(data, &doc); err != nil {
|
||||
t.Fatalf("invalid config YAML: %v", err)
|
||||
}
|
||||
route := doc["route"].(map[string]any)
|
||||
if route["final"] != "PROXY" {
|
||||
t.Fatalf("route.final = %v, want PROXY", route["final"])
|
||||
if port, _ := doc["mixed-port"].(int); port != 19090 {
|
||||
t.Fatalf("mixed-port = %v, want 19090", doc["mixed-port"])
|
||||
}
|
||||
inbound := doc["inbounds"].([]any)[0].(map[string]any)
|
||||
if inbound["listen"] != "127.0.0.1" || inbound["listen_port"].(float64) != 19090 {
|
||||
t.Fatalf("unexpected inbound: %v", inbound)
|
||||
proxies, _ := doc["proxies"].([]any)
|
||||
if len(proxies) != 1 {
|
||||
t.Fatalf("proxies = %v, want 1 entry", doc["proxies"])
|
||||
}
|
||||
proxyMap, _ := proxies[0].(map[string]any)
|
||||
if proxyMap["name"] != "PROXY" || proxyMap["server"] != "127.0.0.1" || proxyMap["cipher"] != "aes-256-gcm" {
|
||||
t.Fatalf("unexpected probe proxy: %v", proxyMap)
|
||||
}
|
||||
groups, _ := doc["proxy-groups"].([]any)
|
||||
group, _ := groups[0].(map[string]any)
|
||||
if group["type"] != "select" {
|
||||
t.Fatalf("proxy-group type = %v, want select", group["type"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleEgressInfoUnsupported(t *testing.T) {
|
||||
func TestHandleEgressInfoCacheHit(t *testing.T) {
|
||||
deps := newTestDeps(t)
|
||||
app := newApp(deps)
|
||||
code, _ := doRequest(t, app, "POST", "/api/utils/egress-info", `{"type":"unknown","server":"1.2.3.4","port":443}`, nil)
|
||||
assertStatus(t, "EgressInfo unsupported", code, 400)
|
||||
// Any node type is probed now; a cached result must short-circuit before
|
||||
// any probing so the response is deterministic without network access.
|
||||
deps.CacheRepo.SafePut(service.EgressCacheKey(model.ProxyNode{"type": "ss", "server": "1.2.3.4", "port": 8388}),
|
||||
`{"egressIp":"1.1.1.1","country":"Japan","latencyMs":12}`, nil, 300)
|
||||
code, out := doRequest(t, app, "POST", "/api/utils/egress-info", `{"type":"ss","server":"1.2.3.4","port":8388}`, nil)
|
||||
assertStatus(t, "EgressInfo cache hit", code, 200)
|
||||
data, _ := out["data"].(map[string]any)
|
||||
if data["egressIp"] != "1.1.1.1" || data["cached"] != true {
|
||||
t.Fatalf("unexpected cached egress response: %v", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeServerPortLatency(t *testing.T) {
|
||||
@@ -2180,7 +2199,7 @@ func TestAddCachedEgressInfo(t *testing.T) {
|
||||
"server": "127.0.0.1",
|
||||
"port": 8388,
|
||||
}
|
||||
deps.CacheRepo.SafePut(egressCacheKey(node), `{"egressIp":"1.1.1.1","country":"Japan","region":"Tokyo","latencyMs":12}`, nil, 300)
|
||||
deps.CacheRepo.SafePut(service.EgressCacheKey(node), `{"egressIp":"1.1.1.1","country":"Japan","region":"Tokyo","latencyMs":12}`, nil, 300)
|
||||
nodes := deps.addCachedEgressInfo([]model.ProxyNode{node})
|
||||
if nodes[0]["egressIp"] != "1.1.1.1" || nodes[0]["latencyMs"].(float64) != 12 {
|
||||
t.Fatalf("cached egress not merged: %v", nodes[0])
|
||||
|
||||
Reference in New Issue
Block a user