AdGuardHome/internal/dhcpd/db.go
Eugene Burkov 94cf50a53d Pull request 1955: Upd libraries
Merge in DNS/adguard-home from upd-libs to master

Squashed commit of the following:

commit 72f4fb7d5ab8eaecb917a843dec56e3fa38b725c
Merge: 8defe106f 111005b8d
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Aug 10 19:50:33 2023 +0300

    Merge branch 'master' into upd-libs

commit 8defe106fc982ba916f42d5722f1cc0152042d04
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Aug 10 19:38:01 2023 +0300

    dnsforward: revert behavior

commit 8ccb2f675373772f8a6f829b80b317fda0c5f730
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 9 18:35:12 2023 +0300

    all: imp code

commit a3112d3438a466bae515e56a6ee97394066ba481
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 9 17:26:52 2023 +0300

    filtering: revert type

commit 56d2528fb4c8ee5504de0c6ec24050d63c8e6330
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 9 14:57:04 2023 +0300

    stats: fix sort

commit 0dbb446602b7536df508a8e53c4a532455c9d064
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Aug 9 14:52:28 2023 +0300

    all: upd golibs & dnsproxy
2023-08-10 20:00:17 +03:00

127 lines
2.7 KiB
Go

// On-disk database for lease table
package dhcpd
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/google/renameio/v2/maybe"
"golang.org/x/exp/slices"
)
const (
// dataFilename contains saved leases.
dataFilename = "leases.json"
// dataVersion is the current version of the stored DHCP leases structure.
dataVersion = 1
)
// dataLeases is the structure of the stored DHCP leases.
type dataLeases struct {
// Version is the current version of the structure.
Version int `json:"version"`
// Leases is the list containing stored DHCP leases.
Leases []*Lease `json:"leases"`
}
// dbLoad loads stored leases.
func (s *server) dbLoad() (err error) {
data, err := os.ReadFile(s.conf.dbFilePath)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("reading db: %w", err)
}
return nil
}
dl := &dataLeases{}
err = json.Unmarshal(data, dl)
if err != nil {
return fmt.Errorf("decoding db: %w", err)
}
leases := dl.Leases
leases4 := []*Lease{}
leases6 := []*Lease{}
for _, l := range leases {
if l.IP.Is4() {
leases4 = append(leases4, l)
} else {
leases6 = append(leases6, l)
}
}
err = s.srv4.ResetLeases(leases4)
if err != nil {
return fmt.Errorf("resetting dhcpv4 leases: %w", err)
}
if s.srv6 != nil {
err = s.srv6.ResetLeases(leases6)
if err != nil {
return fmt.Errorf("resetting dhcpv6 leases: %w", err)
}
}
log.Info("dhcp: loaded leases v4:%d v6:%d total-read:%d from DB",
len(leases4), len(leases6), len(leases))
return nil
}
// dbStore stores DHCP leases.
func (s *server) dbStore() (err error) {
// Use an empty slice here as opposed to nil so that it doesn't write
// "null" into the database file if leases are empty.
leases := []*Lease{}
leases4 := s.srv4.getLeasesRef()
leases = append(leases, leases4...)
if s.srv6 != nil {
leases6 := s.srv6.getLeasesRef()
leases = append(leases, leases6...)
}
return writeDB(s.conf.dbFilePath, leases)
}
// writeDB writes leases to file at path.
func writeDB(path string, leases []*Lease) (err error) {
defer func() { err = errors.Annotate(err, "writing db: %w") }()
slices.SortFunc(leases, func(a, b *Lease) (res int) {
return strings.Compare(a.Hostname, b.Hostname)
})
dl := &dataLeases{
Version: dataVersion,
Leases: leases,
}
buf, err := json.Marshal(dl)
if err != nil {
// Don't wrap the error since it's informative enough as is.
return err
}
err = maybe.WriteFile(path, buf, 0o644)
if err != nil {
// Don't wrap the error since it's informative enough as is.
return err
}
log.Info("dhcp: stored %d leases in %q", len(leases), path)
return nil
}