Skip to content

Commit 0341e8f

Browse files
committed
fix: 修复多处潜在 panic 及数据竞争,清理死代码
- CheckSocks: 修复 tmpEffectiveList 为空时 rand.Intn(0) panic - netspace: FOFA/Quake/Hunter 类型断言改为安全形式,防止异常响应 panic - globals: SwitchAndGetProxy 原子性返回代理地址,消除 EffectiveList 锁外读竞态 - delInvalidProxy: 移除无效的 proxyIndex 调整逻辑(getNextProxy 从不读取该值) - 删除空函数 IncrActiveConns、未使用的 activeMu - goreleaser: 升级为 v2 语法(format→formats, version_template→name_template) - utils_test: 修正 TestDelInvalidProxy(原测试调用次数不足且检查已删除逻辑)
1 parent e05e00b commit 0341e8f

6 files changed

Lines changed: 82 additions & 66 deletions

File tree

.goreleaser.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ builds:
1515
ldflags: -s -w
1616

1717
archives:
18-
- format: zip
18+
- formats: [zip]
1919
name_template: >-
2020
{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}
2121
files:
@@ -26,7 +26,7 @@ checksum:
2626
name_template: 'checksums.txt'
2727

2828
snapshot:
29-
version_template: "{{ .Tag }}-next"
29+
name_template: "{{ .Tag }}-next"
3030

3131
changelog:
3232
sort: asc

