懒得勤快 4 years ago
parent
commit
ad7b4314e9

+ 19 - 6
src/Masuit.MyBlogs.Core/Common/CommonHelper.cs

@@ -18,6 +18,7 @@ using System.Drawing;
 using System.IO;
 using System.Linq;
 using System.Net;
+using System.Net.Http;
 using System.Net.Sockets;
 using System.Text;
 using System.Threading.Tasks;
@@ -138,6 +139,23 @@ namespace Masuit.MyBlogs.Core.Common
         public static readonly DatabaseReader MaxmindReader = new(Path.Combine(AppContext.BaseDirectory + "App_Data", "GeoLite2-City.mmdb"));
         private static readonly DatabaseReader MaxmindAsnReader = new(Path.Combine(AppContext.BaseDirectory + "App_Data", "GeoLite2-ASN.mmdb"));
 
+        /// <summary>
+        /// 是否是代理ip
+        /// </summary>
+        /// <param name="ip"></param>
+        /// <returns></returns>
+        public static async Task<bool> IsProxy(this string ip)
+        {
+            var httpClient = Startup.ServiceProvider.GetRequiredService<IHttpClientFactory>().CreateClient();
+            httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36 Edg/92.0.902.62");
+            var html = await httpClient.GetStringAsync("https://ipinfo.io/" + ip);
+            var ctx = BrowsingContext.New(Configuration.Default);
+            var doc = await ctx.OpenAsync(res => res.Content(html));
+            var isAnycast = doc.DocumentElement.QuerySelectorAll(".title").Where(e => e.TextContent.Contains("Anycast")).Select(e => e.Parent).Any(n => n.TextContent.Contains("True"));
+            var isproxy = doc.DocumentElement.QuerySelectorAll("#block-privacy img").Any(e => e.OuterHtml.Contains("right"));
+            return isAnycast || isproxy;
+        }
+
         public static AsnResponse GetIPAsn(this string ip)
         {
             if (ip.IsPrivateIP())
@@ -389,14 +407,9 @@ namespace Masuit.MyBlogs.Core.Common
         public string City { get; set; }
         public string ISP { get; set; }
         public long? ASN { get; set; }
-        public string Location => Country + Province + City;
+        public string Location => new[] { Country, Province, City }.Where(s => !string.IsNullOrEmpty(s)).Distinct().Join("");
         public string Network => ASN.HasValue ? ISP + "(AS" + ASN + ")" : ISP;
 
-        public string[] ToArray()
-        {
-            return new[] { Country, Province, City, ISP, ASN + "" }.Where(s => !string.IsNullOrEmpty(s) && s != "0").ToArray();
-        }
-
         public override string ToString()
         {
             string location = Location;

+ 5 - 3
src/Masuit.MyBlogs.Core/Controllers/ToolsController.cs

@@ -40,7 +40,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <param name="ip"></param>
         /// <returns></returns>
         [Route("ip"), Route("ip/{ip?}", Order = 1), ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "ip" }, VaryByHeader = "Cookie")]
-        public ActionResult GetIpInfo([IsIPAddress] string ip)
+        public async Task<ActionResult> GetIpInfo([IsIPAddress] string ip)
         {
             if (string.IsNullOrEmpty(ip))
             {
@@ -54,11 +54,13 @@ namespace Masuit.MyBlogs.Core.Controllers
 
             ViewBag.IP = ip;
             var cityInfo = Policy<CityResponse>.Handle<AddressNotFoundException>().Fallback(() => new CityResponse()).Execute(() => CommonHelper.MaxmindReader.City(ip));
+            var location = ip.GetIPLocation();
             var address = new IpInfo()
             {
                 CityInfo = cityInfo,
-                Address = $"{ip.GetIPLocation()}(UTC{TZConvert.GetTimeZoneInfo(cityInfo.Location.TimeZone ?? "Asia/Shanghai").BaseUtcOffset.Hours:+#;-#;0})",
-                Asn = ip.GetIPAsn()
+                Address = $"{location}(UTC{TZConvert.GetTimeZoneInfo(cityInfo.Location.TimeZone ?? "Asia/Shanghai").BaseUtcOffset.Hours:+#;-#;0})",
+                Asn = ip.GetIPAsn(),
+                IsProxy = location.Contains(new[] { "cloud", "Compute", "Serv", "Tech", "Solution", "Host", "云", "Data Services" }) || await ip.IsProxy()
             };
             if (Request.Method.Equals(HttpMethods.Get))
             {

+ 1 - 0
src/Masuit.MyBlogs.Core/Models/ViewModel/IpInfo.cs

@@ -7,5 +7,6 @@ namespace Masuit.MyBlogs.Core.Models.ViewModel
         public string Address { get; set; }
         public CityResponse CityInfo { get; set; }
         public AsnResponse Asn { get; set; }
+        public bool IsProxy { get; set; }
     }
 }

+ 1 - 6
src/Masuit.MyBlogs.Core/Views/Post/Details_Admin.cshtml

@@ -131,12 +131,7 @@
                         </header>
                         <article class="article" id="article">
                             @Html.Raw(Model.Content)
-                            @if (!string.IsNullOrEmpty(Model.ProtectContent))
-                            {
-                                <div class="row protected">
-                                    @Html.Raw(Model.ProtectContent)
-                                </div>
-                            }
+                            @Html.Raw(Model.ProtectContent)
                         </article>
                     </section>
                 </main>

+ 4 - 0
src/Masuit.MyBlogs.Core/Views/Tools/GetIPInfo.cshtml

@@ -52,6 +52,10 @@
             <td>经纬度:</td>
             <td>(经度:@(Model.CityInfo.Location.Longitude),纬度:@(Model.CityInfo.Location.Latitude))</td>
         </tr>
+        <tr>
+            <td>代理或任播节点:</td>
+            <td>@Model.IsProxy</td>
+        </tr>
     </table>
 </div>
 <div class="container-fluid">

+ 8 - 3
src/Masuit.MyBlogs.Core/wwwroot/ng-views/controllers/main.js

@@ -271,7 +271,12 @@
             console.log("Oops, error");
         });
         if (($scope.InternalMsgs||[]).length==0) {
-			fetch("/msg/GetUnreadMsgs",{
+			getUnreadMsgs(true);
+        }
+    }
+
+    function getUnreadMsgs(show) {
+        fetch("/msg/GetUnreadMsgs",{
                 headers:{
                     'Accept': 'application/json',
                     'Content-Type': 'application/json'
@@ -292,15 +297,15 @@
             }).catch(function(e) {
                 console.log("Oops, error");
             });
-        }
     }
 	getmsgs(true);
 	setInterval(getmsgs,5000);
 	$scope.read = function(id) {
 		$http.post("/msg/read", {
 			id
-		});
+		}).then(getUnreadMsgs);
 	}
+
 	$scope.changeUsername = function() {
 		swal({
 			title:'请输入新的用户名',

+ 1 - 1
src/Masuit.MyBlogs.Core/wwwroot/ng-views/views/system/firewall.html

@@ -141,7 +141,7 @@
             <tr ng-repeat="item in ranking">
                 <td>{{$index+1}}</td>
                 <td>
-                    <a ng-href="http://ip.bczs.net/{{item.Key}}" target="_blank">{{item.Key}}</a>
+                    <a ng-href="https://ipinfo.io/{{item.Key}}" target="_blank">{{item.Key}}</a>
                     <button class="badge btn btn-primary pull-right" ng-click="addToBlackList(item.Key)">添加到黑名单</button>
                 </td>
                 <td>{{item.Count}}</td>