From a0abad6644d1cbe80d9293b6742e38c4c6ddc282 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Thu, 18 Feb 2021 14:11:54 +0300 Subject: [PATCH] Pull request: 2692 time format Merge in DNS/adguard-home from 2692-time-format to master Closes #2692. Squashed commit of the following: commit a77e3ffc1e2ca7ad1eb4b56641eee787595268e9 Merge: 5262c0b9 1122e71c Author: Eugene Burkov Date: Thu Feb 18 13:58:29 2021 +0300 Merge branch 'master' into 2692-time-format commit 5262c0b95979a6c7a01bdd56e9476c4cdf217119 Author: Eugene Burkov Date: Thu Feb 18 13:50:38 2021 +0300 dhcpd: imp docs commit 3744338d51dd003a0052672ceffa260c70f5738d Author: Eugene Burkov Date: Wed Feb 17 21:10:37 2021 +0300 dhcpd: fix lease time format --- CHANGELOG.md | 2 ++ internal/dhcpd/dhcpd.go | 31 ++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33a36ee4..0d47edd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,11 +19,13 @@ and this project adheres to ### Fixed +- DHCP lease's `expired` field incorrect time format ([#2692]). - Incomplete DNS upstreams validation ([#2674]). - Wrong parsing of DHCP options of the `ip` type ([#2688]). [#2674]: https://github.com/AdguardTeam/AdGuardHome/issues/2674 [#2688]: https://github.com/AdguardTeam/AdGuardHome/issues/2688 +[#2692]: https://github.com/AdguardTeam/AdGuardHome/issues/2692 diff --git a/internal/dhcpd/dhcpd.go b/internal/dhcpd/dhcpd.go index e1d5ab1b..7a48d239 100644 --- a/internal/dhcpd/dhcpd.go +++ b/internal/dhcpd/dhcpd.go @@ -19,7 +19,12 @@ import ( const ( defaultDiscoverTime = time.Second * 3 - leaseExpireStatic = 1 + // leaseExpireStatic is used to define the Expiry field for static + // leases. + // + // TODO(e.burkov): Remove it when static leases determining mechanism + // will be improved. + leaseExpireStatic = 1 ) var webHandlersRegistered = false @@ -37,12 +42,24 @@ type Lease struct { // MarshalJSON implements the json.Marshaler interface for *Lease. func (l *Lease) MarshalJSON() ([]byte, error) { + var expiryStr string + if expiry := l.Expiry; expiry.Unix() != leaseExpireStatic { + // The front-end is waiting for RFC 3999 format of the time + // value. It also shouldn't got an Expiry field for static + // leases. + // + // See https://github.com/AdguardTeam/AdGuardHome/issues/2692. + expiryStr = expiry.Format(time.RFC3339) + } + type lease Lease return json.Marshal(&struct { HWAddr string `json:"mac"` + Expiry string `json:"expires,omitempty"` *lease }{ HWAddr: l.HWAddr.String(), + Expiry: expiryStr, lease: (*lease)(l), }) } @@ -248,14 +265,10 @@ const ( LeasesAll = LeasesDynamic | LeasesStatic ) -// Leases returns the list of current DHCP leases (thread-safe) -func (s *Server) Leases(flags int) []Lease { - result := s.srv4.GetLeases(flags) - - v6leases := s.srv6.GetLeases(flags) - result = append(result, v6leases...) - - return result +// Leases returns the list of active IPv4 and IPv6 DHCP leases. It's safe for +// concurrent use. +func (s *Server) Leases(flags int) (leases []Lease) { + return append(s.srv4.GetLeases(flags), s.srv6.GetLeases(flags)...) } // FindMACbyIP - find a MAC address by IP address in the currently active DHCP leases