WebServiceAppsApi.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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 DnsServerCore.ApplicationCommon;
  16. using DnsServerCore.Auth;
  17. using DnsServerCore.Dns.Applications;
  18. using Microsoft.AspNetCore.Http;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.IO;
  22. using System.Net;
  23. using System.Net.Http;
  24. using System.Text.Json;
  25. using System.Threading;
  26. using System.Threading.Tasks;
  27. using TechnitiumLibrary;
  28. using TechnitiumLibrary.Net.Http.Client;
  29. namespace DnsServerCore
  30. {
  31. sealed class WebServiceAppsApi : IDisposable
  32. {
  33. #region variables
  34. readonly DnsWebService _dnsWebService;
  35. readonly Uri _appStoreUri;
  36. string _storeAppsJsonData;
  37. DateTime _storeAppsJsonDataUpdatedOn;
  38. const int STORE_APPS_JSON_DATA_CACHE_TIME_SECONDS = 900;
  39. Timer _appUpdateTimer;
  40. const int APP_UPDATE_TIMER_INITIAL_INTERVAL = 10000;
  41. const int APP_UPDATE_TIMER_PERIODIC_INTERVAL = 86400000;
  42. #endregion
  43. #region constructor
  44. public WebServiceAppsApi(DnsWebService dnsWebService, Uri appStoreUri)
  45. {
  46. _dnsWebService = dnsWebService;
  47. _appStoreUri = appStoreUri;
  48. }
  49. #endregion
  50. #region IDisposable
  51. bool _disposed;
  52. public void Dispose()
  53. {
  54. if (_disposed)
  55. return;
  56. if (_appUpdateTimer is not null)
  57. _appUpdateTimer.Dispose();
  58. _disposed = true;
  59. }
  60. #endregion
  61. #region private
  62. private void StartAutomaticUpdate()
  63. {
  64. if (_appUpdateTimer is null)
  65. {
  66. _appUpdateTimer = new Timer(async delegate (object state)
  67. {
  68. try
  69. {
  70. if (_dnsWebService.DnsServer.DnsApplicationManager.Applications.Count < 1)
  71. return;
  72. _dnsWebService._log.Write("DNS Server has started automatic update check for DNS Apps.");
  73. string storeAppsJsonData = await GetStoreAppsJsonData(true);
  74. using JsonDocument jsonDocument = JsonDocument.Parse(storeAppsJsonData);
  75. JsonElement jsonStoreAppsArray = jsonDocument.RootElement;
  76. foreach (DnsApplication application in _dnsWebService.DnsServer.DnsApplicationManager.Applications.Values)
  77. {
  78. foreach (JsonElement jsonStoreApp in jsonStoreAppsArray.EnumerateArray())
  79. {
  80. string name = jsonStoreApp.GetProperty("name").GetString();
  81. if (name.Equals(application.Name))
  82. {
  83. string url = null;
  84. Version storeAppVersion = null;
  85. Version lastServerVersion = null;
  86. foreach (JsonElement jsonVersion in jsonStoreApp.GetProperty("versions").EnumerateArray())
  87. {
  88. string strServerVersion = jsonVersion.GetProperty("serverVersion").GetString();
  89. Version requiredServerVersion = new Version(strServerVersion);
  90. if (_dnsWebService._currentVersion < requiredServerVersion)
  91. continue;
  92. if ((lastServerVersion is not null) && (lastServerVersion > requiredServerVersion))
  93. continue;
  94. string version = jsonVersion.GetProperty("version").GetString();
  95. url = jsonVersion.GetProperty("url").GetString();
  96. storeAppVersion = new Version(version);
  97. lastServerVersion = requiredServerVersion;
  98. }
  99. if ((storeAppVersion is not null) && (storeAppVersion > application.Version))
  100. {
  101. try
  102. {
  103. await DownloadAndUpdateAppAsync(application.Name, url, true);
  104. _dnsWebService._log.Write("DNS application '" + application.Name + "' was automatically updated successfully from: " + url);
  105. }
  106. catch (Exception ex)
  107. {
  108. _dnsWebService._log.Write("Failed to automatically download and update DNS application '" + application.Name + "': " + ex.ToString());
  109. }
  110. }
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. _dnsWebService._log.Write(ex);
  119. }
  120. });
  121. _appUpdateTimer.Change(APP_UPDATE_TIMER_INITIAL_INTERVAL, APP_UPDATE_TIMER_PERIODIC_INTERVAL);
  122. }
  123. }
  124. private void StopAutomaticUpdate()
  125. {
  126. if (_appUpdateTimer is not null)
  127. {
  128. _appUpdateTimer.Dispose();
  129. _appUpdateTimer = null;
  130. }
  131. }
  132. private async Task<string> GetStoreAppsJsonData(bool doRetry)
  133. {
  134. if ((_storeAppsJsonData is null) || (DateTime.UtcNow > _storeAppsJsonDataUpdatedOn.AddSeconds(STORE_APPS_JSON_DATA_CACHE_TIME_SECONDS)))
  135. {
  136. SocketsHttpHandler handler = new SocketsHttpHandler();
  137. handler.Proxy = _dnsWebService.DnsServer.Proxy;
  138. handler.UseProxy = _dnsWebService.DnsServer.Proxy is not null;
  139. handler.AutomaticDecompression = DecompressionMethods.All;
  140. HttpClientNetworkHandler networkHandler = new HttpClientNetworkHandler(handler, _dnsWebService.DnsServer.PreferIPv6 ? HttpClientNetworkType.PreferIPv6 : HttpClientNetworkType.Default, _dnsWebService.DnsServer);
  141. if (!doRetry)
  142. networkHandler.Retries = 1;
  143. using (HttpClient http = new HttpClient(networkHandler))
  144. {
  145. _storeAppsJsonData = await http.GetStringAsync(_appStoreUri);
  146. _storeAppsJsonDataUpdatedOn = DateTime.UtcNow;
  147. }
  148. }
  149. return _storeAppsJsonData;
  150. }
  151. private async Task<DnsApplication> DownloadAndUpdateAppAsync(string applicationName, string url, bool doRetry)
  152. {
  153. string tmpFile = Path.GetTempFileName();
  154. try
  155. {
  156. using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
  157. {
  158. //download to temp file
  159. SocketsHttpHandler handler = new SocketsHttpHandler();
  160. handler.Proxy = _dnsWebService.DnsServer.Proxy;
  161. handler.UseProxy = _dnsWebService.DnsServer.Proxy is not null;
  162. handler.AutomaticDecompression = DecompressionMethods.All;
  163. HttpClientNetworkHandler networkHandler = new HttpClientNetworkHandler(handler, _dnsWebService.DnsServer.PreferIPv6 ? HttpClientNetworkType.PreferIPv6 : HttpClientNetworkType.Default, _dnsWebService.DnsServer);
  164. if (!doRetry)
  165. networkHandler.Retries = 1;
  166. using (HttpClient http = new HttpClient(networkHandler))
  167. {
  168. using (Stream httpStream = await http.GetStreamAsync(url))
  169. {
  170. await httpStream.CopyToAsync(fS);
  171. }
  172. }
  173. //update app
  174. fS.Position = 0;
  175. return await _dnsWebService.DnsServer.DnsApplicationManager.UpdateApplicationAsync(applicationName, fS);
  176. }
  177. }
  178. finally
  179. {
  180. try
  181. {
  182. File.Delete(tmpFile);
  183. }
  184. catch (Exception ex)
  185. {
  186. _dnsWebService._log.Write(ex);
  187. }
  188. }
  189. }
  190. private void WriteAppAsJson(Utf8JsonWriter jsonWriter, DnsApplication application, JsonElement jsonStoreAppsArray = default)
  191. {
  192. jsonWriter.WriteStartObject();
  193. jsonWriter.WriteString("name", application.Name);
  194. jsonWriter.WriteString("description", application.Description);
  195. jsonWriter.WriteString("version", DnsWebService.GetCleanVersion(application.Version));
  196. if (jsonStoreAppsArray.ValueKind != JsonValueKind.Undefined)
  197. {
  198. foreach (JsonElement jsonStoreApp in jsonStoreAppsArray.EnumerateArray())
  199. {
  200. string name = jsonStoreApp.GetProperty("name").GetString();
  201. if (name.Equals(application.Name))
  202. {
  203. string version = null;
  204. string url = null;
  205. Version storeAppVersion = null;
  206. Version lastServerVersion = null;
  207. foreach (JsonElement jsonVersion in jsonStoreApp.GetProperty("versions").EnumerateArray())
  208. {
  209. string strServerVersion = jsonVersion.GetProperty("serverVersion").GetString();
  210. Version requiredServerVersion = new Version(strServerVersion);
  211. if (_dnsWebService._currentVersion < requiredServerVersion)
  212. continue;
  213. if ((lastServerVersion is not null) && (lastServerVersion > requiredServerVersion))
  214. continue;
  215. version = jsonVersion.GetProperty("version").GetString();
  216. url = jsonVersion.GetProperty("url").GetString();
  217. storeAppVersion = new Version(version);
  218. lastServerVersion = requiredServerVersion;
  219. }
  220. if (storeAppVersion is null)
  221. break; //no compatible update available
  222. jsonWriter.WriteString("updateVersion", version);
  223. jsonWriter.WriteString("updateUrl", url);
  224. jsonWriter.WriteBoolean("updateAvailable", storeAppVersion > application.Version);
  225. break;
  226. }
  227. }
  228. }
  229. jsonWriter.WritePropertyName("dnsApps");
  230. {
  231. jsonWriter.WriteStartArray();
  232. foreach (KeyValuePair<string, IDnsApplication> dnsApp in application.DnsApplications)
  233. {
  234. jsonWriter.WriteStartObject();
  235. jsonWriter.WriteString("classPath", dnsApp.Key);
  236. jsonWriter.WriteString("description", dnsApp.Value.Description);
  237. if (dnsApp.Value is IDnsAppRecordRequestHandler appRecordHandler)
  238. {
  239. jsonWriter.WriteBoolean("isAppRecordRequestHandler", true);
  240. jsonWriter.WriteString("recordDataTemplate", appRecordHandler.ApplicationRecordDataTemplate);
  241. }
  242. else
  243. {
  244. jsonWriter.WriteBoolean("isAppRecordRequestHandler", false);
  245. }
  246. jsonWriter.WriteBoolean("isRequestController", dnsApp.Value is IDnsRequestController);
  247. jsonWriter.WriteBoolean("isAuthoritativeRequestHandler", dnsApp.Value is IDnsAuthoritativeRequestHandler);
  248. jsonWriter.WriteBoolean("isQueryLogger", dnsApp.Value is IDnsQueryLogger);
  249. jsonWriter.WriteBoolean("isPostProcessor", dnsApp.Value is IDnsPostProcessor);
  250. jsonWriter.WriteEndObject();
  251. }
  252. jsonWriter.WriteEndArray();
  253. }
  254. jsonWriter.WriteEndObject();
  255. }
  256. #endregion
  257. #region public
  258. public async Task ListInstalledAppsAsync(HttpContext context)
  259. {
  260. UserSession session = context.GetCurrentSession();
  261. if (
  262. !_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.View) &&
  263. !_dnsWebService._authManager.IsPermitted(PermissionSection.Zones, session.User, PermissionFlag.View) &&
  264. !_dnsWebService._authManager.IsPermitted(PermissionSection.Logs, session.User, PermissionFlag.View)
  265. )
  266. {
  267. throw new DnsWebServiceException("Access was denied.");
  268. }
  269. List<string> apps = new List<string>(_dnsWebService.DnsServer.DnsApplicationManager.Applications.Keys);
  270. apps.Sort();
  271. JsonDocument jsonDocument = null;
  272. try
  273. {
  274. JsonElement jsonStoreAppsArray = default;
  275. if (apps.Count > 0)
  276. {
  277. try
  278. {
  279. string storeAppsJsonData = await GetStoreAppsJsonData(false).WithTimeout(5000);
  280. jsonDocument = JsonDocument.Parse(storeAppsJsonData);
  281. jsonStoreAppsArray = jsonDocument.RootElement;
  282. }
  283. catch
  284. { }
  285. }
  286. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  287. jsonWriter.WritePropertyName("apps");
  288. jsonWriter.WriteStartArray();
  289. foreach (string app in apps)
  290. {
  291. if (_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(app, out DnsApplication application))
  292. WriteAppAsJson(jsonWriter, application, jsonStoreAppsArray);
  293. }
  294. jsonWriter.WriteEndArray();
  295. }
  296. finally
  297. {
  298. if (jsonDocument is not null)
  299. jsonDocument.Dispose();
  300. }
  301. }
  302. public async Task ListStoreApps(HttpContext context)
  303. {
  304. UserSession session = context.GetCurrentSession();
  305. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.View))
  306. throw new DnsWebServiceException("Access was denied.");
  307. string storeAppsJsonData = await GetStoreAppsJsonData(false).WithTimeout(30000);
  308. using JsonDocument jsonDocument = JsonDocument.Parse(storeAppsJsonData);
  309. JsonElement jsonStoreAppsArray = jsonDocument.RootElement;
  310. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  311. jsonWriter.WritePropertyName("storeApps");
  312. jsonWriter.WriteStartArray();
  313. foreach (JsonElement jsonStoreApp in jsonStoreAppsArray.EnumerateArray())
  314. {
  315. string name = jsonStoreApp.GetProperty("name").GetString();
  316. string description = jsonStoreApp.GetProperty("description").GetString();
  317. string version = null;
  318. string url = null;
  319. string size = null;
  320. Version storeAppVersion = null;
  321. Version lastServerVersion = null;
  322. foreach (JsonElement jsonVersion in jsonStoreApp.GetProperty("versions").EnumerateArray())
  323. {
  324. string strServerVersion = jsonVersion.GetProperty("serverVersion").GetString();
  325. Version requiredServerVersion = new Version(strServerVersion);
  326. if (_dnsWebService._currentVersion < requiredServerVersion)
  327. continue;
  328. if ((lastServerVersion is not null) && (lastServerVersion > requiredServerVersion))
  329. continue;
  330. version = jsonVersion.GetProperty("version").GetString();
  331. url = jsonVersion.GetProperty("url").GetString();
  332. size = jsonVersion.GetProperty("size").GetString();
  333. storeAppVersion = new Version(version);
  334. lastServerVersion = requiredServerVersion;
  335. }
  336. if (storeAppVersion is null)
  337. continue; //app is not compatible
  338. jsonWriter.WriteStartObject();
  339. jsonWriter.WriteString("name", name);
  340. jsonWriter.WriteString("description", description);
  341. jsonWriter.WriteString("version", version);
  342. jsonWriter.WriteString("url", url);
  343. jsonWriter.WriteString("size", size);
  344. bool installed = _dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication installedApp);
  345. jsonWriter.WriteBoolean("installed", installed);
  346. if (installed)
  347. {
  348. jsonWriter.WriteString("installedVersion", DnsWebService.GetCleanVersion(installedApp.Version));
  349. jsonWriter.WriteBoolean("updateAvailable", storeAppVersion > installedApp.Version);
  350. }
  351. jsonWriter.WriteEndObject();
  352. }
  353. jsonWriter.WriteEndArray();
  354. }
  355. public async Task DownloadAndInstallAppAsync(HttpContext context)
  356. {
  357. UserSession session = context.GetCurrentSession();
  358. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  359. throw new DnsWebServiceException("Access was denied.");
  360. HttpRequest request = context.Request;
  361. string name = request.GetQueryOrForm("name").Trim();
  362. string url = request.GetQueryOrForm("url");
  363. if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
  364. throw new DnsWebServiceException("Parameter 'url' value must start with 'https://'.");
  365. string tmpFile = Path.GetTempFileName();
  366. try
  367. {
  368. using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
  369. {
  370. //download to temp file
  371. SocketsHttpHandler handler = new SocketsHttpHandler();
  372. handler.Proxy = _dnsWebService.DnsServer.Proxy;
  373. handler.UseProxy = _dnsWebService.DnsServer.Proxy is not null;
  374. handler.AutomaticDecompression = DecompressionMethods.All;
  375. using (HttpClient http = new HttpClient(new HttpClientNetworkHandler(handler, _dnsWebService.DnsServer.PreferIPv6 ? HttpClientNetworkType.PreferIPv6 : HttpClientNetworkType.Default, _dnsWebService.DnsServer)))
  376. {
  377. using (Stream httpStream = await http.GetStreamAsync(url))
  378. {
  379. await httpStream.CopyToAsync(fS);
  380. }
  381. }
  382. //install app
  383. fS.Position = 0;
  384. DnsApplication application = await _dnsWebService.DnsServer.DnsApplicationManager.InstallApplicationAsync(name, fS);
  385. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was installed successfully from: " + url);
  386. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  387. jsonWriter.WritePropertyName("installedApp");
  388. WriteAppAsJson(jsonWriter, application);
  389. }
  390. }
  391. finally
  392. {
  393. try
  394. {
  395. File.Delete(tmpFile);
  396. }
  397. catch (Exception ex)
  398. {
  399. _dnsWebService._log.Write(ex);
  400. }
  401. }
  402. }
  403. public async Task DownloadAndUpdateAppAsync(HttpContext context)
  404. {
  405. UserSession session = context.GetCurrentSession();
  406. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  407. throw new DnsWebServiceException("Access was denied.");
  408. HttpRequest request = context.Request;
  409. string name = request.GetQueryOrForm("name").Trim();
  410. string url = request.GetQueryOrForm("url");
  411. if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
  412. throw new DnsWebServiceException("Parameter 'url' value must start with 'https://'.");
  413. DnsApplication application = await DownloadAndUpdateAppAsync(name, url, false);
  414. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was updated successfully from: " + url);
  415. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  416. jsonWriter.WritePropertyName("updatedApp");
  417. WriteAppAsJson(jsonWriter, application);
  418. }
  419. public async Task InstallAppAsync(HttpContext context)
  420. {
  421. UserSession session = context.GetCurrentSession();
  422. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  423. throw new DnsWebServiceException("Access was denied.");
  424. HttpRequest request = context.Request;
  425. string name = request.GetQueryOrForm("name").Trim();
  426. if (!request.HasFormContentType || (request.Form.Files.Count == 0))
  427. throw new DnsWebServiceException("DNS application zip file is missing.");
  428. string tmpFile = Path.GetTempFileName();
  429. try
  430. {
  431. using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
  432. {
  433. //write to temp file
  434. await request.Form.Files[0].CopyToAsync(fS);
  435. //install app
  436. fS.Position = 0;
  437. DnsApplication application = await _dnsWebService.DnsServer.DnsApplicationManager.InstallApplicationAsync(name, fS);
  438. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was installed successfully.");
  439. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  440. jsonWriter.WritePropertyName("installedApp");
  441. WriteAppAsJson(jsonWriter, application);
  442. }
  443. }
  444. finally
  445. {
  446. try
  447. {
  448. File.Delete(tmpFile);
  449. }
  450. catch (Exception ex)
  451. {
  452. _dnsWebService._log.Write(ex);
  453. }
  454. }
  455. }
  456. public async Task UpdateAppAsync(HttpContext context)
  457. {
  458. UserSession session = context.GetCurrentSession();
  459. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  460. throw new DnsWebServiceException("Access was denied.");
  461. HttpRequest request = context.Request;
  462. string name = request.GetQueryOrForm("name").Trim();
  463. if (!request.HasFormContentType || (request.Form.Files.Count == 0))
  464. throw new DnsWebServiceException("DNS application zip file is missing.");
  465. string tmpFile = Path.GetTempFileName();
  466. try
  467. {
  468. using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
  469. {
  470. //write to temp file
  471. await request.Form.Files[0].CopyToAsync(fS);
  472. //update app
  473. fS.Position = 0;
  474. DnsApplication application = await _dnsWebService.DnsServer.DnsApplicationManager.UpdateApplicationAsync(name, fS);
  475. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was updated successfully.");
  476. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  477. jsonWriter.WritePropertyName("updatedApp");
  478. WriteAppAsJson(jsonWriter, application);
  479. }
  480. }
  481. finally
  482. {
  483. try
  484. {
  485. File.Delete(tmpFile);
  486. }
  487. catch (Exception ex)
  488. {
  489. _dnsWebService._log.Write(ex);
  490. }
  491. }
  492. }
  493. public void UninstallApp(HttpContext context)
  494. {
  495. UserSession session = context.GetCurrentSession();
  496. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  497. throw new DnsWebServiceException("Access was denied.");
  498. HttpRequest request = context.Request;
  499. string name = request.GetQueryOrForm("name").Trim();
  500. _dnsWebService.DnsServer.DnsApplicationManager.UninstallApplication(name);
  501. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was uninstalled successfully.");
  502. }
  503. public async Task GetAppConfigAsync(HttpContext context)
  504. {
  505. UserSession session = context.GetCurrentSession();
  506. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.View))
  507. throw new DnsWebServiceException("Access was denied.");
  508. HttpRequest request = context.Request;
  509. string name = request.GetQueryOrForm("name").Trim();
  510. if (!_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication application))
  511. throw new DnsWebServiceException("DNS application was not found: " + name);
  512. string config = await application.GetConfigAsync();
  513. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  514. jsonWriter.WriteString("config", config);
  515. }
  516. public async Task SetAppConfigAsync(HttpContext context)
  517. {
  518. UserSession session = context.GetCurrentSession();
  519. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Modify))
  520. throw new DnsWebServiceException("Access was denied.");
  521. HttpRequest request = context.Request;
  522. string name = request.GetQueryOrForm("name").Trim();
  523. if (!_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication application))
  524. throw new DnsWebServiceException("DNS application was not found: " + name);
  525. string config = request.QueryOrForm("config");
  526. if (config is null)
  527. throw new DnsWebServiceException("Parameter 'config' missing.");
  528. if (config.Length == 0)
  529. config = null;
  530. await application.SetConfigAsync(config);
  531. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' app config was saved successfully.");
  532. }
  533. #endregion
  534. #region properties
  535. public bool EnableAutomaticUpdate
  536. {
  537. get { return _appUpdateTimer is not null; }
  538. set
  539. {
  540. if (value)
  541. StartAutomaticUpdate();
  542. else
  543. StopAutomaticUpdate();
  544. }
  545. }
  546. #endregion
  547. }
  548. }