-
Notifications
You must be signed in to change notification settings - Fork 366
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
Showing
3 changed files
with
139 additions
and
20 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
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 |
---|---|---|
@@ -1,27 +1,127 @@ | ||
package reporter | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net" | ||
"strings" | ||
|
||
"github.com/xgadget-lab/nexttrace/ipgeo" | ||
"github.com/xgadget-lab/nexttrace/methods" | ||
) | ||
|
||
type Reporter interface { | ||
Print() | ||
} | ||
|
||
func New(rs map[uint16][]methods.TracerouteHop) Reporter { | ||
func New(rs map[uint16][]methods.TracerouteHop, ip string) Reporter { | ||
experimentTag() | ||
r := reporter{ | ||
routeResult: rs, | ||
targetIP: ip, | ||
} | ||
fmt.Println(r) | ||
return &r | ||
} | ||
|
||
type reporter struct { | ||
targetIP string | ||
routeReport map[uint16][]routeReportNode | ||
routeResult map[uint16][]methods.TracerouteHop | ||
} | ||
|
||
type routeReportNode struct { | ||
asn string | ||
isp string | ||
geo []string | ||
ix bool | ||
} | ||
|
||
func experimentTag() { | ||
fmt.Println("Route-Path是一个实验性功能,我们的IP库不能很好的支持我们提供骨干网的地理位置信息,所以IP位置有时候会异常") | ||
} | ||
|
||
func reduceRouteReportNode(ip string, ipGeoData ipgeo.IPGeoData) (routeReportNode, error) { | ||
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 | ||
} | ||
} | ||
|
||
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 | ||
} | ||
if strings.HasPrefix(ip, "59.43") { | ||
rpn.asn = "4809" | ||
} else { | ||
rpn.asn = ipGeoData.Asnumber | ||
} | ||
|
||
if ipGeoData.Country == "" || ipGeoData.City == "" { | ||
return rpn, errors.New("GeoData Search Failed") | ||
} else { | ||
rpn.geo = []string{ipGeoData.Country, ipGeoData.City} | ||
} | ||
if ipGeoData.Isp == "" { | ||
rpn.isp = ipGeoData.Owner | ||
} else { | ||
rpn.isp = ipGeoData.Isp | ||
} | ||
return rpn, nil | ||
} | ||
|
||
func (r *reporter) InitialBaseData() Reporter { | ||
var nodeIndex uint16 = 1 | ||
reportNodes := map[uint16][]routeReportNode{} | ||
for i := uint16(1); int(i) < len(r.routeResult)+1; i++ { | ||
traceHop := r.routeResult[i][0] | ||
if traceHop.Success { | ||
currentIP := traceHop.Address.String() | ||
ipGeoData, _ := ipgeo.LeoIP(currentIP) | ||
rpn, err := reduceRouteReportNode(currentIP, *ipGeoData) | ||
if err == nil { | ||
reportNodes[nodeIndex] = append(reportNodes[nodeIndex], rpn) | ||
nodeIndex += 1 | ||
} | ||
} | ||
} | ||
r.routeReport = reportNodes | ||
return r | ||
} | ||
|
||
func (r *reporter) Print() { | ||
fmt.Println(r) | ||
r.InitialBaseData() | ||
//fmt.Println(r.routeReport) | ||
for i := uint16(1); int(i) < len(r.routeReport)+1; i++ { | ||
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] | ||
if nodeReportBefore.asn == nodeReport.asn { | ||
// Same ASN but Coutry or City Changed | ||
if nodeReportBefore.geo[0] != nodeReport.geo[0] { | ||
fmt.Printf("』→ %s『%s", nodeReport.geo[0], nodeReport.geo[1]) | ||
} else { | ||
if nodeReportBefore.geo[1] != nodeReport.geo[1] { | ||
fmt.Printf(" → %s", nodeReport.geo[1]) | ||
} | ||
} | ||
} else { | ||
fmt.Printf("』」") | ||
if int(i) != len(r.routeReport)+1 { | ||
fmt.Printf("\n ╭╯\n ╰") | ||
} | ||
if nodeReport.ix { | ||
fmt.Printf("AS%s \033[42;37mIXP\033[0m %s「%s『%s", nodeReport.asn, nodeReport.isp, nodeReport.geo[0], nodeReport.geo[1]) | ||
} else { | ||
fmt.Printf("AS%s %s「%s『%s", nodeReport.asn, nodeReport.isp, nodeReport.geo[0], nodeReport.geo[1]) | ||
} | ||
} | ||
} | ||
} | ||
fmt.Println("』」") | ||
} |
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