From bdfba172d190bab272bc048d5a1493c6cb191f24 Mon Sep 17 00:00:00 2001 From: tsosunchia <59512455+tsosunchia@users.noreply.github.com> Date: Fri, 3 Mar 2023 12:02:48 +0800 Subject: [PATCH] revert --- README.md | 11 ++++-- README_zh_CN.md | 10 +++-- cmd/cmd.go | 20 +++++----- dn42/geofeed.go | 7 +--- dn42/ptr.go | 7 +--- ipgeo/ip2region.go | 14 +------ ipgeo/ipinfo.go | 7 +--- ipgeo/leo.go | 5 +-- printer/basic.go | 20 ++-------- printer/realtime_printer.go | 56 ++++++---------------------- printer/realtime_printer_router.go | 60 ++++++------------------------ trace/icmp_ipv4.go | 24 ++---------- trace/icmp_ipv6.go | 24 ++---------- trace/tcp_ipv4.go | 19 ++-------- trace/tcp_ipv6.go | 25 ++----------- trace/udp.go | 19 ++-------- tracemap/tracemap.go | 5 +-- util/util.go | 19 ++-------- 18 files changed, 80 insertions(+), 272 deletions(-) diff --git a/README.md b/README.md index 68c2c25..a5b3497 100644 --- a/README.md +++ b/README.md @@ -127,13 +127,16 @@ nexttrace --route-path www.time.com.my ```bash # You can specify the IP database by yourself [IP-API.com here], if not specified, LeoMoeAPI will be used nexttrace --data-provider ip-api.com -## Note that the `ipinfo` and `IPInsight` API needs users to purchase services from them. -## If necessary, you can clone this project, add the token provided by `ipinfo` or `IPInsight` and compile it yourself -## Note For the offline database IPInfoLocal, please download it yourself to the same directory as NextTrace and rename it to ipinfoLocal.mmdb. (You can download it from here: https://ipinfo.io/signup?ref=free-database-downloads) -## For the offline database Ip2region, you can download it manually to the same directory as NextTrace and rename it to ip2region.db, or let NextTrace download it automatically. +## Note There are frequency limits for free queries of the ipinfo and IPInsight APIs. You can purchase services from these providers to remove the limits. +## If necessary, you can clone this project, add the token provided by ipinfo or IPInsight and compile it yourself +## Note For the offline database IPInfoLocal, please download it manually and rename it to ipinfoLocal.mmdb. (You can download it from here: https://ipinfo.io/signup?ref=free-database-downloads) +## For the offline database Ip2region, you can download it manually and rename it to ip2region.db, or let NextTrace download it automatically. ## Fill the token to: ipgeo/tokens.go ## Please be aware: Due to the serious abuse of IP.SB, you will often be not able to query IP data from this source ## IP-API.com has a stricter restiction on API calls, if you can't query IP data from this source, please try again in a few minutes. + +# You can also specify the default IP database by setting an environment variable +export NEXTTRACE_DATAPROVIDER=ipinfo ``` `NextTrace` supports mixed parameters and shortened parameters diff --git a/README_zh_CN.md b/README_zh_CN.md index 9c71641..478275b 100644 --- a/README_zh_CN.md +++ b/README_zh_CN.md @@ -146,13 +146,15 @@ nexttrace --route-path www.time.com.my ```bash # 可以自行指定IP数据库[此处为IP-API.com],不指定则默认为LeoMoeAPI nexttrace --data-provider ip-api.com -## 特别的: 其中 `ipinfo` 和 `IPInsight` API 需要从这些服务商自行购买服务,如有需要可以 clone 本项目添加其提供的 token 自行编译 +## 特别的: 其中 ipinfo 和 IPInsight API 对于免费版查询有频率限制,可从这些服务商自行购买服务以解除限制,如有需要可以 clone 本项目添加其提供的 token 自行编译 ## TOKEN填写路径:ipgeo/tokens.go -## 特别的: 对于离线库`IPInfoLocal`,请自行下载到与NextTrace同目录下,并命名为`ipinfoLocal.mmdb` (可以从这里下载:https://ipinfo.io/signup?ref=free-database-downloads) -## 对于离线库`Ip2region`可NextTrace自动下载,也可自行下载到与NextTrace同目录下,并命名为`ip2region.db` - +## 特别的: 对于离线库 IPInfoLocal,请自行下载并命名为 ipinfoLocal.mmdb (可以从这里下载:https://ipinfo.io/signup?ref=free-database-downloads) +## 对于离线库 Ip2region 可NextTrace自动下载,也可自行下载并命名为 ip2region.db ## 另外:由于IP.SB被滥用比较严重,会经常出现无法查询的问题,请知悉。 ## IP-API.com限制调用较为严格,如有查询不到的情况,请几分钟后再试。 + +# 也可以通过设置环境变量来指定默认IP数据库 +export NEXTTRACE_DATAPROVIDER=ipinfo ``` `NextTrace`支持使用混合参数和简略参数 diff --git a/cmd/cmd.go b/cmd/cmd.go index 688fca1..1ef4d28 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -141,15 +141,17 @@ func Excute() { } if strings.ToUpper(*dataOrigin) == "LEOMOEAPI" { - w := wshandle.New() - w.Interrupt = make(chan os.Signal, 1) - signal.Notify(w.Interrupt, os.Interrupt) - defer func() { - err := w.Conn.Close() - if err != nil { - return - } - }() + val, ok := os.LookupEnv("NEXTTRACE_DATAPROVIDER") + if ok { + *dataOrigin = val + } else { + w := wshandle.New() + w.Interrupt = make(chan os.Signal, 1) + signal.Notify(w.Interrupt, os.Interrupt) + defer func() { + w.Conn.Close() + }() + } } printer.PrintTraceRouteNav(ip, domain, *dataOrigin) diff --git a/dn42/geofeed.go b/dn42/geofeed.go index 118de38..7b183f6 100644 --- a/dn42/geofeed.go +++ b/dn42/geofeed.go @@ -37,12 +37,7 @@ func ReadGeoFeed() ([]GeoFeedRow, error) { if err != nil { return nil, err } - defer func(f *os.File) { - err := f.Close() - if err != nil { - panic(err) - } - }(f) + defer f.Close() r := csv.NewReader(f) rows, err := r.ReadAll() diff --git a/dn42/ptr.go b/dn42/ptr.go index 834d029..c470ff9 100644 --- a/dn42/ptr.go +++ b/dn42/ptr.go @@ -36,12 +36,7 @@ func FindPtrRecord(ptr string) (PtrRow, error) { if err != nil { return PtrRow{}, err } - defer func(f *os.File) { - err := f.Close() - if err != nil { - panic(err) - } - }(f) + defer f.Close() r := csv.NewReader(f) rows, err := r.ReadAll() diff --git a/ipgeo/ip2region.go b/ipgeo/ip2region.go index 1f9e3e9..41ae0c0 100644 --- a/ipgeo/ip2region.go +++ b/ipgeo/ip2region.go @@ -22,24 +22,14 @@ func downloadDataBase() error { if err != nil { return err } - defer func(Body io.ReadCloser) { - err := Body.Close() - if err != nil { - panic(err) - } - }(resp.Body) + defer resp.Body.Close() // Create the file out, err := os.Create(ipDataBasePath) if err != nil { return err } - defer func(out *os.File) { - err := out.Close() - if err != nil { - panic(err) - } - }(out) + defer out.Close() // Write the body to file _, err = io.Copy(out, resp.Body) diff --git a/ipgeo/ipinfo.go b/ipgeo/ipinfo.go index bfb26ac..6576a1b 100644 --- a/ipgeo/ipinfo.go +++ b/ipgeo/ipinfo.go @@ -13,12 +13,7 @@ func IPInfo(ip string) (*IPGeoData, error) { if err != nil { return nil, err } - defer func(Body io.ReadCloser) { - err := Body.Close() - if err != nil { - panic(err) - } - }(resp.Body) + defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return nil, err diff --git a/ipgeo/leo.go b/ipgeo/leo.go index 0f9f020..efdcc3f 100644 --- a/ipgeo/leo.go +++ b/ipgeo/leo.go @@ -55,10 +55,7 @@ func receiveParse() { } m := make(map[string][]string) - err := json.Unmarshal([]byte(res.Get("router").String()), &m) - if err != nil { - return - } + json.Unmarshal([]byte(res.Get("router").String()), &m) lat, _ := strconv.ParseFloat(res.Get("lat").String(), 32) lng, _ := strconv.ParseFloat(res.Get("lng").String(), 32) diff --git a/printer/basic.go b/printer/basic.go index 75d2110..d3f4a8f 100644 --- a/printer/basic.go +++ b/printer/basic.go @@ -12,19 +12,16 @@ var buildDate = "" var commitID = "" func Version() { - _, err := fmt.Fprintf(color.Output, "%s %s %s %s\n", + fmt.Fprintf(color.Output, "%s %s %s %s\n", color.New(color.FgWhite, color.Bold).Sprintf("%s", "NextTrace"), color.New(color.FgHiBlack, color.Bold).Sprintf("%s", version), color.New(color.FgHiBlack, color.Bold).Sprintf("%s", buildDate), color.New(color.FgHiBlack, color.Bold).Sprintf("%s", commitID), ) - if err != nil { - panic(err) - } } func CopyRight() { - _, err := fmt.Fprintf(color.Output, "\n%s\n%s\n%s %s\n\n%s\n%s %s\n%s %s\n%s %s\n\n%s\n%s\n%s %s\n\n", + fmt.Fprintf(color.Output, "\n%s\n%s\n%s %s\n\n%s\n%s %s\n%s %s\n%s %s\n\n%s\n%s\n%s %s\n\n", color.New(color.FgCyan, color.Bold).Sprintf("%s", "NextTrace CopyRight"), color.New(color.FgGreen, color.Bold).Sprintf("%s", "NextTrace Project Creator"), color.New(color.FgWhite, color.Bold).Sprintf("%s", "Leo"), @@ -41,36 +38,27 @@ func CopyRight() { color.New(color.FgWhite, color.Bold).Sprintf("%s", "zhshch"), color.New(color.FgHiBlack, color.Bold).Sprintf("%s", "zhshch@athorx.com"), ) - if err != nil { - return - } MoeQingOrgCopyRight() PluginCopyRight() } func MoeQingOrgCopyRight() { - _, err := fmt.Fprintf(color.Output, "%s\n%s %s\n%s %s\n\n", + fmt.Fprintf(color.Output, "%s\n%s %s\n%s %s\n\n", color.New(color.FgHiYellow, color.Bold).Sprintf("%s", "MoeQing Network"), color.New(color.FgWhite, color.Bold).Sprintf("%s", "YekongTAT"), color.New(color.FgHiBlack, color.Bold).Sprintf("%s", "yekongtat@gmail.com"), color.New(color.FgWhite, color.Bold).Sprintf("%s", "Haima"), color.New(color.FgHiBlack, color.Bold).Sprintf("%s", "haima@peers.cloud"), ) - if err != nil { - return - } } func PluginCopyRight() { - _, err := fmt.Fprintf(color.Output, "%s\n%s %s\n\n", + fmt.Fprintf(color.Output, "%s\n%s %s\n\n", color.New(color.FgGreen, color.Bold).Sprintf("%s", "NextTrace Map Plugin Author"), color.New(color.FgWhite, color.Bold).Sprintf("%s", "Tso"), color.New(color.FgHiBlack, color.Bold).Sprintf("%s", "tsosunchia@gmail.com"), ) - if err != nil { - return - } } func PrintTraceRouteNav(ip net.IP, domain string, dataOrigin string) { diff --git a/printer/realtime_printer.go b/printer/realtime_printer.go index bc9e66f..23bbbde 100644 --- a/printer/realtime_printer.go +++ b/printer/realtime_printer.go @@ -39,12 +39,9 @@ func RealtimePrinter(res *trace.Result, ttl int) { } if latestIP == "" { - _, err := fmt.Fprintf(color.Output, "%s\n", + fmt.Fprintf(color.Output, "%s\n", color.New(color.FgWhite, color.Bold).Sprintf("*"), ) - if err != nil { - return - } return } @@ -54,19 +51,13 @@ func RealtimePrinter(res *trace.Result, ttl int) { fmt.Printf("%4s", "") } if net.ParseIP(ip).To4() == nil { - _, err := fmt.Fprintf(color.Output, "%s", + fmt.Fprintf(color.Output, "%s", color.New(color.FgWhite, color.Bold).Sprintf("%-25s", ip), ) - if err != nil { - return - } } else { - _, err := fmt.Fprintf(color.Output, "%s", + fmt.Fprintf(color.Output, "%s", color.New(color.FgWhite, color.Bold).Sprintf("%-15s", ip), ) - if err != nil { - return - } } i, _ := strconv.Atoi(v[0]) @@ -86,15 +77,9 @@ func RealtimePrinter(res *trace.Result, ttl int) { case res.Hops[ttl][i].Geo.Whois == "CMIN2-NET": fallthrough case strings.HasPrefix(res.Hops[ttl][i].Address.String(), "59.43."): - _, err := fmt.Fprintf(color.Output, " %s", color.New(color.FgHiYellow, color.Bold).Sprintf("AS%-6s", res.Hops[ttl][i].Geo.Asnumber)) - if err != nil { - return - } + fmt.Fprintf(color.Output, " %s", color.New(color.FgHiYellow, color.Bold).Sprintf("AS%-6s", res.Hops[ttl][i].Geo.Asnumber)) default: - _, err := fmt.Fprintf(color.Output, " %s", color.New(color.FgHiGreen, color.Bold).Sprintf("AS%-6s", res.Hops[ttl][i].Geo.Asnumber)) - if err != nil { - return - } + fmt.Fprintf(color.Output, " %s", color.New(color.FgHiGreen, color.Bold).Sprintf("AS%-6s", res.Hops[ttl][i].Geo.Asnumber)) } } else { @@ -130,15 +115,9 @@ func RealtimePrinter(res *trace.Result, ttl int) { case whoisFormat[0] == "[CMIN2-NET]": fallthrough case strings.HasPrefix(res.Hops[ttl][i].Address.String(), "59.43."): - _, err := fmt.Fprintf(color.Output, " %s", color.New(color.FgHiYellow, color.Bold).Sprintf("%-16s", whoisFormat[0])) - if err != nil { - return - } + fmt.Fprintf(color.Output, " %s", color.New(color.FgHiYellow, color.Bold).Sprintf("%-16s", whoisFormat[0])) default: - _, err := fmt.Fprintf(color.Output, " %s", color.New(color.FgHiGreen, color.Bold).Sprintf("%-16s", whoisFormat[0])) - if err != nil { - return - } + fmt.Fprintf(color.Output, " %s", color.New(color.FgHiGreen, color.Bold).Sprintf("%-16s", whoisFormat[0])) } } @@ -168,7 +147,8 @@ func RealtimePrinter(res *trace.Result, ttl int) { } if net.ParseIP(ip).To4() != nil { - _, err := fmt.Fprintf(color.Output, " %s %s %s %s %s\n %s ", + + fmt.Fprintf(color.Output, " %s %s %s %s %s\n %s ", color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.Country), color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.Prov), color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.City), @@ -176,11 +156,8 @@ func RealtimePrinter(res *trace.Result, ttl int) { fmt.Sprintf("%-6s", res.Hops[ttl][i].Geo.Owner), color.New(color.FgHiBlack, color.Bold).Sprintf("%-39s", res.Hops[ttl][i].Hostname), ) - if err != nil { - return - } } else { - _, err := fmt.Fprintf(color.Output, " %s %s %s %s %s\n %s ", + fmt.Fprintf(color.Output, " %s %s %s %s %s\n %s ", color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.Country), color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.Prov), color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.City), @@ -188,26 +165,17 @@ func RealtimePrinter(res *trace.Result, ttl int) { fmt.Sprintf("%-6s", res.Hops[ttl][i].Geo.Owner), color.New(color.FgHiBlack, color.Bold).Sprintf("%-32s", res.Hops[ttl][i].Hostname), ) - if err != nil { - return - } } for j := 1; j < len(v); j++ { if len(v) == 2 || j == 1 { - _, err := fmt.Fprintf(color.Output, "%s", + fmt.Fprintf(color.Output, "%s", color.New(color.FgHiCyan, color.Bold).Sprintf("%s", v[j]), ) - if err != nil { - return - } } else { - _, err := fmt.Fprintf(color.Output, " / %s", + fmt.Fprintf(color.Output, " / %s", color.New(color.FgHiCyan, color.Bold).Sprintf("%s", v[j]), ) - if err != nil { - return - } } } fmt.Println() diff --git a/printer/realtime_printer_router.go b/printer/realtime_printer_router.go index 215a414..18007bb 100644 --- a/printer/realtime_printer_router.go +++ b/printer/realtime_printer_router.go @@ -39,12 +39,9 @@ func RealtimePrinterWithRouter(res *trace.Result, ttl int) { } if latestIP == "" { - _, err := fmt.Fprintf(color.Output, "%s\n", + fmt.Fprintf(color.Output, "%s\n", color.New(color.FgWhite, color.Bold).Sprintf("*"), ) - if err != nil { - return - } return } @@ -54,28 +51,19 @@ func RealtimePrinterWithRouter(res *trace.Result, ttl int) { fmt.Printf("%4s", "") } if net.ParseIP(ip).To4() == nil { - _, err := fmt.Fprintf(color.Output, "%s", + fmt.Fprintf(color.Output, "%s", color.New(color.FgWhite, color.Bold).Sprintf("%-25s", ip), ) - if err != nil { - return - } } else { - _, err := fmt.Fprintf(color.Output, "%s", + fmt.Fprintf(color.Output, "%s", color.New(color.FgWhite, color.Bold).Sprintf("%-15s", ip), ) - if err != nil { - return - } } i, _ := strconv.Atoi(v[0]) if res.Hops[ttl][i].Geo.Asnumber != "" { - _, err := fmt.Fprintf(color.Output, " %s", color.New(color.FgHiGreen, color.Bold).Sprintf("AS%-6s", res.Hops[ttl][i].Geo.Asnumber)) - if err != nil { - return - } + fmt.Fprintf(color.Output, " %s", color.New(color.FgHiGreen, color.Bold).Sprintf("AS%-6s", res.Hops[ttl][i].Geo.Asnumber)) } else { fmt.Printf(" %-8s", "*") } @@ -89,10 +77,7 @@ func RealtimePrinterWithRouter(res *trace.Result, ttl int) { if whoisFormat[0] != "" { whoisFormat[0] = "[" + whoisFormat[0] + "]" } - _, err := fmt.Fprintf(color.Output, " %s", color.New(color.FgHiGreen, color.Bold).Sprintf("%-16s", whoisFormat[0])) - if err != nil { - return - } + fmt.Fprintf(color.Output, " %s", color.New(color.FgHiGreen, color.Bold).Sprintf("%-16s", whoisFormat[0])) } if res.Hops[ttl][i].Geo.Country == "" { @@ -101,7 +86,7 @@ func RealtimePrinterWithRouter(res *trace.Result, ttl int) { if net.ParseIP(ip).To4() != nil { - _, err := fmt.Fprintf(color.Output, " %s %s %s %s %s\n %s ", + fmt.Fprintf(color.Output, " %s %s %s %s %s\n %s ", color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.Country), color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.Prov), color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.City), @@ -109,11 +94,8 @@ func RealtimePrinterWithRouter(res *trace.Result, ttl int) { fmt.Sprintf("%-6s", res.Hops[ttl][i].Geo.Owner), color.New(color.FgHiBlack, color.Bold).Sprintf("%-39s", res.Hops[ttl][i].Hostname), ) - if err != nil { - return - } } else { - _, err := fmt.Fprintf(color.Output, " %s %s %s %s %s\n %s ", + fmt.Fprintf(color.Output, " %s %s %s %s %s\n %s ", color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.Country), color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.Prov), color.New(color.FgWhite, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.City), @@ -121,41 +103,29 @@ func RealtimePrinterWithRouter(res *trace.Result, ttl int) { fmt.Sprintf("%-6s", res.Hops[ttl][i].Geo.Owner), color.New(color.FgHiBlack, color.Bold).Sprintf("%-32s", res.Hops[ttl][i].Hostname), ) - if err != nil { - return - } } for j := 1; j < len(v); j++ { if len(v) == 2 || j == 1 { - _, err := fmt.Fprintf(color.Output, "%s", + fmt.Fprintf(color.Output, "%s", color.New(color.FgHiCyan, color.Bold).Sprintf("%s", v[j]), ) - if err != nil { - return - } } else { - _, err := fmt.Fprintf(color.Output, " / %s", + fmt.Fprintf(color.Output, " / %s", color.New(color.FgHiCyan, color.Bold).Sprintf("%s", v[j]), ) - if err != nil { - return - } } } i = 0 fmt.Println() if res.Hops[ttl][i].Geo != nil && !blockDisplay { - _, err := fmt.Fprintf(color.Output, "%s %s %s %s %s\n", + fmt.Fprintf(color.Output, "%s %s %s %s %s\n", color.New(color.FgWhite, color.Bold).Sprintf("-"), color.New(color.FgHiYellow, color.Bold).Sprintf("%s", res.Hops[ttl][i].Geo.Prefix), color.New(color.FgWhite, color.Bold).Sprintf("路由表"), color.New(color.FgHiCyan, color.Bold).Sprintf("Beta"), color.New(color.FgWhite, color.Bold).Sprintf("-"), ) - if err != nil { - return - } GetRouter(&res.Hops[ttl][i].Geo.Router, "AS"+res.Hops[ttl][i].Geo.Asnumber) } blockDisplay = true @@ -166,22 +136,16 @@ func GetRouter(r *map[string][]string, node string) { routeMap := *r for _, v := range routeMap[node] { if len(routeMap[v]) != 0 { - _, err := fmt.Fprintf(color.Output, " %s %s %s\n", + fmt.Fprintf(color.Output, " %s %s %s\n", color.New(color.FgWhite, color.Bold).Sprintf("%s", routeMap[v][0]), color.New(color.FgWhite, color.Bold).Sprintf("%s", v), color.New(color.FgHiBlue, color.Bold).Sprintf("%s", node), ) - if err != nil { - return - } } else { - _, err := fmt.Fprintf(color.Output, " %s %s\n", + fmt.Fprintf(color.Output, " %s %s\n", color.New(color.FgWhite, color.Bold).Sprintf("%s", v), color.New(color.FgHiBlue, color.Bold).Sprintf("%s", node), ) - if err != nil { - return - } } } diff --git a/trace/icmp_ipv4.go b/trace/icmp_ipv4.go index 8397b8b..6e5d288 100644 --- a/trace/icmp_ipv4.go +++ b/trace/icmp_ipv4.go @@ -66,12 +66,7 @@ func (t *ICMPTracer) Execute() (*Result, error) { if err != nil { return &t.res, err } - defer func(icmpListen net.PacketConn) { - err := icmpListen.Close() - if err != nil { - log.Println(err) - } - }(t.icmpListen) + defer t.icmpListen.Close() var cancel context.CancelFunc t.ctx, cancel = context.WithCancel(context.Background()) @@ -90,12 +85,7 @@ func (t *ICMPTracer) Execute() (*Result, error) { } for i := 0; i < t.NumMeasurements; i++ { t.wg.Add(1) - go func() { - err := t.send(ttl) - if err != nil { - log.Println(err) - } - }() + go t.send(ttl) <-time.After(time.Millisecond * time.Duration(t.Config.PacketInterval)) } <-time.After(time.Millisecond * time.Duration(t.Config.TTLInterval)) @@ -268,10 +258,7 @@ func (t *ICMPTracer) send(ttl int) error { }, } - err := ipv4.NewPacketConn(t.icmpListen).SetTTL(ttl) - if err != nil { - return err - } + ipv4.NewPacketConn(t.icmpListen).SetTTL(ttl) wb, err := icmpHeader.Marshal(nil) if err != nil { @@ -311,10 +298,7 @@ func (t *ICMPTracer) send(ttl int) error { h.TTL = ttl h.RTT = rtt - err := h.fetchIPData(t.Config) - if err != nil { - return err - } + h.fetchIPData(t.Config) t.res.add(h) case <-time.After(t.Timeout): diff --git a/trace/icmp_ipv6.go b/trace/icmp_ipv6.go index 7501314..8454e91 100644 --- a/trace/icmp_ipv6.go +++ b/trace/icmp_ipv6.go @@ -67,12 +67,7 @@ func (t *ICMPTracerv6) Execute() (*Result, error) { if err != nil { return &t.res, err } - defer func(icmpListen net.PacketConn) { - err := icmpListen.Close() - if err != nil { - panic(err) - } - }(t.icmpListen) + defer t.icmpListen.Close() var cancel context.CancelFunc t.ctx, cancel = context.WithCancel(context.Background()) @@ -91,12 +86,7 @@ func (t *ICMPTracerv6) Execute() (*Result, error) { } for i := 0; i < t.NumMeasurements; i++ { t.wg.Add(1) - go func() { - err := t.send(ttl) - if err != nil { - log.Println(err) - } - }() + go t.send(ttl) <-time.After(time.Millisecond * time.Duration(t.Config.PacketInterval)) } <-time.After(time.Millisecond * time.Duration(t.Config.TTLInterval)) @@ -268,10 +258,7 @@ func (t *ICMPTracerv6) send(ttl int) error { p := ipv6.NewPacketConn(t.icmpListen) icmpHeader.Body.(*icmp.Echo).Seq = ttl - err := p.SetHopLimit(ttl) - if err != nil { - return err - } + p.SetHopLimit(ttl) wb, err := icmpHeader.Marshal(nil) if err != nil { @@ -311,10 +298,7 @@ func (t *ICMPTracerv6) send(ttl int) error { h.TTL = ttl h.RTT = rtt - err := h.fetchIPData(t.Config) - if err != nil { - return err - } + h.fetchIPData(t.Config) t.res.add(h) diff --git a/trace/tcp_ipv4.go b/trace/tcp_ipv4.go index f2f4dec..e1f386f 100644 --- a/trace/tcp_ipv4.go +++ b/trace/tcp_ipv4.go @@ -55,12 +55,7 @@ func (t *TCPTracer) Execute() (*Result, error) { if err != nil { return &t.res, err } - defer func(icmp net.PacketConn) { - err := icmp.Close() - if err != nil { - panic(err) - } - }(t.icmp) + defer t.icmp.Close() var cancel context.CancelFunc t.ctx, cancel = context.WithCancel(context.Background()) @@ -83,12 +78,7 @@ func (t *TCPTracer) Execute() (*Result, error) { } for i := 0; i < t.NumMeasurements; i++ { t.wg.Add(1) - go func() { - err := t.send(ttl) - if err != nil { - log.Println(err) - } - }() + go t.send(ttl) } if t.RealtimePrinter != nil { @@ -295,10 +285,7 @@ func (t *TCPTracer) send(ttl int) error { h.TTL = ttl h.RTT = rtt - err := h.fetchIPData(t.Config) - if err != nil { - return err - } + h.fetchIPData(t.Config) t.res.add(h) diff --git a/trace/tcp_ipv6.go b/trace/tcp_ipv6.go index 091e8cc..c0b2686 100644 --- a/trace/tcp_ipv6.go +++ b/trace/tcp_ipv6.go @@ -2,7 +2,6 @@ package trace import ( "encoding/binary" - "log" "math" "math/rand" "net" @@ -51,12 +50,7 @@ func (t *TCPTracerv6) Execute() (*Result, error) { if err != nil { return &t.res, err } - defer func(icmp net.PacketConn) { - err := icmp.Close() - if err != nil { - log.Println(err) - } - }(t.icmp) + defer t.icmp.Close() var cancel context.CancelFunc t.ctx, cancel = context.WithCancel(context.Background()) @@ -76,12 +70,7 @@ func (t *TCPTracerv6) Execute() (*Result, error) { } for i := 0; i < t.NumMeasurements; i++ { t.wg.Add(1) - go func() { - err := t.send(ttl) - if err != nil { - log.Println(err) - } - }() + go t.send(ttl) } if t.RealtimePrinter != nil { // 对于实时模式,应该按照TTL进行并发请求 @@ -238,10 +227,7 @@ func (t *TCPTracerv6) send(ttl int) error { return err } - err = ipv6.NewPacketConn(t.tcp).SetHopLimit(ttl) - if err != nil { - return err - } + ipv6.NewPacketConn(t.tcp).SetHopLimit(ttl) if err != nil { return err } @@ -282,10 +268,7 @@ func (t *TCPTracerv6) send(ttl int) error { h.TTL = ttl h.RTT = rtt - err := h.fetchIPData(t.Config) - if err != nil { - return err - } + h.fetchIPData(t.Config) t.res.add(h) diff --git a/trace/udp.go b/trace/udp.go index 7afa2c6..6277466 100644 --- a/trace/udp.go +++ b/trace/udp.go @@ -41,12 +41,7 @@ func (t *UDPTracer) Execute() (*Result, error) { if err != nil { return &t.res, err } - defer func(icmp net.PacketConn) { - err := icmp.Close() - if err != nil { - log.Println(err) - } - }(t.icmp) + defer t.icmp.Close() var cancel context.CancelFunc t.ctx, cancel = context.WithCancel(context.Background()) @@ -64,12 +59,7 @@ func (t *UDPTracer) Execute() (*Result, error) { } for i := 0; i < t.NumMeasurements; i++ { t.wg.Add(1) - go func() { - err := t.send(ttl) - if err != nil { - log.Println(err) - } - }() + go t.send(ttl) } if t.RealtimePrinter != nil { @@ -266,10 +256,7 @@ func (t *UDPTracer) send(ttl int) error { h.TTL = ttl h.RTT = rtt - err := h.fetchIPData(t.Config) - if err != nil { - return err - } + h.fetchIPData(t.Config) t.res.add(h) diff --git a/tracemap/tracemap.go b/tracemap/tracemap.go index 26a3f11..5546d69 100644 --- a/tracemap/tracemap.go +++ b/tracemap/tracemap.go @@ -13,11 +13,8 @@ func GetMapUrl(r string) { url := "https://api.leo.moe/tracemap/api" resp, _ := http.Post(url, "application/json", strings.NewReader(r)) body, _ := io.ReadAll(resp.Body) - _, err := fmt.Fprintf(color.Output, "%s %s\n", + fmt.Fprintf(color.Output, "%s %s\n", color.New(color.FgWhite, color.Bold).Sprintf("%s", "MapTrace URL:"), color.New(color.FgBlue, color.Bold).Sprintf("%s", string(body)), ) - if err != nil { - return - } } diff --git a/util/util.go b/util/util.go index 1cb14cc..40e50b9 100644 --- a/util/util.go +++ b/util/util.go @@ -20,12 +20,7 @@ func LocalIPPort(dstip net.IP) (net.IP, int) { // We don't actually connect to anything, but we can determine // based on our destination ip what source ip we should use. if con, err := net.DialUDP("udp", nil, serverAddr); err == nil { - defer func(con *net.UDPConn) { - err := con.Close() - if err != nil { - log.Fatal(err) - } - }(con) + defer con.Close() if udpaddr, ok := con.LocalAddr().(*net.UDPAddr); ok { return udpaddr.IP, udpaddr.Port } @@ -42,12 +37,7 @@ func LocalIPPortv6(dstip net.IP) (net.IP, int) { // We don't actually connect to anything, but we can determine // based on our destination ip what source ip we should use. if con, err := net.DialUDP("udp", nil, serverAddr); err == nil { - defer func(con *net.UDPConn) { - err := con.Close() - if err != nil { - log.Fatal(err) - } - }(con) + defer con.Close() if udpaddr, ok := con.LocalAddr().(*net.UDPAddr); ok { return udpaddr.IP, udpaddr.Port } @@ -98,13 +88,10 @@ func DomainLookUp(host string, ipv4Only bool, dotServer string) net.IP { } else { fmt.Println("Please Choose the IP You Want To TraceRoute") for i, ip := range ips { - _, err := fmt.Fprintf(color.Output, "%s %s\n", + fmt.Fprintf(color.Output, "%s %s\n", color.New(color.FgHiYellow, color.Bold).Sprintf("%d.", i), color.New(color.FgWhite, color.Bold).Sprintf("%s", ip), ) - if err != nil { - return nil - } } var index int fmt.Printf("Your Option: ")