/
server.go
437 lines (380 loc) · 11.8 KB
/
server.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package speedtest
import (
"context"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"github.com/showwin/speedtest-go/speedtest/transport"
"math"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"sync"
"time"
)
const (
speedTestServersUrl = "https://www.speedtest.net/api/js/servers"
speedTestServersAlternativeUrl = "https://www.speedtest.net/speedtest-servers-static.php"
speedTestServersAdvanced = "https://www.speedtest.net/api/ios-config.php"
)
type payloadType int
const (
typeJSONPayload payloadType = iota
typeXMLPayload
)
var (
ErrServerNotFound = errors.New("no server available or found")
)
// Server information
type Server struct {
URL string `xml:"url,attr" json:"url"`
Lat string `xml:"lat,attr" json:"lat"`
Lon string `xml:"lon,attr" json:"lon"`
Name string `xml:"name,attr" json:"name"`
Country string `xml:"country,attr" json:"country"`
Sponsor string `xml:"sponsor,attr" json:"sponsor"`
ID string `xml:"id,attr" json:"id"`
Host string `xml:"host,attr" json:"host"`
Distance float64 `json:"distance"`
Latency time.Duration `json:"latency"`
MaxLatency time.Duration `json:"max_latency"`
MinLatency time.Duration `json:"min_latency"`
Jitter time.Duration `json:"jitter"`
DLSpeed ByteRate `json:"dl_speed"`
ULSpeed ByteRate `json:"ul_speed"`
TestDuration TestDuration `json:"test_duration"`
PacketLoss transport.PLoss `json:"packet_loss"`
Context *Speedtest `json:"-"`
}
type TestDuration struct {
Ping *time.Duration `json:"ping"`
Download *time.Duration `json:"download"`
Upload *time.Duration `json:"upload"`
Total *time.Duration `json:"total"`
}
// CustomServer use defaultClient, given a URL string, return a new Server object, with as much
// filled in as we can
func CustomServer(host string) (*Server, error) {
return defaultClient.CustomServer(host)
}
// CustomServer given a URL string, return a new Server object, with as much
// filled in as we can
func (s *Speedtest) CustomServer(host string) (*Server, error) {
u, err := url.Parse(host)
if err != nil {
return nil, err
}
parseHost := fmt.Sprintf("%s://%s%s", u.Scheme, u.Host, "/speedtest/upload.php")
return &Server{
ID: "Custom",
Lat: "?",
Lon: "?",
Country: "?",
URL: parseHost,
Name: u.Host,
Host: u.Host,
Sponsor: "?",
Context: s,
}, nil
}
// ServerList list of Server
// Users(Client) also exists with @param speedTestServersAdvanced
type ServerList struct {
Servers []*Server `xml:"servers>server"`
Users []User `xml:"client"`
}
// Servers for sorting servers.
type Servers []*Server
// ByDistance for sorting servers.
type ByDistance struct {
Servers
}
func (servers Servers) Available() *Servers {
retServer := Servers{}
for _, server := range servers {
if server.Latency != PingTimeout {
retServer = append(retServer, server)
}
}
for i := 0; i < len(retServer)-1; i++ {
for j := 0; j < len(retServer)-i-1; j++ {
if retServer[j].Latency > retServer[j+1].Latency {
retServer[j], retServer[j+1] = retServer[j+1], retServer[j]
}
}
}
return &retServer
}
// Len finds length of servers. For sorting servers.
func (servers Servers) Len() int {
return len(servers)
}
// Swap swaps i-th and j-th. For sorting servers.
func (servers Servers) Swap(i, j int) {
servers[i], servers[j] = servers[j], servers[i]
}
// Hosts return hosts of servers
func (servers Servers) Hosts() []string {
var retServer []string
for _, server := range servers {
retServer = append(retServer, server.Host)
}
return retServer
}
// Less compares the distance. For sorting servers.
func (b ByDistance) Less(i, j int) bool {
return b.Servers[i].Distance < b.Servers[j].Distance
}
// FetchServerByID retrieves a server by given serverID.
func (s *Speedtest) FetchServerByID(serverID string) (*Server, error) {
return s.FetchServerByIDContext(context.Background(), serverID)
}
// FetchServerByID retrieves a server by given serverID.
func FetchServerByID(serverID string) (*Server, error) {
return defaultClient.FetchServerByID(serverID)
}
// FetchServerByIDContext retrieves a server by given serverID, observing the given context.
func (s *Speedtest) FetchServerByIDContext(ctx context.Context, serverID string) (*Server, error) {
u, err := url.Parse(speedTestServersAdvanced)
if err != nil {
return nil, err
}
query := u.Query()
query.Set(strings.ToLower("serverID"), serverID)
u.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
resp, err := s.doer.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var list ServerList
decoder := xml.NewDecoder(resp.Body)
if err = decoder.Decode(&list); err != nil {
return nil, err
}
for i := range list.Servers {
if list.Servers[i].ID == serverID {
list.Servers[i].Context = s
if len(list.Users) > 0 {
sLat, _ := strconv.ParseFloat(list.Servers[i].Lat, 64)
sLon, _ := strconv.ParseFloat(list.Servers[i].Lon, 64)
uLat, _ := strconv.ParseFloat(list.Users[0].Lat, 64)
uLon, _ := strconv.ParseFloat(list.Users[0].Lon, 64)
list.Servers[i].Distance = distance(sLat, sLon, uLat, uLon)
}
return list.Servers[i], err
}
}
return nil, ErrServerNotFound
}
// FetchServers retrieves a list of available servers
func (s *Speedtest) FetchServers() (Servers, error) {
return s.FetchServerListContext(context.Background())
}
// FetchServers retrieves a list of available servers
func FetchServers() (Servers, error) {
return defaultClient.FetchServers()
}
// FetchServerListContext retrieves a list of available servers, observing the given context.
func (s *Speedtest) FetchServerListContext(ctx context.Context) (Servers, error) {
u, err := url.Parse(speedTestServersUrl)
if err != nil {
return Servers{}, err
}
query := u.Query()
if len(s.config.Keyword) > 0 {
query.Set("search", s.config.Keyword)
}
if s.config.Location != nil {
query.Set("lat", strconv.FormatFloat(s.config.Location.Lat, 'f', -1, 64))
query.Set("lon", strconv.FormatFloat(s.config.Location.Lon, 'f', -1, 64))
}
u.RawQuery = query.Encode()
dbg.Printf("Retrieving servers: %s\n", u.String())
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return Servers{}, err
}
resp, err := s.doer.Do(req)
if err != nil {
return Servers{}, err
}
_payloadType := typeJSONPayload
if resp.ContentLength == 0 {
_ = resp.Body.Close()
req, err = http.NewRequestWithContext(ctx, http.MethodGet, speedTestServersAlternativeUrl, nil)
if err != nil {
return Servers{}, err
}
resp, err = s.doer.Do(req)
if err != nil {
return Servers{}, err
}
_payloadType = typeXMLPayload
}
defer resp.Body.Close()
var servers Servers
switch _payloadType {
case typeJSONPayload:
// Decode xml
decoder := json.NewDecoder(resp.Body)
if err = decoder.Decode(&servers); err != nil {
return servers, err
}
case typeXMLPayload:
var list ServerList
// Decode xml
decoder := xml.NewDecoder(resp.Body)
if err = decoder.Decode(&list); err != nil {
return servers, err
}
servers = list.Servers
default:
return servers, errors.New("response payload decoding not implemented")
}
dbg.Printf("Servers Num: %d\n", len(servers))
// set doer of server
for _, server := range servers {
server.Context = s
}
// ping once
var wg sync.WaitGroup
pCtx, fc := context.WithTimeout(context.Background(), time.Second*4)
dbg.Println("Echo each server...")
for _, server := range servers {
wg.Add(1)
go func(gs *Server) {
var latency []int64
if s.config.PingMode == TCP {
latency, err = gs.TCPPing(pCtx, 1, time.Millisecond, nil)
} else if s.config.PingMode == ICMP {
latency, err = gs.ICMPPing(pCtx, 4*time.Second, 1, time.Millisecond, nil)
} else {
latency, err = gs.HTTPPing(pCtx, 1, time.Millisecond, nil)
}
if err != nil || len(latency) < 1 {
gs.Latency = PingTimeout
} else {
gs.Latency = time.Duration(latency[0]) * time.Nanosecond
}
wg.Done()
}(server)
}
wg.Wait()
fc()
// Calculate distance
// If we don't call FetchUserInfo() before FetchServers(),
// we don't calculate the distance, instead we use the
// remote computing distance provided by Ookla as default.
if s.User != nil {
for _, server := range servers {
sLat, _ := strconv.ParseFloat(server.Lat, 64)
sLon, _ := strconv.ParseFloat(server.Lon, 64)
uLat, _ := strconv.ParseFloat(s.User.Lat, 64)
uLon, _ := strconv.ParseFloat(s.User.Lon, 64)
server.Distance = distance(sLat, sLon, uLat, uLon)
}
}
// Sort by distance
sort.Sort(ByDistance{servers})
if len(servers) <= 0 {
return servers, ErrServerNotFound
}
return servers, nil
}
// FetchServerListContext retrieves a list of available servers, observing the given context.
func FetchServerListContext(ctx context.Context) (Servers, error) {
return defaultClient.FetchServerListContext(ctx)
}
func distance(lat1 float64, lon1 float64, lat2 float64, lon2 float64) float64 {
radius := 6378.137
phi1 := lat1 * math.Pi / 180.0
phi2 := lat2 * math.Pi / 180.0
deltaPhiHalf := (lat1 - lat2) * math.Pi / 360.0
deltaLambdaHalf := (lon1 - lon2) * math.Pi / 360.0
sinePhiHalf2 := math.Sin(deltaPhiHalf)*math.Sin(deltaPhiHalf) + math.Cos(phi1)*math.Cos(phi2)*math.Sin(deltaLambdaHalf)*math.Sin(deltaLambdaHalf) // phi half-angle sine ^ 2
delta := 2 * math.Atan2(math.Sqrt(sinePhiHalf2), math.Sqrt(1-sinePhiHalf2)) // 2 arc sine
return radius * delta // r * delta
}
// FindServer finds server by serverID in given server list.
// If the id is not found in the given list, return the server with the lowest latency.
func (servers Servers) FindServer(serverID []int) (Servers, error) {
retServer := Servers{}
if len(servers) <= 0 {
return retServer, ErrServerNotFound
}
for _, sid := range serverID {
for _, s := range servers {
id, _ := strconv.Atoi(s.ID)
if sid == id {
retServer = append(retServer, s)
break
}
}
}
if len(retServer) == 0 {
// choose the lowest latency server
var minLatency int64 = math.MaxInt64
var minServerIndex int
for index, server := range servers {
if server.Latency <= 0 {
continue
}
if minLatency > server.Latency.Milliseconds() {
minLatency = server.Latency.Milliseconds()
minServerIndex = index
}
}
retServer = append(retServer, servers[minServerIndex])
}
return retServer, nil
}
// String representation of ServerList
func (servers ServerList) String() string {
slr := ""
for _, server := range servers.Servers {
slr += server.String()
}
return slr
}
// String representation of Servers
func (servers Servers) String() string {
slr := ""
for _, server := range servers {
slr += server.String()
}
return slr
}
// String representation of Server
func (s *Server) String() string {
if s.Sponsor == "?" {
return fmt.Sprintf("[%4s] %s", s.ID, s.Name)
}
if len(s.Country) == 0 {
return fmt.Sprintf("[%4s] %.2fkm %s by %s", s.ID, s.Distance, s.Name, s.Sponsor)
}
return fmt.Sprintf("[%4s] %.2fkm %s (%s) by %s", s.ID, s.Distance, s.Name, s.Country, s.Sponsor)
}
// CheckResultValid checks that results are logical given UL and DL speeds
func (s *Server) CheckResultValid() bool {
return !(s.DLSpeed*100 < s.ULSpeed) || !(s.DLSpeed > s.ULSpeed*100)
}
func (s *Server) testDurationTotalCount() {
total := s.getNotNullValue(s.TestDuration.Ping) +
s.getNotNullValue(s.TestDuration.Download) +
s.getNotNullValue(s.TestDuration.Upload)
s.TestDuration.Total = &total
}
func (s *Server) getNotNullValue(time *time.Duration) time.Duration {
if time == nil {
return 0
}
return *time
}