123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678 |
- /*
- Technitium DNS Server
- Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- using DnsServerCore.ApplicationCommon;
- using Microsoft.Data.Sqlite;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Data.Common;
- using System.IO;
- using System.Net;
- using System.Text.Json;
- using System.Threading;
- using System.Threading.Tasks;
- using TechnitiumLibrary;
- using TechnitiumLibrary.Net.Dns;
- using TechnitiumLibrary.Net.Dns.ResourceRecords;
- namespace QueryLogsSqlite
- {
- public sealed class App : IDnsApplication, IDnsQueryLogger
- {
- #region variables
- IDnsServer _dnsServer;
- bool _enableLogging;
- int _maxLogDays;
- int _maxLogRecords;
- bool _useInMemoryDb;
- string _connectionString;
- SqliteConnection _inMemoryConnection;
- readonly ConcurrentQueue<LogEntry> _queuedLogs = new ConcurrentQueue<LogEntry>();
- readonly Timer _queueTimer;
- const int QUEUE_TIMER_INTERVAL = 10000;
- const int BULK_INSERT_COUNT = 1000;
- readonly Timer _cleanupTimer;
- const int CLEAN_UP_TIMER_INITIAL_INTERVAL = 5 * 1000;
- const int CLEAN_UP_TIMER_PERIODIC_INTERVAL = 15 * 60 * 1000;
- #endregion
- #region constructor
- public App()
- {
- _queueTimer = new Timer(async delegate (object state)
- {
- try
- {
- await BulkInsertLogsAsync();
- }
- catch (Exception ex)
- {
- _dnsServer.WriteLog(ex);
- }
- finally
- {
- try
- {
- _queueTimer.Change(QUEUE_TIMER_INTERVAL, Timeout.Infinite);
- }
- catch (ObjectDisposedException)
- { }
- }
- });
- _cleanupTimer = new Timer(async delegate (object state)
- {
- try
- {
- await using (SqliteConnection connection = new SqliteConnection(_connectionString))
- {
- await connection.OpenAsync();
- if (_maxLogRecords > 0)
- {
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "DELETE FROM dns_logs WHERE ROWID IN (SELECT ROWID FROM dns_logs ORDER BY ROWID DESC LIMIT -1 OFFSET @maxLogRecords);";
- command.Parameters.AddWithValue("@maxLogRecords", _maxLogRecords);
- await command.ExecuteNonQueryAsync();
- }
- }
- if (_maxLogDays > 0)
- {
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "DELETE FROM dns_logs WHERE timestamp < @timestamp;";
- command.Parameters.AddWithValue("@timestamp", DateTime.UtcNow.AddDays(_maxLogDays * -1));
- await command.ExecuteNonQueryAsync();
- }
- }
- }
- }
- catch (Exception ex)
- {
- _dnsServer.WriteLog(ex);
- }
- finally
- {
- try
- {
- _cleanupTimer.Change(CLEAN_UP_TIMER_PERIODIC_INTERVAL, Timeout.Infinite);
- }
- catch (ObjectDisposedException)
- { }
- }
- });
- }
- #endregion
- #region IDisposable
- public void Dispose()
- {
- _enableLogging = false; //turn off logging
- if (_queueTimer is not null)
- _queueTimer.Dispose();
- if (_cleanupTimer is not null)
- _cleanupTimer.Dispose();
- BulkInsertLogsAsync().Sync(); //flush any pending logs
- if (_inMemoryConnection is not null)
- {
- _inMemoryConnection.Dispose();
- _inMemoryConnection = null;
- }
- SqliteConnection.ClearAllPools(); //close db file
- }
- #endregion
- #region private
- private async Task BulkInsertLogsAsync()
- {
- try
- {
- List<LogEntry> logs = new List<LogEntry>(BULK_INSERT_COUNT);
- while (true)
- {
- while ((logs.Count < BULK_INSERT_COUNT) && _queuedLogs.TryDequeue(out LogEntry log))
- {
- logs.Add(log);
- }
- if (logs.Count < 1)
- break;
- await using (SqliteConnection connection = new SqliteConnection(_connectionString))
- {
- await connection.OpenAsync();
- await using (DbTransaction transaction = await connection.BeginTransactionAsync())
- {
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "INSERT INTO dns_logs (timestamp, client_ip, protocol, response_type, rcode, qname, qtype, qclass, answer) VALUES (@timestamp, @client_ip, @protocol, @response_type, @rcode, @qname, @qtype, @qclass, @answer);";
- SqliteParameter paramTimestamp = command.Parameters.Add("@timestamp", SqliteType.Text);
- SqliteParameter paramClientIp = command.Parameters.Add("@client_ip", SqliteType.Text);
- SqliteParameter paramProtocol = command.Parameters.Add("@protocol", SqliteType.Integer);
- SqliteParameter paramResponseType = command.Parameters.Add("@response_type", SqliteType.Integer);
- SqliteParameter paramRcode = command.Parameters.Add("@rcode", SqliteType.Integer);
- SqliteParameter paramQname = command.Parameters.Add("@qname", SqliteType.Text);
- SqliteParameter paramQtype = command.Parameters.Add("@qtype", SqliteType.Integer);
- SqliteParameter paramQclass = command.Parameters.Add("@qclass", SqliteType.Integer);
- SqliteParameter paramAnswer = command.Parameters.Add("@answer", SqliteType.Text);
- foreach (LogEntry log in logs)
- {
- paramTimestamp.Value = log.Timestamp.ToString("yyyy-MM-dd HH:mm:ss.FFFFFFF");
- paramClientIp.Value = log.RemoteEP.Address.ToString();
- paramProtocol.Value = (int)log.Protocol;
- if (log.Response.Tag == null)
- paramResponseType.Value = (int)DnsServerResponseType.Recursive;
- else
- paramResponseType.Value = (int)(DnsServerResponseType)log.Response.Tag;
- paramRcode.Value = (int)log.Response.RCODE;
- if (log.Request.Question.Count > 0)
- {
- DnsQuestionRecord query = log.Request.Question[0];
- paramQname.Value = query.Name.ToLowerInvariant();
- paramQtype.Value = (int)query.Type;
- paramQclass.Value = (int)query.Class;
- }
- else
- {
- paramQname.Value = DBNull.Value;
- paramQtype.Value = DBNull.Value;
- paramQclass.Value = DBNull.Value;
- }
- if (log.Response.Answer.Count == 0)
- {
- paramAnswer.Value = DBNull.Value;
- }
- else if ((log.Response.Answer.Count > 2) && log.Response.IsZoneTransfer)
- {
- paramAnswer.Value = "[ZONE TRANSFER]";
- }
- else
- {
- string answer = null;
- for (int i = 0; i < log.Response.Answer.Count; i++)
- {
- if (answer is null)
- answer = log.Response.Answer[i].RDATA.ToString();
- else
- answer += ", " + log.Response.Answer[i].RDATA.ToString();
- }
- paramAnswer.Value = answer;
- }
- await command.ExecuteNonQueryAsync();
- }
- await transaction.CommitAsync();
- }
- }
- }
- logs.Clear();
- }
- }
- catch (Exception ex)
- {
- if (_dnsServer is not null)
- _dnsServer.WriteLog(ex);
- }
- }
- #endregion
- #region public
- public async Task InitializeAsync(IDnsServer dnsServer, string config)
- {
- _dnsServer = dnsServer;
- using JsonDocument jsonDocument = JsonDocument.Parse(config);
- JsonElement jsonConfig = jsonDocument.RootElement;
- _enableLogging = jsonConfig.GetPropertyValue("enableLogging", true);
- _maxLogDays = jsonConfig.GetPropertyValue("maxLogDays", 0);
- _maxLogRecords = jsonConfig.GetPropertyValue("maxLogRecords", 0);
- _useInMemoryDb = jsonConfig.GetPropertyValue("useInMemoryDb", false);
- if (_useInMemoryDb)
- {
- if (_inMemoryConnection is null)
- {
- SqliteConnection.ClearAllPools(); //close db file, if any
- _connectionString = "Data Source=QueryLogs;Mode=Memory;Cache=Shared";
- _inMemoryConnection = new SqliteConnection(_connectionString);
- await _inMemoryConnection.OpenAsync();
- }
- }
- else
- {
- if (_inMemoryConnection is not null)
- {
- await _inMemoryConnection.DisposeAsync();
- _inMemoryConnection = null;
- }
- string sqliteDbPath = jsonConfig.GetPropertyValue("sqliteDbPath", "querylogs.db");
- string connectionString = jsonConfig.GetPropertyValue("connectionString", "Data Source='{sqliteDbPath}'; Cache=Shared;");
- if (!Path.IsPathRooted(sqliteDbPath))
- sqliteDbPath = Path.Combine(_dnsServer.ApplicationFolder, sqliteDbPath);
- connectionString = connectionString.Replace("{sqliteDbPath}", sqliteDbPath);
- if ((_connectionString is not null) && !_connectionString.Equals(connectionString))
- SqliteConnection.ClearAllPools(); //close previous db file
- _connectionString = connectionString;
- }
- await using (SqliteConnection connection = new SqliteConnection(_connectionString))
- {
- await connection.OpenAsync();
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = @"
- CREATE TABLE IF NOT EXISTS dns_logs
- (
- dlid INTEGER PRIMARY KEY,
- timestamp DATETIME NOT NULL,
- client_ip VARCHAR(39) NOT NULL,
- protocol TINYINT NOT NULL,
- response_type TINYINT NOT NULL,
- rcode TINYINT NOT NULL,
- qname VARCHAR(255),
- qtype SMALLINT,
- qclass SMALLINT,
- answer TEXT
- );
- ";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_timestamp ON dns_logs (timestamp);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_client_ip ON dns_logs (client_ip);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_protocol ON dns_logs (protocol);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_response_type ON dns_logs (response_type);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_rcode ON dns_logs (rcode);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_qname ON dns_logs (qname);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_qtype ON dns_logs (qtype);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_qclass ON dns_logs (qclass);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_timestamp_client_ip ON dns_logs (timestamp, client_ip);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_timestamp_qname ON dns_logs (timestamp, qname);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_client_qname ON dns_logs (client_ip, qname);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_query ON dns_logs (qname, qtype);";
- await command.ExecuteNonQueryAsync();
- }
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "CREATE INDEX IF NOT EXISTS index_all ON dns_logs (timestamp, client_ip, protocol, response_type, rcode, qname, qtype, qclass);";
- await command.ExecuteNonQueryAsync();
- }
- }
- if (_enableLogging)
- _queueTimer.Change(QUEUE_TIMER_INTERVAL, Timeout.Infinite);
- else
- _queueTimer.Change(Timeout.Infinite, Timeout.Infinite);
- if ((_maxLogDays > 0) || (_maxLogRecords > 0))
- _cleanupTimer.Change(CLEAN_UP_TIMER_INITIAL_INTERVAL, Timeout.Infinite);
- else
- _cleanupTimer.Change(Timeout.Infinite, Timeout.Infinite);
- if (!jsonConfig.TryGetProperty("maxLogRecords", out _))
- {
- config = config.Replace("\"sqliteDbPath\"", "\"maxLogRecords\": 0,\r\n \"useInMemoryDb\": false,\r\n \"sqliteDbPath\"");
- await File.WriteAllTextAsync(Path.Combine(dnsServer.ApplicationFolder, "dnsApp.config"), config);
- }
- }
- public Task InsertLogAsync(DateTime timestamp, DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram response)
- {
- if (_enableLogging)
- _queuedLogs.Enqueue(new LogEntry(timestamp, request, remoteEP, protocol, response));
- return Task.CompletedTask;
- }
- public async Task<DnsLogPage> QueryLogsAsync(long pageNumber, int entriesPerPage, bool descendingOrder, DateTime? start, DateTime? end, IPAddress clientIpAddress, DnsTransportProtocol? protocol, DnsServerResponseType? responseType, DnsResponseCode? rcode, string qname, DnsResourceRecordType? qtype, DnsClass? qclass)
- {
- if (pageNumber == 0)
- pageNumber = 1;
- if (qname is not null)
- qname = qname.ToLowerInvariant();
- string whereClause = string.Empty;
- if (start is not null)
- whereClause += "timestamp >= @start AND ";
- if (end is not null)
- whereClause += "timestamp <= @end AND ";
- if (clientIpAddress is not null)
- whereClause += "client_ip = @client_ip AND ";
- if (protocol is not null)
- whereClause += "protocol = @protocol AND ";
- if (responseType is not null)
- whereClause += "response_type = @response_type AND ";
- if (rcode is not null)
- whereClause += "rcode = @rcode AND ";
- if (qname is not null)
- {
- if (qname.Contains('*'))
- {
- whereClause += "qname like @qname AND ";
- qname = qname.Replace("*", "%");
- }
- else
- {
- whereClause += "qname = @qname AND ";
- }
- }
- if (qtype is not null)
- whereClause += "qtype = @qtype AND ";
- if (qclass is not null)
- whereClause += "qclass = @qclass AND ";
- if (!string.IsNullOrEmpty(whereClause))
- whereClause = whereClause.Substring(0, whereClause.Length - 5);
- await using (SqliteConnection connection = new SqliteConnection(_connectionString))
- {
- await connection.OpenAsync();
- //find total entries
- long totalEntries;
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = "SELECT Count(*) FROM dns_logs" + (string.IsNullOrEmpty(whereClause) ? ";" : " WHERE " + whereClause + ";");
- if (start is not null)
- command.Parameters.AddWithValue("@start", start);
- if (end is not null)
- command.Parameters.AddWithValue("@end", end);
- if (clientIpAddress is not null)
- command.Parameters.AddWithValue("@client_ip", clientIpAddress.ToString());
- if (protocol is not null)
- command.Parameters.AddWithValue("@protocol", (byte)protocol);
- if (responseType is not null)
- command.Parameters.AddWithValue("@response_type", (byte)responseType);
- if (rcode is not null)
- command.Parameters.AddWithValue("@rcode", (byte)rcode);
- if (qname is not null)
- command.Parameters.AddWithValue("@qname", qname);
- if (qtype is not null)
- command.Parameters.AddWithValue("@qtype", (ushort)qtype);
- if (qclass is not null)
- command.Parameters.AddWithValue("@qclass", (ushort)qclass);
- totalEntries = (long)await command.ExecuteScalarAsync();
- }
- long totalPages = (totalEntries / entriesPerPage) + (totalEntries % entriesPerPage > 0 ? 1 : 0);
- if ((pageNumber > totalPages) || (pageNumber < 0))
- pageNumber = totalPages;
- long endRowNum;
- long startRowNum;
- if (descendingOrder)
- {
- endRowNum = totalEntries - ((pageNumber - 1) * entriesPerPage);
- startRowNum = endRowNum - entriesPerPage;
- }
- else
- {
- endRowNum = pageNumber * entriesPerPage;
- startRowNum = endRowNum - entriesPerPage;
- }
- List<DnsLogEntry> entries = new List<DnsLogEntry>(entriesPerPage);
- await using (SqliteCommand command = connection.CreateCommand())
- {
- command.CommandText = @"
- SELECT * FROM (
- SELECT
- ROW_NUMBER() OVER (
- ORDER BY dlid
- ) row_num,
- timestamp,
- client_ip,
- protocol,
- response_type,
- rcode,
- qname,
- qtype,
- qclass,
- answer
- FROM
- dns_logs
- " + (string.IsNullOrEmpty(whereClause) ? "" : "WHERE " + whereClause) + @"
- ) t
- WHERE
- row_num > @start_row_num AND row_num <= @end_row_num
- ORDER BY row_num" + (descendingOrder ? " DESC" : "");
- command.Parameters.AddWithValue("@start_row_num", startRowNum);
- command.Parameters.AddWithValue("@end_row_num", endRowNum);
- if (start is not null)
- command.Parameters.AddWithValue("@start", start);
- if (end is not null)
- command.Parameters.AddWithValue("@end", end);
- if (clientIpAddress is not null)
- command.Parameters.AddWithValue("@client_ip", clientIpAddress.ToString());
- if (protocol is not null)
- command.Parameters.AddWithValue("@protocol", (byte)protocol);
- if (responseType is not null)
- command.Parameters.AddWithValue("@response_type", (byte)responseType);
- if (rcode is not null)
- command.Parameters.AddWithValue("@rcode", (byte)rcode);
- if (qname is not null)
- command.Parameters.AddWithValue("@qname", qname);
- if (qtype is not null)
- command.Parameters.AddWithValue("@qtype", (ushort)qtype);
- if (qclass is not null)
- command.Parameters.AddWithValue("@qclass", (ushort)qclass);
- await using (SqliteDataReader reader = await command.ExecuteReaderAsync())
- {
- while (await reader.ReadAsync())
- {
- DnsQuestionRecord question;
- if (reader.IsDBNull(6))
- question = null;
- else
- question = new DnsQuestionRecord(reader.GetString(6), (DnsResourceRecordType)reader.GetInt32(7), (DnsClass)reader.GetInt32(8), false);
- string answer;
- if (reader.IsDBNull(9))
- answer = null;
- else
- answer = reader.GetString(9);
- entries.Add(new DnsLogEntry(reader.GetInt64(0), reader.GetDateTime(1), IPAddress.Parse(reader.GetString(2)), (DnsTransportProtocol)reader.GetByte(3), (DnsServerResponseType)reader.GetByte(4), (DnsResponseCode)reader.GetByte(5), question, answer));
- }
- }
- }
- return new DnsLogPage(pageNumber, totalPages, totalEntries, entries);
- }
- }
- #endregion
- #region properties
- public string Description
- { get { return "Logs all incoming DNS requests and their responses in a Sqlite database that can be queried from the DNS Server web console. The query logging throughput is limited by the disk throughput on which the Sqlite db file is stored. This app is not recommended to be used with very high throughput (more than 20,000 requests/second)."; } }
- #endregion
- class LogEntry
- {
- #region variables
- public readonly DateTime Timestamp;
- public readonly DnsDatagram Request;
- public readonly IPEndPoint RemoteEP;
- public readonly DnsTransportProtocol Protocol;
- public readonly DnsDatagram Response;
- #endregion
- #region constructor
- public LogEntry(DateTime timestamp, DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram response)
- {
- Timestamp = timestamp;
- Request = request;
- RemoteEP = remoteEP;
- Protocol = protocol;
- Response = response;
- }
- #endregion
- }
- }
- }
|