mirror of
https://github.com/nxtrace/NTrace-core.git
synced 2025-08-12 06:26:39 +00:00
Compare commits
188 Commits
v0.1.3-bet
...
v0.1.7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39917bb732 | ||
|
|
494f2ac819 | ||
|
|
0b09addd17 | ||
|
|
99dffc959c | ||
|
|
c30bcfee11 | ||
|
|
1554565460 | ||
|
|
2a069d7afe | ||
|
|
e6db19f5fd | ||
|
|
5603317fa3 | ||
|
|
3e71926127 | ||
|
|
e8f74c4ad3 | ||
|
|
aad80205c3 | ||
|
|
50cc9858d4 | ||
|
|
27f49f9cd0 | ||
|
|
e4320da08d | ||
|
|
7f16a27580 | ||
|
|
7db77024a3 | ||
|
|
c4ea506c35 | ||
|
|
39e2471845 | ||
|
|
18a9eefec9 | ||
|
|
1b743d1f17 | ||
|
|
3f0b14341a | ||
|
|
2ffd7fdb58 | ||
|
|
8f77050fca | ||
|
|
65652bd4e2 | ||
|
|
4176407a8a | ||
|
|
6bc4abeaf8 | ||
|
|
af0d886a02 | ||
|
|
09fdd2ac37 | ||
|
|
2b9d8176d4 | ||
|
|
ed2f89310f | ||
|
|
bd5a8902d4 | ||
|
|
e2a1bfe8cf | ||
|
|
356b782b3d | ||
|
|
40922ae13a | ||
|
|
8e90795a54 | ||
|
|
bbbb2377e1 | ||
|
|
efdfd9d612 | ||
|
|
016f06bafd | ||
|
|
1049986ebc | ||
|
|
80a75288d2 | ||
|
|
986b8ce300 | ||
|
|
ab774406ac | ||
|
|
9604b7befe | ||
|
|
dc6537005a | ||
|
|
839d227770 | ||
|
|
84e989e71b | ||
|
|
3b74b302cc | ||
|
|
9b0e58359f | ||
|
|
f0d7151144 | ||
|
|
3c65c29eff | ||
|
|
3aa4696fa9 | ||
|
|
b4abaffc7c | ||
|
|
c96fb4efa3 | ||
|
|
876de6bde1 | ||
|
|
7dbec0c7a1 | ||
|
|
d690f680f5 | ||
|
|
e96686013d | ||
|
|
6726b55fc9 | ||
|
|
9f29c75491 | ||
|
|
2f5bf3f195 | ||
|
|
5981e82ee3 | ||
|
|
80f7857a65 | ||
|
|
700d38de1c | ||
|
|
2dbf3f04a4 | ||
|
|
45df06ea80 | ||
|
|
98953048ce | ||
|
|
eddd4226b0 | ||
|
|
5afd9eb09e | ||
|
|
70006aaa13 | ||
|
|
497fb647c0 | ||
|
|
be8552c3cd | ||
|
|
f320fd6202 | ||
|
|
a42c5e3734 | ||
|
|
e6480c84e0 | ||
|
|
126115c04e | ||
|
|
ea7fd2af0f | ||
|
|
3fc81f4e71 | ||
|
|
0c2b77bd81 | ||
|
|
ac33c086c6 | ||
|
|
fadfdc87d4 | ||
|
|
eb77a2b69b | ||
|
|
02e6c6e1bf | ||
|
|
838af3b7a1 | ||
|
|
7cd16036a6 | ||
|
|
2ef4f61d7b | ||
|
|
688622738f | ||
|
|
83fe583f2a | ||
|
|
4b32594c17 | ||
|
|
927b6d4035 | ||
|
|
aa651f30cc | ||
|
|
ffec9a93cd | ||
|
|
59a744b3b5 | ||
|
|
9656dfe172 | ||
|
|
84c48dae99 | ||
|
|
4eaac372f6 | ||
|
|
c92d8a5172 | ||
|
|
acab410d4c | ||
|
|
858555fd86 | ||
|
|
31e419b199 | ||
|
|
1dddd43e67 | ||
|
|
4148d0d4b1 | ||
|
|
4f7977da8f | ||
|
|
cfc8034cb4 | ||
|
|
dbc0f87847 | ||
|
|
74a320898f | ||
|
|
329b3fdd6b | ||
|
|
3fb88f4cf4 | ||
|
|
1de84cac71 | ||
|
|
83d093f5aa | ||
|
|
8b03ca7a38 | ||
|
|
7a847bf0d5 | ||
|
|
11fe41611c | ||
|
|
71b24fb7a0 | ||
|
|
0b08e4b4a4 | ||
|
|
2608c05da1 | ||
|
|
314bdd0cce | ||
|
|
ea958059c6 | ||
|
|
b59c349264 | ||
|
|
030a487526 | ||
|
|
cac6d33fde | ||
|
|
1725a65827 | ||
|
|
4d886066a3 | ||
|
|
156043730d | ||
|
|
91ad3bc539 | ||
|
|
351da5f5a3 | ||
|
|
f8fc90d7a5 | ||
|
|
9c75635acc | ||
|
|
b20b27fd20 | ||
|
|
cfc1dfdfe5 | ||
|
|
97c4387af4 | ||
|
|
37b5202126 | ||
|
|
afb6a3e1df | ||
|
|
c9a3916cd0 | ||
|
|
89d56c437e | ||
|
|
6299dcd9a3 | ||
|
|
82f28a13f3 | ||
|
|
af732bc212 | ||
|
|
8d5f58bf15 | ||
|
|
8bd5654474 | ||
|
|
4de61823ee | ||
|
|
39ec016d0d | ||
|
|
67999411af | ||
|
|
7cc6b71727 | ||
|
|
16ba835537 | ||
|
|
1b7c3b8d0d | ||
|
|
bd47935a2d | ||
|
|
1f16001e4f | ||
|
|
f56e6cdba3 | ||
|
|
ecd3df8ee8 | ||
|
|
1658da1653 | ||
|
|
5110c9b990 | ||
|
|
aa446574f1 | ||
|
|
2016990629 | ||
|
|
e639b7b12d | ||
|
|
3cfe6598dd | ||
|
|
59193cae47 | ||
|
|
1def15e805 | ||
|
|
1950032371 | ||
|
|
f81a0b3da3 | ||
|
|
9e3d4186a1 | ||
|
|
4737669436 | ||
|
|
dbecfd880d | ||
|
|
a67a4bc559 | ||
|
|
077d72d5cd | ||
|
|
d45b5eb032 | ||
|
|
5fe1110ab3 | ||
|
|
812c953976 | ||
|
|
f89505ab87 | ||
|
|
640eb8c02d | ||
|
|
fc3462ff9e | ||
|
|
071a6b124a | ||
|
|
45a8cf21f6 | ||
|
|
1315efa4d2 | ||
|
|
c2bd51faab | ||
|
|
f1ce2bbb77 | ||
|
|
a18bf1889b | ||
|
|
12c93de8c5 | ||
|
|
8280b62881 | ||
|
|
608847b1cb | ||
|
|
b4838ba402 | ||
|
|
fbd70a8eb1 | ||
|
|
fd1632fccb | ||
|
|
4a725d2c48 | ||
|
|
96bb323f72 | ||
|
|
961c29e499 | ||
|
|
870d1f3b5a | ||
|
|
24eedfa3fe |
@@ -5,7 +5,11 @@ set -e
|
||||
DIST_PREFIX="nexttrace"
|
||||
DEBUG_MODE=${2}
|
||||
TARGET_DIR="dist"
|
||||
PLATFORMS="darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 linux/mips"
|
||||
PLATFORMS="darwin/amd64 darwin/arm64 linux/386 linux/amd64 linux/arm64 linux/mips openbsd/amd64 openbsd/arm64 freebsd/amd64 freebsd/arm64"
|
||||
|
||||
BUILD_VERSION="$(git describe --tags --always)"
|
||||
BUILD_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
|
||||
COMMIT_SHA1="$(git rev-parse --short HEAD)"
|
||||
|
||||
rm -rf ${TARGET_DIR}
|
||||
mkdir ${TARGET_DIR}
|
||||
@@ -21,16 +25,36 @@ for pl in ${PLATFORMS}; do
|
||||
echo "build => ${TARGET}"
|
||||
if [ "${DEBUG_MODE}" == "debug" ]; then
|
||||
go build -trimpath -gcflags "all=-N -l" -o ${TARGET} \
|
||||
-ldflags "-X 'main.version=${BUILD_VERSION}' \
|
||||
-X 'main.buildDate=${BUILD_DATE}' \
|
||||
-X 'main.commitID=${COMMIT_SHA1}'\
|
||||
-ldflags "-X 'github.com/xgadget-lab/nexttrace/printer.version=${BUILD_VERSION}' \
|
||||
-X 'github.com/xgadget-lab/nexttrace/printer.buildDate=${BUILD_DATE}' \
|
||||
-X 'github.com/xgadget-lab/nexttrace/printer.commitID=${COMMIT_SHA1}'\
|
||||
-w -s"
|
||||
else
|
||||
go build -trimpath -o ${TARGET} \
|
||||
-ldflags "-X 'main.version=${BUILD_VERSION}' \
|
||||
-X 'main.buildDate=${BUILD_DATE}' \
|
||||
-X 'main.commitID=${COMMIT_SHA1}'\
|
||||
-ldflags "-X 'github.com/xgadget-lab/nexttrace/printer.version=${BUILD_VERSION}' \
|
||||
-X 'github.com/xgadget-lab/nexttrace/printer.buildDate=${BUILD_DATE}' \
|
||||
-X 'github.com/xgadget-lab/nexttrace/printer.commitID=${COMMIT_SHA1}'\
|
||||
-w -s"
|
||||
fi
|
||||
done
|
||||
|
||||
export GOOS='linux'
|
||||
export GOARCH='arm'
|
||||
export GOARM='7'
|
||||
export TARGET=${TARGET_DIR}/${DIST_PREFIX}_${GOOS}_${GOARCH}v7
|
||||
echo "build => ${TARGET}"
|
||||
if [ "${DEBUG_MODE}" == "debug" ]; then
|
||||
go build -trimpath -gcflags "all=-N -l" -o ${TARGET} \
|
||||
-ldflags "-X 'github.com/xgadget-lab/nexttrace/printer.version=${BUILD_VERSION}' \
|
||||
-X 'github.com/xgadget-lab/nexttrace/printer.buildDate=${BUILD_DATE}' \
|
||||
-X 'github.com/xgadget-lab/nexttrace/printer.commitID=${COMMIT_SHA1}'\
|
||||
-w -s"
|
||||
else
|
||||
go build -trimpath -o ${TARGET} \
|
||||
-ldflags "-X 'github.com/xgadget-lab/nexttrace/printer.version=${BUILD_VERSION}' \
|
||||
-X 'github.com/xgadget-lab/nexttrace/printer.buildDate=${BUILD_DATE}' \
|
||||
-X 'github.com/xgadget-lab/nexttrace/printer.commitID=${COMMIT_SHA1}'\
|
||||
-w -s"
|
||||
fi
|
||||
|
||||
|
||||
|
||||
40
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
40
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
---
|
||||
name: nexttrace 程序问题
|
||||
about: "提交一个 nexttrace 的程序问题报告。"
|
||||
copyright: [v2fly](https://github.com/v2fly)
|
||||
---
|
||||
|
||||
<!--
|
||||
除非特殊情况,请完整填写所有问题。不按模板发的 issue 将直接被关闭。
|
||||
如果你遇到的问题不是 nexttrace 的 bug,比如你不清楚如何配置,请在 https://github.com/xgadget-lab/nexttrace/discussions 进行讨论。
|
||||
-->
|
||||
|
||||
## 你正在使用哪个版本的 nexttrace?
|
||||
|
||||
<!-- 比如linux_amd64 macOS_arm64 -->
|
||||
|
||||
|
||||
## 你看到的异常现象是什么?
|
||||
|
||||
<!-- 请描述具体现象 -->
|
||||
|
||||
|
||||
## 你期待看到的正常表现是怎样的?
|
||||
|
||||
|
||||
|
||||
## 请附上你的命令
|
||||
|
||||
<!-- 提交 issue 前,请隐去您的隐私信息 -->
|
||||
|
||||
|
||||
## 请附上出错时软件输出的错误信息
|
||||
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
18
.github/workflows/build.yml
vendored
18
.github/workflows/build.yml
vendored
@@ -1,11 +1,10 @@
|
||||
on:
|
||||
push: # 每次带有 tag 的 push 候触发
|
||||
tags:
|
||||
- 'v*'
|
||||
push:
|
||||
pull_request:
|
||||
|
||||
name: Build Release
|
||||
name: Test & Build Release
|
||||
jobs:
|
||||
test:
|
||||
Test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
@@ -17,8 +16,9 @@ jobs:
|
||||
- name: Test
|
||||
run: go test -v -coverprofile='coverage.out' -covermode=count ./...
|
||||
|
||||
release:
|
||||
Build:
|
||||
needs: test
|
||||
if: startsWith(github.ref, 'refs/tags/v')
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
@@ -36,8 +36,14 @@ jobs:
|
||||
files: |
|
||||
dist/nexttrace_darwin_amd64
|
||||
dist/nexttrace_darwin_arm64
|
||||
dist/nexttrace_linux_386
|
||||
dist/nexttrace_linux_amd64
|
||||
dist/nexttrace_linux_arm64
|
||||
dist/nexttrace_linux_armv7
|
||||
dist/nexttrace_linux_mips
|
||||
dist/nexttrace_openbsd_amd64
|
||||
dist/nexttrace_openbsd_arm64
|
||||
dist/nexttrace_freebsd_amd64
|
||||
dist/nexttrace_freebsd_arm64
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GT_Token }}
|
||||
|
||||
17
.github/workflows/test.yml
vendored
17
.github/workflows/test.yml
vendored
@@ -1,17 +0,0 @@
|
||||
on:
|
||||
push:
|
||||
pull_request:
|
||||
|
||||
name: Test
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: "1.18"
|
||||
|
||||
- name: Test
|
||||
run: go test -v -coverprofile='coverage.out' -covermode=count ./...
|
||||
147
README.md
147
README.md
@@ -1,24 +1,34 @@
|
||||
<div align="center">
|
||||
|
||||
<img src="asset/logo.png" height="200px"/>
|
||||
<img src="asset/logo.png" height="200px" alt="NextTrace Logo"/>
|
||||
|
||||
</div>
|
||||
|
||||
# NextTrace
|
||||
|
||||
一款开源的可视化路由跟踪工具,使用Golang开发。
|
||||
一款开源的可视化路由跟踪工具,使用 Golang 开发。
|
||||
|
||||
## How To Use
|
||||
|
||||
### Install
|
||||
### Automated Install
|
||||
|
||||
```bash
|
||||
bash -c "$(curl -Ls https://raw.githubusercontent.com/xgadget-lab/nexttrace/main/nt_install.sh)"
|
||||
# Linux 一键安装脚本
|
||||
bash <(curl -Ls https://raw.githubusercontent.com/xgadget-lab/nexttrace/main/nt_install.sh)
|
||||
|
||||
# GHPROXY 镜像(国内使用)
|
||||
bash <(curl -Ls https://ghproxy.com/https://raw.githubusercontent.com/xgadget-lab/nexttrace/main/nt_install.sh)
|
||||
|
||||
# macOS brew 安装命令
|
||||
brew tap xgadget-lab/nexttrace && brew install nexttrace
|
||||
```
|
||||
|
||||
- `Release`里面为很多系统以及不同架构提供了编译好的二进制可执行文件,如果没有可以自行编译。
|
||||
- 一些本项目的必要依赖在`Windows`上`Golang`底层实现不完全,所以目前`NextTrace`在`Windows`平台不可用。
|
||||
|
||||
### Get Started
|
||||
|
||||
`NextTrace`默认使用`icmp`协议发起`TraceRoute`请求,该协议同时支持`IPv4`和`IPv6`
|
||||
`NextTrace` 默认使用`ICMP`协议发起`TraceRoute`请求,该协议同时支持`IPv4`和`IPv6`
|
||||
|
||||
```bash
|
||||
# IPv4 ICMP Trace
|
||||
@@ -31,7 +41,17 @@ nexttrace -table 1.0.0.1
|
||||
nexttrace 2606:4700:4700::1111
|
||||
```
|
||||
|
||||
`NextTrace`也可以使用`TCP`和`UDP`协议发起`Traceroute`请求,不过目前只支持`IPv4`
|
||||
`NextTrace` 现已经支持快速测试,有一次性测试回程路由需求的朋友可以使用
|
||||
```bash
|
||||
# 北上广(电信+联通+移动+教育网)IPv4 ICMP 快速测试
|
||||
nexttrace -f
|
||||
|
||||
# 也可以使用 TCP SYN 而非 ICMP 进行测试
|
||||
nexttrace -f -T
|
||||
```
|
||||
|
||||
`NextTrace` 也可以使用`TCP`和`UDP`协议发起`Traceroute`请求,不过目前只支持`IPv4`
|
||||
|
||||
```bash
|
||||
# TCP SYN Trace
|
||||
nexttrace -T www.bing.com
|
||||
@@ -45,36 +65,68 @@ nexttrace -U 1.0.0.1
|
||||
nexttrace -U -p 53 1.0.0.1
|
||||
```
|
||||
|
||||
`NextTrace`也同样支持一些进阶功能,如IP反向解析、并发数控制、模式切换等
|
||||
`NextTrace`也同样支持一些进阶功能,如 IP 反向解析、并发数控制、模式切换等
|
||||
|
||||
```bash
|
||||
# 每一跳发送2个探测包
|
||||
nexttrace -q 2 www.hkix.net
|
||||
|
||||
# 无并发,每次只发送一个探测包
|
||||
nexttrace -r 1 www.hkix.net
|
||||
|
||||
# 打开IP反向解析功能,在IPv6的骨干网定位辅助有较大帮助
|
||||
nexttrace -rdns www.bbix.net
|
||||
|
||||
# 联合使用
|
||||
nexttrace -r 1 -q 1 -report www.time.com.my
|
||||
# 特色功能:打印Route-Path图
|
||||
# Route-Path图示例:
|
||||
# AS6453 塔塔通信「Singapore『Singapore』」
|
||||
# ╭╯
|
||||
# ╰AS9299 Philippine Long Distance Telephone Co.「Philippines『Metro Manila』」
|
||||
# ╭╯
|
||||
# ╰AS36776 Five9 Inc.「Philippines『Metro Manila』」
|
||||
# ╭╯
|
||||
# ╰AS37963 阿里云「ALIDNS.COM『ALIDNS.COM』」
|
||||
nexttrace -report www.time.com.my
|
||||
```
|
||||
|
||||
### IP数据库
|
||||
`NextTrace`支持用户自主选择 IP 数据库(目前支持:`LeoMoeAPI`, `IP.SB`, `IPInfo`, `IPInsight`, `IPAPI.com`)
|
||||
|
||||
目前使用的IP数据库默认为我们自己搭建的API服务,如果后期遇到滥用,我们可能会选择关闭。
|
||||
```bash
|
||||
# 可以自行指定IP数据库[此处为IP.SB],不指定则默认为LeoMoeAPI
|
||||
nexttrace -d IP.SB
|
||||
## 特别的:其中 ipinfo API 需要从ipinfo自行购买服务,如有需要可以clone本项目添加其提供的token自行编译
|
||||
## TOKEN填写路径:ipgeo/tokens.go
|
||||
## 另外:由于IP.SB被滥用比较严重,会经常出现无法查询的问题,请知悉。
|
||||
## IPAPI.com限制调用较为严格,如有查询不到的情况,请几分钟后再试。
|
||||
```
|
||||
|
||||
我们也会在后期开放服务端源代码,您也可以根据该项目的源码自行搭建属于您的API服务器。
|
||||
`NextTrace`支持参数混合使用
|
||||
|
||||
NextTrace所有的的IP地理位置`API DEMO`可以参考[这里](https://github.com/xgadget-lab/nexttrace/blob/main/ipgeo/)
|
||||
```bash
|
||||
Example:
|
||||
nexttrace -d IPInsight -m 20 -p 443 -q 5 -r 20 -rdns 1.1.1.1
|
||||
nexttrace -T -q 2 -r 1 -rdns -table -report 2001:4860:4860::8888
|
||||
```
|
||||
|
||||
### 全部用法详见Usage菜单
|
||||
### IP 数据库
|
||||
|
||||
目前使用的 IP 数据库默认为我们自己搭建的 API 服务,如果后期遇到滥用,我们可能会选择关闭。
|
||||
|
||||
我们也会在后期开放服务端源代码,您也可以根据该项目的源码自行搭建属于您的 API 服务器。
|
||||
|
||||
NextTrace 所有的的 IP 地理位置`API DEMO`可以参考[这里](https://github.com/xgadget-lab/nexttrace/blob/main/ipgeo/)
|
||||
|
||||
### 全部用法详见 Usage 菜单
|
||||
|
||||
```shell
|
||||
Usage of nexttrace:
|
||||
'nexttrace [options] <hostname>' or 'nexttrace <hostname> [option...]'
|
||||
Options:
|
||||
-T Use TCP SYN for tracerouting (default port is 80)
|
||||
-U Use UDP Package for tracerouting (default port is 53 in UDP)
|
||||
-V Check Version
|
||||
-d string
|
||||
Choose IP Geograph Data Provider [LeoMoeAPI, IP.SB, IPInfo, IPInsight] (default "LeoMoeAPI")
|
||||
Choose IP Geograph Data Provider [LeoMoeAPI, IP.SB, IPInfo, IPInsight, IPAPI.com] (default "LeoMoeAPI")
|
||||
-m int
|
||||
Set the max number of hops (max TTL to be reached). (default 30)
|
||||
-p int
|
||||
@@ -85,31 +137,29 @@ Usage of nexttrace:
|
||||
Set ParallelRequests number. It should be 1 when there is a multi-routing. (default 18)
|
||||
-rdns
|
||||
Set whether rDNS will be display
|
||||
-realtime
|
||||
Output trace results in runtime
|
||||
-report
|
||||
Route Path
|
||||
-table
|
||||
Output trace results as table
|
||||
-report
|
||||
Route Path
|
||||
|
||||
```
|
||||
|
||||
## 项目截图
|
||||
|
||||

|
||||
<div align="center">
|
||||
|
||||

|
||||
<img src=asset/screenshot.png alt="NextTrace Screenshot" height="688" />
|
||||
|
||||
<!--
|
||||
Leo注:描述可能不合适,建议再加以斟酌已经修改
|
||||
## History
|
||||
</div>
|
||||
|
||||
- v0.0.6.alpha - Now
|
||||
- https://github.com/xgadget-lab/nexttrace
|
||||
- 因为项目计划调整,更名并转移到当前仓库。重构了部分代码,提高了效率,增加了ICMP(IPv4 & IPv6)支持,并规划了更多功能。
|
||||
- 最初版本 - v0.0.5.alpha
|
||||
- https://github.com/OwO-Network/traceroute
|
||||
- 感谢 Leo (leo.moe) & Vincent (vincent.moe) 发起了这个项目,并完成了最初的工作。
|
||||
-->
|
||||
## FAQ 常见问题
|
||||
|
||||
如果你在安装或者使用的时候遇到了问题,我们建议你不要把新建一个 `issue` 作为首选项
|
||||
|
||||
以下是我们推荐的排错流程:
|
||||
|
||||
1. 查看是否为常见问题 -> [前往 Github Wiki](https://github.com/xgadget-lab/nexttrace/wiki/FAQ---%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E8%A7%A3%E7%AD%94)
|
||||
2. 疑似 BUG、或者功能建议 -> [前往 Github Issues](https://github.com/xgadget-lab/nexttrace/issues)
|
||||
|
||||
## Thanks
|
||||
|
||||
@@ -117,30 +167,45 @@ Leo注:描述可能不合适,建议再加以斟酌已经修改
|
||||
|
||||
[Sam Sam](https://github.com/samleong123) (samsam123@samsam123.name.my)
|
||||
|
||||
[waiting4new](https://github.com/waiting4new)、[FFEE_CO](https://github.com/fkx4-p)、[nsnnns](https://github.com/tsosunchia)
|
||||
[tsosunchia](https://github.com/tsosunchia)
|
||||
|
||||
[waiting4new](https://github.com/waiting4new)
|
||||
|
||||
[FFEE_CO](https://github.com/fkx4-p)
|
||||
|
||||
## IP Database Copyright
|
||||
|
||||
### IPv4 Database
|
||||
|
||||
#### China MainLand
|
||||
#### China
|
||||
|
||||
* 项目组自行维护 ~ 御三家骨干网数据 ~ 5%
|
||||
|
||||
* 埃文科技 Paid Database ~ 95%
|
||||
| ISP | 类型 | 数据源 | 占比 |
|
||||
| :------------: | :----: | :-------: | :--: |
|
||||
| 电信/联通/移动 | 骨干网 | NextTrace | 10% |
|
||||
| 电信/联通/移动 | 城域网 | 埃文科技 | 90% |
|
||||
|
||||
#### WorldWide
|
||||
|
||||
* 埃文科技 Paid Database ~ 15%
|
||||
| ISP | 类型 | 数据源 | 占比 |
|
||||
| :-----: | :----: | :-------: | :--: |
|
||||
| Tier-01 | 骨干网 | IPInfo | 2% |
|
||||
| Tier-01 | 骨干网 | 埃文科技 | 3% |
|
||||
| Tier-01 | 骨干网 | IPInSight | 5% |
|
||||
| Tier-01 | 城域网 | IPInSight | 90% |
|
||||
|
||||
* IpInfo Free ~ 15%
|
||||
|
||||
* IPInSight Free ~ 70%
|
||||
| ISP | 类型 | 数据源 | 占比 |
|
||||
| :----: | :----: | :-------: | :--: |
|
||||
| Others | 骨干网 | IPInSight | 5% |
|
||||
| Others | 城域网 | IPInSight | 95% |
|
||||
|
||||
### IPv6 Database
|
||||
|
||||
| ISP | 类型 | 数据源 | 占比 |
|
||||
| :-: | :--: | :--------------: | :--: |
|
||||
| All | 全部 | IP2Location Lite | 100% |
|
||||
|
||||
This product includes IP2Location LITE data available from <a href="https://lite.ip2location.com">https://lite.ip2location.com</a>.
|
||||
|
||||
### Others
|
||||
|
||||
其他第三方API尽管集成在本项目内,但是具体的TOS以及AUP,请详见第三方API官网。如遇到IP数据错误,也请直接联系他们纠错。
|
||||
其他第三方 API 尽管集成在本项目内,但是具体的 TOS 以及 AUP,请详见第三方 API 官网。如遇到 IP 数据错误,也请直接联系他们纠错。
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 866 KiB After Width: | Height: | Size: 1.3 MiB |
152
fast_trace/basic.go
Normal file
152
fast_trace/basic.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package fastTrace
|
||||
|
||||
type AllLocationCollection struct {
|
||||
Beijing BackBoneCollection
|
||||
Shanghai BackBoneCollection
|
||||
Guangzhou BackBoneCollection
|
||||
Hangzhou BackBoneCollection
|
||||
Hefei BackBoneCollection
|
||||
Changsha BackBoneCollection
|
||||
}
|
||||
|
||||
type BackBoneCollection struct {
|
||||
Location string
|
||||
CT163 ISPCollection
|
||||
CTCN2 ISPCollection
|
||||
CU169 ISPCollection
|
||||
CU9929 ISPCollection
|
||||
CM ISPCollection
|
||||
EDU ISPCollection
|
||||
CST ISPCollection
|
||||
}
|
||||
|
||||
type ISPCollection struct {
|
||||
ISPName string
|
||||
IP string
|
||||
}
|
||||
|
||||
const (
|
||||
CT163 string = "电信 163 AS4134"
|
||||
CTCN2 string = "电信 CN2 AS4809"
|
||||
CU169 string = "联通 169 AS4837"
|
||||
CU9929 string = "联通 A网 AS9929"
|
||||
CM string = "移动 骨干网 AS9808"
|
||||
EDU string = "教育网 CERNET AS4538"
|
||||
)
|
||||
|
||||
var TestIPsCollection = AllLocationCollection{
|
||||
Beijing: Beijing,
|
||||
Shanghai: Shanghai,
|
||||
Guangzhou: Guangzhou,
|
||||
Hangzhou: Hangzhou,
|
||||
Hefei: Hefei,
|
||||
}
|
||||
|
||||
var Beijing = BackBoneCollection{
|
||||
Location: "北京",
|
||||
CT163: ISPCollection{
|
||||
ISPName: CT163,
|
||||
IP: "106.37.67.1",
|
||||
},
|
||||
|
||||
CU169: ISPCollection{
|
||||
ISPName: CU169,
|
||||
IP: "123.125.96.156",
|
||||
},
|
||||
|
||||
CM: ISPCollection{
|
||||
ISPName: CM,
|
||||
IP: "211.136.25.153",
|
||||
},
|
||||
|
||||
EDU: ISPCollection{
|
||||
ISPName: EDU,
|
||||
IP: "101.6.15.130",
|
||||
},
|
||||
}
|
||||
|
||||
var Shanghai = BackBoneCollection{
|
||||
Location: "上海",
|
||||
CT163: ISPCollection{
|
||||
ISPName: CT163,
|
||||
IP: "101.226.28.198",
|
||||
},
|
||||
|
||||
CTCN2: ISPCollection{
|
||||
ISPName: CTCN2,
|
||||
IP: "58.32.4.1",
|
||||
},
|
||||
|
||||
CU169: ISPCollection{
|
||||
ISPName: CU169,
|
||||
IP: "139.226.206.150",
|
||||
},
|
||||
|
||||
CU9929: ISPCollection{
|
||||
ISPName: CU9929,
|
||||
IP: "210.13.86.1",
|
||||
},
|
||||
|
||||
CM: ISPCollection{
|
||||
ISPName: CM,
|
||||
IP: "120.204.34.85",
|
||||
},
|
||||
|
||||
EDU: ISPCollection{
|
||||
ISPName: EDU,
|
||||
IP: "202.120.58.155",
|
||||
},
|
||||
}
|
||||
|
||||
var Guangzhou = BackBoneCollection{
|
||||
Location: "广州",
|
||||
CT163: ISPCollection{
|
||||
ISPName: CT163,
|
||||
IP: "106.37.67.1",
|
||||
},
|
||||
|
||||
CU169: ISPCollection{
|
||||
ISPName: CU169,
|
||||
IP: "157.18.0.22",
|
||||
},
|
||||
|
||||
CM: ISPCollection{
|
||||
ISPName: CM,
|
||||
IP: "120.198.26.254",
|
||||
},
|
||||
}
|
||||
|
||||
var Hangzhou = BackBoneCollection{
|
||||
Location: "杭州",
|
||||
CT163: ISPCollection{
|
||||
ISPName: CT163,
|
||||
IP: "61.164.23.196",
|
||||
},
|
||||
CU169: ISPCollection{
|
||||
ISPName: CU169,
|
||||
IP: "60.12.244.1",
|
||||
},
|
||||
CM: ISPCollection{
|
||||
ISPName: CM,
|
||||
IP: "112.17.224.98",
|
||||
},
|
||||
// 浙江大学 教育网
|
||||
EDU: ISPCollection{
|
||||
ISPName: EDU,
|
||||
IP: "210.32.2.1",
|
||||
},
|
||||
}
|
||||
|
||||
var Hefei = BackBoneCollection{
|
||||
Location: "合肥",
|
||||
// 中国科学技术大学 教育网
|
||||
EDU: ISPCollection{
|
||||
ISPName: EDU,
|
||||
IP: "202.38.64.1",
|
||||
},
|
||||
// 中国科学技术大学 科技网
|
||||
CST: ISPCollection{
|
||||
ISPName: "中国科学技术大学 科技网 AS7497",
|
||||
IP: "210.72.22.2",
|
||||
},
|
||||
}
|
||||
117
fast_trace/fast_trace.go
Normal file
117
fast_trace/fast_trace.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package fastTrace
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/xgadget-lab/nexttrace/ipgeo"
|
||||
"github.com/xgadget-lab/nexttrace/printer"
|
||||
"github.com/xgadget-lab/nexttrace/trace"
|
||||
)
|
||||
|
||||
type FastTracer struct {
|
||||
TracerouteMethod trace.Method
|
||||
}
|
||||
|
||||
func (f *FastTracer) tracert(location string, ispCollection ISPCollection) {
|
||||
fmt.Printf("『%s %s 』\n", location, ispCollection.ISPName)
|
||||
fmt.Printf("traceroute to %s, 30 hops max, 32 byte packets\n", ispCollection.IP)
|
||||
ip := net.ParseIP(ispCollection.IP)
|
||||
var conf = trace.Config{
|
||||
DestIP: ip,
|
||||
DestPort: 80,
|
||||
MaxHops: 30,
|
||||
NumMeasurements: 3,
|
||||
ParallelRequests: 18,
|
||||
RDns: true,
|
||||
IPGeoSource: ipgeo.GetSource("LeoMoeAPI"),
|
||||
Timeout: 1 * time.Second,
|
||||
}
|
||||
|
||||
if f.TracerouteMethod == trace.ICMPTrace {
|
||||
conf.RealtimePrinter = printer.RealtimePrinter
|
||||
}
|
||||
|
||||
res, err := trace.Traceroute(f.TracerouteMethod, conf)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if f.TracerouteMethod == trace.TCPTrace {
|
||||
printer.TracerouteTablePrinter(res)
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FastTracer) testAll() {
|
||||
f.testCT()
|
||||
f.testCU()
|
||||
f.testCM()
|
||||
f.testEDU()
|
||||
}
|
||||
|
||||
func (f *FastTracer) testCT() {
|
||||
f.tracert(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.CT163)
|
||||
f.tracert(TestIPsCollection.Shanghai.Location, TestIPsCollection.Shanghai.CT163)
|
||||
f.tracert(TestIPsCollection.Shanghai.Location, TestIPsCollection.Shanghai.CTCN2)
|
||||
f.tracert(TestIPsCollection.Hangzhou.Location, TestIPsCollection.Hangzhou.CT163)
|
||||
f.tracert(TestIPsCollection.Guangzhou.Location, TestIPsCollection.Guangzhou.CT163)
|
||||
}
|
||||
|
||||
func (f *FastTracer) testCU() {
|
||||
f.tracert(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.CU169)
|
||||
f.tracert(TestIPsCollection.Shanghai.Location, TestIPsCollection.Shanghai.CU169)
|
||||
f.tracert(TestIPsCollection.Shanghai.Location, TestIPsCollection.Shanghai.CU9929)
|
||||
f.tracert(TestIPsCollection.Hangzhou.Location, TestIPsCollection.Hangzhou.CU169)
|
||||
f.tracert(TestIPsCollection.Guangzhou.Location, TestIPsCollection.Guangzhou.CU169)
|
||||
}
|
||||
|
||||
func (f *FastTracer) testCM() {
|
||||
f.tracert(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.CM)
|
||||
f.tracert(TestIPsCollection.Shanghai.Location, TestIPsCollection.Shanghai.CM)
|
||||
f.tracert(TestIPsCollection.Hangzhou.Location, TestIPsCollection.Hangzhou.CM)
|
||||
f.tracert(TestIPsCollection.Guangzhou.Location, TestIPsCollection.Guangzhou.CM)
|
||||
}
|
||||
|
||||
func (f *FastTracer) testEDU() {
|
||||
f.tracert(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.EDU)
|
||||
f.tracert(TestIPsCollection.Shanghai.Location, TestIPsCollection.Shanghai.EDU)
|
||||
f.tracert(TestIPsCollection.Hangzhou.Location, TestIPsCollection.Hangzhou.EDU)
|
||||
f.tracert(TestIPsCollection.Hefei.Location, TestIPsCollection.Hefei.EDU)
|
||||
// 科技网暂时算在EDU里面,等拿到了足够多的数据再分离出去,单独用于测试
|
||||
f.tracert(TestIPsCollection.Hefei.Location, TestIPsCollection.Hefei.CST)
|
||||
}
|
||||
|
||||
func FastTest(tm bool) {
|
||||
var c string
|
||||
|
||||
fmt.Println("您想测试哪些ISP的路由?\n1. 国内四网\n2. 电信\n3. 联通\n4. 移动\n5. 教育网")
|
||||
fmt.Print("请选择选项:")
|
||||
fmt.Scanln(&c)
|
||||
|
||||
ft := FastTracer{}
|
||||
|
||||
if !tm {
|
||||
ft.TracerouteMethod = trace.ICMPTrace
|
||||
fmt.Println("您将默认使用ICMP协议进行路由跟踪,如果您想使用TCP SYN进行路由跟踪,可以加入 -T 参数")
|
||||
} else {
|
||||
ft.TracerouteMethod = trace.TCPTrace
|
||||
}
|
||||
|
||||
switch c {
|
||||
case "1":
|
||||
ft.testAll()
|
||||
case "2":
|
||||
ft.testCT()
|
||||
case "3":
|
||||
ft.testCU()
|
||||
case "4":
|
||||
ft.testCM()
|
||||
case "5":
|
||||
ft.testEDU()
|
||||
default:
|
||||
ft.testAll()
|
||||
}
|
||||
}
|
||||
2
go.mod
2
go.mod
@@ -11,7 +11,7 @@ require (
|
||||
require (
|
||||
github.com/mattn/go-colorable v0.1.9 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/panjf2000/ants/v2 v2.5.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // direct
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
6
go.sum
6
go.sum
@@ -1,4 +1,3 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
@@ -13,8 +12,6 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/panjf2000/ants/v2 v2.5.0 h1:1rWGWSnxCsQBga+nQbA4/iY6VMeNoOIAM0ZWh9u3q2Q=
|
||||
github.com/panjf2000/ants/v2 v2.5.0/go.mod h1:cU93usDlihJZ5CfRGNDYsiBYvoilLvBF5Qp/BT2GNRE=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rodaine/table v1.0.1 h1:U/VwCnUxlVYxw8+NJiLIuCxA/xa6jL38MY3FYysVWWQ=
|
||||
@@ -52,7 +49,8 @@ golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapK
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
43
ipgeo/ipapicom.go
Normal file
43
ipgeo/ipapicom.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package ipgeo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func IPApiCom(ip string) (*IPGeoData, error) {
|
||||
url := "http://ip-api.com/json/" + ip + "?fields=status,message,country,regionName,city,isp,as"
|
||||
client := &http.Client{
|
||||
// 2 秒超时
|
||||
Timeout: 2 * time.Second,
|
||||
}
|
||||
req, _ := http.NewRequest("GET", url, nil)
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0")
|
||||
content, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Println("ip-api.com 请求超时(2s),请切换其他API使用")
|
||||
return nil, err
|
||||
}
|
||||
body, _ := ioutil.ReadAll(content.Body)
|
||||
res := gjson.ParseBytes(body)
|
||||
|
||||
if res.Get("status").String() != "success" {
|
||||
return &IPGeoData{}, errors.New("超过API阈值")
|
||||
}
|
||||
|
||||
re := regexp.MustCompile("[0-9]+")
|
||||
|
||||
return &IPGeoData{
|
||||
Asnumber: re.FindString(res.Get("as").String()),
|
||||
Country: res.Get("country").String(),
|
||||
City: res.Get("city").String(),
|
||||
Prov: res.Get("regionName").String(),
|
||||
Isp: res.Get("isp").String(),
|
||||
}, nil
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package ipgeo
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type IPGeoData struct {
|
||||
Asnumber string
|
||||
@@ -22,7 +24,11 @@ func GetSource(s string) Source {
|
||||
return IPSB
|
||||
case "IPINSIGHT":
|
||||
return IPInSight
|
||||
case "IPAPI.COM":
|
||||
return IPApiCom
|
||||
case "IPINFO":
|
||||
return IPInfo
|
||||
default:
|
||||
return nil
|
||||
return LeoIP
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestIPInfo(t *testing.T) {
|
||||
res, err := IPInfo("1.1.1.1")
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, res)
|
||||
assert.NotEmpty(t, res.Country)
|
||||
// assert.NotEmpty(t, res.Country)
|
||||
assert.NotEmpty(t, res.City)
|
||||
assert.NotEmpty(t, res.Prov)
|
||||
}
|
||||
@@ -41,3 +41,12 @@ func TestIPInSight(t *testing.T) {
|
||||
// 这个库有时候不提供城市信息,返回值为""
|
||||
//assert.NotEmpty(t, res.City)
|
||||
}
|
||||
|
||||
func TestIPApiCom(t *testing.T) {
|
||||
res, err := IPApiCom("1.1.1.1")
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, res)
|
||||
assert.NotEmpty(t, res.Country)
|
||||
assert.NotEmpty(t, res.City)
|
||||
assert.NotEmpty(t, res.Prov)
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
)
|
||||
|
||||
func IPInfo(ip string) (*IPGeoData, error) {
|
||||
|
||||
resp, err := http.Get("https://ipinfo.io/" + ip + "?token=" + token.ipinfo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -21,9 +20,17 @@ func IPInfo(ip string) (*IPGeoData, error) {
|
||||
|
||||
res := gjson.ParseBytes(body)
|
||||
|
||||
var country string
|
||||
|
||||
if res.Get("country").String() == "HK" || res.Get("country").String() == "TW" {
|
||||
country = "CN"
|
||||
}
|
||||
|
||||
return &IPGeoData{
|
||||
Country: res.Get("country").String(),
|
||||
City: res.Get("city").String(),
|
||||
Prov: res.Get("region").String(),
|
||||
Asnumber: res.Get("asn").Get("asn").String(),
|
||||
Country: country,
|
||||
City: res.Get("city").String(),
|
||||
Prov: res.Get("region").String(),
|
||||
Isp: res.Get("asn").Get("domain").String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@ package ipgeo
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
@@ -11,7 +13,7 @@ import (
|
||||
func IPSB(ip string) (*IPGeoData, error) {
|
||||
url := "https://api.ip.sb/geoip/" + ip
|
||||
client := &http.Client{
|
||||
// 2秒超时
|
||||
// 2 秒超时
|
||||
Timeout: 2 * time.Second,
|
||||
}
|
||||
req, _ := http.NewRequest("GET", url, nil)
|
||||
@@ -19,11 +21,17 @@ func IPSB(ip string) (*IPGeoData, error) {
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:100.0) Gecko/20100101 Firefox/100.0")
|
||||
content, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Println("api.ip.sb 请求超时(2s),请切换其他API使用")
|
||||
return nil, err
|
||||
}
|
||||
body, _ := ioutil.ReadAll(content.Body)
|
||||
res := gjson.ParseBytes(body)
|
||||
|
||||
if res.Get("country").String() == "" {
|
||||
// 什么都拿不到,证明被Cloudflare风控了
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return &IPGeoData{
|
||||
Asnumber: res.Get("asn").String(),
|
||||
Country: res.Get("country").String(),
|
||||
|
||||
98
main.go
98
main.go
@@ -4,9 +4,12 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
fastTrace "github.com/xgadget-lab/nexttrace/fast_trace"
|
||||
"github.com/xgadget-lab/nexttrace/ipgeo"
|
||||
"github.com/xgadget-lab/nexttrace/printer"
|
||||
"github.com/xgadget-lab/nexttrace/reporter"
|
||||
@@ -14,31 +17,58 @@ import (
|
||||
"github.com/xgadget-lab/nexttrace/util"
|
||||
)
|
||||
|
||||
var tcpSYNFlag = flag.Bool("T", false, "Use TCP SYN for tracerouting (default port is 80)")
|
||||
var udpPackageFlag = flag.Bool("U", false, "Use UDP Package for tracerouting (default port is 53 in UDP)")
|
||||
var port = flag.Int("p", 80, "Set SYN Traceroute Port")
|
||||
var numMeasurements = flag.Int("q", 3, "Set the number of probes per each hop.")
|
||||
var parallelRequests = flag.Int("r", 18, "Set ParallelRequests number. It should be 1 when there is a multi-routing.")
|
||||
var maxHops = flag.Int("m", 30, "Set the max number of hops (max TTL to be reached).")
|
||||
var dataOrigin = flag.String("d", "LeoMoeAPI", "Choose IP Geograph Data Provider [LeoMoeAPI, IP.SB, IPInfo, IPInsight]")
|
||||
var rdnsenable = flag.Bool("rdns", false, "Set whether rDNS will be display")
|
||||
var routePath = flag.Bool("report", false, "Route Path")
|
||||
var realtimePrint = flag.Bool("realtime", false, "Output trace results in runtime")
|
||||
var tablePrint = flag.Bool("table", false, "Output trace results as table")
|
||||
var ver = flag.Bool("V", false, "Check Version")
|
||||
var fSet = flag.NewFlagSet("", flag.ExitOnError)
|
||||
var fastTest = fSet.Bool("f", false, "One-Key Fast Traceroute")
|
||||
var tcpSYNFlag = fSet.Bool("T", false, "Use TCP SYN for tracerouting (default port is 80)")
|
||||
var udpPackageFlag = fSet.Bool("U", false, "Use UDP Package for tracerouting (default port is 53 in UDP)")
|
||||
var port = fSet.Int("p", 80, "Set SYN Traceroute Port")
|
||||
var numMeasurements = fSet.Int("q", 3, "Set the number of probes per each hop.")
|
||||
var parallelRequests = fSet.Int("r", 18, "Set ParallelRequests number. It should be 1 when there is a multi-routing.")
|
||||
var maxHops = fSet.Int("m", 30, "Set the max number of hops (max TTL to be reached).")
|
||||
var dataOrigin = fSet.String("d", "LeoMoeAPI", "Choose IP Geograph Data Provider [LeoMoeAPI, IP.SB, IPInfo, IPInsight, IPAPI.com]")
|
||||
var noRdns = fSet.Bool("n", false, "Disable IP Reverse DNS lookup")
|
||||
var routePath = fSet.Bool("report", false, "Route Path")
|
||||
var tablePrint = fSet.Bool("table", false, "Output trace results as table")
|
||||
var ver = fSet.Bool("V", false, "Print Version")
|
||||
|
||||
func printArgHelp() {
|
||||
fmt.Println("\nArgs Error\nUsage : 'nexttrace [option...] HOSTNAME' or 'nexttrace HOSTNAME [option...]'\nOPTIONS: [-VTU] [-d DATAORIGIN.STR ] [ -m TTL ] [ -p PORT ] [ -q PROBES.COUNT ] [ -r PARALLELREQUESTS.COUNT ] [-rdns] [ -table ] -report")
|
||||
fSet.PrintDefaults()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
func flagApply() string {
|
||||
flag.Parse()
|
||||
printer.Version()
|
||||
|
||||
target := ""
|
||||
if len(os.Args) < 2 {
|
||||
printArgHelp()
|
||||
}
|
||||
|
||||
// flag parse
|
||||
if !strings.HasPrefix(os.Args[1], "-") {
|
||||
target = os.Args[1]
|
||||
fSet.Parse(os.Args[2:])
|
||||
} else {
|
||||
fSet.Parse(os.Args[1:])
|
||||
target = fSet.Arg(0)
|
||||
}
|
||||
|
||||
// Print Version
|
||||
if *ver {
|
||||
os.Exit(0)
|
||||
}
|
||||
ipArg := flag.Args()
|
||||
if flag.NArg() != 1 {
|
||||
fmt.Println("Args Error\nUsage : ./nexttrace [-T] [-rdns] [-displayMode <displayMode>] [-d <dataOrigin> ] [ -m <hops> ] [ -p <port> ] [ -q <probes> ] [ -r <parallelrequests> ] <hostname>")
|
||||
os.Exit(2)
|
||||
|
||||
// -f Fast Test
|
||||
if *fastTest {
|
||||
fastTrace.FastTest(*tcpSYNFlag)
|
||||
os.Exit(0)
|
||||
}
|
||||
return ipArg[0]
|
||||
|
||||
if target == "" {
|
||||
printArgHelp()
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
func main() {
|
||||
@@ -49,7 +79,14 @@ func main() {
|
||||
log.Fatalln("Traceroute requires root/sudo privileges.")
|
||||
}
|
||||
|
||||
ip := util.DomainLookUp(domain)
|
||||
var ip net.IP
|
||||
|
||||
if *tcpSYNFlag || *udpPackageFlag {
|
||||
ip = util.DomainLookUp(domain, true)
|
||||
} else {
|
||||
ip = util.DomainLookUp(domain, false)
|
||||
}
|
||||
|
||||
printer.PrintTraceRouteNav(ip, domain, *dataOrigin)
|
||||
|
||||
var m trace.Method = ""
|
||||
@@ -73,12 +110,12 @@ func main() {
|
||||
MaxHops: *maxHops,
|
||||
NumMeasurements: *numMeasurements,
|
||||
ParallelRequests: *parallelRequests,
|
||||
RDns: *rdnsenable,
|
||||
RDns: !*noRdns,
|
||||
IPGeoSource: ipgeo.GetSource(*dataOrigin),
|
||||
Timeout: 2 * time.Second,
|
||||
Timeout: 1 * time.Second,
|
||||
}
|
||||
|
||||
if m == trace.ICMPTrace && !*tablePrint {
|
||||
if !*tablePrint {
|
||||
conf.RealtimePrinter = printer.RealtimePrinter
|
||||
}
|
||||
|
||||
@@ -88,21 +125,12 @@ func main() {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
if *routePath {
|
||||
r := reporter.New(res, ip.String())
|
||||
r.Print()
|
||||
return
|
||||
}
|
||||
|
||||
if m == trace.ICMPTrace && *tablePrint {
|
||||
if *tablePrint {
|
||||
printer.TracerouteTablePrinter(res)
|
||||
}
|
||||
|
||||
if m == trace.TCPTrace || m == trace.UDPTrace {
|
||||
if *realtimePrint {
|
||||
printer.TraceroutePrinter(res)
|
||||
} else {
|
||||
printer.TracerouteTablePrinter(res)
|
||||
}
|
||||
if *routePath {
|
||||
r := reporter.New(res, ip.String())
|
||||
r.Print()
|
||||
}
|
||||
}
|
||||
|
||||
243
nt_install.sh
243
nt_install.sh
@@ -1,254 +1,115 @@
|
||||
#!/bin/bash
|
||||
|
||||
usrPath="/usr/local/bin"
|
||||
Green_font="\033[32m"
|
||||
Yellow_font="\033[33m"
|
||||
Red_font="\033[31m"
|
||||
Font_suffix="\033[0m"
|
||||
Info="${Green_font}[Info]${Font_suffix}"
|
||||
Error="${Red_font}[Error]${Font_suffix}"
|
||||
Tips="${Green_font}[Tips]${Font_suffix}"
|
||||
Temp_path="/var/tmp/nexttrace"
|
||||
|
||||
checkRootPermit() {
|
||||
[[ $EUID -ne 0 ]] && echo "请使用sudo/root权限运行本脚本" && exit 1
|
||||
[[ $EUID -ne 0 ]] && echo -e "${Error} 请使用sudo/root权限运行本脚本" && exit 1
|
||||
}
|
||||
|
||||
checkSystemArch() {
|
||||
arch=$(uname -m)
|
||||
if [[ $arch == "x86_64" ]]; then
|
||||
archParam="amd64"
|
||||
archParam="amd64"
|
||||
fi
|
||||
|
||||
if [[ $arch == "aarch64" ]]; then
|
||||
archParam="arm64"
|
||||
fi
|
||||
|
||||
if [[ $arch == "arm64" ]]; then
|
||||
archParam="arm64"
|
||||
fi
|
||||
|
||||
if [[ $archParam == "" ]]; then
|
||||
echo "未知的系统架构,请联系作者"
|
||||
exit 1
|
||||
archParam="arm64"
|
||||
fi
|
||||
}
|
||||
|
||||
checkSystemDistribution() {
|
||||
case "$OSTYPE" in
|
||||
darwin*)
|
||||
osDistribution="darwin"
|
||||
downPath="/var/tmp/nexttrace"
|
||||
;;
|
||||
linux*)
|
||||
osDistribution="linux"
|
||||
downPath="/var/tmp/nexttrace"
|
||||
;;
|
||||
linux*)
|
||||
osDistribution="linux"
|
||||
downPath="/usr/local/bin/nexttrace"
|
||||
;;
|
||||
*)
|
||||
echo "unknown: $OSTYPE"
|
||||
exit 1
|
||||
;;
|
||||
echo "unknown: $OSTYPE"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
getLocation() {
|
||||
echo "正在获取地理位置信息..."
|
||||
countryCode=$(curl -s "http://ip-api.com/line/?fields=countryCode")
|
||||
}
|
||||
|
||||
installWgetPackage() {
|
||||
# macOS should install wget originally. Nothing to do
|
||||
echo "wget 正在安装中..."
|
||||
echo -e "${Info} wget 正在安装中..."
|
||||
# try apt
|
||||
# 是时候直接使用 APT 来管理包了
|
||||
apt -h &>/dev/null
|
||||
apt -h &> /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
# 先更新一下数据源,有些机器数据源比较老可能会404
|
||||
apt update -y &>/dev/null
|
||||
apt install wget -y &>/dev/null
|
||||
# 先更新一下数据源,有些机器数据源比较老可能会404
|
||||
apt update -y &> /dev/null
|
||||
apt install wget -y &> /dev/null
|
||||
fi
|
||||
|
||||
# try yum
|
||||
yum -h &>/dev/null
|
||||
yum -h &> /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
yum -y update &>/dev/null
|
||||
yum install wget -y &>/dev/null
|
||||
yum install wget -y &> /dev/null
|
||||
fi
|
||||
|
||||
# try dnf
|
||||
dnf -h &>/dev/null
|
||||
dnf -h &> /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
dnf check-update &>/dev/null
|
||||
dnf install wget -y &>/dev/null
|
||||
dnf install wget -y &> /dev/null
|
||||
fi
|
||||
|
||||
# try pacman
|
||||
pacman -h &>/dev/null
|
||||
pacman -h &> /dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
pacman -Sy &>/dev/null
|
||||
pacman -S wget &>/dev/null
|
||||
fi
|
||||
|
||||
# 有的发行版自带的wget,只有 --help 参数
|
||||
wget --help &>/dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "wget 安装失败"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
installJqPackage() {
|
||||
# macOS should install wget originally. Nothing to do
|
||||
echo "jq 正在安装中..."
|
||||
# try apt
|
||||
apt-get -h &>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
# 先更新一下数据源,有些机器数据源比较老可能会404
|
||||
apt-get update -y &>/dev/null
|
||||
apt-get install jq -y &>/dev/null
|
||||
pacman -Sy
|
||||
pacman -S wget
|
||||
fi
|
||||
|
||||
# try yum
|
||||
yum -h &>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
yum -y update &>/dev/null
|
||||
yum install jq -y &>/dev/null
|
||||
fi
|
||||
|
||||
# try dnf
|
||||
dnf -h &>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
dnf check-update &>/dev/null
|
||||
dnf install jq -y &>/dev/null
|
||||
fi
|
||||
|
||||
# try pacman
|
||||
pacman -h &>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
pacman -Sy &>/dev/null
|
||||
pacman -S jq &>/dev/null
|
||||
fi
|
||||
|
||||
jq -h &>/dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "jq 安装失败"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
checkWgetPackage() {
|
||||
wget -h &>/dev/null
|
||||
wget -h &> /dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
read -r -p "您还没有安装wget,是否安装? (y/n)" input
|
||||
|
||||
case $input in
|
||||
[yY][eE][sS] | [yY])
|
||||
installWgetPackage
|
||||
;;
|
||||
|
||||
[nN][oO] | [nN])
|
||||
echo "您选择了取消安装,脚本即将退出"
|
||||
exit 1
|
||||
;;
|
||||
|
||||
*)
|
||||
installWgetPackage
|
||||
;;
|
||||
esac
|
||||
installWgetPackage
|
||||
fi
|
||||
}
|
||||
|
||||
checkVersion() {
|
||||
echo "正在检查版本..."
|
||||
version=$(curl -sL https://api.github.com/repos/xgadget-lab/nexttrace/releases/latest | jq -r '.tag_name')
|
||||
if [[ $version == "" ]]; then
|
||||
echo "获取版本失败,请检查网络连接"
|
||||
exit 1
|
||||
fi
|
||||
echo 当前最新release版本:${version}
|
||||
read -r -p "是否安装/更新软件? (y/n)" input
|
||||
case $input in
|
||||
[yY][eE][sS] | [yY])
|
||||
break
|
||||
;;
|
||||
[nN][oO] | [nN])
|
||||
echo "您选择了取消安装/更新,脚本即将退出"
|
||||
exit 1
|
||||
;;
|
||||
*)
|
||||
break
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
checkJqPackage() {
|
||||
jq -h &>/dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "您还没有安装jq, 当您取消安装,我们会使用awk获取当前版本号。"
|
||||
read -r -p "但是如遇Github变更API,这可能会存在问题,是否安装? (y/n)" input
|
||||
|
||||
case $input in
|
||||
[yY][eE][sS] | [yY])
|
||||
installJqPackage
|
||||
;;
|
||||
|
||||
[nN][oO] | [nN])
|
||||
echo "您选择了取消安装"
|
||||
return 0
|
||||
;;
|
||||
|
||||
*)
|
||||
installJqPackage
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
downloadBinrayFile() {
|
||||
echo "正在获取最新版的 NextTrace 发行版文件信息..."
|
||||
checkJqPackage
|
||||
echo -e "${Info} 获取最新版的 NextTrace 发行版文件信息"
|
||||
# 简单说明一下,Github提供了一个API,可以获取最新发行版本的二进制文件下载地址(对应的是browser_download_url),根据刚刚测得的osDistribution、archParam,获取对应的下载地址
|
||||
if [[ $? -eq 1 ]]; then
|
||||
# 支持 jq 不回退
|
||||
# echo nexttrace_${osDistribution}_${archParam}
|
||||
latestURL=$(curl -s https://api.github.com/repos/xgadget-lab/nexttrace/releases/latest | jq ".assets[] | select(.name == \"nexttrace_${osDistribution}_${archParam}\") | .browser_download_url")
|
||||
latestURL=${latestURL:1:-1}
|
||||
else
|
||||
# 不支持 jq,用户拒绝安装,回退 awk
|
||||
latestURL=$(curl -s https://api.github.com/repos/xgadget-lab/nexttrace/releases/latest | grep -i "browser_download_url.*${osDistribution}.*${archParam}" | awk -F '"' '{print $4}')
|
||||
fi
|
||||
latestURL=$(curl -s https://api.github.com/repos/xgadget-lab/nexttrace/releases/latest | grep -i "browser_download_url.*${osDistribution}.*${archParam}" | awk -F '"' '{print $4}')
|
||||
|
||||
if [ "$countryCode" == "CN" ]; then
|
||||
read -r -p "检测到国内网络环境,是否使用镜像下载以加速(y/n)" input
|
||||
case $input in
|
||||
[yY][eE][sS] | [yY])
|
||||
latestURL="https://ghproxy.com/"$latestURL
|
||||
;;
|
||||
|
||||
[nN][oO] | [nN])
|
||||
echo "您选择了不使用镜像,下载可能会变得异常缓慢,或者失败"
|
||||
;;
|
||||
|
||||
*)
|
||||
latestURL="https://ghproxy.com/"$latestURL
|
||||
;;
|
||||
esac
|
||||
echo -e "${Info} 检测到国内环境,正在使用镜像下载"
|
||||
latestURL="https://ghproxy.com/"$latestURL
|
||||
fi
|
||||
|
||||
echo "正在下载 NextTrace 二进制文件..."
|
||||
wget -O ${downPath} ${latestURL} &>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "NextTrace 现在已经在您的系统中可用"
|
||||
changeMode
|
||||
mv ${downPath} ${usrPath}
|
||||
if [[ ${osDistribution} == "macOS" ]]; then
|
||||
xattr -r -d com.apple.quarantine ${usrPath}/nexttrace
|
||||
fi
|
||||
|
||||
echo -e "${Info} 正在下载 NextTrace 二进制文件..."
|
||||
wget -O ${Temp_path} ${latestURL} &> /dev/null
|
||||
if [ $? -eq 0 ];
|
||||
then
|
||||
changeMode
|
||||
mv ${Temp_path} ${downPath}
|
||||
echo -e "${Info} NextTrace 现在已经在您的系统中可用"
|
||||
else
|
||||
echo "NextTrace 下载失败,请检查您的网络是否正常"
|
||||
exit 1
|
||||
echo -e "${Error} NextTrace 下载失败,请检查您的网络是否正常"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
changeMode() {
|
||||
chmod +x ${downPath} &>/dev/null
|
||||
chmod +x ${Temp_path} &> /dev/null
|
||||
}
|
||||
|
||||
runBinrayFileHelp() {
|
||||
if [ -e ${usrPath} ]; then
|
||||
${usrPath}/nexttrace -h
|
||||
if [ -e ${downPath} ]; then
|
||||
${downPath} -V
|
||||
echo -e "${Tips} 一切准备就绪!使用命令 nexttrace 1.1.1.1 开始您的第一次路由测试吧~ 更多进阶命令玩法可以用 nexttrace -h 查看哦\n 关于软件卸载,因为nexttrace是绿色版单文件,卸载只需输入命令 rm /usr/local/bin/nexttrace 即可"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -257,8 +118,6 @@ checkRootPermit
|
||||
checkSystemDistribution
|
||||
checkSystemArch
|
||||
checkWgetPackage
|
||||
# TODO: 检查版本并更新
|
||||
#checkVersion
|
||||
|
||||
# Download Procedure
|
||||
getLocation
|
||||
|
||||
@@ -5,8 +5,12 @@ import (
|
||||
"net"
|
||||
)
|
||||
|
||||
var version = "v0.0.0.alpha"
|
||||
var buildDate = ""
|
||||
var commitID = ""
|
||||
|
||||
func Version() {
|
||||
fmt.Print("NextTrace v0.1.2-Beta ") // TODO get version from git
|
||||
fmt.Println("NextTrace", version, buildDate, commitID)
|
||||
fmt.Println("XGadget-lab Leo (leo.moe) & Vincent (vincent.moe) & zhshch (xzhsh.ch)")
|
||||
}
|
||||
|
||||
|
||||
@@ -52,19 +52,16 @@ func formatIpGeoData(ip string, data *ipgeo.IPGeoData) string {
|
||||
// TODO: 判断阿里云和腾讯云内网,数据不足,有待进一步完善
|
||||
// TODO: 移动IDC判断到Hop.fetchIPData函数,减少API调用
|
||||
if strings.HasPrefix(ip, "9.") {
|
||||
res = append(res, "局域网", "腾讯云")
|
||||
res = append(res, "LAN Address")
|
||||
} else if strings.HasPrefix(ip, "11.") {
|
||||
res = append(res, "局域网", "阿里云")
|
||||
res = append(res, "LAN Address")
|
||||
} else if data.Country == "" {
|
||||
res = append(res, "局域网")
|
||||
res = append(res, "LAN Address")
|
||||
} else {
|
||||
// 有些IP的归属信息为空,这个时候将ISP的信息填入
|
||||
if data.Owner == "" {
|
||||
data.Owner = data.Isp
|
||||
}
|
||||
if data.District != "" {
|
||||
data.City = data.City + ", " + data.District
|
||||
}
|
||||
if data.Prov == "" && data.City == "" {
|
||||
// anyCast或是骨干网数据不应该有国家信息
|
||||
data.Owner = data.Owner + ", " + data.Owner
|
||||
|
||||
@@ -2,16 +2,17 @@ package printer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/xgadget-lab/nexttrace/ipgeo"
|
||||
"github.com/xgadget-lab/nexttrace/trace"
|
||||
"github.com/xgadget-lab/nexttrace/util"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/xgadget-lab/nexttrace/ipgeo"
|
||||
"github.com/xgadget-lab/nexttrace/trace"
|
||||
"github.com/xgadget-lab/nexttrace/util"
|
||||
)
|
||||
|
||||
func TestPrintTraceRouteNav(t *testing.T) {
|
||||
PrintTraceRouteNav(util.DomainLookUp("1.1.1.1"), "1.1.1.1", "dataOrigin")
|
||||
PrintTraceRouteNav(util.DomainLookUp("1.1.1.1", false), "1.1.1.1", "dataOrigin")
|
||||
}
|
||||
|
||||
var testGeo = &ipgeo.IPGeoData{
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func RealtimePrinter(res *trace.Result, ttl int) {
|
||||
fmt.Print(ttl)
|
||||
fmt.Print(ttl + 1)
|
||||
for i := range res.Hops[ttl] {
|
||||
HopPrinter(res.Hops[ttl][i])
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ package printer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/xgadget-lab/nexttrace/ipgeo"
|
||||
"strings"
|
||||
|
||||
"github.com/xgadget-lab/nexttrace/ipgeo"
|
||||
|
||||
"github.com/xgadget-lab/nexttrace/trace"
|
||||
|
||||
"github.com/fatih/color"
|
||||
@@ -38,7 +39,7 @@ func TracerouteTablePrinter(res *trace.Result) {
|
||||
if data.City != "" {
|
||||
tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Country+", "+data.Prov+", "+data.City, data.Owner)
|
||||
} else {
|
||||
tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Country, data.Owner)
|
||||
tbl.AddRow(data.Hop, data.IP, data.Latency, data.Asnumber, data.Country + ", " + data.Prov, data.Owner)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -73,16 +74,18 @@ func tableDataGenerator(h trace.Hop) *rowData {
|
||||
Hop: fmt.Sprint(h.TTL),
|
||||
IP: IP,
|
||||
Latency: lantency,
|
||||
Country: "局域网",
|
||||
Owner: "腾讯云",
|
||||
Country: "LAN Address",
|
||||
Prov: "LAN Address",
|
||||
Owner: "",
|
||||
}
|
||||
} else if strings.HasPrefix(IP, "11.") {
|
||||
return &rowData{
|
||||
Hop: fmt.Sprint(h.TTL),
|
||||
IP: IP,
|
||||
Latency: lantency,
|
||||
Country: "局域网",
|
||||
Owner: "阿里云",
|
||||
Country: "LAN Address",
|
||||
Prov: "LAN Address",
|
||||
Owner: "",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -128,10 +128,18 @@ func (r *reporter) InitialBaseData() Reporter {
|
||||
}
|
||||
|
||||
func (r *reporter) Print() {
|
||||
var beforeActiveTTL uint16 = 1
|
||||
var beforeActiveTTL uint16 = 0
|
||||
r.InitialBaseData()
|
||||
// 尝试首个有效 TTL
|
||||
for i := uint16(0); i < r.targetTTL; i++ {
|
||||
if len(r.routeReport[i]) != 0 {
|
||||
beforeActiveTTL = i
|
||||
// 找到以后便不再循环
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for i := uint16(1); i < r.targetTTL; i++ {
|
||||
for i := beforeActiveTTL; i < r.targetTTL; i++ {
|
||||
// 计算该TTL内的数据长度,如果为0,则代表没有有效数据
|
||||
if len(r.routeReport[i]) == 0 {
|
||||
// 跳过改跃点的数据整理
|
||||
@@ -139,7 +147,7 @@ func (r *reporter) Print() {
|
||||
}
|
||||
nodeReport := r.routeReport[i][0]
|
||||
|
||||
if i == 1 {
|
||||
if i == beforeActiveTTL {
|
||||
fmt.Printf("AS%s %s「%s『%s", nodeReport.asn, nodeReport.isp, nodeReport.geo[0], nodeReport.geo[1])
|
||||
} else {
|
||||
nodeReportBefore := r.routeReport[beforeActiveTTL][0]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package trace
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
@@ -74,19 +75,32 @@ func (t *ICMPTracer) listenICMP() {
|
||||
if msg.N == nil {
|
||||
continue
|
||||
}
|
||||
rm, err := icmp.ParseMessage(1, msg.Msg[:*msg.N])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
|
||||
if binary.BigEndian.Uint16(msg.Msg[32:34]) != uint16(os.Getpid()&0xffff) {
|
||||
// 如果类型为应答消息,且应答消息包的进程ID与主进程相同时不跳过
|
||||
if msg.Msg[0] != 0 || binary.BigEndian.Uint16(msg.Msg[4:6]) != uint16(os.Getpid()&0xffff) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
switch rm.Type {
|
||||
case ipv4.ICMPTypeTimeExceeded:
|
||||
t.handleICMPMessage(msg, 0, rm.Body.(*icmp.TimeExceeded).Data)
|
||||
case ipv4.ICMPTypeEchoReply:
|
||||
t.handleICMPMessage(msg, 1, rm.Body.(*icmp.Echo).Data)
|
||||
default:
|
||||
// log.Println("received icmp message of unknown type", rm.Type)
|
||||
|
||||
dstip := net.IP(msg.Msg[24:28])
|
||||
if dstip.Equal(t.DestIP) || dstip.Equal(net.IPv4zero) {
|
||||
// 匹配再继续解析包,否则直接丢弃
|
||||
rm, err := icmp.ParseMessage(1, msg.Msg[:*msg.N])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
switch rm.Type {
|
||||
case ipv4.ICMPTypeTimeExceeded:
|
||||
t.handleICMPMessage(msg, 0, rm.Body.(*icmp.TimeExceeded).Data)
|
||||
case ipv4.ICMPTypeEchoReply:
|
||||
t.handleICMPMessage(msg, 1, rm.Body.(*icmp.Echo).Data)
|
||||
default:
|
||||
// log.Println("received icmp message of unknown type", rm.Type)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -64,14 +64,26 @@ func (t *TCPTracer) Execute() (*Result, error) {
|
||||
t.sem = semaphore.NewWeighted(int64(t.ParallelRequests))
|
||||
|
||||
for ttl := 1; ttl <= t.MaxHops; ttl++ {
|
||||
// 如果到达最终跳,则退出
|
||||
if t.final != -1 && ttl > t.final {
|
||||
break
|
||||
}
|
||||
for i := 0; i < t.NumMeasurements; i++ {
|
||||
t.wg.Add(1)
|
||||
go t.send(ttl)
|
||||
|
||||
}
|
||||
if t.RealtimePrinter != nil {
|
||||
// 对于实时模式,应该按照TTL进行并发请求
|
||||
t.wg.Wait()
|
||||
t.RealtimePrinter(&t.res, ttl-1)
|
||||
}
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
}
|
||||
t.wg.Wait()
|
||||
// 如果是表格模式,则一次性并发请求
|
||||
if t.RealtimePrinter == nil {
|
||||
t.wg.Wait()
|
||||
}
|
||||
t.res.reduce(t.final)
|
||||
|
||||
return &t.res, nil
|
||||
@@ -88,18 +100,21 @@ func (t *TCPTracer) listenICMP() {
|
||||
if msg.N == nil {
|
||||
continue
|
||||
}
|
||||
rm, err := icmp.ParseMessage(1, msg.Msg[:*msg.N])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
switch rm.Type {
|
||||
case ipv4.ICMPTypeTimeExceeded:
|
||||
t.handleICMPMessage(msg, rm.Body.(*icmp.TimeExceeded).Data)
|
||||
case ipv4.ICMPTypeDestinationUnreachable:
|
||||
t.handleICMPMessage(msg, rm.Body.(*icmp.DstUnreach).Data)
|
||||
default:
|
||||
//log.Println("received icmp message of unknown type", rm.Type)
|
||||
dstip := net.IP(msg.Msg[24:28])
|
||||
if dstip.Equal(t.DestIP) {
|
||||
rm, err := icmp.ParseMessage(1, msg.Msg[:*msg.N])
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
}
|
||||
switch rm.Type {
|
||||
case ipv4.ICMPTypeTimeExceeded:
|
||||
t.handleICMPMessage(msg, rm.Body.(*icmp.TimeExceeded).Data)
|
||||
case ipv4.ICMPTypeDestinationUnreachable:
|
||||
t.handleICMPMessage(msg, rm.Body.(*icmp.DstUnreach).Data)
|
||||
default:
|
||||
//log.Println("received icmp message of unknown type", rm.Type)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,11 +39,11 @@ func formatIpGeoData(ip string, data *ipgeo.IPGeoData) string {
|
||||
// TODO: 判断阿里云和腾讯云内网,数据不足,有待进一步完善
|
||||
// TODO: 移动IDC判断到Hop.fetchIPData函数,减少API调用
|
||||
if strings.HasPrefix(ip, "9.") {
|
||||
res = append(res, "局域网", "腾讯云")
|
||||
res = append(res, "LAN Address", "")
|
||||
} else if strings.HasPrefix(ip, "11.") {
|
||||
res = append(res, "局域网", "阿里云")
|
||||
res = append(res, "LAN Address", "")
|
||||
} else if data.Country == "" {
|
||||
res = append(res, "局域网")
|
||||
res = append(res, "LAN Address")
|
||||
} else {
|
||||
// 有些IP的归属信息为空,这个时候将ISP的信息填入
|
||||
if data.Owner == "" {
|
||||
|
||||
19
trace/udp.go
19
trace/udp.go
@@ -53,13 +53,26 @@ func (t *UDPTracer) Execute() (*Result, error) {
|
||||
|
||||
t.sem = semaphore.NewWeighted(int64(t.ParallelRequests))
|
||||
for ttl := 1; ttl <= t.MaxHops; ttl++ {
|
||||
// 如果到达最终跳,则退出
|
||||
if t.final != -1 && ttl > t.final {
|
||||
break
|
||||
}
|
||||
for i := 0; i < t.NumMeasurements; i++ {
|
||||
t.wg.Add(1)
|
||||
go t.send(ttl)
|
||||
}
|
||||
}
|
||||
|
||||
t.wg.Wait()
|
||||
}
|
||||
if t.RealtimePrinter != nil {
|
||||
// 对于实时模式,应该按照TTL进行并发请求
|
||||
t.wg.Wait()
|
||||
t.RealtimePrinter(&t.res, ttl-1)
|
||||
}
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
}
|
||||
// 如果是表格模式,则一次性并发请求
|
||||
if t.RealtimePrinter == nil {
|
||||
t.wg.Wait()
|
||||
}
|
||||
t.res.reduce(t.final)
|
||||
|
||||
return &t.res, nil
|
||||
|
||||
21
util/util.go
21
util/util.go
@@ -25,7 +25,7 @@ func LocalIPPort(dstip net.IP) (net.IP, int) {
|
||||
return nil, -1
|
||||
}
|
||||
|
||||
func DomainLookUp(host string) net.IP {
|
||||
func DomainLookUp(host string, ipv4Only bool) net.IP {
|
||||
ips, err := net.LookupIP(host)
|
||||
if err != nil {
|
||||
fmt.Println("Domain " + host + " Lookup Fail.")
|
||||
@@ -36,17 +36,20 @@ func DomainLookUp(host string) net.IP {
|
||||
var ipv6Flag = false
|
||||
|
||||
for _, ip := range ips {
|
||||
ipSlice = append(ipSlice, ip)
|
||||
// 仅返回ipv4的ip
|
||||
// if ip.To4() != nil {
|
||||
// ipSlice = append(ipSlice, ip)
|
||||
// } else {
|
||||
// ipv6Flag = true
|
||||
// }
|
||||
if ipv4Only {
|
||||
// 仅返回ipv4的ip
|
||||
if ip.To4() != nil {
|
||||
ipSlice = append(ipSlice, ip)
|
||||
} else {
|
||||
ipv6Flag = true
|
||||
}
|
||||
} else {
|
||||
ipSlice = append(ipSlice, ip)
|
||||
}
|
||||
}
|
||||
|
||||
if ipv6Flag {
|
||||
fmt.Println("[Info] IPv6 Traceroute is not supported right now.")
|
||||
fmt.Println("[Info] IPv6 TCP/UDP Traceroute is not supported right now.")
|
||||
if len(ipSlice) == 0 {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user