Skip to content

Commit

Permalink
move some options to "advanced" subcategory
Browse files Browse the repository at this point in the history
  • Loading branch information
zyxkad committed Feb 22, 2024
1 parent 0ae0e03 commit 3184860
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 119 deletions.
27 changes: 15 additions & 12 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,12 @@ bash ./start.sh
配置文件应为**运行目录**下的`config.yaml`文件, 使用`yaml`格式解析
例:
```yaml
# 是否打印调试日志, 默认为否
debug: false
# 是否打印访问信息, 默认为否 (这个选项对于压缩日志文件十分有用)
record-serve-info: false
# 跳过第一次同步, 直接启动节点 (debug用)
skip-first-sync: false
# 是否在连接断开后直接退出
exit-when-disconnected: false
# 日志最长保存时间 (天). 设置为 0 禁用清理过期日志
log-slots: 7
# 是否禁用 bmclapi 分发的证书, 同 CLUSTER_BYOC
byoc: false
# 是否仅从主服务器下载文件
noopen: false
# 跳过文件哈希值校验
no-heavy-check: false
# 是否信任 X-Forwarded-For 标头 (有反代时启用)
trusted-x-forwarded-for: false
# 实际开放的公网主机名, 同 CLUSTER_IP
Expand All @@ -127,8 +117,6 @@ cluster-id: ${CLUSTER_ID}
cluster-secret: ${CLUSTER_SECRET}
# 文件同步间隔 (分钟)
sync-interval: 10
# 发送心跳包的超时限制 (秒), 网不好就调高点
keepalive-timeout: 10
# 同步文件时最多打开的连接数量. 注意: 该选项目前没用
download-max-conn: 64

Expand Down Expand Up @@ -235,6 +223,21 @@ webdav-users:
# 密码
password: example-password

# 以下为高级选项, 通常用于调试. ** 如果不理解其工作原理请不要碰 **
advanced:
# 是否打印调试日志
debug-log: false
# 跳过第一次同步, 直接启动节点
skip-first-sync: false
# 是否在连接断开后直接退出
exit-when-disconnected: false
# 是否仅从主服务器下载文件
noopen: false
# 跳过文件哈希值校验
no-heavy-check: false
# 发送心跳包的超时限制 (秒), 网不好就调高点
keepalive-timeout: 10

```

