2020-12-07 04:32:06 -07:00
|
|
|
// Package stats provides units for managing statistics of the filtering DNS
|
|
|
|
// server.
|
2019-08-22 06:34:58 -07:00
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2019-09-25 05:36:09 -07:00
|
|
|
"net/http"
|
2019-08-22 06:34:58 -07:00
|
|
|
)
|
|
|
|
|
2019-09-10 07:59:10 -07:00
|
|
|
type unitIDCallback func() uint32
|
2019-08-22 06:34:58 -07:00
|
|
|
|
2019-09-25 05:36:09 -07:00
|
|
|
// DiskConfig - configuration settings that are stored on disk
|
|
|
|
type DiskConfig struct {
|
|
|
|
Interval uint32 `yaml:"statistics_interval"` // time interval for statistics (in days)
|
|
|
|
}
|
|
|
|
|
2019-09-16 06:14:52 -07:00
|
|
|
// Config - module configuration
|
|
|
|
type Config struct {
|
2020-03-03 10:21:53 -07:00
|
|
|
Filename string // database file name
|
|
|
|
LimitDays uint32 // time limit (in days)
|
|
|
|
UnitID unitIDCallback // user function to get the current unit ID. If nil, the current time hour is used.
|
|
|
|
AnonymizeClientIP bool // anonymize clients' IP addresses
|
2019-09-25 05:36:09 -07:00
|
|
|
|
|
|
|
// Called when the configuration is changed by HTTP request
|
|
|
|
ConfigModified func()
|
|
|
|
|
|
|
|
// Register an HTTP handler
|
|
|
|
HTTPRegister func(string, string, func(http.ResponseWriter, *http.Request))
|
2019-12-11 02:38:58 -07:00
|
|
|
|
|
|
|
limit uint32 // maximum time we need to keep data for (in hours)
|
2019-09-16 06:14:52 -07:00
|
|
|
}
|
|
|
|
|
2019-08-22 06:34:58 -07:00
|
|
|
// New - create object
|
2019-09-16 06:14:52 -07:00
|
|
|
func New(conf Config) (Stats, error) {
|
|
|
|
return createObject(conf)
|
2019-08-22 06:34:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stats - main interface
|
|
|
|
type Stats interface {
|
2020-01-16 04:25:40 -07:00
|
|
|
Start()
|
|
|
|
|
2019-08-22 06:34:58 -07:00
|
|
|
// Close object.
|
|
|
|
// This function is not thread safe
|
|
|
|
// (can't be called in parallel with any other function of this interface).
|
|
|
|
Close()
|
|
|
|
|
|
|
|
// Update counters
|
|
|
|
Update(e Entry)
|
|
|
|
|
2019-10-07 05:56:33 -07:00
|
|
|
// Get IP addresses of the clients with the most number of requests
|
2021-01-20 07:27:53 -07:00
|
|
|
GetTopClientsIP(limit uint) []net.IP
|
2019-10-07 05:56:33 -07:00
|
|
|
|
2019-09-25 05:36:09 -07:00
|
|
|
// WriteDiskConfig - write configuration
|
|
|
|
WriteDiskConfig(dc *DiskConfig)
|
2019-08-22 06:34:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TimeUnit - time unit
|
|
|
|
type TimeUnit int
|
|
|
|
|
|
|
|
// Supported time units
|
|
|
|
const (
|
|
|
|
Hours TimeUnit = iota
|
|
|
|
Days
|
|
|
|
)
|
|
|
|
|
|
|
|
// Result of DNS request processing
|
|
|
|
type Result int
|
|
|
|
|
|
|
|
// Supported result values
|
|
|
|
const (
|
|
|
|
RNotFiltered Result = iota + 1
|
|
|
|
RFiltered
|
|
|
|
RSafeBrowsing
|
|
|
|
RSafeSearch
|
|
|
|
RParental
|
|
|
|
rLast
|
|
|
|
)
|
|
|
|
|
2021-01-27 08:32:13 -07:00
|
|
|
// Entry is a statistics data entry.
|
2019-08-22 06:34:58 -07:00
|
|
|
type Entry struct {
|
2021-01-27 08:32:13 -07:00
|
|
|
// Clients is the client's primary ID.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Make this a {net.IP, string} enum?
|
|
|
|
Client string
|
|
|
|
|
2019-08-22 06:34:58 -07:00
|
|
|
Domain string
|
|
|
|
Result Result
|
2019-09-10 07:59:10 -07:00
|
|
|
Time uint32 // processing time (msec)
|
2019-08-22 06:34:58 -07:00
|
|
|
}
|