main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,9 @@ func main() {
212212
fmt.Println(utils.ColorYellow + "按回车键随机切换到下一个代理IP,输入 s 回车查看统计..." + utils.ColorReset)
213213
continue
214214
}
215-
utils.SetNextProxyIndex()
216-
currentIndex := utils.GetCurrentProxyIndex()
217-
if currentIndex >= 0 && len(utils.EffectiveList) > 0 {
218-
fmt.Printf(utils.ColorGreen+"已随机切换到代理IP: %s (剩余可用: %d)\n"+utils.ColorReset, utils.EffectiveList[currentIndex], len(utils.EffectiveList))
215+
addr, total := utils.SwitchAndGetProxy()
216+
if addr != "" {
217+
fmt.Printf(utils.ColorGreen+"已随机切换到代理IP: %s (剩余可用: %d)\n"+utils.ColorReset, addr, total)
219218
} else {
220219
fmt.Println(utils.ColorRed + "没有可用的代理IP" + utils.ColorReset)
221220
}

utils/globals.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,17 @@ var (
3737
// 优雅关闭
3838
ShutdownChan chan struct{}
3939
ActiveConns int32 // 当前活跃连接数
40-
activeMu sync.Mutex
4140
)
4241

43-
// GetCurrentProxyIndex 获取当前随机选择的代理索引
44-
func GetCurrentProxyIndex() int {
42+
// SwitchAndGetProxy 随机切换代理并原子性返回新地址和当前可用总数
43+
func SwitchAndGetProxy() (addr string, total int) {
4544
mu.Lock()
4645
defer mu.Unlock()
4746
if len(EffectiveList) == 0 {
48-
return -1
47+
return "", 0
4948
}
50-
// 随机选择一个索引
5149
proxyIndex = rand.Intn(len(EffectiveList))
52-
return proxyIndex
53-
}
54-
55-
// SetNextProxyIndex 随机选择下一个代理索引
56-
func SetNextProxyIndex() {
57-
mu.Lock()
58-
defer mu.Unlock()
59-
if len(EffectiveList) > 0 {
60-
// 随机选择一个索引
61-
proxyIndex = rand.Intn(len(EffectiveList))
62-
}
50+
return EffectiveList[proxyIndex], len(EffectiveList)
6351
}
6452

6553
func Banner() {

utils/netspace.go

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,22 @@ func GetSocksFromQuake(quake QUAKEConfig) {
3535
fmt.Println("QUAKE:", data["message"])
3636
return
3737
}
38-
arr := data["data"].([]interface{})
38+
arr, ok := data["data"].([]interface{})
39+
if !ok {
40+
fmt.Println("quake: 返回数据格式异常")
41+
return
42+
}
3943
fmt.Println("+++quake数据已取+++")
4044
for _, item := range arr {
41-
itemMap := item.(map[string]interface{})
42-
ip := itemMap["ip"].(string)
43-
port := itemMap["port"].(float64)
45+
itemMap, ok := item.(map[string]interface{})
46+
if !ok {
47+
continue
48+
}
49+
ip, ok1 := itemMap["ip"].(string)
50+
port, ok2 := itemMap["port"].(float64)
51+
if !ok1 || !ok2 {
52+
continue
53+
}
4454
addSocks(ip + ":" + strconv.FormatFloat(port, 'f', -1, 64))
4555
}
4656
}
@@ -71,11 +81,23 @@ func GetSocksFromFofa(fofa FOFAConfig) {
7181
fmt.Println("FOFA:", data["errmsg"])
7282
return
7383
}
74-
array := data["results"].([]interface{})
84+
array, ok := data["results"].([]interface{})
85+
if !ok {
86+
fmt.Println("FOFA: 返回数据格式异常")
87+
return
88+
}
7589
fmt.Println("+++fofa数据已取+++")
7690
for _, itemArray := range array {
77-
itemSlice := itemArray.([]interface{})
78-
addSocks(itemSlice[0].(string) + ":" + itemSlice[1].(string))
91+
itemSlice, ok := itemArray.([]interface{})
92+
if !ok || len(itemSlice) < 2 {
93+
continue
94+
}
95+
ip, ok1 := itemSlice[0].(string)
96+
port, ok2 := itemSlice[1].(string)
97+
if !ok1 || !ok2 {
98+
continue
99+
}
100+
addSocks(ip + ":" + port)
79101
}
80102

81103
}
@@ -111,17 +133,35 @@ func GetSocksFromHunter(hunter HUNTERConfig) {
111133
return
112134
}
113135

114-
rsData := data["data"].(map[string]interface{})
115-
total := rsData["total"].(float64)
136+
rsData, ok := data["data"].(map[string]interface{})
137+
if !ok {
138+
fmt.Println("HUNTER: 返回数据格式异常")
139+
return
140+
}
141+
total, ok := rsData["total"].(float64)
142+
if !ok {
143+
fmt.Println("HUNTER: 返回total字段格式异常")
144+
break
145+
}
116146
if total == 0 {
117147
fmt.Println("HUNTER:xxx根据配置语法,未取到数据xxx")
118148
break
119149
}
120-
arr := rsData["arr"].([]interface{})
150+
arr, ok := rsData["arr"].([]interface{})
151+
if !ok {
152+
fmt.Println("HUNTER: 返回arr字段格式异常")
153+
break
154+
}
121155
for _, item := range arr {
122-
itemMap := item.(map[string]interface{})
123-
ip := itemMap["ip"].(string)
124-
port := itemMap["port"].(float64)
156+
itemMap, ok := item.(map[string]interface{})
157+
if !ok {
158+
continue
159+
}
160+
ip, ok1 := itemMap["ip"].(string)
161+
port, ok2 := itemMap["port"].(float64)
162+
if !ok1 || !ok2 {
163+
continue
164+
}
125165
exeData++
126166
addSocks(ip + ":" + strconv.FormatFloat(port, 'f', -1, 64))
127167
}

utils/utils.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,6 @@ func GetMaxFailCount() int {
197197
return getMaxFailCount()
198198
}
199199

200-
// IncrActiveConns 增加活跃连接计数
201-
func IncrActiveConns() {
202-
// 注意:使用 atomic 包操作,这里用 mutex 保护
203-
// 为简化,在 transmitReqFromClient 中直接处理
204-
}
205-
206200
// GetActiveConns 获取当前活跃连接数(用于优雅关闭)
207201
func GetActiveConns() int32 {
208202
return ActiveConns
@@ -375,7 +369,9 @@ func CheckSocks(checkSocks CheckSocksConfig, socksListParam []string) {
375369
mu.Lock()
376370
EffectiveList = make([]string, len(tmpEffectiveList))
377371
copy(EffectiveList, tmpEffectiveList)
378-
proxyIndex = rand.Intn(len(tmpEffectiveList)) // 随机初始化索引
372+
if len(tmpEffectiveList) > 0 {
373+
proxyIndex = rand.Intn(len(tmpEffectiveList))
374+
}
379375
mu.Unlock()
380376
// 初始化/更新代理统计信息
381377
InitProxyStats(EffectiveList)
@@ -540,21 +536,11 @@ func delInvalidProxy(proxy string) bool {
540536
for i, p := range EffectiveList {
541537
if p == proxy {
542538
EffectiveList = append(EffectiveList[:i], EffectiveList[i+1:]...)
543-
if proxyIndex > i {
544-
proxyIndex--
545-
}
546539
break
547540
}
548541
}
549542
// 清理统计信息
550543
delete(StatsMap, proxy)
551-
552-
// 确保 proxyIndex 不越界
553-
if len(EffectiveList) == 0 {
554-
proxyIndex = 0
555-
} else if proxyIndex >= len(EffectiveList) {
556-
proxyIndex = 0
557-
}
558544
return true
559545
}
560546

utils/utils_test.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,27 @@ func TestGetNextProxyRandom(t *testing.T) {
6262
}
6363

6464
func TestDelInvalidProxy(t *testing.T) {
65-
// 设置测试数据
6665
EffectiveList = []string{"127.0.0.1:1080", "127.0.0.2:1080", "127.0.0.3:1080"}
67-
proxyIndex = 1 // 当前指向第二个元素
68-
69-
// 删除第一个代理 (索引 0)
70-
delInvalidProxy("127.0.0.1:1080")
71-
66+
StatsMap = nil
67+
MaxFailCount = 2 // 需要连续 2 次失败才删除
68+
69+
// 第一次失败:未达阈值,不应删除
70+
removed := delInvalidProxy("127.0.0.1:1080")
71+
if removed {
72+
t.Error("Expected false on first failure (streak 1 < maxFail 2)")
73+
}
74+
if len(EffectiveList) != 3 {
75+
t.Errorf("Expected 3 socks (no delete yet), got: %d", len(EffectiveList))
76+
}
77+
78+
// 第二次失败:达到阈值,应触发删除
79+
removed = delInvalidProxy("127.0.0.1:1080")
80+
if !removed {
81+
t.Error("Expected proxy to be removed on second failure when MaxFailCount=2")
82+
}
7283
if len(EffectiveList) != 2 {
7384
t.Errorf("Expected 2 socks after delete, got: %d", len(EffectiveList))
7485
}
75-
76-
// proxyIndex (1) > i (0),所以 proxyIndex-- 变成 0
77-
// 现在指向新的第一个元素 "127.0.0.2:1080"
78-
if proxyIndex != 0 {
79-
t.Errorf("Expected proxyIndex=0, got: %d", proxyIndex)
80-
}
81-
82-
// 验证剩余代理
8386
if EffectiveList[0] != "127.0.0.2:1080" || EffectiveList[1] != "127.0.0.3:1080" {
8487
t.Errorf("Unexpected EffectiveList: %v", EffectiveList)
8588
}

0 commit comments

Comments
 (0)