fix: 添加hop日志记录

This commit is contained in:
spiritlhl 2025-04-08 13:15:22 +00:00
parent 16484ea6ca
commit 466c8dbe5d
7 changed files with 242 additions and 207 deletions

View File

@ -24,7 +24,7 @@ jobs:
run: | run: |
git config --global user.name 'github-actions' git config --global user.name 'github-actions'
git config --global user.email 'github-actions@github.com' git config --global user.email 'github-actions@github.com'
TAG="v0.0.4-$(date +'%Y%m%d%H%M%S')" TAG="v0.0.5-$(date +'%Y%m%d%H%M%S')"
git tag $TAG git tag $TAG
git push origin $TAG git push origin $TAG
env: env:

199
bk/asn.go
View File

@ -72,87 +72,126 @@ func removeDuplicates(elements []string) []string {
return result // 返回去重后的结果切片 return result // 返回去重后的结果切片
} }
// trace IPv4追踪函数
func trace(ch chan Result, i int) { func trace(ch chan Result, i int) {
hops, err := Trace(net.ParseIP(ipv4s[i])) if EnableLoger {
if err != nil { InitLogger()
s := fmt.Sprintf("%v %-15s %v", ipv4Names[i], ipv4s[i], err) defer Logger.Sync()
ch <- Result{i, s} Logger.Info(fmt.Sprintf("开始追踪 %s (%s)", ipv4Names[i], ipv4s[i]))
return }
} hops, err := Trace(net.ParseIP(ipv4s[i]))
var asns []string if err != nil {
for _, h := range hops { s := fmt.Sprintf("%v %-15s %v", ipv4Names[i], ipv4s[i], err)
for _, n := range h.Nodes {
asn := ipAsn(n.IP.String()) if EnableLoger {
if asn != "" { Logger.Error(fmt.Sprintf("追踪 %s (%s) 失败: %v", ipv4Names[i], ipv4s[i], err))
asns = append(asns, asn) }
} ch <- Result{i, s}
} return
} }
// 处理CN2不同路线的区别 // 记录每个hop的信息
if asns != nil && len(asns) > 0 { if EnableLoger {
var tempText string for hopNum, hop := range hops {
asns = removeDuplicates(asns) for nodeNum, node := range hop.Nodes {
tempText += fmt.Sprintf("%v ", ipv4Names[i]) Logger.Info(fmt.Sprintf("追踪 %s (%s) - Hop %d, Node %d: %s (RTT: %v)",
hasAS4134 := false ipv4Names[i], ipv4s[i], hopNum+1, nodeNum+1, node.IP.String(), node.RTT))
hasAS4809 := false }
for _, asn := range asns { }
if asn == "AS4134" { }
hasAS4134 = true var asns []string
} for _, h := range hops {
if asn == "AS4809" { for _, n := range h.Nodes {
hasAS4809 = true asn := ipAsn(n.IP.String())
} if asn != "" {
} asns = append(asns, asn)
// 判断是否包含 AS4134 和 AS4809 if EnableLoger {
if hasAS4134 && hasAS4809 { Logger.Info(fmt.Sprintf("IP %s 对应的ASN: %s", n.IP.String(), asn))
// 同时包含 AS4134 和 AS4809 属于 CN2GT }
asns = append([]string{"AS4809b"}, asns...) }
} else if hasAS4809 { }
// 仅包含 AS4809 属于 CN2GIA }
asns = append([]string{"AS4809a"}, asns...) // 处理CN2不同路线的区别
} if asns != nil && len(asns) > 0 {
tempText += fmt.Sprintf("%-15s ", ipv4s[i]) var tempText string
for _, asn := range asns { asns = removeDuplicates(asns)
asnDescription := m[asn] tempText += fmt.Sprintf("%v ", ipv4Names[i])
switch asn { hasAS4134 := false
case "": hasAS4809 := false
continue for _, asn := range asns {
case "AS4809": // 被 AS4809a 和 AS4809b 替代了 if asn == "AS4134" {
continue hasAS4134 = true
case "AS9929": }
if !strings.Contains(tempText, asnDescription) { if asn == "AS4809" {
tempText += DarkGreen(asnDescription) + " " hasAS4809 = true
} }
case "AS4809a": }
if !strings.Contains(tempText, asnDescription) { // 判断是否包含 AS4134 和 AS4809
tempText += DarkGreen(asnDescription) + " " if hasAS4134 && hasAS4809 {
} // 同时包含 AS4134 和 AS4809 属于 CN2GT
case "AS23764": asns = append([]string{"AS4809b"}, asns...)
if !strings.Contains(tempText, asnDescription) { if EnableLoger {
tempText += DarkGreen(asnDescription) + " " Logger.Info(fmt.Sprintf("%s (%s) 线路识别为: CN2GT", ipv4Names[i], ipv4s[i]))
} }
case "AS4809b": } else if hasAS4809 {
if !strings.Contains(tempText, asnDescription) { // 仅包含 AS4809 属于 CN2GIA
tempText += Green(asnDescription) + " " asns = append([]string{"AS4809a"}, asns...)
} if EnableLoger {
case "AS58807": Logger.Info(fmt.Sprintf("%s (%s) 线路识别为: CN2GIA", ipv4Names[i], ipv4s[i]))
if !strings.Contains(tempText, asnDescription) { }
tempText += Green(asnDescription) + " " }
} tempText += fmt.Sprintf("%-15s ", ipv4s[i])
default: for _, asn := range asns {
if !strings.Contains(tempText, asnDescription) { asnDescription := m[asn]
tempText += White(asnDescription) + " " switch asn {
} case "":
} continue
} case "AS4809": // 被 AS4809a 和 AS4809b 替代了
if tempText == (fmt.Sprintf("%v ", ipv4Names[i]) + fmt.Sprintf("%-15s ", ipv4s[i])) { continue
tempText += fmt.Sprintf("%v", Red("检测不到已知线路的ASN")) case "AS9929":
} if !strings.Contains(tempText, asnDescription) {
ch <- Result{i, tempText} tempText += DarkGreen(asnDescription) + " "
} else { }
s := fmt.Sprintf("%v %-15s %v", ipv4Names[i], ipv4s[i], Red("检测不到回程路由节点的IP地址")) case "AS4809a":
ch <- Result{i, s} if !strings.Contains(tempText, asnDescription) {
} tempText += DarkGreen(asnDescription) + " "
}
case "AS23764":
if !strings.Contains(tempText, asnDescription) {
tempText += DarkGreen(asnDescription) + " "
}
case "AS4809b":
if !strings.Contains(tempText, asnDescription) {
tempText += Green(asnDescription) + " "
}
case "AS58807":
if !strings.Contains(tempText, asnDescription) {
tempText += Green(asnDescription) + " "
}
default:
if !strings.Contains(tempText, asnDescription) {
tempText += White(asnDescription) + " "
}
}
}
if tempText == (fmt.Sprintf("%v ", ipv4Names[i]) + fmt.Sprintf("%-15s ", ipv4s[i])) {
tempText += fmt.Sprintf("%v", Red("检测不到已知线路的ASN"))
if EnableLoger {
Logger.Warn(fmt.Sprintf("%s (%s) 检测不到已知线路的ASN", ipv4Names[i], ipv4s[i]))
}
}
if EnableLoger {
Logger.Info(fmt.Sprintf("%s (%s) 追踪完成,结果: %s", ipv4Names[i], ipv4s[i], tempText))
}
ch <- Result{i, tempText}
} else {
s := fmt.Sprintf("%v %-15s %v", ipv4Names[i], ipv4s[i], Red("检测不到回程路由节点的IP地址"))
if EnableLoger {
Logger.Warn(fmt.Sprintf("%s (%s) 检测不到回程路由节点的IP地址", ipv4Names[i], ipv4s[i]))
}
ch <- Result{i, s}
}
} }
func ipAsn(ip string) string { func ipAsn(ip string) string {

View File

@ -11,46 +11,30 @@ import (
) )
func (t *Tracer) listen(network string, laddr *net.IPAddr) (*net.IPConn, error) { func (t *Tracer) listen(network string, laddr *net.IPAddr) (*net.IPConn, error) {
if EnableLoger { conn, err := net.ListenIP(network, laddr)
InitLogger() if err != nil {
defer Logger.Sync() if EnableLoger {
conn, err := net.ListenIP(network, laddr) Logger.Info(err.Error())
if err != nil { }
Logger.Info(err.Error()) return nil, err
return nil, err }
} raw, err := conn.SyscallConn()
raw, err := conn.SyscallConn() if err != nil {
if err != nil { if EnableLoger {
Logger.Info(err.Error()) Logger.Info(err.Error())
conn.Close() }
return nil, err conn.Close()
} return nil, err
_ = raw.Control(func(fd uintptr) { }
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1) _ = raw.Control(func(fd uintptr) {
}) err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1)
if err != nil { })
Logger.Info(err.Error()) if err != nil {
conn.Close() if EnableLoger {
return nil, err Logger.Info(err.Error())
} }
return conn, nil conn.Close()
} else { return nil, err
conn, err := net.ListenIP(network, laddr) }
if err != nil { return conn, nil
return nil, err
}
raw, err := conn.SyscallConn()
if err != nil {
conn.Close()
return nil, err
}
_ = raw.Control(func(fd uintptr) {
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1)
})
if err != nil {
conn.Close()
return nil, err
}
return conn, nil
}
} }

