From 666fdc46853f7ea55d71413dbff172fcfdf2d016 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Thu, 7 Jun 2018 18:13:30 +0800 Subject: [PATCH] Fixed Incorrect local IP address judgment of IPv6. (#140) --- src/DotNetCore.CAP/Dashboard/DashboardRequest.cs | 11 +++-------- .../Dashboard/LocalRequestsOnlyAuthorizationFilter.cs | 9 +++++---- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/DotNetCore.CAP/Dashboard/DashboardRequest.cs b/src/DotNetCore.CAP/Dashboard/DashboardRequest.cs index 5fdb054..23f46b8 100644 --- a/src/DotNetCore.CAP/Dashboard/DashboardRequest.cs +++ b/src/DotNetCore.CAP/Dashboard/DashboardRequest.cs @@ -28,19 +28,14 @@ namespace DotNetCore.CAP.Dashboard public CapDashboardRequest(HttpContext context) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - _context = context; + _context = context ?? throw new ArgumentNullException(nameof(context)); } public override string Method => _context.Request.Method; public override string Path => _context.Request.Path.Value; public override string PathBase => _context.Request.PathBase.Value; - public override string LocalIpAddress => _context.Connection.LocalIpAddress.ToString(); - public override string RemoteIpAddress => _context.Connection.RemoteIpAddress.ToString(); + public override string LocalIpAddress => _context.Connection.LocalIpAddress.MapToIPv4().ToString(); + public override string RemoteIpAddress => _context.Connection.RemoteIpAddress.MapToIPv4().ToString(); public override string GetQuery(string key) { diff --git a/src/DotNetCore.CAP/Dashboard/LocalRequestsOnlyAuthorizationFilter.cs b/src/DotNetCore.CAP/Dashboard/LocalRequestsOnlyAuthorizationFilter.cs index 42746d7..dafd845 100644 --- a/src/DotNetCore.CAP/Dashboard/LocalRequestsOnlyAuthorizationFilter.cs +++ b/src/DotNetCore.CAP/Dashboard/LocalRequestsOnlyAuthorizationFilter.cs @@ -9,26 +9,27 @@ namespace DotNetCore.CAP.Dashboard { public bool Authorize(DashboardContext context) { + var ipAddress = context.Request.RemoteIpAddress; // if unknown, assume not local - if (string.IsNullOrEmpty(context.Request.RemoteIpAddress)) + if (string.IsNullOrEmpty(ipAddress)) { return false; } // check if localhost - if (context.Request.RemoteIpAddress == "127.0.0.1" || context.Request.RemoteIpAddress == "::1") + if (ipAddress == "127.0.0.1" || ipAddress == "0.0.0.1") { return true; } // compare with local address - if (context.Request.RemoteIpAddress == context.Request.LocalIpAddress) + if (ipAddress == context.Request.LocalIpAddress) { return true; } // check if private ip - if (Helper.IsInnerIP(context.Request.RemoteIpAddress)) + if (Helper.IsInnerIP(ipAddress)) { return true; }