Compare commits

..

40 Commits

Author SHA1 Message Date
zhshch2002
5fe1110ab3 update: 减少GitHub Actions重复运行 2022-05-25 16:52:26 +08:00
tsosunchia
812c953976 Delete bug_cn.md 2022-05-25 16:08:42 +08:00
tsosunchia
f89505ab87 Update issue templates 2022-05-25 16:08:00 +08:00
tsosunchia
640eb8c02d Create bug_cn.md 2022-05-25 15:57:28 +08:00
tsosunchia
fc3462ff9e Merge pull request #9 from tsosunchia/main
update README.md 更改安装方法说明
2022-05-25 15:33:57 +08:00
tsosunchia
071a6b124a update README.md 更改安装方法说明 2022-05-25 15:33:13 +08:00
tsosunchia
45a8cf21f6 Merge pull request #8 from tsosunchia/main
add 添加定时更新任务功能
2022-05-25 15:18:53 +08:00
tsosunchia
1315efa4d2 Merge branch 'xgadget-lab:main' into main 2022-05-25 15:16:22 +08:00
tsosunchia
c2bd51faab add 自动添加定时更新任务功能 2022-05-25 15:16:02 +08:00
sjlleo
f1ce2bbb77 update: 为什么我们要尝试自己维护骨干网的IP数据库 2022-05-25 14:43:02 +08:00
sjlleo
a18bf1889b add: faq 2022-05-25 14:16:38 +08:00
tsosunchia
12c93de8c5 Merge pull request #6 from tsosunchia/main
Implement update function
2022-05-25 14:04:09 +08:00
tsosunchia
8280b62881 Merge remote-tracking branch 'refs/remotes/origin/main' 2022-05-25 14:02:40 +08:00
tsosunchia
608847b1cb Implement update function 2022-05-25 14:01:21 +08:00
tsosunchia
b4838ba402 Merge pull request #5 from tsosunchia/main
添加macOS BREW包管理器
2022-05-25 13:38:06 +08:00
tsosunchia
fbd70a8eb1 add BREW PACKAGE MANAGER 2022-05-25 13:36:54 +08:00
tsosunchia
fd1632fccb Merge branch 'xgadget-lab:main' into main 2022-05-25 13:18:06 +08:00
zhshch2002
4a725d2c48 revert: 96bb323f "update: test.yml" 2022-05-25 13:15:06 +08:00
zhshch2002
96bb323f72 update: test.yml 2022-05-25 13:09:30 +08:00
zhshch2002
961c29e499 fix: build sh version var 2022-05-25 13:04:18 +08:00
zhshch2002
870d1f3b5a update: get version from git 2022-05-25 12:56:47 +08:00
sjlleo
ebd435db53 refactor: report 支持多线程和rDNS反查 2022-05-25 10:48:47 +08:00
sjlleo
53b2249ce5 fix: 尝试性修复部分 CentOS 6、Debian8 下 wget 安装失败 2022-05-25 08:13:57 +08:00
sjlleo
7215a1e2b7 update: 更新 NextTrace 最新的 CLI 参数 2022-05-24 21:33:18 +08:00
sjlleo
24b06d2fd7 fix: strange error 2022-05-24 21:10:28 +08:00
sjlleo
f1f95dff29 fix: route-path 显示不全的问题 2022-05-24 21:05:59 +08:00
zhshch2002
0d9b8c8861 fix: typo 2022-05-24 18:27:22 +08:00
zhshch2002
290524b502 add: Version print 2022-05-24 18:14:31 +08:00
sjlleo
905ef267f2 improve 2022-05-24 11:57:50 +08:00
sjlleo
9720c19153 add: route report 2022-05-24 11:57:10 +08:00
sjlleo
f2c494441b Add files via upload 2022-05-24 11:45:02 +08:00
sjlleo
44aba64505 Delete screenshot2.png 2022-05-24 11:44:30 +08:00
sjlleo
c0be6774c1 Delete screenshot3.png 2022-05-24 11:44:19 +08:00
zhshch2002
23693895e4 update: GitHub actions build.yml 2022-05-24 11:22:13 +08:00
zhshch2002
7edebf938b remove: test for TestIPSB api 2022-05-24 11:20:23 +08:00
zhshch2002
d4a176f864 add: test github actions 2022-05-24 11:17:43 +08:00
zhshch2002
69388c956e remove: listener_channe, signal, taskgroup 2022-05-24 11:17:43 +08:00
sjlleo
738ff949b1 Special Version for my friends - missuo 2022-05-24 10:59:38 +08:00
sjlleo
b59f0eb9da update: rdns 功能介绍 2022-05-24 09:21:30 +08:00
tsosunchia
24eedfa3fe Update nt_install.sh 2022-05-23 23:24:42 +08:00
22 changed files with 610 additions and 277 deletions

