Add handles logging

This commit is contained in:
Aleksey Dmitrevskiy 2019-02-27 17:39:07 +03:00
parent 523c5ef10a
commit 1ed3a9673d
1 changed files with 40 additions and 0 deletions

View File

@ -83,6 +83,7 @@ func httpUpdateConfigReloadDNSReturnOK(w http.ResponseWriter, r *http.Request) {
}
func handleStatus(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
data := map[string]interface{}{
"dns_address": config.DNS.BindHost,
"http_port": config.BindPort,
@ -111,11 +112,13 @@ func handleStatus(w http.ResponseWriter, r *http.Request) {
}
func handleProtectionEnable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.ProtectionEnabled = true
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleProtectionDisable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.ProtectionEnabled = false
httpUpdateConfigReloadDNSReturnOK(w, r)
}
@ -124,16 +127,19 @@ func handleProtectionDisable(w http.ResponseWriter, r *http.Request) {
// stats
// -----
func handleQueryLogEnable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.QueryLogEnabled = true
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleQueryLogDisable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.QueryLogEnabled = false
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleQueryLog(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
data := dnsServer.GetQueryLog()
jsonVal, err := json.Marshal(data)
@ -150,6 +156,7 @@ func handleQueryLog(w http.ResponseWriter, r *http.Request) {
}
func handleStatsTop(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
s := dnsServer.GetStatsTop()
// use manual json marshalling because we want maps to be sorted by value
@ -196,6 +203,7 @@ func handleStatsTop(w http.ResponseWriter, r *http.Request) {
// handleStatsReset resets the stats caches
func handleStatsReset(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
dnsServer.PurgeStats()
_, err := fmt.Fprintf(w, "OK\n")
if err != nil {
@ -205,6 +213,7 @@ func handleStatsReset(w http.ResponseWriter, r *http.Request) {
// handleStats returns aggregated stats data for the 24 hours
func handleStats(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
summed := dnsServer.GetAggregatedStats()
statsJSON, err := json.Marshal(summed)
@ -222,6 +231,7 @@ func handleStats(w http.ResponseWriter, r *http.Request) {
// HandleStatsHistory returns historical stats data for the 24 hours
func handleStatsHistory(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
// handle time unit and prepare our time window size
timeUnitString := r.URL.Query().Get("time_unit")
var timeUnit time.Duration
@ -297,6 +307,7 @@ func sortByValue(m map[string]int) []string {
// -----------------------
func handleSetUpstreamDNS(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
body, err := ioutil.ReadAll(r.Body)
if err != nil {
httpError(w, http.StatusBadRequest, "Failed to read request body: %s", err)
@ -328,16 +339,19 @@ func handleSetUpstreamDNS(w http.ResponseWriter, r *http.Request) {
}
func handleAllServersEnable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.AllServers = true
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleAllServersDisable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.AllServers = false
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleAllServersStatus(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
data := map[string]interface{}{
"enabled": config.DNS.AllServers,
}
@ -356,6 +370,7 @@ func handleAllServersStatus(w http.ResponseWriter, r *http.Request) {
}
func handleTestUpstreamDNS(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
body, err := ioutil.ReadAll(r.Body)
if err != nil {
httpError(w, http.StatusBadRequest, "Failed to read request body: %s", err)
@ -424,6 +439,7 @@ func checkDNS(input string) error {
}
func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
now := time.Now()
if now.Sub(versionCheckLastTime) <= versionCheckPeriod && len(versionCheckJSON) != 0 {
// return cached copy
@ -463,16 +479,19 @@ func handleGetVersionJSON(w http.ResponseWriter, r *http.Request) {
// ---------
func handleFilteringEnable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.FilteringEnabled = true
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleFilteringDisable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.FilteringEnabled = false
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleFilteringStatus(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
data := map[string]interface{}{
"enabled": config.DNS.FilteringEnabled,
}
@ -497,6 +516,7 @@ func handleFilteringStatus(w http.ResponseWriter, r *http.Request) {
}
func handleFilteringAddURL(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
f := filter{}
err := json.NewDecoder(r.Body).Decode(&f)
if err != nil {
@ -569,6 +589,7 @@ func handleFilteringAddURL(w http.ResponseWriter, r *http.Request) {
}
func handleFilteringRemoveURL(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
parameters, err := parseParametersFromBody(r.Body)
if err != nil {
httpError(w, http.StatusBadRequest, "failed to parse parameters from body: %s", err)
@ -606,6 +627,7 @@ func handleFilteringRemoveURL(w http.ResponseWriter, r *http.Request) {
}
func handleFilteringEnableURL(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
parameters, err := parseParametersFromBody(r.Body)
if err != nil {
httpError(w, http.StatusBadRequest, "failed to parse parameters from body: %s", err)
@ -643,6 +665,7 @@ func handleFilteringEnableURL(w http.ResponseWriter, r *http.Request) {
}
func handleFilteringDisableURL(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
parameters, err := parseParametersFromBody(r.Body)
if err != nil {
httpError(w, http.StatusBadRequest, "failed to parse parameters from body: %s", err)
@ -678,6 +701,7 @@ func handleFilteringDisableURL(w http.ResponseWriter, r *http.Request) {
}
func handleFilteringSetRules(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
body, err := ioutil.ReadAll(r.Body)
if err != nil {
httpError(w, http.StatusBadRequest, "Failed to read request body: %s", err)
@ -689,6 +713,7 @@ func handleFilteringSetRules(w http.ResponseWriter, r *http.Request) {
}
func handleFilteringRefresh(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
force := r.URL.Query().Get("force")
updated := refreshFiltersIfNecessary(force != "")
fmt.Fprintf(w, "OK %d filters updated\n", updated)
@ -699,16 +724,19 @@ func handleFilteringRefresh(w http.ResponseWriter, r *http.Request) {
// ------------
func handleSafeBrowsingEnable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.SafeBrowsingEnabled = true
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleSafeBrowsingDisable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.SafeBrowsingEnabled = false
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleSafeBrowsingStatus(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
data := map[string]interface{}{
"enabled": config.DNS.SafeBrowsingEnabled,
}
@ -729,6 +757,7 @@ func handleSafeBrowsingStatus(w http.ResponseWriter, r *http.Request) {
// parental
// --------
func handleParentalEnable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
parameters, err := parseParametersFromBody(r.Body)
if err != nil {
httpError(w, http.StatusBadRequest, "failed to parse parameters from body: %s", err)
@ -773,11 +802,13 @@ func handleParentalEnable(w http.ResponseWriter, r *http.Request) {
}
func handleParentalDisable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.ParentalEnabled = false
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleParentalStatus(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
data := map[string]interface{}{
"enabled": config.DNS.ParentalEnabled,
}
@ -803,16 +834,19 @@ func handleParentalStatus(w http.ResponseWriter, r *http.Request) {
// ------------
func handleSafeSearchEnable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.SafeSearchEnabled = true
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleSafeSearchDisable(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
config.DNS.SafeSearchEnabled = false
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleSafeSearchStatus(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
data := map[string]interface{}{
"enabled": config.DNS.SafeSearchEnabled,
}
@ -845,6 +879,7 @@ type firstRunData struct {
}
func handleInstallGetAddresses(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
data := firstRunData{}
// find out if port 80 is available -- if not, fall back to 3000
@ -880,6 +915,7 @@ func handleInstallGetAddresses(w http.ResponseWriter, r *http.Request) {
}
func handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
newSettings := firstRunData{}
err := json.NewDecoder(r.Body).Decode(&newSettings)
if err != nil {
@ -938,10 +974,12 @@ func handleInstallConfigure(w http.ResponseWriter, r *http.Request) {
// TLS
// ---
func handleTLSStatus(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
marshalTLS(w, config.TLS)
}
func handleTLSValidate(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
data, err := unmarshalTLS(r)
if err != nil {
httpError(w, http.StatusBadRequest, "Failed to unmarshal TLS config: %s", err)
@ -967,6 +1005,7 @@ func handleTLSValidate(w http.ResponseWriter, r *http.Request) {
}
func handleTLSConfigure(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
data, err := unmarshalTLS(r)
if err != nil {
httpError(w, http.StatusBadRequest, "Failed to unmarshal TLS config: %s", err)
@ -1224,6 +1263,7 @@ func marshalTLS(w http.ResponseWriter, data tlsConfig) {
// DNS-over-HTTPS
// --------------
func handleDOH(w http.ResponseWriter, r *http.Request) {
log.Tracef("")
if r.TLS == nil {
httpError(w, http.StatusNotFound, "Not Found")
return