Allow custom url

This commit is contained in:
Ben Adrian Sarmiento
2024-06-16 21:11:40 +02:00
parent e5bb026f87
commit f2331c6a2e
4 changed files with 20 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ import (
func main() {
var configPath string // Variable to hold the config path
var testURL string // Variable to hold the download URL
var rootCmd = &cobra.Command{
Use: "zurg",
@@ -49,10 +50,13 @@ func main() {
Use: "network-test",
Short: "Test your network with RD servers",
Run: func(cmd *cobra.Command, args []string) {
internal.NetworkTest()
internal.NetworkTest(testURL) // Pass the downloadURL to NetworkTest
},
}
// Adding a flag for the test URL with a default value
networkTestCmd.Flags().StringVarP(&testURL, "test-url", "t", "", "URL to test network with")
rootCmd.AddCommand(versionCmd, clearDownloadsCmd, clearTorrentsCmd, networkTestCmd)
if err := rootCmd.Execute(); err != nil {

View File

@@ -56,7 +56,7 @@ func MainApp(configPath string) {
repoClient4 := http.NewHTTPClient("", 0, 1, false, []string{}, proxyURL, log.Named("network_test"))
repoClient6 := http.NewHTTPClient("", 0, 1, true, []string{}, proxyURL, log.Named("network_test"))
repo := http.NewIPRepository(repoClient4, repoClient6, log.Named("network_test"))
repo := http.NewIPRepository(repoClient4, repoClient6, "", log.Named("network_test"))
repo.NetworkTest(false)
apiClient := http.NewHTTPClient(

View File

@@ -19,7 +19,7 @@ func ShowVersion() {
version.GetBuiltAt(), version.GetGitCommit(), version.GetVersion())
}
func NetworkTest() {
func NetworkTest(testURL string) {
utils.EnsureDirExists("logs")
utils.EnsureDirExists("data")
@@ -32,7 +32,7 @@ func NetworkTest() {
repoClient4 := http.NewHTTPClient("", 0, 1, false, []string{}, proxyURL, log.Named("network_test"))
repoClient6 := http.NewHTTPClient("", 0, 1, true, []string{}, proxyURL, log.Named("network_test"))
repo := http.NewIPRepository(repoClient4, repoClient6, log.Named("network_test"))
repo := http.NewIPRepository(repoClient4, repoClient6, testURL, log.Named("network_test"))
repo.NetworkTest(true)
}

View File

@@ -8,6 +8,7 @@ import (
"math/rand"
"net"
"net/http"
"net/url"
"os"
"sort"
"time"
@@ -20,15 +21,17 @@ type IPRepository struct {
ipv6client *HTTPClient
ipv4latencyMap map[string]float64
ipv6latencyMap map[string]float64
testURL string
log *logutil.Logger
}
func NewIPRepository(ipv4client *HTTPClient, ipv6client *HTTPClient, log *logutil.Logger) *IPRepository {
func NewIPRepository(ipv4client *HTTPClient, ipv6client *HTTPClient, testURL string, log *logutil.Logger) *IPRepository {
repo := &IPRepository{
ipv4client: ipv4client,
ipv6client: ipv6client,
ipv4latencyMap: make(map[string]float64),
ipv6latencyMap: make(map[string]float64),
testURL: testURL,
log: log,
}
@@ -169,7 +172,13 @@ func (r *IPRepository) runLatencyTest() {
func (r *IPRepository) testDomainLatency(client *HTTPClient, domain string) (float64, error) {
const testFileSize = 1 // byte
const iterations = 3
url := fmt.Sprintf("https://%s/speedtest/test.rar/%f", domain, rand.Float64())
testURL := fmt.Sprintf("https://%s/speedtest/test.rar/%f", domain, rand.Float64())
if r.testURL != "" {
parsedURL, err := url.Parse(r.testURL)
if err == nil {
testURL = fmt.Sprintf("https://%s%s", domain, parsedURL.Path)
}
}
var totalDuration float64
var retErr error
@@ -177,7 +186,7 @@ func (r *IPRepository) testDomainLatency(client *HTTPClient, domain string) (flo
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, testURL, nil)
if err != nil {
r.log.Warnf("Failed to create request for %s: %v", domain, err)
retErr = err