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(
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 {

View File

@@ -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 "[::]"

View File

@@ -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
}

View File

@@ -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