mirror of
https://github.com/oneclickvirt/backtrace.git
synced 2026-11-24 23:39:41 +08:00
feat: 添加远程备用地址的相关逻辑
This commit is contained in:
1 parent
aad93e51f8
commit
2adf709e63
3 files changed
+101
-10
No files matched your search
+62
@@ -31,6 +31,7 @@ func removeDuplicates(elements []string) []string {
|
||||
return result // 返回去重后的结果切片
|
||||
}
|
||||
|
||||
// getData 获取目标地址的文本内容
|
||||
func getData(endpoint string) string {
|
||||
client := req.C()
|
||||
client.SetTimeout(10 * time.Second)
|
||||
@@ -64,3 +65,64 @@ func getData(endpoint string) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// tryAlternativeIPs 从IcmpTargets获取备选IP地址
|
||||
func tryAlternativeIPs(targetName string, ipVersion string) []string {
|
||||
jsonData := getData(model.IcmpTargets)
|
||||
if jsonData == "" {
|
||||
return nil
|
||||
}
|
||||
// 简单解析JSON,提取省份和ISP信息
|
||||
var targetProvince, targetISP string
|
||||
// 从目标名称中提取省份和ISP信息
|
||||
if strings.Contains(targetName, "北京") {
|
||||
targetProvince = "北京"
|
||||
} else if strings.Contains(targetName, "上海") {
|
||||
targetProvince = "上海"
|
||||
} else if strings.Contains(targetName, "广州") {
|
||||
targetProvince = "广东"
|
||||
} else if strings.Contains(targetName, "成都") {
|
||||
targetProvince = "四川"
|
||||
}
|
||||
if strings.Contains(targetName, "电信") {
|
||||
targetISP = "电信"
|
||||
} else if strings.Contains(targetName, "联通") {
|
||||
targetISP = "联通"
|
||||
} else if strings.Contains(targetName, "移动") {
|
||||
targetISP = "移动"
|
||||
}
|
||||
// 如果没有提取到信息,返回空
|
||||
if targetProvince == "" || targetISP == "" {
|
||||
return nil
|
||||
}
|
||||
// 解析JSON数据寻找匹配的记录
|
||||
var result []string
|
||||
for _, line := range strings.Split(jsonData, "},{") {
|
||||
if strings.Contains(line, "\"province\":\""+targetProvince+"省\"") &&
|
||||
strings.Contains(line, "\"isp\":\""+targetISP+"\"") &&
|
||||
strings.Contains(line, "\"ip_version\":\""+ipVersion+"\"") {
|
||||
// 提取IP列表
|
||||
ipsStart := strings.Index(line, "\"ips\":\"") + 7
|
||||
if ipsStart > 7 {
|
||||
ipsEnd := strings.Index(line[ipsStart:], "\"")
|
||||
if ipsEnd > 0 {
|
||||
ipsList := line[ipsStart : ipsStart+ipsEnd]
|
||||
ips := strings.Split(ipsList, ",")
|
||||
// 最多返回3个不重复的IP地址
|
||||
count := 0
|
||||
for _, ip := range ips {
|
||||
if ip != "" {
|
||||
result = append(result, ip)
|
||||
count++
|
||||
if count >= 3 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user