2019-09-27 16:58:57 +01:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2019-11-12 12:36:17 +00:00
|
|
|
"github.com/AdguardTeam/golibs/jsonutil"
|
2019-09-27 16:58:57 +01:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
type request struct {
|
2019-10-14 13:55:58 +01:00
|
|
|
olderThan string
|
|
|
|
filterDomain string
|
|
|
|
filterClient string
|
|
|
|
filterQuestionType string
|
|
|
|
filterResponseStatus string
|
2019-09-27 16:58:57 +01: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) handleQueryLog(w http.ResponseWriter, r *http.Request) {
|
2019-10-14 13:55:58 +01:00
|
|
|
var err error
|
2019-09-27 16:58:57 +01:00
|
|
|
req := request{}
|
2019-10-14 13:55:58 +01:00
|
|
|
q := r.URL.Query()
|
|
|
|
req.olderThan = q.Get("older_than")
|
|
|
|
req.filterDomain = q.Get("filter_domain")
|
|
|
|
req.filterClient = q.Get("filter_client")
|
|
|
|
req.filterQuestionType = q.Get("filter_question_type")
|
|
|
|
req.filterResponseStatus = q.Get("filter_response_status")
|
2019-09-27 16:58:57 +01:00
|
|
|
|
|
|
|
params := getDataParams{
|
2019-10-14 13:55:58 +01:00
|
|
|
Domain: req.filterDomain,
|
|
|
|
Client: req.filterClient,
|
2019-09-27 16:58:57 +01:00
|
|
|
ResponseStatus: responseStatusAll,
|
|
|
|
}
|
2019-10-14 13:55:58 +01:00
|
|
|
if len(req.olderThan) != 0 {
|
|
|
|
params.OlderThan, err = time.Parse(time.RFC3339Nano, req.olderThan)
|
2019-09-27 16:58:57 +01:00
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "invalid time stamp: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if getDoubleQuotesEnclosedValue(¶ms.Domain) {
|
|
|
|
params.StrictMatchDomain = true
|
|
|
|
}
|
|
|
|
if getDoubleQuotesEnclosedValue(¶ms.Client) {
|
|
|
|
params.StrictMatchClient = true
|
|
|
|
}
|
|
|
|
|
2019-10-14 13:55:58 +01:00
|
|
|
if len(req.filterQuestionType) != 0 {
|
2019-10-24 18:00:58 +01:00
|
|
|
_, ok := dns.StringToType[req.filterQuestionType]
|
2019-09-27 16:58:57 +01:00
|
|
|
if !ok {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "invalid question_type")
|
|
|
|
return
|
|
|
|
}
|
2019-10-24 18:00:58 +01:00
|
|
|
params.QuestionType = req.filterQuestionType
|
2019-09-27 16:58:57 +01:00
|
|
|
}
|
|
|
|
|
2019-10-14 13:55:58 +01:00
|
|
|
if len(req.filterResponseStatus) != 0 {
|
|
|
|
switch req.filterResponseStatus {
|
2019-09-27 16:58:57 +01:00
|
|
|
case "filtered":
|
|
|
|
params.ResponseStatus = responseStatusFiltered
|
|
|
|
default:
|
|
|
|
httpError(r, w, http.StatusBadRequest, "invalid response_status")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data := l.getData(params)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) handleQueryLogClear(w http.ResponseWriter, r *http.Request) {
|
|
|
|
l.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
type qlogConfig struct {
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
Interval uint32 `json:"interval"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get configuration
|
|
|
|
func (l *queryLog) handleQueryLogInfo(w http.ResponseWriter, r *http.Request) {
|
|
|
|
resp := qlogConfig{}
|
|
|
|
resp.Enabled = l.conf.Enabled
|
|
|
|
resp.Interval = l.conf.Interval
|
|
|
|
|
|
|
|
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 12:36:17 +00:00
|
|
|
d := qlogConfig{}
|
|
|
|
req, err := jsonutil.DecodeObject(&d, r.Body)
|
2019-09-27 16:58:57 +01:00
|
|
|
if err != nil {
|
2019-11-12 12:36:17 +00:00
|
|
|
httpError(r, w, http.StatusBadRequest, "%s", err)
|
2019-09-27 16:58:57 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-12 12:36:17 +00:00
|
|
|
if req.Exists("interval") && !checkInterval(d.Interval) {
|
2019-09-27 16:58:57 +01:00
|
|
|
httpError(r, w, http.StatusBadRequest, "Unsupported interval")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-12 12:36:17 +00: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") {
|
|
|
|
conf.Interval = d.Interval
|
2019-09-27 16:58:57 +01:00
|
|
|
}
|
2019-11-12 12:36:17 +00:00
|
|
|
l.conf = &conf
|
|
|
|
l.lock.Unlock()
|
2019-09-27 16:58:57 +01:00
|
|
|
|
|
|
|
l.conf.ConfigModified()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register web handlers
|
|
|
|
func (l *queryLog) initWeb() {
|
2019-10-14 13:55:58 +01:00
|
|
|
l.conf.HTTPRegister("GET", "/control/querylog", l.handleQueryLog)
|
2019-09-27 16:58:57 +01:00
|
|
|
l.conf.HTTPRegister("GET", "/control/querylog_info", l.handleQueryLogInfo)
|
|
|
|
l.conf.HTTPRegister("POST", "/control/querylog_clear", l.handleQueryLogClear)
|
|
|
|
l.conf.HTTPRegister("POST", "/control/querylog_config", l.handleQueryLogConfig)
|
|
|
|
}
|