Config dump

This commit is contained in:
Ben Sarmiento
2024-04-26 15:00:22 +02:00
parent 755b50c82f
commit 2dbabd3ead
4 changed files with 80 additions and 40 deletions

View File

@@ -47,27 +47,43 @@ func MainApp(configPath string) {
apiClient := http.NewHTTPClient( apiClient := http.NewHTTPClient(
config.GetToken(), config.GetToken(),
config.GetRetriesUntilFailed()*10, config.GetRetriesUntilFailed()*10, // default retries = 2, so this is 20
config.GetApiTimeoutSecs(), config.GetApiTimeoutSecs(), // default api timeout = 60
false, false, // ipv6 support is not needed for api client
config, config,
log.Named("api_client"), log.Named("api_client"),
) )
unrestrictClient := http.NewHTTPClient( unrestrictClient := http.NewHTTPClient(
config.GetToken(), config.GetToken(),
config.GetRetriesUntilFailed(), config.GetRetriesUntilFailed(), // default retries = 2
config.GetDownloadTimeoutSecs(), config.GetDownloadTimeoutSecs(), // default download timeout = 10
false, false, // this is also api client, so no ipv6 support
config, config,
log.Named("unrestrict_client"), 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()) workerPool, err := ants.NewPool(config.GetNumOfWorkers())
if err != nil { if err != nil {
@@ -77,12 +93,25 @@ func MainApp(configPath string) {
defer workerPool.Release() defer workerPool.Release()
utils.EnsureDirExists("data") // Ensure the data directory exists 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) downloader := universal.NewDownloader(downloadClient)
router := chi.NewRouter() 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() { // go func() {
// if err := netHttp.ListenAndServe(":6060", nil); err != nil && err != netHttp.ErrServerClosed { // if err := netHttp.ListenAndServe(":6060", nil); err != nil && err != netHttp.ErrServerClosed {

View File

@@ -4,7 +4,6 @@ type ConfigInterface interface {
GetConfig() ZurgConfig GetConfig() ZurgConfig
GetVersion() string GetVersion() string
GetToken() string GetToken() string
GetTokens() []string
GetNumOfWorkers() int GetNumOfWorkers() int
GetRefreshEverySecs() int GetRefreshEverySecs() int
GetRepairEveryMins() int GetRepairEveryMins() int
@@ -35,9 +34,8 @@ type ConfigInterface interface {
} }
type ZurgConfig struct { type ZurgConfig struct {
Version string `yaml:"zurg" json:"-"` Version string `yaml:"zurg" json:"-"`
Token string `yaml:"token" json:"-"` Token string `yaml:"token" json:"-"`
Tokens []string `yaml:"tokens" json:"-"`
ApiTimeoutSecs int `yaml:"api_timeout_secs" json:"api_timeout_secs"` ApiTimeoutSecs int `yaml:"api_timeout_secs" json:"api_timeout_secs"`
CanRepair bool `yaml:"enable_repair" json:"enable_repair"` CanRepair bool `yaml:"enable_repair" json:"enable_repair"`
@@ -74,10 +72,6 @@ func (z *ZurgConfig) GetToken() string {
return z.Token return z.Token
} }
func (z *ZurgConfig) GetTokens() []string {
return z.Tokens
}
func (z *ZurgConfig) GetHost() string { func (z *ZurgConfig) GetHost() string {
if z.Host == "" { if z.Host == "" {
return "[::]" return "[::]"

View File

@@ -23,6 +23,16 @@ func loadV1Config(content []byte, log *logutil.Logger) (*ZurgConfigV1, error) {
if err := yaml.Unmarshal(content, &configV1); err != nil { if err := yaml.Unmarshal(content, &configV1); err != nil {
return nil, err 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 configV1.log = log
return &configV1, nil return &configV1, nil
} }

View File

@@ -25,16 +25,16 @@ import (
) )
type HTTPClient struct { type HTTPClient struct {
client *http.Client client *http.Client
maxRetries int maxRetries int
timeoutSecs int timeoutSecs int
backoff func(attempt int) time.Duration backoff func(attempt int) time.Duration
bearerToken string bearerToken string
ensureIPv6Host bool supportIPv6 bool
cfg config.ConfigInterface cfg config.ConfigInterface
ipv6 cmap.ConcurrentMap[string, string] ipv6 cmap.ConcurrentMap[string, string]
ipv6Hosts []string ipv6Hosts []string
log *logutil.Logger 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) 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{ client := HTTPClient{
bearerToken: token, bearerToken: token,
client: http.DefaultClient, client: http.DefaultClient,
maxRetries: maxRetries, maxRetries: maxRetries,
timeoutSecs: timeoutSecs, timeoutSecs: timeoutSecs,
backoff: backoffFunc, backoff: backoffFunc,
ensureIPv6Host: ensureIPv6Host, supportIPv6: supportIPv6,
cfg: cfg, cfg: cfg,
ipv6: cmap.New[string](), ipv6: cmap.New[string](),
log: log, log: log,
} }
var dialer proxy.Dialer = &net.Dialer{ 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) { 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 return
} }
// get subdomain of req.Host // get subdomain of req.Host