WebServiceOtherZonesApi.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2022 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.Dns.Zones;
  16. using Newtonsoft.Json;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Net;
  20. using TechnitiumLibrary.Net.Dns;
  21. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  22. namespace DnsServerCore
  23. {
  24. class WebServiceOtherZonesApi
  25. {
  26. #region variables
  27. readonly DnsWebService _dnsWebService;
  28. #endregion
  29. #region constructor
  30. public WebServiceOtherZonesApi(DnsWebService dnsWebService)
  31. {
  32. _dnsWebService = dnsWebService;
  33. }
  34. #endregion
  35. #region public
  36. #region cache api
  37. public void FlushCache(HttpListenerRequest request)
  38. {
  39. _dnsWebService.DnsServer.CacheZoneManager.Flush();
  40. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Cache was flushed.");
  41. }
  42. public void ListCachedZones(HttpListenerRequest request, JsonTextWriter jsonWriter)
  43. {
  44. string domain = request.QueryString["domain"];
  45. if (domain == null)
  46. domain = "";
  47. string direction = request.QueryString["direction"];
  48. List<string> subZones = new List<string>();
  49. List<DnsResourceRecord> records = new List<DnsResourceRecord>();
  50. while (true)
  51. {
  52. subZones.Clear();
  53. records.Clear();
  54. _dnsWebService.DnsServer.CacheZoneManager.ListSubDomains(domain, subZones);
  55. _dnsWebService.DnsServer.CacheZoneManager.ListAllRecords(domain, records);
  56. if (records.Count > 0)
  57. break;
  58. if (subZones.Count != 1)
  59. break;
  60. if (direction == "up")
  61. {
  62. if (domain.Length == 0)
  63. break;
  64. int i = domain.IndexOf('.');
  65. if (i < 0)
  66. domain = "";
  67. else
  68. domain = domain.Substring(i + 1);
  69. }
  70. else if (domain.Length == 0)
  71. {
  72. domain = subZones[0];
  73. }
  74. else
  75. {
  76. domain = subZones[0] + "." + domain;
  77. }
  78. }
  79. subZones.Sort();
  80. jsonWriter.WritePropertyName("domain");
  81. jsonWriter.WriteValue(domain);
  82. jsonWriter.WritePropertyName("zones");
  83. jsonWriter.WriteStartArray();
  84. if (domain.Length != 0)
  85. domain = "." + domain;
  86. foreach (string subZone in subZones)
  87. jsonWriter.WriteValue(subZone + domain);
  88. jsonWriter.WriteEndArray();
  89. WebServiceZonesApi.WriteRecordsAsJson(records, jsonWriter, false);
  90. }
  91. public void DeleteCachedZone(HttpListenerRequest request)
  92. {
  93. string domain = request.QueryString["domain"];
  94. if (string.IsNullOrEmpty(domain))
  95. throw new DnsWebServiceException("Parameter 'domain' missing.");
  96. if (_dnsWebService.DnsServer.CacheZoneManager.DeleteZone(domain))
  97. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Cached zone was deleted: " + domain);
  98. }
  99. #endregion
  100. #region allowed zones api
  101. public void ListAllowedZones(HttpListenerRequest request, JsonTextWriter jsonWriter)
  102. {
  103. string domain = request.QueryString["domain"];
  104. if (domain == null)
  105. domain = "";
  106. string direction = request.QueryString["direction"];
  107. List<string> subZones = new List<string>();
  108. List<DnsResourceRecord> records = new List<DnsResourceRecord>();
  109. while (true)
  110. {
  111. subZones.Clear();
  112. records.Clear();
  113. _dnsWebService.DnsServer.AllowedZoneManager.ListSubDomains(domain, subZones);
  114. _dnsWebService.DnsServer.AllowedZoneManager.ListAllRecords(domain, records);
  115. if (records.Count > 0)
  116. break;
  117. if (subZones.Count != 1)
  118. break;
  119. if (direction == "up")
  120. {
  121. if (domain.Length == 0)
  122. break;
  123. int i = domain.IndexOf('.');
  124. if (i < 0)
  125. domain = "";
  126. else
  127. domain = domain.Substring(i + 1);
  128. }
  129. else if (domain.Length == 0)
  130. {
  131. domain = subZones[0];
  132. }
  133. else
  134. {
  135. domain = subZones[0] + "." + domain;
  136. }
  137. }
  138. subZones.Sort();
  139. jsonWriter.WritePropertyName("domain");
  140. jsonWriter.WriteValue(domain);
  141. jsonWriter.WritePropertyName("zones");
  142. jsonWriter.WriteStartArray();
  143. if (domain.Length != 0)
  144. domain = "." + domain;
  145. foreach (string subZone in subZones)
  146. jsonWriter.WriteValue(subZone + domain);
  147. jsonWriter.WriteEndArray();
  148. WebServiceZonesApi.WriteRecordsAsJson(new List<DnsResourceRecord>(records), jsonWriter, false);
  149. }
  150. public void ImportAllowedZones(HttpListenerRequest request)
  151. {
  152. if (!request.ContentType.StartsWith("application/x-www-form-urlencoded"))
  153. throw new DnsWebServiceException("Invalid content type. Expected application/x-www-form-urlencoded.");
  154. string formRequest;
  155. using (StreamReader sR = new StreamReader(request.InputStream, request.ContentEncoding))
  156. {
  157. formRequest = sR.ReadToEnd();
  158. }
  159. string[] formParts = formRequest.Split('&');
  160. foreach (string formPart in formParts)
  161. {
  162. if (formPart.StartsWith("allowedZones="))
  163. {
  164. string[] allowedZones = formPart.Substring(13).Split(',');
  165. bool added = false;
  166. foreach (string allowedZone in allowedZones)
  167. {
  168. if (_dnsWebService.DnsServer.AllowedZoneManager.AllowZone(allowedZone))
  169. added = true;
  170. }
  171. if (added)
  172. {
  173. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Total " + allowedZones.Length + " zones were imported into allowed zone successfully.");
  174. _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
  175. }
  176. return;
  177. }
  178. }
  179. throw new DnsWebServiceException("Parameter 'allowedZones' missing.");
  180. }
  181. public void ExportAllowedZones(HttpListenerResponse response)
  182. {
  183. IReadOnlyList<AuthZoneInfo> zoneInfoList = _dnsWebService.DnsServer.AllowedZoneManager.ListZones();
  184. response.ContentType = "text/plain";
  185. response.AddHeader("Content-Disposition", "attachment;filename=AllowedZones.txt");
  186. using (StreamWriter sW = new StreamWriter(new BufferedStream(response.OutputStream)))
  187. {
  188. foreach (AuthZoneInfo zoneInfo in zoneInfoList)
  189. sW.WriteLine(zoneInfo.Name);
  190. }
  191. }
  192. public void DeleteAllowedZone(HttpListenerRequest request)
  193. {
  194. string domain = request.QueryString["domain"];
  195. if (string.IsNullOrEmpty(domain))
  196. throw new DnsWebServiceException("Parameter 'domain' missing.");
  197. if (_dnsWebService.DnsServer.AllowedZoneManager.DeleteZone(domain))
  198. {
  199. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Allowed zone was deleted: " + domain);
  200. _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
  201. }
  202. }
  203. public void FlushAllowedZone(HttpListenerRequest request)
  204. {
  205. _dnsWebService.DnsServer.AllowedZoneManager.Flush();
  206. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Allowed zone was flushed successfully.");
  207. _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
  208. }
  209. public void AllowZone(HttpListenerRequest request)
  210. {
  211. string domain = request.QueryString["domain"];
  212. if (string.IsNullOrEmpty(domain))
  213. throw new DnsWebServiceException("Parameter 'domain' missing.");
  214. if (IPAddress.TryParse(domain, out IPAddress ipAddress))
  215. domain = (new DnsQuestionRecord(ipAddress, DnsClass.IN)).Name;
  216. if (_dnsWebService.DnsServer.AllowedZoneManager.AllowZone(domain))
  217. {
  218. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Zone was allowed: " + domain);
  219. _dnsWebService.DnsServer.AllowedZoneManager.SaveZoneFile();
  220. }
  221. }
  222. #endregion
  223. #region blocked zones api
  224. public void ListBlockedZones(HttpListenerRequest request, JsonTextWriter jsonWriter)
  225. {
  226. string domain = request.QueryString["domain"];
  227. if (domain == null)
  228. domain = "";
  229. string direction = request.QueryString["direction"];
  230. List<string> subZones = new List<string>();
  231. List<DnsResourceRecord> records = new List<DnsResourceRecord>();
  232. while (true)
  233. {
  234. subZones.Clear();
  235. records.Clear();
  236. _dnsWebService.DnsServer.BlockedZoneManager.ListSubDomains(domain, subZones);
  237. _dnsWebService.DnsServer.BlockedZoneManager.ListAllRecords(domain, records);
  238. if (records.Count > 0)
  239. break;
  240. if (subZones.Count != 1)
  241. break;
  242. if (direction == "up")
  243. {
  244. if (domain.Length == 0)
  245. break;
  246. int i = domain.IndexOf('.');
  247. if (i < 0)
  248. domain = "";
  249. else
  250. domain = domain.Substring(i + 1);
  251. }
  252. else if (domain.Length == 0)
  253. {
  254. domain = subZones[0];
  255. }
  256. else
  257. {
  258. domain = subZones[0] + "." + domain;
  259. }
  260. }
  261. subZones.Sort();
  262. jsonWriter.WritePropertyName("domain");
  263. jsonWriter.WriteValue(domain);
  264. jsonWriter.WritePropertyName("zones");
  265. jsonWriter.WriteStartArray();
  266. if (domain.Length != 0)
  267. domain = "." + domain;
  268. foreach (string subZone in subZones)
  269. jsonWriter.WriteValue(subZone + domain);
  270. jsonWriter.WriteEndArray();
  271. WebServiceZonesApi.WriteRecordsAsJson(new List<DnsResourceRecord>(records), jsonWriter, false);
  272. }
  273. public void ImportBlockedZones(HttpListenerRequest request)
  274. {
  275. if (!request.ContentType.StartsWith("application/x-www-form-urlencoded"))
  276. throw new DnsWebServiceException("Invalid content type. Expected application/x-www-form-urlencoded.");
  277. string formRequest;
  278. using (StreamReader sR = new StreamReader(request.InputStream, request.ContentEncoding))
  279. {
  280. formRequest = sR.ReadToEnd();
  281. }
  282. string[] formParts = formRequest.Split('&');
  283. foreach (string formPart in formParts)
  284. {
  285. if (formPart.StartsWith("blockedZones="))
  286. {
  287. string[] blockedZones = formPart.Substring(13).Split(',');
  288. bool added = false;
  289. foreach (string blockedZone in blockedZones)
  290. {
  291. if (_dnsWebService.DnsServer.BlockedZoneManager.BlockZone(blockedZone))
  292. added = true;
  293. }
  294. if (added)
  295. {
  296. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Total " + blockedZones.Length + " zones were imported into blocked zone successfully.");
  297. _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
  298. }
  299. return;
  300. }
  301. }
  302. throw new DnsWebServiceException("Parameter 'blockedZones' missing.");
  303. }
  304. public void ExportBlockedZones(HttpListenerResponse response)
  305. {
  306. IReadOnlyList<AuthZoneInfo> zoneInfoList = _dnsWebService.DnsServer.BlockedZoneManager.ListZones();
  307. response.ContentType = "text/plain";
  308. response.AddHeader("Content-Disposition", "attachment;filename=BlockedZones.txt");
  309. using (StreamWriter sW = new StreamWriter(new BufferedStream(response.OutputStream)))
  310. {
  311. foreach (AuthZoneInfo zoneInfo in zoneInfoList)
  312. sW.WriteLine(zoneInfo.Name);
  313. }
  314. }
  315. public void DeleteBlockedZone(HttpListenerRequest request)
  316. {
  317. string domain = request.QueryString["domain"];
  318. if (string.IsNullOrEmpty(domain))
  319. throw new DnsWebServiceException("Parameter 'domain' missing.");
  320. if (_dnsWebService.DnsServer.BlockedZoneManager.DeleteZone(domain))
  321. {
  322. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Blocked zone was deleted: " + domain);
  323. _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
  324. }
  325. }
  326. public void FlushBlockedZone(HttpListenerRequest request)
  327. {
  328. _dnsWebService.DnsServer.BlockedZoneManager.Flush();
  329. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Blocked zone was flushed successfully.");
  330. _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
  331. }
  332. public void BlockZone(HttpListenerRequest request)
  333. {
  334. string domain = request.QueryString["domain"];
  335. if (string.IsNullOrEmpty(domain))
  336. throw new DnsWebServiceException("Parameter 'domain' missing.");
  337. if (IPAddress.TryParse(domain, out IPAddress ipAddress))
  338. domain = (new DnsQuestionRecord(ipAddress, DnsClass.IN)).Name;
  339. if (_dnsWebService.DnsServer.BlockedZoneManager.BlockZone(domain))
  340. {
  341. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] Domain was added to blocked zone: " + domain);
  342. _dnsWebService.DnsServer.BlockedZoneManager.SaveZoneFile();
  343. }
  344. }
  345. #endregion
  346. #endregion
  347. }
  348. }