Skip to content

Commit

Permalink
Print server sponsor when available
Browse files Browse the repository at this point in the history
  • Loading branch information
maddie committed Mar 26, 2020
1 parent 9dc19eb commit 9b7219a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
4 changes: 3 additions & 1 deletion defs/bytes_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (c *BytesCounter) Read(p []byte) (int, error) {
c.total += n
c.pos += n
if c.pos == uploadSize {
c.pos = 0
c.resetReader()
}
c.lock.Unlock()
Expand All @@ -63,6 +62,7 @@ func (c *BytesCounter) AvgBytes() float64 {
return float64(c.total) / time.Now().Sub(c.start).Seconds()
}

// AvgMbps returns the average mbits/second
func (c *BytesCounter) AvgMbps() float64 {
var base float64 = 125000
if c.mebi {
Expand All @@ -71,6 +71,7 @@ func (c *BytesCounter) AvgMbps() float64 {
return c.AvgBytes() / base
}

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

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

// resetReader resets the `reader` field to 0 position
func (c *BytesCounter) resetReader() (int64, error) {
c.pos = 0
return c.reader.Seek(0, 0)
}

Expand Down
19 changes: 11 additions & 8 deletions defs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ import (

// Server represents a speed test server
type Server struct {
Name string `json:"name"`
Server string `json:"server"`
DownloadURL string `json:"dlURL"`
UploadURL string `json:"ulURL"`
PingURL string `json:"pingURL"`
GetIPURL string `json:"getIpURL"`
ICMPFail bool `json:"-"`
TLog TelemetryLog `json:"-"`
Name string `json:"name"`
Server string `json:"server"`
DownloadURL string `json:"dlURL"`
UploadURL string `json:"ulURL"`
PingURL string `json:"pingURL"`
GetIPURL string `json:"getIpURL"`
SponsorName string `json:"sponsorName"`
SponsorURL string `json:"sponsorURL"`

ICMPFail bool `json:"-"`
TLog TelemetryLog `json:"-"`
}

// IsUp checks the speed test backend is up by accessing the ping URL
Expand Down
21 changes: 21 additions & 0 deletions speedtest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math"
"mime/multipart"
"net/http"
"net/url"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -45,6 +46,26 @@ func doSpeedTest(c *cli.Context, servers []defs.Server, telemetryServer defs.Tel

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

var sponsorMsg string
if currentServer.SponsorName != "" {
sponsorMsg += currentServer.SponsorName

if currentServer.SponsorURL != "" {
su, err := url.Parse(currentServer.SponsorURL)
if err != nil {
log.Debugf("Sponsor URL is invalid: %s", currentServer.SponsorURL)
} else {
if su.Scheme == "" {
su.Scheme = "https"
}
sponsorMsg += " @ " + su.String()
}
}
}
if sponsorMsg != "" {
log.Infof("Sponsored by: %s", sponsorMsg)
}

if currentServer.IsUp() {
ispInfo, err := currentServer.GetIPInfo(c.String(defs.OptionDistance))
if err != nil {
Expand Down

0 comments on commit 9b7219a

Please sign in to comment.