Browse Source

获取IP信息增强

懒得勤快 5 years ago
parent
commit
e61292257b

+ 20 - 12
src/Masuit.MyBlogs.Core/Common/CommonHelper.cs

@@ -144,21 +144,29 @@ namespace Masuit.MyBlogs.Core.Common
         {
             return ips.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(s =>
             {
-                var ip = IPAddress.Parse(s.Trim());
-                switch (ip.AddressFamily)
-                {
-                    case AddressFamily.InterNetwork when ip.IsPrivateIP():
-                    case AddressFamily.InterNetworkV6 when ip.IsPrivateIP():
-                        return "内网IP";
-                    case AddressFamily.InterNetwork:
-                        return IPSearcher.MemorySearch(ip.ToString())?.Region.Replace("|0", string.Empty).Split("|").ToHashSet().Join("|");
-                    default:
-                        var response = MaxmindReader.City(ip);
-                        return response.Country.Names.GetValueOrDefault("zh-CN") + response.City.Names.GetValueOrDefault("zh-CN");
-                }
+                var (location, network) = GetIPLocation(IPAddress.Parse(s.Trim()));
+                return location + "|" + network;
             }).Join(" , ");
         }
 
+        public static (string location, string network) GetIPLocation(this IPAddress ip)
+        {
+            switch (ip.AddressFamily)
+            {
+                case AddressFamily.InterNetwork when ip.IsPrivateIP():
+                case AddressFamily.InterNetworkV6 when ip.IsPrivateIP():
+                    return ("内网", "内网IP");
+                case AddressFamily.InterNetwork:
+                    var parts = IPSearcher.MemorySearch(ip.ToString()).Region.Split('|');
+                    var network = parts[^1] == "0" ? "未知" : parts[^1];
+                    var location = parts[..^1].Where(s => s != "0").Distinct().Join("");
+                    return (location, network);
+                default:
+                    var response = MaxmindReader.City(ip);
+                    return (response.Country.Names.GetValueOrDefault("zh-CN") + response.City.Names.GetValueOrDefault("zh-CN"), "未知");
+            }
+        }
+
         /// <summary>
         /// 类型映射
         /// </summary>

+ 4 - 4
src/Masuit.MyBlogs.Core/Controllers/CommentController.cs

@@ -118,7 +118,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                     //新评论,只通知博主和楼主
                     foreach (var s in emails)
                     {
-                        BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客文章新评论:", content.Set("link", Url.Action("Details", "Post", new { id = comment.PostId, cid = comment.Id }, Request.Scheme) + "#comment").Render(), s));
+                        BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客文章新评论:", content.Set("link", Url.Action("Details", "Post", new { id = comment.PostId, cid = comment.Id }, Request.Scheme) + "#comment").Render(false), s));
                     }
                 }
                 else
@@ -131,7 +131,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                     string link = Url.Action("Details", "Post", new { id = comment.PostId, cid = comment.Id }, Request.Scheme) + "#comment";
                     foreach (var s in emails)
                     {
-                        BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]}文章评论回复:", content.Set("link", link).Render(), s));
+                        BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]}文章评论回复:", content.Set("link", link).Render(false), s));
                     }
                 }
 #endif
@@ -140,7 +140,7 @@ namespace Masuit.MyBlogs.Core.Controllers
 
             foreach (var s in emails)
             {
-                BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客文章新评论(待审核):", content.Set("link", Url.Action("Details", "Post", new { id = comment.PostId, cid = comment.Id }, Request.Scheme) + "#comment").Render() + "<p style='color:red;'>(待审核)</p>", s));
+                BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客文章新评论(待审核):", content.Set("link", Url.Action("Details", "Post", new { id = comment.PostId, cid = comment.Id }, Request.Scheme) + "#comment").Render(false) + "<p style='color:red;'>(待审核)</p>", s));
             }
 
             return ResultData(null, true, "评论成功,待站长审核通过以后将显示");
@@ -248,7 +248,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                 }, Request.Scheme) + "#comment";
                 foreach (var email in emails)
                 {
-                    BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]}文章评论回复:", content.Set("link", link).Render(), email));
+                    BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]}文章评论回复:", content.Set("link", link).Render(false), email));
                 }
 #endif
                 return ResultData(null, true, "审核通过!");

+ 2 - 10
src/Masuit.MyBlogs.Core/Controllers/ErrorController.cs

@@ -12,10 +12,10 @@ using Microsoft.AspNetCore.Diagnostics;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.EntityFrameworkCore;
-using Microsoft.EntityFrameworkCore.Internal;
 using Microsoft.Net.Http.Headers;
 using System;
 using System.Linq;
+using System.Net;
 using System.Web;
 using SameSiteMode = Microsoft.AspNetCore.Http.SameSiteMode;
 
@@ -91,15 +91,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                             ex.Message
                         });
                     case AccessDenyException _:
-                        var parts = ip.GetIPLocation().Split('|');
-                        var network = "未知";
-                        var location = parts.Join("");
-                        if (parts.Length > 2)
-                        {
-                            network = parts[^1];
-                            location = parts[..^1].Join("");
-                        }
-
+                        var (location, network) = IPAddress.Parse(ip).GetIPLocation();
                         var tips = new Template(CommonHelper.SystemSettings.GetOrAdd("AccessDenyTips", @"<h4>遇到了什么问题?</h4>
                 <h4>基于主观因素考虑,您所在的地区暂时不允许访问本站,如有疑问,请联系站长!或者请联系站长开通本站的访问权限!</h4>")).Set("clientip", ip).Set(nameof(location), location).Set(nameof(network), network).Render();
                         Response.StatusCode = 403;

+ 4 - 4
src/Masuit.MyBlogs.Core/Controllers/MsgController.cs

@@ -168,7 +168,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                 if (msg.ParentId == 0)
                 {
                     //新评论,只通知博主
-                    BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客新留言:", content.Set("link", Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme)).Render(), email));
+                    BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客新留言:", content.Set("link", Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme)).Render(false), email));
                 }
                 else
                 {
@@ -178,7 +178,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                     string link = Url.Action("Index", "Msg", new { cid = msg.Id }, Request.Scheme);
                     foreach (var s in emails)
                     {
-                        BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Set("link", link).Render(), s));
+                        BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Set("link", link).Render(false), s));
                     }
                 }
 #endif
@@ -188,7 +188,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             BackgroundJob.Enqueue(() => CommonHelper.SendMail(Request.Host + "|博客新留言(待审核):", content.Set("link", Url.Action("Index", "Msg", new
             {
                 cid = msg.Id
-            }, Request.Scheme)).Render() + "<p style='color:red;'>(待审核)</p>", email));
+            }, Request.Scheme)).Render(false) + "<p style='color:red;'>(待审核)</p>", email));
             return ResultData(null, true, "留言发表成功,待站长审核通过以后将显示到列表中!");
         }
 
@@ -210,7 +210,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             var link = Url.Action("Index", "Msg", new { cid = pid }, Request.Scheme);
             foreach (var s in emails)
             {
-                BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Set("link", link).Render(), s));
+                BackgroundJob.Enqueue(() => CommonHelper.SendMail($"{Request.Host}{CommonHelper.SystemSettings["Title"]} 留言回复:", content.Set("link", link).Render(false), s));
             }
 #endif
             return ResultData(null, b, b ? "审核通过!" : "审核失败!");