SubscribeController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using Masuit.MyBlogs.Core.Common;
  2. using Masuit.MyBlogs.Core.Extensions;
  3. using Masuit.MyBlogs.Core.Extensions.Firewall;
  4. using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
  5. using Masuit.MyBlogs.Core.Models.Entity;
  6. using Masuit.MyBlogs.Core.Models.Enum;
  7. using Masuit.MyBlogs.Core.Models.ViewModel;
  8. using Masuit.Tools;
  9. using Masuit.Tools.AspNetCore.Mime;
  10. using Masuit.Tools.Core.Net;
  11. using Masuit.Tools.Linq;
  12. using Masuit.Tools.Models;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Microsoft.Net.Http.Headers;
  15. using System.Linq.Expressions;
  16. using System.Text;
  17. using System.Text.RegularExpressions;
  18. using EFCoreSecondLevelCacheInterceptor;
  19. using Microsoft.EntityFrameworkCore;
  20. using WilderMinds.RssSyndication;
  21. using Collections.Pooled;
  22. namespace Masuit.MyBlogs.Core.Controllers
  23. {
  24. /// <summary>
  25. /// 订阅服务
  26. /// </summary>
  27. public class SubscribeController : Controller
  28. {
  29. public IPostService PostService { get; set; }
  30. public IAdvertisementService AdvertisementService { get; set; }
  31. /// <summary>
  32. /// RSS订阅
  33. /// </summary>
  34. /// <returns></returns>
  35. [Route("/rss"), ResponseCache(Duration = 3600)]
  36. public async Task<IActionResult> Rss()
  37. {
  38. if (CommonHelper.SystemSettings.GetOrAdd("EnableRss", "true") != "true")
  39. {
  40. throw new NotFoundException("不允许订阅");
  41. }
  42. var time = DateTime.Today.AddDays(-1);
  43. string scheme = Request.Scheme;
  44. var host = Request.Host;
  45. using var raw = PostService.GetQuery(PostBaseWhere().And(p => p.Rss && p.ModifyDate >= time), p => p.ModifyDate, false).Include(p => p.Category).AsNoTracking().Cacheable().ToPooledList();
  46. var data = await raw.SelectAsync(async p =>
  47. {
  48. var summary = await p.Content.GetSummary(300, 50);
  49. return new Item()
  50. {
  51. Author = new Author
  52. {
  53. Name = p.Modifier
  54. },
  55. Body = summary,
  56. Categories = new List<string>
  57. {
  58. p.Category.Name
  59. },
  60. Link = new Uri(scheme + "://" + host + "/" + p.Id),
  61. PublishDate = p.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone)),
  62. Title = p.Title,
  63. Permalink = scheme + "://" + host + "/" + p.Id,
  64. Guid = p.Id.ToString(),
  65. FullHtmlContent = summary
  66. };
  67. });
  68. var posts = data.ToList();
  69. InsertAdvertisement(posts);
  70. var feed = new Feed()
  71. {
  72. Title = CommonHelper.SystemSettings["Title"],
  73. Description = CommonHelper.SystemSettings["Description"],
  74. Link = new Uri(scheme + "://" + host + "/rss"),
  75. Copyright = CommonHelper.SystemSettings["Title"],
  76. Language = "zh-cn",
  77. Items = posts
  78. };
  79. var rss = feed.Serialize(new SerializeOption()
  80. {
  81. Encoding = Encoding.UTF8
  82. });
  83. return Content(rss, ContentType.Xml);
  84. }
  85. private void InsertAdvertisement(IList<Item> posts, int? cid = null, string keywords = "")
  86. {
  87. if (posts.Count > 2)
  88. {
  89. var ad = AdvertisementService.GetByWeightedPrice((AdvertiseType)(DateTime.Now.Second % 4 + 1), Request.Location(), cid, keywords);
  90. if (ad is not null)
  91. {
  92. posts.Insert(new Random().Next(1, posts.Count), new Item()
  93. {
  94. Author = new Author()
  95. {
  96. Name = ad.Title
  97. },
  98. Body = ad.Description,
  99. Title = ad.Title,
  100. FullHtmlContent = ad.Description,
  101. Guid = ad.IndexId,
  102. PublishDate = DateTime.UtcNow,
  103. Link = new Uri(Url.ActionLink("Redirect", "Advertisement", new { id = ad.Id })),
  104. Permalink = Url.ActionLink("Redirect", "Advertisement", new { id = ad.Id })
  105. });
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// RSS分类订阅
  111. /// </summary>
  112. /// <returns></returns>
  113. [Route("/cat/{id}/rss"), ResponseCache(Duration = 3600)]
  114. public async Task<IActionResult> CategoryRss([FromServices] ICategoryService categoryService, int id)
  115. {
  116. if (CommonHelper.SystemSettings.GetOrAdd("EnableRss", "true") != "true")
  117. {
  118. throw new NotFoundException("不允许订阅");
  119. }
  120. var time = DateTime.Today.AddDays(-1);
  121. string scheme = Request.Scheme;
  122. var host = Request.Host;
  123. var category = await categoryService.GetByIdAsync(id) ?? throw new NotFoundException("分类未找到");
  124. var cids = category.Flatten().Select(c => c.Id).ToArray();
  125. using var raw = PostService.GetQuery(PostBaseWhere().And(p => p.Rss && cids.Contains(p.CategoryId) && p.ModifyDate >= time), p => p.ModifyDate, false).Include(p => p.Category).AsNoTracking().Cacheable().ToPooledList();
  126. var data = await raw.SelectAsync(async p =>
  127. {
  128. var summary = await p.Content.GetSummary(300, 50);
  129. return new Item()
  130. {
  131. Author = new Author
  132. {
  133. Name = p.Modifier
  134. },
  135. Body = summary,
  136. Categories = new List<string>
  137. {
  138. p.Category.Name
  139. },
  140. Link = new Uri(scheme + "://" + host + "/" + p.Id),
  141. PublishDate = p.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone)),
  142. Title = p.Title,
  143. Permalink = scheme + "://" + host + "/" + p.Id,
  144. Guid = p.Id.ToString(),
  145. FullHtmlContent = summary
  146. };
  147. });
  148. var posts = data.ToList();
  149. InsertAdvertisement(posts, id, category.Name);
  150. var feed = new Feed()
  151. {
  152. Title = Request.Host + $":分类{category.Name}文章订阅",
  153. Description = category.Description,
  154. Link = new Uri(scheme + "://" + host + "/rss"),
  155. Copyright = CommonHelper.SystemSettings["Title"],
  156. Language = "zh-cn",
  157. Items = posts
  158. };
  159. var rss = feed.Serialize(new SerializeOption()
  160. {
  161. Encoding = Encoding.UTF8
  162. });
  163. return Content(rss, ContentType.Xml);
  164. }
  165. /// <summary>
  166. /// RSS专题订阅
  167. /// </summary>
  168. /// <returns></returns>
  169. [Route("/special/{id}/rss"), ResponseCache(Duration = 3600)]
  170. public async Task<IActionResult> SeminarRss([FromServices] ISeminarService seminarService, int id)
  171. {
  172. if (CommonHelper.SystemSettings.GetOrAdd("EnableRss", "true") != "true")
  173. {
  174. throw new NotFoundException("不允许订阅");
  175. }
  176. var time = DateTime.Today.AddDays(-1);
  177. string scheme = Request.Scheme;
  178. var host = Request.Host;
  179. var seminar = await seminarService.GetByIdAsync(id) ?? throw new NotFoundException("专题未找到");
  180. using var raw = PostService.GetQuery(PostBaseWhere().And(p => p.Rss && p.Seminar.Any(s => s.Id == id) && p.ModifyDate >= time), p => p.ModifyDate, false).Include(p => p.Category).AsNoTracking().Cacheable().ToPooledList();
  181. var data = await raw.SelectAsync(async p =>
  182. {
  183. var summary = await p.Content.GetSummary(300, 50);
  184. return new Item()
  185. {
  186. Author = new Author
  187. {
  188. Name = p.Modifier
  189. },
  190. Body = summary,
  191. Categories = new List<string>
  192. {
  193. p.Category.Name
  194. },
  195. Link = new Uri(scheme + "://" + host + "/" + p.Id),
  196. PublishDate = p.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone)),
  197. Title = p.Title,
  198. Permalink = scheme + "://" + host + "/" + p.Id,
  199. Guid = p.Id.ToString(),
  200. FullHtmlContent = summary
  201. };
  202. });
  203. var posts = data.ToList();
  204. InsertAdvertisement(posts, id, seminar.Title);
  205. var feed = new Feed()
  206. {
  207. Title = Request.Host + $":专题{seminar.Title}文章订阅",
  208. Description = seminar.Description,
  209. Link = new Uri(scheme + "://" + host + "/rss"),
  210. Copyright = CommonHelper.SystemSettings["Title"],
  211. Language = "zh-cn",
  212. Items = posts
  213. };
  214. var rss = feed.Serialize(new SerializeOption()
  215. {
  216. Encoding = Encoding.UTF8
  217. });
  218. return Content(rss, ContentType.Xml);
  219. }
  220. /// <summary>
  221. /// RSS文章订阅
  222. /// </summary>
  223. /// <returns></returns>
  224. [Route("/{id}/rss"), ResponseCache(Duration = 3600)]
  225. public async Task<IActionResult> PostRss(int id)
  226. {
  227. if (CommonHelper.SystemSettings.GetOrAdd("EnableRss", "true") != "true")
  228. {
  229. throw new NotFoundException("不允许订阅");
  230. }
  231. string scheme = Request.Scheme;
  232. var host = Request.Host;
  233. var post = await PostService.GetAsync(p => p.Rss && p.Status == Status.Published && p.Id == id) ?? throw new NotFoundException("文章未找到");
  234. CheckPermission(post);
  235. var summary = await post.Content.GetSummary(300, 50);
  236. var item = new Item()
  237. {
  238. Author = new Author
  239. {
  240. Name = post.Modifier
  241. },
  242. Body = summary,
  243. Categories = new List<string>
  244. {
  245. post.Category.Path()
  246. },
  247. Link = new Uri(scheme + "://" + host + "/" + post.Id),
  248. PublishDate = post.ModifyDate.ToTimeZone(HttpContext.Session.Get<string>(SessionKey.TimeZone)),
  249. Title = post.Title,
  250. Permalink = scheme + "://" + host + "/" + post.Id,
  251. Guid = post.Id.ToString(),
  252. FullHtmlContent = summary
  253. };
  254. var feed = new Feed()
  255. {
  256. Title = Request.Host + $":文章【{post.Title}】更新订阅",
  257. Description = summary,
  258. Link = new Uri(scheme + "://" + host + "/rss/" + id),
  259. Copyright = CommonHelper.SystemSettings["Title"],
  260. Language = "zh-cn",
  261. Items = new List<Item>() { item }
  262. };
  263. var rss = feed.Serialize(new SerializeOption()
  264. {
  265. Encoding = Encoding.UTF8
  266. });
  267. return Content(rss, ContentType.Xml);
  268. }
  269. protected Expression<Func<Post, bool>> PostBaseWhere()
  270. {
  271. var location = Request.Location() + "|" + Request.Headers[HeaderNames.Referer] + "|" + Request.Headers[HeaderNames.UserAgent];
  272. return p => p.Status == Status.Published && p.LimitMode != RegionLimitMode.OnlyForSearchEngine
  273. && (p.LimitMode == null || p.LimitMode == RegionLimitMode.All ? true :
  274. p.LimitMode == RegionLimitMode.AllowRegion ? Regex.IsMatch(location, p.Regions, RegexOptions.IgnoreCase) :
  275. p.LimitMode == RegionLimitMode.ForbidRegion ? !Regex.IsMatch(location, p.Regions, RegexOptions.IgnoreCase) :
  276. p.LimitMode == RegionLimitMode.AllowRegionExceptForbidRegion ? Regex.IsMatch(location, p.Regions, RegexOptions.IgnoreCase) && !Regex.IsMatch(location, p.ExceptRegions, RegexOptions.IgnoreCase) :
  277. !Regex.IsMatch(location, p.Regions, RegexOptions.IgnoreCase) || Regex.IsMatch(location, p.ExceptRegions, RegexOptions.IgnoreCase));
  278. }
  279. private void CheckPermission(Post post)
  280. {
  281. var location = Request.Location() + "|" + Request.Headers[HeaderNames.Referer] + "|" + Request.Headers[HeaderNames.UserAgent];
  282. switch (post.LimitMode)
  283. {
  284. case RegionLimitMode.OnlyForSearchEngine:
  285. Disallow(post);
  286. break;
  287. case RegionLimitMode.AllowRegion:
  288. if (!Regex.IsMatch(location, post.Regions, RegexOptions.IgnoreCase) && !Request.IsRobot())
  289. {
  290. Disallow(post);
  291. }
  292. break;
  293. case RegionLimitMode.ForbidRegion:
  294. if (Regex.IsMatch(location, post.Regions, RegexOptions.IgnoreCase) && !Request.IsRobot())
  295. {
  296. Disallow(post);
  297. }
  298. break;
  299. case RegionLimitMode.AllowRegionExceptForbidRegion:
  300. if (Regex.IsMatch(location, post.ExceptRegions, RegexOptions.IgnoreCase))
  301. {
  302. Disallow(post);
  303. }
  304. goto case RegionLimitMode.AllowRegion;
  305. case RegionLimitMode.ForbidRegionExceptAllowRegion:
  306. if (Regex.IsMatch(location, post.ExceptRegions, RegexOptions.IgnoreCase))
  307. {
  308. break;
  309. }
  310. goto case RegionLimitMode.ForbidRegion;
  311. }
  312. }
  313. private void Disallow(Post post)
  314. {
  315. RedisHelper.IncrBy("interceptCount");
  316. RedisHelper.LPush("intercept", new IpIntercepter()
  317. {
  318. IP = HttpContext.Connection.RemoteIpAddress.ToString(),
  319. RequestUrl = $"//{Request.Host}/{post.Id}",
  320. Referer = Request.Headers[HeaderNames.Referer],
  321. Time = DateTime.Now,
  322. UserAgent = Request.Headers[HeaderNames.UserAgent],
  323. Remark = "无权限查看该文章",
  324. Address = Request.Location(),
  325. HttpVersion = Request.Protocol,
  326. Headers = new
  327. {
  328. Request.Protocol,
  329. Request.Headers
  330. }.ToJsonString()
  331. });
  332. throw new NotFoundException("文章未找到");
  333. }
  334. }
  335. }