2019-09-27 18:58:57 +03:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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 15:55:58 +03:00
|
|
|
olderThan string
|
|
|
|
filterDomain string
|
|
|
|
filterClient string
|
|
|
|
filterQuestionType string
|
|
|
|
filterResponseStatus string
|
2019-09-27 18:58:57 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *queryLog) handleQueryLog(w http.ResponseWriter, r *http.Request) {
|
2019-10-14 15:55:58 +03:00
|
|
|
var err error
|
2019-09-27 18:58:57 +03:00
|
|
|
req := request{}
|
2019-10-14 15:55:58 +03: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 18:58:57 +03:00
|
|
|
|
|
|
|
params := getDataParams{
|
2019-10-14 15:55:58 +03:00
|
|
|
Domain: req.filterDomain,
|
|
|
|
Client: req.filterClient,
|
2019-09-27 18:58:57 +03:00
|
|
|
ResponseStatus: responseStatusAll,
|
|
|
|
}
|
2019-10-14 15:55:58 +03:00
|
|
|
if len(req.olderThan) != 0 {
|
|
|
|
params.OlderThan, err = time.Parse(time.RFC3339Nano, req.olderThan)
|
2019-09-27 18:58:57 +03: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 15:55:58 +03:00
|
|
|
if len(req.filterQuestionType) != 0 {
|
|
|
|
qtype, ok := dns.StringToType[req.filterQuestionType]
|
2019-09-27 18:58:57 +03:00
|
|
|
if !ok {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "invalid question_type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
params.QuestionType = qtype
|
|
|
|
}
|
|
|
|
|
2019-10-14 15:55:58 +03:00
|
|
|
if len(req.filterResponseStatus) != 0 {
|
|
|
|
switch req.filterResponseStatus {
|
2019-09-27 18:58:57 +03: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) {
|
|
|
|
|
|
|
|
reqData := qlogConfig{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&reqData)
|
|
|
|
if err != nil {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "json decode: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkInterval(reqData.Interval) {
|
|
|
|
httpError(r, w, http.StatusBadRequest, "Unsupported interval")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := Config{
|
|
|
|
Enabled: reqData.Enabled,
|
|
|
|
Interval: reqData.Interval,
|
|
|
|
}
|
|
|
|
l.configure(conf)
|
|
|
|
|
|
|
|
l.conf.ConfigModified()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register web handlers
|
|
|
|
func (l *queryLog) initWeb() {
|
2019-10-14 15:55:58 +03:00
|
|
|
l.conf.HTTPRegister("GET", "/control/querylog", l.handleQueryLog)
|
2019-09-27 18:58:57 +03: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)
|
|
|
|
}
|