WebServiceLogsApi.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2021 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 DnsServerCore.ApplicationCommon;
  16. using DnsServerCore.Dns.Applications;
  17. using Newtonsoft.Json;
  18. using System;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Net;
  22. using System.Threading.Tasks;
  23. using TechnitiumLibrary.Net;
  24. using TechnitiumLibrary.Net.Dns;
  25. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  26. namespace DnsServerCore
  27. {
  28. class WebServiceLogsApi
  29. {
  30. #region variables
  31. readonly DnsWebService _dnsWebService;
  32. #endregion
  33. #region constructor
  34. public WebServiceLogsApi(DnsWebService dnsWebService)
  35. {
  36. _dnsWebService = dnsWebService;
  37. }
  38. #endregion
  39. #region public
  40. public void ListLogs(JsonTextWriter jsonWriter)
  41. {
  42. string[] logFiles = _dnsWebService.Log.ListLogFiles();
  43. Array.Sort(logFiles);
  44. Array.Reverse(logFiles);
  45. jsonWriter.WritePropertyName("logFiles");
  46. jsonWriter.WriteStartArray();
  47. foreach (string logFile in logFiles)
  48. {
  49. jsonWriter.WriteStartObject();
  50. jsonWriter.WritePropertyName("fileName");
  51. jsonWriter.WriteValue(Path.GetFileNameWithoutExtension(logFile));
  52. jsonWriter.WritePropertyName("size");
  53. jsonWriter.WriteValue(WebUtilities.GetFormattedSize(new FileInfo(logFile).Length));
  54. jsonWriter.WriteEndObject();
  55. }
  56. jsonWriter.WriteEndArray();
  57. }
  58. public void DeleteLog(HttpListenerRequest request)
  59. {
  60. string log = request.QueryString["log"];
  61. if (string.IsNullOrEmpty(log))
  62. throw new DnsWebServiceException("Parameter 'log' missing.");
  63. _dnsWebService.Log.DeleteLog(log);
  64. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Log file was deleted: " + log);
  65. }
  66. public void DeleteAllLogs(HttpListenerRequest request)
  67. {
  68. _dnsWebService.Log.DeleteAllLogs();
  69. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] All log files were deleted.");
  70. }
  71. public void DeleteAllStats(HttpListenerRequest request)
  72. {
  73. _dnsWebService.DnsServer.StatsManager.DeleteAllStats();
  74. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] All stats files were deleted.");
  75. }
  76. public async Task QueryLogsAsync(HttpListenerRequest request, JsonTextWriter jsonWriter)
  77. {
  78. string name = request.QueryString["name"];
  79. if (string.IsNullOrEmpty(name))
  80. throw new DnsWebServiceException("Parameter 'name' missing.");
  81. string classPath = request.QueryString["classPath"];
  82. if (string.IsNullOrEmpty(classPath))
  83. throw new DnsWebServiceException("Parameter 'classPath' missing.");
  84. if (!_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication application))
  85. throw new DnsWebServiceException("DNS application was not found: " + name);
  86. if (!application.DnsQueryLoggers.TryGetValue(classPath, out IDnsQueryLogger logger))
  87. throw new DnsWebServiceException("DNS application '" + classPath + "' class path was not found: " + name);
  88. long pageNumber;
  89. string strPageNumber = request.QueryString["pageNumber"];
  90. if (string.IsNullOrEmpty(strPageNumber))
  91. pageNumber = 1;
  92. else
  93. pageNumber = long.Parse(strPageNumber);
  94. int entriesPerPage;
  95. string strEntriesPerPage = request.QueryString["entriesPerPage"];
  96. if (string.IsNullOrEmpty(strEntriesPerPage))
  97. entriesPerPage = 25;
  98. else
  99. entriesPerPage = int.Parse(strEntriesPerPage);
  100. bool descendingOrder;
  101. string strDescendingOrder = request.QueryString["descendingOrder"];
  102. if (string.IsNullOrEmpty(strDescendingOrder))
  103. descendingOrder = true;
  104. else
  105. descendingOrder = bool.Parse(strDescendingOrder);
  106. DateTime? start;
  107. string strStart = request.QueryString["start"];
  108. if (string.IsNullOrEmpty(strStart))
  109. start = null;
  110. else
  111. start = DateTime.ParseExact(strStart, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal | DateTimeStyles.AdjustToUniversal);
  112. DateTime? end;
  113. string strEnd = request.QueryString["end"];
  114. if (string.IsNullOrEmpty(strEnd))
  115. end = null;
  116. else
  117. end = DateTime.ParseExact(strEnd, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal | DateTimeStyles.AdjustToUniversal);
  118. IPAddress clientIpAddress;
  119. string strClientIpAddress = request.QueryString["clientIpAddress"];
  120. if (string.IsNullOrEmpty(strClientIpAddress))
  121. clientIpAddress = null;
  122. else
  123. clientIpAddress = IPAddress.Parse(strClientIpAddress);
  124. DnsTransportProtocol? protocol;
  125. string strProtocol = request.QueryString["protocol"];
  126. if (string.IsNullOrEmpty(strProtocol))
  127. protocol = null;
  128. else
  129. protocol = Enum.Parse<DnsTransportProtocol>(strProtocol, true);
  130. DnsServerResponseType? responseType;
  131. string strResponseType = request.QueryString["responseType"];
  132. if (string.IsNullOrEmpty(strResponseType))
  133. responseType = null;
  134. else
  135. responseType = Enum.Parse<DnsServerResponseType>(strResponseType, true);
  136. DnsResponseCode? rcode;
  137. string strRcode = request.QueryString["rcode"];
  138. if (string.IsNullOrEmpty(strRcode))
  139. rcode = null;
  140. else
  141. rcode = Enum.Parse<DnsResponseCode>(strRcode, true);
  142. string qname = request.QueryString["qname"];
  143. if (string.IsNullOrEmpty(qname))
  144. qname = null;
  145. DnsResourceRecordType? qtype;
  146. string strQtype = request.QueryString["qtype"];
  147. if (string.IsNullOrEmpty(strQtype))
  148. qtype = null;
  149. else
  150. qtype = Enum.Parse<DnsResourceRecordType>(strQtype, true);
  151. DnsClass? qclass;
  152. string strQclass = request.QueryString["qclass"];
  153. if (string.IsNullOrEmpty(strQclass))
  154. qclass = null;
  155. else
  156. qclass = Enum.Parse<DnsClass>(strQclass, true);
  157. DnsLogPage page = await logger.QueryLogsAsync(pageNumber, entriesPerPage, descendingOrder, start, end, clientIpAddress, protocol, responseType, rcode, qname, qtype, qclass);
  158. jsonWriter.WritePropertyName("pageNumber");
  159. jsonWriter.WriteValue(page.PageNumber);
  160. jsonWriter.WritePropertyName("totalPages");
  161. jsonWriter.WriteValue(page.TotalPages);
  162. jsonWriter.WritePropertyName("totalEntries");
  163. jsonWriter.WriteValue(page.TotalEntries);
  164. jsonWriter.WritePropertyName("entries");
  165. jsonWriter.WriteStartArray();
  166. foreach (DnsLogEntry entry in page.Entries)
  167. {
  168. jsonWriter.WriteStartObject();
  169. jsonWriter.WritePropertyName("rowNumber");
  170. jsonWriter.WriteValue(entry.RowNumber);
  171. jsonWriter.WritePropertyName("timestamp");
  172. jsonWriter.WriteValue(entry.Timestamp);
  173. jsonWriter.WritePropertyName("clientIpAddress");
  174. jsonWriter.WriteValue(entry.ClientIpAddress.ToString());
  175. jsonWriter.WritePropertyName("protocol");
  176. jsonWriter.WriteValue(entry.Protocol.ToString());
  177. jsonWriter.WritePropertyName("responseType");
  178. jsonWriter.WriteValue(entry.ResponseType.ToString());
  179. jsonWriter.WritePropertyName("rcode");
  180. jsonWriter.WriteValue(entry.RCODE.ToString());
  181. jsonWriter.WritePropertyName("qname");
  182. jsonWriter.WriteValue(entry.Question?.Name);
  183. jsonWriter.WritePropertyName("qtype");
  184. jsonWriter.WriteValue(entry.Question?.Type.ToString());
  185. jsonWriter.WritePropertyName("qclass");
  186. jsonWriter.WriteValue(entry.Question?.Class.ToString());
  187. jsonWriter.WritePropertyName("answer");
  188. jsonWriter.WriteValue(entry.Answer);
  189. jsonWriter.WriteEndObject();
  190. }
  191. jsonWriter.WriteEndArray();
  192. }
  193. #endregion
  194. }
  195. }