DnsApplication.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Reflection;
  20. using System.Threading.Tasks;
  21. namespace DnsServerCore.Dns.Applications
  22. {
  23. public sealed class DnsApplication : IDisposable
  24. {
  25. #region variables
  26. readonly static Type _dnsApplicationInterface = typeof(IDnsApplication);
  27. readonly IDnsServer _dnsServer;
  28. readonly string _name;
  29. readonly DnsApplicationAssemblyLoadContext _appContext;
  30. readonly string _description;
  31. readonly Version _version;
  32. readonly IReadOnlyDictionary<string, IDnsApplication> _dnsApplications;
  33. readonly IReadOnlyDictionary<string, IDnsAppRecordRequestHandler> _dnsAppRecordRequestHandlers;
  34. readonly IReadOnlyDictionary<string, IDnsRequestController> _dnsRequestControllers;
  35. readonly IReadOnlyDictionary<string, IDnsAuthoritativeRequestHandler> _dnsAuthoritativeRequestHandlers;
  36. readonly IReadOnlyDictionary<string, IDnsRequestBlockingHandler> _dnsRequestBlockingHandlers;
  37. readonly IReadOnlyDictionary<string, IDnsQueryLogger> _dnsQueryLoggers;
  38. readonly IReadOnlyDictionary<string, IDnsPostProcessor> _dnsPostProcessors;
  39. #endregion
  40. #region constructor
  41. public DnsApplication(IDnsServer dnsServer, string name)
  42. {
  43. _dnsServer = dnsServer;
  44. _name = name;
  45. _appContext = new DnsApplicationAssemblyLoadContext(_dnsServer);
  46. //load apps
  47. Dictionary<string, IDnsApplication> dnsApplications = new Dictionary<string, IDnsApplication>();
  48. Dictionary<string, IDnsAppRecordRequestHandler> dnsAppRecordRequestHandlers = new Dictionary<string, IDnsAppRecordRequestHandler>(2);
  49. Dictionary<string, IDnsRequestController> dnsRequestControllers = new Dictionary<string, IDnsRequestController>(1);
  50. Dictionary<string, IDnsAuthoritativeRequestHandler> dnsAuthoritativeRequestHandlers = new Dictionary<string, IDnsAuthoritativeRequestHandler>(1);
  51. Dictionary<string, IDnsRequestBlockingHandler> dnsRequestBlockingHandlers = new Dictionary<string, IDnsRequestBlockingHandler>(1);
  52. Dictionary<string, IDnsQueryLogger> dnsQueryLoggers = new Dictionary<string, IDnsQueryLogger>(1);
  53. Dictionary<string, IDnsPostProcessor> dnsPostProcessors = new Dictionary<string, IDnsPostProcessor>(1);
  54. foreach (Assembly appAssembly in _appContext.AppAssemblies)
  55. {
  56. try
  57. {
  58. foreach (Type classType in appAssembly.ExportedTypes)
  59. {
  60. bool isDnsApp = false;
  61. foreach (Type interfaceType in classType.GetInterfaces())
  62. {
  63. if (interfaceType == _dnsApplicationInterface)
  64. {
  65. isDnsApp = true;
  66. break;
  67. }
  68. }
  69. if (isDnsApp)
  70. {
  71. try
  72. {
  73. IDnsApplication app = Activator.CreateInstance(classType) as IDnsApplication;
  74. dnsApplications.Add(classType.FullName, app);
  75. if (app is IDnsAppRecordRequestHandler appRecordHandler)
  76. dnsAppRecordRequestHandlers.Add(classType.FullName, appRecordHandler);
  77. if (app is IDnsRequestController requestController)
  78. dnsRequestControllers.Add(classType.FullName, requestController);
  79. if (app is IDnsAuthoritativeRequestHandler requestHandler)
  80. dnsAuthoritativeRequestHandlers.Add(classType.FullName, requestHandler);
  81. if (app is IDnsRequestBlockingHandler blockingHandler)
  82. dnsRequestBlockingHandlers.Add(classType.FullName, blockingHandler);
  83. if (app is IDnsQueryLogger logger)
  84. dnsQueryLoggers.Add(classType.FullName, logger);
  85. if (app is IDnsPostProcessor postProcessor)
  86. dnsPostProcessors.Add(classType.FullName, postProcessor);
  87. if (_description is null)
  88. {
  89. AssemblyDescriptionAttribute attribute = appAssembly.GetCustomAttribute<AssemblyDescriptionAttribute>();
  90. if (attribute is not null)
  91. _description = attribute.Description.Replace("\\n", "\n");
  92. }
  93. if (_version is null)
  94. _version = appAssembly.GetName().Version;
  95. }
  96. catch (Exception ex)
  97. {
  98. _dnsServer.WriteLog(ex);
  99. }
  100. }
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. _dnsServer.WriteLog(ex);
  106. }
  107. }
  108. if (_version is null)
  109. {
  110. if (dnsApplications.Count > 0)
  111. _version = new Version(1, 0);
  112. else
  113. _version = new Version(0, 0);
  114. }
  115. _dnsApplications = dnsApplications;
  116. _dnsAppRecordRequestHandlers = dnsAppRecordRequestHandlers;
  117. _dnsRequestControllers = dnsRequestControllers;
  118. _dnsAuthoritativeRequestHandlers = dnsAuthoritativeRequestHandlers;
  119. _dnsRequestBlockingHandlers = dnsRequestBlockingHandlers;
  120. _dnsQueryLoggers = dnsQueryLoggers;
  121. _dnsPostProcessors = dnsPostProcessors;
  122. }
  123. #endregion
  124. #region IDisposable
  125. bool _disposed;
  126. private void Dispose(bool disposing)
  127. {
  128. if (_disposed)
  129. return;
  130. if (disposing)
  131. {
  132. if (_dnsApplications is not null)
  133. {
  134. foreach (KeyValuePair<string, IDnsApplication> app in _dnsApplications)
  135. app.Value.Dispose();
  136. }
  137. if (_appContext != null)
  138. _appContext.Unload();
  139. }
  140. _disposed = true;
  141. }
  142. public void Dispose()
  143. {
  144. Dispose(true);
  145. }
  146. #endregion
  147. #region internal
  148. internal async Task InitializeAsync()
  149. {
  150. string config = await GetConfigAsync();
  151. foreach (KeyValuePair<string, IDnsApplication> app in _dnsApplications)
  152. {
  153. try
  154. {
  155. await app.Value.InitializeAsync(_dnsServer, config);
  156. }
  157. catch (Exception ex)
  158. {
  159. _dnsServer.WriteLog(ex);
  160. }
  161. }
  162. }
  163. #endregion
  164. #region public
  165. public Task<string> GetConfigAsync()
  166. {
  167. string configFile = Path.Combine(_dnsServer.ApplicationFolder, "dnsApp.config");
  168. if (File.Exists(configFile))
  169. return File.ReadAllTextAsync(configFile);
  170. return Task.FromResult<string>(null);
  171. }
  172. public async Task SetConfigAsync(string config)
  173. {
  174. string configFile = Path.Combine(_dnsServer.ApplicationFolder, "dnsApp.config");
  175. foreach (KeyValuePair<string, IDnsApplication> app in _dnsApplications)
  176. await app.Value.InitializeAsync(_dnsServer, config);
  177. if (string.IsNullOrEmpty(config))
  178. File.Delete(configFile);
  179. else
  180. await File.WriteAllTextAsync(configFile, config);
  181. }
  182. #endregion
  183. #region properties
  184. public IDnsServer DnsServer
  185. { get { return _dnsServer; } }
  186. public string Name
  187. { get { return _name; } }
  188. public string Description
  189. { get { return _description; } }
  190. public Version Version
  191. { get { return _version; } }
  192. public IReadOnlyDictionary<string, IDnsApplication> DnsApplications
  193. { get { return _dnsApplications; } }
  194. public IReadOnlyDictionary<string, IDnsAppRecordRequestHandler> DnsAppRecordRequestHandlers
  195. { get { return _dnsAppRecordRequestHandlers; } }
  196. public IReadOnlyDictionary<string, IDnsRequestController> DnsRequestControllers
  197. { get { return _dnsRequestControllers; } }
  198. public IReadOnlyDictionary<string, IDnsAuthoritativeRequestHandler> DnsAuthoritativeRequestHandlers
  199. { get { return _dnsAuthoritativeRequestHandlers; } }
  200. public IReadOnlyDictionary<string, IDnsRequestBlockingHandler> DnsRequestBlockingHandler
  201. { get { return _dnsRequestBlockingHandlers; } }
  202. public IReadOnlyDictionary<string, IDnsQueryLogger> DnsQueryLoggers
  203. { get { return _dnsQueryLoggers; } }
  204. public IReadOnlyDictionary<string, IDnsPostProcessor> DnsPostProcessors
  205. { get { return _dnsPostProcessors; } }
  206. #endregion
  207. }
  208. }