🎨 新增ip检测配置功能
This commit is contained in:
31
utils/ip.go
31
utils/ip.go
@@ -3,6 +3,9 @@ package utils
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.echol.cn/loser/lckt/global"
|
||||
user2 "git.echol.cn/loser/lckt/model/user"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -14,13 +17,6 @@ type ipAdcodeResp struct {
|
||||
} `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}
|
||||
@@ -38,6 +34,27 @@ func GetIPAdcode(ip string) string {
|
||||
|
||||
// CheckIPInAdcodes 检测用户IP是否在指定的地区编码范围内
|
||||
func CheckIPInAdcodes(ipAdcode string) bool {
|
||||
// 查询IP配置
|
||||
ipCon := user2.IpCheck{}
|
||||
err := global.GVA_DB.First(&ipCon).Error
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error("获取IP配置失败", zap.Error(err))
|
||||
return false
|
||||
}
|
||||
// 如果IP检测未启用,直接返回true
|
||||
if !ipCon.Status {
|
||||
return true
|
||||
}
|
||||
// 解析允许的地区编码列表
|
||||
var adcodes []string
|
||||
// 通过逗号分隔
|
||||
for _, code := range strings.Split(ipCon.Addrs, ",") {
|
||||
trimmed := strings.TrimSpace(code)
|
||||
if trimmed != "" {
|
||||
adcodes = append(adcodes, trimmed)
|
||||
}
|
||||
}
|
||||
|
||||
lower := strings.ToLower(ipAdcode)
|
||||
for _, code := range adcodes {
|
||||
// 检测是否包含
|
||||
|
||||
Reference in New Issue
Block a user