refactor: 优化 TCPing 任务的目标解析逻辑
- 改进了 TCPing 任务中对 host:port 格式的解析,支持 IPv6 地址格式并默认使用端口 80。 - 移除了不必要的空行以提升代码可读性。 - 更新了安装脚本,移除了不再使用的镜像源。
This commit is contained in:
@@ -96,7 +96,6 @@ detect_fastest_mirror() {
|
||||
|
||||
# Ubuntu/Debian 镜像源列表
|
||||
UBUNTU_MIRRORS=(
|
||||
"mirrors.tuna.tsinghua.edu.cn"
|
||||
"mirrors.huaweicloud.com"
|
||||
"mirrors.163.com"
|
||||
"archive.ubuntu.com"
|
||||
@@ -104,7 +103,6 @@ detect_fastest_mirror() {
|
||||
|
||||
# CentOS/RHEL 镜像源列表
|
||||
CENTOS_MIRRORS=(
|
||||
"mirrors.tuna.tsinghua.edu.cn"
|
||||
"mirrors.huaweicloud.com"
|
||||
)
|
||||
|
||||
|
||||
@@ -28,14 +28,43 @@ type TCPingTask struct {
|
||||
}
|
||||
|
||||
func NewTCPingTask(taskID, target string, interval, maxDuration time.Duration) (*TCPingTask, error) {
|
||||
// 解析host:port
|
||||
parts := strings.Split(target, ":")
|
||||
if len(parts) != 2 {
|
||||
return nil, fmt.Errorf("无效的target格式,需要 host:port")
|
||||
// 解析host:port,如果没有端口则默认80
|
||||
var host string
|
||||
var portStr string
|
||||
var port int
|
||||
|
||||
// 检查是否是IPv6格式(如 [::1]:8080)
|
||||
if strings.HasPrefix(target, "[") {
|
||||
// IPv6格式
|
||||
closeBracket := strings.LastIndex(target, "]")
|
||||
if closeBracket == -1 {
|
||||
return nil, fmt.Errorf("无效的target格式,IPv6地址格式应为 [host]:port")
|
||||
}
|
||||
host = target[1:closeBracket]
|
||||
if closeBracket+1 < len(target) && target[closeBracket+1] == ':' {
|
||||
portStr = target[closeBracket+2:]
|
||||
} else {
|
||||
portStr = "80" // 默认端口
|
||||
}
|
||||
} else {
|
||||
// 普通格式 host:port 或 host
|
||||
lastColonIndex := strings.LastIndex(target, ":")
|
||||
if lastColonIndex == -1 {
|
||||
// 没有冒号,使用默认端口80
|
||||
host = target
|
||||
portStr = "80"
|
||||
} else {
|
||||
host = target[:lastColonIndex]
|
||||
portStr = target[lastColonIndex+1:]
|
||||
// 如果端口部分为空,使用默认端口80
|
||||
if portStr == "" {
|
||||
portStr = "80"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
host := parts[0]
|
||||
port, err := strconv.Atoi(parts[1])
|
||||
var err error
|
||||
port, err = strconv.Atoi(portStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("无效的端口: %v", err)
|
||||
}
|
||||
@@ -185,4 +214,3 @@ func (t *TCPingTask) executeTCPing() map[string]interface{} {
|
||||
"ip": targetIP,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,21 +16,49 @@ func handleTCPing(c *gin.Context, url string, params map[string]interface{}) {
|
||||
seq = seqVal
|
||||
}
|
||||
|
||||
// 解析host:port格式
|
||||
parts := strings.Split(url, ":")
|
||||
if len(parts) != 2 {
|
||||
// 解析host:port格式,如果没有端口则默认80
|
||||
var host string
|
||||
var portStr string
|
||||
var port int
|
||||
|
||||
// 检查是否是IPv6格式(如 [::1]:8080)
|
||||
if strings.HasPrefix(url, "[") {
|
||||
// IPv6格式
|
||||
closeBracket := strings.LastIndex(url, "]")
|
||||
if closeBracket == -1 {
|
||||
c.JSON(200, gin.H{
|
||||
"seq": seq,
|
||||
"type": "ceTCPing",
|
||||
"url": url,
|
||||
"error": "格式错误,需要 host:port",
|
||||
"error": "格式错误,IPv6地址格式应为 [host]:port",
|
||||
})
|
||||
return
|
||||
}
|
||||
host = url[1:closeBracket]
|
||||
if closeBracket+1 < len(url) && url[closeBracket+1] == ':' {
|
||||
portStr = url[closeBracket+2:]
|
||||
} else {
|
||||
portStr = "80" // 默认端口
|
||||
}
|
||||
} else {
|
||||
// 普通格式 host:port 或 host
|
||||
lastColonIndex := strings.LastIndex(url, ":")
|
||||
if lastColonIndex == -1 {
|
||||
// 没有冒号,使用默认端口80
|
||||
host = url
|
||||
portStr = "80"
|
||||
} else {
|
||||
host = url[:lastColonIndex]
|
||||
portStr = url[lastColonIndex+1:]
|
||||
// 如果端口部分为空,使用默认端口80
|
||||
if portStr == "" {
|
||||
portStr = "80"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
host := parts[0]
|
||||
portStr := parts[1]
|
||||
port, err := strconv.Atoi(portStr)
|
||||
var err error
|
||||
port, err = strconv.Atoi(portStr)
|
||||
if err != nil {
|
||||
c.JSON(200, gin.H{
|
||||
"seq": seq,
|
||||
@@ -160,4 +188,3 @@ func handleTCPing(c *gin.Context, url string, params map[string]interface{}) {
|
||||
|
||||
c.JSON(200, result)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user