Add network test command

This commit is contained in:
Ben Adrian Sarmiento
2024-06-16 09:47:29 +02:00
parent f4d3f273f6
commit 42a08583ce
7 changed files with 60 additions and 47 deletions

View File

@@ -45,7 +45,15 @@ func main() {
},
}
rootCmd.AddCommand(versionCmd, clearDownloadsCmd, clearTorrentsCmd)
var networkTestCmd = &cobra.Command{
Use: "network-test",
Short: "Test your network with RD servers",
Run: func(cmd *cobra.Command, args []string) {
internal.NetworkTest()
},
}
rootCmd.AddCommand(versionCmd, clearDownloadsCmd, clearTorrentsCmd, networkTestCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)

View File

@@ -52,8 +52,10 @@ func MainApp(configPath string) {
os.Exit(1)
}
repoClient4 := http.NewHTTPClient("", 0, 1, false, []string{}, config, log.Named("network_test"))
repoClient6 := http.NewHTTPClient("", 0, 1, true, []string{}, config, log.Named("network_test"))
proxyURL := os.Getenv("PROXY")
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.NetworkTest(false)
@@ -63,7 +65,7 @@ func MainApp(configPath string) {
config.GetApiTimeoutSecs(), // default api timeout = 60
false, // no need for ipv6 support
[]string{}, // no optimal hosts needed
config,
proxyURL,
log.Named("api_client"),
)
@@ -73,7 +75,7 @@ func MainApp(configPath string) {
config.GetDownloadTimeoutSecs(), // default download timeout = 10
false, // no need for ipv6 support
[]string{}, // no optimal hosts needed
config,
proxyURL,
log.Named("unrestrict_client"),
)
@@ -86,7 +88,7 @@ func MainApp(configPath string) {
config.GetDownloadTimeoutSecs(),
config.ShouldForceIPv6(),
hosts,
config,
proxyURL,
log.Named("download_client"),
)

View File

