fix: accept plain seconds for user_manager duration fields
This commit is contained in:
parent
d2707dc283
commit
4b9fabd1c8
2 changed files with 38 additions and 7 deletions
|
|
@ -1,15 +1,46 @@
|
||||||
package option
|
package option
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/json/badoption"
|
"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 {
|
type UserManagerOptions struct {
|
||||||
AuthServer string `json:"auth_server,omitempty"`
|
AuthServer string `json:"auth_server,omitempty"`
|
||||||
Timeout badoption.Duration `json:"timeout,omitempty"`
|
Timeout badoption.Duration `json:"timeout,omitempty"`
|
||||||
CacheTTL badoption.Duration `json:"cache_ttl,omitempty"`
|
CacheTTL DurationSeconds `json:"cache_ttl,omitempty"`
|
||||||
RefreshInterval badoption.Duration `json:"refresh_interval,omitempty"`
|
RefreshInterval DurationSeconds `json:"refresh_interval,omitempty"`
|
||||||
ReportInterval badoption.Duration `json:"report_interval,omitempty"`
|
ReportInterval DurationSeconds `json:"report_interval,omitempty"`
|
||||||
ReportTraffic bool `json:"report_traffic,omitempty"`
|
ReportTraffic bool `json:"report_traffic,omitempty"`
|
||||||
APISecret string `json:"api_secret,omitempty"`
|
APISecret string `json:"api_secret,omitempty"`
|
||||||
APIListen string `json:"api_listen,omitempty"`
|
APIListen string `json:"api_listen,omitempty"`
|
||||||
|
|
|
||||||
|
|
@ -87,9 +87,9 @@ func New(ctx context.Context, logger log.ContextLogger, tag string, options opti
|
||||||
logger: logger,
|
logger: logger,
|
||||||
authServer: options.AuthServer,
|
authServer: options.AuthServer,
|
||||||
timeout: time.Duration(options.Timeout),
|
timeout: time.Duration(options.Timeout),
|
||||||
cacheTTL: time.Duration(options.CacheTTL),
|
cacheTTL: options.CacheTTL.Build(),
|
||||||
refreshInt: time.Duration(options.RefreshInterval),
|
refreshInt: options.RefreshInterval.Build(),
|
||||||
reportInt: time.Duration(options.ReportInterval),
|
reportInt: options.ReportInterval.Build(),
|
||||||
reportTraffic: options.ReportTraffic,
|
reportTraffic: options.ReportTraffic,
|
||||||
apiSecret: options.APISecret,
|
apiSecret: options.APISecret,
|
||||||
apiListen: options.APIListen,
|
apiListen: options.APIListen,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue