Skip to content

Commit

Permalink
Add control of source interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien B authored and Sébastien B committed Dec 4, 2019
1 parent c5a12b9 commit 1990cb2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
70 changes: 57 additions & 13 deletions speedtest.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"crypto/tls"
"log"
"os"
"net"
"net/http"
"crypto/tls"
"os"

"gopkg.in/alecthomas/kingpin.v2"
)
Expand All @@ -24,29 +25,72 @@ func setTimeout() {
}
}

func setCertVerify(insecure bool) {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{
InsecureSkipVerify: insecure,
func newTransport(insecure bool, iface string) (tr http.Transport, ip net.IP) {
tlsConf := &tls.Config{InsecureSkipVerify: insecure}

if iface != "" {
ief, err := net.InterfaceByName(iface)
if err != nil {
log.Fatal(err)
}
addrs, err := ief.Addrs()
if err != nil {
log.Fatal(err)
}

tcpAddr := &net.TCPAddr{IP: addrs[0].(*net.IPNet).IP}
d := net.Dialer{LocalAddr: tcpAddr}
tr = http.Transport{Dial: d.Dial, TLSClientConfig: tlsConf}

ip = tcpAddr.IP
} else {
tr = http.Transport{TLSClientConfig: tlsConf}
}

return tr, ip
}

func setSourceAddr(iface string) (ip net.IP) {

ief, err := net.InterfaceByName(iface)
if err != nil {
log.Fatal(err)
}
addrs, err := ief.Addrs()
if err != nil {
log.Fatal(err)
}

tcpAddr := &net.TCPAddr{IP: addrs[0].(*net.IPNet).IP}
d := net.Dialer{LocalAddr: tcpAddr}

client.Transport.(*http.Transport).Dial = d.Dial

return tcpAddr.IP
}

var (
insecure = kingpin.Flag("insecure", "Disable TLS certificate verify").Short('i').Bool()
showList = kingpin.Flag("list", "Show available speedtest.net servers").Short('l').Bool()
serverIds = kingpin.Flag("server", "Select server id to speedtest").Short('s').Ints()
timeoutOpt = kingpin.Flag("timeout", "Define timeout seconds. Default: 10 sec").Short('t').Int()
app = kingpin.New("speedtest", "Run a speedtest").Author("Newlode")
insecure = app.Flag("insecure", "Disable TLS certificate verify").Short('i').Default("true").Bool()
iface = app.Flag("iface", "Force the use of IFACE for this test").Short('I').String()
showList = app.Flag("list", "Show available speedtest.net servers").Short('l').Bool()
serverIds = app.Flag("server", "Select server id to speedtest").Short('s').Ints()
timeoutOpt = app.Flag("timeout", "Define timeout seconds. Default: 10 sec").Short('t').Int()
timeout = 10
)

func main() {
kingpin.Version("1.0.3")
kingpin.Parse()

var ip net.IP
kingpin.Version("1.0.5")
kingpin.MustParse(app.Parse(os.Args[1:]))

setTimeout()
setCertVerify(*insecure)
tr, ip := newTransport(*insecure, *iface)
client = http.Client{Transport: &tr}

user := fetchUserInfo()
user.Show()
user.Show(ip.String())

list := fetchServerList(user)
if *showList {
Expand Down
4 changes: 2 additions & 2 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func fetchUserInfo() User {
}

// Show user location
func (u *User) Show() {
func (u *User) Show(ip string) {
if u.IP != "" {
fmt.Println("Testing From IP: " + u.IP + " (" + u.Isp + ") [" + u.Lat + ", " + u.Lon + "]")
fmt.Println("Testing From IP " + ip + "/" + u.IP + " (" + u.Isp + ") [" + u.Lat + ", " + u.Lon + "]")
}
}

0 comments on commit 1990cb2

Please sign in to comment.