Skip to content

Commit

Permalink
Optimized the problem of requesting regional restriction matching errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wikiZ committed Jun 18, 2024
1 parent febd771 commit fde5465
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 92 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
186 changes: 94 additions & 92 deletions core/ipLookUpHelper.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit fde5465

Please sign in to comment.