From 2dbabd3eadf4ef0c9bec0f5fe3373fa704f77d93 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Fri, 26 Apr 2024 15:00:22 +0200 Subject: [PATCH] Config dump --- internal/app.go | 51 +++++++++++++++++++++++++++++++--------- internal/config/types.go | 10 ++------ internal/config/v1.go | 10 ++++++++ pkg/http/client.go | 49 +++++++++++++++++++++----------------- 4 files changed, 80 insertions(+), 40 deletions(-) diff --git a/internal/app.go b/internal/app.go index e873e1e..bd3b9c0 100644 --- a/internal/app.go +++ b/internal/app.go @@ -47,27 +47,43 @@ func MainApp(configPath string) { apiClient := http.NewHTTPClient( config.GetToken(), - config.GetRetriesUntilFailed()*10, - config.GetApiTimeoutSecs(), - false, + config.GetRetriesUntilFailed()*10, // default retries = 2, so this is 20 + config.GetApiTimeoutSecs(), // default api timeout = 60 + false, // ipv6 support is not needed for api client config, log.Named("api_client"), ) unrestrictClient := http.NewHTTPClient( config.GetToken(), - config.GetRetriesUntilFailed(), - config.GetDownloadTimeoutSecs(), - false, + config.GetRetriesUntilFailed(), // default retries = 2 + config.GetDownloadTimeoutSecs(), // default download timeout = 10 + false, // this is also api client, so no ipv6 support config, log.Named("unrestrict_client"), ) - downloadClient := http.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), config.GetDownloadTimeoutSecs(), true, config, log.Named("download_client")) + downloadClient := http.NewHTTPClient( + "", + config.GetRetriesUntilFailed(), + config.GetDownloadTimeoutSecs(), + true, + config, + log.Named("download_client"), + ) - api := realdebrid.NewRealDebrid(apiClient, unrestrictClient, downloadClient, config, log.Named("realdebrid")) + api := realdebrid.NewRealDebrid( + apiClient, + unrestrictClient, + downloadClient, + config, + log.Named("realdebrid"), + ) - premium.MonitorPremiumStatus(api, zurglog) + premium.MonitorPremiumStatus( + api, + zurglog, + ) workerPool, err := ants.NewPool(config.GetNumOfWorkers()) if err != nil { @@ -77,12 +93,25 @@ func MainApp(configPath string) { defer workerPool.Release() utils.EnsureDirExists("data") // Ensure the data directory exists - torrentMgr := torrent.NewTorrentManager(config, api, workerPool, log.Named("manager")) + torrentMgr := torrent.NewTorrentManager( + config, + api, + workerPool, + log.Named("manager"), + ) downloader := universal.NewDownloader(downloadClient) router := chi.NewRouter() - handlers.AttachHandlers(router, downloader, torrentMgr, config, api, workerPool, log.Named("router")) + handlers.AttachHandlers( + router, + downloader, + torrentMgr, + config, + api, + workerPool, + log.Named("router"), + ) // go func() { // if err := netHttp.ListenAndServe(":6060", nil); err != nil && err != netHttp.ErrServerClosed { diff --git a/internal/config/types.go b/internal/config/types.go index 194d2cf..f30c81f 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -4,7 +4,6 @@ type ConfigInterface interface { GetConfig() ZurgConfig GetVersion() string GetToken() string - GetTokens() []string GetNumOfWorkers() int GetRefreshEverySecs() int GetRepairEveryMins() int @@ -35,9 +34,8 @@ type ConfigInterface interface { } type ZurgConfig struct { - Version string `yaml:"zurg" json:"-"` - Token string `yaml:"token" json:"-"` - Tokens []string `yaml:"tokens" json:"-"` + Version string `yaml:"zurg" json:"-"` + Token string `yaml:"token" json:"-"` ApiTimeoutSecs int `yaml:"api_timeout_secs" json:"api_timeout_secs"` CanRepair bool `yaml:"enable_repair" json:"enable_repair"` @@ -74,10 +72,6 @@ func (z *ZurgConfig) GetToken() string { return z.Token } -func (z *ZurgConfig) GetTokens() []string { - return z.Tokens -} - func (z *ZurgConfig) GetHost() string { if z.Host == "" { return "[::]" diff --git a/internal/config/v1.go b/internal/config/v1.go index 2331496..20b816d 100644 --- a/internal/config/v1.go +++ b/internal/config/v1.go @@ -23,6 +23,16 @@ func loadV1Config(content []byte, log *logutil.Logger) (*ZurgConfigV1, error) { if err := yaml.Unmarshal(content, &configV1); err != nil { return nil, err } + + // don't log token and password + bufToken := configV1.Token + configV1.Token = strings.Repeat("*", len(bufToken)-4) + bufToken[len(bufToken)-4:] + bufPassword := configV1.Password + configV1.Password = strings.Repeat("*", len(bufPassword)) + log.Debugf("Config dump: %+v", configV1) + configV1.Token = bufToken + configV1.Password = bufPassword + configV1.log = log return &configV1, nil } diff --git a/pkg/http/client.go b/pkg/http/client.go index 1cf6e18..01727e7 100644 --- a/pkg/http/client.go +++ b/pkg/http/client.go @@ -25,16 +25,16 @@ import ( ) type HTTPClient struct { - client *http.Client - maxRetries int - timeoutSecs int - backoff func(attempt int) time.Duration - bearerToken string - ensureIPv6Host bool - cfg config.ConfigInterface - ipv6 cmap.ConcurrentMap[string, string] - ipv6Hosts []string - log *logutil.Logger + client *http.Client + maxRetries int + timeoutSecs int + backoff func(attempt int) time.Duration + bearerToken string + supportIPv6 bool + cfg config.ConfigInterface + ipv6 cmap.ConcurrentMap[string, string] + ipv6Hosts []string + log *logutil.Logger } // { @@ -51,17 +51,24 @@ func (e *ApiErrorResponse) Error() string { return fmt.Sprintf("api response error: %s (code: %d)", e.Message, e.Code) } -func NewHTTPClient(token string, maxRetries int, timeoutSecs int, ensureIPv6Host bool, cfg config.ConfigInterface, log *logutil.Logger) *HTTPClient { +func NewHTTPClient( + token string, + maxRetries int, + timeoutSecs int, + supportIPv6 bool, + cfg config.ConfigInterface, + log *logutil.Logger, +) *HTTPClient { client := HTTPClient{ - bearerToken: token, - client: http.DefaultClient, - maxRetries: maxRetries, - timeoutSecs: timeoutSecs, - backoff: backoffFunc, - ensureIPv6Host: ensureIPv6Host, - cfg: cfg, - ipv6: cmap.New[string](), - log: log, + bearerToken: token, + client: http.DefaultClient, + maxRetries: maxRetries, + timeoutSecs: timeoutSecs, + backoff: backoffFunc, + supportIPv6: supportIPv6, + cfg: cfg, + ipv6: cmap.New[string](), + log: log, } var dialer proxy.Dialer = &net.Dialer{ @@ -191,7 +198,7 @@ func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) { } func (r *HTTPClient) replaceHostIfNeeded(req *http.Request) { - if strings.HasSuffix(req.Host, "download.real-debrid.cloud") || !r.ensureIPv6Host || !r.cfg.ShouldForceIPv6() { + if strings.HasSuffix(req.Host, "download.real-debrid.cloud") || !r.supportIPv6 || !r.cfg.ShouldForceIPv6() { return } // get subdomain of req.Host