123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- /*
- Technitium DNS Server
- Copyright (C) 2022 Shreyas Zare (shreyas@technitium.com)
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- using DnsServerCore.ApplicationCommon;
- using DnsServerCore.Dns.Applications;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- using TechnitiumLibrary;
- namespace DnsServerCore
- {
- class WebServiceAppsApi
- {
- #region variables
- readonly DnsWebService _dnsWebService;
- readonly Uri _appStoreUri;
- string _storeAppsJsonData;
- DateTime _storeAppsJsonDataUpdatedOn;
- const int STORE_APPS_JSON_DATA_CACHE_TIME_SECONDS = 300;
- #endregion
- #region constructor
- public WebServiceAppsApi(DnsWebService dnsWebService, Uri appStoreUri)
- {
- _dnsWebService = dnsWebService;
- _appStoreUri = appStoreUri;
- }
- #endregion
- #region private
- private async Task<string> GetStoreAppsJsonData()
- {
- if ((_storeAppsJsonData == null) || (DateTime.UtcNow > _storeAppsJsonDataUpdatedOn.AddSeconds(STORE_APPS_JSON_DATA_CACHE_TIME_SECONDS)))
- {
- SocketsHttpHandler handler = new SocketsHttpHandler();
- handler.Proxy = _dnsWebService.DnsServer.Proxy;
- handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
- using (HttpClient http = new HttpClient(handler))
- {
- _storeAppsJsonData = await http.GetStringAsync(_appStoreUri);
- _storeAppsJsonDataUpdatedOn = DateTime.UtcNow;
- }
- }
- return _storeAppsJsonData;
- }
- #endregion
- #region public
- public async Task ListInstalledAppsAsync(JsonTextWriter jsonWriter)
- {
- List<string> apps = new List<string>(_dnsWebService.DnsServer.DnsApplicationManager.Applications.Keys);
- apps.Sort();
- dynamic jsonStoreAppsArray = null;
- if (apps.Count > 0)
- {
- try
- {
- string storeAppsJsonData = await GetStoreAppsJsonData().WithTimeout(5000);
- jsonStoreAppsArray = JsonConvert.DeserializeObject(storeAppsJsonData);
- }
- catch
- { }
- }
- jsonWriter.WritePropertyName("apps");
- jsonWriter.WriteStartArray();
- foreach (string app in apps)
- {
- if (_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(app, out DnsApplication application))
- {
- jsonWriter.WriteStartObject();
- jsonWriter.WritePropertyName("name");
- jsonWriter.WriteValue(application.Name);
- jsonWriter.WritePropertyName("version");
- jsonWriter.WriteValue(DnsWebService.GetCleanVersion(application.Version));
- if (jsonStoreAppsArray != null)
- {
- foreach (dynamic jsonStoreApp in jsonStoreAppsArray)
- {
- string name = jsonStoreApp.name.Value;
- if (name.Equals(application.Name))
- {
- string version = jsonStoreApp.version.Value;
- string url = jsonStoreApp.url.Value;
- jsonWriter.WritePropertyName("updateVersion");
- jsonWriter.WriteValue(version);
- jsonWriter.WritePropertyName("updateUrl");
- jsonWriter.WriteValue(url);
- jsonWriter.WritePropertyName("updateAvailable");
- jsonWriter.WriteValue(new Version(version) > application.Version);
- break;
- }
- }
- }
- jsonWriter.WritePropertyName("dnsApps");
- {
- jsonWriter.WriteStartArray();
- foreach (KeyValuePair<string, IDnsApplication> dnsApp in application.DnsApplications)
- {
- jsonWriter.WriteStartObject();
- jsonWriter.WritePropertyName("classPath");
- jsonWriter.WriteValue(dnsApp.Key);
- jsonWriter.WritePropertyName("description");
- jsonWriter.WriteValue(dnsApp.Value.Description);
- if (dnsApp.Value is IDnsAppRecordRequestHandler appRecordHandler)
- {
- jsonWriter.WritePropertyName("isAppRecordRequestHandler");
- jsonWriter.WriteValue(true);
- jsonWriter.WritePropertyName("recordDataTemplate");
- jsonWriter.WriteValue(appRecordHandler.ApplicationRecordDataTemplate);
- }
- else
- {
- jsonWriter.WritePropertyName("isAppRecordRequestHandler");
- jsonWriter.WriteValue(false);
- }
- jsonWriter.WritePropertyName("isRequestController");
- jsonWriter.WriteValue(dnsApp.Value is IDnsRequestController);
- jsonWriter.WritePropertyName("isAuthoritativeRequestHandler");
- jsonWriter.WriteValue(dnsApp.Value is IDnsAuthoritativeRequestHandler);
- jsonWriter.WritePropertyName("isQueryLogger");
- jsonWriter.WriteValue(dnsApp.Value is IDnsQueryLogger);
- jsonWriter.WriteEndObject();
- }
- jsonWriter.WriteEndArray();
- }
- jsonWriter.WriteEndObject();
- }
- }
- jsonWriter.WriteEndArray();
- }
- public async Task ListStoreApps(JsonTextWriter jsonWriter)
- {
- string storeAppsJsonData = await GetStoreAppsJsonData();
- dynamic jsonStoreAppsArray = JsonConvert.DeserializeObject(storeAppsJsonData);
- jsonWriter.WritePropertyName("storeApps");
- jsonWriter.WriteStartArray();
- foreach (dynamic jsonStoreApp in jsonStoreAppsArray)
- {
- string name = jsonStoreApp.name.Value;
- string version = jsonStoreApp.version.Value;
- string description = jsonStoreApp.description.Value;
- string url = jsonStoreApp.url.Value;
- string size = jsonStoreApp.size.Value;
- jsonWriter.WriteStartObject();
- jsonWriter.WritePropertyName("name");
- jsonWriter.WriteValue(name);
- jsonWriter.WritePropertyName("version");
- jsonWriter.WriteValue(version);
- jsonWriter.WritePropertyName("description");
- jsonWriter.WriteValue(description);
- jsonWriter.WritePropertyName("url");
- jsonWriter.WriteValue(url);
- jsonWriter.WritePropertyName("size");
- jsonWriter.WriteValue(size);
- bool installed = _dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication installedApp);
- jsonWriter.WritePropertyName("installed");
- jsonWriter.WriteValue(installed);
- if (installed)
- {
- jsonWriter.WritePropertyName("installedVersion");
- jsonWriter.WriteValue(DnsWebService.GetCleanVersion(installedApp.Version));
- jsonWriter.WritePropertyName("updateAvailable");
- jsonWriter.WriteValue(new Version(version) > installedApp.Version);
- }
- jsonWriter.WriteEndObject();
- }
- jsonWriter.WriteEndArray();
- }
- public async Task DownloadAndInstallAppAsync(HttpListenerRequest request)
- {
- string name = request.QueryString["name"];
- if (string.IsNullOrEmpty(name))
- throw new DnsWebServiceException("Parameter 'name' missing.");
- name = name.Trim();
- string url = request.QueryString["url"];
- if (string.IsNullOrEmpty(url))
- throw new DnsWebServiceException("Parameter 'url' missing.");
- if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
- throw new DnsWebServiceException("Parameter 'url' value must start with 'https://'.");
- string tmpFile = Path.GetTempFileName();
- try
- {
- using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
- {
- //download to temp file
- SocketsHttpHandler handler = new SocketsHttpHandler();
- handler.Proxy = _dnsWebService.DnsServer.Proxy;
- handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
- using (HttpClient http = new HttpClient(handler))
- {
- using (Stream httpStream = await http.GetStreamAsync(url))
- {
- await httpStream.CopyToAsync(fS);
- }
- }
- //install app
- fS.Position = 0;
- await _dnsWebService.DnsServer.DnsApplicationManager.InstallApplicationAsync(name, fS);
- _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DNS application '" + name + "' was installed successfully from: " + url);
- }
- }
- finally
- {
- try
- {
- File.Delete(tmpFile);
- }
- catch (Exception ex)
- {
- _dnsWebService.Log.Write(ex);
- }
- }
- }
- public async Task DownloadAndUpdateAppAsync(HttpListenerRequest request)
- {
- string name = request.QueryString["name"];
- if (string.IsNullOrEmpty(name))
- throw new DnsWebServiceException("Parameter 'name' missing.");
- name = name.Trim();
- string url = request.QueryString["url"];
- if (string.IsNullOrEmpty(url))
- throw new DnsWebServiceException("Parameter 'url' missing.");
- if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
- throw new DnsWebServiceException("Parameter 'url' value must start with 'https://'.");
- string tmpFile = Path.GetTempFileName();
- try
- {
- using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
- {
- //download to temp file
- SocketsHttpHandler handler = new SocketsHttpHandler();
- handler.Proxy = _dnsWebService.DnsServer.Proxy;
- handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
- using (HttpClient http = new HttpClient(handler))
- {
- using (Stream httpStream = await http.GetStreamAsync(url))
- {
- await httpStream.CopyToAsync(fS);
- }
- }
- //update app
- fS.Position = 0;
- await _dnsWebService.DnsServer.DnsApplicationManager.UpdateApplicationAsync(name, fS);
- _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DNS application '" + name + "' was updated successfully from: " + url);
- }
- }
- finally
- {
- try
- {
- File.Delete(tmpFile);
- }
- catch (Exception ex)
- {
- _dnsWebService.Log.Write(ex);
- }
- }
- }
- public async Task InstallAppAsync(HttpListenerRequest request)
- {
- string name = request.QueryString["name"];
- if (string.IsNullOrEmpty(name))
- throw new DnsWebServiceException("Parameter 'name' missing.");
- name = name.Trim();
- #region skip to content
- int crlfCount = 0;
- int byteRead;
- while (crlfCount != 4)
- {
- byteRead = request.InputStream.ReadByte();
- switch (byteRead)
- {
- case -1:
- throw new EndOfStreamException();
- case 13: //CR
- case 10: //LF
- crlfCount++;
- break;
- default:
- crlfCount = 0;
- break;
- }
- }
- #endregion
- string tmpFile = Path.GetTempFileName();
- try
- {
- using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
- {
- //write to temp file
- await request.InputStream.CopyToAsync(fS);
- //install app
- fS.Position = 0;
- await _dnsWebService.DnsServer.DnsApplicationManager.InstallApplicationAsync(name, fS);
- _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DNS application '" + name + "' was installed successfully.");
- }
- }
- finally
- {
- try
- {
- File.Delete(tmpFile);
- }
- catch (Exception ex)
- {
- _dnsWebService.Log.Write(ex);
- }
- }
- }
- public async Task UpdateAppAsync(HttpListenerRequest request)
- {
- string name = request.QueryString["name"];
- if (string.IsNullOrEmpty(name))
- throw new DnsWebServiceException("Parameter 'name' missing.");
- name = name.Trim();
- #region skip to content
- int crlfCount = 0;
- int byteRead;
- while (crlfCount != 4)
- {
- byteRead = request.InputStream.ReadByte();
- switch (byteRead)
- {
- case -1:
- throw new EndOfStreamException();
- case 13: //CR
- case 10: //LF
- crlfCount++;
- break;
- default:
- crlfCount = 0;
- break;
- }
- }
- #endregion
- string tmpFile = Path.GetTempFileName();
- try
- {
- using (FileStream fS = new FileStream(tmpFile, FileMode.Create, FileAccess.ReadWrite))
- {
- //write to temp file
- await request.InputStream.CopyToAsync(fS);
- //update app
- fS.Position = 0;
- await _dnsWebService.DnsServer.DnsApplicationManager.UpdateApplicationAsync(name, fS);
- _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DNS application '" + name + "' was updated successfully.");
- }
- }
- finally
- {
- try
- {
- File.Delete(tmpFile);
- }
- catch (Exception ex)
- {
- _dnsWebService.Log.Write(ex);
- }
- }
- }
- public void UninstallApp(HttpListenerRequest request)
- {
- string name = request.QueryString["name"];
- if (string.IsNullOrEmpty(name))
- throw new DnsWebServiceException("Parameter 'name' missing.");
- name = name.Trim();
- _dnsWebService.DnsServer.DnsApplicationManager.UninstallApplication(name);
- _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DNS application '" + name + "' was uninstalled successfully.");
- }
- public async Task GetAppConfigAsync(HttpListenerRequest request, JsonTextWriter jsonWriter)
- {
- string name = request.QueryString["name"];
- if (string.IsNullOrEmpty(name))
- throw new DnsWebServiceException("Parameter 'name' missing.");
- name = name.Trim();
- if (!_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication application))
- throw new DnsWebServiceException("DNS application was not found: " + name);
- string config = await application.GetConfigAsync();
- jsonWriter.WritePropertyName("config");
- jsonWriter.WriteValue(config);
- }
- public async Task SetAppConfigAsync(HttpListenerRequest request)
- {
- string name = request.QueryString["name"];
- if (string.IsNullOrEmpty(name))
- throw new DnsWebServiceException("Parameter 'name' missing.");
- name = name.Trim();
- if (!_dnsWebService.DnsServer.DnsApplicationManager.Applications.TryGetValue(name, out DnsApplication application))
- throw new DnsWebServiceException("DNS application was not found: " + name);
- string formRequest;
- using (StreamReader sR = new StreamReader(request.InputStream, request.ContentEncoding))
- {
- formRequest = sR.ReadToEnd();
- }
- string[] formParts = formRequest.Split('&');
- foreach (string formPart in formParts)
- {
- if (formPart.StartsWith("config="))
- {
- string config = formPart.Substring(7);
- if (config.Length == 0)
- config = null;
- await application.SetConfigAsync(config);
- _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DNS application '" + name + "' app config was saved successfully.");
- return;
- }
- }
- throw new DnsWebServiceException("Missing POST parameter: config");
- }
- #endregion
- }
- }
|