View File

@ -14,43 +14,31 @@ func (t *Tracer) listen(network string, laddr *net.IPAddr) (*net.IPConn, error)
if EnableLoger { if EnableLoger {
InitLogger() InitLogger()
defer Logger.Sync() defer Logger.Sync()
conn, err := net.ListenIP(network, laddr)
if err != nil {
Logger.Info(err.Error())
return nil, err
}
raw, err := conn.SyscallConn()
if err != nil {
Logger.Info(err.Error())
conn.Close()
return nil, err
}
_ = raw.Control(func(fd uintptr) {
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1)
})
if err != nil {
Logger.Info(err.Error())
conn.Close()
return nil, err
}
return conn, nil
} else {
conn, err := net.ListenIP(network, laddr)
if err != nil {
return nil, err
}
raw, err := conn.SyscallConn()
if err != nil {
conn.Close()
return nil, err
}
_ = raw.Control(func(fd uintptr) {
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1)
})
if err != nil {
conn.Close()
return nil, err
}
return conn, nil
} }
conn, err := net.ListenIP(network, laddr)
if err != nil {
if EnableLoger {
Logger.Info(err.Error())
}
return nil, err
}
raw, err := conn.SyscallConn()
if err != nil {
if EnableLoger {
Logger.Info(err.Error())
}
conn.Close()
return nil, err
}
_ = raw.Control(func(fd uintptr) {
err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_HDRINCL, 1)
})
if err != nil {
if EnableLoger {
Logger.Info(err.Error())
}
conn.Close()
return nil, err
}
return conn, nil
} }

