This commit is contained in:
2025-11-21 18:35:31 +08:00
parent 27806421f7
commit 1402473d20
3 changed files with 62 additions and 18 deletions

View File

@@ -2,6 +2,7 @@ package handler
import (
"encoding/base64"
"fmt"
"net"
"os/exec"
"strings"
@@ -90,7 +91,7 @@ func handleDns(c *gin.Context, url string, params map[string]interface{}) {
// 解析dig输出行格式如example.com. 300 IN A 192.168.1.1
parts := strings.Fields(line)
if len(parts) >= 5 {
recordType := parts[3] // IN
// parts[3] 是 "IN" (record type)parts[4] 是记录类型 (A, AAAA, CNAME等)
recordClass := parts[4] // A, AAAA, CNAME等
recordValue := ""
if len(parts) > 5 {

View File

@@ -9,6 +9,12 @@ import (
)
func handleFindPing(c *gin.Context, url string, params map[string]interface{}) {
// 获取seq参数
seq := ""
if seqVal, ok := params["seq"].(string); ok {
seq = seqVal
}
// url应该是CIDR格式如 8.8.8.0/24
cidr := url
if cidrParam, ok := params["cidr"].(string); ok && cidrParam != "" {
@@ -19,8 +25,9 @@ func handleFindPing(c *gin.Context, url string, params map[string]interface{}) {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
c.JSON(200, gin.H{
"type": "ceFindPing",
"error": "无效的CIDR格式",
"seq": seq,
"type": "ceFindPing",
"error": "无效的CIDR格式",
})
return
}
@@ -65,11 +72,12 @@ func handleFindPing(c *gin.Context, url string, params map[string]interface{}) {
wg.Wait()
c.JSON(200, gin.H{
"type": "ceFindPing",
"cidr": cidr,
"alive_ips": aliveIPs,
"seq": seq,
"type": "ceFindPing",
"cidr": cidr,
"alive_ips": aliveIPs,
"alive_count": len(aliveIPs),
"total_ips": len(ipList),
"total_ips": len(ipList),
})
}

View File

@@ -10,13 +10,20 @@ import (
)
func handleTCPing(c *gin.Context, url string, params map[string]interface{}) {
// 获取seq参数
seq := ""
if seqVal, ok := params["seq"].(string); ok {
seq = seqVal
}
// 解析host:port格式
parts := strings.Split(url, ":")
if len(parts) != 2 {
c.JSON(200, gin.H{
"type": "ceTCPing",
"url": url,
"error": "格式错误,需要 host:port",
"seq": seq,
"type": "ceTCPing",
"url": url,
"error": "格式错误,需要 host:port",
})
return
}
@@ -26,13 +33,30 @@ func handleTCPing(c *gin.Context, url string, params map[string]interface{}) {
port, err := strconv.Atoi(portStr)
if err != nil {
c.JSON(200, gin.H{
"type": "ceTCPing",
"url": url,
"error": "端口格式错误",
"seq": seq,
"type": "ceTCPing",
"url": url,
"error": "端口格式错误",
})
return
}
// 解析hostname获取IP
var primaryIP string
ips, err := net.LookupIP(host)
if err == nil && len(ips) > 0 {
// 优先使用IPv4
for _, ip := range ips {
if ip.To4() != nil {
primaryIP = ip.String()
break
}
}
if primaryIP == "" && len(ips) > 0 {
primaryIP = ips[0].String()
}
}
// 执行TCP连接测试
start := time.Now()
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, portStr), 5*time.Second)
@@ -40,20 +64,31 @@ func handleTCPing(c *gin.Context, url string, params map[string]interface{}) {
if err != nil {
c.JSON(200, gin.H{
"type": "ceTCPing",
"url": url,
"host": host,
"port": port,
"seq": seq,
"type": "ceTCPing",
"url": url,
"ip": primaryIP,
"host": host,
"port": port,
"latency": -1,
"error": err.Error(),
"error": err.Error(),
})
return
}
defer conn.Close()
// 如果之前没有获取到IP从连接中获取
if primaryIP == "" {
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
primaryIP = addr.IP.String()
}
}
c.JSON(200, gin.H{
"seq": seq,
"type": "ceTCPing",
"url": url,
"ip": primaryIP,
"host": host,
"port": port,
"latency": latency,