feat(service): 优化地址解析逻辑以支持端口号直接解析

重构 parseTraditionalStyle 函数,简化 NetworkAddress 构造方式,
并引入 utils.IsNumber 判断纯端口号情况,提升地址解析的准确性与兼容性。
```
This commit is contained in:
2025-09-23 13:26:47 +08:00
parent 75aa6ae647
commit 139983134b
2 changed files with 140 additions and 13 deletions

View File

@@ -5,6 +5,8 @@ import (
"net"
"net/url"
"strings"
"git.apinb.com/bsm-sdk/core/utils"
)
type NetworkAddress struct {
@@ -126,28 +128,24 @@ func determineTCPProtocol(host string) string {
// 解析传统格式如 ":8080", "127.0.0.1:8080", "/tmp/socket"
func parseTraditionalStyle(addr string) (*NetworkAddress, error) {
result := &NetworkAddress{Raw: addr}
// 检查是否是 Unix socket包含路径分隔符
if strings.Contains(addr, "/") || strings.HasPrefix(addr, "@/") {
result.Protocol = "unix"
result.Path = addr
return result, nil
return &NetworkAddress{Protocol: "unix", Path: addr}, nil
}
// 否则按 TCP 地址解析
result.Protocol = "tcp"
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, fmt.Errorf("解析地址失败: %w", err)
if err == nil {
return &NetworkAddress{Protocol: "tcp", Host: host, Port: port}, nil
}
result.Host = host
result.Port = port
result.Protocol = determineTCPProtocol(host)
// 检查是否是端口号
if ok := utils.IsNumber(addr); ok {
return &NetworkAddress{Protocol: "tcp", Host: "0.0.0.0", Port: addr}, nil
}
return nil, fmt.Errorf("解析地址失败: %w", err)
return result, nil
}
// 获取网络类型用于 net.Dial 或 net.Listen