mirror of
https://github.com/oneclickvirt/backtrace.git
synced 2025-11-05 00:02:37 +08:00
Compare commits
4 Commits
3f86c41ebf
...
971f832fff
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
971f832fff | ||
|
|
e3e938e152 | ||
|
|
d8148ff279 | ||
|
|
50b411fa41 |
7
.github/workflows/ci.yaml
vendored
7
.github/workflows/ci.yaml
vendored
@ -20,14 +20,17 @@ jobs:
|
|||||||
go-version: ${{ matrix.go }}
|
go-version: ${{ matrix.go }}
|
||||||
|
|
||||||
- name: Test
|
- 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
|
- name: Create Tag
|
||||||
if: success()
|
if: success()
|
||||||
run: |
|
run: |
|
||||||
git config --global user.name 'github-actions'
|
git config --global user.name 'github-actions'
|
||||||
git config --global user.email 'github-actions@github.com'
|
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 tag $TAG
|
||||||
git push origin $TAG
|
git push origin $TAG
|
||||||
echo "TAG=$TAG" >> $GITHUB_ENV
|
echo "TAG=$TAG" >> $GITHUB_ENV
|
||||||
|
|||||||
@ -75,7 +75,7 @@ rm -rf /usr/bin/backtrace
|
|||||||
## 在Golang中使用
|
## 在Golang中使用
|
||||||
|
|
||||||
```
|
```
|
||||||
go get github.com/oneclickvirt/backtrace@v0.0.7-20250811023541
|
go get github.com/oneclickvirt/backtrace@v0.0.8-20251102140847
|
||||||
```
|
```
|
||||||
|
|
||||||
## 概览图
|
## 概览图
|
||||||
|
|||||||
72
bk/utils.go
72
bk/utils.go
@ -33,6 +33,41 @@ func removeDuplicates(elements []string) []string {
|
|||||||
return result
|
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 获取目标地址的文本内容
|
// getData 获取目标地址的文本内容
|
||||||
func getData(endpoint string) string {
|
func getData(endpoint string) string {
|
||||||
client := req.C()
|
client := req.C()
|
||||||
@ -45,15 +80,41 @@ func getData(endpoint string) string {
|
|||||||
InitLogger()
|
InitLogger()
|
||||||
defer Logger.Sync()
|
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)
|
resp, err := client.R().Get(url)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
b, err := io.ReadAll(resp.Body)
|
b, err := io.ReadAll(resp.Body)
|
||||||
if strings.Contains(string(b), "error") {
|
if err == nil && !strings.Contains(string(b), "error") {
|
||||||
continue
|
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 err == nil {
|
||||||
if model.EnableLoger {
|
if model.EnableLoger {
|
||||||
Logger.Info(fmt.Sprintf("Received data length: %d", len(b)))
|
Logger.Info(fmt.Sprintf("Received data length: %d", len(b)))
|
||||||
@ -62,8 +123,7 @@ func getData(endpoint string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if model.EnableLoger {
|
if model.EnableLoger {
|
||||||
Logger.Info(fmt.Sprintf("HTTP request failed: %v", err))
|
Logger.Info(fmt.Sprintf("Direct connection failed: %v", err))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
|||||||
module github.com/oneclickvirt/backtrace
|
module github.com/oneclickvirt/backtrace
|
||||||
|
|
||||||
go 1.24.5
|
go 1.25.3
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
|
|||||||
@ -2,7 +2,7 @@ package model
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
const BackTraceVersion = "v0.0.7"
|
const BackTraceVersion = "v0.0.8"
|
||||||
|
|
||||||
var EnableLoger = false
|
var EnableLoger = false
|
||||||
|
|
||||||
@ -17,6 +17,7 @@ type IcmpTarget struct {
|
|||||||
var (
|
var (
|
||||||
IcmpTargets = "https://raw.githubusercontent.com/spiritLHLS/icmp_targets/main/nodes.json"
|
IcmpTargets = "https://raw.githubusercontent.com/spiritLHLS/icmp_targets/main/nodes.json"
|
||||||
CdnList = []string{
|
CdnList = []string{
|
||||||
|
"https://cdn.spiritlhl.net/",
|
||||||
"http://cdn1.spiritlhl.net/",
|
"http://cdn1.spiritlhl.net/",
|
||||||
"http://cdn2.spiritlhl.net/",
|
"http://cdn2.spiritlhl.net/",
|
||||||
"http://cdn3.spiritlhl.net/",
|
"http://cdn3.spiritlhl.net/",
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user