fix: accept plain seconds for user_manager duration fields

This commit is contained in:
Niko Marmeladkov 2026-06-18 17:27:20 +03:00
parent d2707dc283
commit 4b9fabd1c8
Signed by untrusted user who does not match committer: Niko
GPG key ID: E3B955F9442D44E3
2 changed files with 38 additions and 7 deletions

View file

@ -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"`

View file

@ -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,