Merge: Query log: add "interval" setting; add "clear log" button

#776 Close #446

* commit '8c89973365e660d47fa5fce88113414011961acb':
  - client: fix api client export
  - client: fix id and method name
  + client: handle logs configuration
  + openapi: /querylog_info, /querylog_config, /querylog_clear
  + doc: Relations between subsystems
  * querylog: move code to a separate package
This commit is contained in:
Simon Zolin 2019-09-12 18:58:18 +03:00
commit 7b46f82fad
38 changed files with 912 additions and 409 deletions

View file

@ -42,6 +42,15 @@ Contents:
* API: Clear statistics data * API: Clear statistics data
* API: Set statistics parameters * API: Set statistics parameters
* API: Get statistics parameters * API: Get statistics parameters
* Query logs
* API: Set querylog parameters
* API: Get querylog parameters
## Relations between subsystems
![](agh-arch.png)
## First startup ## First startup
@ -976,3 +985,37 @@ Response:
{ {
"interval": 1 | 7 | 30 | 90 "interval": 1 | 7 | 30 | 90
} }
## Query logs
### API: Set querylog parameters
Request:
POST /control/querylog_config
{
"enabled": true | false
"interval": 1 | 7 | 30 | 90
}
Response:
200 OK
### API: Get querylog parameters
Request:
GET /control/querylog_info
Response:
200 OK
{
"enabled": true | false
"interval": 1 | 7 | 30 | 90
}

BIN
agh-arch.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

5
client/package-lock.json generated vendored
View file

