ErrorController.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using Hangfire;
  2. using Masuit.MyBlogs.Core.Common;
  3. using Masuit.MyBlogs.Core.Configs;
  4. using Masuit.MyBlogs.Core.Extensions;
  5. using Masuit.MyBlogs.Core.Infrastructure.Services;
  6. using Masuit.MyBlogs.Core.Models.Enum;
  7. using Masuit.Tools;
  8. using Masuit.Tools.Logging;
  9. using Masuit.Tools.Security;
  10. using Masuit.Tools.Systems;
  11. using Microsoft.AspNetCore.Diagnostics;
  12. using Microsoft.AspNetCore.Http;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Microsoft.EntityFrameworkCore;
  15. using System;
  16. using System.Web;
  17. namespace Masuit.MyBlogs.Core.Controllers
  18. {
  19. /// <summary>
  20. /// 错误页
  21. /// </summary>
  22. [ApiExplorerSettings(IgnoreApi = true)]
  23. public class ErrorController : Controller
  24. {
  25. public BroadcastService BroadcastService { get; set; }
  26. /// <summary>
  27. /// 404
  28. /// </summary>
  29. /// <returns></returns>
  30. [Route("error"), Route("{*url}", Order = 99999), ResponseCache(Duration = 36000)]
  31. public ActionResult Index()
  32. {
  33. if (Request.Method.ToLower().Equals("get"))
  34. {
  35. Response.StatusCode = 404;
  36. return View();
  37. }
  38. return Json(new
  39. {
  40. StatusCode = 404,
  41. Success = false,
  42. Message = "页面未找到!"
  43. });
  44. }
  45. /// <summary>
  46. /// 503
  47. /// </summary>
  48. /// <returns></returns>
  49. [Route("ServiceUnavailable")]
  50. public ActionResult ServiceUnavailable()
  51. {
  52. var feature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
  53. if (feature != null)
  54. {
  55. string err;
  56. var req = HttpContext.Request;
  57. var ip = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
  58. switch (feature.Error)
  59. {
  60. case DbUpdateConcurrencyException ex:
  61. err = $"异常源:{ex.Source},异常类型:{ex.GetType().Name},\n请求路径:{req.Scheme}://{req.Host}{HttpUtility.UrlDecode(req.Path)},客户端用户代理:{req.Headers["User-Agent"]},客户端IP:{ip}\t{ex.InnerException?.Message}\t";
  62. LogManager.Error(err, ex);
  63. break;
  64. case DbUpdateException ex:
  65. err = $"异常源:{ex.Source},异常类型:{ex.GetType().Name},\n请求路径:{req.Scheme}://{req.Host}{HttpUtility.UrlDecode(req.Path)},客户端用户代理:{req.Headers["User-Agent"]},客户端IP:{ip}\t{ex?.InnerException?.Message}\t";
  66. LogManager.Error(err, ex);
  67. break;
  68. case AggregateException ex:
  69. LogManager.Debug("↓↓↓" + ex.Message + "↓↓↓");
  70. ex.Handle(e =>
  71. {
  72. LogManager.Error($"异常源:{e.Source},异常类型:{e.GetType().Name},\n请求路径:{req.Scheme}://{req.Host}{HttpUtility.UrlDecode(req.Path)},客户端用户代理:{req.Headers["User-Agent"]},客户端IP:{ip}\t", e);
  73. return true;
  74. });
  75. break;
  76. case NotFoundException ex:
  77. Response.StatusCode = 404;
  78. return Request.Method.ToLower().Equals("get") ? (ActionResult)View("Index") : Json(new
  79. {
  80. StatusCode = 404,
  81. Success = false,
  82. ex.Message
  83. });
  84. default:
  85. LogManager.Error($"异常源:{feature.Error.Source},异常类型:{feature.Error.GetType().Name},\n请求路径:{req.Scheme}://{req.Host}{HttpUtility.UrlDecode(req.Path)},客户端用户代理:{req.Headers["User-Agent"]},客户端IP:{ip}\t", feature.Error);
  86. break;
  87. }
  88. }
  89. if (Request.Method.ToLower().Equals("get"))
  90. {
  91. Response.StatusCode = 503;
  92. return View();
  93. }
  94. return Json(new
  95. {
  96. StatusCode = 503,
  97. Success = false,
  98. Message = "服务器发生错误!"
  99. });
  100. }
  101. /// <summary>
  102. /// 访问被拒绝
  103. /// </summary>
  104. /// <returns></returns>
  105. [Route("AccessDeny"), ResponseCache(Duration = 360000)]
  106. public ActionResult AccessDeny()
  107. {
  108. if (Request.Cookies["Email"].MDString3(AppConfig.BaiduAK).Equals(Request.Cookies["FullAccessToken"]))
  109. {
  110. return Redirect("/");
  111. }
  112. Response.StatusCode = 403;
  113. return View();
  114. }
  115. /// <summary>
  116. /// 临时被拒绝
  117. /// </summary>
  118. /// <returns></returns>
  119. [Route("TempDeny"), ResponseCache(Duration = 360000)]
  120. public ActionResult TempDeny()
  121. {
  122. Response.StatusCode = 403;
  123. return View();
  124. }
  125. /// <summary>
  126. /// 网站升级中
  127. /// </summary>
  128. /// <returns></returns>
  129. [Route("ComingSoon"), ResponseCache(Duration = 360000)]
  130. public ActionResult ComingSoon()
  131. {
  132. return View();
  133. }
  134. /// <summary>
  135. /// 检查访问密码
  136. /// </summary>
  137. /// <param name="email"></param>
  138. /// <param name="token"></param>
  139. /// <returns></returns>
  140. [HttpPost, ValidateAntiForgeryToken, AllowAccessFirewall, ResponseCache(Duration = 115, VaryByQueryKeys = new[] { "email", "token" })]
  141. public ActionResult CheckViewToken(string email, string token)
  142. {
  143. if (string.IsNullOrEmpty(token))
  144. {
  145. return ResultData(null, false, "请输入访问密码!");
  146. }
  147. var s = RedisHelper.Get("token:" + email);
  148. if (!token.Equals(s))
  149. {
  150. return ResultData(null, false, "访问密码不正确!");
  151. }
  152. Response.Cookies.Append("Email", email, new CookieOptions
  153. {
  154. Expires = DateTime.Now.AddYears(1)
  155. });
  156. Response.Cookies.Append("FullAccessToken", email.MDString3(AppConfig.BaiduAK), new CookieOptions
  157. {
  158. Expires = DateTime.Now.AddYears(1)
  159. });
  160. return ResultData(null);
  161. }
  162. /// <summary>
  163. /// 检查授权邮箱
  164. /// </summary>
  165. /// <param name="email"></param>
  166. /// <returns></returns>
  167. [HttpPost, ValidateAntiForgeryToken, AllowAccessFirewall, ResponseCache(Duration = 115, VaryByQueryKeys = new[] { "email" })]
  168. public ActionResult GetViewToken(string email)
  169. {
  170. if (string.IsNullOrEmpty(email) || !email.MatchEmail())
  171. {
  172. return ResultData(null, false, "请输入正确的邮箱!");
  173. }
  174. if (RedisHelper.Exists("get:" + email))
  175. {
  176. RedisHelper.Expire("get:" + email, 120);
  177. return ResultData(null, false, "发送频率限制,请在2分钟后重新尝试发送邮件!请检查你的邮件,若未收到,请检查你的邮箱地址或邮件垃圾箱!");
  178. }
  179. if (!BroadcastService.Any(b => b.Email.Equals(email) && b.SubscribeType == SubscribeType.ArticleToken))
  180. {
  181. return ResultData(null, false, "您目前没有权限访问这个链接,请联系站长开通访问权限!");
  182. }
  183. var token = SnowFlake.GetInstance().GetUniqueShortId(6);
  184. RedisHelper.Set("token:" + email, token, 86400);
  185. BackgroundJob.Enqueue(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Domain"] + "博客访问验证码", $"{CommonHelper.SystemSettings["Domain"]}本次验证码是:<span style='color:red'>{token}</span>,有效期为24h,请按时使用!", email));
  186. RedisHelper.Set("get:" + email, token, 120);
  187. return ResultData(null);
  188. }
  189. /// <summary>
  190. /// 响应数据
  191. /// </summary>
  192. /// <param name="data">数据</param>
  193. /// <param name="success">响应状态</param>
  194. /// <param name="message">响应消息</param>
  195. /// <returns></returns>
  196. public ActionResult ResultData(object data, bool success = true, string message = "")
  197. {
  198. return Ok(new
  199. {
  200. Success = success,
  201. Message = message,
  202. Data = data
  203. });
  204. }
  205. }
  206. }