Extensions.cs 8.3 KB

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