mirror of
https://github.com/oneclickvirt/backtrace.git
synced 2025-04-22 04:02:07 +08:00
fix: 修复备用地址启用的逻辑
This commit is contained in:
parent
a268e0b48b
commit
6f0fc98154
@ -38,6 +38,23 @@ func newPacketV4(id uint16, dst net.IP, ttl int) []byte {
|
|||||||
return append(buf, p...)
|
return append(buf, p...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// extractIpv4ASNsFromHops 从跃点中提取ASN列表
|
||||||
|
func extractIpv4ASNsFromHops(hops []*Hop, enableLogger bool) []string {
|
||||||
|
var asns []string
|
||||||
|
for _, h := range hops {
|
||||||
|
for _, n := range h.Nodes {
|
||||||
|
asn := ipv4Asn(n.IP.String())
|
||||||
|
if asn != "" {
|
||||||
|
asns = append(asns, asn)
|
||||||
|
if enableLogger {
|
||||||
|
Logger.Info(fmt.Sprintf("IP %s 对应的ASN: %s", n.IP.String(), asn))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return asns
|
||||||
|
}
|
||||||
|
|
||||||
// trace IPv4追踪函数
|
// trace IPv4追踪函数
|
||||||
func trace(ch chan Result, i int) {
|
func trace(ch chan Result, i int) {
|
||||||
if model.EnableLoger {
|
if model.EnableLoger {
|
||||||
@ -47,21 +64,6 @@ func trace(ch chan Result, i int) {
|
|||||||
}
|
}
|
||||||
// 先尝试原始IP地址
|
// 先尝试原始IP地址
|
||||||
hops, err := Trace(net.ParseIP(model.Ipv4s[i]))
|
hops, err := Trace(net.ParseIP(model.Ipv4s[i]))
|
||||||
if err != nil || len(hops) == 0 {
|
|
||||||
// 如果失败,尝试从IcmpTargets获取备选IP
|
|
||||||
if tryAltIPs := tryAlternativeIPs(model.Ipv4Names[i], "v4"); len(tryAltIPs) > 0 {
|
|
||||||
for _, altIP := range tryAltIPs {
|
|
||||||
if model.EnableLoger {
|
|
||||||
Logger.Info(fmt.Sprintf("尝试备选IP %s 追踪 %s", altIP, model.Ipv4Names[i]))
|
|
||||||
}
|
|
||||||
hops, err = Trace(net.ParseIP(altIP))
|
|
||||||
if err == nil && len(hops) > 0 {
|
|
||||||
break // 成功找到可用IP
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 如果所有尝试都失败
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s := fmt.Sprintf("%v %-15s %v", model.Ipv4Names[i], model.Ipv4s[i], Red("检测不到回程路由节点的IP地址"))
|
s := fmt.Sprintf("%v %-15s %v", model.Ipv4Names[i], model.Ipv4s[i], Red("检测不到回程路由节点的IP地址"))
|
||||||
if model.EnableLoger {
|
if model.EnableLoger {
|
||||||
@ -69,6 +71,27 @@ func trace(ch chan Result, i int) {
|
|||||||
}
|
}
|
||||||
ch <- Result{i, s}
|
ch <- Result{i, s}
|
||||||
}
|
}
|
||||||
|
asns := extractIpv4ASNsFromHops(hops, model.EnableLoger)
|
||||||
|
// 如果没有找到ASN,尝试备选IP
|
||||||
|
if len(asns) == 0 {
|
||||||
|
// 尝试从IcmpTargets获取备选IP
|
||||||
|
if tryAltIPs := tryAlternativeIPs(model.Ipv4Names[i], "v4"); len(tryAltIPs) > 0 {
|
||||||
|
for _, altIP := range tryAltIPs {
|
||||||
|
if model.EnableLoger {
|
||||||
|
Logger.Info(fmt.Sprintf("尝试备选IP %s 追踪 %s", altIP, model.Ipv4Names[i]))
|
||||||
|
}
|
||||||
|
hops, err = Trace(net.ParseIP(altIP))
|
||||||
|
if err == nil && len(hops) > 0 {
|
||||||
|
newAsns := extractIpv4ASNsFromHops(hops, model.EnableLoger)
|
||||||
|
asns = append(asns, newAsns...)
|
||||||
|
if len(newAsns) > 0 {
|
||||||
|
break // 成功找到可用IP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
asns = removeDuplicates(asns)
|
||||||
// 记录每个hop的信息
|
// 记录每个hop的信息
|
||||||
if model.EnableLoger {
|
if model.EnableLoger {
|
||||||
for hopNum, hop := range hops {
|
for hopNum, hop := range hops {
|
||||||
@ -78,20 +101,8 @@ func trace(ch chan Result, i int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var asns []string
|
// 处理不同线路
|
||||||
for _, h := range hops {
|
if len(asns) > 0 {
|
||||||
for _, n := range h.Nodes {
|
|
||||||
asn := ipv4Asn(n.IP.String())
|
|
||||||
if asn != "" {
|
|
||||||
asns = append(asns, asn)
|
|
||||||
if model.EnableLoger {
|
|
||||||
Logger.Info(fmt.Sprintf("IP %s 对应的ASN: %s", n.IP.String(), asn))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 处理CN2不同路线的区别
|
|
||||||
if asns != nil && len(asns) > 0 {
|
|
||||||
var tempText string
|
var tempText string
|
||||||
asns = removeDuplicates(asns)
|
asns = removeDuplicates(asns)
|
||||||
tempText += fmt.Sprintf("%v ", model.Ipv4Names[i])
|
tempText += fmt.Sprintf("%v ", model.Ipv4Names[i])
|
||||||
|
@ -53,6 +53,24 @@ func (t *Tracer) serveIPv6(conn *ipv6.PacketConn) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// extractIpv6ASNsFromHops 从跃点中提取ASN列表
|
||||||
|
func extractIpv6ASNsFromHops(hops []*Hop, enableLogger bool) []string {
|
||||||
|
var asns []string
|
||||||
|
for _, h := range hops {
|
||||||
|
for _, n := range h.Nodes {
|
||||||
|
asn := ipv6Asn(n.IP.String())
|
||||||
|
if asn != "" {
|
||||||
|
asns = append(asns, asn)
|
||||||
|
if enableLogger {
|
||||||
|
Logger.Info(fmt.Sprintf("IP %s 对应的ASN: %s", n.IP.String(), asn))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return asns
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// traceIPv6 IPv6追踪函数
|
// traceIPv6 IPv6追踪函数
|
||||||
func traceIPv6(ch chan Result, i int, offset int) {
|
func traceIPv6(ch chan Result, i int, offset int) {
|
||||||
if model.EnableLoger {
|
if model.EnableLoger {
|
||||||
@ -62,21 +80,6 @@ func traceIPv6(ch chan Result, i int, offset int) {
|
|||||||
}
|
}
|
||||||
// 先尝试原始IP地址
|
// 先尝试原始IP地址
|
||||||
hops, err := Trace(net.ParseIP(model.Ipv6s[i]))
|
hops, err := Trace(net.ParseIP(model.Ipv6s[i]))
|
||||||
if err != nil || len(hops) == 0 {
|
|
||||||
// 如果失败,尝试从IcmpTargets获取备选IP
|
|
||||||
if tryAltIPs := tryAlternativeIPs(model.Ipv6Names[i], "v6"); len(tryAltIPs) > 0 {
|
|
||||||
for _, altIP := range tryAltIPs {
|
|
||||||
if model.EnableLoger {
|
|
||||||
Logger.Info(fmt.Sprintf("尝试备选IP %s 追踪 %s", altIP, model.Ipv6Names[i]))
|
|
||||||
}
|
|
||||||
hops, err = Trace(net.ParseIP(altIP))
|
|
||||||
if err == nil && len(hops) > 0 {
|
|
||||||
break // 成功找到可用IP
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 如果所有尝试都失败
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s := fmt.Sprintf("%v %-24s %v", model.Ipv6Names[i], model.Ipv6s[i], Red("检测不到回程路由节点的IP地址"))
|
s := fmt.Sprintf("%v %-24s %v", model.Ipv6Names[i], model.Ipv6s[i], Red("检测不到回程路由节点的IP地址"))
|
||||||
if model.EnableLoger {
|
if model.EnableLoger {
|
||||||
@ -84,6 +87,27 @@ func traceIPv6(ch chan Result, i int, offset int) {
|
|||||||
}
|
}
|
||||||
ch <- Result{i + offset, s}
|
ch <- Result{i + offset, s}
|
||||||
}
|
}
|
||||||
|
asns := extractIpv6ASNsFromHops(hops, model.EnableLoger)
|
||||||
|
// 如果没有找到ASN,尝试备选IP
|
||||||
|
if len(asns) == 0 {
|
||||||
|
// 尝试从IcmpTargets获取备选IP
|
||||||
|
if tryAltIPs := tryAlternativeIPs(model.Ipv4Names[i], "v4"); len(tryAltIPs) > 0 {
|
||||||
|
for _, altIP := range tryAltIPs {
|
||||||
|
if model.EnableLoger {
|
||||||
|
Logger.Info(fmt.Sprintf("尝试备选IP %s 追踪 %s", altIP, model.Ipv4Names[i]))
|
||||||
|
}
|
||||||
|
hops, err = Trace(net.ParseIP(altIP))
|
||||||
|
if err == nil && len(hops) > 0 {
|
||||||
|
newAsns := extractIpv6ASNsFromHops(hops, model.EnableLoger)
|
||||||
|
asns = append(asns, newAsns...)
|
||||||
|
if len(newAsns) > 0 {
|
||||||
|
break // 成功找到可用IP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
asns = removeDuplicates(asns)
|
||||||
// 记录每个hop的信息
|
// 记录每个hop的信息
|
||||||
if model.EnableLoger {
|
if model.EnableLoger {
|
||||||
for hopNum, hop := range hops {
|
for hopNum, hop := range hops {
|
||||||
@ -93,20 +117,8 @@ func traceIPv6(ch chan Result, i int, offset int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var asns []string
|
// 处理不同线路
|
||||||
for _, h := range hops {
|
if len(asns) > 0 {
|
||||||
for _, n := range h.Nodes {
|
|
||||||
asn := ipv6Asn(n.IP.String())
|
|
||||||
if asn != "" {
|
|
||||||
asns = append(asns, asn)
|
|
||||||
if model.EnableLoger {
|
|
||||||
Logger.Info(fmt.Sprintf("IP %s 对应的ASN: %s", n.IP.String(), asn))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 处理路由信息
|
|
||||||
if asns != nil && len(asns) > 0 {
|
|
||||||
var tempText string
|
var tempText string
|
||||||
asns = removeDuplicates(asns)
|
asns = removeDuplicates(asns)
|
||||||
tempText += fmt.Sprintf("%v ", model.Ipv6Names[i])
|
tempText += fmt.Sprintf("%v ", model.Ipv6Names[i])
|
||||||
|
19
bk/utils.go
19
bk/utils.go
@ -17,16 +17,17 @@ type Result struct {
|
|||||||
s string
|
s string
|
||||||
}
|
}
|
||||||
|
|
||||||
// removeDuplicates 切片元素去重
|
// removeDuplicates 切片去重
|
||||||
func removeDuplicates(elements []string) []string {
|
func removeDuplicates(elements []string) []string {
|
||||||
encountered := map[string]bool{}
|
if elements == nil {
|
||||||
result := []string{}
|
return nil
|
||||||
for v := range elements {
|
}
|
||||||
if encountered[elements[v]] {
|
seen := make(map[string]struct{})
|
||||||
// 存在过就不加入了
|
var result []string
|
||||||
} else {
|
for _, v := range elements {
|
||||||
encountered[elements[v]] = true
|
if _, ok := seen[v]; !ok {
|
||||||
result = append(result, elements[v])
|
seen[v] = struct{}{}
|
||||||
|
result = append(result, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
Loading…
Reference in New Issue
Block a user