Compare commits

...

4 Commits

Author SHA1 Message Date
github-actions
971f832fff Update README.md with new tag v0.0.8-20251102140847 2025-11-02 14:08:48 +00:00
spiritlhl
e3e938e152 fix:修复cdn检测可用性的漏洞 2025-11-02 14:07:36 +00:00
spiritlhl
d8148ff279 fix:删除无效测试 2025-11-02 14:04:12 +00:00
spiritlhl
50b411fa41 fix: 更新版本 2025-11-02 13:50:26 +00:00
5 changed files with 76 additions and 12 deletions

View File

@ -20,14 +20,17 @@ jobs:
go-version: ${{ matrix.go }}
- name: Test
run: go test ./... -coverprofile=coverage.txt
run: |
# Run all tests without generating a combined coverage profile
set -euo pipefail
go test ./...
- name: Create Tag
if: success()
run: |
git config --global user.name 'github-actions'
git config --global user.email 'github-actions@github.com'
TAG="v0.0.7-$(date +'%Y%m%d%H%M%S')"
TAG="v0.0.8-$(date +'%Y%m%d%H%M%S')"
git tag $TAG
git push origin $TAG
echo "TAG=$TAG" >> $GITHUB_ENV

View File

@ -75,7 +75,7 @@ rm -rf /usr/bin/backtrace
## 在Golang中使用
```
go get github.com/oneclickvirt/backtrace@v0.0.7-20250811023541
go get github.com/oneclickvirt/backtrace@v0.0.8-20251102140847
```
## 概览图

View File

@ -33,6 +33,41 @@ func removeDuplicates(elements []string) []string {
return result
}
// checkCdn 检查CDN可用性参考shell脚本的测试逻辑
func checkCdn(testUrl string) string {
client := req.C()
client.SetTimeout(6 * time.Second)
if model.EnableLoger {
InitLogger()
defer Logger.Sync()
}
for _, cdnUrl := range model.CdnList {
url := cdnUrl + testUrl
if model.EnableLoger {
Logger.Info(fmt.Sprintf("Testing CDN: %s", url))
}
resp, err := client.R().Get(url)
if err == nil {
b, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err == nil && strings.Contains(string(b), "success") {
if model.EnableLoger {
Logger.Info(fmt.Sprintf("CDN available: %s", cdnUrl))
}
return cdnUrl
}
}
if model.EnableLoger {
Logger.Info(fmt.Sprintf("CDN test failed: %s, error: %v", cdnUrl, err))
}
time.Sleep(500 * time.Millisecond)
}
if model.EnableLoger {
Logger.Info("No CDN available, using direct connection")
}
return ""
}
// getData 获取目标地址的文本内容
func getData(endpoint string) string {
client := req.C()
@ -45,15 +80,41 @@ func getData(endpoint string) string {
InitLogger()
defer Logger.Sync()
}
for _, baseUrl := range model.CdnList {
url := baseUrl + endpoint
// 先测试CDN可用性
testUrl := "https://raw.githubusercontent.com/spiritLHLS/ecs/main/back/test"
cdnUrl := checkCdn(testUrl)
// 如果有可用的CDN使用CDN获取数据
if cdnUrl != "" {
url := cdnUrl + endpoint
if model.EnableLoger {
Logger.Info(fmt.Sprintf("Using CDN: %s", url))
}
resp, err := client.R().Get(url)
if err == nil {
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if strings.Contains(string(b), "error") {
continue
if err == nil && !strings.Contains(string(b), "error") {
if model.EnableLoger {
Logger.Info(fmt.Sprintf("Received data length: %d", len(b)))
}
return string(b)
}
}
if model.EnableLoger {
Logger.Info(fmt.Sprintf("CDN request failed: %v", err))
}
}
// CDN不可用尝试直连
if model.EnableLoger {
Logger.Info(fmt.Sprintf("Trying direct connection: %s", endpoint))
}
resp, err := client.R().Get(endpoint)
if err == nil {
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err == nil {
if model.EnableLoger {
Logger.Info(fmt.Sprintf("Received data length: %d", len(b)))
@ -62,8 +123,7 @@ func getData(endpoint string) string {
}
}
if model.EnableLoger {
Logger.Info(fmt.Sprintf("HTTP request failed: %v", err))
}
Logger.Info(fmt.Sprintf("Direct connection failed: %v", err))
}
return ""
}

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/oneclickvirt/backtrace
go 1.24.5
go 1.25.3
require (
github.com/google/uuid v1.6.0

View File

@ -2,7 +2,7 @@ package model
import "time"
const BackTraceVersion = "v0.0.7"
const BackTraceVersion = "v0.0.8"
var EnableLoger = false
@ -17,6 +17,7 @@ type IcmpTarget struct {
var (
IcmpTargets = "https://raw.githubusercontent.com/spiritLHLS/icmp_targets/main/nodes.json"
CdnList = []string{
"https://cdn.spiritlhl.net/",
"http://cdn1.spiritlhl.net/",
"http://cdn2.spiritlhl.net/",
"http://cdn3.spiritlhl.net/",