View File

@ -11,46 +11,34 @@ import (
) )
func (t *Tracer) listen(network string, laddr *net.IPAddr) (*net.IPConn, error) { func (t *Tracer) listen(network string, laddr *net.IPAddr) (*net.IPConn, error) {
if EnableLoger { if EnableLoger {
InitLogger() InitLogger()
defer Logger.Sync() defer Logger.Sync()
conn, err := net.ListenIP(network, laddr) }
if err != nil { conn, err := net.ListenIP(network, laddr)
Logger.Info(err.Error()) if err != nil {
return nil, err if EnableLoger {
} Logger.Info(err.Error())
raw, err := conn.SyscallConn() }
if err != nil { return nil, err
Logger.Info(err.Error()) }
conn.Close() raw, err := conn.SyscallConn()
return nil, err if err != nil {
} if EnableLoger {
_ = raw.Control(func(fd uintptr) { Logger.Info(err.Error())
err = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, windows.IP_HDRINCL, 1) }
}) conn.Close()
if err != nil { return nil, err
Logger.Info(err.Error()) }
conn.Close() _ = raw.Control(func(fd uintptr) {
return nil, err err = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, windows.IP_HDRINCL, 1)
} })
return conn, nil if err != nil {
} else { if EnableLoger {
conn, err := net.ListenIP(network, laddr) Logger.Info(err.Error())
if err != nil { }
return nil, err conn.Close()
} return nil, err
raw, err := conn.SyscallConn() }
if err != nil { return conn, nil
conn.Close()
return nil, err
}
_ = raw.Control(func(fd uintptr) {
err = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, windows.IP_HDRINCL, 1)
})
if err != nil {
conn.Close()
return nil, err
}
return conn, nil
}
} }

View File