@@ -5,10 +5,13 @@ import (
"fmt"
"os"
"strings"
"time"
"github.com/debridmediamanager/zurg/internal/clear"
"github.com/debridmediamanager/zurg/internal/version"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/debridmediamanager/zurg/pkg/http"
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/utils"
)
func ShowVersion() {
@@ -16,8 +19,19 @@ func ShowVersion() {
version.GetBuiltAt(), version.GetGitCommit(), version.GetVersion())
}
func NetworkTest(testType string) {
realdebrid.RunTest(testType)
func NetworkTest() {
utils.EnsureDirExists("logs")
utils.EnsureDirExists("data")
dateStr := time.Now().Format(time.DateOnly)
timeStr := strings.ReplaceAll(time.Now().Format(time.TimeOnly), ":", "-")
logPath := fmt.Sprintf("logs/network-test-%s-%s.log", dateStr, timeStr)
log := logutil.NewLogger(logPath)
repoClient4 := http.NewHTTPClient("", 0, 1, false, []string{}, os.Getenv("PROXY"), log.Named("network_test"))
repoClient6 := http.NewHTTPClient("", 0, 1, true, []string{}, os.Getenv("PROXY"), log.Named("network_test"))
repo := http.NewIPRepository(repoClient4, repoClient6, log.Named("network_test"))
repo.NetworkTest(true)
}
func ClearDownloads() {

View File

@@ -18,7 +18,6 @@ type ConfigInterface interface {
GetPort() string
GetUsername() string
GetPassword() string
GetProxy() string
GetDirectories() []string
MeetsConditions(directory, torrentName string, torrentSize int64, torrentIDs, fileNames []string, fileSizes []int64, mediaInfos []*ffprobe.ProbeData) bool
GetOnLibraryUpdate() string
@@ -101,13 +100,6 @@ func (z *ZurgConfig) GetPassword() string {
return z.Password
}
func (z *ZurgConfig) GetProxy() string {
if os.Getenv("PROXY") != "" {
return os.Getenv("PROXY")
}
return z.Proxy
}
func (z *ZurgConfig) GetNumOfWorkers() int {
if z.NumOfWorkers == 0 {
return 20

View File

@@ -14,7 +14,6 @@ import (
"strings"
"time"
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/pkg/logutil"
http_dialer "github.com/mwitkow/go-http-dialer"
"golang.org/x/net/proxy"
@@ -23,15 +22,15 @@ import (
)
type HTTPClient struct {
client *http.Client
maxRetries int
timeoutSecs int
backoff func(attempt int) time.Duration
bearerToken string
cfg config.ConfigInterface
dnsCache cmap.ConcurrentMap[string, string]
optimalHosts []string
log *logutil.Logger
client *http.Client
maxRetries int
timeoutSecs int
rateLimitSleepSecs int
backoff func(attempt int) time.Duration
bearerToken string
dnsCache cmap.ConcurrentMap[string, string]
optimalHosts []string
log *logutil.Logger
}
type ApiErrorResponse struct {
@@ -49,26 +48,26 @@ func NewHTTPClient(
timeoutSecs int,
forceIPv6 bool,
optimalHosts []string,
cfg config.ConfigInterface,
proxyURL string,
log *logutil.Logger,
) *HTTPClient {
client := HTTPClient{
bearerToken: token,
client: &http.Client{},
maxRetries: maxRetries,
timeoutSecs: timeoutSecs,
backoff: backoffFunc,
cfg: cfg,
dnsCache: cmap.New[string](),
optimalHosts: optimalHosts,
log: log,
bearerToken: token,
client: &http.Client{},
maxRetries: maxRetries,
timeoutSecs: timeoutSecs,
rateLimitSleepSecs: 4,
backoff: backoffFunc,
dnsCache: cmap.New[string](),
optimalHosts: optimalHosts,
log: log,
}
var dialer proxy.Dialer = &net.Dialer{
Timeout: time.Duration(timeoutSecs) * time.Second, // timeout for dns resolution, tcp handshake
}
if proxyURLString := cfg.GetProxy(); proxyURLString != "" {
if proxyURLString := proxyURL; proxyURLString != "" {
proxyURL, err := url.Parse(proxyURLString)
if err != nil {
log.Errorf("Failed to parse proxy URL: %v", err)
@@ -81,15 +80,10 @@ func NewHTTPClient(
}
}
maxConnections := cfg.GetNumOfWorkers()
if maxConnections > 32 {
maxConnections = 32 // real-debrid has a limit of 32 connections per server/host
}
client.client.Transport = &http.Transport{
ResponseHeaderTimeout: time.Duration(timeoutSecs) * time.Second,
MaxIdleConns: 0,
MaxConnsPerHost: maxConnections,
MaxConnsPerHost: 32,
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
return dialer.Dial(network, address)
},
@@ -168,7 +162,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
}
}
incr := r.shouldRetry(req, resp, err, r.cfg.GetRateLimitSleepSecs())
incr := r.shouldRetry(req, resp, err, r.rateLimitSleepSecs)
if incr > 0 {
attempt += incr
if attempt > r.maxRetries {

View File

@@ -53,12 +53,15 @@ func (r *IPRepository) NetworkTest(forceRun bool) {
}
if ipv4Loaded && ipv6Loaded {
return
} else {
r.log.Warn("Network test files not found")
}
}
r.log.Info("Network test will start now (this will only run once). IGNORE THE WARNINGS!")
r.log.Info("Network test will start now. IGNORE THE WARNINGS!")
r.runLatencyTest()
r.log.Infof("Network test completed. Saving the results to %s and %s", ipv4latencyFile, ipv6latencyFile)
r.log.Info("Network test completed!")
r.log.Infof("To rerun the network test, run 'zurg network-test', or delete the files %s and %s and run zurg again", ipv4latencyFile, ipv6latencyFile)
r.writeLatencyFile(ipv4latencyFile, r.ipv4latencyMap)
r.writeLatencyFile(ipv6latencyFile, r.ipv6latencyMap)
}