Skip to content

Commit

Permalink
v0.0.6 - 尝试支持speedtest原生命令
Browse files Browse the repository at this point in the history
  • Loading branch information
spiritLHLS committed Jun 30, 2024
1 parent b836758 commit c11bb30
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: |
git config --global user.name 'github-actions'
git config --global user.email 'github-actions@github.com'
TAG="v0.0.5-$(date +'%Y%m%d%H%M%S')"
TAG="v0.0.6-$(date +'%Y%m%d%H%M%S')"
git tag $TAG
git push origin $TAG
env:
Expand Down
36 changes: 31 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
}()
fmt.Println("项目地址:", Blue("https://github.com/oneclickvirt/speedtest"))
var showVersion, nearByServer, showHead bool
var language, operator, platform string
var language, operator, platform, testMethod string
var num int
flag.BoolVar(&showVersion, "v", false, "Show version information")
flag.BoolVar(&nearByServer, "nearby", false, "Test only nearby servers")
Expand All @@ -26,6 +26,7 @@ func main() {
flag.StringVar(&language, "l", "zh", "Language parameter (options: en, zh)")
flag.StringVar(&platform, "pf", "net", "Platform parameter (options: net, cn)")
flag.StringVar(&operator, "opt", "global", "Operator parameter (options: cmcc, cu, ct, sg, tw, jp, hk, global)")
flag.StringVar(&testMethod, "m", "speedtest", "Test Method parameter (options: origin, speedtest, speedtest-go)")
flag.IntVar(&num, "num", -1, "Number of test servers, default -1 not to limit")
flag.Parse()
if showVersion {
Expand All @@ -36,7 +37,11 @@ func main() {
sp.ShowHead(language)
}
if nearByServer {
sp.NearbySpeedTest()
if strings.ToLower(testMethod) == "origin" {
sp.NearbySpeedTest()
} else if strings.ToLower(testMethod) == "speedtest" {
sp.OfficialNearbySpeedTest()
}
return
}
var url, parseType string
Expand Down Expand Up @@ -77,9 +82,30 @@ func main() {
}
parseType = "id"
}
if url != "" && parseType != "" {
sp.CustomSpeedTest(url, parseType, num)
if strings.ToLower(testMethod) == "origin" {
if url != "" && parseType != "" {
sp.CustomSpeedTest(url, parseType, num)
} else {
fmt.Println("-opt/-pf with wrong operator.")
}
} else if strings.ToLower(testMethod) == "speedtest" {
err := sp.OfficialAvailableTest()
if err == nil {
if url != "" && parseType != "" {
sp.OfficialCustomSpeedTest(url, parseType, num)
} else {
fmt.Println("-opt/-pf with wrong operator.")
}
} else {
fmt.Println("Can not match speedtest command, switch to use origin test")
if url != "" && parseType != "" {
sp.CustomSpeedTest(url, parseType, num)
} else {
fmt.Println("-opt/-pf with wrong operator.")
}
}
} else {
fmt.Println("Wrong operator.")
fmt.Println("-m with wrong operator.")
}

}
2 changes: 1 addition & 1 deletion model/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package model

const SpeedTestVersion = "v0.0.5"
const SpeedTestVersion = "v0.0.6"

var EnableLoger = false
var (
Expand Down
129 changes: 129 additions & 0 deletions sp/sp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/csv"
"fmt"
"io"
"os/exec"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -158,6 +159,134 @@ func ShowHead(language string) {
}
}

func OfficialAvailableTest() error {
if model.EnableLoger {
InitLogger()
defer Logger.Sync()
}
spvCheck := exec.Command("speedtest", "--version")
output, err := spvCheck.CombinedOutput()
if err != nil {
return err
} else {
version := strings.Split(string(output), "\n")[0]
if strings.Contains(version, "Speedtest by Ookla") && !strings.Contains(version, "err") {
// 此时确认可使用speedtest命令进行测速
return nil
}
}
return fmt.Errorf("No match speedtest command")
}

func OfficialNearbySpeedTest() {
if model.EnableLoger {
InitLogger()
defer Logger.Sync()
}
var serverName, UPStr, DLStr, Latency, PacketLoss string // serverID,
// speedtest --progress=no --accept-license --accept-gdpr
sptCheck := exec.Command("speedtest", "--progress=no", "--accept-license", "--accept-gdpr")
temp, err := sptCheck.CombinedOutput()
if err == nil {
serverName = "Speedtest.net"
tempList := strings.Split(string(temp), "\n")
for _, line := range tempList {
if strings.Contains(line, "Idle Latency") {
Latency = strings.TrimSpace(strings.Split(strings.Split(line, ":")[1], "(")[0])
} else if strings.Contains(line, "Download") {
DLStr = strings.TrimSpace(strings.Split(strings.Split(line, ":")[1], "(")[0])
} else if strings.Contains(line, "Upload") {
UPStr = strings.TrimSpace(strings.Split(strings.Split(line, ":")[1], "(")[0])
} else if strings.Contains(line, "Packet Loss") {
PacketLoss = strings.TrimSpace(strings.Split(line, ":")[1])
}
}
if Latency != "" && DLStr != "" && UPStr != "" && PacketLoss != "" {
fmt.Print(formatString(serverName, 16))
fmt.Print(formatString(UPStr, 16))
fmt.Print(formatString(DLStr, 16))
fmt.Print(formatString(Latency, 16))
fmt.Print(formatString(PacketLoss, 16))
fmt.Println()
}
}
}

func OfficialCustomSpeedTest(url, byWhat string, num int) {
if model.EnableLoger {
InitLogger()
defer Logger.Sync()
}
if !strings.Contains(url, ".net") {
return
}
data := getData(url)
var targets speedtest.Servers
if byWhat == "id" {
targets = parseDataFromID(data, url)
} else if byWhat == "url" {
targets = parseDataFromURL(data, url)
}
var pingList []time.Duration
var err error
serverMap := make(map[time.Duration]*speedtest.Server)
for _, server := range targets {
err = server.PingTest(nil)
if err != nil {
server.Latency = 1000 * time.Millisecond
if model.EnableLoger {
Logger.Info(err.Error())
}
}
pingList = append(pingList, server.Latency)
serverMap[server.Latency] = server
}
sort.Slice(pingList, func(i, j int) bool {
return pingList[i] < pingList[j]
})
if num == -1 || num >= len(pingList) {
num = len(pingList)
} else if len(pingList) == 0 {
fmt.Println("No match servers")
if model.EnableLoger {
Logger.Info("No match servers")
}
return
}
var serverName, UPStr, DLStr, Latency, PacketLoss string
for i := 0; i < len(pingList); i++ {
server := serverMap[pingList[i]]
if i < num {
// speedtest --progress=no --accept-license --accept-gdpr
sptCheck := exec.Command("speedtest", "--progress=no", "--server-id="+server.ID, "--accept-license", "--accept-gdpr")
temp, err := sptCheck.CombinedOutput()
if err == nil {
serverName = server.Name
tempList := strings.Split(string(temp), "\n")
for _, line := range tempList {
if strings.Contains(line, "Idle Latency") {
Latency = strings.TrimSpace(strings.Split(strings.Split(line, ":")[1], "(")[0])
} else if strings.Contains(line, "Download") {
DLStr = strings.TrimSpace(strings.Split(strings.Split(line, ":")[1], "(")[0])
} else if strings.Contains(line, "Upload") {
UPStr = strings.TrimSpace(strings.Split(strings.Split(line, ":")[1], "(")[0])
} else if strings.Contains(line, "Packet Loss") {
PacketLoss = strings.TrimSpace(strings.Split(line, ":")[1])
}
}
if Latency != "" && DLStr != "" && UPStr != "" && PacketLoss != "" {
fmt.Print(formatString(serverName, 16))
fmt.Print(formatString(UPStr, 16))
fmt.Print(formatString(DLStr, 16))
fmt.Print(formatString(Latency, 16))
fmt.Print(formatString(PacketLoss, 16))
fmt.Println()
}
}
}
}
}

func NearbySpeedTest() {
if model.EnableLoger {
InitLogger()
Expand Down

0 comments on commit c11bb30

Please sign in to comment.