## 子命令
Expand Down
10 changes: 5 additions & 5 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (cr *Cluster) Connect(ctx context.Context) bool {

cr.reconnectCount = 0

if config.Debug {
if config.Advanced.DebugLog {
engio.OnRecv(func(_ *engine.Socket, data []byte) {
logDebugf("Engine.IO recv: %q", (string)(data))
})
Expand All @@ -215,7 +215,7 @@ func (cr *Cluster) Connect(ctx context.Context) bool {
logInfo("Engine.IO connected")
})
engio.OnDisconnect(func(_ *engine.Socket, err error) {
if config.ExitWhenDisconnected {
if config.Advanced.ExitWhenDisconnected {
if cr.shouldEnable {
logErrorf("Cluster disconnected from remote; exit.")
os.Exit(0x08)
Expand Down Expand Up @@ -298,7 +298,7 @@ func (cr *Cluster) Enable(ctx context.Context) (err error) {
return
}

if !cr.socket.IO().Connected() && config.ExitWhenDisconnected {
if !cr.socket.IO().Connected() && config.Advanced.ExitWhenDisconnected {
logErrorf("Cluster disconnected from remote; exit.")
os.Exit(0x08)
return
Expand Down Expand Up @@ -430,7 +430,7 @@ func (cr *Cluster) Disable(ctx context.Context) (ok bool) {
}
logInfo("Disabling cluster")
if resCh, err := cr.socket.EmitWithAck("disable"); err == nil {
tctx, cancel := context.WithTimeout(ctx, time.Second*(time.Duration)(config.KeepaliveTimeout))
tctx, cancel := context.WithTimeout(ctx, time.Second*(time.Duration)(config.Advanced.KeepaliveTimeout))
select {
case <-tctx.Done():
cancel()
Expand Down Expand Up @@ -857,7 +857,7 @@ func (cr *Cluster) syncFiles(ctx context.Context, files []FileInfo, heavyCheck b

var stats syncStats
stats.pg = pg
stats.noOpen = config.NoOpen || syncCfg.Source == "center"
stats.noOpen = config.Advanced.NoOpen || syncCfg.Source == "center"
stats.slots = NewBufSlots(syncCfg.Concurrency)
stats.totalFiles = totalFiles
for _, f := range missing {
Expand Down
209 changes: 118 additions & 91 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ import (
"gopkg.in/yaml.v3"
)

type AdvancedConfig struct {
DebugLog bool `yaml:"debug-log"`
SkipFirstSync bool `yaml:"skip-first-sync"`
ExitWhenDisconnected bool `yaml:"exit-when-disconnected"`
NoOpen bool `yaml:"noopen"`
NoHeavyCheck bool `yaml:"no-heavy-check"`
KeepaliveTimeout int `yaml:"keepalive-timeout"`
}

type ServeLimitConfig struct {
Enable bool `yaml:"enable"`
MaxConn int `yaml:"max-conn"`
Expand Down Expand Up @@ -85,29 +94,24 @@ type WebDavUser struct {
}

type Config struct {
Debug bool `yaml:"debug"`
RecordServeInfo bool `yaml:"record-serve-info"`
SkipFirstSync bool `yaml:"skip-first-sync"`
ExitWhenDisconnected bool `yaml:"exit-when-disconnected"`
LogSlots int `yaml:"log-slots"`
Byoc bool `yaml:"byoc"`
NoOpen bool `yaml:"noopen"`
NoHeavyCheck bool `yaml:"no-heavy-check"`
TrustedXForwardedFor bool `yaml:"trusted-x-forwarded-for"`
PublicHost string `yaml:"public-host"`
PublicPort uint16 `yaml:"public-port"`
Port uint16 `yaml:"port"`
ClusterId string `yaml:"cluster-id"`
ClusterSecret string `yaml:"cluster-secret"`
SyncInterval int `yaml:"sync-interval"`
KeepaliveTimeout int `yaml:"keepalive-timeout"`
DownloadMaxConn int `yaml:"download-max-conn"`

Cache CacheConfig `yaml:"cache"`
ServeLimit ServeLimitConfig `yaml:"serve-limit"`
Dashboard DashboardConfig `yaml:"dashboard"`
Storages []StorageOption `yaml:"storages"`
WebdavUsers map[string]*WebDavUser `yaml:"webdav-users"`
Advanced AdvancedConfig `yaml:"advanced"`
}

func (cfg *Config) applyWebManifest(manifest map[string]any) {
Expand All @@ -119,22 +123,16 @@ func (cfg *Config) applyWebManifest(manifest map[string]any) {
}

var defaultConfig = Config{
Debug: false,
RecordServeInfo: false,
SkipFirstSync: false,
ExitWhenDisconnected: false,
LogSlots: 7,
Byoc: false,
NoOpen: false,
NoHeavyCheck: false,
TrustedXForwardedFor: false,
PublicHost: "",
PublicPort: 0,
Port: 4000,
ClusterId: "${CLUSTER_ID}",
ClusterSecret: "${CLUSTER_SECRET}",
SyncInterval: 10,
KeepaliveTimeout: 10,
DownloadMaxConn: 16,

Cache: CacheConfig{
Expand All @@ -158,94 +156,123 @@ var defaultConfig = Config{
Storages: nil,

WebdavUsers: map[string]*WebDavUser{},

Advanced: AdvancedConfig{
DebugLog: false,
SkipFirstSync: false,
ExitWhenDisconnected: false,
NoOpen: false,
NoHeavyCheck: false,
KeepaliveTimeout: 10,
},
}

func migrateConfig(data []byte, config *Config) {
var oldConfig map[string]any
if err := yaml.Unmarshal(data, &oldConfig); err != nil {
return
}
if nohttps, ok := oldConfig["nohttps"].(bool); ok {
config.Byoc = nohttps
}
if v, ok := oldConfig["record_serve_info"].(bool); ok {
config.RecordServeInfo = v
}
if v, ok := oldConfig["no_heavy_check"].(bool); ok {
config.NoHeavyCheck = v
}
if v, ok := oldConfig["public_host"].(string); ok {
config.PublicHost = v
}
if v, ok := oldConfig["public_port"].(int); ok {
config.PublicPort = (uint16)(v)
}
if v, ok := oldConfig["cluster_id"].(string); ok {
config.ClusterId = v
}
if v, ok := oldConfig["cluster_secret"].(string); ok {
config.ClusterSecret = v

// if nohttps, ok := oldConfig["nohttps"].(bool); ok {
// config.Byoc = nohttps
// }
// if v, ok := oldConfig["record_serve_info"].(bool); ok {
// config.RecordServeInfo = v
// }
// if v, ok := oldConfig["no_heavy_check"].(bool); ok {
// config.NoHeavyCheck = v
// }
// if v, ok := oldConfig["public_host"].(string); ok {
// config.PublicHost = v
// }
// if v, ok := oldConfig["public_port"].(int); ok {
// config.PublicPort = (uint16)(v)
// }
// if v, ok := oldConfig["cluster_id"].(string); ok {
// config.ClusterId = v
// }
// if v, ok := oldConfig["cluster_secret"].(string); ok {
// config.ClusterSecret = v
// }
// if v, ok := oldConfig["sync_interval"].(int); ok {
// config.SyncInterval = v
// }
// if v, ok := oldConfig["keepalive_timeout"].(int); ok {
// config.KeepaliveTimeout = v
// }
// if v, ok := oldConfig["download_max_conn"].(int); ok {
// config.DownloadMaxConn = v
// }
// if limit, ok := oldConfig["serve_limit"].(map[string]any); ok {
// var sl ServeLimitConfig
// if sl.Enable, ok = limit["enable"].(bool); !ok {
// goto SKIP_SERVE_LIMIT
// }
// if sl.MaxConn, ok = limit["max_conn"].(int); !ok {
// goto SKIP_SERVE_LIMIT
// }
// if sl.UploadRate, ok = limit["upload_rate"].(int); !ok {
// goto SKIP_SERVE_LIMIT
// }
// config.ServeLimit = sl
// SKIP_SERVE_LIMIT:
// }

// if oss, ok := oldConfig["oss"].(map[string]any); ok && oss["enable"] == true {
// var storages []StorageOption
// logInfo("Migrate old oss config to latest format")
// if list, ok := oss["list"].([]any); ok {
// for _, v := range list {
// if item, ok := v.(map[string]any); ok {
// var (
// stItem StorageOption
// mountOpt = new(MountStorageOption)
// )
// stItem.Type = StorageMount
// folderPath, ok := item["folder_path"].(string)
// if !ok {
// continue
// }
// mountOpt.Path = folderPath
// redirectBase, ok := item["redirect_base"].(string)
// if !ok {
// continue
// }
// mountOpt.RedirectBase = redirectBase
// preGenMeasures, ok := item["pre-create-measures"].(bool)
// if ok {
// mountOpt.PreGenMeasures = preGenMeasures
// }
// weight, ok := item["possibility"].(int)
// if !ok {
// weight = 100
// }
// stItem.Weight = (uint)(weight)
// stItem.Data = mountOpt
// storages = append(storages, stItem)
// }
// }
// }
// config.Storages = storages
// }

if v, ok := oldConfig["debug"].(bool); ok {
config.Advanced.DebugLog = v
}
if v, ok := oldConfig["sync_interval"].(int); ok {
config.SyncInterval = v
if v, ok := oldConfig["skip-first-sync"].(bool); ok {
config.Advanced.SkipFirstSync = v
}
if v, ok := oldConfig["keepalive_timeout"].(int); ok {
config.KeepaliveTimeout = v
if v, ok := oldConfig["exit-when-disconnected"].(bool); ok {
config.Advanced.ExitWhenDisconnected = v
}
if v, ok := oldConfig["download_max_conn"].(int); ok {
config.DownloadMaxConn = v
if v, ok := oldConfig["noopen"].(bool); ok {
config.Advanced.NoOpen = v
}
if limit, ok := oldConfig["serve_limit"].(map[string]any); ok {
var sl ServeLimitConfig
if sl.Enable, ok = limit["enable"].(bool); !ok {
goto SKIP_SERVE_LIMIT
}
if sl.MaxConn, ok = limit["max_conn"].(int); !ok {
goto SKIP_SERVE_LIMIT
}
if sl.UploadRate, ok = limit["upload_rate"].(int); !ok {
goto SKIP_SERVE_LIMIT
}
config.ServeLimit = sl
SKIP_SERVE_LIMIT:
if v, ok := oldConfig["no-heavy-check"].(bool); ok {
config.Advanced.NoHeavyCheck = v
}

if oss, ok := oldConfig["oss"].(map[string]any); ok && oss["enable"] == true {
var storages []StorageOption
logInfo("Migrate old oss config to latest format")
if list, ok := oss["list"].([]any); ok {
for _, v := range list {
if item, ok := v.(map[string]any); ok {
var (
stItem StorageOption
mountOpt = new(MountStorageOption)
)
stItem.Type = StorageMount
folderPath, ok := item["folder_path"].(string)
if !ok {
continue
}
mountOpt.Path = folderPath
redirectBase, ok := item["redirect_base"].(string)
if !ok {
continue
}
mountOpt.RedirectBase = redirectBase
preGenMeasures, ok := item["pre-create-measures"].(bool)
if ok {
mountOpt.PreGenMeasures = preGenMeasures
}
weight, ok := item["possibility"].(int)
if !ok {
weight = 100
}
stItem.Weight = (uint)(weight)
stItem.Data = mountOpt
storages = append(storages, stItem)
}
}
}
config.Storages = storages
if v, ok := oldConfig["keepalive-timeout"].(int); ok {
config.Advanced.KeepaliveTimeout = v
}
}

Expand Down Expand Up @@ -321,7 +348,7 @@ func readConfig() (config Config) {
}

if os.Getenv("DEBUG") == "true" {
config.Debug = true
config.Advanced.DebugLog = true
}
if v := os.Getenv("CLUSTER_IP"); v != "" {
config.PublicHost = v
Expand Down Expand Up @@ -351,9 +378,9 @@ func readConfig() (config Config) {
}
switch noopen := os.Getenv("FORCE_NOOPEN"); noopen {
case "true":
config.NoOpen = true
config.Advanced.NoOpen = true
case "false":
config.NoOpen = false
config.Advanced.NoOpen = false
}
return
}
Expand Down
Loading

0 comments on commit 3184860

Please sign in to comment.