IDnsQueryLogger.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2023 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Net;
  18. using System.Threading.Tasks;
  19. using TechnitiumLibrary.Net.Dns;
  20. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  21. namespace DnsServerCore.ApplicationCommon
  22. {
  23. public enum DnsServerResponseType : byte
  24. {
  25. Authoritative = 1,
  26. Recursive = 2,
  27. Cached = 3,
  28. Blocked = 4,
  29. UpstreamBlocked = 5,
  30. CacheBlocked = 6
  31. }
  32. /// <summary>
  33. /// Allows a DNS App to log incoming DNS requests and their corresponding responses.
  34. /// </summary>
  35. public interface IDnsQueryLogger
  36. {
  37. /// <summary>
  38. /// Allows a DNS App to log incoming DNS requests and responses. This method is called by the DNS Server after an incoming request is processed and a response is sent.
  39. /// </summary>
  40. /// <param name="timestamp">The time stamp of the log entry.</param>
  41. /// <param name="request">The incoming DNS request that was received.</param>
  42. /// <param name="remoteEP">The end point (IP address and port) of the client making the request.</param>
  43. /// <param name="protocol">The protocol using which the request was received.</param>
  44. /// <param name="response">The DNS response that was sent.</param>
  45. Task InsertLogAsync(DateTime timestamp, DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, DnsDatagram response);
  46. /// <summary>
  47. /// Allows DNS Server HTTP API to query the logs recorded by the DNS App.
  48. /// </summary>
  49. /// <param name="pageNumber">The page number to be displayed to the user.</param>
  50. /// <param name="entriesPerPage">Total entries per page.</param>
  51. /// <param name="descendingOrder">Lists log entries in descending order.</param>
  52. /// <param name="start">Optional parameter to filter records by start date time.</param>
  53. /// <param name="end">Optional parameter to filter records by end date time.</param>
  54. /// <param name="clientIpAddress">Optional parameter to filter records by the client IP address.</param>
  55. /// <param name="protocol">Optional parameter to filter records by the DNS transport protocol.</param>
  56. /// <param name="responseType">Optional parameter to filter records by the type of response.</param>
  57. /// <param name="rcode">Optional parameter to filter records by the response code.</param>
  58. /// <param name="qname">Optional parameter to filter records by the request QNAME.</param>
  59. /// <param name="qtype">Optional parameter to filter records by the request QTYPE.</param>
  60. /// <param name="qclass">Optional parameter to filter records by the request QCLASS.</param>
  61. /// <returns>The <code>DnsLogPage</code> object that contains all the entries in the requested page number.</returns>
  62. 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);
  63. }
  64. public class DnsLogPage
  65. {
  66. #region variables
  67. readonly long _pageNumber;
  68. readonly long _totalPages;
  69. readonly long _totalEntries;
  70. readonly IReadOnlyList<DnsLogEntry> _entries;
  71. #endregion
  72. #region constructor
  73. /// <summary>
  74. /// Creates a new object initialized with all the log page parameters.
  75. /// </summary>
  76. /// <param name="pageNumber">The actual page number of the selected data set.</param>
  77. /// <param name="totalPages">The total pages for the selected data set.</param>
  78. /// <param name="totalEntries">The total number of entries in the selected data set.</param>
  79. /// <param name="entries">The DNS log entries in this page.</param>
  80. public DnsLogPage(long pageNumber, long totalPages, long totalEntries, IReadOnlyList<DnsLogEntry> entries)
  81. {
  82. _pageNumber = pageNumber;
  83. _totalPages = totalPages;
  84. _totalEntries = totalEntries;
  85. _entries = entries;
  86. }
  87. #endregion
  88. #region properties
  89. /// <summary>
  90. /// The actual page number of the selected data set.
  91. /// </summary>
  92. public long PageNumber
  93. { get { return _pageNumber; } }
  94. /// <summary>
  95. /// The total pages for the selected data set.
  96. /// </summary>
  97. public long TotalPages
  98. { get { return _totalPages; } }
  99. /// <summary>
  100. /// The total number of entries in the selected data set.
  101. /// </summary>
  102. public long TotalEntries
  103. { get { return _totalEntries; } }
  104. /// <summary>
  105. /// The DNS log entries in this page.
  106. /// </summary>
  107. public IReadOnlyList<DnsLogEntry> Entries
  108. { get { return _entries; } }
  109. #endregion
  110. }
  111. public class DnsLogEntry
  112. {
  113. #region variables
  114. readonly long _rowNumber;
  115. readonly DateTime _timestamp;
  116. readonly IPAddress _clientIpAddress;
  117. readonly DnsTransportProtocol _protocol;
  118. readonly DnsServerResponseType _responseType;
  119. readonly DnsResponseCode _rcode;
  120. readonly DnsQuestionRecord _question;
  121. readonly string _answer;
  122. #endregion
  123. #region constructor
  124. /// <summary>
  125. /// Creates a new object initialized with all the log entry parameters.
  126. /// </summary>
  127. /// <param name="rowNumber">The row number of the entry in the selected data set.</param>
  128. /// <param name="timestamp">The time stamp of the log entry.</param>
  129. /// <param name="clientIpAddress">The client IP address of the request.</param>
  130. /// <param name="protocol">The DNS transport protocol of the request.</param>
  131. /// <param name="responseType">The type of response sent by the DNS server.</param>
  132. /// <param name="rcode">The response code sent by the DNS server.</param>
  133. /// <param name="question">The question section in the request.</param>
  134. /// <param name="answer">The answer in text format sent by the DNS server.</param>
  135. public DnsLogEntry(long rowNumber, DateTime timestamp, IPAddress clientIpAddress, DnsTransportProtocol protocol, DnsServerResponseType responseType, DnsResponseCode rcode, DnsQuestionRecord question, string answer)
  136. {
  137. _rowNumber = rowNumber;
  138. _timestamp = timestamp;
  139. _clientIpAddress = clientIpAddress;
  140. _protocol = protocol;
  141. _responseType = responseType;
  142. _rcode = rcode;
  143. _question = question;
  144. _answer = answer;
  145. switch (_timestamp.Kind)
  146. {
  147. case DateTimeKind.Local:
  148. _timestamp = _timestamp.ToUniversalTime();
  149. break;
  150. case DateTimeKind.Unspecified:
  151. _timestamp = DateTime.SpecifyKind(_timestamp, DateTimeKind.Utc);
  152. break;
  153. }
  154. }
  155. #endregion
  156. #region properties
  157. /// <summary>
  158. /// The row number of the entry in the selected data set.
  159. /// </summary>
  160. public long RowNumber
  161. { get { return _rowNumber; } }
  162. /// <summary>
  163. /// The time stamp of the log entry.
  164. /// </summary>
  165. public DateTime Timestamp
  166. { get { return _timestamp; } }
  167. /// <summary>
  168. /// The client IP address of the request.
  169. /// </summary>
  170. public IPAddress ClientIpAddress
  171. { get { return _clientIpAddress; } }
  172. /// <summary>
  173. /// The DNS transport protocol of the request.
  174. /// </summary>
  175. public DnsTransportProtocol Protocol
  176. { get { return _protocol; } }
  177. /// <summary>
  178. /// The type of response sent by the DNS server.
  179. /// </summary>
  180. public DnsServerResponseType ResponseType
  181. { get { return _responseType; } }
  182. /// <summary>
  183. /// The response code sent by the DNS server.
  184. /// </summary>
  185. public DnsResponseCode RCODE
  186. { get { return _rcode; } }
  187. /// <summary>
  188. /// The question section in the request.
  189. /// </summary>
  190. public DnsQuestionRecord Question
  191. { get { return _question; } }
  192. /// <summary>
  193. /// The answer in text format sent by the DNS server.
  194. /// </summary>
  195. public string Answer
  196. { get { return _answer; } }
  197. #endregion
  198. }
  199. }