From a9127c4a45fab3707acbc60cdb6470111633ed4c Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Wed, 31 Aug 2022 18:57:02 +0300 Subject: [PATCH] Pull request: upd-fmt Merge in DNS/adguard-home from upd-fmt to master Squashed commit of the following: commit 73d9a71feccd8256841ae5a01e4a6aae0bcb2f65 Author: Ainar Garipov Date: Wed Aug 31 18:37:39 2022 +0300 all: upd fmt --- internal/aghnet/arpdb_bsd.go | 3 +- internal/aghnet/arpdb_linux.go | 11 +- internal/aghnet/arpdb_openbsd.go | 5 +- internal/aghnet/arpdb_windows.go | 10 +- internal/aghnet/hostgen.go | 4 +- internal/aghnet/hostscontainer.go | 3 +- internal/aghnet/ipset.go | 2 +- internal/aghnet/systemresolvers_windows.go | 5 +- internal/aghos/os.go | 11 +- internal/dhcpd/routeradv.go | 67 ++++----- internal/home/filter.go | 34 ++--- internal/home/home.go | 7 +- internal/home/service.go | 24 ++-- internal/home/tls.go | 7 +- internal/home/upgrade.go | 154 ++++++++++----------- internal/stats/stats.go | 6 +- internal/stats/unit.go | 46 +++--- 17 files changed, 190 insertions(+), 209 deletions(-) diff --git a/internal/aghnet/arpdb_bsd.go b/internal/aghnet/arpdb_bsd.go index 26ac7758..317579de 100644 --- a/internal/aghnet/arpdb_bsd.go +++ b/internal/aghnet/arpdb_bsd.go @@ -33,8 +33,7 @@ func newARPDB() (arp *cmdARPDB) { // parseArpA parses the output of the "arp -a -n" command on macOS and FreeBSD. // The expected input format: // -// host.name (192.168.0.1) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet] -// +// host.name (192.168.0.1) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet] func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) { ns = make([]Neighbor, 0, lenHint) for sc.Scan() { diff --git a/internal/aghnet/arpdb_linux.go b/internal/aghnet/arpdb_linux.go index 9cc38906..e8b34a2f 100644 --- a/internal/aghnet/arpdb_linux.go +++ b/internal/aghnet/arpdb_linux.go @@ -117,9 +117,8 @@ func (arp *fsysARPDB) Neighbors() (ns []Neighbor) { // parseArpAWrt parses the output of the "arp -a -n" command on OpenWrt. The // expected input format: // -// IP address HW type Flags HW address Mask Device -// 192.168.11.98 0x1 0x2 5a:92:df:a9:7e:28 * wan -// +// IP address HW type Flags HW address Mask Device +// 192.168.11.98 0x1 0x2 5a:92:df:a9:7e:28 * wan func parseArpAWrt(sc *bufio.Scanner, lenHint int) (ns []Neighbor) { if !sc.Scan() { // Skip the header. @@ -161,8 +160,7 @@ func parseArpAWrt(sc *bufio.Scanner, lenHint int) (ns []Neighbor) { // parseArpA parses the output of the "arp -a -n" command on Linux. The // expected input format: // -// hostname (192.168.1.1) at ab:cd:ef:ab:cd:ef [ether] on enp0s3 -// +// hostname (192.168.1.1) at ab:cd:ef:ab:cd:ef [ether] on enp0s3 func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) { ns = make([]Neighbor, 0, lenHint) for sc.Scan() { @@ -208,8 +206,7 @@ func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) { // parseIPNeigh parses the output of the "ip neigh" command on Linux. The // expected input format: // -// 192.168.1.1 dev enp0s3 lladdr ab:cd:ef:ab:cd:ef REACHABLE -// +// 192.168.1.1 dev enp0s3 lladdr ab:cd:ef:ab:cd:ef REACHABLE func parseIPNeigh(sc *bufio.Scanner, lenHint int) (ns []Neighbor) { ns = make([]Neighbor, 0, lenHint) for sc.Scan() { diff --git a/internal/aghnet/arpdb_openbsd.go b/internal/aghnet/arpdb_openbsd.go index d5ec5fea..e1626c0b 100644 --- a/internal/aghnet/arpdb_openbsd.go +++ b/internal/aghnet/arpdb_openbsd.go @@ -32,9 +32,8 @@ func newARPDB() (arp *cmdARPDB) { // parseArpA parses the output of the "arp -a -n" command on OpenBSD. The // expected input format: // -// Host Ethernet Address Netif Expire Flags -// 192.168.1.1 ab:cd:ef:ab:cd:ef em0 19m59s -// +// Host Ethernet Address Netif Expire Flags +// 192.168.1.1 ab:cd:ef:ab:cd:ef em0 19m59s func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) { // Skip the header. if !sc.Scan() { diff --git a/internal/aghnet/arpdb_windows.go b/internal/aghnet/arpdb_windows.go index 8d5431eb..e81f6818 100644 --- a/internal/aghnet/arpdb_windows.go +++ b/internal/aghnet/arpdb_windows.go @@ -25,12 +25,10 @@ func newARPDB() (arp *cmdARPDB) { // parseArpA parses the output of the "arp /a" command on Windows. The expected // input format (the first line is empty): // -// -// Interface: 192.168.56.16 --- 0x7 -// Internet Address Physical Address Type -// 192.168.56.1 0a-00-27-00-00-00 dynamic -// 192.168.56.255 ff-ff-ff-ff-ff-ff static -// +// Interface: 192.168.56.16 --- 0x7 +// Internet Address Physical Address Type +// 192.168.56.1 0a-00-27-00-00-00 dynamic +// 192.168.56.255 ff-ff-ff-ff-ff-ff static func parseArpA(sc *bufio.Scanner, lenHint int) (ns []Neighbor) { ns = make([]Neighbor, 0, lenHint) for sc.Scan() { diff --git a/internal/aghnet/hostgen.go b/internal/aghnet/hostgen.go index 683c8d9f..f19001cd 100644 --- a/internal/aghnet/hostgen.go +++ b/internal/aghnet/hostgen.go @@ -45,11 +45,11 @@ func generateIPv6Hostname(ipv6 net.IP) (hostname string) { // GenerateHostname generates the hostname from ip. In case of using IPv4 the // result should be like: // -// 192-168-10-1 +// 192-168-10-1 // // In case of using IPv6, the result is like: // -// ff80-f076-0000-0000-0000-0000-0000-0010 +// ff80-f076-0000-0000-0000-0000-0000-0010 // // ip must be either an IPv4 or an IPv6. func GenerateHostname(ip net.IP) (hostname string) { diff --git a/internal/aghnet/hostscontainer.go b/internal/aghnet/hostscontainer.go index ee345905..5d750945 100644 --- a/internal/aghnet/hostscontainer.go +++ b/internal/aghnet/hostscontainer.go @@ -70,8 +70,7 @@ func (rm *requestMatcher) MatchRequest( // rule or an empty string if the last doesn't exist. The returned rules are in // a processed format like: // -// ip host1 host2 ... -// +// ip host1 host2 ... func (rm *requestMatcher) Translate(rule string) (hostRule string) { rm.stateLock.RLock() defer rm.stateLock.RUnlock() diff --git a/internal/aghnet/ipset.go b/internal/aghnet/ipset.go index 88a3f2b7..03e8a758 100644 --- a/internal/aghnet/ipset.go +++ b/internal/aghnet/ipset.go @@ -18,7 +18,7 @@ type IpsetManager interface { // // The syntax of the ipsetConf is: // -// DOMAIN[,DOMAIN].../IPSET_NAME[,IPSET_NAME]... +// DOMAIN[,DOMAIN].../IPSET_NAME[,IPSET_NAME]... // // If ipsetConf is empty, msg and err are nil. The error is of type // *aghos.UnsupportedError if the OS is not supported. diff --git a/internal/aghnet/systemresolvers_windows.go b/internal/aghnet/systemresolvers_windows.go index f82d6e7e..2e8ed3df 100644 --- a/internal/aghnet/systemresolvers_windows.go +++ b/internal/aghnet/systemresolvers_windows.go @@ -62,9 +62,8 @@ func writeExit(w io.WriteCloser) { // scanAddrs scans the DNS addresses from nslookup's output. The expected // output of nslookup looks like this: // -// Default Server: 192-168-1-1.qualified.domain.ru -// Address: 192.168.1.1 -// +// Default Server: 192-168-1-1.qualified.domain.ru +// Address: 192.168.1.1 func scanAddrs(s *bufio.Scanner) (addrs []string) { for s.Scan() { line := strings.TrimSpace(s.Text()) diff --git a/internal/aghos/os.go b/internal/aghos/os.go index 3b688749..b39ecbbd 100644 --- a/internal/aghos/os.go +++ b/internal/aghos/os.go @@ -121,13 +121,12 @@ func PIDByCommand(command string, except ...int) (pid int, err error) { } // parsePSOutput scans the output of ps searching the largest PID of the process -// associated with cmdName ignoring PIDs from ignore. A valid line from -// r should look like these: -// -// 123 ./example-cmd -// 1230 some/base/path/example-cmd -// 3210 example-cmd +// associated with cmdName ignoring PIDs from ignore. A valid line from r +// should look like these: // +// 123 ./example-cmd +// 1230 some/base/path/example-cmd +// 3210 example-cmd func parsePSOutput(r io.Reader, cmdName string, ignore []int) (largest, instNum int, err error) { s := bufio.NewScanner(r) for s.Scan() { diff --git a/internal/dhcpd/routeradv.go b/internal/dhcpd/routeradv.go index 863ac5e1..9c87ca9f 100644 --- a/internal/dhcpd/routeradv.go +++ b/internal/dhcpd/routeradv.go @@ -65,39 +65,42 @@ func hwAddrToLinkLayerAddr(hwa net.HardwareAddr) (lla []byte, err error) { } // Create an ICMPv6.RouterAdvertisement packet with all necessary options. +// Data scheme: // -// ICMPv6: -// type[1] -// code[1] -// chksum[2] -// body (RouterAdvertisement): -// Cur Hop Limit[1] -// Flags[1]: MO...... -// Router Lifetime[2] -// Reachable Time[4] -// Retrans Timer[4] -// Option=Prefix Information(3): -// Type[1] -// Length * 8bytes[1] -// Prefix Length[1] -// Flags[1]: LA...... -// Valid Lifetime[4] -// Preferred Lifetime[4] -// Reserved[4] -// Prefix[16] -// Option=MTU(5): -// Type[1] -// Length * 8bytes[1] -// Reserved[2] -// MTU[4] -// Option=Source link-layer address(1): -// Link-Layer Address[8/24] -// Option=Recursive DNS Server(25): -// Type[1] -// Length * 8bytes[1] -// Reserved[2] -// Lifetime[4] -// Addresses of IPv6 Recursive DNS Servers[16] +// ICMPv6: +// - type[1] +// - code[1] +// - chksum[2] +// - body (RouterAdvertisement): +// - Cur Hop Limit[1] +// - Flags[1]: MO...... +// - Router Lifetime[2] +// - Reachable Time[4] +// - Retrans Timer[4] +// - Option=Prefix Information(3): +// - Type[1] +// - Length * 8bytes[1] +// - Prefix Length[1] +// - Flags[1]: LA...... +// - Valid Lifetime[4] +// - Preferred Lifetime[4] +// - Reserved[4] +// - Prefix[16] +// - Option=MTU(5): +// - Type[1] +// - Length * 8bytes[1] +// - Reserved[2] +// - MTU[4] +// - Option=Source link-layer address(1): +// - Link-Layer Address[8/24] +// - Option=Recursive DNS Server(25): +// - Type[1] +// - Length * 8bytes[1] +// - Reserved[2] +// - Lifetime[4] +// - Addresses of IPv6 Recursive DNS Servers[16] +// +// TODO(a.garipov): Replace with an existing implementation from a dependency. func createICMPv6RAPacket(params icmpv6RA) (data []byte, err error) { var lla []byte lla, err = hwAddrToLinkLayerAddr(params.sourceLinkLayerAddress) diff --git a/internal/home/filter.go b/internal/home/filter.go index e7688f3f..78abd76a 100644 --- a/internal/home/filter.go +++ b/internal/home/filter.go @@ -267,7 +267,8 @@ func (f *Filtering) periodicallyRefreshFilters() { // Refresh filters // flags: filterRefresh* // important: -// TRUE: ignore the fact that we're currently updating the filters +// +// TRUE: ignore the fact that we're currently updating the filters func (f *Filtering) refreshFilters(flags int, important bool) (int, error) { set := atomic.CompareAndSwapUint32(&f.refreshStatus, 0, 1) if !important && !set { @@ -363,25 +364,24 @@ const ( filterRefreshBlocklists = 4 // update block-lists ) -// Checks filters updates if necessary -// If force is true, it ignores the filter.LastUpdated field value -// flags: filterRefresh* +// refreshFiltersIfNecessary checks filters and updates them if necessary. If +// force is true, it ignores the filter.LastUpdated field value. // // Algorithm: -// . Get the list of filters to be updated -// . For each filter run the download and checksum check operation -// . Store downloaded data in a temporary file inside data/filters directory -// . For each filter: -// . If filter data hasn't changed, just set new update time on file -// . If filter data has changed: -// . rename the temporary file ( -> 1.txt) -// Note that this method works only on UNIX. -// On Windows we don't pass files to filtering - we pass the whole data. -// . Pass new filters to filtering object - it analyzes new data while the old filters are still active -// . filtering activates new filters // -// Return the number of updated filters -// Return TRUE - there was a network error and nothing could be updated +// 1. Get the list of filters to be updated. For each filter, run the download +// and checksum check operation. Store downloaded data in a temporary file +// inside data/filters directory +// +// 2. For each filter, if filter data hasn't changed, just set new update time +// on file. Otherwise, rename the temporary file ( -> 1.txt). Note +// that this method works only on Unix systems. On Windows, don't pass +// files to filtering, pass the whole data. +// +// refreshFiltersIfNecessary returns the number of updated filters. It also +// returns true if there was a network error and nothing could be updated. +// +// TODO(a.garipov, e.burkov): What the hell? func (f *Filtering) refreshFiltersIfNecessary(flags int) (int, bool) { log.Debug("Filters: updating...") diff --git a/internal/home/home.go b/internal/home/home.go index 29cb6d5d..a84753fd 100644 --- a/internal/home/home.go +++ b/internal/home/home.go @@ -741,11 +741,10 @@ func loadOptions() options { // printWebAddrs prints addresses built from proto, addr, and an appropriate // port. At least one address is printed with the value of port. If the value -// of betaPort is 0, the second address is not printed. The output example: -// -// Go to http://127.0.0.1:80 -// Go to http://127.0.0.1:3000 (BETA) +// of betaPort is 0, the second address is not printed. Output example: // +// Go to http://127.0.0.1:80 +// Go to http://127.0.0.1:3000 (BETA) func printWebAddrs(proto, addr string, port, betaPort int) { const ( hostMsg = "Go to %s://%s" diff --git a/internal/home/service.go b/internal/home/service.go index 831a80d0..20367718 100644 --- a/internal/home/service.go +++ b/internal/home/service.go @@ -159,15 +159,16 @@ func sendSigReload() { } // handleServiceControlAction one of the possible control actions: -// install -- installs a service/daemon -// uninstall -- uninstalls it -// status -- prints the service status -// start -- starts the previously installed service -// stop -- stops the previously installed service -// restart - restarts the previously installed service -// run - this is a special command that is not supposed to be used directly -// it is specified when we register a service, and it indicates to the app -// that it is being run as a service/daemon. +// +// - install: Installs a service/daemon. +// - uninstall: Uninstalls it. +// - status: Prints the service status. +// - start: Starts the previously installed service. +// - stop: Stops the previously installed service. +// - restart: Restarts the previously installed service. +// - run: This is a special command that is not supposed to be used directly +// it is specified when we register a service, and it indicates to the app +// that it is being run as a service/daemon. func handleServiceControlAction(opts options, clientBuildFS fs.FS) { // Call chooseSystem explicitly to introduce OpenBSD support for service // package. It's a noop for other GOOS values. @@ -397,12 +398,11 @@ var launchdConfig = ` // the systemdScript constant in file service_systemd_linux.go in module // github.com/kardianos/service. The following changes have been made: // -// 1. The RestartSec setting is set to a lower value of 10 to make sure we +// 1. The RestartSec setting is set to a lower value of 10 to make sure we // always restart quickly. // -// 2. The ExecStartPre setting is added to make sure that the log directory is +// 2. The ExecStartPre setting is added to make sure that the log directory is // always created to prevent the 209/STDOUT errors. -// const systemdScript = `[Unit] Description={{.Description}} ConditionFileIsExecutable={{.Path|cmdEscape}} diff --git a/internal/home/tls.go b/internal/home/tls.go index 7240496c..b454e152 100644 --- a/internal/home/tls.go +++ b/internal/home/tls.go @@ -568,10 +568,9 @@ func validatePkey(data *tlsConfigStatus, pkey string) error { return nil } -// Process certificate data and its private key. -// All parameters are optional. -// On error, return partially set object -// with 'WarningValidation' field containing error description. +// validateCertificates processes certificate data and its private key. All +// parameters are optional. On error, validateCertificates returns a partially +// set object with field WarningValidation containing error description. func validateCertificates(certChain, pkey, serverName string) tlsConfigStatus { var data tlsConfigStatus diff --git a/internal/home/upgrade.go b/internal/home/upgrade.go index 4426149c..63e17030 100644 --- a/internal/home/upgrade.go +++ b/internal/home/upgrade.go @@ -240,8 +240,9 @@ func upgradeSchema3to4(diskConf yobj) error { // Replace "auth_name", "auth_pass" string settings with an array: // users: -// - name: "..." -// password: "..." +// - name: "..." +// password: "..." +// // ... func upgradeSchema4to5(diskConf yobj) error { log.Printf("%s(): called", funcName()) @@ -288,16 +289,18 @@ func upgradeSchema4to5(diskConf yobj) error { // clients: // ... -// ip: 127.0.0.1 -// mac: ... +// +// ip: 127.0.0.1 +// mac: ... // // -> // // clients: // ... -// ids: -// - 127.0.0.1 -// - ... +// +// ids: +// - 127.0.0.1 +// - ... func upgradeSchema5to6(diskConf yobj) error { log.Printf("%s(): called", funcName()) @@ -355,19 +358,21 @@ func upgradeSchema5to6(diskConf yobj) error { } // dhcp: -// enabled: false -// interface_name: vboxnet0 -// gateway_ip: 192.168.56.1 -// ... +// +// enabled: false +// interface_name: vboxnet0 +// gateway_ip: 192.168.56.1 +// ... // // -> // // dhcp: -// enabled: false -// interface_name: vboxnet0 -// dhcpv4: -// gateway_ip: 192.168.56.1 -// ... +// +// enabled: false +// interface_name: vboxnet0 +// dhcpv4: +// gateway_ip: 192.168.56.1 +// ... func upgradeSchema6to7(diskConf yobj) error { log.Printf("Upgrade yaml: 6 to 7") @@ -443,15 +448,14 @@ func upgradeSchema6to7(diskConf yobj) error { // upgradeSchema7to8 performs the following changes: // -// # BEFORE: -// 'dns': -// 'bind_host': '127.0.0.1' -// -// # AFTER: -// 'dns': -// 'bind_hosts': -// - '127.0.0.1' +// # BEFORE: +// 'dns': +// 'bind_host': '127.0.0.1' // +// # AFTER: +// 'dns': +// 'bind_hosts': +// - '127.0.0.1' func upgradeSchema7to8(diskConf yobj) (err error) { log.Printf("Upgrade yaml: 7 to 8") @@ -481,14 +485,13 @@ func upgradeSchema7to8(diskConf yobj) (err error) { // upgradeSchema8to9 performs the following changes: // -// # BEFORE: -// 'dns': -// 'autohost_tld': 'lan' -// -// # AFTER: -// 'dns': -// 'local_domain_name': 'lan' +// # BEFORE: +// 'dns': +// 'autohost_tld': 'lan' // +// # AFTER: +// 'dns': +// 'local_domain_name': 'lan' func upgradeSchema8to9(diskConf yobj) (err error) { log.Printf("Upgrade yaml: 8 to 9") @@ -564,16 +567,15 @@ func addQUICPort(ups string, port int) (withPort string) { // upgradeSchema9to10 performs the following changes: // -// # BEFORE: -// 'dns': -// 'upstream_dns': -// - 'quic://some-upstream.com' -// -// # AFTER: -// 'dns': -// 'upstream_dns': -// - 'quic://some-upstream.com:784' +// # BEFORE: +// 'dns': +// 'upstream_dns': +// - 'quic://some-upstream.com' // +// # AFTER: +// 'dns': +// 'upstream_dns': +// - 'quic://some-upstream.com:784' func upgradeSchema9to10(diskConf yobj) (err error) { log.Printf("Upgrade yaml: 9 to 10") @@ -623,15 +625,14 @@ func upgradeSchema9to10(diskConf yobj) (err error) { // upgradeSchema10to11 performs the following changes: // -// # BEFORE: -// 'rlimit_nofile': 42 -// -// # AFTER: -// 'os': -// 'group': '' -// 'rlimit_nofile': 42 -// 'user': '' +// # BEFORE: +// 'rlimit_nofile': 42 // +// # AFTER: +// 'os': +// 'group': '' +// 'rlimit_nofile': 42 +// 'user': '' func upgradeSchema10to11(diskConf yobj) (err error) { log.Printf("Upgrade yaml: 10 to 11") @@ -658,12 +659,11 @@ func upgradeSchema10to11(diskConf yobj) (err error) { // upgradeSchema11to12 performs the following changes: // -// # BEFORE: -// 'querylog_interval': 90 -// -// # AFTER: -// 'querylog_interval': '2160h' +// # BEFORE: +// 'querylog_interval': 90 // +// # AFTER: +// 'querylog_interval': '2160h' func upgradeSchema11to12(diskConf yobj) (err error) { log.Printf("Upgrade yaml: 11 to 12") diskConf["schema_version"] = 12 @@ -698,16 +698,15 @@ func upgradeSchema11to12(diskConf yobj) (err error) { // upgradeSchema12to13 performs the following changes: // -// # BEFORE: -// 'dns': -// # … -// 'local_domain_name': 'lan' -// -// # AFTER: -// 'dhcp': -// # … -// 'local_domain_name': 'lan' +// # BEFORE: +// 'dns': +// # … +// 'local_domain_name': 'lan' // +// # AFTER: +// 'dhcp': +// # … +// 'local_domain_name': 'lan' func upgradeSchema12to13(diskConf yobj) (err error) { log.Printf("Upgrade yaml: 12 to 13") diskConf["schema_version"] = 13 @@ -744,23 +743,22 @@ func upgradeSchema12to13(diskConf yobj) (err error) { // upgradeSchema13to14 performs the following changes: // -// # BEFORE: -// 'clients': -// - 'name': 'client-name' -// # … -// -// # AFTER: -// 'clients': -// 'persistent': -// - 'name': 'client-name' -// # … -// 'runtime_sources': -// 'whois': true -// 'arp': true -// 'rdns': true -// 'dhcp': true -// 'hosts': true +// # BEFORE: +// 'clients': +// - 'name': 'client-name' +// # … // +// # AFTER: +// 'clients': +// 'persistent': +// - 'name': 'client-name' +// # … +// 'runtime_sources': +// 'whois': true +// 'arp': true +// 'rdns': true +// 'dhcp': true +// 'hosts': true func upgradeSchema13to14(diskConf yobj) (err error) { log.Printf("Upgrade yaml: 13 to 14") diskConf["schema_version"] = 14 diff --git a/internal/stats/stats.go b/internal/stats/stats.go index aea6d92d..e9397907 100644 --- a/internal/stats/stats.go +++ b/internal/stats/stats.go @@ -423,9 +423,9 @@ func (s *StatsCtx) flush() (cont bool, sleepFor time.Duration) { // periodicFlush checks and flushes the unit to the database if the freshly // generated unit ID differs from the current's ID. Flushing process includes: -// - swapping the current unit with the new empty one; -// - writing the current unit to the database; -// - removing the stale unit from the database. +// - swapping the current unit with the new empty one; +// - writing the current unit to the database; +// - removing the stale unit from the database. func (s *StatsCtx) periodicFlush() { for cont, sleepFor := true, time.Duration(0); cont; time.Sleep(sleepFor) { cont, sleepFor = s.flush() diff --git a/internal/stats/unit.go b/internal/stats/unit.go index 28e0b2bc..0e39cfea 100644 --- a/internal/stats/unit.go +++ b/internal/stats/unit.go @@ -353,33 +353,25 @@ func topsCollector(units []*unitDB, max int, pg pairsGetter) []map[string]uint64 return convertTopSlice(a2) } -/* Algorithm: -. Prepare array of N units, where N is the value of "limit" configuration setting - . Load data for the most recent units from file - If a unit with required ID doesn't exist, just add an empty unit - . Get data for the current unit -. Process data from the units and prepare an output map object: - * per time unit counters: - * DNS-queries/time-unit - * blocked/time-unit - * safebrowsing-blocked/time-unit - * parental-blocked/time-unit - If time-unit is an hour, just add values from each unit to an array. - If time-unit is a day, aggregate per-hour data into days. - * top counters: - * queries/domain - * queries/blocked-domain - * queries/client - To get these values we first sum up data for all units into a single map. - Then we get the pairs with the highest numbers (the values are sorted in descending order) - * total counters: - * DNS-queries - * blocked - * safebrowsing-blocked - * safesearch-blocked - * parental-blocked - These values are just the sum of data for all units. -*/ +// getData returns the statistics data using the following algorithm: +// +// 1. Prepare a slice of N units, where N is the value of "limit" configuration +// setting. Load data for the most recent units from the file. If a unit +// with required ID doesn't exist, just add an empty unit. Get data for the +// current unit. +// +// 2. Process data from the units and prepare an output map object, including +// per time unit counters (DNS queries per time-unit, blocked queries per +// time unit, etc.). If the time unit is hour, just add values from each +// unit to the slice; otherwise, the time unit is day, so aggregate per-hour +// data into days. +// +// To get the top counters (queries per domain, queries per blocked domain, +// etc.), first sum up data for all units into a single map. Then, get the +// pairs with the highest numbers. +// +// The total counters (DNS queries, blocked, etc.) are just the sum of data +// for all units. func (s *StatsCtx) getData(limit uint32) (StatsResp, bool) { if limit == 0 { return StatsResp{