50 lines
949 B
Go
50 lines
949 B
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type ipAdcodeResp struct {
|
|
Adcode struct {
|
|
O string `json:"o"`
|
|
} `json:"adcode"`
|
|
}
|
|
|
|
// adcodes 是允许的地区编码列表
|
|
var adcodes = []string{
|
|
"重庆",
|
|
"海南",
|
|
"广东",
|
|
}
|
|
|
|
func GetIPAdcode(ip string) string {
|
|
url := fmt.Sprintf("https://api.vore.top/api/IPdata?ip=%s", ip)
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
|
resp, err := client.Get(url)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
defer resp.Body.Close()
|
|
var result ipAdcodeResp
|
|
if err = json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
return ""
|
|
}
|
|
return result.Adcode.O
|
|
}
|
|
|
|
// CheckIPInAdcodes 检测用户IP是否在指定的地区编码范围内
|
|
func CheckIPInAdcodes(ipAdcode string) bool {
|
|
lower := strings.ToLower(ipAdcode)
|
|
for _, code := range adcodes {
|
|
// 检测是否包含
|
|
if strings.Contains(lower, strings.ToLower(code)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|