-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
102 lines (88 loc) · 2.31 KB
/
client.go
File metadata and controls
102 lines (88 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"net"
"time"
)
const pingCount = 10
const protoFmt = "ntwrk%s :%s\r\n"
const timeout = time.Duration(15) * time.Second
// testContext holds a test function, action name, and address to connect to.
type testContext struct {
Action string
Fn func(net.Conn, time.Duration) (int64, error)
Addr string
}
// startClient starts the network test suite.
func startClient(host string, port int) {
addr := fmt.Sprintf("%s:%d", host, port)
ping(addr)
perform(testContext{"download", download, addr})
perform(testContext{"upload", upload, addr})
}
// perform runs a network test and prints the recorded bandwidth.
func perform(ctx testContext) {
conn, err := openConn(ctx.Addr, ctx.Action)
if err != nil {
fmt.Printf("error: %s\n", err.Error())
return
}
since := time.Now()
ticker := time.NewTicker(time.Millisecond * 150)
go func() {
for t := range ticker.C {
elapsed := t.Sub(since)
progress := formatProgress(elapsed, timeout)
fmt.Printf("\r %9s: %s", ctx.Action, progress)
}
}()
defer ticker.Stop()
bytes, err := ctx.Fn(conn, timeout)
if err != nil {
fmt.Printf("error: %s\n", err.Error())
return
}
elapsed := time.Since(since).Seconds()
fmt.Printf(" %s\n", formatBytes(bytes, elapsed))
}
// ping performs a network latency test.
func ping(addr string) {
conn, err := openConn(addr, "echo")
if err != nil {
fmt.Printf("error: %s\n", err.Error())
return
}
resp := make([]byte, 6)
since := time.Now()
for i := 0; i < pingCount; i++ {
conn.Write([]byte("echo\r\n"))
conn.Read(resp)
if string(resp) != "echo\r\n" {
fmt.Println("error: invalid echo reply")
return
}
}
elapsed := time.Since(since) / pingCount
fmt.Printf("\r %9s: %s\n", "latency", elapsed)
}
// whoami requests the client's external IP address from `host` and prints it.
func whoami(host string, port int) {
addr := fmt.Sprintf("%s:%d", host, port)
resp := make([]byte, 40)
conn, err := openConn(addr, "whoami")
if err != nil {
fmt.Printf("error: %s\n", err.Error())
return
}
conn.Read(resp)
fmt.Print(string(resp))
}
// openConn opens a connection to `host` and writes a formatted message to it.
func openConn(host string, action string) (conn net.Conn, err error) {
conn, err = net.Dial("tcp", host)
if err != nil {
return
}
fmt.Fprintf(conn, protoFmt, proto, action)
return
}