@ -54,20 +54,40 @@ func (t *Tracer) serveIPv6(conn *ipv6.PacketConn) error {
} }
} }
// IPv6追踪函数 // traceIPv6 IPv6追踪函数
func traceIPv6(ch chan Result, i int, offset int) { func traceIPv6(ch chan Result, i int, offset int) {
if EnableLoger {
InitLogger()
defer Logger.Sync()
Logger.Info(fmt.Sprintf("开始追踪 %s (%s)", ipv6Names[i], ipv6s[i]))
}
hops, err := Trace(net.ParseIP(ipv6s[i])) hops, err := Trace(net.ParseIP(ipv6s[i]))
if err != nil { if err != nil {
s := fmt.Sprintf("%v %-40s %v", ipv6Names[i], ipv6s[i], err) s := fmt.Sprintf("%v %-40s %v", ipv6Names[i], ipv6s[i], err)
if EnableLoger {
Logger.Error(fmt.Sprintf("追踪 %s (%s) 失败: %v", ipv6Names[i], ipv6s[i], err))
}
ch <- Result{i + offset, s} ch <- Result{i + offset, s}
return return
} }
// 记录每个hop的信息
if EnableLoger {
for hopNum, hop := range hops {
for nodeNum, node := range hop.Nodes {
Logger.Info(fmt.Sprintf("追踪 %s (%s) - Hop %d, Node %d: %s (RTT: %v)",
ipv6Names[i], ipv6s[i], hopNum+1, nodeNum+1, node.IP.String(), node.RTT))
}
}
}
var asns []string var asns []string
for _, h := range hops { for _, h := range hops {
for _, n := range h.Nodes { for _, n := range h.Nodes {
asn := ipAsn(n.IP.String()) asn := ipAsn(n.IP.String())
if asn != "" { if asn != "" {
asns = append(asns, asn) asns = append(asns, asn)
if EnableLoger {
Logger.Info(fmt.Sprintf("IP %s 对应的ASN: %s", n.IP.String(), asn))
}
} }
} }
} }
@ -90,9 +110,15 @@ func traceIPv6(ch chan Result, i int, offset int) {
if hasAS4134 && hasAS4809 { if hasAS4134 && hasAS4809 {
// 同时包含 AS4134 和 AS4809 属于 CN2GT // 同时包含 AS4134 和 AS4809 属于 CN2GT
asns = append([]string{"AS4809b"}, asns...) asns = append([]string{"AS4809b"}, asns...)
if EnableLoger {
Logger.Info(fmt.Sprintf("%s (%s) 线路识别为: CN2GT", ipv6Names[i], ipv6s[i]))
}
} else if hasAS4809 { } else if hasAS4809 {
// 仅包含 AS4809 属于 CN2GIA // 仅包含 AS4809 属于 CN2GIA
asns = append([]string{"AS4809a"}, asns...) asns = append([]string{"AS4809a"}, asns...)
if EnableLoger {
Logger.Info(fmt.Sprintf("%s (%s) 线路识别为: CN2GIA", ipv6Names[i], ipv6s[i]))
}
} }
tempText += fmt.Sprintf("%-40s ", ipv6s[i]) tempText += fmt.Sprintf("%-40s ", ipv6s[i])
for _, asn := range asns { for _, asn := range asns {
@ -130,10 +156,20 @@ func traceIPv6(ch chan Result, i int, offset int) {
} }
if tempText == (fmt.Sprintf("%v ", ipv6Names[i]) + fmt.Sprintf("%-40s ", ipv6s[i])) { if tempText == (fmt.Sprintf("%v ", ipv6Names[i]) + fmt.Sprintf("%-40s ", ipv6s[i])) {
tempText += fmt.Sprintf("%v", Red("检测不到已知线路的ASN")) tempText += fmt.Sprintf("%v", Red("检测不到已知线路的ASN"))
if EnableLoger {
Logger.Warn(fmt.Sprintf("%s (%s) 检测不到已知线路的ASN", ipv6Names[i], ipv6s[i]))
}
}
if EnableLoger {
Logger.Info(fmt.Sprintf("%s (%s) 追踪完成,结果: %s", ipv6Names[i], ipv6s[i], tempText))
} }
ch <- Result{i + offset, tempText} ch <- Result{i + offset, tempText}
} else { } else {
s := fmt.Sprintf("%v %-40s %v", ipv6Names[i], ipv6s[i], Red("检测不到回程路由节点的IP地址")) s := fmt.Sprintf("%v %-40s %v", ipv6Names[i], ipv6s[i], Red("检测不到回程路由节点的IP地址"))
if EnableLoger {
Logger.Warn(fmt.Sprintf("%s (%s) 检测不到回程路由节点的IP地址", ipv6Names[i], ipv6s[i]))
}
ch <- Result{i + offset, s} ch <- Result{i + offset, s}
} }
} }

View File

@ -1,5 +1,5 @@
package backtrace package backtrace
const BackTraceVersion = "v0.0.4" const BackTraceVersion = "v0.0.5"
var EnableLoger = false var EnableLoger = false