Skip to content

Commit 9b7219a

Browse files
committed
Print server sponsor when available
1 parent 9dc19eb commit 9b7219a

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

defs/bytes_counter.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ func (c *BytesCounter) Read(p []byte) (int, error) {
4545
c.total += n
4646
c.pos += n
4747
if c.pos == uploadSize {
48-
c.pos = 0
4948
c.resetReader()
5049
}
5150
c.lock.Unlock()
@@ -63,6 +62,7 @@ func (c *BytesCounter) AvgBytes() float64 {
6362
return float64(c.total) / time.Now().Sub(c.start).Seconds()
6463
}
6564

65+
// AvgMbps returns the average mbits/second
6666
func (c *BytesCounter) AvgMbps() float64 {
6767
var base float64 = 125000
6868
if c.mebi {
@@ -71,6 +71,7 @@ func (c *BytesCounter) AvgMbps() float64 {
7171
return c.AvgBytes() / base
7272
}
7373

74+
// AvgHumanize returns the average bytes/kilobytes/megabytes/gigabytes (or bytes/kibibytes/mebibytes/gibibytes) per second
7475
func (c *BytesCounter) AvgHumanize() string {
7576
val := c.AvgBytes()
7677

@@ -99,6 +100,7 @@ func (c *BytesCounter) GenerateBlob() {
99100

100101
// resetReader resets the `reader` field to 0 position
101102
func (c *BytesCounter) resetReader() (int64, error) {
103+
c.pos = 0
102104
return c.reader.Seek(0, 0)
103105
}
104106

defs/server.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ import (
2222

2323
// Server represents a speed test server
2424
type Server struct {
25-
Name string `json:"name"`
26-
Server string `json:"server"`
27-
DownloadURL string `json:"dlURL"`
28-
UploadURL string `json:"ulURL"`
29-
PingURL string `json:"pingURL"`
30-
GetIPURL string `json:"getIpURL"`
31-
ICMPFail bool `json:"-"`
32-
TLog TelemetryLog `json:"-"`
25+
Name string `json:"name"`
26+
Server string `json:"server"`
27+
DownloadURL string `json:"dlURL"`
28+
UploadURL string `json:"ulURL"`
29+
PingURL string `json:"pingURL"`
30+
GetIPURL string `json:"getIpURL"`
31+
SponsorName string `json:"sponsorName"`
32+
SponsorURL string `json:"sponsorURL"`
33+
34+
ICMPFail bool `json:"-"`
35+
TLog TelemetryLog `json:"-"`
3336
}
3437

3538
// IsUp checks the speed test backend is up by accessing the ping URL

speedtest/helper.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"math"
99
"mime/multipart"
1010
"net/http"
11+
"net/url"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -45,6 +46,26 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel
4546

4647
log.Infof("Selected server: %s [%s]", currentServer.Name, u.Hostname())
4748

49+
var sponsorMsg string
50+
if currentServer.SponsorName != "" {
51+
sponsorMsg += currentServer.SponsorName
52+
53+
if currentServer.SponsorURL != "" {
54+
su, err := url.Parse(currentServer.SponsorURL)
55+
if err != nil {
56+
log.Debugf("Sponsor URL is invalid: %s", currentServer.SponsorURL)
57+
} else {
58+
if su.Scheme == "" {
59+
su.Scheme = "https"
60+
}
61+
sponsorMsg += " @ " + su.String()
62+
}
63+
}
64+
}
65+
if sponsorMsg != "" {
66+
log.Infof("Sponsored by: %s", sponsorMsg)
67+
}
68+
4869
if currentServer.IsUp() {
4970
ispInfo, err := currentServer.GetIPInfo(c.String(defs.OptionDistance))
5071
if err != nil {

0 commit comments

Comments
 (0)