From fde5465503fc53d7d562512daad4d19f1df500df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E8=B5=B7?= <37897216+wikiZ@users.noreply.github.com> Date: Tue, 18 Jun 2024 20:33:35 +0800 Subject: [PATCH] Optimized the problem of requesting regional restriction matching errors --- CHANGELOG | 4 + core/ipLookUpHelper.go | 186 +++++++++++++++++++++-------------------- 2 files changed, 98 insertions(+), 92 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f348e6d..812ba1f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +## [24.06.18.2001] - 2024-06-18 +### Update +- Optimized the problem of requesting regional restriction matching errors + ## [23.08.21.0113] - 2023-08-21 ### Added - Custom Delete Response Fields diff --git a/core/ipLookUpHelper.go b/core/ipLookUpHelper.go index 3fb891b..8e6f05e 100644 --- a/core/ipLookUpHelper.go +++ b/core/ipLookUpHelper.go @@ -1,92 +1,94 @@ -/** - * @Author 风起 - * @contact: onlyzaliks@gmail.com - * @File: ipLookUp.go - * @Time: 2022/5/5 9:13 - **/ - -package core - -import ( - "bytes" - "encoding/json" - "fmt" - "regexp" - "strings" - - "RedGuard/lib" - - "github.com/tidwall/gjson" -) - -// IPLookup IP origin,API is defined to extract relevant information -type ipLookup struct { - allowStatus int // API http request status code - hasCount int // Check if the first character of location is in English - body string // Get THE API response body JSON data - Tag string // The json data Tag - location string // Restrict the geographical location of the online -} - -var ( - _apiUrl = []string{ - // Chinese Users IP API - "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=%s&co=&resource_id=6006", - // IP API for users in other countries - "https://ipapi.co/%s/json/", - } - logger = lib.Logger() // logger output model -) - -// IPLookUp returns true if Check whether the IP address is the same as the owning place -// @param ip string Specify IP address -// @param location string Specify location -// NOTE: other countries Server You are advised to set location to English -// This will prioritize IP API that are more efficient for you -func IPLookUp(location, ip string) (state bool) { - var IPLook ipLookup - for _, url := range _apiUrl { - // Check preferentially invoked - if IPLook.hasCount != 1 { - // Check that the first character of location is in English - if regexp.MustCompile("[a-zA-Z]").MatchString(location[0:1]) { - // Other countries IP API are preferentially invoked if conditions are met - url, IPLook.hasCount = _apiUrl[1], 1 - } - } else { - url = _apiUrl[0] // preferentially invoked Chinese Users IP API - } - - // Get json data for the IP API response body - IPLook.allowStatus, IPLook.body = lib.HTTPRequest(fmt.Sprintf(url, ip)) - if IPLook.allowStatus == 200 { - // Select the response JSON tag when json data is available - if url == _apiUrl[0] { - IPLook.Tag = `data.#.location` // Chinese Users IP API Tag - break - } - IPLook.Tag = `city` - IPLook.location += gjson.Get(IPLook.body, `region`).String() - break - } - } - // Check for valid JSON data - if gjson.Valid(IPLook.body) { - // Extracting JSON data - result := gjson.Get(IPLook.body, IPLook.Tag) - if result.Exists() { - for _, name := range result.Array() { - IPLook.location += name.String() - } - var prettyJSON bytes.Buffer - // Format output JSON data - _ = json.Indent(&prettyJSON, []byte(IPLook.body), "", "\t") - logger.Emergency(string(prettyJSON.Bytes())) - // Check whether the IP address is the same as the specified location - if strings.Contains(strings.ToLower(IPLook.location), strings.ToLower(location)) { - return true // The query result is true - } - } - } - return false -} +/** + * @Author 风起 + * @contact: onlyzaliks@gmail.com + * @File: ipLookUp.go + * @Time: 2022/5/5 9:13 + **/ + +package core + +import ( + "bytes" + "encoding/json" + "fmt" + "regexp" + "strings" + + "RedGuard/lib" + + "github.com/tidwall/gjson" +) + +// IPLookup IP origin,API is defined to extract relevant information +type ipLookup struct { + allowStatus int // API http request status code + hasCount int // Check if the first character of location is in English + body string // Get THE API response body JSON data + Tag string // The json data Tag + location string // Restrict the geographical location of the online +} + +var ( + _apiUrl = []string{ + // Chinese Users IP API + "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=%s&co=&resource_id=6006", + // IP API for users in other countries + "https://ipapi.co/%s/json/", + } + logger = lib.Logger() // logger output model +) + +// IPLookUp returns true if Check whether the IP address is the same as the owning place +// @param ip string Specify IP address +// @param location string Specify location +// NOTE: other countries Server You are advised to set location to English +// This will prioritize IP API that are more efficient for you +func IPLookUp(location, ip string) (state bool) { + var IPLook ipLookup + for _, url := range _apiUrl { + // Check preferentially invoked + if IPLook.hasCount != 1 { + // Check that the first character of location is in English + if regexp.MustCompile("[a-zA-Z]").MatchString(location[0:1]) { + // Other countries IP API are preferentially invoked if conditions are met + url, IPLook.hasCount = _apiUrl[1], 1 + } + } else { + url = _apiUrl[0] // preferentially invoked Chinese Users IP API + } + + // Get json data for the IP API response body + IPLook.allowStatus, IPLook.body = lib.HTTPRequest(fmt.Sprintf(url, ip)) + if IPLook.allowStatus == 200 { + // Select the response JSON tag when json data is available + if url == _apiUrl[0] { + IPLook.Tag = `data.#.location` // Chinese Users IP API Tag + break + } + IPLook.Tag = `city` + IPLook.location += gjson.Get(IPLook.body, `region`).String() + break + } + } + // Check for valid JSON data + if gjson.Valid(IPLook.body) { + // Extracting JSON data + result := gjson.Get(IPLook.body, IPLook.Tag) + if result.Exists() { + for _, name := range result.Array() { + IPLook.location += name.String() + } + var prettyJSON bytes.Buffer + // Format output JSON data + _ = json.Indent(&prettyJSON, []byte(IPLook.body), "", "\t") + logger.Emergency(string(prettyJSON.Bytes())) + // Check whether the IP address is the same as the specified location + for _, location := range strings.Split(location, ",") { + if strings.Contains(strings.ToLower(IPLook.location), strings.ToLower(location)) { + return true // The query result is true + } + } + } + } + return false +}