diff --git a/go.mod b/go.mod index 13e3e74a..728a0d7f 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.16 require ( github.com/AdguardTeam/dnsproxy v0.39.8 - github.com/AdguardTeam/golibs v0.9.3 + github.com/AdguardTeam/golibs v0.10.0 github.com/AdguardTeam/urlfilter v0.14.6 github.com/NYTimes/gziphandler v1.1.1 github.com/ameshkov/dnscrypt/v2 v2.2.2 @@ -27,7 +27,7 @@ require ( github.com/ti-mo/netfilter v0.4.0 go.etcd.io/bbolt v1.3.6 golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 - golang.org/x/net v0.0.0-20210825183410-e898025ed96a + golang.org/x/net v0.0.0-20210929193557-e81a3d93ecf6 golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e gopkg.in/natefinch/lumberjack.v2 v2.0.0 gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index b3dcd9f3..6ec3396e 100644 --- a/go.sum +++ b/go.sum @@ -14,8 +14,8 @@ github.com/AdguardTeam/dnsproxy v0.39.8/go.mod h1:eDpJKAdkHORRwAedjuERv+7SWlcz4c github.com/AdguardTeam/golibs v0.4.0/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4= github.com/AdguardTeam/golibs v0.4.2/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4= github.com/AdguardTeam/golibs v0.9.2/go.mod h1:fCAMwPBJ8S7YMYbTWvYS+eeTLblP5E04IDtNAo7y7IY= -github.com/AdguardTeam/golibs v0.9.3 h1:noeKHJEzrSwxzX0Zi3USM3cXf1qQV99SO772jet/uEY= -github.com/AdguardTeam/golibs v0.9.3/go.mod h1:fCAMwPBJ8S7YMYbTWvYS+eeTLblP5E04IDtNAo7y7IY= +github.com/AdguardTeam/golibs v0.10.0 h1:A7MXRfZ+ItpOyS9tWKtqrLj3vZtE9FJFC+dOVY/LcWs= +github.com/AdguardTeam/golibs v0.10.0/go.mod h1:rSfQRGHIdgfxriDDNgNJ7HmE5zRoURq8R+VdR81Zuzw= github.com/AdguardTeam/gomitmproxy v0.2.0/go.mod h1:Qdv0Mktnzer5zpdpi5rAwixNJzW2FN91LjKJCkVbYGU= github.com/AdguardTeam/urlfilter v0.14.6 h1:emqoKZElooHACYehRBYENeKVN1a/rspxiqTIMYLuoIo= github.com/AdguardTeam/urlfilter v0.14.6/go.mod h1:klx4JbOfc4EaNb5lWLqOwfg+pVcyRukmoJRvO55lL5U= @@ -303,8 +303,8 @@ golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20210825183410-e898025ed96a h1:bRuuGXV8wwSdGTB+CtJf+FjgO1APK1CoO39T4BN/XBw= -golang.org/x/net v0.0.0-20210825183410-e898025ed96a/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20210929193557-e81a3d93ecf6 h1:Z04ewVs7JhXaYkmDhBERPi41gnltfQpMWDnTnQbaCqk= +golang.org/x/net v0.0.0-20210929193557-e81a3d93ecf6/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -367,8 +367,9 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/internal/aghtime/duration.go b/internal/aghtime/duration.go deleted file mode 100644 index af6c3d8a..00000000 --- a/internal/aghtime/duration.go +++ /dev/null @@ -1,68 +0,0 @@ -// Package aghtime defines some types for convenient work with time values. -package aghtime - -import ( - "time" - - "github.com/AdguardTeam/golibs/errors" -) - -// Duration is a wrapper for time.Duration providing functionality for encoding. -type Duration struct { - // time.Duration is embedded here to avoid implementing all the methods. - time.Duration -} - -// String implements the fmt.Stringer interface for Duration. It wraps -// time.Duration.String method and additionally cuts off non-leading zero values -// of minutes and seconds. Some values which are differ between the -// implementations: -// -// Duration: "1m", time.Duration: "1m0s" -// Duration: "1h", time.Duration: "1h0m0s" -// Duration: "1h1m", time.Duration: "1h1m0s" -// -func (d Duration) String() (str string) { - str = d.Duration.String() - - const ( - tailMin = len(`0s`) - tailMinSec = len(`0m0s`) - - secsInHour = time.Hour / time.Second - minsInHour = time.Hour / time.Minute - ) - - switch rounded := d.Duration / time.Second; { - case - rounded == 0, - rounded*time.Second != d.Duration, - rounded%60 != 0: - // Return the uncut value if it's either equal to zero or has - // fractions of a second or even whole seconds in it. - return str - - case (rounded%secsInHour)/minsInHour != 0: - return str[:len(str)-tailMin] - - default: - return str[:len(str)-tailMinSec] - } -} - -// MarshalText implements the encoding.TextMarshaler interface for Duration. -func (d Duration) MarshalText() (text []byte, err error) { - return []byte(d.String()), nil -} - -// UnmarshalText implements the encoding.TextUnmarshaler interface for -// *Duration. -// -// TODO(e.burkov): Make it able to parse larger units like days. -func (d *Duration) UnmarshalText(b []byte) (err error) { - defer func() { err = errors.Annotate(err, "unmarshaling duration: %w") }() - - d.Duration, err = time.ParseDuration(string(b)) - - return err -} diff --git a/internal/aghtime/duration_test.go b/internal/aghtime/duration_test.go deleted file mode 100644 index 76518107..00000000 --- a/internal/aghtime/duration_test.go +++ /dev/null @@ -1,240 +0,0 @@ -package aghtime - -import ( - "bytes" - "encoding/json" - "strings" - "testing" - "time" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - yaml "gopkg.in/yaml.v2" -) - -func TestDuration_String(t *testing.T) { - testCases := []struct { - name string - val time.Duration - }{{ - name: "1s", - val: time.Second, - }, { - name: "1m", - val: time.Minute, - }, { - name: "1h", - val: time.Hour, - }, { - name: "1m1s", - val: time.Minute + time.Second, - }, { - name: "1h1m", - val: time.Hour + time.Minute, - }, { - name: "1h0m1s", - val: time.Hour + time.Second, - }, { - name: "1ms", - val: time.Millisecond, - }, { - name: "1h0m0.001s", - val: time.Hour + time.Millisecond, - }, { - name: "1.001s", - val: time.Second + time.Millisecond, - }, { - name: "1m1.001s", - val: time.Minute + time.Second + time.Millisecond, - }, { - name: "0s", - val: 0, - }} - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - d := Duration{Duration: tc.val} - assert.Equal(t, tc.name, d.String()) - }) - } -} - -// durationEncodingTester is a helper struct to simplify testing different -// Duration marshalling and unmarshaling cases. -type durationEncodingTester struct { - PtrMap map[string]*Duration `json:"ptr_map" yaml:"ptr_map"` - PtrSlice []*Duration `json:"ptr_slice" yaml:"ptr_slice"` - PtrValue *Duration `json:"ptr_value" yaml:"ptr_value"` - PtrArray [1]*Duration `json:"ptr_array" yaml:"ptr_array"` - Map map[string]Duration `json:"map" yaml:"map"` - Slice []Duration `json:"slice" yaml:"slice"` - Value Duration `json:"value" yaml:"value"` - Array [1]Duration `json:"array" yaml:"array"` -} - -const nl = "\n" -const ( - jsonStr = `{` + - `"ptr_map":{"dur":"1ms"},` + - `"ptr_slice":["1ms"],` + - `"ptr_value":"1ms",` + - `"ptr_array":["1ms"],` + - `"map":{"dur":"1ms"},` + - `"slice":["1ms"],` + - `"value":"1ms",` + - `"array":["1ms"]` + - `}` - yamlStr = `ptr_map:` + nl + - ` dur: 1ms` + nl + - `ptr_slice:` + nl + - `- 1ms` + nl + - `ptr_value: 1ms` + nl + - `ptr_array:` + nl + - `- 1ms` + nl + - `map:` + nl + - ` dur: 1ms` + nl + - `slice:` + nl + - `- 1ms` + nl + - `value: 1ms` + nl + - `array:` + nl + - `- 1ms` -) - -// defaultTestDur is the default time.Duration value to be used throughout the tests of -// Duration. -const defaultTestDur = time.Millisecond - -// checkFields verifies m's fields. It expects the m to be unmarshaled from -// one of the constant strings above. -func (m *durationEncodingTester) checkFields(t *testing.T, d Duration) { - t.Run("pointers_map", func(t *testing.T) { - require.NotNil(t, m.PtrMap) - - fromPtrMap, ok := m.PtrMap["dur"] - require.True(t, ok) - require.NotNil(t, fromPtrMap) - - assert.Equal(t, d, *fromPtrMap) - }) - - t.Run("pointers_slice", func(t *testing.T) { - require.Len(t, m.PtrSlice, 1) - - fromPtrSlice := m.PtrSlice[0] - require.NotNil(t, fromPtrSlice) - - assert.Equal(t, d, *fromPtrSlice) - }) - - t.Run("pointers_array", func(t *testing.T) { - fromPtrArray := m.PtrArray[0] - require.NotNil(t, fromPtrArray) - - assert.Equal(t, d, *fromPtrArray) - }) - - t.Run("pointer_value", func(t *testing.T) { - require.NotNil(t, m.PtrValue) - - assert.Equal(t, d, *m.PtrValue) - }) - - t.Run("map", func(t *testing.T) { - fromMap, ok := m.Map["dur"] - require.True(t, ok) - - assert.Equal(t, d, fromMap) - }) - - t.Run("slice", func(t *testing.T) { - require.Len(t, m.Slice, 1) - - assert.Equal(t, d, m.Slice[0]) - }) - - t.Run("array", func(t *testing.T) { - assert.Equal(t, d, m.Array[0]) - }) - - t.Run("value", func(t *testing.T) { - assert.Equal(t, d, m.Value) - }) -} - -func TestDuration_MarshalText(t *testing.T) { - d := Duration{defaultTestDur} - dPtr := &d - - v := durationEncodingTester{ - PtrMap: map[string]*Duration{"dur": dPtr}, - PtrSlice: []*Duration{dPtr}, - PtrValue: dPtr, - PtrArray: [1]*Duration{dPtr}, - Map: map[string]Duration{"dur": d}, - Slice: []Duration{d}, - Value: d, - Array: [1]Duration{d}, - } - - b := &bytes.Buffer{} - t.Run("json", func(t *testing.T) { - t.Cleanup(b.Reset) - err := json.NewEncoder(b).Encode(v) - require.NoError(t, err) - - assert.JSONEq(t, jsonStr, b.String()) - }) - - t.Run("yaml", func(t *testing.T) { - t.Cleanup(b.Reset) - err := yaml.NewEncoder(b).Encode(v) - require.NoError(t, err) - - assert.YAMLEq(t, yamlStr, b.String(), b.String()) - }) - - t.Run("direct", func(t *testing.T) { - data, err := d.MarshalText() - require.NoError(t, err) - - assert.EqualValues(t, []byte(defaultTestDur.String()), data) - }) -} - -func TestDuration_UnmarshalText(t *testing.T) { - d := Duration{defaultTestDur} - var v *durationEncodingTester - - t.Run("json", func(t *testing.T) { - v = &durationEncodingTester{} - - r := strings.NewReader(jsonStr) - err := json.NewDecoder(r).Decode(v) - require.NoError(t, err) - - v.checkFields(t, d) - }) - - t.Run("yaml", func(t *testing.T) { - v = &durationEncodingTester{} - - r := strings.NewReader(yamlStr) - err := yaml.NewDecoder(r).Decode(v) - require.NoError(t, err) - - v.checkFields(t, d) - }) - - t.Run("direct", func(t *testing.T) { - dd := &Duration{} - - err := dd.UnmarshalText([]byte(d.String())) - require.NoError(t, err) - - assert.Equal(t, d, *dd) - }) - - t.Run("bad_data", func(t *testing.T) { - assert.Error(t, (&Duration{}).UnmarshalText([]byte(`abc`))) - }) -} diff --git a/internal/dhcpd/v4.go b/internal/dhcpd/v4.go index 6c2d4b5e..d7e4dfae 100644 --- a/internal/dhcpd/v4.go +++ b/internal/dhcpd/v4.go @@ -16,6 +16,7 @@ import ( "github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/stringutil" + "github.com/AdguardTeam/golibs/timeutil" "github.com/go-ping/ping" "github.com/insomniacslk/dhcp/dhcpv4" "github.com/insomniacslk/dhcp/dhcpv4/server4" @@ -1126,7 +1127,7 @@ func v4Create(conf V4ServerConf) (srv DHCPServer, err error) { s.leasedOffsets = newBitSet() if conf.LeaseDuration == 0 { - s.conf.leaseTime = time.Hour * 24 + s.conf.leaseTime = timeutil.Day s.conf.LeaseDuration = uint32(s.conf.leaseTime.Seconds()) } else { s.conf.leaseTime = time.Second * time.Duration(conf.LeaseDuration) diff --git a/internal/dhcpd/v6.go b/internal/dhcpd/v6.go index 92cf59ce..625f99be 100644 --- a/internal/dhcpd/v6.go +++ b/internal/dhcpd/v6.go @@ -14,6 +14,7 @@ import ( "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/netutil" + "github.com/AdguardTeam/golibs/timeutil" "github.com/insomniacslk/dhcp/dhcpv6" "github.com/insomniacslk/dhcp/dhcpv6/server6" "github.com/insomniacslk/dhcp/iana" @@ -707,7 +708,7 @@ func v6Create(conf V6ServerConf) (DHCPServer, error) { } if conf.LeaseDuration == 0 { - s.conf.leaseTime = time.Hour * 24 + s.conf.leaseTime = timeutil.Day s.conf.LeaseDuration = uint32(s.conf.leaseTime.Seconds()) } else { s.conf.leaseTime = time.Second * time.Duration(conf.LeaseDuration) diff --git a/internal/dnsforward/config.go b/internal/dnsforward/config.go index eba5b23d..0fdbf00b 100644 --- a/internal/dnsforward/config.go +++ b/internal/dnsforward/config.go @@ -11,7 +11,6 @@ import ( "strings" "time" - "github.com/AdguardTeam/AdGuardHome/internal/aghtime" "github.com/AdguardTeam/AdGuardHome/internal/filtering" "github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/dnsproxy/upstream" @@ -19,6 +18,7 @@ import ( "github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/stringutil" + "github.com/AdguardTeam/golibs/timeutil" "github.com/ameshkov/dnscrypt/v2" ) @@ -90,7 +90,7 @@ type FilteringConfig struct { FastestAddr bool `yaml:"fastest_addr"` // use Fastest Address algorithm // FastestTimeout replaces the default timeout for dialing IP addresses // when FastestAddr is true. - FastestTimeout aghtime.Duration `yaml:"fastest_timeout"` + FastestTimeout timeutil.Duration `yaml:"fastest_timeout"` // Access settings // -- diff --git a/internal/dnsforward/dnsforward_test.go b/internal/dnsforward/dnsforward_test.go index 8552ea7c..397f496e 100644 --- a/internal/dnsforward/dnsforward_test.go +++ b/internal/dnsforward/dnsforward_test.go @@ -23,6 +23,7 @@ import ( "github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/dnsproxy/upstream" "github.com/AdguardTeam/golibs/errors" + "github.com/AdguardTeam/golibs/timeutil" "github.com/miekg/dns" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -107,7 +108,7 @@ func createServerTLSConfig(t *testing.T) (*tls.Config, []byte, []byte) { require.NoErrorf(t, err, "failed to generate serial number: %s", err) notBefore := time.Now() - notAfter := notBefore.Add(5 * 365 * time.Hour * 24) + notAfter := notBefore.Add(5 * 365 * timeutil.Day) template := x509.Certificate{ SerialNumber: serialNumber, diff --git a/internal/home/auth.go b/internal/home/auth.go index a2c36c80..bfcc7267 100644 --- a/internal/home/auth.go +++ b/internal/home/auth.go @@ -15,12 +15,13 @@ import ( "github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/netutil" + "github.com/AdguardTeam/golibs/timeutil" "go.etcd.io/bbolt" "golang.org/x/crypto/bcrypt" ) // cookieTTL is the time-to-live of the session cookie. -const cookieTTL = 365 * 24 * time.Hour +const cookieTTL = 365 * timeutil.Day // sessionCookieName is the name of the session cookie. const sessionCookieName = "agh_session" diff --git a/internal/home/config.go b/internal/home/config.go index f7cbd0be..44afe953 100644 --- a/internal/home/config.go +++ b/internal/home/config.go @@ -6,9 +6,7 @@ import ( "os" "path/filepath" "sync" - "time" - "github.com/AdguardTeam/AdGuardHome/internal/aghtime" "github.com/AdguardTeam/AdGuardHome/internal/dhcpd" "github.com/AdguardTeam/AdGuardHome/internal/dnsforward" "github.com/AdguardTeam/AdGuardHome/internal/filtering" @@ -18,6 +16,7 @@ import ( "github.com/AdguardTeam/dnsproxy/fastip" "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/log" + "github.com/AdguardTeam/golibs/timeutil" "github.com/google/renameio/maybe" yaml "gopkg.in/yaml.v2" ) @@ -108,9 +107,9 @@ type dnsConfig struct { QueryLogEnabled bool `yaml:"querylog_enabled"` // if true, query log is enabled QueryLogFileEnabled bool `yaml:"querylog_file_enabled"` // if true, query log will be written to a file // QueryLogInterval is the interval for query log's files rotation. - QueryLogInterval aghtime.Duration `yaml:"querylog_interval"` - QueryLogMemSize uint32 `yaml:"querylog_size_memory"` // number of entries kept in memory before they are flushed to disk - AnonymizeClientIP bool `yaml:"anonymize_client_ip"` // anonymize clients' IP addresses in logs and stats + QueryLogInterval timeutil.Duration `yaml:"querylog_interval"` + QueryLogMemSize uint32 `yaml:"querylog_size_memory"` // number of entries kept in memory before they are flushed to disk + AnonymizeClientIP bool `yaml:"anonymize_client_ip"` // anonymize clients' IP addresses in logs and stats dnsforward.FilteringConfig `yaml:",inline"` @@ -119,7 +118,7 @@ type dnsConfig struct { DnsfilterConf filtering.Config `yaml:",inline"` // UpstreamTimeout is the timeout for querying upstream servers. - UpstreamTimeout aghtime.Duration `yaml:"upstream_timeout"` + UpstreamTimeout timeutil.Duration `yaml:"upstream_timeout"` // LocalDomainName is the domain name used for known internal hosts. // For example, a machine called "myhost" can be addressed as @@ -182,7 +181,7 @@ var config = &configuration{ Ratelimit: 20, RefuseAny: true, AllServers: false, - FastestTimeout: aghtime.Duration{ + FastestTimeout: timeutil.Duration{ Duration: fastip.DefaultPingWaitTimeout, }, @@ -196,7 +195,7 @@ var config = &configuration{ }, FilteringEnabled: true, // whether or not use filter lists FiltersUpdateIntervalHours: 24, - UpstreamTimeout: aghtime.Duration{Duration: dnsforward.DefaultTimeout}, + UpstreamTimeout: timeutil.Duration{Duration: dnsforward.DefaultTimeout}, LocalDomainName: "lan", ResolveClients: true, UsePrivateRDNS: true, @@ -223,7 +222,7 @@ func initConfig() { config.DNS.QueryLogEnabled = true config.DNS.QueryLogFileEnabled = true - config.DNS.QueryLogInterval = aghtime.Duration{Duration: 90 * 24 * time.Hour} + config.DNS.QueryLogInterval = timeutil.Duration{Duration: 90 * timeutil.Day} config.DNS.QueryLogMemSize = 1000 config.DNS.CacheSize = 4 * 1024 * 1024 @@ -292,7 +291,7 @@ func parseConfig() error { } if config.DNS.UpstreamTimeout.Duration == 0 { - config.DNS.UpstreamTimeout = aghtime.Duration{Duration: dnsforward.DefaultTimeout} + config.DNS.UpstreamTimeout = timeutil.Duration{Duration: dnsforward.DefaultTimeout} } return nil @@ -339,7 +338,7 @@ func (c *configuration) write() error { Context.queryLog.WriteDiskConfig(&dc) config.DNS.QueryLogEnabled = dc.Enabled config.DNS.QueryLogFileEnabled = dc.FileEnabled - config.DNS.QueryLogInterval = aghtime.Duration{Duration: dc.RotationIvl} + config.DNS.QueryLogInterval = timeutil.Duration{Duration: dc.RotationIvl} config.DNS.QueryLogMemSize = dc.MemSize config.DNS.AnonymizeClientIP = dc.AnonymizeClientIP } diff --git a/internal/home/upgrade.go b/internal/home/upgrade.go index 8f587e71..89d41556 100644 --- a/internal/home/upgrade.go +++ b/internal/home/upgrade.go @@ -11,10 +11,10 @@ import ( "strings" "time" - "github.com/AdguardTeam/AdGuardHome/internal/aghtime" "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/netutil" + "github.com/AdguardTeam/golibs/timeutil" "github.com/google/renameio/maybe" "golang.org/x/crypto/bcrypt" yaml "gopkg.in/yaml.v2" @@ -685,7 +685,7 @@ func upgradeSchema11to12(diskConf yobj) (err error) { } } - dns[field] = aghtime.Duration{Duration: time.Duration(qlogIvl) * 24 * time.Hour} + dns[field] = timeutil.Duration{Duration: time.Duration(qlogIvl) * timeutil.Day} return nil } diff --git a/internal/home/upgrade_test.go b/internal/home/upgrade_test.go index c4da5a58..c5fe0bbb 100644 --- a/internal/home/upgrade_test.go +++ b/internal/home/upgrade_test.go @@ -4,7 +4,7 @@ import ( "testing" "time" - "github.com/AdguardTeam/AdGuardHome/internal/aghtime" + "github.com/AdguardTeam/golibs/timeutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -426,7 +426,7 @@ func TestUpgradeSchema11to12(t *testing.T) { name string }{{ ivl: 1, - want: aghtime.Duration{Duration: 24 * time.Hour}, + want: timeutil.Duration{Duration: timeutil.Day}, wantErr: "", name: "success", }, { @@ -463,8 +463,8 @@ func TestUpgradeSchema11to12(t *testing.T) { newDNSConf, ok = dnsVal.(yobj) require.True(t, ok) - var newIvl aghtime.Duration - newIvl, ok = newDNSConf["querylog_interval"].(aghtime.Duration) + var newIvl timeutil.Duration + newIvl, ok = newDNSConf["querylog_interval"].(timeutil.Duration) require.True(t, ok) assert.Equal(t, tc.want, newIvl) @@ -505,8 +505,8 @@ func TestUpgradeSchema11to12(t *testing.T) { ivl, ok = dnsVal["querylog_interval"] require.True(t, ok) - var ivlVal aghtime.Duration - ivlVal, ok = ivl.(aghtime.Duration) + var ivlVal timeutil.Duration + ivlVal, ok = ivl.(timeutil.Duration) require.True(t, ok) assert.Equal(t, 90*24*time.Hour, ivlVal.Duration) diff --git a/internal/querylog/qlog.go b/internal/querylog/qlog.go index f836aa8c..c3cdedfa 100644 --- a/internal/querylog/qlog.go +++ b/internal/querylog/qlog.go @@ -12,6 +12,7 @@ import ( "github.com/AdguardTeam/AdGuardHome/internal/filtering" "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/log" + "github.com/AdguardTeam/golibs/timeutil" "github.com/miekg/dns" ) @@ -103,11 +104,11 @@ func (l *queryLog) Close() { func checkInterval(ivl time.Duration) (ok bool) { // The constants for possible values of query log's rotation interval. const ( - quarterDay = 6 * time.Hour - day = 24 * time.Hour - week = day * 7 - month = day * 30 - threeMonths = day * 90 + quarterDay = timeutil.Day / 4 + day = timeutil.Day + week = timeutil.Day * 7 + month = timeutil.Day * 30 + threeMonths = timeutil.Day * 90 ) return ivl == quarterDay || ivl == day || ivl == week || ivl == month || ivl == threeMonths diff --git a/internal/querylog/qlog_test.go b/internal/querylog/qlog_test.go index 9a641ae4..b6617cd3 100644 --- a/internal/querylog/qlog_test.go +++ b/internal/querylog/qlog_test.go @@ -11,6 +11,7 @@ import ( "github.com/AdguardTeam/AdGuardHome/internal/aghtest" "github.com/AdguardTeam/AdGuardHome/internal/filtering" "github.com/AdguardTeam/dnsproxy/proxyutil" + "github.com/AdguardTeam/golibs/timeutil" "github.com/miekg/dns" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -26,7 +27,7 @@ func TestQueryLog(t *testing.T) { l := newQueryLog(Config{ Enabled: true, FileEnabled: true, - RotationIvl: 24 * time.Hour, + RotationIvl: timeutil.Day, MemSize: 100, BaseDir: t.TempDir(), }) @@ -128,7 +129,7 @@ func TestQueryLog(t *testing.T) { func TestQueryLogOffsetLimit(t *testing.T) { l := newQueryLog(Config{ Enabled: true, - RotationIvl: 24 * time.Hour, + RotationIvl: timeutil.Day, MemSize: 100, BaseDir: t.TempDir(), }) @@ -203,7 +204,7 @@ func TestQueryLogMaxFileScanEntries(t *testing.T) { l := newQueryLog(Config{ Enabled: true, FileEnabled: true, - RotationIvl: 24 * time.Hour, + RotationIvl: timeutil.Day, MemSize: 100, BaseDir: t.TempDir(), }) @@ -231,7 +232,7 @@ func TestQueryLogFileDisabled(t *testing.T) { l := newQueryLog(Config{ Enabled: true, FileEnabled: false, - RotationIvl: 24 * time.Hour, + RotationIvl: timeutil.Day, MemSize: 2, BaseDir: t.TempDir(), }) diff --git a/internal/querylog/querylog.go b/internal/querylog/querylog.go index b0403fdf..b043ef71 100644 --- a/internal/querylog/querylog.go +++ b/internal/querylog/querylog.go @@ -9,6 +9,7 @@ import ( "github.com/AdguardTeam/AdGuardHome/internal/filtering" "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/log" + "github.com/AdguardTeam/golibs/timeutil" "github.com/miekg/dns" ) @@ -125,7 +126,7 @@ func newQueryLog(conf Config) (l *queryLog) { "querylog: warning: unsupported rotation interval %d, setting to 1 day", conf.RotationIvl, ) - l.conf.RotationIvl = 24 * time.Hour + l.conf.RotationIvl = timeutil.Day } return l diff --git a/internal/querylog/querylogfile.go b/internal/querylog/querylogfile.go index 4acaab1f..dbf2f59f 100644 --- a/internal/querylog/querylogfile.go +++ b/internal/querylog/querylogfile.go @@ -8,6 +8,7 @@ import ( "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/log" + "github.com/AdguardTeam/golibs/timeutil" ) // flushLogBuffer flushes the current buffer to file and resets the current buffer @@ -150,6 +151,6 @@ func (l *queryLog) periodicRotate() { } // What? - time.Sleep(24 * time.Hour) + time.Sleep(timeutil.Day) } } diff --git a/internal/querylog/search_test.go b/internal/querylog/search_test.go index e2934cea..7d5444ba 100644 --- a/internal/querylog/search_test.go +++ b/internal/querylog/search_test.go @@ -5,6 +5,7 @@ import ( "testing" "time" + "github.com/AdguardTeam/golibs/timeutil" "github.com/miekg/dns" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -37,7 +38,7 @@ func TestQueryLog_Search_findClient(t *testing.T) { l := newQueryLog(Config{ FindClient: findClient, BaseDir: t.TempDir(), - RotationIvl: 24 * time.Hour, + RotationIvl: timeutil.Day, MemSize: 100, Enabled: true, FileEnabled: true,