Extensions.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.Auth;
  16. using Microsoft.AspNetCore.Builder;
  17. using Microsoft.AspNetCore.Http;
  18. using Microsoft.AspNetCore.Routing;
  19. using System;
  20. using System.Net;
  21. using System.Text.Json;
  22. using TechnitiumLibrary.Net;
  23. namespace DnsServerCore
  24. {
  25. static class Extensions
  26. {
  27. readonly static string[] HTTP_METHODS = new string[] { "GET", "POST" };
  28. public static IPEndPoint GetRemoteEndPoint(this HttpContext context, bool ignoreXRealIpHeader = false)
  29. {
  30. try
  31. {
  32. IPAddress remoteIP = context.Connection.RemoteIpAddress;
  33. if (remoteIP is null)
  34. return new IPEndPoint(IPAddress.Any, 0);
  35. if (remoteIP.IsIPv4MappedToIPv6)
  36. remoteIP = remoteIP.MapToIPv4();
  37. if (!ignoreXRealIpHeader && NetUtilities.IsPrivateIP(remoteIP))
  38. {
  39. string xRealIp = context.Request.Headers["X-Real-IP"];
  40. if (IPAddress.TryParse(xRealIp, out IPAddress address))
  41. {
  42. //get the real IP address of the requesting client from X-Real-IP header set in nginx proxy_pass block
  43. return new IPEndPoint(address, 0);
  44. }
  45. }
  46. return new IPEndPoint(remoteIP, context.Connection.RemotePort);
  47. }
  48. catch
  49. {
  50. return new IPEndPoint(IPAddress.Any, 0);
  51. }
  52. }
  53. public static UserSession GetCurrentSession(this HttpContext context)
  54. {
  55. if (context.Items["session"] is UserSession userSession)
  56. return userSession;
  57. throw new InvalidOperationException();
  58. }
  59. public static Utf8JsonWriter GetCurrentJsonWriter(this HttpContext context)
  60. {
  61. if (context.Items["jsonWriter"] is Utf8JsonWriter jsonWriter)
  62. return jsonWriter;
  63. throw new InvalidOperationException();
  64. }
  65. public static IEndpointConventionBuilder MapGetAndPost(this IEndpointRouteBuilder endpoints, string pattern, RequestDelegate requestDelegate)
  66. {
  67. return endpoints.MapMethods(pattern, HTTP_METHODS, requestDelegate);
  68. }
  69. public static IEndpointConventionBuilder MapGetAndPost(this IEndpointRouteBuilder endpoints, string pattern, Delegate handler)
  70. {
  71. return endpoints.MapMethods(pattern, HTTP_METHODS, handler);
  72. }
  73. public static string QueryOrForm(this HttpRequest request, string parameter)
  74. {
  75. string value = request.Query[parameter];
  76. if ((value is null) && request.HasFormContentType)
  77. value = request.Form[parameter];
  78. return value;
  79. }
  80. public static string GetQueryOrForm(this HttpRequest request, string parameter)
  81. {
  82. string value = request.QueryOrForm(parameter);
  83. if (string.IsNullOrEmpty(value))
  84. throw new DnsWebServiceException("Parameter '" + parameter + "' missing.");
  85. return value;
  86. }
  87. public static string GetQueryOrForm(this HttpRequest request, string parameter, string defaultValue)
  88. {
  89. string value = request.QueryOrForm(parameter);
  90. if (string.IsNullOrEmpty(value))
  91. return defaultValue;
  92. return value;
  93. }
  94. public static T GetQueryOrForm<T>(this HttpRequest request, string parameter, Func<string, T> parse)
  95. {
  96. string value = request.QueryOrForm(parameter);
  97. if (string.IsNullOrEmpty(value))
  98. throw new DnsWebServiceException("Parameter '" + parameter + "' missing.");
  99. return parse(value);
  100. }
  101. public static T GetQueryOrFormEnum<T>(this HttpRequest request, string parameter) where T : struct
  102. {
  103. string value = request.QueryOrForm(parameter);
  104. if (string.IsNullOrEmpty(value))
  105. throw new DnsWebServiceException("Parameter '" + parameter + "' missing.");
  106. return Enum.Parse<T>(value, true);
  107. }
  108. public static T GetQueryOrForm<T>(this HttpRequest request, string parameter, Func<string, T> parse, T defaultValue)
  109. {
  110. string value = request.QueryOrForm(parameter);
  111. if (string.IsNullOrEmpty(value))
  112. return defaultValue;
  113. return parse(value);
  114. }
  115. public static T GetQueryOrFormEnum<T>(this HttpRequest request, string parameter, T defaultValue) where T : struct
  116. {
  117. string value = request.QueryOrForm(parameter);
  118. if (string.IsNullOrEmpty(value))
  119. return defaultValue;
  120. return Enum.Parse<T>(value, true);
  121. }
  122. public static bool TryGetQueryOrForm(this HttpRequest request, string parameter, out string value)
  123. {
  124. value = request.QueryOrForm(parameter);
  125. if (string.IsNullOrEmpty(value))
  126. return false;
  127. return true;
  128. }
  129. public static bool TryGetQueryOrForm<T>(this HttpRequest request, string parameter, Func<string, T> parse, out T value)
  130. {
  131. string strValue = request.QueryOrForm(parameter);
  132. if (string.IsNullOrEmpty(strValue))
  133. {
  134. value = default;
  135. return false;
  136. }
  137. value = parse(strValue);
  138. return true;
  139. }
  140. public static bool TryGetQueryOrFormEnum<T>(this HttpRequest request, string parameter, out T value) where T : struct
  141. {
  142. string strValue = request.QueryOrForm(parameter);
  143. if (string.IsNullOrEmpty(strValue))
  144. {
  145. value = default;
  146. return false;
  147. }
  148. return Enum.TryParse(strValue, true, out value);
  149. }
  150. public static string GetQueryOrFormAlt(this HttpRequest request, string parameter, string alternateParameter)
  151. {
  152. string value = request.QueryOrForm(parameter);
  153. if (string.IsNullOrEmpty(value))
  154. {
  155. value = request.QueryOrForm(alternateParameter);
  156. if (string.IsNullOrEmpty(value))
  157. throw new DnsWebServiceException("Parameter '" + parameter + "' missing.");
  158. }
  159. return value;
  160. }
  161. public static string GetQueryOrFormAlt(this HttpRequest request, string parameter, string alternateParameter, string defaultValue)
  162. {
  163. string value = request.QueryOrForm(parameter);
  164. if (string.IsNullOrEmpty(value))
  165. {
  166. value = request.QueryOrForm(alternateParameter);
  167. if (string.IsNullOrEmpty(value))
  168. return defaultValue;
  169. }
  170. return value;
  171. }
  172. public static T GetQueryOrFormAlt<T>(this HttpRequest request, string parameter, string alternateParameter, Func<string, T> parse)
  173. {
  174. string value = request.QueryOrForm(parameter);
  175. if (string.IsNullOrEmpty(value))
  176. {
  177. value = request.QueryOrForm(alternateParameter);
  178. if (string.IsNullOrEmpty(value))
  179. throw new DnsWebServiceException("Parameter '" + parameter + "' missing.");
  180. }
  181. return parse(value);
  182. }
  183. public static T GetQueryOrFormAlt<T>(this HttpRequest request, string parameter, string alternateParameter, Func<string, T> parse, T defaultValue)
  184. {
  185. string value = request.QueryOrForm(parameter);
  186. if (string.IsNullOrEmpty(value))
  187. {
  188. value = request.QueryOrForm(alternateParameter);
  189. if (string.IsNullOrEmpty(value))
  190. return defaultValue;
  191. }
  192. return parse(value);
  193. }
  194. }
  195. }