From 20281f62da426b1ed3f471d6b95fc62392787c2b Mon Sep 17 00:00:00 2001 From: Elivo Date: Wed, 12 Aug 2020 22:55:58 +0800 Subject: [PATCH] =?UTF-8?q?Apewer-6.0.3=EF=BC=9A=E4=BF=AE=E6=AD=A3=20GetPa?= =?UTF-8?q?rameter=20=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer.Run/Apewer.Run.csproj | 2 +- Apewer.Run/ApiClient.cs | 2 +- Apewer.Run/Methods.cs | 2 +- Apewer.Run/Web.cs | 81 ++++++++++++++++++++++++++++++++++ Apewer/Apewer.csproj | 2 +- Apewer/NetworkUtility.cs | 23 +++++----- Apewer/Web/WebUtility.cs | 84 +++++++++++++++++++++++++++--------- Apewer/_ChangeLog.md | 3 ++ 8 files changed, 162 insertions(+), 37 deletions(-) create mode 100644 Apewer.Run/Web.cs diff --git a/Apewer.Run/Apewer.Run.csproj b/Apewer.Run/Apewer.Run.csproj index 073fd37..bcd48ff 100644 --- a/Apewer.Run/Apewer.Run.csproj +++ b/Apewer.Run/Apewer.Run.csproj @@ -9,7 +9,7 @@ - + diff --git a/Apewer.Run/ApiClient.cs b/Apewer.Run/ApiClient.cs index dd2988e..d64c816 100644 --- a/Apewer.Run/ApiClient.cs +++ b/Apewer.Run/ApiClient.cs @@ -15,7 +15,7 @@ namespace Apewer.Run public ApiClient() { - var client = new Web.ApiClient(); + var client = new Apewer.Web.ApiClient(); client.ApiUrl = "http://agent.niutax.com/"; client.UserAgent = Assembly.GetExecutingAssembly().FullName; diff --git a/Apewer.Run/Methods.cs b/Apewer.Run/Methods.cs index 6f51eb0..fc39c3b 100644 --- a/Apewer.Run/Methods.cs +++ b/Apewer.Run/Methods.cs @@ -12,7 +12,7 @@ namespace Apewer.Run public Methods() { - FormatMinutes(); + // FormatMinutes(); } void FormatMinutes() diff --git a/Apewer.Run/Web.cs b/Apewer.Run/Web.cs new file mode 100644 index 0000000..f0209be --- /dev/null +++ b/Apewer.Run/Web.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Apewer.Run +{ + + public class Web + { + + public Web() + { + // GetParameter(Json.Parse("{\"status\":2,\"after\":0,\"before\":0}"), "status"); + } + + string GetParameter(Json data, params string[] names) + { + if (names == null || names.Length < 1) return null; + + var sameNames = new List(); + var lowerNames = new List(); + foreach (var name in names) + { + if (string.IsNullOrEmpty(name)) continue; + sameNames.Add(name); + + var lower = TextUtility.ToLower(name); + if (string.IsNullOrEmpty(lower)) continue; + lowerNames.Add(lower); + } + + // POST 优先。 + if (data != null && data.IsObject) + { + var properties = data.GetProperties(); + if (properties != null) + { + // Json 区分大小写,先全字匹配。 + foreach (var property in properties) + { + if (!property.IsProperty) continue; + if (sameNames.Contains(property.Name) && property.Value != null) + { + var value = property.Value; + if (value == null) continue; + var text = value.ToString(); + if (!string.IsNullOrEmpty(text)) return text; + } + } + + // 以小写模糊匹配。 + foreach (var property in properties) + { + if (!property.IsProperty) continue; + var lowerName = TextUtility.ToLower(property.Name); + if (lowerNames.Contains(lowerName) && property.Value != null) + { + var value = property.Value; + if (value == null) continue; + var text = value.ToString(); + if (!string.IsNullOrEmpty(text)) return text; + } + } + } + } + +#if NETFX + // NETFX 最后搜索 GET 参数。 + if (request.Url != null) + { + var value = GetParameter(request.Url, names); + if (!string.IsNullOrEmpty(value)) return value; + } +#endif + + return null; + } + + } + +} diff --git a/Apewer/Apewer.csproj b/Apewer/Apewer.csproj index 53dbad3..baaf37f 100644 --- a/Apewer/Apewer.csproj +++ b/Apewer/Apewer.csproj @@ -14,7 +14,7 @@ netstandard2.1;net20;net40;net461;netcoreapp3.1 Apewer Apewer Libraries - 6.0.2 + 6.0.3 diff --git a/Apewer/NetworkUtility.cs b/Apewer/NetworkUtility.cs index 2b7028b..623ddc3 100644 --- a/Apewer/NetworkUtility.cs +++ b/Apewer/NetworkUtility.cs @@ -182,24 +182,23 @@ namespace Apewer /// 判断 IPv4 地址格式是否正确。 public static bool IsIP(string ipv4) { - int r = 0; try { - if (!string.IsNullOrEmpty(ipv4)) + if (string.IsNullOrEmpty(ipv4)) return false; + + var split = ipv4.Split('.'); + if (split.Length != 4) return false; + + for (int i = 0; i < 4; i++) { - var a = ipv4.Split('.'); - if (a.Length == 4) - { - for (int i = 0; i < 4; i++) - { - int n = Convert.ToInt32(a[i]); - if ((n >= 0) && (n <= 255)) r += 1; - } - } + var n = Convert.ToInt32(split[i]); + if (n < 0 || n > 255) return false; + if (n.ToString() != split[i]) return false; } + return true; } catch { } - return (r == 4) ? true : false; + return false; } /// 对目标地址进行解析。 diff --git a/Apewer/Web/WebUtility.cs b/Apewer/Web/WebUtility.cs index a448ade..9278f21 100644 --- a/Apewer/Web/WebUtility.cs +++ b/Apewer/Web/WebUtility.cs @@ -111,7 +111,15 @@ namespace Apewer.Web #if NETFX || NETCORE - #region http + #region http + + /// 获取直连端的 IP。 + public static string GetDirectIP(ApiRequest request) + { + if (request == null) return null; + if (request.Context == null) return null; + return GetDirectIP(request.Context.Request); + } /// 获取直连端的 IP。 public static string GetDirectIP(HttpRequest request) @@ -125,6 +133,14 @@ namespace Apewer.Web #endif } + /// 获取客户端 IP。 + public static string GetClientIP(ApiRequest request, bool withProxy = false) + { + if (request == null) return null; + if (request.Context == null) return null; + return GetClientIP(request.Context.Request, withProxy); + } + /// 获取客户端 IP。 #if NETFX public static string GetClientIP(System.Web.HttpRequest request, bool withProxy = false) @@ -147,7 +163,7 @@ namespace Apewer.Web } /// 请求来自于可信客户端。 - public static bool FromTrusted(HttpRequest request, params string[] domains) + public static bool FromTrusted(HttpRequest request, params string[] addresses) { if (request == null) return false; @@ -158,17 +174,26 @@ namespace Apewer.Web if (NetworkUtility.FromLAN(direct)) return true; } - if (domains != null && domains.Length > 0) + // 遍历传入的地址。 + if (addresses != null && addresses.Length > 0) { + // 读取 HTTP 头。 var forwarded = GetForwardedIP(request); - if (!string.IsNullOrEmpty(forwarded)) + if (!NetworkUtility.IsIP(forwarded)) return false; + + foreach (var address in addresses) { - foreach (var domain in domains) + if (NetworkUtility.IsIP(address)) { - if (string.IsNullOrEmpty(domain)) continue; - if (domain == forwarded) return true; - var ip = NetworkUtility.Resolve(domain); - if (NetworkUtility.IsIP(ip) && ip == forwarded) return true; + if (forwarded == address) return true; + } + else + { + var ip = NetworkUtility.Resolve(address); + if (NetworkUtility.IsIP(ip)) + { + if (forwarded == ip) return true; + } } } } @@ -177,17 +202,17 @@ namespace Apewer.Web } /// 请求来自于可信客户端。 - public static bool FromTrusted(HttpContext context, params string[] domains) + public static bool FromTrusted(HttpContext context, params string[] addresses) { if (context == null) return false; - return FromTrusted(context.Request, domains); + return FromTrusted(context.Request, addresses); } /// 请求来自于可信客户端。 - public static bool FromTrusted(ApiRequest request, params string[] domains) + public static bool FromTrusted(ApiRequest request, params string[] addresses) { if (request == null) return false; - return FromTrusted(request.Context, domains); + return FromTrusted(request.Context, addresses); } /// 获取 HTTP 方法。 @@ -287,6 +312,13 @@ namespace Apewer.Web return value; } + /// 获取 X-Forwarded-For。 + public static string GetForwardedIP(ApiRequest request) + { + if (request == null) return null; + return GetForwardedIP(request.Headers); + } + /// 获取 X-Forwarded-For。 public static string GetForwardedIP(HttpRequest request) => GetForwardedIP(GetHeaders(request)); @@ -373,11 +405,11 @@ namespace Apewer.Web foreach (var name in names) { if (string.IsNullOrEmpty(name)) continue; - sameNames.Add(name); + if (!sameNames.Contains(name)) sameNames.Add(name); var lower = TextUtility.ToLower(name); if (string.IsNullOrEmpty(lower)) continue; - lowerNames.Add(lower); + if (!lowerNames.Contains(lower)) lowerNames.Add(lower); } // POST 优先。 @@ -423,14 +455,24 @@ namespace Apewer.Web if (!string.IsNullOrEmpty(value)) return value; } -#if NETFX - // NETFX 最后搜索 GET 参数。 - if (request.Url != null) + // GET 参数。 + if (request.Parameters != null) { - var value = GetParameter(request.Url, names); - if (!string.IsNullOrEmpty(value)) return value; + // 全字匹配。 + foreach (var kvp in request.Parameters) + { + if (string.IsNullOrEmpty(kvp.Key) || string.IsNullOrEmpty(kvp.Value)) continue; + if (sameNames.Contains(kvp.Key)) return kvp.Value; + } + + // 以小写模糊匹配。 + foreach (var kvp in request.Parameters) + { + var lowerKey = TextUtility.ToLower(kvp.Key); + if (string.IsNullOrEmpty(lowerKey) || string.IsNullOrEmpty(kvp.Value)) continue; + if (lowerNames.Contains(lowerKey)) return kvp.Value; + } } -#endif return null; } diff --git a/Apewer/_ChangeLog.md b/Apewer/_ChangeLog.md index 7070cc4..efc5e18 100644 --- a/Apewer/_ChangeLog.md +++ b/Apewer/_ChangeLog.md @@ -5,6 +5,9 @@ ### 最新提交 +### 6.0.3 +- 修正 GetParameter 获取不到 GET 参数的问题。 + ### 6.0.2 - 修正 GetParameter 匹配 Json 的问题。