Skip to content
This repository was archived by the owner on Nov 3, 2025. It is now read-only.

Commit 0703fbf

Browse files
committed
ensure order
1 parent 05b8a5c commit 0703fbf

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

config/subscription.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package config
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"fmt"
57
"io"
68
"log"
@@ -9,7 +11,6 @@ import (
911
"helios/models"
1012

1113
"github.com/btcsuite/btcutil/base58"
12-
"github.com/bytedance/sonic"
1314
)
1415

1516
var GlobalConfig models.Config
@@ -35,18 +36,51 @@ func FetchSubscription(url string) error {
3536
return fmt.Errorf("failed to decode base58 data")
3637
}
3738

38-
var config models.Config
39-
if err := sonic.UnmarshalString(string(decodedData), &config); err != nil {
39+
// 先用 encoding/json 解析以保持键的顺序
40+
var rawConfig struct {
41+
APISites json.RawMessage `json:"api_site"`
42+
}
43+
44+
if err := json.Unmarshal(decodedData, &rawConfig); err != nil {
4045
return fmt.Errorf("failed to unmarshal config JSON: %v", err)
4146
}
47+
48+
// 构建最终的 config
49+
var config models.Config
50+
config.APISites = make(map[string]models.APISite)
51+
config.SiteList = make([]models.APISite, 0)
52+
53+
// 使用 json.Decoder 按顺序解析 api_site 对象
54+
dec := json.NewDecoder(bytes.NewReader(rawConfig.APISites))
55+
56+
// 读取开始的 {
57+
if _, err := dec.Token(); err != nil {
58+
return fmt.Errorf("failed to parse api_site: %v", err)
59+
}
60+
61+
// 按顺序读取每个键值对
62+
for dec.More() {
63+
// 读取键
64+
token, err := dec.Token()
65+
if err != nil {
66+
return fmt.Errorf("failed to read key: %v", err)
67+
}
68+
key := token.(string)
69+
70+
// 读取值
71+
var site models.APISite
72+
if err := dec.Decode(&site); err != nil {
73+
return fmt.Errorf("failed to decode site: %v", err)
74+
}
75+
76+
site.Key = key
77+
config.APISites[key] = site
78+
config.SiteList = append(config.SiteList, site)
79+
}
80+
4281
if len(config.APISites) == 0 {
4382
return fmt.Errorf("no API sites found in subscription")
4483
}
45-
config.SiteList = config.APISites
46-
for k, v := range config.APISites {
47-
v.Key = k
48-
config.APISites[k] = v
49-
}
5084

5185
GlobalConfig = config
5286
log.Printf("Subscription config loaded successfully. API sites: %d", len(config.APISites))

0 commit comments

Comments
 (0)