1
0
Эх сурвалжийг харах

修正获取ip地址的bug

懒得勤快 4 жил өмнө
parent
commit
dc71639994

+ 5 - 6
src/Masuit.MyBlogs.Core/Controllers/ErrorController.cs

@@ -64,26 +64,25 @@ namespace Masuit.MyBlogs.Core.Controllers
             if (feature != null)
             if (feature != null)
             {
             {
                 string err;
                 string err;
-                var req = HttpContext.Request;
                 var ip = HttpContext.Connection.RemoteIpAddress;
                 var ip = HttpContext.Connection.RemoteIpAddress;
                 switch (feature.Error)
                 switch (feature.Error)
                 {
                 {
                     case DbUpdateConcurrencyException ex:
                     case DbUpdateConcurrencyException ex:
-                        err = $"数据库并发更新异常,更新表:{ex.Entries.Select(e => e.Metadata.Name)},请求路径:{req.Scheme}://{req.Host}{HttpUtility.UrlDecode(feature.Path)}{req.QueryString},客户端用户代理:{req.Headers[HeaderNames.UserAgent]},客户端IP:{ip}\t{ex.InnerException?.Message},请求参数:\n{await GetRequestBody(req)}\n堆栈信息:";
+                        err = $"数据库并发更新异常,更新表:{ex.Entries.Select(e => e.Metadata.Name)},请求路径({Request.Method}):{Request.Scheme}://{Request.Host}{HttpUtility.UrlDecode(feature.Path)}{Request.QueryString},客户端用户代理:{Request.Headers[HeaderNames.UserAgent]},客户端IP:{ip}\t{ex.InnerException?.Message},请求参数:\n{await GetRequestBody(Request)}\n堆栈信息:";
                         LogManager.Error(err, ex);
                         LogManager.Error(err, ex);
                         break;
                         break;
                     case DbUpdateException ex:
                     case DbUpdateException ex:
-                        err = $"数据库更新时异常,更新表:{ex.Entries.Select(e => e.Metadata.Name)},请求路径:{req.Scheme}://{req.Host}{HttpUtility.UrlDecode(feature.Path)}{req.QueryString} ,客户端用户代理:{req.Headers[HeaderNames.UserAgent]},客户端IP:{ip}\t{ex.InnerException?.Message},请求参数:\n{await GetRequestBody(req)}\n堆栈信息:";
+                        err = $"数据库更新时异常,更新表:{ex.Entries.Select(e => e.Metadata.Name)},请求路径({Request.Method}):{Request.Scheme}://{Request.Host}{HttpUtility.UrlDecode(feature.Path)}{Request.QueryString} ,客户端用户代理:{Request.Headers[HeaderNames.UserAgent]},客户端IP:{ip}\t{ex.InnerException?.Message},请求参数:\n{await GetRequestBody(Request)}\n堆栈信息:";
                         LogManager.Error(err, ex);
                         LogManager.Error(err, ex);
                         break;
                         break;
                     case AggregateException ex:
                     case AggregateException ex:
                         LogManager.Debug("↓↓↓" + ex.Message + "↓↓↓");
                         LogManager.Debug("↓↓↓" + ex.Message + "↓↓↓");
                         ex.Flatten().Handle(e =>
                         ex.Flatten().Handle(e =>
                         {
                         {
-                            LogManager.Error($"异常源:{e.Source},异常类型:{e.GetType().Name},请求路径:{req.Scheme}://{req.Host}{HttpUtility.UrlDecode(feature.Path)}{req.QueryString} ,客户端用户代理:{req.Headers[HeaderNames.UserAgent]},客户端IP:{ip}\t", e);
+                            LogManager.Error($"异常源:{e.Source},异常类型:{e.GetType().Name},请求路径({Request.Method}):{Request.Scheme}://{Request.Host}{HttpUtility.UrlDecode(feature.Path)}{Request.QueryString} ,客户端用户代理:{Request.Headers[HeaderNames.UserAgent]},客户端IP:{ip}\t", e);
                             return true;
                             return true;
                         });
                         });
-                        var body = await GetRequestBody(req);
+                        var body = await GetRequestBody(Request);
                         if (!string.IsNullOrEmpty(body))
                         if (!string.IsNullOrEmpty(body))
                         {
                         {
                             LogManager.Debug("↑↑↑请求参数:\n" + body);
                             LogManager.Debug("↑↑↑请求参数:\n" + body);
@@ -104,7 +103,7 @@ namespace Masuit.MyBlogs.Core.Controllers
                             Message = $"检测到您的IP({ip})访问过于频繁,已被本站暂时禁止访问,请稍后再试!"
                             Message = $"检测到您的IP({ip})访问过于频繁,已被本站暂时禁止访问,请稍后再试!"
                         }) : View("TempDeny");
                         }) : View("TempDeny");
                     default:
                     default:
-                        LogManager.Error($"异常源:{feature.Error.Source},异常类型:{feature.Error.GetType().Name},请求路径:{req.Scheme}://{req.Host}{HttpUtility.UrlDecode(feature.Path)}{req.QueryString} ,客户端用户代理:{req.Headers[HeaderNames.UserAgent]},客户端IP:{ip},请求参数:\n{await GetRequestBody(req)}\n堆栈信息:", feature.Error);
+                        LogManager.Error($"异常源:{feature.Error.Source},异常类型:{feature.Error.GetType().Name},请求路径({Request.Method}):{Request.Scheme}://{Request.Host}{HttpUtility.UrlDecode(feature.Path)}{Request.QueryString} ,客户端用户代理:{Request.Headers[HeaderNames.UserAgent]},客户端IP:{ip},请求参数:\n{await GetRequestBody(Request)}\n堆栈信息:", feature.Error);
                         break;
                         break;
                 }
                 }
             }
             }

+ 8 - 2
src/Masuit.MyBlogs.Core/Controllers/ToolsController.cs

@@ -57,11 +57,17 @@ namespace Masuit.MyBlogs.Core.Controllers
             ViewBag.IP = ip;
             ViewBag.IP = ip;
             var cityInfo = Policy<CityResponse>.Handle<AddressNotFoundException>().Fallback(() => new CityResponse()).Execute(() => CommonHelper.MaxmindReader.City(ipAddress));
             var cityInfo = Policy<CityResponse>.Handle<AddressNotFoundException>().Fallback(() => new CityResponse()).Execute(() => CommonHelper.MaxmindReader.City(ipAddress));
             var loc = ipAddress.GetIPLocation();
             var loc = ipAddress.GetIPLocation();
+            var asn = ipAddress.GetIPAsn();
             var address = new IpInfo()
             var address = new IpInfo()
             {
             {
-                CityInfo = cityInfo,
+                Location = cityInfo.Location,
                 Address = loc.Location,
                 Address = loc.Location,
-                Asn = ipAddress.GetIPAsn(),
+                Network = new NetworkInfo()
+                {
+                    Asn = asn.AutonomousSystemNumber,
+                    Router = asn.Network + "",
+                    Organization = asn.AutonomousSystemOrganization
+                },
                 TimeZone = $"UTC{TZConvert.GetTimeZoneInfo(cityInfo.Location.TimeZone ?? "Asia/Shanghai").BaseUtcOffset.Hours:+#;-#;0}",
                 TimeZone = $"UTC{TZConvert.GetTimeZoneInfo(cityInfo.Location.TimeZone ?? "Asia/Shanghai").BaseUtcOffset.Hours:+#;-#;0}",
                 IsProxy = loc.Network.Contains(new[] { "cloud", "Compute", "Serv", "Tech", "Solution", "Host", "云", "Datacenter", "Data Center", "Business" }) || await ipAddress.IsProxy()
                 IsProxy = loc.Network.Contains(new[] { "cloud", "Compute", "Serv", "Tech", "Solution", "Host", "云", "Datacenter", "Data Center", "Business" }) || await ipAddress.IsProxy()
             };
             };

+ 10 - 3
src/Masuit.MyBlogs.Core/Models/ViewModel/IpInfo.cs

@@ -1,13 +1,20 @@
-using MaxMind.GeoIP2.Responses;
+using MaxMind.GeoIP2.Model;
 
 
 namespace Masuit.MyBlogs.Core.Models.ViewModel
 namespace Masuit.MyBlogs.Core.Models.ViewModel
 {
 {
     public class IpInfo
     public class IpInfo
     {
     {
         public string Address { get; set; }
         public string Address { get; set; }
-        public CityResponse CityInfo { get; set; }
-        public AsnResponse Asn { get; set; }
+        public Location Location { get; set; }
+        public NetworkInfo Network { get; set; }
         public bool IsProxy { get; set; }
         public bool IsProxy { get; set; }
         public string TimeZone { get; set; }
         public string TimeZone { get; set; }
     }
     }
+
+    public class NetworkInfo
+    {
+        public string Router { get; set; }
+        public long? Asn { get; set; }
+        public string Organization { get; set; }
+    }
 }
 }

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

@@ -34,15 +34,15 @@
     <table class="table table-bordered table-condensed table-hover">
     <table class="table table-bordered table-condensed table-hover">
         <tr>
         <tr>
             <td>IP路由信息:</td>
             <td>IP路由信息:</td>
-            <td>@Model.Asn.Network</td>
+            <td>@Model.Network.Router</td>
         </tr>
         </tr>
         <tr>
         <tr>
             <td>IP机房信息:</td>
             <td>IP机房信息:</td>
-            <td>@Model.Asn.AutonomousSystemOrganization</td>
+            <td>@Model.Network.Organization</td>
         </tr>
         </tr>
         <tr>
         <tr>
             <td>ASN:</td>
             <td>ASN:</td>
-            <td>@Model.Asn.AutonomousSystemNumber</td>
+            <td>@Model.Network.Asn</td>
         </tr>
         </tr>
         <tr>
         <tr>
             <td>参考地理位置:</td>
             <td>参考地理位置:</td>
@@ -54,7 +54,7 @@
         </tr>
         </tr>
         <tr>
         <tr>
             <td>经纬度:</td>
             <td>经纬度:</td>
-            <td>(经度:@(Model.CityInfo.Location.Longitude),纬度:@(Model.CityInfo.Location.Latitude))</td>
+            <td>(经度:@(Model.Location.Longitude),纬度:@(Model.Location.Latitude))</td>
         </tr>
         </tr>
         <tr>
         <tr>
             <td>代理或任播节点:</td>
             <td>代理或任播节点:</td>
@@ -63,17 +63,17 @@
     </table>
     </table>
 </div>
 </div>
 <div class="container-fluid">
 <div class="container-fluid">
-    @if (Model.CityInfo != null)
+    @if (Model.Location != null)
     {
     {
         <div id="allmap"></div>
         <div id="allmap"></div>
         <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=89772e94509a9b903724e247cbc175c2"></script>
         <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=89772e94509a9b903724e247cbc175c2"></script>
         <script>
         <script>
 	    var map = new BMap.Map("allmap"); // 创建Map实例,设置地图允许的最小/大级别
 	    var map = new BMap.Map("allmap"); // 创建Map实例,设置地图允许的最小/大级别
 
 
-	    map.centerAndZoom(new BMap.Point(@(Model.CityInfo.Location.Longitude), @(Model.CityInfo.Location.Latitude)), 16);
+	    map.centerAndZoom(new BMap.Point(@(Model.Location.Longitude), @(Model.Location.Latitude)), 16);
 	    map.enableScrollWheelZoom(true);
 	    map.enableScrollWheelZoom(true);
 	    map.clearOverlays();
 	    map.clearOverlays();
-	    var new_point = new BMap.Point(@(Model.CityInfo.Location.Longitude), @(Model.CityInfo.Location.Latitude));
+	    var new_point = new BMap.Point(@(Model.Location.Longitude), @(Model.Location.Latitude));
 	    var marker = new BMap.Marker(new_point); // 创建标注
 	    var marker = new BMap.Marker(new_point); // 创建标注
 	    map.addOverlay(marker); // 将标注添加到地图中
 	    map.addOverlay(marker); // 将标注添加到地图中
 	    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
 	    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画