Skip to content

Commit

Permalink
add: 指定网卡进行路由跟踪
Browse files Browse the repository at this point in the history
  • Loading branch information
sjlleo committed Sep 2, 2022
1 parent 49ce0cb commit 1261e24
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var tablePrint = fSet.Bool("table", false, "Output trace results as table")
var classicPrint = fSet.Bool("classic", false, "Classic Output trace results like BestTrace")
var beginHop = fSet.Int("b", 1, "Set The Begin TTL")
var ver = fSet.Bool("V", false, "Print Version")
var src_addr = fSet.String("S", "", "Use the following IP address as the source address in outgoing packets")
var src_dev = fSet.String("D", "", "Use the following Network Devices as the source address in outgoing packets")

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")
Expand Down Expand Up @@ -92,6 +94,18 @@ func main() {
ip = util.DomainLookUp(domain, false)
}

if *src_dev != "" {
dev, _ := net.InterfaceByName(*src_dev)

if addrs, err := dev.Addrs(); err == nil {
for _, addr := range addrs {
if (addr.(*net.IPNet).IP.To4() == nil) == (ip.To4() == nil) {
*src_addr = addr.(*net.IPNet).IP.String()
}
}
}
}

if strings.ToUpper(*dataOrigin) == "LEOMOEAPI" {
w := wshandle.New()
w.Interrupt = make(chan os.Signal, 1)
Expand Down Expand Up @@ -119,6 +133,7 @@ func main() {
}

var conf = trace.Config{
SrcAddr: *src_addr,
BeginHop: *beginHop,
DestIP: ip,
DestPort: *port,
Expand Down
2 changes: 1 addition & 1 deletion trace/icmp_ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (t *ICMPTracer) Execute() (*Result, error) {

var err error

t.icmpListen, err = net.ListenPacket("ip4:1", "0.0.0.0")
t.icmpListen, err = net.ListenPacket("ip4:1", t.SrcAddr)
if err != nil {
return &t.res, err
}
Expand Down
2 changes: 1 addition & 1 deletion trace/icmp_ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (t *ICMPTracerv6) Execute() (*Result, error) {

var err error

t.icmpListen, err = net.ListenPacket("ip6:58", "::")
t.icmpListen, err = net.ListenPacket("ip6:58", t.SrcAddr)
if err != nil {
return &t.res, err
}
Expand Down
9 changes: 7 additions & 2 deletions trace/tcp_ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ func (t *TCPTracer) Execute() (*Result, error) {
t.SrcIP, _ = util.LocalIPPort(t.DestIP)

var err error
t.tcp, err = net.ListenPacket("ip4:tcp", t.SrcIP.String())
if t.SrcAddr != "" {
t.tcp, err = net.ListenPacket("ip4:tcp", t.SrcAddr)
} else {
t.tcp, err = net.ListenPacket("ip4:tcp", t.SrcIP.String())
}

if err != nil {
return nil, err
}
t.icmp, err = icmp.ListenPacket("ip4:icmp", "0.0.0.0")
t.icmp, err = icmp.ListenPacket("ip4:icmp", t.SrcAddr)
if err != nil {
return &t.res, err
}
Expand Down
1 change: 1 addition & 0 deletions trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
)

type Config struct {
SrcAddr string
BeginHop int
MaxHops int
NumMeasurements int
Expand Down
2 changes: 1 addition & 1 deletion trace/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (t *UDPTracer) Execute() (*Result, error) {
}

var err error
t.icmp, err = icmp.ListenPacket("ip4:icmp", "0.0.0.0")
t.icmp, err = icmp.ListenPacket("ip4:icmp", t.SrcAddr)
if err != nil {
return &t.res, err
}
Expand Down

0 comments on commit 1261e24

Please sign in to comment.