WebServiceAppsApi.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 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("isRequestBlockingHandler", dnsApp.Value is IDnsRequestBlockingHandler);
  249. jsonWriter.WriteBoolean("isQueryLogger", dnsApp.Value is IDnsQueryLogger);
  250. jsonWriter.WriteBoolean("isPostProcessor", dnsApp.Value is IDnsPostProcessor);
  251. jsonWriter.WriteEndObject();
  252. }
  253. jsonWriter.WriteEndArray();
  254. }
  255. jsonWriter.WriteEndObject();
  256. }
  257. #endregion
  258. #region public
  259. public async Task ListInstalledAppsAsync(HttpContext context)
  260. {
  261. UserSession session = context.GetCurrentSession();
  262. if (
  263. !_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.View) &&
  264. !_dnsWebService._authManager.IsPermitted(PermissionSection.Zones, session.User, PermissionFlag.View) &&
  265. !_dnsWebService._authManager.IsPermitted(PermissionSection.Logs, session.User, PermissionFlag.View)
  266. )
  267. {
  268. throw new DnsWebServiceException("Access was denied.");
  269. }
  270. List<string> apps = new List<string>(_dnsWebService.DnsServer.DnsApplicationManager.Applications.Keys);
  271. apps.Sort();
  272. JsonDocument jsonDocument = null;
  273. try
  274. {
  275. JsonElement jsonStoreAppsArray = default;
  276. if (apps.Count > 0)
  277. {
  278. try
  279. {
  280. string storeAppsJsonData = await GetStoreAppsJsonData(false).WithTimeout(5000);
  281. jsonDocument = JsonDocument.Parse(storeAppsJsonData);
  282. jsonStoreAppsArray = jsonDocument.RootElement;
  283. }
  284. catch (Exception ex)
  285. {
  286. _dnsWebService._log.Write(ex);
  287. }
  288. }
  289. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  290. jsonWriter.WritePropertyName("apps");
  291. jsonWriter.WriteStartArray();
  292. foreach (string app in apps)
  293. {
  294. if (_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(app, out DnsApplication application))
  295. WriteAppAsJson(jsonWriter, application, jsonStoreAppsArray);
  296. }
  297. jsonWriter.WriteEndArray();
  298. }
  299. finally
  300. {
  301. if (jsonDocument is not null)
  302. jsonDocument.Dispose();
  303. }
  304. }
  305. public async Task ListStoreApps(HttpContext context)
  306. {
  307. UserSession session = context.GetCurrentSession();
  308. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.View))
  309. throw new DnsWebServiceException("Access was denied.");
  310. string storeAppsJsonData = await GetStoreAppsJsonData(false).WithTimeout(30000);
  311. using JsonDocument jsonDocument = JsonDocument.Parse(storeAppsJsonData);
  312. JsonElement jsonStoreAppsArray = jsonDocument.RootElement;
  313. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  314. jsonWriter.WritePropertyName("storeApps");
  315. jsonWriter.WriteStartArray();
  316. foreach (JsonElement jsonStoreApp in jsonStoreAppsArray.EnumerateArray())
  317. {
  318. string name = jsonStoreApp.GetProperty("name").GetString();
  319. string description = jsonStoreApp.GetProperty("description").GetString();
  320. string version = null;
  321. string url = null;
  322. string size = null;
  323. Version storeAppVersion = null;
  324. Version lastServerVersion = null;
  325. foreach (JsonElement jsonVersion in jsonStoreApp.GetProperty("versions").EnumerateArray())
  326. {
  327. string strServerVersion = jsonVersion.GetProperty("serverVersion").GetString();
  328. Version requiredServerVersion = new Version(strServerVersion);
  329. if (_dnsWebService._currentVersion < requiredServerVersion)
  330. continue;
  331. if ((lastServerVersion is not null) && (lastServerVersion > requiredServerVersion))
  332. continue;
  333. version = jsonVersion.GetProperty("version").GetString();
  334. url = jsonVersion.GetProperty("url").GetString();
  335. size = jsonVersion.GetProperty("size").GetString();
  336. storeAppVersion = new Version(version);
  337. lastServerVersion = requiredServerVersion;
  338. }
  339. if (storeAppVersion is null)
  340. continue; //app is not compatible
  341. jsonWriter.WriteStartObject();
  342. jsonWriter.WriteString("name", name);
  343. jsonWriter.WriteString("description", description);
  344. jsonWriter.WriteString("version", version);
  345. jsonWriter.WriteString("url", url);
  346. jsonWriter.WriteString("size", size);
  347. bool installed = _dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication installedApp);
  348. jsonWriter.WriteBoolean("installed", installed);
  349. if (installed)
  350. {
  351. jsonWriter.WriteString("installedVersion", DnsWebService.GetCleanVersion(installedApp.Version));
  352. jsonWriter.WriteBoolean("updateAvailable", storeAppVersion > installedApp.Version);
  353. }
  354. jsonWriter.WriteEndObject();
  355. }
  356. jsonWriter.WriteEndArray();
  357. }
  358. public async Task DownloadAndInstallAppAsync(HttpContext context)
  359. {
  360. UserSession session = context.GetCurrentSession();
  361. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  362. throw new DnsWebServiceException("Access was denied.");
  363. HttpRequest request = context.Request;
  364. string name = request.GetQueryOrForm("name").Trim();
  365. string url = request.GetQueryOrForm("url");
  366. if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
  367. throw new DnsWebServiceException("Parameter 'url' value must start with 'https://'.");
  368. string tmpFile = Path.GetTempFileName();
  369. try
  370. {
  371. using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
  372. {
  373. //download to temp file
  374. SocketsHttpHandler handler = new SocketsHttpHandler();
  375. handler.Proxy = _dnsWebService.DnsServer.Proxy;
  376. handler.UseProxy = _dnsWebService.DnsServer.Proxy is not null;
  377. handler.AutomaticDecompression = DecompressionMethods.All;
  378. using (HttpClient http = new HttpClient(new HttpClientNetworkHandler(handler, _dnsWebService.DnsServer.PreferIPv6 ? HttpClientNetworkType.PreferIPv6 : HttpClientNetworkType.Default, _dnsWebService.DnsServer)))
  379. {
  380. using (Stream httpStream = await http.GetStreamAsync(url))
  381. {
  382. await httpStream.CopyToAsync(fS);
  383. }
  384. }
  385. //install app
  386. fS.Position = 0;
  387. DnsApplication application = await _dnsWebService.DnsServer.DnsApplicationManager.InstallApplicationAsync(name, fS);
  388. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was installed successfully from: " + url);
  389. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  390. jsonWriter.WritePropertyName("installedApp");
  391. WriteAppAsJson(jsonWriter, application);
  392. }
  393. }
  394. finally
  395. {
  396. try
  397. {
  398. File.Delete(tmpFile);
  399. }
  400. catch (Exception ex)
  401. {
  402. _dnsWebService._log.Write(ex);
  403. }
  404. }
  405. }
  406. public async Task DownloadAndUpdateAppAsync(HttpContext context)
  407. {
  408. UserSession session = context.GetCurrentSession();
  409. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  410. throw new DnsWebServiceException("Access was denied.");
  411. HttpRequest request = context.Request;
  412. string name = request.GetQueryOrForm("name").Trim();
  413. string url = request.GetQueryOrForm("url");
  414. if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
  415. throw new DnsWebServiceException("Parameter 'url' value must start with 'https://'.");
  416. DnsApplication application = await DownloadAndUpdateAppAsync(name, url, false);
  417. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was updated successfully from: " + url);
  418. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  419. jsonWriter.WritePropertyName("updatedApp");
  420. WriteAppAsJson(jsonWriter, application);
  421. }
  422. public async Task InstallAppAsync(HttpContext context)
  423. {
  424. UserSession session = context.GetCurrentSession();
  425. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  426. throw new DnsWebServiceException("Access was denied.");
  427. HttpRequest request = context.Request;
  428. string name = request.GetQueryOrForm("name").Trim();
  429. if (!request.HasFormContentType || (request.Form.Files.Count == 0))
  430. throw new DnsWebServiceException("DNS application zip file is missing.");
  431. string tmpFile = Path.GetTempFileName();
  432. try
  433. {
  434. using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
  435. {
  436. //write to temp file
  437. await request.Form.Files[0].CopyToAsync(fS);
  438. //install app
  439. fS.Position = 0;
  440. DnsApplication application = await _dnsWebService.DnsServer.DnsApplicationManager.InstallApplicationAsync(name, fS);
  441. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was installed successfully.");
  442. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  443. jsonWriter.WritePropertyName("installedApp");
  444. WriteAppAsJson(jsonWriter, application);
  445. }
  446. }
  447. finally
  448. {
  449. try
  450. {
  451. File.Delete(tmpFile);
  452. }
  453. catch (Exception ex)
  454. {
  455. _dnsWebService._log.Write(ex);
  456. }
  457. }
  458. }
  459. public async Task UpdateAppAsync(HttpContext context)
  460. {
  461. UserSession session = context.GetCurrentSession();
  462. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  463. throw new DnsWebServiceException("Access was denied.");
  464. HttpRequest request = context.Request;
  465. string name = request.GetQueryOrForm("name").Trim();
  466. if (!request.HasFormContentType || (request.Form.Files.Count == 0))
  467. throw new DnsWebServiceException("DNS application zip file is missing.");
  468. string tmpFile = Path.GetTempFileName();
  469. try
  470. {
  471. using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
  472. {
  473. //write to temp file
  474. await request.Form.Files[0].CopyToAsync(fS);
  475. //update app
  476. fS.Position = 0;
  477. DnsApplication application = await _dnsWebService.DnsServer.DnsApplicationManager.UpdateApplicationAsync(name, fS);
  478. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was updated successfully.");
  479. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  480. jsonWriter.WritePropertyName("updatedApp");
  481. WriteAppAsJson(jsonWriter, application);
  482. }
  483. }
  484. finally
  485. {
  486. try
  487. {
  488. File.Delete(tmpFile);
  489. }
  490. catch (Exception ex)
  491. {
  492. _dnsWebService._log.Write(ex);
  493. }
  494. }
  495. }
  496. public void UninstallApp(HttpContext context)
  497. {
  498. UserSession session = context.GetCurrentSession();
  499. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Delete))
  500. throw new DnsWebServiceException("Access was denied.");
  501. HttpRequest request = context.Request;
  502. string name = request.GetQueryOrForm("name").Trim();
  503. _dnsWebService.DnsServer.DnsApplicationManager.UninstallApplication(name);
  504. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' was uninstalled successfully.");
  505. }
  506. public async Task GetAppConfigAsync(HttpContext context)
  507. {
  508. UserSession session = context.GetCurrentSession();
  509. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.View))
  510. throw new DnsWebServiceException("Access was denied.");
  511. HttpRequest request = context.Request;
  512. string name = request.GetQueryOrForm("name").Trim();
  513. if (!_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication application))
  514. throw new DnsWebServiceException("DNS application was not found: " + name);
  515. string config = await application.GetConfigAsync();
  516. Utf8JsonWriter jsonWriter = context.GetCurrentJsonWriter();
  517. jsonWriter.WriteString("config", config);
  518. }
  519. public async Task SetAppConfigAsync(HttpContext context)
  520. {
  521. UserSession session = context.GetCurrentSession();
  522. if (!_dnsWebService._authManager.IsPermitted(PermissionSection.Apps, session.User, PermissionFlag.Modify))
  523. throw new DnsWebServiceException("Access was denied.");
  524. HttpRequest request = context.Request;
  525. string name = request.GetQueryOrForm("name").Trim();
  526. if (!_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication application))
  527. throw new DnsWebServiceException("DNS application was not found: " + name);
  528. string config = request.QueryOrForm("config");
  529. if (config is null)
  530. throw new DnsWebServiceException("Parameter 'config' missing.");
  531. if (config.Length == 0)
  532. config = null;
  533. await application.SetConfigAsync(config);
  534. _dnsWebService._log.Write(context.GetRemoteEndPoint(), "[" + session.User.Username + "] DNS application '" + name + "' app config was saved successfully.");
  535. }
  536. #endregion
  537. #region properties
  538. public bool EnableAutomaticUpdate
  539. {
  540. get { return _appUpdateTimer is not null; }
  541. set
  542. {
  543. if (value)
  544. StartAutomaticUpdate();
  545. else
  546. StopAutomaticUpdate();
  547. }
  548. }
  549. #endregion
  550. }
  551. }