View File

@@ -7,6 +7,10 @@ DEBUG_MODE=${2}
TARGET_DIR="dist"
PLATFORMS="darwin/amd64 darwin/arm64 linux/amd64 linux/arm64 linux/mips"
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,15 +25,15 @@ 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

40
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View 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 前,请隐去您的隐私信息 -->
## 请附上出错时软件输出的错误信息

View 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.

View File

@@ -1,17 +1,31 @@
on:
push: # 每次带有 tag 的 push 候触发
tags:
- 'v*'
push:
pull_request:
name: Build Release
name: Test & Build Release
jobs:
release:
Test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master # checkout 代码
- uses: actions/setup-go@v2 # 配置 Go 环境
- uses: actions/checkout@v3
- uses: actions/setup-go@v2
with:
go-version: "1.18" # 改成自己的版本
go-version: "1.18"
- name: Test
run: go test -v -coverprofile='coverage.out' -covermode=count ./...
Build:
needs: test
if: contains('{{ github.ref }}', 'refs/tags/v')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v2
with:
go-version: "1.18"
- run: bash .cross_compile.sh

View File

@@ -6,14 +6,14 @@
# NextTrace
一款开源的可视化路由跟踪工具使用Golang开发。
一款开源的可视化路由跟踪工具,使用 Golang 开发。
## How To Use
### Install
```bash
bash -c "$(curl -Ls https://raw.githubusercontent.com/xgadget-lab/nexttrace/main/nt_install.sh)"
curl -Ls https://raw.githubusercontent.com/xgadget-lab/nexttrace/main/nt_install.sh -O && sudo bash nt_install.sh
```
### Get Started
@@ -24,9 +24,6 @@ bash -c "$(curl -Ls https://raw.githubusercontent.com/xgadget-lab/nexttrace/main
# IPv4 ICMP Trace
nexttrace 1.0.0.1
# 获得 route-path
nexttrace -report 1.0.0.1
# 表格打印一次性输出全部跳数需等待20-40秒
nexttrace -table 1.0.0.1
@@ -35,6 +32,7 @@ nexttrace 2606:4700:4700::1111
```
`NextTrace`也可以使用`TCP``UDP`协议发起`Traceroute`请求,不过目前只支持`IPv4`
```bash
# TCP SYN Trace
nexttrace -T www.bing.com
@@ -48,24 +46,36 @@ nexttrace -U 1.0.0.1
nexttrace -U -p 53 1.0.0.1
```
### IP数据库
`NextTrace`也同样支持一些进阶功能,如 IP 反向解析、并发数控制、模式切换等
目前使用的IP数据库默认为我们自己搭建的API服务如果后期遇到滥用我们可能会选择关闭。
```bash
# 无并发,每次只发送一个探测包
nexttrace -r 1 www.hkix.net
我们也会在后期开放服务端源代码您也可以根据该项目的源码自行搭建属于您的API服务器。
# 打开IP反向解析功能在IPv6的骨干网定位辅助有较大帮助
nexttrace -rdns www.bbix.net
NextTrace所有的的IP地理位置`API DEMO`可以参考[这里](https://github.com/xgadget-lab/nexttrace/blob/main/ipgeo/)
# 联合使用
nexttrace -r 1 -q 1 -report www.time.com.my
```
### 全部用法详见Usage菜单
### IP 数据库
目前使用的 IP 数据库默认为我们自己搭建的 API 服务,如果后期遇到滥用,我们可能会选择关闭。
我们也会在后期开放服务端源代码,您也可以根据该项目的源码自行搭建属于您的 API 服务器。
NextTrace 所有的的 IP 地理位置`API DEMO`可以参考[这里](https://github.com/xgadget-lab/nexttrace/blob/main/ipgeo/)
### 全部用法详见 Usage 菜单
```shell
Usage of nexttrace:
-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")
-displayMode string
Choose The Display Mode [table, classic] (default "table")
-m int
Set the max number of hops (max TTL to be reached). (default 30)
-p int
@@ -76,28 +86,46 @@ 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
```
## FAQ 常见问题
如果你在安装或者使用的时候遇到了问题,我们建议你不要把新建一个 `issue` 作为首选项
或许可以在这里找到答案 -> [前往 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)
<!-- 等待一个更好的项目截图
## 项目截图
![](asset/screenshot2.png)
![](asset/screenshot3.png)
![](asset/screenshot.png)
## Thanks
-->
[Sam Sam](https://github.com/samleong123) (samsam123@samsam123.name.my)
<!--
Leo注描述可能不合适建议再加以斟酌已经修改
## History
- 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) 发起了这个项目,并完成了最初的工作。
-->
## Thanks
[Vincent Young](https://github.com/missuo) (i@yyt.moe)
[waiting4new](https://github.com/waiting4new)
[Sam Sam](https://github.com/samleong123) (samsam123@samsam123.name.my)
FFEE_CO
nsnnns
[waiting4new](https://github.com/waiting4new)、[FFEE_CO](https://github.com/fkx4-p)、[nsnnns](https://github.com/tsosunchia)
## IP Database Copyright
@@ -105,17 +133,25 @@ nsnnns
#### China MainLand
* 项目组自行维护 ~ 御三家骨干网数据 ~ 5%
- 项目组自行维护 ~ 御三家骨干网数据 ~ 5%
* 埃文科技 Paid Database ~ 95%
- 埃文科技 Paid Database ~ 95%
**这里有朋友就要问了,为什么不全部使用埃文的付费库?**
埃文的库一直都不是最优选择IPIP.NET 才是,但是因为他们不对私,所以我们只能选择价格更便宜的埃文库。
埃文家的数据库,在骨干网这块,准度可以说是非常糟糕,作为一款可视化的路由跟踪工具,骨干网的数据库准度非常重要。
所以我们选择了尝试自行去校准一部分骨干网数据,但是由于我们缺乏检测节点以及志愿者,所以这项工作可能会进展的尤其缓慢。
#### WorldWide
* 埃文科技 Paid Database ~ 15%
- 埃文科技 Paid Database ~ 15%
* IpInfo Free ~ 15%
- IpInfo Free ~ 15%
* IPInSight Free ~ 70%
- IPInSight Free ~ 70%
### IPv6 Database
@@ -123,4 +159,4 @@ This product includes IP2Location LITE data available from <a href="https://lite
### Others
其他第三方API尽管集成在本项目内但是具体的TOS以及AUP请详见第三方API官网。如遇到IP数据错误也请直接联系他们纠错。
其他第三方 API 尽管集成在本项目内,但是具体的 TOS 以及 AUP请详见第三方 API 官网。如遇到 IP 数据错误,也请直接联系他们纠错。

Binary file not shown.

Before

Width:  |  Height:  |  Size: 139 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 136 KiB

BIN
asset/screenshot_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

View File

@@ -15,11 +15,12 @@ func TestLeoIP(t *testing.T) {
}
func TestIPSB(t *testing.T) {
res, err := IPSB("1.1.1.1")
assert.Nil(t, err)
assert.NotNil(t, res)
assert.NotEmpty(t, res.Asnumber)
assert.NotEmpty(t, res.Isp)
// Not available
//res, err := IPSB("1.1.1.1")
//assert.Nil(t, err)
//assert.NotNil(t, res)
//assert.NotEmpty(t, res.Asnumber)
//assert.NotEmpty(t, res.Isp)
}
func TestIPInfo(t *testing.T) {

View File

@@ -1,61 +0,0 @@
package listener_channel
import (
"golang.org/x/net/context"
"net"
"time"
)
type ReceivedMessage struct {
N *int
Peer net.Addr
Msg []byte
Err error
}
type ListenerChannel struct {
ctx context.Context
cancel context.CancelFunc
Conn net.PacketConn
Messages chan ReceivedMessage
}
func New(conn net.PacketConn) *ListenerChannel {
ctx, cancel := context.WithCancel(context.Background())
results := make(chan ReceivedMessage, 50)
return &ListenerChannel{Conn: conn, ctx: ctx, cancel: cancel, Messages: results}
}
func (l *ListenerChannel) Start() {
for {
select {
case <-l.ctx.Done():
return
default:
}
reply := make([]byte, 1500)
err := l.Conn.SetReadDeadline(time.Now().Add(2 * time.Second))
if err != nil {
l.Messages <- ReceivedMessage{Err: err}
continue
}
n, peer, err := l.Conn.ReadFrom(reply)
if err != nil {
l.Messages <- ReceivedMessage{Err: err}
continue
}
l.Messages <- ReceivedMessage{
N: &n,
Peer: peer,
Err: nil,
Msg: reply,
}
}
}
func (l *ListenerChannel) Stop() {
l.cancel()
}

View File

@@ -25,9 +25,14 @@ 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")
func flagApply() string {
flag.Parse()
printer.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>")
@@ -37,11 +42,13 @@ func flagApply() string {
}
func main() {
domain := flagApply()
if os.Getuid() != 0 {
log.Fatalln("Traceroute requires root/sudo privileges.")
}
domain := flagApply()
ip := util.DomainLookUp(domain)
printer.PrintTraceRouteNav(ip, domain, *dataOrigin)

View File

@@ -1,5 +1,11 @@
#!/bin/bash
auto=False
#是否忽略一切警告,按默认执行
if [[ $1 == "--auto" ]]; then
auto=True
fi
usrPath="/usr/local/bin"
checkRootPermit() {
@@ -49,14 +55,15 @@ getLocation() {
}
installWgetPackage() {
# macOS should install wget originally. Nothing to do
echo "wget 正在安装中..."
# try apt
# 是时候直接使用 APT 来管理包了
apt-get -h &>/dev/null
if [ $? -eq 0 ]; then
# 先更新一下数据源有些机器数据源比较老可能会404
apt-get update -y &>/dev/null
apt-get install wget -y &>/dev/null
apt-get --no-install-recommends install wget -y &>/dev/null
return 0
fi
# try yum
@@ -64,6 +71,7 @@ installWgetPackage() {
if [ $? -eq 0 ]; then
yum -y update &>/dev/null
yum install wget -y &>/dev/null
return 0
fi
# try dnf
@@ -71,6 +79,7 @@ installWgetPackage() {
if [ $? -eq 0 ]; then
dnf check-update &>/dev/null
dnf install wget -y &>/dev/null
return 0
fi
# try pacman
@@ -78,9 +87,27 @@ installWgetPackage() {
if [ $? -eq 0 ]; then
pacman -Sy &>/dev/null
pacman -S wget &>/dev/null
return 0
fi
wget -h &>/dev/null
# try zypper
zypper -h &>/dev/null
if [ $? -eq 0 ]; then
zypper refresh &>/dev/null
zypper install -y --no-recommends wget &>/dev/null
return 0
fi
# try brew
brew -v &>/dev/null
if [ $? -eq 0 ]; then
brew update &>/dev/null
brew install wget &>/dev/null
return 0
fi
# 有的发行版自带的wget只有 --help 参数
wget --help &>/dev/null
if [ $? -ne 0 ]; then
echo "wget 安装失败"
exit 1
@@ -88,14 +115,14 @@ installWgetPackage() {
}
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
apt-get --no-install-recommends install jq -y &>/dev/null
return 0
fi
# try yum
@@ -103,6 +130,7 @@ installJqPackage() {
if [ $? -eq 0 ]; then
yum -y update &>/dev/null
yum install jq -y &>/dev/null
return 0
fi
# try dnf
@@ -110,6 +138,15 @@ installJqPackage() {
if [ $? -eq 0 ]; then
dnf check-update &>/dev/null
dnf install jq -y &>/dev/null
return 0
fi
# try zypper
zypper -h &>/dev/null
if [ $? -eq 0 ]; then
zypper refresh &>/dev/null
zypper install -y --no-recommends jq &>/dev/null
return 0
fi
# try pacman
@@ -117,6 +154,15 @@ installJqPackage() {
if [ $? -eq 0 ]; then
pacman -Sy &>/dev/null
pacman -S jq &>/dev/null
return 0
fi
# try brew
brew -v &>/dev/null
if [ $? -eq 0 ]; then
brew update &>/dev/null
brew install jq &>/dev/null
return 0
fi
jq -h &>/dev/null
@@ -129,6 +175,10 @@ installJqPackage() {
checkWgetPackage() {
wget -h &>/dev/null
if [ $? -ne 0 ]; then
if [[ $auto == True ]]; then
installWgetPackage
return 0
fi
read -r -p "您还没有安装wget是否安装? (y/n)" input
case $input in
@@ -148,34 +198,14 @@ checkWgetPackage() {
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
if [[ $auto == True ]]; then
installJqPackage
return 0
fi
read -r -p "您还没有安装jq是否安装? (y/n)" input
case $input in
[yY][eE][sS] | [yY])
@@ -183,8 +213,8 @@ checkJqPackage() {
;;
[nN][oO] | [nN])
echo "您选择了取消安装"
return 0
echo "您选择了取消安装,脚本即将退出"
exit 1
;;
*)
@@ -195,6 +225,39 @@ checkJqPackage() {
return 1
}
checkVersion() {
checkJqPackage
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
currentVersion=$(nexttrace -V | head -n 1 | awk '{print $2}')
if [[ $currentVersion == $version ]]; then
echo "当前版本已是最新版本"
exit 0
fi
echo 当前最新release版本${version}
echo 您当前的版本:${currentVersion}
if [[ $auto == True ]]; then
return 0
fi
read -r -p "是否更新软件? (y/n)" input
case $input in
[yY][eE][sS] | [yY])
return 0
;;
[nN][oO] | [nN])
echo "您选择了取消安装/更新,脚本即将退出"
exit 1
;;
*)
return 0
;;
esac
}
downloadBinrayFile() {
echo "正在获取最新版的 NextTrace 发行版文件信息..."
checkJqPackage
@@ -209,20 +272,24 @@ downloadBinrayFile() {
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
if [ "$countryCode" == "CN" ]; then
read -r -p "检测到国内网络环境,是否使用镜像下载以加速(y/n)" input
case $input in
[yY][eE][sS] | [yY])
if [[ $auto == True ]]; then
latestURL="https://ghproxy.com/"$latestURL
;;
else
read -r -p "检测到国内网络环境,是否使用镜像下载以加速(y/n)" input
case $input in
[yY][eE][sS] | [yY])
latestURL="https://ghproxy.com/"$latestURL
;;
[nN][oO] | [nN])
echo "您选择了不使用镜像,下载可能会变得异常缓慢,或者失败"
;;
[nN][oO] | [nN])
echo "您选择了不使用镜像,下载可能会变得异常缓慢,或者失败"
;;
*)
latestURL="https://ghproxy.com/"$latestURL
;;
esac
*)
latestURL="https://ghproxy.com/"$latestURL
;;
esac
fi
fi
echo "正在下载 NextTrace 二进制文件..."
@@ -231,7 +298,7 @@ downloadBinrayFile() {
echo "NextTrace 现在已经在您的系统中可用"
changeMode
mv ${downPath} ${usrPath}
if [[ ${osDistribution} == "macOS" ]]; then
if [[ ${osDistribution} == "darwin" ]]; then
xattr -r -d com.apple.quarantine ${usrPath}/nexttrace
fi
else
@@ -250,13 +317,43 @@ runBinrayFileHelp() {
fi
}
addCronTask() {
if [[ $auto == True ]]; then
return 0
fi
read -r -p "是否添加自动更新任务?(y/n)" input
case $input in
[yY][eE][sS] | [yY])
if [[ ${osDistribution} == "darwin" ]]; then
crontab -l >crontab.bak
sed -i '' '/nt_install.sh/d' crontab.bak
elif [[ ${osDistribution} == "linux" ]]; then
crontab -l >crontab.bak
sed -i '/nt_install.sh/d' crontab.bak
else
echo "暂不支持您的系统,无法自动添加crontab任务"
return 0
fi
echo "1 1 * * * $(dirname $(readlink -f "$0"))/nt_install.sh --auto >> /var/log/nt_install.log" >>crontab.bak
crontab crontab.bak
rm -f crontab.bak
;;
[nN][oO] | [nN])
echo "您选择了不添加自动更新任务,您也可以通过命令 再次执行此脚本 手动更新"
;;
*)
echo "您选择了不添加自动更新任务,您可以通过命令 再次执行此脚本 手动更新"
;;
esac
}
# Check Procedure
checkRootPermit
checkSystemDistribution
checkSystemArch
checkWgetPackage
# TODO: 检查版本并更新
#checkVersion
checkJqPackage
checkVersion
# Download Procedure
getLocation
@@ -264,3 +361,4 @@ downloadBinrayFile
# Run Procedure
runBinrayFileHelp
addCronTask

View File

@@ -5,6 +5,15 @@ import (
"net"
)
var version = "v0.0.0.alpha"
var buildDate = ""
var commitID = ""
func Version() {
fmt.Println("NextTrace", version, buildDate, commitID)
fmt.Println("XGadget-lab Leo (leo.moe) & Vincent (vincent.moe) & zhshch (xzhsh.ch)")
}
func PrintTraceRouteNav(ip net.IP, domain string, dataOrigin string) {
fmt.Println("IP Geo Data Provider: " + dataOrigin)

104
printer/printer_test.go Normal file
View File

@@ -0,0 +1,104 @@
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"
)
func TestPrintTraceRouteNav(t *testing.T) {
PrintTraceRouteNav(util.DomainLookUp("1.1.1.1"), "1.1.1.1", "dataOrigin")
}
var testGeo = &ipgeo.IPGeoData{
Asnumber: "TestAsnumber",
Country: "TestCountry",
Prov: "TestProv",
City: "TestCity",
District: "TestDistrict",
Owner: "TestOwner",
Isp: "TestIsp",
}
var testResult = &trace.Result{
Hops: [][]trace.Hop{
{
{
Success: true,
Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")},
Hostname: "test",
TTL: 0,
RTT: 10 * time.Millisecond,
Error: nil,
Geo: testGeo,
},
{
Success: true,
Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")},
Hostname: "test",
TTL: 0,
RTT: 10 * time.Millisecond,
Error: nil,
Geo: testGeo,
},
},
{
{
Success: false,
Address: nil,
Hostname: "",
TTL: 0,
RTT: 0,
Error: errors.New("test error"),
Geo: nil,
},
{
Success: true,
Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")},
Hostname: "test",
TTL: 0,
RTT: 10 * time.Millisecond,
Error: nil,
Geo: nil,
},
},
{
{
Success: true,
Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")},
Hostname: "test",
TTL: 0,
RTT: 0,
Error: nil,
Geo: &ipgeo.IPGeoData{},
},
{
Success: true,
Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")},
Hostname: "",
TTL: 0,
RTT: 10 * time.Millisecond,
Error: nil,
Geo: testGeo,
},
},
},
}
func TestTraceroutePrinter(t *testing.T) {
TraceroutePrinter(testResult)
}
func TestTracerouteTablePrinter(t *testing.T) {
TracerouteTablePrinter(testResult)
}
func TestRealtimePrinter(t *testing.T) {
RealtimePrinter(testResult, 0)
RealtimePrinter(testResult, 1)
RealtimePrinter(testResult, 2)
}

View File

@@ -2,6 +2,7 @@ package printer
import (
"fmt"
"github.com/xgadget-lab/nexttrace/ipgeo"
"strings"
"github.com/xgadget-lab/nexttrace/trace"
@@ -89,6 +90,10 @@ func tableDataGenerator(h trace.Hop) *rowData {
IP = fmt.Sprint(h.Hostname, " (", IP, ") ")
}
if h.Geo == nil {
h.Geo = &ipgeo.IPGeoData{}
}
r := &rowData{
Hop: fmt.Sprint(h.TTL),
IP: IP,

View File

@@ -1,10 +1,10 @@
package reporter
import (
"errors"
"fmt"
"net"
"strings"
"sync"
"github.com/xgadget-lab/nexttrace/ipgeo"
"github.com/xgadget-lab/nexttrace/trace"
@@ -24,9 +24,12 @@ func New(rs *trace.Result, ip string) Reporter {
}
type reporter struct {
targetIP string
routeReport map[uint16][]routeReportNode
routeResult *trace.Result
targetTTL uint16
targetIP string
routeReport map[uint16][]routeReportNode
routeReportLock sync.Mutex
routeResult *trace.Result
wg sync.WaitGroup
}
type routeReportNode struct {
@@ -40,71 +43,107 @@ func experimentTag() {
fmt.Println("Route-Path 功能实验室")
}
func (r *reporter) generateRouteReportNode(ip string, ipGeoData ipgeo.IPGeoData) (routeReportNode, error) {
rpn := routeReportNode{}
go func() {
ptr, err := net.LookupAddr(ip)
if err == nil {
if strings.Contains(strings.ToLower(ptr[0]), "ix") {
rpn.ix = true
} else {
rpn.ix = false
}
}
}()
func (r *reporter) generateRouteReportNode(ip string, ipGeoData ipgeo.IPGeoData, ttl uint16) {
var success bool = true
defer r.wg.Done()
rpn := routeReportNode{}
ptr, err := net.LookupAddr(ip)
if err == nil {
if strings.Contains(strings.ToLower(ptr[0]), "ix") {
rpn.ix = true
} else {
rpn.ix = false
}
}
// TODO: 这种写法不好,后面再重构一下
// 判断反向解析的域名中又或者是IP地理位置数据库中是否出现了 IX
if strings.Contains(strings.ToLower(ipGeoData.Isp), "exchange") || strings.Contains(strings.ToLower(ipGeoData.Isp), "ix") || strings.Contains(strings.ToLower(ipGeoData.Owner), "exchange") || strings.Contains(strings.ToLower(ipGeoData.Owner), "ix") {
rpn.ix = true
}
// TODO: 正则判断POP并且提取带宽大小等信息
// CN2 需要特殊处理因为他们很多没有ASN
// 但是目前这种写法是不规范的属于凭空标记4809的IP
// TODO: 用更好的方式显示 CN2 骨干网的路由 Path
if strings.HasPrefix(ip, "59.43") {
rpn.asn = "4809"
} else {
rpn.asn = ipGeoData.Asnumber
}
// 无论最后一跳是否为存在地理位置信息AnyCast都应该给予显示
if ipGeoData.Country == "" || ipGeoData.City == "" || ipGeoData.City == "-" && ip != r.targetIP {
return rpn, errors.New("GeoData Search Failed")
if (ipGeoData.Country == "" || ipGeoData.Country == "LAN Address" || ipGeoData.Country == "-") && ip != r.targetIP {
success = false
} else {
if ipGeoData.City == "" {
rpn.geo = []string{ipGeoData.Country, ipGeoData.Country}
rpn.geo = []string{ipGeoData.Country, ipGeoData.Prov}
} else {
rpn.geo = []string{ipGeoData.Country, ipGeoData.City}
}
}
if ipGeoData.Asnumber == "" {
rpn.asn = "*"
}
if ipGeoData.Isp == "" {
rpn.isp = ipGeoData.Owner
} else {
rpn.isp = ipGeoData.Isp
}
return rpn, nil
// 有效记录
if success {
// 锁住资源防止同时写panic
r.routeReportLock.Lock()
// 添加到MAP中
r.routeReport[ttl] = append(r.routeReport[ttl], rpn)
// 写入完成,解锁释放资源给其他协程
r.routeReportLock.Unlock()
}
}
func (r *reporter) InitialBaseData() Reporter {
var nodeIndex uint16 = 1
reportNodes := map[uint16][]routeReportNode{}
for i := uint16(0); int(i) < len(r.routeResult.Hops); i++ {
r.routeReport = reportNodes
r.targetTTL = uint16(len(r.routeResult.Hops))
for i := uint16(0); i < r.targetTTL; i++ {
traceHop := r.routeResult.Hops[i][0]
if traceHop.Success {
currentIP := traceHop.Address.String()
rpn, err := r.generateRouteReportNode(currentIP, *traceHop.Geo)
if err == nil {
reportNodes[nodeIndex] = append(reportNodes[nodeIndex], rpn)
nodeIndex += 1
}
r.wg.Add(1)
go r.generateRouteReportNode(currentIP, *traceHop.Geo, i)
}
}
r.routeReport = reportNodes
// 等待所有的子协程运行完毕
r.wg.Wait()
return r
}
func (r *reporter) Print() {
var beforeActiveTTL uint16 = 1
r.InitialBaseData()
for i := uint16(1); int(i) < len(r.routeReport)+1; i++ {
for i := uint16(1); i < r.targetTTL; i++ {
// 计算该TTL内的数据长度如果为0则代表没有有效数据
if len(r.routeReport[i]) == 0 {
// 跳过改跃点的数据整理
continue
}
nodeReport := r.routeReport[i][0]
if i == 1 {
fmt.Printf("AS%s %s「%s『%s", nodeReport.asn, nodeReport.isp, nodeReport.geo[0], nodeReport.geo[1])
} else {
nodeReportBefore := r.routeReport[i-1][0]
nodeReportBefore := r.routeReport[beforeActiveTTL][0]
// ASN 相同,同个 ISP 内部的数据传递
if nodeReportBefore.asn == nodeReport.asn {
// Same ASN but Coutry or City Changed
if nodeReportBefore.geo[0] != nodeReport.geo[0] {
@@ -115,8 +154,11 @@ func (r *reporter) Print() {
}
}
} else {
// ASN 不同,跨 ISP 的数据传递,这里可能会出现 POP、IP Transit、Peer、Exchange
fmt.Printf("』」")
if int(i) != len(r.routeReport)+1 {
// 部分 Shell 客户端可能无法很好的展示这个特殊字符
// TODO: 寻找其他替代字符
fmt.Printf("\n ╭╯\n ╰")
}
if nodeReport.ix {
@@ -126,6 +168,8 @@ func (r *reporter) Print() {
}
}
}
// 标记为最新的一个有效跃点
beforeActiveTTL = i
}
fmt.Println("』」")
}

View File

@@ -1,31 +1,115 @@
package reporter
import (
"net"
"testing"
"time"
"github.com/xgadget-lab/nexttrace/ipgeo"
"github.com/xgadget-lab/nexttrace/trace"
"github.com/xgadget-lab/nexttrace/util"
)
var testResult = &trace.Result{
Hops: [][]trace.Hop{
{
{
Success: true,
Address: &net.IPAddr{IP: net.ParseIP("192.168.3.1")},
Hostname: "test",
TTL: 0,
RTT: 10 * time.Millisecond,
Error: nil,
Geo: &ipgeo.IPGeoData{
Asnumber: "4808",
Country: "中国",
Prov: "北京市",
City: "北京市",
District: "北京市",
Owner: "",
Isp: "中国联通",
},
},
},
{
{
Success: true,
Address: &net.IPAddr{IP: net.ParseIP("114.249.16.1")},
Hostname: "test",
TTL: 0,
RTT: 10 * time.Millisecond,
Error: nil,
Geo: &ipgeo.IPGeoData{
Asnumber: "4808",
Country: "中国",
Prov: "北京市",
City: "北京市",
District: "北京市",
Owner: "",
Isp: "中国联通",
},
},
},
{
{
Success: true,
Address: &net.IPAddr{IP: net.ParseIP("219.158.5.150")},
Hostname: "test",
TTL: 0,
RTT: 10 * time.Millisecond,
Error: nil,
Geo: &ipgeo.IPGeoData{
Asnumber: "4837",
Country: "中国",
Prov: "",
City: "",
District: "",
Owner: "",
Isp: "中国联通",
},
},
},
{
{
Success: true,
Address: &net.IPAddr{IP: net.ParseIP("62.115.125.160")},
Hostname: "test",
TTL: 0,
RTT: 10 * time.Millisecond,
Error: nil,
Geo: &ipgeo.IPGeoData{
Asnumber: "1299",
Country: "Sweden",
Prov: "Stockholm County",
City: "Stockholm",
District: "",
Owner: "",
Isp: "Telia Company AB",
},
},
},
{
{
Success: true,
Address: &net.IPAddr{IP: net.ParseIP("213.226.68.73")},
Hostname: "test",
TTL: 0,
RTT: 10 * time.Millisecond,
Error: nil,
Geo: &ipgeo.IPGeoData{
Asnumber: "56630",
Country: "Germany",
Prov: "Hesse, Frankfurt",
City: "",
District: "",
Owner: "",
Isp: "Melbikomas UAB",
},
},
},
},
}
func TestPrint(t *testing.T) {
ip := util.DomainLookUp("213.226.68.73")
var m trace.Method = "tcp"
var conf = trace.Config{
DestIP: ip,
DestPort: 80,
MaxHops: 30,
NumMeasurements: 1,
ParallelRequests: 1,
RDns: true,
IPGeoSource: ipgeo.GetSource("LeoMoeAPI"),
Timeout: 2 * time.Second,
//Quic: false,
}
res, _ := trace.Traceroute(m, conf)
r := New(res, ip.String())
r := New(testResult, "213.226.68.73")
r.Print()
}

View File

@@ -1,19 +0,0 @@
package signal
type Signal struct {
sigChan chan struct{}
}
func New() *Signal {
return &Signal{sigChan: make(chan struct{}, 1)}
}
func (s *Signal) Signal() {
if len(s.sigChan) == 0 {
s.sigChan <- struct{}{}
}
}
func (s *Signal) Chan() chan struct{} {
return s.sigChan
}

View File

@@ -1,45 +0,0 @@
package taskgroup
import (
"sync"
)
type TaskGroup struct {
count int
mu sync.Mutex
done []chan struct{}
}
func New() *TaskGroup {
return &TaskGroup{
count: 0,
mu: sync.Mutex{},
done: []chan struct{}{},
}
}
func (t *TaskGroup) Add() {
t.mu.Lock()
defer t.mu.Unlock()
t.count++
}
func (t *TaskGroup) Done() {
t.mu.Lock()
defer t.mu.Unlock()
if t.count-1 == 0 {
for _, doneChannel := range t.done {
doneChannel <- struct{}{}
}
t.done = []chan struct{}{}
}
t.count--
}
func (t *TaskGroup) Wait() {
doneChannel := make(chan struct{})
t.mu.Lock()
t.done = append(t.done, doneChannel)
t.mu.Unlock()
<-doneChannel
}

View File

@@ -10,7 +10,6 @@ import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/xgadget-lab/nexttrace/listener_channel"
"github.com/xgadget-lab/nexttrace/util"
"golang.org/x/net/context"
"golang.org/x/net/icmp"
@@ -110,10 +109,7 @@ func (t *TCPTracer) listenICMP() {
// @title listenTCP
// @description 监听TCP的响应数据包
func (t *TCPTracer) listenTCP() {
lc := listener_channel.New(t.tcp)
defer lc.Stop()
lc := NewPacketListener(t.tcp, t.ctx)
go lc.Start()
for {

View File

@@ -10,7 +10,6 @@ import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/xgadget-lab/nexttrace/listener_channel"
"github.com/xgadget-lab/nexttrace/util"
"golang.org/x/net/context"
"golang.org/x/net/icmp"
@@ -111,10 +110,7 @@ func (t *TCPTracerv6) listenICMP() {
// @title listenTCP
// @description 监听TCP的响应数据包
func (t *TCPTracerv6) listenTCP() {
lc := listener_channel.New(t.tcp)
defer lc.Stop()
lc := NewPacketListener(t.tcp, t.ctx)
go lc.Start()
for {