mirror of
https://github.com/oneclickvirt/backtrace.git
synced 2026-11-24 23:39:41 +08:00
feat: 结构化IPV4和IPV6的代码
This commit is contained in:
1 parent
f1ccde0ea9
commit
a832d354da
15 files changed
+381
-247
No files matched your search
+30
-29
@@ -5,6 +5,7 @@ import (
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/oneclickvirt/backtrace/model"
|
||||
. "github.com/oneclickvirt/defaultset"
|
||||
"golang.org/x/net/icmp"
|
||||
"golang.org/x/net/ipv6"
|
||||
@@ -27,7 +28,7 @@ func newPacketV6(id uint16, dst net.IP, ttl int) []byte {
|
||||
}
|
||||
|
||||
func (t *Tracer) serveIPv6(conn *ipv6.PacketConn) error {
|
||||
if EnableLoger {
|
||||
if model.EnableLoger {
|
||||
InitLogger()
|
||||
defer Logger.Sync()
|
||||
}
|
||||
@@ -36,17 +37,17 @@ func (t *Tracer) serveIPv6(conn *ipv6.PacketConn) error {
|
||||
for {
|
||||
n, cm, src, err := conn.ReadFrom(buf)
|
||||
if err != nil {
|
||||
if EnableLoger {
|
||||
if model.EnableLoger {
|
||||
Logger.Error("读取IPv6响应失败: " + err.Error())
|
||||
}
|
||||
return err
|
||||
}
|
||||
if EnableLoger {
|
||||
if model.EnableLoger {
|
||||
Logger.Info(fmt.Sprintf("收到IPv6响应: 来源=%v, 跳数=%d", src, cm.HopLimit))
|
||||
}
|
||||
fromIP := src.(*net.IPAddr).IP
|
||||
err = t.serveData(fromIP, buf[:n])
|
||||
if err != nil && EnableLoger {
|
||||
if err != nil && model.EnableLoger {
|
||||
Logger.Warn("处理IPv6数据失败: " + err.Error())
|
||||
}
|
||||
}
|
||||
@@ -54,36 +55,36 @@ func (t *Tracer) serveIPv6(conn *ipv6.PacketConn) error {
|
||||
|
||||
// traceIPv6 IPv6追踪函数
|
||||
func traceIPv6(ch chan Result, i int, offset int) {
|
||||
if EnableLoger {
|
||||
if model.EnableLoger {
|
||||
InitLogger()
|
||||
defer Logger.Sync()
|
||||
Logger.Info(fmt.Sprintf("开始追踪 %s (%s)", ipv6Names[i], ipv6s[i]))
|
||||
Logger.Info(fmt.Sprintf("开始追踪 %s (%s)", model.Ipv6Names[i], model.Ipv6s[i]))
|
||||
}
|
||||
hops, err := Trace(net.ParseIP(ipv6s[i]))
|
||||
hops, err := Trace(net.ParseIP(model.Ipv6s[i]))
|
||||
if err != nil {
|
||||
s := fmt.Sprintf("%v %-24s %v", ipv6Names[i], ipv6s[i], err)
|
||||
if EnableLoger {
|
||||
Logger.Error(fmt.Sprintf("追踪 %s (%s) 失败: %v", ipv6Names[i], ipv6s[i], err))
|
||||
s := fmt.Sprintf("%v %-24s %v", model.Ipv6Names[i], model.Ipv6s[i], err)
|
||||
if model.EnableLoger {
|
||||
Logger.Error(fmt.Sprintf("追踪 %s (%s) 失败: %v", model.Ipv6Names[i], model.Ipv6s[i], err))
|
||||
}
|
||||
ch <- Result{i + offset, s}
|
||||
return
|
||||
}
|
||||
// 记录每个hop的信息
|
||||
if EnableLoger {
|
||||
if model.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))
|
||||
model.Ipv6Names[i], model.Ipv6s[i], hopNum+1, nodeNum+1, node.IP.String(), node.RTT))
|
||||
}
|
||||
}
|
||||
}
|
||||
var asns []string
|
||||
for _, h := range hops {
|
||||
for _, n := range h.Nodes {
|
||||
asn := ipAsn(n.IP.String())
|
||||
asn := ipv6Asn(n.IP.String())
|
||||
if asn != "" {
|
||||
asns = append(asns, asn)
|
||||
if EnableLoger {
|
||||
if model.EnableLoger {
|
||||
Logger.Info(fmt.Sprintf("IP %s 对应的ASN: %s", n.IP.String(), asn))
|
||||
}
|
||||
}
|
||||
@@ -93,7 +94,7 @@ func traceIPv6(ch chan Result, i int, offset int) {
|
||||
if asns != nil && len(asns) > 0 {
|
||||
var tempText string
|
||||
asns = removeDuplicates(asns)
|
||||
tempText += fmt.Sprintf("%v ", ipv6Names[i])
|
||||
tempText += fmt.Sprintf("%v ", model.Ipv6Names[i])
|
||||
hasAS4134 := false
|
||||
hasAS4809 := false
|
||||
for _, asn := range asns {
|
||||
@@ -108,19 +109,19 @@ func traceIPv6(ch chan Result, i int, offset int) {
|
||||
if hasAS4134 && hasAS4809 {
|
||||
// 同时包含 AS4134 和 AS4809 属于 CN2GT
|
||||
asns = append([]string{"AS4809b"}, asns...)
|
||||
if EnableLoger {
|
||||
Logger.Info(fmt.Sprintf("%s (%s) 线路识别为: CN2GT", ipv6Names[i], ipv6s[i]))
|
||||
if model.EnableLoger {
|
||||
Logger.Info(fmt.Sprintf("%s (%s) 线路识别为: CN2GT", model.Ipv6Names[i], model.Ipv6s[i]))
|
||||
}
|
||||
} else if hasAS4809 {
|
||||
// 仅包含 AS4809 属于 CN2GIA
|
||||
asns = append([]string{"AS4809a"}, asns...)
|
||||
if EnableLoger {
|
||||
Logger.Info(fmt.Sprintf("%s (%s) 线路识别为: CN2GIA", ipv6Names[i], ipv6s[i]))
|
||||
if model.EnableLoger {
|
||||
Logger.Info(fmt.Sprintf("%s (%s) 线路识别为: CN2GIA", model.Ipv6Names[i], model.Ipv6s[i]))
|
||||
}
|
||||
}
|
||||
tempText += fmt.Sprintf("%-24s ", ipv6s[i])
|
||||
tempText += fmt.Sprintf("%-24s ", model.Ipv6s[i])
|
||||
for _, asn := range asns {
|
||||
asnDescription := m[asn]
|
||||
asnDescription := model.M[asn]
|
||||
switch asn {
|
||||
case "":
|
||||
continue
|
||||
@@ -152,21 +153,21 @@ 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 ", model.Ipv6Names[i]) + fmt.Sprintf("%-40s ", model.Ipv6s[i])) {
|
||||
tempText += fmt.Sprintf("%v", Red("检测不到已知线路的ASN"))
|
||||
|
||||
if EnableLoger {
|
||||
Logger.Warn(fmt.Sprintf("%s (%s) 检测不到已知线路的ASN", ipv6Names[i], ipv6s[i]))
|
||||
if model.EnableLoger {
|
||||
Logger.Warn(fmt.Sprintf("%s (%s) 检测不到已知线路的ASN", model.Ipv6Names[i], model.Ipv6s[i]))
|
||||
}
|
||||
}
|
||||
if EnableLoger {
|
||||
Logger.Info(fmt.Sprintf("%s (%s) 追踪完成,结果: %s", ipv6Names[i], ipv6s[i], tempText))
|
||||
if model.EnableLoger {
|
||||
Logger.Info(fmt.Sprintf("%s (%s) 追踪完成,结果: %s", model.Ipv6Names[i], model.Ipv6s[i], tempText))
|
||||
}
|
||||
ch <- Result{i + offset, tempText}
|
||||
} else {
|
||||
s := fmt.Sprintf("%v %-24s %v", ipv6Names[i], ipv6s[i], Red("检测不到回程路由节点的IP地址"))
|
||||
if EnableLoger {
|
||||
Logger.Warn(fmt.Sprintf("%s (%s) 检测不到回程路由节点的IP地址", ipv6Names[i], ipv6s[i]))
|
||||
s := fmt.Sprintf("%v %-24s %v", model.Ipv6Names[i], model.Ipv6s[i], Red("检测不到回程路由节点的IP地址"))
|
||||
if model.EnableLoger {
|
||||
Logger.Warn(fmt.Sprintf("%s (%s) 检测不到回程路由节点的IP地址", model.Ipv6Names[i], model.Ipv6s[i]))
|
||||
}
|
||||
ch <- Result{i + offset, s}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user