2019-09-27 18:58:57 +03:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-05-26 15:37:37 +03:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2019-09-27 18:58:57 +03:00
|
|
|
"time"
|
|
|
|
|
2019-11-12 15:36:17 +03:00
|
|
|
"github.com/AdguardTeam/golibs/jsonutil"
|
2019-09-27 18:58:57 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2020-05-26 15:37:37 +03:00
|
|
|
type qlogConfig struct {
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
Interval uint32 `json:"interval"`
|
|
|
|
AnonymizeClientIP bool `json:"anonymize_client_ip"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register web handlers
|
|
|
|
func (l *queryLog) initWeb() {
|
2021-02-04 14:15:34 +03:00
|
|
|
l.conf.HTTPRegister(http.MethodGet, "/control/querylog", l.handleQueryLog)
|
|
|
|
l.conf.HTTPRegister(http.MethodGet, "/control/querylog_info", l.handleQueryLogInfo)
|
|
|
|
l.conf.HTTPRegister(http.MethodPost, "/control/querylog_clear", l.handleQueryLogClear)
|
|
|
|
l.conf.HTTPRegister(http.MethodPost, "/control/querylog_config", l.handleQueryLogConfig)
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
2019-09-27 18:58:57 +03:00
|
|
|
func httpError(r *http.Request, w http.ResponseWriter, code int, format string, args ...interface{}) {
|
|
|
|
text := fmt.Sprintf(format, args...)
|
|
|
|
|
|
|
|
log.Info("QueryLog: %s %s: %s", r.Method, r.URL, text)
|
|
|
|
|
|
|
|
http.Error(w, text, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) handleQueryLog(w http.ResponseWriter, r *http.Request) {
|
2020-05-26 15:37:37 +03:00
|
|
|
params, err := l.parseSearchParams(r)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "failed to parse params: %s", err)
|
|
|
|
return
|
2019-09-27 18:58:57 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 15:37:37 +03:00
|
|
|
// search for the log entries
|
|
|
|
entries, oldest := l.search(params)
|
2019-09-27 18:58:57 +03:00
|
|
|
|
2020-05-26 15:37:37 +03:00
|
|
|
// convert log entries to JSON
|
2020-11-30 13:32:58 +03:00
|
|
|
data := l.entriesToJSON(entries, oldest)
|
2019-09-27 18:58:57 +03:00
|
|
|
|
|
|
|
jsonVal, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "Couldn't marshal data into json: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, err = w.Write(jsonVal)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "Unable to write response json: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:37:37 +03:00
|
|
|
func (l *queryLog) handleQueryLogClear(_ http.ResponseWriter, _ *http.Request) {
|
2019-09-27 18:58:57 +03:00
|
|
|
l.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get configuration
|
|
|
|
func (l *queryLog) handleQueryLogInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
resp := qlogConfig{}
|
|
|
|
resp.Enabled = l.conf.Enabled
|
2021-04-02 17:30:39 +03:00
|
|
|
resp.Interval = l.conf.RotationIvl
|
2020-03-03 20:21:53 +03:00
|
|
|
resp.AnonymizeClientIP = l.conf.AnonymizeClientIP
|
2019-09-27 18:58:57 +03:00
|
|
|
|
|
|
|
jsonVal, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, err = w.Write(jsonVal)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusInternalServerError, "http write: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set configuration
|
|
|
|
func (l *queryLog) handleQueryLogConfig(w http.ResponseWriter, r *http.Request) {
|
2019-11-12 15:36:17 +03:00
|
|
|
d := qlogConfig{}
|
|
|
|
req, err := jsonutil.DecodeObject(&d, r.Body)
|
2019-09-27 18:58:57 +03:00
|
|
|
if err != nil {
|
2019-11-12 15:36:17 +03:00
|
|
|
httpError(r, w, http.StatusBadRequest, "%s", err)
|
2019-09-27 18:58:57 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-12 15:36:17 +03:00
|
|
|
if req.Exists("interval") && !checkInterval(d.Interval) {
|
2019-09-27 18:58:57 +03:00
|
|
|
httpError(r, w, http.StatusBadRequest, "Unsupported interval")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-12 15:36:17 +03:00
|
|
|
l.lock.Lock()
|
|
|
|
// copy data, modify it, then activate. Other threads (readers) don't need to use this lock.
|
|
|
|
conf := *l.conf
|
|
|
|
if req.Exists("enabled") {
|
|
|
|
conf.Enabled = d.Enabled
|
|
|
|
}
|
|
|
|
if req.Exists("interval") {
|
2021-04-02 17:30:39 +03:00
|
|
|
conf.RotationIvl = d.Interval
|
2019-09-27 18:58:57 +03:00
|
|
|
}
|
2020-03-03 20:21:53 +03:00
|
|
|
if req.Exists("anonymize_client_ip") {
|
|
|
|
conf.AnonymizeClientIP = d.AnonymizeClientIP
|
|
|
|
}
|
2019-11-12 15:36:17 +03:00
|
|
|
l.conf = &conf
|
|
|
|
l.lock.Unlock()
|
2019-09-27 18:58:57 +03:00
|
|
|
|
|
|
|
l.conf.ConfigModified()
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:37:37 +03:00
|
|
|
// "value" -> value, return TRUE
|
|
|
|
func getDoubleQuotesEnclosedValue(s *string) bool {
|
|
|
|
t := *s
|
|
|
|
if len(t) >= 2 && t[0] == '"' && t[len(t)-1] == '"' {
|
|
|
|
*s = t[1 : len(t)-1]
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-15 14:19:04 +03:00
|
|
|
// inStr checks if string is in the slice of strings.
|
|
|
|
func inStr(strs []string, str string) (ok bool) {
|
|
|
|
for _, s := range strs {
|
|
|
|
if s == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:37:37 +03:00
|
|
|
// parseSearchCriteria - parses "searchCriteria" from the specified query parameter
|
|
|
|
func (l *queryLog) parseSearchCriteria(q url.Values, name string, ct criteriaType) (bool, searchCriteria, error) {
|
|
|
|
val := q.Get(name)
|
|
|
|
if len(val) == 0 {
|
|
|
|
return false, searchCriteria{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := searchCriteria{
|
|
|
|
criteriaType: ct,
|
|
|
|
value: val,
|
|
|
|
}
|
|
|
|
if getDoubleQuotesEnclosedValue(&c.value) {
|
|
|
|
c.strict = true
|
|
|
|
}
|
|
|
|
|
2021-03-15 14:19:04 +03:00
|
|
|
if ct == ctFilteringStatus && !inStr(filteringStatusValues, c.value) {
|
2020-05-26 15:37:37 +03:00
|
|
|
return false, c, fmt.Errorf("invalid value %s", c.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseSearchParams - parses "searchParams" from the HTTP request's query string
|
2021-03-11 20:36:54 +03:00
|
|
|
func (l *queryLog) parseSearchParams(r *http.Request) (p *searchParams, err error) {
|
|
|
|
p = newSearchParams()
|
2020-05-26 15:37:37 +03:00
|
|
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
olderThan := q.Get("older_than")
|
|
|
|
if len(olderThan) != 0 {
|
|
|
|
p.olderThan, err = time.Parse(time.RFC3339Nano, olderThan)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 20:36:54 +03:00
|
|
|
var limit64 int64
|
|
|
|
if limit64, err = strconv.ParseInt(q.Get("limit"), 10, 64); err == nil {
|
|
|
|
p.limit = int(limit64)
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
2021-03-11 20:36:54 +03:00
|
|
|
|
|
|
|
var offset64 int64
|
|
|
|
if offset64, err = strconv.ParseInt(q.Get("offset"), 10, 64); err == nil {
|
|
|
|
p.offset = int(offset64)
|
2020-09-01 16:30:30 +03:00
|
|
|
|
|
|
|
// If we don't use "olderThan" and use offset/limit instead, we should change the default behavior
|
|
|
|
// and scan all log records until we found enough log entries
|
2020-05-26 15:37:37 +03:00
|
|
|
p.maxFileScanEntries = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
paramNames := map[string]criteriaType{
|
2020-05-28 17:14:50 +03:00
|
|
|
"search": ctDomainOrClient,
|
|
|
|
"response_status": ctFilteringStatus,
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range paramNames {
|
2021-03-11 20:36:54 +03:00
|
|
|
var ok bool
|
|
|
|
var c searchCriteria
|
|
|
|
ok, c, err = l.parseSearchCriteria(q, k, v)
|
2020-05-26 15:37:37 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
p.searchCriteria = append(p.searchCriteria, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
2019-09-27 18:58:57 +03:00
|
|
|
}
|