From 4b9fabd1c8b33fbcdf24d5633276a1e8f088d8a3 Mon Sep 17 00:00:00 2001 From: Niko Marmeladkov Date: Thu, 18 Jun 2026 17:27:20 +0300 Subject: [PATCH] fix: accept plain seconds for user_manager duration fields --- option/user_manager.go | 39 ++++++++++++++++++++++++++++++---- service/usermanager/manager.go | 6 +++--- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/option/user_manager.go b/option/user_manager.go index bbcca628..71a942d2 100644 --- a/option/user_manager.go +++ b/option/user_manager.go @@ -1,15 +1,46 @@ package option import ( + "encoding/json" + "time" + "github.com/sagernet/sing/common/json/badoption" ) +type DurationSeconds time.Duration + +func (d *DurationSeconds) UnmarshalJSON(b []byte) error { + var n int64 + if err := json.Unmarshal(b, &n); err == nil { + *d = DurationSeconds(time.Duration(n) * time.Second) + return nil + } + var s string + if err := json.Unmarshal(b, &s); err == nil { + dur, err := time.ParseDuration(s) + if err != nil { + return err + } + *d = DurationSeconds(dur) + return nil + } + return nil +} + +func (d DurationSeconds) MarshalJSON() ([]byte, error) { + return json.Marshal(time.Duration(d).String()) +} + +func (d DurationSeconds) Build() time.Duration { + return time.Duration(d) +} + type UserManagerOptions struct { - AuthServer string `json:"auth_server,omitempty"` + AuthServer string `json:"auth_server,omitempty"` Timeout badoption.Duration `json:"timeout,omitempty"` - CacheTTL badoption.Duration `json:"cache_ttl,omitempty"` - RefreshInterval badoption.Duration `json:"refresh_interval,omitempty"` - ReportInterval badoption.Duration `json:"report_interval,omitempty"` + CacheTTL DurationSeconds `json:"cache_ttl,omitempty"` + RefreshInterval DurationSeconds `json:"refresh_interval,omitempty"` + ReportInterval DurationSeconds `json:"report_interval,omitempty"` ReportTraffic bool `json:"report_traffic,omitempty"` APISecret string `json:"api_secret,omitempty"` APIListen string `json:"api_listen,omitempty"` diff --git a/service/usermanager/manager.go b/service/usermanager/manager.go index 250e3a46..bb4c8ea8 100644 --- a/service/usermanager/manager.go +++ b/service/usermanager/manager.go @@ -87,9 +87,9 @@ func New(ctx context.Context, logger log.ContextLogger, tag string, options opti logger: logger, authServer: options.AuthServer, timeout: time.Duration(options.Timeout), - cacheTTL: time.Duration(options.CacheTTL), - refreshInt: time.Duration(options.RefreshInterval), - reportInt: time.Duration(options.ReportInterval), + cacheTTL: options.CacheTTL.Build(), + refreshInt: options.RefreshInterval.Build(), + reportInt: options.ReportInterval.Build(), reportTraffic: options.ReportTraffic, apiSecret: options.APISecret, apiListen: options.APIListen,