Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cmd/ndt7-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@
package main

import (
"context"
"crypto/tls"
"flag"
"fmt"
"log"
"net"
"os"
"runtime/pprof"
"strings"
Expand Down Expand Up @@ -138,6 +140,7 @@ var (
flagService = flagx.URL{}
flagUpload = flag.Bool("upload", true, "perform upload measurement")
flagDownload = flag.Bool("download", true, "perform download measurement")
flagSourceIp = flag.String("source", "", "source IP for the test")
)

func init() {
Expand Down Expand Up @@ -195,6 +198,15 @@ func main() {
flagService.URL = nil
}

var parsedIp net.IP = nil
// source IP checking
if *flagSourceIp != "" {
parsedIp = net.ParseIP(*flagSourceIp)
if parsedIp == nil {
fmt.Printf("WARNING: ignoring unparsed source IP: %s\n", *flagSourceIp)
}
}

var e emitter.Emitter

// If -batch, force -format=json.
Expand All @@ -220,6 +232,13 @@ func main() {
c.Dialer.TLSClientConfig = &tls.Config{
InsecureSkipVerify: *flagNoVerify,
}
if parsedIp != nil {
c.Dialer.NetDialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
fmt.Printf("\nINFO: using source IP: %s\n", parsedIp.String())
dialer := net.Dialer{LocalAddr: &net.TCPAddr{IP: parsedIp}}
return dialer.DialContext(ctx, network, addr)
}
}

return c
},
Expand Down