WebServiceAppsApi.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. using (HttpClient http = new HttpClient(doRetry ? new HttpClientRetryHandler(handler) : handler))
  141. {
  142. _storeAppsJsonData = await http.GetStringAsync(_appStoreUri);
  143. _storeAppsJsonDataUpdatedOn = DateTime.UtcNow;
  144. }
  145. }
  146. return _storeAppsJsonData;
  147. }
  148. private async Task<DnsApplication> DownloadAndUpdateAppAsync(string applicationName, string url, bool doRetry)
  149. {
  150. string tmpFile = Path.GetTempFileName();
  151. try
  152. {
  153. using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
  154. {
  155. //download to temp file
  156. SocketsHttpHandler handler = new SocketsHttpHandler();
  157. handler.Proxy = _dnsWebService.DnsServer.Proxy;
  158. handler.UseProxy = _dnsWebService.DnsServer.Proxy is not null;
  159. handler.AutomaticDecompression = DecompressionMethods.All;
  160. using (HttpClient http = new HttpClient(doRetry ? new HttpClientRetryHandler(handler) : handler))
  161. {
  162. using (Stream httpStream = await http.GetStreamAsync(url))
  163. {
  164. await httpStream.CopyToAsync(fS);
  165. }
  166. }
  167. //update app
  168. fS.Position = 0;
  169. return await _dnsWebService.DnsServer.DnsApplicationManager.UpdateApplicationAsync(applicationName, fS);
  170. }
  171. }
  172. finally
  173. {
  174. try
  175. {
  176. File.Delete(tmpFile);
  177. }
  178. catch (Exception ex)
  179. {
  180. _dnsWebService._log.Write(ex);
  181. }
  182. }
  183. }
  184. private void WriteAppAsJson(Utf8JsonWriter jsonWriter, DnsApplication application, JsonElement jsonStoreAppsArray = default)
  185. {
  186. jsonWriter.WriteStartObject();
  187. jsonWriter.WriteString("name", application.Name);
  188. jsonWriter.WriteString("description", application.Description);
  189. jsonWriter.WriteString("version", DnsWebService.GetCleanVersion(application.Version));
  190. if (jsonStoreAppsArray.ValueKind != JsonValueKind.Undefined)
  191. {
  192. foreach (JsonElement jsonStoreApp in jsonStoreAppsArray.EnumerateArray())
  193. {
  194. string name = jsonStoreApp.GetProperty("name").GetString();
  195. if (name.Equals(application.Name))
  196. {
  197. string version = null;
  198. string url = null;
  199. Version storeAppVersion = null;
  200. Version lastServerVersion = null;
  201. foreach (JsonElement jsonVersion in jsonStoreApp.GetProperty("versions").EnumerateArray())
  202. {
  203. string strServerVersion = jsonVersion.GetProperty("serverVersion").GetString();
  204. Version requiredServerVersion = new Version(strServerVersion);
  205. if (_dnsWebService._currentVersion < requiredServerVersion)
  206. continue;
  207. if ((lastServerVersion is not null) && (lastServerVersion > requiredServerVersion))
  208. continue;
  209. version = jsonVersion.GetProperty("version").GetString();
  210. url = jsonVersion.GetProperty("url").GetString();
  211. storeAppVersion = new Version(version);
  212. lastServerVersion = requiredServerVersion;
  213. }
  214. if (storeAppVersion is null)
  215. break; //no compatible update available
  216. jsonWriter.WriteString("updateVersion", version);
  217. jsonWriter.WriteString("updateUrl", url);
  218. jsonWriter.WriteBoolean("updateAvailable", storeAppVersion > application.Version);
  219. break;
  220. }
  221. }
  222. }
  223. jsonWriter.WritePropertyName("dnsApps");
  224. {
  225. jsonWriter.WriteStartArray();
  226. foreach (KeyValuePair<string, IDnsApplication> dnsApp in application.DnsApplications)
  227. {
  228. jsonWriter.WriteStartObject();
  229. jsonWriter.WriteString("classPath", dnsApp.Key);
  230. jsonWriter.WriteString("description", dnsApp.Value.Description);
  231. if (dnsApp.Value is IDnsAppRecordRequestHandler appRecordHandler)
  232. {
  233. jsonWriter.WriteBoolean("isAppRecordRequestHandler", true);
  234. jsonWriter.WriteString("recordDataTemplate", appRecordHandler.ApplicationRecordDataTemplate);
  235. }
  236. else
  237. {
  238. jsonWriter.WriteBoolean("isAppRecordRequestHandler", false);
  239. }
  240. jsonWriter.WriteBoolean("isRequestController", dnsApp.Value is IDnsRequestController);
  241. jsonWriter.WriteBoolean("isAuthoritativeRequestHandler", dnsApp.Value is IDnsAuthoritativeRequestHandler);
  242. jsonWriter.WriteBoolean("isQueryLogger", dnsApp.Value is IDnsQueryLogger);
  243. jsonWriter.WriteBoolean("isPostProcessor", dnsApp.Value is IDnsPostProcessor);
  244. jsonWriter.WriteEndObject();
  245. }
  246. jsonWriter.WriteEndArray();
  247. }
  248. jsonWriter.WriteEndObject();
  249. }
  250. #endregion
  251. #region public
  252. public async Task ListInstalledAppsAsync(HttpContext context)
  253. {
  254. UserSession session = context.GetCurrentSession();
  255. if (
  256. !_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.View) &&
  257. !_dnsWebService._authManager.IsPermitted(PermissionSection.Zones, session.User, PermissionFlag.View) &&
  258. !_dnsWebService._authManager.IsPermitted(PermissionSection.Logs, session.User, PermissionFlag.View)
  259. )
  260. {
  261. throw new DnsWebServiceException("Access was denied.");
  262. }
  263. List<string> apps = new List<string>(_dnsWebService.DnsServer.DnsApplicationManager.Applications.Keys);
  264. apps.Sort();
  265. JsonDocument jsonDocument = null;
  266. try
  267. {
  268. JsonElement jsonStoreAppsArray = default;
  269. if (apps.Count > 0)
  270. {
  271. try
  272. {
  273. string storeAppsJsonData = await GetStoreAppsJsonData(false).WithTimeout(5000);
  274. jsonDocument = JsonDocument.Parse(storeAppsJsonData);
  275. jsonStoreAppsArray = jsonDocument.RootElement;
  276. }
  277. catch
  278. { }
  279. }
  280. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  281. jsonWriter.WritePropertyName("apps");
  282. jsonWriter.WriteStartArray();
  283. foreach (string app in apps)
  284. {
  285. if (_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(app, out DnsApplication application))
  286. WriteAppAsJson(jsonWriter, application, jsonStoreAppsArray);
  287. }
  288. jsonWriter.WriteEndArray();
  289. }
  290. finally
  291. {
  292. if (jsonDocument is not null)
  293. jsonDocument.Dispose();
  294. }
  295. }
  296. public async Task ListStoreApps(HttpContext context)
  297. {
  298. UserSession session = context.GetCurrentSession();
  299. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.View))
  300. throw new DnsWebServiceException("Access was denied.");
  301. string storeAppsJsonData = await GetStoreAppsJsonData(false).WithTimeout(30000);
  302. using JsonDocument jsonDocument = JsonDocument.Parse(storeAppsJsonData);
  303. JsonElement jsonStoreAppsArray = jsonDocument.RootElement;
  304. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  305. jsonWriter.WritePropertyName("storeApps");
  306. jsonWriter.WriteStartArray();
  307. foreach (JsonElement jsonStoreApp in jsonStoreAppsArray.EnumerateArray())
  308. {
  309. string name = jsonStoreApp.GetProperty("name").GetString();
  310. string description = jsonStoreApp.GetProperty("description").GetString();
  311. string version = null;
  312. string url = null;
  313. string size = null;
  314. Version storeAppVersion = null;
  315. Version lastServerVersion = null;
  316. foreach (JsonElement jsonVersion in jsonStoreApp.GetProperty("versions").EnumerateArray())
  317. {
  318. string strServerVersion = jsonVersion.GetProperty("serverVersion").GetString();
  319. Version requiredServerVersion = new Version(strServerVersion);
  320. if (_dnsWebService._currentVersion < requiredServerVersion)
  321. continue;
  322. if ((lastServerVersion is not null) && (lastServerVersion > requiredServerVersion))
  323. continue;
  324. version = jsonVersion.GetProperty("version").GetString();
  325. url = jsonVersion.GetProperty("url").GetString();
  326. size = jsonVersion.GetProperty("size").GetString();
  327. storeAppVersion = new Version(version);
  328. lastServerVersion = requiredServerVersion;
  329. }
  330. if (storeAppVersion is null)
  331. continue; //app is not compatible
  332. jsonWriter.WriteStartObject();
  333. jsonWriter.WriteString("name", name);
  334. jsonWriter.WriteString("description", description);
  335. jsonWriter.WriteString("version", version);
  336. jsonWriter.WriteString("url", url);
  337. jsonWriter.WriteString("size", size);
  338. bool installed = _dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication installedApp);
  339. jsonWriter.WriteBoolean("installed", installed);
  340. if (installed)
  341. {
  342. jsonWriter.WriteString("installedVersion", DnsWebService.GetCleanVersion(installedApp.Version));
  343. jsonWriter.WriteBoolean("updateAvailable", storeAppVersion > installedApp.Version);
  344. }
  345. jsonWriter.WriteEndObject();
  346. }
  347. jsonWriter.WriteEndArray();
  348. }
  349. public async Task DownloadAndInstallAppAsync(HttpContext context)
  350. {
  351. UserSession session = context.GetCurrentSession();
  352. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  353. throw new DnsWebServiceException("Access was denied.");
  354. HttpRequest request = context.Request;
  355. string name = request.GetQueryOrForm("name").Trim();
  356. string url = request.GetQueryOrForm("url");
  357. if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
  358. throw new DnsWebServiceException("Parameter 'url' value must start with 'https://'.");
  359. string tmpFile = Path.GetTempFileName();
  360. try
  361. {
  362. using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
  363. {
  364. //download to temp file
  365. SocketsHttpHandler handler = new SocketsHttpHandler();
  366. handler.Proxy = _dnsWebService.DnsServer.Proxy;
  367. handler.UseProxy = _dnsWebService.DnsServer.Proxy is not null;
  368. handler.AutomaticDecompression = DecompressionMethods.All;
  369. using (HttpClient http = new HttpClient(handler))
  370. {
  371. using (Stream httpStream = await http.GetStreamAsync(url))
  372. {
  373. await httpStream.CopyToAsync(fS);
  374. }
  375. }
  376. //install app
  377. fS.Position = 0;
  378. DnsApplication application = await _dnsWebService.DnsServer.DnsApplicationManager.InstallApplicationAsync(name, fS);
  379. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was installed successfully from: " + url);
  380. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  381. jsonWriter.WritePropertyName("installedApp");
  382. WriteAppAsJson(jsonWriter, application);
  383. }
  384. }
  385. finally
  386. {
  387. try
  388. {
  389. File.Delete(tmpFile);
  390. }
  391. catch (Exception ex)
  392. {
  393. _dnsWebService._log.Write(ex);
  394. }
  395. }
  396. }
  397. public async Task DownloadAndUpdateAppAsync(HttpContext context)
  398. {
  399. UserSession session = context.GetCurrentSession();
  400. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  401. throw new DnsWebServiceException("Access was denied.");
  402. HttpRequest request = context.Request;
  403. string name = request.GetQueryOrForm("name").Trim();
  404. string url = request.GetQueryOrForm("url");
  405. if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
  406. throw new DnsWebServiceException("Parameter 'url' value must start with 'https://'.");
  407. DnsApplication application = await DownloadAndUpdateAppAsync(name, url, false);
  408. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was updated successfully from: " + url);
  409. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  410. jsonWriter.WritePropertyName("updatedApp");
  411. WriteAppAsJson(jsonWriter, application);
  412. }
  413. public async Task InstallAppAsync(HttpContext context)
  414. {
  415. UserSession session = context.GetCurrentSession();
  416. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  417. throw new DnsWebServiceException("Access was denied.");
  418. HttpRequest request = context.Request;
  419. string name = request.GetQueryOrForm("name").Trim();
  420. if (!request.HasFormContentType || (request.Form.Files.Count == 0))
  421. throw new DnsWebServiceException("DNS application zip file is missing.");
  422. string tmpFile = Path.GetTempFileName();
  423. try
  424. {
  425. using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
  426. {
  427. //write to temp file
  428. await request.Form.Files[0].CopyToAsync(fS);
  429. //install app
  430. fS.Position = 0;
  431. DnsApplication application = await _dnsWebService.DnsServer.DnsApplicationManager.InstallApplicationAsync(name, fS);
  432. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was installed successfully.");
  433. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  434. jsonWriter.WritePropertyName("installedApp");
  435. WriteAppAsJson(jsonWriter, application);
  436. }
  437. }
  438. finally
  439. {
  440. try
  441. {
  442. File.Delete(tmpFile);
  443. }
  444. catch (Exception ex)
  445. {
  446. _dnsWebService._log.Write(ex);
  447. }
  448. }
  449. }
  450. public async Task UpdateAppAsync(HttpContext context)
  451. {
  452. UserSession session = context.GetCurrentSession();
  453. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  454. throw new DnsWebServiceException("Access was denied.");
  455. HttpRequest request = context.Request;
  456. string name = request.GetQueryOrForm("name").Trim();
  457. if (!request.HasFormContentType || (request.Form.Files.Count == 0))
  458. throw new DnsWebServiceException("DNS application zip file is missing.");
  459. string tmpFile = Path.GetTempFileName();
  460. try
  461. {
  462. using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
  463. {
  464. //write to temp file
  465. await request.Form.Files[0].CopyToAsync(fS);
  466. //update app
  467. fS.Position = 0;
  468. DnsApplication application = await _dnsWebService.DnsServer.DnsApplicationManager.UpdateApplicationAsync(name, fS);
  469. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was updated successfully.");
  470. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  471. jsonWriter.WritePropertyName("updatedApp");
  472. WriteAppAsJson(jsonWriter, application);
  473. }
  474. }
  475. finally
  476. {
  477. try
  478. {
  479. File.Delete(tmpFile);
  480. }
  481. catch (Exception ex)
  482. {
  483. _dnsWebService._log.Write(ex);
  484. }
  485. }
  486. }
  487. public void UninstallApp(HttpContext context)
  488. {
  489. UserSession session = context.GetCurrentSession();
  490. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  491. throw new DnsWebServiceException("Access was denied.");
  492. HttpRequest request = context.Request;
  493. string name = request.GetQueryOrForm("name").Trim();
  494. _dnsWebService.DnsServer.DnsApplicationManager.UninstallApplication(name);
  495. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was uninstalled successfully.");
  496. }
  497. public async Task GetAppConfigAsync(HttpContext context)
  498. {
  499. UserSession session = context.GetCurrentSession();
  500. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.View))
  501. throw new DnsWebServiceException("Access was denied.");
  502. HttpRequest request = context.Request;
  503. string name = request.GetQueryOrForm("name").Trim();
  504. if (!_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication application))
  505. throw new DnsWebServiceException("DNS application was not found: " + name);
  506. string config = await application.GetConfigAsync();
  507. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  508. jsonWriter.WriteString("config", config);
  509. }
  510. public async Task SetAppConfigAsync(HttpContext context)
  511. {
  512. UserSession session = context.GetCurrentSession();
  513. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Modify))
  514. throw new DnsWebServiceException("Access was denied.");
  515. HttpRequest request = context.Request;
  516. string name = request.GetQueryOrForm("name").Trim();
  517. if (!_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication application))
  518. throw new DnsWebServiceException("DNS application was not found: " + name);
  519. string config = request.QueryOrForm("config");
  520. if (config is null)
  521. throw new DnsWebServiceException("Parameter 'config' missing.");
  522. if (config.Length == 0)
  523. config = null;
  524. await application.SetConfigAsync(config);
  525. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' app config was saved successfully.");
  526. }
  527. #endregion
  528. #region properties
  529. public bool EnableAutomaticUpdate
  530. {
  531. get { return _appUpdateTimer is not null; }
  532. set
  533. {
  534. if (value)
  535. StartAutomaticUpdate();
  536. else
  537. StopAutomaticUpdate();
  538. }
  539. }
  540. #endregion
  541. }
  542. }