Skip to content

Commit

Permalink
change api
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghanyun committed Jun 2, 2022
1 parent 4a0fe3d commit dbcd4c5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
28 changes: 28 additions & 0 deletions asn.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"net"
"net/http"
"strconv"
"strings"
)
Expand All @@ -28,6 +31,31 @@ type ASN struct {
ASName string `json:"as_name"`
}

type IpInfo struct {
Ip string `json:"ip"`
Country string `json:"country"`
Org string `json:"org"`
}

func lookupIpInfo(ip string) (*IpInfo, error) {
get, err := http.Get("http://ipinfo.io/" + ip)
if err != nil {
return nil, err
}
body, err := io.ReadAll(get.Body)
if err != nil {
return nil, err
}
//log.Println(string(body))
ipInfo := IpInfo{}
err = json.Unmarshal(body, &ipInfo)
if err != nil {
return nil, err
}
ipInfo.Org = strings.Split(ipInfo.Org, " ")[0]
return &ipInfo, nil
}

const hexDigit = "0123456789abcdef"

func reverseaddr(addr string) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ fi

tar -xf backtrace.tar.gz
rm backtrace.tar.gz
mv backtrace /usr/local/bin/
mv backtrace /usr/bin/
backtrace
40 changes: 29 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
package main

import (
"fmt"
"github.com/fatih/color"
"log"
"net"
"sync"
"time"
)

type Result struct {
i int
s string
}

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 [普通线路]"}
var m = map[string]string{"AS4134": "电信163 [普通线路]", "AS4809": "电信CN2 [优质线路]", "AS4837": "联通4837[普通线路]", "AS9929": "联通9929[优质线路]", "AS9808": "移动CMI [普通线路]", "AS58453": "移动CMI [普通线路]"}

func trace(wg *sync.WaitGroup, i int) {
defer wg.Done()
func trace(ch chan Result, i int) {
hops, err := Trace(net.ParseIP(rIp[i]))
if err != nil {
return
}
for _, h := range hops {
for _, n := range h.Nodes {
ip, err := LookupIP(n.IP.String())
ip, err := lookupIpInfo(n.IP.String())
if err != nil {
log.Println(err)
continue
}
if ip.Country == "CN" {
if as, ok := m[ip.Org]; ok {
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())
s := fmt.Sprintf("%v %-15s %-23s", rName[i], rIp[i], c(as))
ch <- Result{i, s}
return
}
}
Expand All @@ -35,11 +42,22 @@ func trace(wg *sync.WaitGroup, i int) {

func main() {

var wg = sync.WaitGroup{}
var s = [9]string{}
var c = make(chan Result)
log.Println("正在测试三网回程路由...")
for i := range rIp {
wg.Add(1)
go trace(&wg, i)
go trace(c, i)
}
loop:
for i := 0; i < 9; i++ {
select {
case o := <-c:
s[o.i] = o.s
case <-time.After(time.Second * 10):
break loop
}
}
for _, s2 := range s {
log.Println(s2)
}
wg.Wait()
}

0 comments on commit dbcd4c5

Please sign in to comment.