@ -5027,11 +5027,6 @@
} }
} }
}, },
"file-saver": {
"version": "1.3.8",
"resolved": "https://registry.npmjs.org/file-saver/-/file-saver-1.3.8.tgz",
"integrity": "sha512-spKHSBQIxxS81N/O21WmuXA2F6wppUCsutpzenOeZzOCCJ5gEfcbqJP983IrpLXzYmXnMUa6J03SubcNPdKrlg=="
},
"fill-range": { "fill-range": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-4.0.0.tgz", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-4.0.0.tgz",

1
client/package.json vendored
View file

@ -13,7 +13,6 @@
"axios": "^0.19.0", "axios": "^0.19.0",
"classnames": "^2.2.6", "classnames": "^2.2.6",
"date-fns": "^1.29.0", "date-fns": "^1.29.0",
"file-saver": "^1.3.8",
"i18next": "^12.0.0", "i18next": "^12.0.0",
"i18next-browser-languagedetector": "^2.2.3", "i18next-browser-languagedetector": "^2.2.3",
"lodash": "^4.17.15", "lodash": "^4.17.15",

View file

@ -98,7 +98,6 @@
"enforce_safe_search": "Enforce safe search", "enforce_safe_search": "Enforce safe search",
"enforce_save_search_hint": "AdGuard Home can enforce safe search in the following search engines: Google, Youtube, Bing, DuckDuckGo and Yandex.", "enforce_save_search_hint": "AdGuard Home can enforce safe search in the following search engines: Google, Youtube, Bing, DuckDuckGo and Yandex.",
"no_servers_specified": "No servers specified", "no_servers_specified": "No servers specified",
"no_settings": "No settings",
"general_settings": "General settings", "general_settings": "General settings",
"dns_settings": "DNS settings", "dns_settings": "DNS settings",
"encryption_settings": "Encryption settings", "encryption_settings": "Encryption settings",
@ -163,10 +162,7 @@
"show_all_filter_type": "Show all", "show_all_filter_type": "Show all",
"show_filtered_type": "Show filtered", "show_filtered_type": "Show filtered",
"no_logs_found": "No logs found", "no_logs_found": "No logs found",
"disabled_log_btn": "Disable log",
"download_log_file_btn": "Download log file",
"refresh_btn": "Refresh", "refresh_btn": "Refresh",
"enabled_log_btn": "Enable log",
"last_dns_queries": "Last 5000 DNS queries", "last_dns_queries": "Last 5000 DNS queries",
"previous_btn": "Previous", "previous_btn": "Previous",
"next_btn": "Next", "next_btn": "Next",
@ -177,10 +173,15 @@
"updated_custom_filtering_toast": "Updated the custom filtering rules", "updated_custom_filtering_toast": "Updated the custom filtering rules",
"rule_removed_from_custom_filtering_toast": "Rule removed from the custom filtering rules", "rule_removed_from_custom_filtering_toast": "Rule removed from the custom filtering rules",
"rule_added_to_custom_filtering_toast": "Rule added to the custom filtering rules", "rule_added_to_custom_filtering_toast": "Rule added to the custom filtering rules",
"query_log_disabled_toast": "Query log disabled",
"query_log_enabled_toast": "Query log enabled",
"query_log_response_status": "Status: {{value}}", "query_log_response_status": "Status: {{value}}",
"query_log_filtered": "Filtered by {{filter}}", "query_log_filtered": "Filtered by {{filter}}",
"query_log_confirm_clear": "Are you sure you want to clear the entire query log? This will also clear statistics on the dashboard.",
"query_log_cleared": "The query log has been successfully cleared",
"query_log_clear": "Clear query logs",
"query_log_retention": "Query logs retention",
"query_log_enable": "Enable log",
"query_log_configuration": "Logs configuration",
"query_log_disabled": "The query log is disabled and can be configured in the <0>settings</0>",
"source_label": "Source", "source_label": "Source",
"found_in_known_domain_db": "Found in the known domains database.", "found_in_known_domain_db": "Found in the known domains database.",
"category_label": "Category", "category_label": "Category",
@ -372,7 +373,7 @@
"domain": "Domain", "domain": "Domain",
"answer": "Answer", "answer": "Answer",
"filter_added_successfully": "The filter has been successfully added", "filter_added_successfully": "The filter has been successfully added",
"statistics_logs": "Statistics and logs", "statistics_configuration": "Statistics configuration",
"statistics_retention": "Statistics retention", "statistics_retention": "Statistics retention",
"statistics_retention_desc": "If you decrease the interval value, some data will be lost", "statistics_retention_desc": "If you decrease the interval value, some data will be lost",
"statistics_clear": " Clear statistics", "statistics_clear": " Clear statistics",

View file

@ -1,10 +1,8 @@
import { createAction } from 'redux-actions'; import { createAction } from 'redux-actions';
import Api from '../api/Api'; import apiClient from '../api/Api';
import { addErrorToast, addSuccessToast } from './index'; import { addErrorToast, addSuccessToast } from './index';
import { normalizeTextarea } from '../helpers/helpers'; import { normalizeTextarea } from '../helpers/helpers';
const apiClient = new Api();
export const getAccessListRequest = createAction('GET_ACCESS_LIST_REQUEST'); export const getAccessListRequest = createAction('GET_ACCESS_LIST_REQUEST');
export const getAccessListFailure = createAction('GET_ACCESS_LIST_FAILURE'); export const getAccessListFailure = createAction('GET_ACCESS_LIST_FAILURE');
export const getAccessListSuccess = createAction('GET_ACCESS_LIST_SUCCESS'); export const getAccessListSuccess = createAction('GET_ACCESS_LIST_SUCCESS');

View file

@ -1,11 +1,9 @@
import { createAction } from 'redux-actions'; import { createAction } from 'redux-actions';
import { t } from 'i18next'; import { t } from 'i18next';
import Api from '../api/Api'; import apiClient from '../api/Api';
import { addErrorToast, addSuccessToast, getClients } from './index'; import { addErrorToast, addSuccessToast, getClients } from './index';
import { CLIENT_ID } from '../helpers/constants'; import { CLIENT_ID } from '../helpers/constants';
const apiClient = new Api();
export const toggleClientModal = createAction('TOGGLE_CLIENT_MODAL'); export const toggleClientModal = createAction('TOGGLE_CLIENT_MODAL');
export const addClientRequest = createAction('ADD_CLIENT_REQUEST'); export const addClientRequest = createAction('ADD_CLIENT_REQUEST');

View file

@ -1,10 +1,8 @@
import { createAction } from 'redux-actions'; import { createAction } from 'redux-actions';
import Api from '../api/Api'; import apiClient from '../api/Api';
import { addErrorToast, addSuccessToast } from './index'; import { addErrorToast, addSuccessToast } from './index';
import { redirectToCurrentProtocol } from '../helpers/helpers'; import { redirectToCurrentProtocol } from '../helpers/helpers';
const apiClient = new Api();
export const getTlsStatusRequest = createAction('GET_TLS_STATUS_REQUEST'); export const getTlsStatusRequest = createAction('GET_TLS_STATUS_REQUEST');
export const getTlsStatusFailure = createAction('GET_TLS_STATUS_FAILURE'); export const getTlsStatusFailure = createAction('GET_TLS_STATUS_FAILURE');
export const getTlsStatusSuccess = createAction('GET_TLS_STATUS_SUCCESS'); export const getTlsStatusSuccess = createAction('GET_TLS_STATUS_SUCCESS');

View file

@ -4,12 +4,10 @@ import { showLoading, hideLoading } from 'react-redux-loading-bar';
import axios from 'axios'; import axios from 'axios';
import versionCompare from '../helpers/versionCompare'; import versionCompare from '../helpers/versionCompare';
import { normalizeFilteringStatus, normalizeLogs, normalizeTextarea, sortClients } from '../helpers/helpers'; import { normalizeFilteringStatus, normalizeTextarea, sortClients } from '../helpers/helpers';
import { SETTINGS_NAMES, CHECK_TIMEOUT } from '../helpers/constants'; import { SETTINGS_NAMES, CHECK_TIMEOUT } from '../helpers/constants';
import { getTlsStatus } from './encryption'; import { getTlsStatus } from './encryption';
import Api from '../api/Api'; import apiClient from '../api/Api';
const apiClient = new Api();
export const addErrorToast = createAction('ADD_ERROR_TOAST'); export const addErrorToast = createAction('ADD_ERROR_TOAST');
export const addSuccessToast = createAction('ADD_SUCCESS_TOAST'); export const addSuccessToast = createAction('ADD_SUCCESS_TOAST');
@ -292,52 +290,6 @@ export const disableDns = () => async (dispatch) => {
} }
}; };
export const getLogsRequest = createAction('GET_LOGS_REQUEST');
export const getLogsFailure = createAction('GET_LOGS_FAILURE');
export const getLogsSuccess = createAction('GET_LOGS_SUCCESS');
export const getLogs = () => async (dispatch, getState) => {
dispatch(getLogsRequest());
const timer = setInterval(async () => {
const state = getState();
if (state.dashboard.isCoreRunning) {
clearInterval(timer);
try {
const logs = normalizeLogs(await apiClient.getQueryLog());
dispatch(getLogsSuccess(logs));
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(getLogsFailure(error));
}
}
}, 100);
};
export const toggleLogStatusRequest = createAction('TOGGLE_LOGS_REQUEST');
export const toggleLogStatusFailure = createAction('TOGGLE_LOGS_FAILURE');
export const toggleLogStatusSuccess = createAction('TOGGLE_LOGS_SUCCESS');
export const toggleLogStatus = queryLogEnabled => async (dispatch) => {
dispatch(toggleLogStatusRequest());
let toggleMethod;
let successMessage;
if (queryLogEnabled) {
toggleMethod = apiClient.disableQueryLog.bind(apiClient);
successMessage = 'query_log_disabled_toast';
} else {
toggleMethod = apiClient.enableQueryLog.bind(apiClient);
successMessage = 'query_log_enabled_toast';
}
try {
await toggleMethod();
dispatch(addSuccessToast(successMessage));
dispatch(toggleLogStatusSuccess());
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(toggleLogStatusFailure());
}
};
export const setRulesRequest = createAction('SET_RULES_REQUEST'); export const setRulesRequest = createAction('SET_RULES_REQUEST');
export const setRulesFailure = createAction('SET_RULES_FAILURE'); export const setRulesFailure = createAction('SET_RULES_FAILURE');
export const setRulesSuccess = createAction('SET_RULES_SUCCESS'); export const setRulesSuccess = createAction('SET_RULES_SUCCESS');
@ -465,23 +417,6 @@ export const removeFilter = url => async (dispatch) => {
export const toggleFilteringModal = createAction('FILTERING_MODAL_TOGGLE'); export const toggleFilteringModal = createAction('FILTERING_MODAL_TOGGLE');
export const downloadQueryLogRequest = createAction('DOWNLOAD_QUERY_LOG_REQUEST');
export const downloadQueryLogFailure = createAction('DOWNLOAD_QUERY_LOG_FAILURE');
export const downloadQueryLogSuccess = createAction('DOWNLOAD_QUERY_LOG_SUCCESS');
export const downloadQueryLog = () => async (dispatch) => {
let data;
dispatch(downloadQueryLogRequest());
try {
data = await apiClient.downloadQueryLog();
dispatch(downloadQueryLogSuccess());
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(downloadQueryLogFailure());
}
return data;
};
export const handleUpstreamChange = createAction('HANDLE_UPSTREAM_CHANGE'); export const handleUpstreamChange = createAction('HANDLE_UPSTREAM_CHANGE');
export const setUpstreamRequest = createAction('SET_UPSTREAM_REQUEST'); export const setUpstreamRequest = createAction('SET_UPSTREAM_REQUEST');
export const setUpstreamFailure = createAction('SET_UPSTREAM_FAILURE'); export const setUpstreamFailure = createAction('SET_UPSTREAM_FAILURE');

View file

@ -1,9 +1,7 @@
import { createAction } from 'redux-actions'; import { createAction } from 'redux-actions';
import Api from '../api/Api'; import apiClient from '../api/Api';
import { addErrorToast, addSuccessToast } from './index'; import { addErrorToast, addSuccessToast } from './index';
const apiClient = new Api();
export const nextStep = createAction('NEXT_STEP'); export const nextStep = createAction('NEXT_STEP');
export const prevStep = createAction('PREV_STEP'); export const prevStep = createAction('PREV_STEP');

View file

@ -0,0 +1,67 @@
import { createAction } from 'redux-actions';
import apiClient from '../api/Api';
import { addErrorToast, addSuccessToast } from './index';
import { normalizeLogs } from '../helpers/helpers';
export const getLogsRequest = createAction('GET_LOGS_REQUEST');
export const getLogsFailure = createAction('GET_LOGS_FAILURE');
export const getLogsSuccess = createAction('GET_LOGS_SUCCESS');
export const getLogs = () => async (dispatch) => {
dispatch(getLogsRequest());
try {
const logs = normalizeLogs(await apiClient.getQueryLog());
dispatch(getLogsSuccess(logs));
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(getLogsFailure(error));
}
};
export const clearLogsRequest = createAction('CLEAR_LOGS_REQUEST');
export const clearLogsFailure = createAction('CLEAR_LOGS_FAILURE');
export const clearLogsSuccess = createAction('CLEAR_LOGS_SUCCESS');
export const clearLogs = () => async (dispatch) => {
dispatch(clearLogsRequest());
try {
await apiClient.clearQueryLog();
dispatch(clearLogsSuccess());
dispatch(addSuccessToast('query_log_cleared'));
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(clearLogsFailure(error));
}
};
export const getLogsConfigRequest = createAction('GET_LOGS_CONFIG_REQUEST');
export const getLogsConfigFailure = createAction('GET_LOGS_CONFIG_FAILURE');
export const getLogsConfigSuccess = createAction('GET_LOGS_CONFIG_SUCCESS');
export const getLogsConfig = () => async (dispatch) => {
dispatch(getLogsConfigRequest());
try {
const data = await apiClient.getQueryLogInfo();
dispatch(getLogsConfigSuccess(data));
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(getLogsConfigFailure());
}
};
export const setLogsConfigRequest = createAction('SET_LOGS_CONFIG_REQUEST');
export const setLogsConfigFailure = createAction('SET_LOGS_CONFIG_FAILURE');
export const setLogsConfigSuccess = createAction('SET_LOGS_CONFIG_SUCCESS');
export const setLogsConfig = config => async (dispatch) => {
dispatch(setLogsConfigRequest());
try {
await apiClient.setQueryLogConfig(config);
dispatch(addSuccessToast('config_successfully_saved'));
dispatch(setLogsConfigSuccess(config));
} catch (error) {
dispatch(addErrorToast({ error }));
dispatch(setLogsConfigFailure());
}
};

View file

@ -1,10 +1,8 @@
import { createAction } from 'redux-actions'; import { createAction } from 'redux-actions';
import { t } from 'i18next'; import { t } from 'i18next';
import Api from '../api/Api'; import apiClient from '../api/Api';
import { addErrorToast, addSuccessToast } from './index'; import { addErrorToast, addSuccessToast } from './index';
const apiClient = new Api();
export const toggleRewritesModal = createAction('TOGGLE_REWRITES_MODAL'); export const toggleRewritesModal = createAction('TOGGLE_REWRITES_MODAL');
export const getRewritesListRequest = createAction('GET_REWRITES_LIST_REQUEST'); export const getRewritesListRequest = createAction('GET_REWRITES_LIST_REQUEST');

View file

@ -1,9 +1,7 @@
import { createAction } from 'redux-actions'; import { createAction } from 'redux-actions';
import Api from '../api/Api'; import apiClient from '../api/Api';
import { addErrorToast, addSuccessToast } from './index'; import { addErrorToast, addSuccessToast } from './index';
const apiClient = new Api();
export const getBlockedServicesRequest = createAction('GET_BLOCKED_SERVICES_REQUEST'); export const getBlockedServicesRequest = createAction('GET_BLOCKED_SERVICES_REQUEST');
export const getBlockedServicesFailure = createAction('GET_BLOCKED_SERVICES_FAILURE'); export const getBlockedServicesFailure = createAction('GET_BLOCKED_SERVICES_FAILURE');
export const getBlockedServicesSuccess = createAction('GET_BLOCKED_SERVICES_SUCCESS'); export const getBlockedServicesSuccess = createAction('GET_BLOCKED_SERVICES_SUCCESS');

View file

@ -1,14 +1,12 @@
import { createAction } from 'redux-actions'; import { createAction } from 'redux-actions';
import Api from '../api/Api'; import apiClient from '../api/Api';
import { addErrorToast, addSuccessToast } from './index'; import { addErrorToast, addSuccessToast } from './index';
import { normalizeTopStats, secondsToMilliseconds } from '../helpers/helpers'; import { normalizeTopStats, secondsToMilliseconds } from '../helpers/helpers';
const apiClient = new Api(); export const getStatsConfigRequest = createAction('GET_STATS_CONFIG_REQUEST');
export const getStatsConfigFailure = createAction('GET_STATS_CONFIG_FAILURE');
export const getStatsConfigRequest = createAction('GET_LOGS_CONFIG_REQUEST'); export const getStatsConfigSuccess = createAction('GET_STATS_CONFIG_SUCCESS');
export const getStatsConfigFailure = createAction('GET_LOGS_CONFIG_FAILURE');
export const getStatsConfigSuccess = createAction('GET_LOGS_CONFIG_SUCCESS');
export const getStatsConfig = () => async (dispatch) => { export const getStatsConfig = () => async (dispatch) => {
dispatch(getStatsConfigRequest()); dispatch(getStatsConfigRequest());

View file

@ -1,6 +1,6 @@
import axios from 'axios'; import axios from 'axios';
export default class Api { class Api {
baseUrl = 'control'; baseUrl = 'control';
async makeRequest(path, method = 'POST', config) { async makeRequest(path, method = 'POST', config) {
@ -25,9 +25,6 @@ export default class Api {
GLOBAL_START = { path: 'start', method: 'POST' }; GLOBAL_START = { path: 'start', method: 'POST' };
GLOBAL_STATUS = { path: 'status', method: 'GET' }; GLOBAL_STATUS = { path: 'status', method: 'GET' };
GLOBAL_STOP = { path: 'stop', method: 'POST' }; GLOBAL_STOP = { path: 'stop', method: 'POST' };
GLOBAL_QUERY_LOG = { path: 'querylog', method: 'GET' };
GLOBAL_QUERY_LOG_ENABLE = { path: 'querylog_enable', method: 'POST' };
GLOBAL_QUERY_LOG_DISABLE = { path: 'querylog_disable', method: 'POST' };
GLOBAL_SET_UPSTREAM_DNS = { path: 'set_upstreams_config', method: 'POST' }; GLOBAL_SET_UPSTREAM_DNS = { path: 'set_upstreams_config', method: 'POST' };
GLOBAL_TEST_UPSTREAM_DNS = { path: 'test_upstream_dns', method: 'POST' }; GLOBAL_TEST_UPSTREAM_DNS = { path: 'test_upstream_dns', method: 'POST' };
GLOBAL_VERSION = { path: 'version.json', method: 'POST' }; GLOBAL_VERSION = { path: 'version.json', method: 'POST' };
@ -50,27 +47,6 @@ export default class Api {
return this.makeRequest(path, method); return this.makeRequest(path, method);
} }
getQueryLog() {
const { path, method } = this.GLOBAL_QUERY_LOG;
return this.makeRequest(path, method);
}
downloadQueryLog() {
const { path, method } = this.GLOBAL_QUERY_LOG;
const queryString = '?download=1';
return this.makeRequest(path + queryString, method);
}
enableQueryLog() {
const { path, method } = this.GLOBAL_QUERY_LOG_ENABLE;
return this.makeRequest(path, method);
}
disableQueryLog() {
const { path, method } = this.GLOBAL_QUERY_LOG_DISABLE;
return this.makeRequest(path, method);
}
setUpstream(url) { setUpstream(url) {
const { path, method } = this.GLOBAL_SET_UPSTREAM_DNS; const { path, method } = this.GLOBAL_SET_UPSTREAM_DNS;
const config = { const config = {
@ -521,4 +497,37 @@ export default class Api {
const { path, method } = this.STATS_RESET; const { path, method } = this.STATS_RESET;
return this.makeRequest(path, method); return this.makeRequest(path, method);
} }
// Query log
GET_QUERY_LOG = { path: 'querylog', method: 'GET' };
QUERY_LOG_CONFIG = { path: 'querylog_config', method: 'POST' };
QUERY_LOG_INFO = { path: 'querylog_info', method: 'GET' };
QUERY_LOG_CLEAR = { path: 'querylog_clear', method: 'POST' };
getQueryLog() {
const { path, method } = this.GET_QUERY_LOG;
return this.makeRequest(path, method);
}
getQueryLogInfo() {
const { path, method } = this.QUERY_LOG_INFO;
return this.makeRequest(path, method);
}
setQueryLogConfig(data) {
const { path, method } = this.QUERY_LOG_CONFIG;
const config = {
data,
headers: { 'Content-Type': 'application/json' },
};
return this.makeRequest(path, method, config);
}
clearQueryLog() {
const { path, method } = this.QUERY_LOG_CLEAR;
return this.makeRequest(path, method);
}
} }
const apiClient = new Api();
export default apiClient;

View file

@ -1,7 +1,6 @@
import React, { Component, Fragment } from 'react'; import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import ReactTable from 'react-table'; import ReactTable from 'react-table';
import { saveAs } from 'file-saver/FileSaver';
import escapeRegExp from 'lodash/escapeRegExp'; import escapeRegExp from 'lodash/escapeRegExp';
import endsWith from 'lodash/endsWith'; import endsWith from 'lodash/endsWith';
import { Trans, withNamespaces } from 'react-i18next'; import { Trans, withNamespaces } from 'react-i18next';
@ -17,7 +16,6 @@ import PopoverFiltered from '../ui/PopoverFilter';
import Popover from '../ui/Popover'; import Popover from '../ui/Popover';
import './Logs.css'; import './Logs.css';
const DOWNLOAD_LOG_FILENAME = 'dns-logs.txt';
const FILTERED_REASON = 'Filtered'; const FILTERED_REASON = 'Filtered';
const RESPONSE_FILTER = { const RESPONSE_FILTER = {
ALL: 'all', ALL: 'all',
@ -29,18 +27,19 @@ class Logs extends Component {
this.getLogs(); this.getLogs();
this.props.getFilteringStatus(); this.props.getFilteringStatus();
this.props.getClients(); this.props.getClients();
this.props.getLogsConfig();
} }
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
// get logs when queryLog becomes enabled // get logs when queryLog becomes enabled
if (this.props.dashboard.queryLogEnabled && !prevProps.dashboard.queryLogEnabled) { if (this.props.queryLogs.enabled && !prevProps.queryLogs.enabled) {
this.props.getLogs(); this.props.getLogs();
} }
} }
getLogs = () => { getLogs = () => {
// get logs on initialization if queryLogIsEnabled // get logs on initialization if queryLogIsEnabled
if (this.props.dashboard.queryLogEnabled) { if (this.props.queryLogs.enabled) {
this.props.getLogs(); this.props.getLogs();
} }
}; };
@ -155,10 +154,7 @@ class Logs extends Component {
} else { } else {
const filterItem = Object.keys(filters).filter(key => filters[key].id === filterId)[0]; const filterItem = Object.keys(filters).filter(key => filters[key].id === filterId)[0];
if ( if (typeof filterItem !== 'undefined' && typeof filters[filterItem] !== 'undefined') {
typeof filterItem !== 'undefined' &&
typeof filters[filterItem] !== 'undefined'
) {
filterName = filters[filterItem].name; filterName = filters[filterItem].name;
} }
@ -255,10 +251,7 @@ class Logs extends Component {
if (filter.value === RESPONSE_FILTER.FILTERED) { if (filter.value === RESPONSE_FILTER.FILTERED) {
// eslint-disable-next-line no-underscore-dangle // eslint-disable-next-line no-underscore-dangle
const { reason } = row._original; const { reason } = row._original;
return ( return this.checkFiltered(reason) || this.checkWhiteList(reason);
this.checkFiltered(reason) ||
this.checkWhiteList(reason)
);
} }
return true; return true;
}, },
@ -347,74 +340,44 @@ class Logs extends Component {
return null; return null;
} }
handleDownloadButton = async (e) => {
e.preventDefault();
const data = await this.props.downloadQueryLog();
const jsonStr = JSON.stringify(data);
const dataBlob = new Blob([jsonStr], { type: 'text/plain;charset=utf-8' });
saveAs(dataBlob, DOWNLOAD_LOG_FILENAME);
};
renderButtons(queryLogEnabled, logStatusProcessing) {
if (queryLogEnabled) {
return (
<Fragment>
<button
className="btn btn-gray btn-sm mr-2"
type="submit"
onClick={() => this.props.toggleLogStatus(queryLogEnabled)}
disabled={logStatusProcessing}
>
<Trans>disabled_log_btn</Trans>
</button>
<button
className="btn btn-primary btn-sm mr-2"
type="submit"
onClick={this.handleDownloadButton}
>
<Trans>download_log_file_btn</Trans>
</button>
<button
className="btn btn-outline-primary btn-sm"
type="submit"
onClick={this.getLogs}
>
<Trans>refresh_btn</Trans>
</button>
</Fragment>
);
}
return (
<button
className="btn btn-success btn-sm mr-2"
type="submit"
onClick={() => this.props.toggleLogStatus(queryLogEnabled)}
disabled={logStatusProcessing}
>
<Trans>enabled_log_btn</Trans>
</button>
);
}
render() { render() {
const { queryLogs, dashboard, t } = this.props; const { queryLogs, dashboard, t } = this.props;
const { queryLogEnabled } = dashboard; const { enabled, processingGetLogs, processingGetConfig } = queryLogs;
const { processingClients } = dashboard;
const isDataReady =
!processingGetLogs && !processingGetConfig && !dashboard.processingClients;
const refreshButton = enabled ? (
<button
className="btn btn-icon btn-outline-primary btn-sm ml-3"
type="submit"
onClick={this.getLogs}
>
<svg className="icons">
<use xlinkHref="#refresh" />
</svg>
</button>
) : (
''
);
return ( return (
<Fragment> <Fragment>
<PageTitle title={t('query_log')} subtitle={t('last_dns_queries')}> <PageTitle title={t('query_log')} subtitle={t('last_dns_queries')}>
<div className="page-title__actions"> {refreshButton}
{this.renderButtons(queryLogEnabled, dashboard.logStatusProcessing)}
</div>
</PageTitle> </PageTitle>
<Card> <Card>
{queryLogEnabled && {enabled && (processingGetLogs || processingClients || processingGetConfig) && (
queryLogs.getLogsProcessing && <Loading />
dashboard.processingClients && <Loading />} )}
{queryLogEnabled && {enabled && isDataReady && this.renderLogs(queryLogs.logs)}
!queryLogs.getLogsProcessing && {!enabled && !processingGetConfig && (
!dashboard.processingClients && <div className="lead text-center py-6">
this.renderLogs(queryLogs.logs)} <Trans components={[<Link to="/settings#logs-config" key="0">link</Link>]}>
query_log_disabled
</Trans>
</div>
)}
</Card> </Card>
</Fragment> </Fragment>
); );
@ -425,13 +388,12 @@ Logs.propTypes = {
getLogs: PropTypes.func.isRequired, getLogs: PropTypes.func.isRequired,
queryLogs: PropTypes.object.isRequired, queryLogs: PropTypes.object.isRequired,
dashboard: PropTypes.object.isRequired, dashboard: PropTypes.object.isRequired,
toggleLogStatus: PropTypes.func.isRequired,
downloadQueryLog: PropTypes.func.isRequired,
getFilteringStatus: PropTypes.func.isRequired, getFilteringStatus: PropTypes.func.isRequired,
filtering: PropTypes.object.isRequired, filtering: PropTypes.object.isRequired,
setRules: PropTypes.func.isRequired, setRules: PropTypes.func.isRequired,
addSuccessToast: PropTypes.func.isRequired, addSuccessToast: PropTypes.func.isRequired,
getClients: PropTypes.func.isRequired, getClients: PropTypes.func.isRequired,
getLogsConfig: PropTypes.func.isRequired,
t: PropTypes.func.isRequired, t: PropTypes.func.isRequired,
}; };

View file

@ -0,0 +1,82 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Field, reduxForm } from 'redux-form';
import { Trans, withNamespaces } from 'react-i18next';
import flow from 'lodash/flow';
import { renderSelectField, renderRadioField, toNumber } from '../../../helpers/form';
import { QUERY_LOG_INTERVALS_DAYS } from '../../../helpers/constants';
const getIntervalFields = (processing, t, handleChange, toNumber) =>
QUERY_LOG_INTERVALS_DAYS.map((interval) => {
const title =
interval === 1 ? t('interval_24_hour') : t('interval_days', { count: interval });
return (
<Field
key={interval}
name="interval"
type="radio"
component={renderRadioField}
value={interval}
placeholder={title}
onChange={handleChange}
normalize={toNumber}
disabled={processing}
/>
);
});
const Form = (props) => {
const {
handleSubmit, handleChange, processing, t,
} = props;
return (
<form onSubmit={handleSubmit}>
<div className="row">
<div className="col-12">
<div className="form__group form__group--settings">
<Field
name="enabled"
type="checkbox"
component={renderSelectField}
placeholder={t('query_log_enable')}
onChange={handleChange}
disabled={processing}
/>
</div>
</div>
<div className="col-12">
<label className="form__label">
<Trans>query_log_retention</Trans>
</label>
</div>
<div className="col-12">
<div className="form__group form__group--settings">
<div className="custom-controls-stacked">
{getIntervalFields(processing, t, handleChange, toNumber)}
</div>
</div>
</div>
</div>
</form>
);
};
Form.propTypes = {
handleSubmit: PropTypes.func.isRequired,
handleChange: PropTypes.func,
change: PropTypes.func.isRequired,
submitting: PropTypes.bool.isRequired,
invalid: PropTypes.bool.isRequired,
processing: PropTypes.bool.isRequired,
t: PropTypes.func.isRequired,
};
export default flow([
withNamespaces(),
reduxForm({
form: 'logConfigForm',
}),
])(Form);

View file

@ -0,0 +1,69 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withNamespaces, Trans } from 'react-i18next';
import debounce from 'lodash/debounce';
import { DEBOUNCE_TIMEOUT } from '../../../helpers/constants';
import Card from '../../ui/Card';
import Form from './Form';
class LogsConfig extends Component {
handleFormChange = debounce((values) => {
this.props.setLogsConfig(values);
}, DEBOUNCE_TIMEOUT);
handleClear = () => {
const { t, clearLogs } = this.props;
// eslint-disable-next-line no-alert
if (window.confirm(t('query_log_confirm_clear'))) {
clearLogs();
}
};
render() {
const {
t, enabled, interval, processing, processingClear,
} = this.props;
return (
<Card
title={t('query_log_configuration')}
bodyType="card-body box-body--settings"
id="logs-config"
>
<div className="form">
<Form
initialValues={{
enabled,
interval,
}}
onSubmit={this.handleFormChange}
onChange={this.handleFormChange}
processing={processing}
/>
<button
type="button"
className="btn btn-outline-secondary btn-sm"
onClick={this.handleClear}
disabled={processingClear}
>
<Trans>query_log_clear</Trans>
</button>
</div>
</Card>
);
}
}
LogsConfig.propTypes = {
interval: PropTypes.number.isRequired,
enabled: PropTypes.bool.isRequired,
processing: PropTypes.bool.isRequired,
processingClear: PropTypes.bool.isRequired,
setLogsConfig: PropTypes.func.isRequired,
clearLogs: PropTypes.func.isRequired,
t: PropTypes.func.isRequired,
};
export default withNamespaces()(LogsConfig);

View file

@ -9,9 +9,8 @@ import { STATS_INTERVALS_DAYS } from '../../../helpers/constants';
const getIntervalFields = (processing, t, handleChange, toNumber) => const getIntervalFields = (processing, t, handleChange, toNumber) =>
STATS_INTERVALS_DAYS.map((interval) => { STATS_INTERVALS_DAYS.map((interval) => {
const title = interval === 1 const title =
? t('interval_24_hour') interval === 1 ? t('interval_24_hour') : t('interval_days', { count: interval });
: t('interval_days', { count: interval });
return ( return (
<Field <Field
@ -37,7 +36,7 @@ const Form = (props) => {
<form onSubmit={handleSubmit}> <form onSubmit={handleSubmit}>
<div className="row"> <div className="row">
<div className="col-12"> <div className="col-12">
<label className="form__label form__label--with-desc" htmlFor="server_name"> <label className="form__label form__label--with-desc">
<Trans>statistics_retention</Trans> <Trans>statistics_retention</Trans>
</label> </label>
<div className="form__desc form__desc--top"> <div className="form__desc form__desc--top">
@ -45,7 +44,7 @@ const Form = (props) => {
</div> </div>
</div> </div>
<div className="col-12"> <div className="col-12">
<div className="form__group mt-2"> <div className="form__group form__group--settings mt-2">
<div className="custom-controls-stacked"> <div className="custom-controls-stacked">
{getIntervalFields(processing, t, handleChange, toNumber)} {getIntervalFields(processing, t, handleChange, toNumber)}
</div> </div>
@ -69,6 +68,6 @@ Form.propTypes = {
export default flow([ export default flow([
withNamespaces(), withNamespaces(),
reduxForm({ reduxForm({
form: 'logConfigForm', form: 'statsConfigForm',
}), }),
])(Form); ])(Form);

View file

@ -4,8 +4,8 @@ import { withNamespaces, Trans } from 'react-i18next';
import debounce from 'lodash/debounce'; import debounce from 'lodash/debounce';
import { DEBOUNCE_TIMEOUT } from '../../../helpers/constants'; import { DEBOUNCE_TIMEOUT } from '../../../helpers/constants';
import Form from './Form';
import Card from '../../ui/Card'; import Card from '../../ui/Card';
import Form from './Form';
class StatsConfig extends Component { class StatsConfig extends Component {
handleFormChange = debounce((values) => { handleFormChange = debounce((values) => {
@ -26,7 +26,7 @@ class StatsConfig extends Component {
} = this.props; } = this.props;
return ( return (
<Card title={t('statistics_logs')} bodyType="card-body box-body--settings"> <Card title={t('statistics_configuration')} bodyType="card-body box-body--settings">
<div className="form"> <div className="form">
<Form <Form
initialValues={{ initialValues={{
@ -39,7 +39,7 @@ class StatsConfig extends Component {
<button <button
type="button" type="button"
className="btn btn-outline-secondary btn-sm mt-3" className="btn btn-outline-secondary btn-sm"
onClick={this.handleReset} onClick={this.handleReset}
disabled={processingReset} disabled={processingReset}
> >

View file

@ -1,9 +1,10 @@
import React, { Component, Fragment } from 'react'; import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { withNamespaces, Trans } from 'react-i18next'; import { withNamespaces } from 'react-i18next';
import Services from './Services'; import Services from './Services';
import StatsConfig from './StatsConfig'; import StatsConfig from './StatsConfig';
import LogsConfig from './LogsConfig';
import Checkbox from '../ui/Checkbox'; import Checkbox from '../ui/Checkbox';
import Loading from '../ui/Loading'; import Loading from '../ui/Loading';
import PageTitle from '../ui/PageTitle'; import PageTitle from '../ui/PageTitle';
@ -39,6 +40,7 @@ class Settings extends Component {
this.props.initSettings(this.settings); this.props.initSettings(this.settings);
this.props.getBlockedServices(); this.props.getBlockedServices();
this.props.getStatsConfig(); this.props.getStatsConfig();
this.props.getLogsConfig();
} }
renderSettings = (settings) => { renderSettings = (settings) => {
@ -55,11 +57,7 @@ class Settings extends Component {
); );
}); });
} }
return ( return '';
<div>
<Trans>no_settings</Trans>
</div>
);
}; };
render() { render() {
@ -70,13 +68,23 @@ class Settings extends Component {
setStatsConfig, setStatsConfig,
resetStats, resetStats,
stats, stats,
queryLogs,
setLogsConfig,
clearLogs,
t, t,
} = this.props; } = this.props;
const isDataReady =
!settings.processing &&
!services.processing &&
!stats.processingGetConfig &&
!queryLogs.processingGetConfig;
return ( return (
<Fragment> <Fragment>
<PageTitle title={t('general_settings')} /> <PageTitle title={t('general_settings')} />
{settings.processing && <Loading />} {!isDataReady && <Loading />}
{!settings.processing && ( {isDataReady && (
<div className="content"> <div className="content">
<div className="row"> <div className="row">
<div className="col-md-12"> <div className="col-md-12">
@ -95,6 +103,16 @@ class Settings extends Component {
resetStats={resetStats} resetStats={resetStats}
/> />
</div> </div>
<div className="col-md-12">
<LogsConfig
enabled={queryLogs.enabled}
interval={queryLogs.interval}
processing={queryLogs.processingSetConfig}
processingClear={queryLogs.processingClear}
setLogsConfig={setLogsConfig}
clearLogs={clearLogs}
/>
</div>
<div className="col-md-12"> <div className="col-md-12">
<Services <Services
services={services} services={services}

View file

@ -23,7 +23,7 @@
} }
.checkbox--form .checkbox__label:before { .checkbox--form .checkbox__label:before {
top: 2px; top: 1px;
margin-right: 10px; margin-right: 10px;
} }

View file

@ -1,5 +1,6 @@
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { getLogs, toggleLogStatus, downloadQueryLog, getFilteringStatus, setRules, addSuccessToast, getClients } from '../actions'; import { getFilteringStatus, setRules, addSuccessToast, getClients } from '../actions';
import { getLogs, getLogsConfig } from '../actions/queryLogs';
import Logs from '../components/Logs'; import Logs from '../components/Logs';
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
@ -10,12 +11,11 @@ const mapStateToProps = (state) => {
const mapDispatchToProps = { const mapDispatchToProps = {
getLogs, getLogs,
toggleLogStatus,
downloadQueryLog,
getFilteringStatus, getFilteringStatus,
setRules, setRules,
addSuccessToast, addSuccessToast,
getClients, getClients,
getLogsConfig,
}; };
export default connect( export default connect(

View file

@ -2,14 +2,18 @@ import { connect } from 'react-redux';
import { initSettings, toggleSetting } from '../actions'; import { initSettings, toggleSetting } from '../actions';
import { getBlockedServices, setBlockedServices } from '../actions/services'; import { getBlockedServices, setBlockedServices } from '../actions/services';
import { getStatsConfig, setStatsConfig, resetStats } from '../actions/stats'; import { getStatsConfig, setStatsConfig, resetStats } from '../actions/stats';
import { clearLogs, getLogsConfig, setLogsConfig } from '../actions/queryLogs';
import Settings from '../components/Settings'; import Settings from '../components/Settings';
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
const { settings, services, stats } = state; const {
settings, services, stats, queryLogs,
} = state;
const props = { const props = {
settings, settings,
services, services,
stats, stats,
queryLogs,
}; };
return props; return props;
}; };
@ -22,6 +26,9 @@ const mapDispatchToProps = {
getStatsConfig, getStatsConfig,
setStatsConfig, setStatsConfig,
resetStats, resetStats,
clearLogs,
getLogsConfig,
setLogsConfig,
}; };
export default connect( export default connect(

View file

@ -263,3 +263,5 @@ export const FILTERED_STATUS = {
}; };
export const STATS_INTERVALS_DAYS = [1, 7, 30, 90]; export const STATS_INTERVALS_DAYS = [1, 7, 30, 90];
export const QUERY_LOG_INTERVALS_DAYS = [1, 7, 30, 90];

View file

@ -12,6 +12,7 @@ import access from './access';
import rewrites from './rewrites'; import rewrites from './rewrites';
import services from './services'; import services from './services';
import stats from './stats'; import stats from './stats';
import queryLogs from './queryLogs';
const settings = handleActions({ const settings = handleActions({
[actions.initSettingsRequest]: state => ({ ...state, processing: true }), [actions.initSettingsRequest]: state => ({ ...state, processing: true }),
@ -43,6 +44,7 @@ const settings = handleActions({
processingTestUpstream: false, processingTestUpstream: false,
processingSetUpstream: false, processingSetUpstream: false,
processingDhcpStatus: false, processingDhcpStatus: false,
settingsList: {},
}); });
const dashboard = handleActions({ const dashboard = handleActions({
@ -54,7 +56,6 @@ const dashboard = handleActions({
running, running,
dns_port: dnsPort, dns_port: dnsPort,
dns_addresses: dnsAddresses, dns_addresses: dnsAddresses,
querylog_enabled: queryLogEnabled,
upstream_dns: upstreamDns, upstream_dns: upstreamDns,
bootstrap_dns: bootstrapDns, bootstrap_dns: bootstrapDns,
all_servers: allServers, all_servers: allServers,
@ -69,7 +70,6 @@ const dashboard = handleActions({
dnsVersion: version, dnsVersion: version,
dnsPort, dnsPort,
dnsAddresses, dnsAddresses,
queryLogEnabled,
upstreamDns: upstreamDns.join('\n'), upstreamDns: upstreamDns.join('\n'),
bootstrapDns: bootstrapDns.join('\n'), bootstrapDns: bootstrapDns.join('\n'),
allServers, allServers,
@ -94,13 +94,6 @@ const dashboard = handleActions({
return newState; return newState;
}, },
[actions.toggleLogStatusRequest]: state => ({ ...state, logStatusProcessing: true }),
[actions.toggleLogStatusFailure]: state => ({ ...state, logStatusProcessing: false }),
[actions.toggleLogStatusSuccess]: (state) => {
const { queryLogEnabled } = state;
return ({ ...state, queryLogEnabled: !queryLogEnabled, logStatusProcessing: false });
},
[actions.getVersionRequest]: state => ({ ...state, processingVersion: true }), [actions.getVersionRequest]: state => ({ ...state, processingVersion: true }),
[actions.getVersionFailure]: state => ({ ...state, processingVersion: false }), [actions.getVersionFailure]: state => ({ ...state, processingVersion: false }),
[actions.getVersionSuccess]: (state, { payload }) => { [actions.getVersionSuccess]: (state, { payload }) => {
@ -179,7 +172,6 @@ const dashboard = handleActions({
}, { }, {
processing: true, processing: true,
isCoreRunning: false, isCoreRunning: false,
logStatusProcessing: false,
processingVersion: true, processingVersion: true,
processingFiltering: true, processingFiltering: true,
processingClients: true, processingClients: true,
@ -197,22 +189,6 @@ const dashboard = handleActions({
autoClients: [], autoClients: [],
}); });
const queryLogs = handleActions({
[actions.getLogsRequest]: state => ({ ...state, getLogsProcessing: true }),
[actions.getLogsFailure]: state => ({ ...state, getLogsProcessing: false }),
[actions.getLogsSuccess]: (state, { payload }) => {
const newState = { ...state, logs: payload, getLogsProcessing: false };
return newState;
},
[actions.downloadQueryLogRequest]: state => ({ ...state, logsDownloading: true }),
[actions.downloadQueryLogFailure]: state => ({ ...state, logsDownloading: false }),
[actions.downloadQueryLogSuccess]: state => ({ ...state, logsDownloading: false }),
}, {
getLogsProcessing: false,
logsDownloading: false,
logs: [],
});
const filtering = handleActions({ const filtering = handleActions({
[actions.setRulesRequest]: state => ({ ...state, processingRules: true }), [actions.setRulesRequest]: state => ({ ...state, processingRules: true }),
[actions.setRulesFailure]: state => ({ ...state, processingRules: false }), [actions.setRulesFailure]: state => ({ ...state, processingRules: false }),

View file

@ -0,0 +1,49 @@
import { handleActions } from 'redux-actions';
import * as actions from '../actions/queryLogs';
const queryLogs = handleActions(
{
[actions.getLogsRequest]: state => ({ ...state, processingGetLogs: true }),
[actions.getLogsFailure]: state => ({ ...state, processingGetLogs: false }),
[actions.getLogsSuccess]: (state, { payload }) => {
const newState = { ...state, logs: payload, processingGetLogs: false };
return newState;
},
[actions.clearLogsRequest]: state => ({ ...state, processingClear: true }),
[actions.clearLogsFailure]: state => ({ ...state, processingClear: false }),
[actions.clearLogsSuccess]: state => ({
...state,
logs: [],
processingClear: false,
}),
[actions.getLogsConfigRequest]: state => ({ ...state, processingGetConfig: true }),
[actions.getLogsConfigFailure]: state => ({ ...state, processingGetConfig: false }),
[actions.getLogsConfigSuccess]: (state, { payload }) => ({
...state,
...payload,
processingGetConfig: false,
}),
[actions.setLogsConfigRequest]: state => ({ ...state, processingSetConfig: true }),
[actions.setLogsConfigFailure]: state => ({ ...state, processingSetConfig: false }),
[actions.setLogsConfigSuccess]: (state, { payload }) => ({
...state,
...payload,
processingSetConfig: false,
}),
},
{
processingGetLogs: true,
processingClear: false,
processingGetConfig: false,
processingSetConfig: false,
logs: [],
interval: 1,
enabled: true,
},
);
export default queryLogs;

View file

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/AdguardTeam/AdGuardHome/dnsfilter" "github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/AdguardTeam/AdGuardHome/querylog"
"github.com/AdguardTeam/AdGuardHome/stats" "github.com/AdguardTeam/AdGuardHome/stats"
"github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream" "github.com/AdguardTeam/dnsproxy/upstream"
@ -40,7 +41,7 @@ const (
type Server struct { type Server struct {
dnsProxy *proxy.Proxy // DNS proxy instance dnsProxy *proxy.Proxy // DNS proxy instance
dnsFilter *dnsfilter.Dnsfilter // DNS filter instance dnsFilter *dnsfilter.Dnsfilter // DNS filter instance
queryLog *queryLog // Query log instance queryLog querylog.QueryLog // Query log instance
stats stats.Stats stats stats.Stats
AllowedClients map[string]bool // IP addresses of whitelist clients AllowedClients map[string]bool // IP addresses of whitelist clients
@ -54,16 +55,11 @@ type Server struct {
} }
// NewServer creates a new instance of the dnsforward.Server // NewServer creates a new instance of the dnsforward.Server
// baseDir is the base directory for query logs
// Note: this function must be called only once // Note: this function must be called only once
func NewServer(baseDir string, stats stats.Stats) *Server { func NewServer(stats stats.Stats, queryLog querylog.QueryLog) *Server {
s := &Server{ s := &Server{}
queryLog: newQueryLog(baseDir),
}
s.stats = stats s.stats = stats
s.queryLog = queryLog
log.Printf("Start DNS server periodic jobs")
go s.queryLog.periodicQueryLogRotate()
return s return s
} }
@ -75,6 +71,7 @@ type FilteringConfig struct {
BlockingMode string `yaml:"blocking_mode"` // mode how to answer filtered requests BlockingMode string `yaml:"blocking_mode"` // mode how to answer filtered requests
BlockedResponseTTL uint32 `yaml:"blocked_response_ttl"` // if 0, then default is used (3600) BlockedResponseTTL uint32 `yaml:"blocked_response_ttl"` // if 0, then default is used (3600)
QueryLogEnabled bool `yaml:"querylog_enabled"` // if true, query log is enabled QueryLogEnabled bool `yaml:"querylog_enabled"` // if true, query log is enabled
QueryLogInterval uint32 `yaml:"querylog_interval"` // time interval for query log (in days)
Ratelimit int `yaml:"ratelimit"` // max number of requests per second from a given IP (0 to disable) Ratelimit int `yaml:"ratelimit"` // max number of requests per second from a given IP (0 to disable)
RatelimitWhitelist []string `yaml:"ratelimit_whitelist"` // a list of whitelisted client IP addresses RatelimitWhitelist []string `yaml:"ratelimit_whitelist"` // a list of whitelisted client IP addresses
RefuseAny bool `yaml:"refuse_any"` // if true, refuse ANY requests RefuseAny bool `yaml:"refuse_any"` // if true, refuse ANY requests
@ -303,8 +300,7 @@ func (s *Server) stopInternal() error {
s.dnsFilter = nil s.dnsFilter = nil
} }
// flush remainder to file return nil
return s.queryLog.flushLogBuffer(true)
} }
// IsRunning returns true if the DNS server is running // IsRunning returns true if the DNS server is running
@ -343,13 +339,6 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.RUnlock() s.RUnlock()
} }
// GetQueryLog returns a map with the current query log ready to be converted to a JSON
func (s *Server) GetQueryLog() []map[string]interface{} {
s.RLock()
defer s.RUnlock()
return s.queryLog.getQueryLog()
}
// Return TRUE if this client should be blocked // Return TRUE if this client should be blocked
func (s *Server) isBlockedIP(ip string) bool { func (s *Server) isBlockedIP(ip string) bool {
if len(s.AllowedClients) != 0 || len(s.AllowedClientsIPNet) != 0 { if len(s.AllowedClients) != 0 || len(s.AllowedClientsIPNet) != 0 {
@ -469,12 +458,12 @@ func (s *Server) handleDNSRequest(p *proxy.Proxy, d *proxy.DNSContext) error {
} }
elapsed := time.Since(start) elapsed := time.Since(start)
if s.conf.QueryLogEnabled && shouldLog { if s.conf.QueryLogEnabled && shouldLog && s.queryLog != nil {
upstreamAddr := "" upstreamAddr := ""
if d.Upstream != nil { if d.Upstream != nil {
upstreamAddr = d.Upstream.Address() upstreamAddr = d.Upstream.Address()
} }
_ = s.queryLog.logRequest(msg, d.Res, res, elapsed, d.Addr, upstreamAddr) s.queryLog.Add(msg, d.Res, res, elapsed, d.Addr, upstreamAddr)
} }
s.updateStats(d, elapsed, *res) s.updateStats(d, elapsed, *res)

View file

@ -10,7 +10,6 @@ import (
"encoding/pem" "encoding/pem"
"math/big" "math/big"
"net" "net"
"os"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -18,18 +17,15 @@ import (
"github.com/AdguardTeam/AdGuardHome/dnsfilter" "github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/dnsproxy/proxy"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/stretchr/testify/assert"
) )
const ( const (
tlsServerName = "testdns.adguard.com" tlsServerName = "testdns.adguard.com"
dataDir = "testData"
testMessagesCount = 10 testMessagesCount = 10
) )
func TestServer(t *testing.T) { func TestServer(t *testing.T) {
s := createTestServer(t) s := createTestServer(t)
defer removeDataDir(t)
err := s.Start(nil) err := s.Start(nil)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
@ -45,10 +41,6 @@ func TestServer(t *testing.T) {
} }
assertGoogleAResponse(t, reply) assertGoogleAResponse(t, reply)
// check query log and stats
log := s.GetQueryLog()
assert.Equal(t, 1, len(log), "Log size")
// message over TCP // message over TCP
req = createGoogleATestMessage() req = createGoogleATestMessage()
addr = s.dnsProxy.Addr("tcp") addr = s.dnsProxy.Addr("tcp")
@ -59,10 +51,6 @@ func TestServer(t *testing.T) {
} }
assertGoogleAResponse(t, reply) assertGoogleAResponse(t, reply)
// check query log and stats again
log = s.GetQueryLog()
assert.Equal(t, 2, len(log), "Log size")
err = s.Stop() err = s.Stop()
if err != nil { if err != nil {
t.Fatalf("DNS server failed to stop: %s", err) t.Fatalf("DNS server failed to stop: %s", err)
@ -72,7 +60,6 @@ func TestServer(t *testing.T) {
func TestServerWithProtectionDisabled(t *testing.T) { func TestServerWithProtectionDisabled(t *testing.T) {
s := createTestServer(t) s := createTestServer(t)
s.conf.ProtectionEnabled = false s.conf.ProtectionEnabled = false
defer removeDataDir(t)
err := s.Start(nil) err := s.Start(nil)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
@ -88,10 +75,6 @@ func TestServerWithProtectionDisabled(t *testing.T) {
} }
assertGoogleAResponse(t, reply) assertGoogleAResponse(t, reply)
// check query log and stats
log := s.GetQueryLog()
assert.Equal(t, 1, len(log), "Log size")
err = s.Stop() err = s.Stop()
if err != nil { if err != nil {
t.Fatalf("DNS server failed to stop: %s", err) t.Fatalf("DNS server failed to stop: %s", err)
@ -102,7 +85,6 @@ func TestDotServer(t *testing.T) {
// Prepare the proxy server // Prepare the proxy server
_, certPem, keyPem := createServerTLSConfig(t) _, certPem, keyPem := createServerTLSConfig(t)
s := createTestServer(t) s := createTestServer(t)
defer removeDataDir(t)
s.conf.TLSConfig = TLSConfig{ s.conf.TLSConfig = TLSConfig{
TLSListenAddr: &net.TCPAddr{Port: 0}, TLSListenAddr: &net.TCPAddr{Port: 0},
@ -143,7 +125,6 @@ func TestDotServer(t *testing.T) {
func TestServerRace(t *testing.T) { func TestServerRace(t *testing.T) {
s := createTestServer(t) s := createTestServer(t)
defer removeDataDir(t)
err := s.Start(nil) err := s.Start(nil)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
@ -168,7 +149,6 @@ func TestServerRace(t *testing.T) {
func TestSafeSearch(t *testing.T) { func TestSafeSearch(t *testing.T) {
s := createTestServer(t) s := createTestServer(t)
s.conf.SafeSearchEnabled = true s.conf.SafeSearchEnabled = true
defer removeDataDir(t)
err := s.Start(nil) err := s.Start(nil)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
@ -210,7 +190,6 @@ func TestSafeSearch(t *testing.T) {
func TestInvalidRequest(t *testing.T) { func TestInvalidRequest(t *testing.T) {
s := createTestServer(t) s := createTestServer(t)
defer removeDataDir(t)
err := s.Start(nil) err := s.Start(nil)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
@ -229,11 +208,6 @@ func TestInvalidRequest(t *testing.T) {
t.Fatalf("got a response to an invalid query") t.Fatalf("got a response to an invalid query")
} }
// check query log and stats
// invalid requests aren't written to the query log
log := s.GetQueryLog()
assert.Equal(t, 0, len(log), "Log size")
err = s.Stop() err = s.Stop()
if err != nil { if err != nil {
t.Fatalf("DNS server failed to stop: %s", err) t.Fatalf("DNS server failed to stop: %s", err)
@ -242,7 +216,6 @@ func TestInvalidRequest(t *testing.T) {
func TestBlockedRequest(t *testing.T) { func TestBlockedRequest(t *testing.T) {
s := createTestServer(t) s := createTestServer(t)
defer removeDataDir(t)
err := s.Start(nil) err := s.Start(nil)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
@ -267,10 +240,6 @@ func TestBlockedRequest(t *testing.T) {
t.Fatalf("Wrong response: %s", reply.String()) t.Fatalf("Wrong response: %s", reply.String())
} }
// check query log and stats
log := s.GetQueryLog()
assert.Equal(t, 1, len(log), "Log size")
err = s.Stop() err = s.Stop()
if err != nil { if err != nil {
t.Fatalf("DNS server failed to stop: %s", err) t.Fatalf("DNS server failed to stop: %s", err)
@ -280,7 +249,6 @@ func TestBlockedRequest(t *testing.T) {
func TestNullBlockedRequest(t *testing.T) { func TestNullBlockedRequest(t *testing.T) {
s := createTestServer(t) s := createTestServer(t)
s.conf.FilteringConfig.BlockingMode = "null_ip" s.conf.FilteringConfig.BlockingMode = "null_ip"
defer removeDataDir(t)
err := s.Start(nil) err := s.Start(nil)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
@ -312,10 +280,6 @@ func TestNullBlockedRequest(t *testing.T) {
t.Fatalf("DNS server %s returned wrong answer type instead of A: %v", addr, reply.Answer[0]) t.Fatalf("DNS server %s returned wrong answer type instead of A: %v", addr, reply.Answer[0])
} }
// check query log and stats
log := s.GetQueryLog()
assert.Equal(t, 1, len(log), "Log size")
err = s.Stop() err = s.Stop()
if err != nil { if err != nil {
t.Fatalf("DNS server failed to stop: %s", err) t.Fatalf("DNS server failed to stop: %s", err)
@ -324,7 +288,6 @@ func TestNullBlockedRequest(t *testing.T) {
func TestBlockedByHosts(t *testing.T) { func TestBlockedByHosts(t *testing.T) {
s := createTestServer(t) s := createTestServer(t)
defer removeDataDir(t)
err := s.Start(nil) err := s.Start(nil)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
@ -356,10 +319,6 @@ func TestBlockedByHosts(t *testing.T) {
t.Fatalf("DNS server %s returned wrong answer type instead of A: %v", addr, reply.Answer[0]) t.Fatalf("DNS server %s returned wrong answer type instead of A: %v", addr, reply.Answer[0])
} }
// check query log and stats
log := s.GetQueryLog()
assert.Equal(t, 1, len(log), "Log size")
err = s.Stop() err = s.Stop()
if err != nil { if err != nil {
t.Fatalf("DNS server failed to stop: %s", err) t.Fatalf("DNS server failed to stop: %s", err)
@ -368,7 +327,6 @@ func TestBlockedByHosts(t *testing.T) {
func TestBlockedBySafeBrowsing(t *testing.T) { func TestBlockedBySafeBrowsing(t *testing.T) {
s := createTestServer(t) s := createTestServer(t)
defer removeDataDir(t)
err := s.Start(nil) err := s.Start(nil)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
@ -411,10 +369,6 @@ func TestBlockedBySafeBrowsing(t *testing.T) {
t.Fatalf("DNS server %s returned wrong answer type instead of A: %v", addr, reply.Answer[0]) t.Fatalf("DNS server %s returned wrong answer type instead of A: %v", addr, reply.Answer[0])
} }
// check query log and stats
log := s.GetQueryLog()
assert.Equal(t, 1, len(log), "Log size")
err = s.Stop() err = s.Stop()
if err != nil { if err != nil {
t.Fatalf("DNS server failed to stop: %s", err) t.Fatalf("DNS server failed to stop: %s", err)
@ -422,7 +376,7 @@ func TestBlockedBySafeBrowsing(t *testing.T) {
} }
func createTestServer(t *testing.T) *Server { func createTestServer(t *testing.T) *Server {
s := NewServer(createDataDir(t), nil) s := NewServer(nil, nil)
s.conf.UDPListenAddr = &net.UDPAddr{Port: 0} s.conf.UDPListenAddr = &net.UDPAddr{Port: 0}
s.conf.TCPListenAddr = &net.TCPAddr{Port: 0} s.conf.TCPListenAddr = &net.TCPAddr{Port: 0}
@ -489,21 +443,6 @@ func createServerTLSConfig(t *testing.T) (*tls.Config, []byte, []byte) {
return &tls.Config{Certificates: []tls.Certificate{cert}, ServerName: tlsServerName, MinVersion: tls.VersionTLS12}, certPem, keyPem return &tls.Config{Certificates: []tls.Certificate{cert}, ServerName: tlsServerName, MinVersion: tls.VersionTLS12}, certPem, keyPem
} }
func createDataDir(t *testing.T) string {
err := os.MkdirAll(dataDir, 0755)
if err != nil {
t.Fatalf("Cannot create %s: %s", dataDir, err)
}
return dataDir
}
func removeDataDir(t *testing.T) {
err := os.RemoveAll(dataDir)
if err != nil {
t.Fatalf("Cannot remove %s: %s", dataDir, err)
}
}
func sendTestMessageAsync(t *testing.T, conn *dns.Conn, g *sync.WaitGroup) { func sendTestMessageAsync(t *testing.T, conn *dns.Conn, g *sync.WaitGroup) {
defer func() { defer func() {
g.Done() g.Done()
@ -607,7 +546,6 @@ func TestIsBlockedIPAllowed(t *testing.T) {
s.conf.AllowedClients = []string{"1.1.1.1", "2.2.0.0/16"} s.conf.AllowedClients = []string{"1.1.1.1", "2.2.0.0/16"}
err := s.Start(nil) err := s.Start(nil)
defer removeDataDir(t)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
} }
@ -631,7 +569,6 @@ func TestIsBlockedIPDisallowed(t *testing.T) {
s.conf.DisallowedClients = []string{"1.1.1.1", "2.2.0.0/16"} s.conf.DisallowedClients = []string{"1.1.1.1", "2.2.0.0/16"}
err := s.Start(nil) err := s.Start(nil)
defer removeDataDir(t)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
} }
@ -655,7 +592,6 @@ func TestIsBlockedIPBlockedDomain(t *testing.T) {
s.conf.BlockedHosts = []string{"host1", "host2"} s.conf.BlockedHosts = []string{"host1", "host2"}
err := s.Start(nil) err := s.Start(nil)
defer removeDataDir(t)
if err != nil { if err != nil {
t.Fatalf("Failed to start server: %s", err) t.Fatalf("Failed to start server: %s", err)
} }

View file

@ -12,6 +12,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/dhcpd" "github.com/AdguardTeam/AdGuardHome/dhcpd"
"github.com/AdguardTeam/AdGuardHome/dnsfilter" "github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/AdguardTeam/AdGuardHome/dnsforward" "github.com/AdguardTeam/AdGuardHome/dnsforward"
"github.com/AdguardTeam/AdGuardHome/querylog"
"github.com/AdguardTeam/AdGuardHome/stats" "github.com/AdguardTeam/AdGuardHome/stats"
"github.com/AdguardTeam/golibs/file" "github.com/AdguardTeam/golibs/file"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
@ -70,6 +71,7 @@ type configuration struct {
transport *http.Transport transport *http.Transport
client *http.Client client *http.Client
stats stats.Stats stats stats.Stats
queryLog querylog.QueryLog
// cached version.json to avoid hammering github.io for each page reload // cached version.json to avoid hammering github.io for each page reload
versionCheckJSON []byte versionCheckJSON []byte
@ -175,6 +177,7 @@ var config = configuration{
BlockingMode: "nxdomain", // mode how to answer filtered requests BlockingMode: "nxdomain", // mode how to answer filtered requests
BlockedResponseTTL: 10, // in seconds BlockedResponseTTL: 10, // in seconds
QueryLogEnabled: true, QueryLogEnabled: true,
QueryLogInterval: 1,
Ratelimit: 20, Ratelimit: 20,
RefuseAny: true, RefuseAny: true,
BootstrapDNS: defaultBootstrap, BootstrapDNS: defaultBootstrap,
@ -274,6 +277,10 @@ func parseConfig() error {
config.DNS.StatsInterval = 1 config.DNS.StatsInterval = 1
} }
if !checkQueryLogInterval(config.DNS.QueryLogInterval) {
config.DNS.QueryLogInterval = 1
}
for _, cy := range config.Clients { for _, cy := range config.Clients {
cli := Client{ cli := Client{
Name: cy.Name, Name: cy.Name,

View file

@ -146,35 +146,6 @@ func handleProtectionDisable(w http.ResponseWriter, r *http.Request) {
httpUpdateConfigReloadDNSReturnOK(w, r) httpUpdateConfigReloadDNSReturnOK(w, r)
} }
// -----
// stats
// -----
func handleQueryLogEnable(w http.ResponseWriter, r *http.Request) {
config.DNS.QueryLogEnabled = true
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleQueryLogDisable(w http.ResponseWriter, r *http.Request) {
config.DNS.QueryLogEnabled = false
httpUpdateConfigReloadDNSReturnOK(w, r)
}
func handleQueryLog(w http.ResponseWriter, r *http.Request) {
data := config.dnsServer.GetQueryLog()
jsonVal, err := json.Marshal(data)
if err != nil {
httpError(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(w, http.StatusInternalServerError, "Unable to write response json: %s", err)
}
}
// ----------------------- // -----------------------
// upstreams configuration // upstreams configuration
// ----------------------- // -----------------------
@ -570,9 +541,6 @@ func registerControlHandlers() {
httpRegister(http.MethodGet, "/control/status", handleStatus) httpRegister(http.MethodGet, "/control/status", handleStatus)
httpRegister(http.MethodPost, "/control/enable_protection", handleProtectionEnable) httpRegister(http.MethodPost, "/control/enable_protection", handleProtectionEnable)
httpRegister(http.MethodPost, "/control/disable_protection", handleProtectionDisable) httpRegister(http.MethodPost, "/control/disable_protection", handleProtectionDisable)
httpRegister(http.MethodGet, "/control/querylog", handleQueryLog)
httpRegister(http.MethodPost, "/control/querylog_enable", handleQueryLogEnable)
httpRegister(http.MethodPost, "/control/querylog_disable", handleQueryLogDisable)
httpRegister(http.MethodPost, "/control/set_upstreams_config", handleSetUpstreamConfig) httpRegister(http.MethodPost, "/control/set_upstreams_config", handleSetUpstreamConfig)
httpRegister(http.MethodPost, "/control/test_upstream_dns", handleTestUpstreamDNS) httpRegister(http.MethodPost, "/control/test_upstream_dns", handleTestUpstreamDNS)
httpRegister(http.MethodPost, "/control/i18n/change_language", handleI18nChangeLanguage) httpRegister(http.MethodPost, "/control/i18n/change_language", handleI18nChangeLanguage)
@ -611,6 +579,7 @@ func registerControlHandlers() {
RegisterClientsHandlers() RegisterClientsHandlers()
registerRewritesHandlers() registerRewritesHandlers()
RegisterBlockedServicesHandlers() RegisterBlockedServicesHandlers()
RegisterQueryLogHandlers()
RegisterStatsHandlers() RegisterStatsHandlers()
http.HandleFunc("/dns-query", postInstall(handleDOH)) http.HandleFunc("/dns-query", postInstall(handleDOH))

91
home/control_querylog.go Normal file
View file

@ -0,0 +1,91 @@
package home
import (
"encoding/json"
"net/http"
"github.com/AdguardTeam/AdGuardHome/querylog"
)
func handleQueryLog(w http.ResponseWriter, r *http.Request) {
data := config.queryLog.GetData()
jsonVal, err := json.Marshal(data)
if err != nil {
httpError(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(w, http.StatusInternalServerError, "Unable to write response json: %s", err)
}
}
func handleQueryLogClear(w http.ResponseWriter, r *http.Request) {
config.queryLog.Clear()
returnOK(w)
}
type qlogConfig struct {
Enabled bool `json:"enabled"`
Interval uint32 `json:"interval"`
}
// Get configuration
func handleQueryLogInfo(w http.ResponseWriter, r *http.Request) {
resp := qlogConfig{}
resp.Enabled = config.DNS.QueryLogEnabled
resp.Interval = config.DNS.QueryLogInterval
jsonVal, err := json.Marshal(resp)
if err != nil {
httpError(w, http.StatusInternalServerError, "json encode: %s", err)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonVal)
if err != nil {
httpError(w, http.StatusInternalServerError, "http write: %s", err)
}
}
// Set configuration
func handleQueryLogConfig(w http.ResponseWriter, r *http.Request) {
reqData := qlogConfig{}
err := json.NewDecoder(r.Body).Decode(&reqData)
if err != nil {
httpError(w, http.StatusBadRequest, "json decode: %s", err)
return
}
if !checkQueryLogInterval(reqData.Interval) {
httpError(w, http.StatusBadRequest, "Unsupported interval")
return
}
config.DNS.QueryLogEnabled = reqData.Enabled
config.DNS.QueryLogInterval = reqData.Interval
_ = config.write()
conf := querylog.Config{
Interval: config.DNS.QueryLogInterval * 24,
}
config.queryLog.Configure(conf)
returnOK(w)
}
func checkQueryLogInterval(i uint32) bool {
return i == 1 || i == 7 || i == 30 || i == 90
}
// RegisterQueryLogHandlers - register handlers
func RegisterQueryLogHandlers() {
httpRegister(http.MethodGet, "/control/querylog", handleQueryLog)
httpRegister(http.MethodGet, "/control/querylog_info", handleQueryLogInfo)
httpRegister(http.MethodPost, "/control/querylog_clear", handleQueryLogClear)
httpRegister(http.MethodPost, "/control/querylog_config", handleQueryLogConfig)
}

View file

@ -9,6 +9,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/dnsfilter" "github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/AdguardTeam/AdGuardHome/dnsforward" "github.com/AdguardTeam/AdGuardHome/dnsforward"
"github.com/AdguardTeam/AdGuardHome/querylog"
"github.com/AdguardTeam/AdGuardHome/stats" "github.com/AdguardTeam/AdGuardHome/stats"
"github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream" "github.com/AdguardTeam/dnsproxy/upstream"
@ -40,7 +41,12 @@ func initDNSServer(baseDir string) {
if err != nil { if err != nil {
log.Fatal("Couldn't initialize statistics module") log.Fatal("Couldn't initialize statistics module")
} }
config.dnsServer = dnsforward.NewServer(baseDir, config.stats) conf := querylog.Config{
BaseDir: baseDir,
Interval: config.DNS.QueryLogInterval * 24,
}
config.queryLog = querylog.New(conf)
config.dnsServer = dnsforward.NewServer(config.stats, config.queryLog)
initRDNS() initRDNS()
} }
@ -186,6 +192,7 @@ func stopDNSServer() error {
} }
config.stats.Close() config.stats.Close()
config.queryLog.Close()
return nil return nil
} }

View file

@ -191,21 +191,42 @@ paths:
description: OK description: OK
schema: schema:
$ref: '#/definitions/QueryLog' $ref: '#/definitions/QueryLog'
/querylog_enable:
post: /querylog_info:
get:
tags: tags:
- log - log
operationId: querylogEnable operationId: queryLogInfo
summary: 'Enable querylog' summary: 'Get query log parameters'
responses: responses:
200: 200:
description: OK description: OK
/querylog_disable: schema:
$ref: "#/definitions/QueryLogConfig"
/querylog_config:
post: post:
tags: tags:
- log - log
operationId: querylogDisable operationId: queryLogConfig
summary: 'Disable filtering' summary: "Set query log parameters"
consumes:
- application/json
parameters:
- in: "body"
name: "body"
schema:
$ref: "#/definitions/QueryLogConfig"
responses:
200:
description: OK
/querylog_clear:
post:
tags:
- log
operationId: querylogClear
summary: 'Clear query log'
responses: responses:
200: 200:
description: OK description: OK
@ -244,6 +265,7 @@ paths:
summary: 'Get statistics parameters' summary: 'Get statistics parameters'
responses: responses:
200: 200:
description: OK
schema: schema:
$ref: "#/definitions/StatsConfig" $ref: "#/definitions/StatsConfig"
description: OK description: OK
@ -1360,6 +1382,18 @@ definitions:
description: "Query log" description: "Query log"
items: items:
$ref: "#/definitions/QueryLogItem" $ref: "#/definitions/QueryLogItem"
QueryLogConfig:
type: "object"
description: "Query log configuration"
properties:
enabled:
type: "boolean"
description: "Is query log enabled"
interval:
type: "integer"
description: "Time period to keep data (1 | 7 | 30 | 90)"
TlsConfig: TlsConfig:
type: "object" type: "object"
description: "TLS configuration settings and status" description: "TLS configuration settings and status"

View file

@ -1,8 +1,9 @@
package dnsforward package querylog
import ( import (
"fmt" "fmt"
"net" "net"
"os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -15,16 +16,14 @@ import (
) )
const ( const (
logBufferCap = 5000 // maximum capacity of logBuffer before it's flushed to disk logBufferCap = 5000 // maximum capacity of logBuffer before it's flushed to disk
queryLogTimeLimit = time.Hour * 24 // how far in the past we care about querylogs queryLogFileName = "querylog.json" // .gz added during compression
queryLogRotationPeriod = time.Hour * 24 // rotate the log every 24 hours queryLogSize = 5000 // maximum API response for /querylog
queryLogFileName = "querylog.json" // .gz added during compression
queryLogSize = 5000 // maximum API response for /querylog
queryLogTopSize = 500 // Keep in memory only top N values
) )
// queryLog is a structure that writes and reads the DNS query log // queryLog is a structure that writes and reads the DNS query log
type queryLog struct { type queryLog struct {
conf Config
logFile string // path to the log file logFile string // path to the log file
logBufferLock sync.RWMutex logBufferLock sync.RWMutex
@ -32,16 +31,53 @@ type queryLog struct {
fileFlushLock sync.Mutex // synchronize a file-flushing goroutine and main thread fileFlushLock sync.Mutex // synchronize a file-flushing goroutine and main thread
flushPending bool // don't start another goroutine while the previous one is still running flushPending bool // don't start another goroutine while the previous one is still running
queryLogCache []*logEntry cache []*logEntry
queryLogLock sync.RWMutex lock sync.RWMutex
} }
// newQueryLog creates a new instance of the query log // newQueryLog creates a new instance of the query log
func newQueryLog(baseDir string) *queryLog { func newQueryLog(conf Config) *queryLog {
l := &queryLog{ l := queryLog{}
logFile: filepath.Join(baseDir, queryLogFileName), l.logFile = filepath.Join(conf.BaseDir, queryLogFileName)
l.conf = conf
go l.periodicQueryLogRotate()
go l.fillFromFile()
return &l
}
func (l *queryLog) Close() {
_ = l.flushLogBuffer(true)
}
func (l *queryLog) Configure(conf Config) {
l.conf = conf
}
// Clear memory buffer and remove the file
func (l *queryLog) Clear() {
l.fileFlushLock.Lock()
defer l.fileFlushLock.Unlock()
l.logBufferLock.Lock()
l.logBuffer = nil
l.flushPending = false
l.logBufferLock.Unlock()
l.lock.Lock()
l.cache = nil
l.lock.Unlock()
err := os.Remove(l.logFile + ".1")
if err != nil {
log.Error("file remove: %s: %s", l.logFile+".1", err)
} }
return l
err = os.Remove(l.logFile)
if err != nil {
log.Error("file remove: %s: %s", l.logFile, err)
}
log.Debug("Query log: cleared")
} }
type logEntry struct { type logEntry struct {
@ -54,17 +90,28 @@ type logEntry struct {
Upstream string `json:",omitempty"` // if empty, means it was cached Upstream string `json:",omitempty"` // if empty, means it was cached
} }
func (l *queryLog) logRequest(question *dns.Msg, answer *dns.Msg, result *dnsfilter.Result, elapsed time.Duration, addr net.Addr, upstream string) *logEntry { // getIPString is a helper function that extracts IP address from net.Addr
func getIPString(addr net.Addr) string {
switch addr := addr.(type) {
case *net.UDPAddr:
return addr.IP.String()
case *net.TCPAddr:
return addr.IP.String()
}
return ""
}
func (l *queryLog) Add(question *dns.Msg, answer *dns.Msg, result *dnsfilter.Result, elapsed time.Duration, addr net.Addr, upstream string) {
var q []byte var q []byte
var a []byte var a []byte
var err error var err error
ip := GetIPString(addr) ip := getIPString(addr)
if question != nil { if question != nil {
q, err = question.Pack() q, err = question.Pack()
if err != nil { if err != nil {
log.Printf("failed to pack question for querylog: %s", err) log.Printf("failed to pack question for querylog: %s", err)
return nil return
} }
} }
@ -72,7 +119,7 @@ func (l *queryLog) logRequest(question *dns.Msg, answer *dns.Msg, result *dnsfil
a, err = answer.Pack() a, err = answer.Pack()
if err != nil { if err != nil {
log.Printf("failed to pack answer for querylog: %s", err) log.Printf("failed to pack answer for querylog: %s", err)
return nil return
} }
} }
@ -101,13 +148,13 @@ func (l *queryLog) logRequest(question *dns.Msg, answer *dns.Msg, result *dnsfil
} }
} }
l.logBufferLock.Unlock() l.logBufferLock.Unlock()
l.queryLogLock.Lock() l.lock.Lock()
l.queryLogCache = append(l.queryLogCache, &entry) l.cache = append(l.cache, &entry)
if len(l.queryLogCache) > queryLogSize { if len(l.cache) > queryLogSize {
toremove := len(l.queryLogCache) - queryLogSize toremove := len(l.cache) - queryLogSize
l.queryLogCache = l.queryLogCache[toremove:] l.cache = l.cache[toremove:]
} }
l.queryLogLock.Unlock() l.lock.Unlock()
// if buffer needs to be flushed to disk, do it now // if buffer needs to be flushed to disk, do it now
if needFlush { if needFlush {
@ -115,16 +162,14 @@ func (l *queryLog) logRequest(question *dns.Msg, answer *dns.Msg, result *dnsfil
// do it in separate goroutine -- we are stalling DNS response this whole time // do it in separate goroutine -- we are stalling DNS response this whole time
go l.flushLogBuffer(false) // nolint go l.flushLogBuffer(false) // nolint
} }
return &entry
} }
// getQueryLogJson returns a map with the current query log ready to be converted to a JSON // getQueryLogJson returns a map with the current query log ready to be converted to a JSON
func (l *queryLog) getQueryLog() []map[string]interface{} { func (l *queryLog) GetData() []map[string]interface{} {
l.queryLogLock.RLock() l.lock.RLock()
values := make([]*logEntry, len(l.queryLogCache)) values := make([]*logEntry, len(l.cache))
copy(values, l.queryLogCache) copy(values, l.cache)
l.queryLogLock.RUnlock() l.lock.RUnlock()
// reverse it so that newest is first // reverse it so that newest is first
for left, right := 0, len(values)-1; left < right; left, right = left+1, right-1 { for left, right := 0, len(values)-1; left < right; left, right = left+1, right-1 {

33
querylog/querylog.go Normal file
View file

@ -0,0 +1,33 @@
package querylog
import (
"net"
"time"
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/miekg/dns"
)
// QueryLog - main interface
type QueryLog interface {
Close()
// Set new configuration at runtime
// Currently only 'Interval' field is supported.
Configure(conf Config)
Add(question *dns.Msg, answer *dns.Msg, result *dnsfilter.Result, elapsed time.Duration, addr net.Addr, upstream string)
GetData() []map[string]interface{}
Clear()
}
// Config - configuration object
type Config struct {
BaseDir string // directory where log file is stored
Interval uint32 // interval to rotate logs (in hours)
}
// New - create instance
func New(conf Config) QueryLog {
return newQueryLog(conf)
}

View file

@ -1,4 +1,4 @@
package dnsforward package querylog
import ( import (
"bytes" "bytes"
@ -11,6 +11,7 @@ import (
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/go-test/deep" "github.com/go-test/deep"
"github.com/miekg/dns"
) )
var ( var (
@ -170,7 +171,7 @@ func (l *queryLog) rotateQueryLog() error {
} }
func (l *queryLog) periodicQueryLogRotate() { func (l *queryLog) periodicQueryLogRotate() {
for range time.Tick(queryLogRotationPeriod) { for range time.Tick(time.Duration(l.conf.Interval) * time.Hour) {
err := l.rotateQueryLog() err := l.rotateQueryLog()
if err != nil { if err != nil {
log.Error("Failed to rotate querylog: %s", err) log.Error("Failed to rotate querylog: %s", err)
@ -178,3 +179,152 @@ func (l *queryLog) periodicQueryLogRotate() {
} }
} }
} }
// Reader is the DB reader context
type Reader struct {
f *os.File
jd *json.Decoder
now time.Time
ql *queryLog
files []string
ifile int
count uint64 // returned elements counter
}
// OpenReader locks the file and returns reader object or nil on error
func (l *queryLog) OpenReader() *Reader {
r := Reader{}
r.ql = l
r.now = time.Now()
return &r
}
// Close closes the reader
func (r *Reader) Close() {
elapsed := time.Since(r.now)
var perunit time.Duration
if r.count > 0 {
perunit = elapsed / time.Duration(r.count)
}
log.Debug("querylog: read %d entries in %v, %v/entry",
r.count, elapsed, perunit)
if r.f != nil {
r.f.Close()
}
}
// BeginRead starts reading
func (r *Reader) BeginRead() {
r.files = []string{
r.ql.logFile,
r.ql.logFile + ".1",
}
}
// Next returns the next entry or nil if reading is finished
func (r *Reader) Next() *logEntry { // nolint
var err error
for {
// open file if needed
if r.f == nil {
if r.ifile == len(r.files) {
return nil
}
fn := r.files[r.ifile]
r.f, err = os.Open(fn)
if err != nil {
log.Error("Failed to open file \"%s\": %s", fn, err)
r.ifile++
continue
}
}
// open decoder if needed
if r.jd == nil {
r.jd = json.NewDecoder(r.f)
}
// check if there's data
if !r.jd.More() {
r.jd = nil
r.f.Close()
r.f = nil
r.ifile++
continue
}
// read data
var entry logEntry
err = r.jd.Decode(&entry)
if err != nil {
log.Error("Failed to decode: %s", err)
// next entry can be fine, try more
continue
}
r.count++
return &entry
}
}
// Total returns the total number of items
func (r *Reader) Total() int {
return 0
}
// Fill cache from file
func (l *queryLog) fillFromFile() {
now := time.Now()
validFrom := now.Unix() - int64(l.conf.Interval*60*60)
r := l.OpenReader()
if r == nil {
return
}
r.BeginRead()
for {
entry := r.Next()
if entry == nil {
break
}
if entry.Time.Unix() < validFrom {
continue
}
if len(entry.Question) == 0 {
log.Printf("entry question is absent, skipping")
continue
}
if entry.Time.After(now) {
log.Printf("t %v vs %v is in the future, ignoring", entry.Time, now)
continue
}
q := new(dns.Msg)
if err := q.Unpack(entry.Question); err != nil {
log.Printf("failed to unpack dns message question: %s", err)
continue
}
if len(q.Question) != 1 {
log.Printf("malformed dns message, has no questions, skipping")
continue
}
l.lock.Lock()
l.cache = append(l.cache, entry)
if len(l.cache) > queryLogSize {
toremove := len(l.cache) - queryLogSize
l.cache = l.cache[toremove:]
}
l.lock.Unlock()
}
r.Close()
}

43
querylog/querylog_test.go Normal file
View file

@ -0,0 +1,43 @@
package querylog
import (
"net"
"testing"
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
func TestQueryLog(t *testing.T) {
conf := Config{
Interval: 1,
}
l := New(conf)
q := dns.Msg{}
q.Question = append(q.Question, dns.Question{
Name: "example.org.",
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
})
a := dns.Msg{}
a.Question = append(a.Question, q.Question[0])
answer := new(dns.A)
answer.Hdr = dns.RR_Header{
Name: q.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
}
answer.A = net.IP{1, 2, 3, 4}
a.Answer = append(a.Answer, answer)
res := dnsfilter.Result{}
l.Add(&q, &a, &res, 0, nil, "upstream")
d := l.GetData()
m := d[0]
mq := m["question"].(map[string]interface{})
assert.True(t, mq["host"].(string) == "example.org")
}