-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
124 lines (110 loc) · 3.42 KB
/
main.go
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// dash-client is the dash command line client.
//
// Usage:
//
// dash-client -y [-hostname <domain>] [-timeout <string>] [-scheme <scheme>]
//
// The `-y` flag indicates you have read the data policy and accept it.
//
// The `-hostname <name>` flag specifies to use the `name` hostname for
// performing the dash test. The default is to autodiscover a suitable
// server by using Measurement Lab's m-lab/locate/v2 API.
//
// The `-timeout <string>` flag specifies the time after which the
// whole test is interrupted. The `<string>` is a string suitable to
// be passed to time.ParseDuration, e.g., "15s". The default is a large
// enough value that should be suitable for common conditions.
//
// The `-scheme <scheme>` flag allows to override the default scheme
// used for the test, i.e. "http". All DASH servers support that,
// future versions of the Go server will support "https".
//
// Additionally, passing any unrecognized flag, such as `-help`, will
// cause dash-client to print a brief help message.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"time"
"github.com/apex/log"
"github.com/m-lab/go/flagx"
"github.com/m-lab/go/rtx"
"github.com/neubot/dash/client"
)
const (
clientName = "dash-client-go"
clientVersion = "0.4.3"
defaultTimeout = 55 * time.Second
)
var (
flagHostname = flag.String("hostname", "", "optional DASH server hostname")
flagTimeout = flag.Duration(
"timeout", defaultTimeout, "time after which the test is aborted")
flagScheme = flagx.Enum{
Options: []string{"https", "http"},
Value: "https",
}
flagY = flag.Bool("y", false,
"I have read and accept the privacy policy at https://github.com/neubot/dash/blob/master/PRIVACY.md")
)
func init() {
flag.Var(
&flagScheme,
"scheme",
`Protocol scheme to use: either "https" (the default) or "http"`,
)
}
func realmain(ctx context.Context, client *client.Client, timeout time.Duration, onresult func()) error {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
ch, err := client.StartDownload(ctx)
if err != nil {
return err
}
for results := range ch {
if onresult != nil {
onresult() // this is an hook that we use for testing
}
data, err := json.Marshal(results)
rtx.PanicOnError(err, "json.Marshal should not fail")
fmt.Printf("%s\n", string(data))
}
if client.Error() != nil {
return client.Error()
}
data, err := json.Marshal(client.ServerResults())
rtx.PanicOnError(err, "json.Marshal should not fail")
fmt.Printf("%s\n", string(data))
return nil
}
func init() {
log.SetLevel(log.DebugLevel) // needs to run exactly once
}
func internalmain(ctx context.Context) error {
flag.Parse()
if !*flagY {
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Please, read the privacy policy at https://github.com/neubot/dash/blob/master/PRIVACY.md.\n")
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "If you accept the privacy policy, rerun adding the `-y` flag to the command line.\n")
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
client := client.New(clientName, clientVersion)
client.Logger = log.Log
client.FQDN = *flagHostname
client.Scheme = flagScheme.Value
return realmain(ctx, client, *flagTimeout, nil)
}
func fmain(f func(context.Context) error, e func(error, string, ...interface{})) {
if err := f(context.Background()); err != nil {
e(err, "DASH experiment failed")
}
}
var defaultMain = internalmain // testability
func main() {
fmain(defaultMain, rtx.Must)
}