Skip to content

Commit 68d6b92

Browse files
committed
refactor(ports): 使用ParseUint替换Atoi简化端口解析逻辑
- 将端口解析逻辑中的strconv.Atoi替换为strconv.ParseUint - 移除额外的端口范围校验代码,因ParseUint已包含范围检验 - 减少冗余代码,提升端口解析的执行效率 - 优化多个模块中与端口相关的代码一致性和可维护性
1 parent 3b23add commit 68d6b92

File tree

4 files changed

+26
-53
lines changed

4 files changed

+26
-53
lines changed

node/sub.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,15 @@ func LoadClashConfigFromURLWithReporter(id int, urlStr string, subName string, d
187187
return nil, fmt.Errorf("split host port error: %v", splitErr)
188188
}
189189

190-
portInt, atoiErr := strconv.Atoi(portStr)
191-
if atoiErr != nil {
192-
return nil, fmt.Errorf("invalid port: %v", atoiErr)
193-
}
194-
195-
// 验证端口范围
196-
if portInt < 0 || portInt > 65535 {
197-
return nil, fmt.Errorf("port out of range: %d", portInt)
190+
portUint, parseErr := strconv.ParseUint(portStr, 10, 16)
191+
if parseErr != nil {
192+
return nil, fmt.Errorf("invalid port: %v", parseErr)
198193
}
199194

200195
// 创建 mihomo metadata
201196
metadata := &constant.Metadata{
202197
Host: host,
203-
DstPort: uint16(portInt),
198+
DstPort: uint16(portUint),
204199
Type: constant.HTTP,
205200
}
206201

node/usage.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,14 @@ func FetchAirportUsageInfo(airport *models.Airport) (*UsageInfo, error) {
6161
return nil, fmt.Errorf("split host port error: %v", splitErr)
6262
}
6363

64-
portInt, atoiErr := strconv.Atoi(portStr)
65-
if atoiErr != nil {
66-
return nil, fmt.Errorf("invalid port: %v", atoiErr)
67-
}
68-
69-
if portInt < 0 || portInt > 65535 {
70-
return nil, fmt.Errorf("port out of range: %d", portInt)
64+
portUint, parseErr := strconv.ParseUint(portStr, 10, 16)
65+
if parseErr != nil {
66+
return nil, fmt.Errorf("invalid port: %v", parseErr)
7167
}
7268

7369
metadata := &constant.Metadata{
7470
Host: host,
75-
DstPort: uint16(portInt),
71+
DstPort: uint16(portUint),
7672
Type: constant.HTTP,
7773
}
7874

services/mihomo/mihomo.go

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,11 @@ func MihomoSpeedTest(
234234
}
235235
}
236236

237-
portInt, err := strconv.Atoi(portStr)
237+
portUint, err := strconv.ParseUint(portStr, 10, 16)
238238
if err != nil {
239239
return 0, 0, 0, "", nil, fmt.Errorf("invalid port: %v", err)
240240
}
241-
// Validate port range to prevent overflow
242-
if portInt < 0 || portInt > 65535 {
243-
return 0, 0, 0, "", nil, fmt.Errorf("port out of range: %d", portInt)
244-
}
245-
port := uint16(portInt)
241+
port := uint16(portUint)
246242

247243
metadata := &constant.Metadata{
248244
Host: parsedUrl.Hostname(),
@@ -294,16 +290,11 @@ func MihomoSpeedTest(
294290
return nil, fmt.Errorf("split host port error: %v", splitErr)
295291
}
296292

297-
pInt, atoiErr := strconv.Atoi(pStr)
298-
if atoiErr != nil {
299-
return nil, fmt.Errorf("invalid port string: %v", atoiErr)
300-
}
301-
302-
// Validate port range
303-
if pInt < 0 || pInt > 65535 {
304-
return nil, fmt.Errorf("port out of range: %d", pInt)
293+
pUint, parseErr := strconv.ParseUint(pStr, 10, 16)
294+
if parseErr != nil {
295+
return nil, fmt.Errorf("invalid port string: %v", parseErr)
305296
}
306-
p := uint16(pInt)
297+
p := uint16(pUint)
307298

308299
md := &constant.Metadata{
309300
Host: h,
@@ -467,18 +458,14 @@ func fetchLandingIPWithAdapter(proxyAdapter constant.Proxy, ipUrl string) string
467458
return nil, splitErr
468459
}
469460

470-
pInt, atoiErr := strconv.Atoi(pStr)
471-
if atoiErr != nil {
472-
return nil, atoiErr
473-
}
474-
475-
if pInt < 0 || pInt > 65535 {
476-
return nil, fmt.Errorf("port out of range: %d", pInt)
461+
pUint, parseErr := strconv.ParseUint(pStr, 10, 16)
462+
if parseErr != nil {
463+
return nil, parseErr
477464
}
478465

479466
md := &constant.Metadata{
480467
Host: h,
481-
DstPort: uint16(pInt),
468+
DstPort: uint16(pUint),
482469
Type: constant.HTTP,
483470
}
484471
return proxyAdapter.DialContext(dialCtx, md)
@@ -530,13 +517,13 @@ func fetchQuality(proxyAdapter constant.Proxy, qualityURL string) *QualityCheckR
530517
if splitErr != nil {
531518
return nil, splitErr
532519
}
533-
pInt, atoiErr := strconv.Atoi(pStr)
534-
if atoiErr != nil {
535-
return nil, atoiErr
520+
pUint, parseErr := strconv.ParseUint(pStr, 10, 16)
521+
if parseErr != nil {
522+
return nil, parseErr
536523
}
537524
md := &constant.Metadata{
538525
Host: h,
539-
DstPort: uint16(pInt),
526+
DstPort: uint16(pUint),
540527
Type: constant.HTTP,
541528
}
542529
return proxyAdapter.DialContext(dialCtx, md)

utils/proxy_client.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,15 @@ func CreateProxyHTTPClient(useProxy bool, proxyLink string, timeout time.Duratio
7777
return nil, fmt.Errorf("split host port error: %v", splitErr)
7878
}
7979

80-
portInt, atoiErr := strconv.Atoi(portStr)
81-
if atoiErr != nil {
82-
return nil, fmt.Errorf("invalid port: %v", atoiErr)
83-
}
84-
85-
// 验证端口范围
86-
if portInt < 0 || portInt > 65535 {
87-
return nil, fmt.Errorf("port out of range: %d", portInt)
80+
portUint, parseErr := strconv.ParseUint(portStr, 10, 16)
81+
if parseErr != nil {
82+
return nil, fmt.Errorf("invalid port: %v", parseErr)
8883
}
8984

9085
// 创建 mihomo metadata
9186
metadata := &constant.Metadata{
9287
Host: host,
93-
DstPort: uint16(portInt),
88+
DstPort: uint16(portUint),
9489
Type: constant.HTTP,
9590
}
9691

0 commit comments

Comments
 (0)