diff --git a/api/media.go b/api/media.go index a48525a9..848405c0 100644 --- a/api/media.go +++ b/api/media.go @@ -12,6 +12,9 @@ import ( "github.com/gotd/td/tg" "github.com/krau/ffmpeg-go" ytdlp "github.com/lrstanley/go-ytdlp" + + "github.com/krau/SaveAny-Bot/config" + ytdlptask "github.com/krau/SaveAny-Bot/core/tasks/ytdlp" ) var ( @@ -185,15 +188,13 @@ func extractDirectMediaMetadata(ctx context.Context, url string) (*MediaMetadata func extractYTDLPMediaMetadata(ctx context.Context, url string) (*MediaMetadataResponse, error) { cmd := ytdlp.New().DumpSingleJSON() - result, err := cmd.Run(ctx, url) + // Use the same proxies as downloads, so a lookup cannot report a URL + // unreachable that a download would fetch. + result, err := ytdlptask.RunWithProxyFailover(ctx, cmd, config.C().Ytdlp, url) if err != nil { return nil, fmt.Errorf("failed to inspect media: %w", err) } - if result.ExitCode != 0 { - return nil, fmt.Errorf("failed to inspect media: %s", result.Stderr) - } - var e *ytdlp.ExtractedInfo infoList := make([]*ytdlp.ExtractedInfo, 0) diff --git a/config/ytdlp.go b/config/ytdlp.go index 0f9d6b66..1250be38 100644 --- a/config/ytdlp.go +++ b/config/ytdlp.go @@ -1,5 +1,11 @@ package config +import ( + "fmt" + "net/url" + "strings" +) + // DefaultYtdlpFilenameTemplate is used when ytdlp.filename_template is not set. const DefaultYtdlpFilenameTemplate = "%(title)s.%(ext)s" @@ -19,4 +25,32 @@ type YtdlpConfig struct { // RestrictFilenames keeps file names ASCII-only (yt-dlp --restrict-filenames), // dropping non-latin characters from titles. Disabled by default. RestrictFilenames bool `toml:"restrict_filenames" mapstructure:"restrict_filenames" json:"restrict_filenames"` + // Proxies are HTTP/HTTPS/SOCKS proxy URLs for yt-dlp's own requests. They + // are tried in order until one succeeds, so put the preferred proxy first. + // Empty means yt-dlp connects directly. + Proxies []string `toml:"proxies" mapstructure:"proxies" json:"proxies"` +} + +// ValidateProxies reports the first entry in Proxies that yt-dlp cannot use. +// Blank entries are ignored. +func (c YtdlpConfig) ValidateProxies() error { + for _, raw := range c.Proxies { + raw = strings.TrimSpace(raw) + if raw == "" { + continue + } + u, err := url.Parse(raw) + if err != nil { + return fmt.Errorf("invalid yt-dlp proxy: %w", err) + } + switch u.Scheme { + case "http", "https", "socks4", "socks4a", "socks5", "socks5h": + default: + return fmt.Errorf("invalid yt-dlp proxy %q: scheme must be http, https, socks4, socks4a, socks5 or socks5h", u.Redacted()) + } + if u.Host == "" { + return fmt.Errorf("invalid yt-dlp proxy %q: missing host", u.Redacted()) + } + } + return nil } diff --git a/config/ytdlp_test.go b/config/ytdlp_test.go new file mode 100644 index 00000000..865e9a3b --- /dev/null +++ b/config/ytdlp_test.go @@ -0,0 +1,26 @@ +package config + +import "testing" + +func TestYtdlpConfigValidateProxies(t *testing.T) { + cases := []struct { + name string + proxies []string + wantErr bool + }{ + {name: "empty", proxies: nil}, + {name: "supported schemes", proxies: []string{"http://a:1", "https://a:1", "socks4://a:1", "socks4a://a:1", "socks5://u:p@a:1", "socks5h://a:1"}}, + {name: "blank entries ignored", proxies: []string{" ", ""}}, + {name: "missing scheme", proxies: []string{"127.0.0.1:1080"}, wantErr: true}, + {name: "unsupported scheme", proxies: []string{"ftp://a:1"}, wantErr: true}, + {name: "missing host", proxies: []string{"socks5://"}, wantErr: true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + err := YtdlpConfig{Proxies: tc.proxies}.ValidateProxies() + if (err != nil) != tc.wantErr { + t.Fatalf("ValidateProxies() error = %v, wantErr %v", err, tc.wantErr) + } + }) + } +} diff --git a/core/tasks/ytdlp/execute.go b/core/tasks/ytdlp/execute.go index 484f0c8d..959bf705 100644 --- a/core/tasks/ytdlp/execute.go +++ b/core/tasks/ytdlp/execute.go @@ -116,7 +116,8 @@ func buildDownloadCommand(cfg config.YtdlpConfig, tempDir string, flags []string func (t *Task) downloadFiles(ctx context.Context, tempDir string) ([]string, error) { logger := log.FromContext(ctx) - cmd, flags, err := buildDownloadCommand(config.C().Ytdlp, tempDir, t.Flags) + cfg := config.C().Ytdlp + cmd, flags, err := buildDownloadCommand(cfg, tempDir, t.Flags) if err != nil { return nil, err } @@ -130,19 +131,13 @@ func (t *Task) downloadFiles(ctx context.Context, tempDir string) ([]string, err // yt-dlp accepts: yt-dlp [OPTIONS] URL [URL...] args := append(flags, t.URLs...) - // Run with context for cancellation support - result, err := cmd.Run(ctx, args...) - if err != nil { + if _, err := RunWithProxyFailover(ctx, cmd, cfg, args...); err != nil { if errors.Is(err, context.Canceled) { return nil, err } return nil, fmt.Errorf("yt-dlp execution failed: %w", err) } - if result.ExitCode != 0 { - return nil, fmt.Errorf("yt-dlp exited with code %d: %s", result.ExitCode, result.Stderr) - } - files, err := collectDownloadedFiles(tempDir) if err != nil { return nil, err @@ -165,6 +160,10 @@ func collectDownloadedFiles(dir string) ([]string, error) { if entry.IsDir() { return nil } + // A failed proxy attempt can leave partial downloads behind. + if isYtdlpTempFile(entry.Name()) { + return nil + } rel, err := filepath.Rel(dir, path) if err != nil { return err @@ -177,6 +176,17 @@ func collectDownloadedFiles(dir string) ([]string, error) { return files, nil } +// isYtdlpTempFile reports whether name is one of yt-dlp's in-progress files +// rather than a finished download. +func isYtdlpTempFile(name string) bool { + for _, suffix := range []string{".part", ".ytdl", ".temp"} { + if strings.HasSuffix(name, suffix) { + return true + } + } + return false +} + // transferFile transfers the file at relPath below tempDir to storage, keeping // the directories an output template created. func (t *Task) transferFile(ctx context.Context, tempDir, relPath string) error { diff --git a/core/tasks/ytdlp/proxy.go b/core/tasks/ytdlp/proxy.go new file mode 100644 index 00000000..5e34b833 --- /dev/null +++ b/core/tasks/ytdlp/proxy.go @@ -0,0 +1,130 @@ +package ytdlp + +import ( + "context" + "errors" + "fmt" + "net/url" + "strings" + + "github.com/charmbracelet/log" + ytdlp "github.com/lrstanley/go-ytdlp" + + "github.com/krau/SaveAny-Bot/config" +) + +// noProxy is the candidate for a direct run without --proxy. +const noProxy = "" + +// RunWithProxyFailover runs cmd once per configured proxy, in order, until a +// run succeeds. With no proxies configured it runs once without --proxy. +// Cancellation stops the rotation immediately. +func RunWithProxyFailover(ctx context.Context, cmd *ytdlp.Command, cfg config.YtdlpConfig, args ...string) (*ytdlp.Result, error) { + logger := log.FromContext(ctx) + candidates := proxyCandidates(cfg) + + var lastErr error + for attempt, proxy := range candidates { + // Each Run rebuilds the command line, so replacing the proxy on the same + // builder never stacks --proxy flags. + if proxy == noProxy { + cmd = cmd.UnsetProxy() + } else { + cmd = cmd.Proxy(proxy) + } + + result, err := cmd.Run(ctx, args...) + switch { + case err == nil && result.ExitCode == 0: + if attempt > 0 { + logger.Infof("yt-dlp succeeded via proxy %d/%d (%s)", attempt+1, len(candidates), redactProxy(proxy)) + } + return result, nil + case err != nil: + lastErr = err + default: + lastErr = fmt.Errorf("yt-dlp exited with code %d: %s", result.ExitCode, result.Stderr) + } + lastErr = redactProxiesInError(lastErr, candidates) + + if errors.Is(lastErr, context.Canceled) || ctx.Err() != nil { + return nil, lastErr + } + if len(candidates) > 1 { + logger.Warnf("yt-dlp failed via proxy %d/%d (%s): %v", attempt+1, len(candidates), redactProxy(proxy), lastErr) + } + } + + if len(candidates) > 1 { + return nil, fmt.Errorf("all %d proxies failed, last error: %w", len(candidates), lastErr) + } + return nil, lastErr +} + +// proxyCandidates returns the configured proxies, trimmed and deduplicated in +// their configured order, or a single direct candidate when none are set. +func proxyCandidates(cfg config.YtdlpConfig) []string { + seen := make(map[string]struct{}, len(cfg.Proxies)) + candidates := make([]string, 0, len(cfg.Proxies)) + for _, proxy := range cfg.Proxies { + proxy = strings.TrimSpace(proxy) + if proxy == "" { + continue + } + if _, dup := seen[proxy]; dup { + continue + } + seen[proxy] = struct{}{} + candidates = append(candidates, proxy) + } + if len(candidates) == 0 { + return []string{noProxy} + } + return candidates +} + +// redactProxy hides the password in a proxy URL before it is logged. +func redactProxy(proxy string) string { + if proxy == noProxy { + return "direct" + } + u, err := url.Parse(proxy) + if err != nil || u.Host == "" { + return "" + } + if u.User != nil { + if _, ok := u.User.Password(); ok { + u.User = url.UserPassword(u.User.Username(), "xxxxx") + } + } + return u.Redacted() +} + +// redactedError keeps the original error for errors.Is/As while replacing its +// message. +type redactedError struct { + msg string + err error +} + +func (e *redactedError) Error() string { return e.msg } +func (e *redactedError) Unwrap() error { return e.err } + +// redactProxiesInError replaces proxy URLs that yt-dlp echoed into err (for +// example in connection errors) with their redacted form. +func redactProxiesInError(err error, proxies []string) error { + msg := err.Error() + redacted := msg + for _, proxy := range proxies { + if proxy == noProxy { + continue + } + if safe := redactProxy(proxy); safe != proxy { + redacted = strings.ReplaceAll(redacted, proxy, safe) + } + } + if redacted == msg { + return err + } + return &redactedError{msg: redacted, err: err} +} diff --git a/core/tasks/ytdlp/proxy_test.go b/core/tasks/ytdlp/proxy_test.go new file mode 100644 index 00000000..4ee59e3a --- /dev/null +++ b/core/tasks/ytdlp/proxy_test.go @@ -0,0 +1,207 @@ +package ytdlp + +import ( + "context" + "os" + "path/filepath" + "reflect" + "runtime" + "strings" + "testing" + + ytdlp "github.com/lrstanley/go-ytdlp" + + "github.com/krau/SaveAny-Bot/config" +) + +// fakeYtdlp writes a stub yt-dlp that records its arguments, so tests never +// touch the network. +func fakeYtdlp(t *testing.T, script string) (bin, logPath string) { + t.Helper() + if runtime.GOOS == "windows" { + t.Skip("shell stub is POSIX-only") + } + dir := t.TempDir() + bin = filepath.Join(dir, "yt-dlp") + logPath = filepath.Join(dir, "invocations.log") + body := "#!/bin/sh\nprintf '%s\\n' \"$*\" >> " + logPath + "\n" + script + if err := os.WriteFile(bin, []byte(body), 0o700); err != nil { + t.Fatalf("write fake yt-dlp: %v", err) + } + return bin, logPath +} + +func invocations(t *testing.T, logPath string) []string { + t.Helper() + data, err := os.ReadFile(logPath) + if os.IsNotExist(err) { + return nil + } + if err != nil { + t.Fatalf("read invocation log: %v", err) + } + return strings.Split(strings.TrimSuffix(string(data), "\n"), "\n") +} + +func TestProxyCandidates(t *testing.T) { + cases := []struct { + name string + proxies []string + want []string + }{ + {name: "none configured runs once without a proxy", want: []string{""}}, + {name: "single proxy", proxies: []string{"http://a:1"}, want: []string{"http://a:1"}}, + { + name: "order is preserved so the first entry is preferred", + proxies: []string{"http://a:1", "socks5://b:2", "http://c:3"}, + want: []string{"http://a:1", "socks5://b:2", "http://c:3"}, + }, + {name: "blank entries dropped", proxies: []string{"", " ", "http://a:1"}, want: []string{"http://a:1"}}, + {name: "surrounding whitespace trimmed", proxies: []string{" http://a:1 "}, want: []string{"http://a:1"}}, + { + name: "duplicates dropped, first position kept", + proxies: []string{"http://a:1", "http://b:2", "http://a:1"}, + want: []string{"http://a:1", "http://b:2"}, + }, + {name: "all entries blank falls back to a direct attempt", proxies: []string{"", " "}, want: []string{""}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got := proxyCandidates(config.YtdlpConfig{Proxies: tc.proxies}) + if !reflect.DeepEqual(got, tc.want) { + t.Fatalf("proxyCandidates() = %#v, want %#v", got, tc.want) + } + }) + } +} + +func TestRunWithProxyFailoverUsesFirstProxy(t *testing.T) { + bin, logPath := fakeYtdlp(t, "exit 0\n") + cfg := config.YtdlpConfig{Proxies: []string{"http://first:1", "http://second:2"}} + + if _, err := RunWithProxyFailover(t.Context(), ytdlp.New().SetExecutable(bin), cfg, "URL"); err != nil { + t.Fatalf("RunWithProxyFailover: %v", err) + } + got := invocations(t, logPath) + if len(got) != 1 || !strings.Contains(got[0], "--proxy http://first:1") { + t.Fatalf("want one run via the first proxy, got %q", got) + } +} + +func TestRunWithProxyFailoverFallsThroughToLater(t *testing.T) { + bin, logPath := fakeYtdlp(t, ` +case "$*" in + *--proxy\ http://alive:3*) exit 0 ;; +esac +exit 1 +`) + proxies := []string{"http://dead:1", "http://alsodead:2", "http://alive:3"} + + if _, err := RunWithProxyFailover(t.Context(), ytdlp.New().SetExecutable(bin), config.YtdlpConfig{Proxies: proxies}, "URL"); err != nil { + t.Fatalf("want the third proxy to succeed, got %v", err) + } + got := invocations(t, logPath) + if len(got) != 3 { + t.Fatalf("want 3 runs, got %d: %q", len(got), got) + } + for i, p := range proxies { + if !strings.Contains(got[i], "--proxy "+p) || strings.Count(got[i], "--proxy") != 1 { + t.Fatalf("run %d: want exactly proxy %s, got %q", i+1, p, got[i]) + } + } +} + +func TestRunWithProxyFailoverAllFail(t *testing.T) { + bin, logPath := fakeYtdlp(t, "echo boom >&2\nexit 1\n") + cfg := config.YtdlpConfig{Proxies: []string{"http://a:1", "http://b:2"}} + + _, err := RunWithProxyFailover(t.Context(), ytdlp.New().SetExecutable(bin), cfg, "URL") + if err == nil || !strings.Contains(err.Error(), "all 2 proxies failed") || !strings.Contains(err.Error(), "boom") { + t.Fatalf("want an error naming the attempts and the last stderr, got %v", err) + } + if n := len(invocations(t, logPath)); n != 2 { + t.Fatalf("want 2 runs, got %d", n) + } +} + +func TestRunWithProxyFailoverNoProxyConfigured(t *testing.T) { + bin, logPath := fakeYtdlp(t, "exit 0\n") + + if _, err := RunWithProxyFailover(t.Context(), ytdlp.New().SetExecutable(bin), config.YtdlpConfig{}, "URL"); err != nil { + t.Fatalf("RunWithProxyFailover: %v", err) + } + got := invocations(t, logPath) + if len(got) != 1 || strings.Contains(got[0], "--proxy") { + t.Fatalf("want one direct run, got %q", got) + } +} + +func TestRunWithProxyFailoverSingleFailureKeepsError(t *testing.T) { + bin, _ := fakeYtdlp(t, "echo boom >&2\nexit 1\n") + + _, err := RunWithProxyFailover(t.Context(), ytdlp.New().SetExecutable(bin), config.YtdlpConfig{}, "URL") + if err == nil || strings.Contains(err.Error(), "proxies failed") || !strings.Contains(err.Error(), "boom") { + t.Fatalf("want the plain yt-dlp error, got %v", err) + } +} + +func TestRunWithProxyFailoverStopsOnCancel(t *testing.T) { + bin, logPath := fakeYtdlp(t, "exit 1\n") + ctx, cancel := context.WithCancel(t.Context()) + cancel() + cfg := config.YtdlpConfig{Proxies: []string{"http://a:1", "http://b:2", "http://c:3"}} + + if _, err := RunWithProxyFailover(ctx, ytdlp.New().SetExecutable(bin), cfg, "URL"); err == nil { + t.Fatal("want an error from a cancelled context") + } + if n := len(invocations(t, logPath)); n > 1 { + t.Fatalf("cancelled run kept trying proxies: %d runs", n) + } +} + +// yt-dlp echoes the proxy URL in some connection errors; the password must not +// reach task errors, which are stored and sent to webhooks. +func TestRunWithProxyFailoverRedactsCredentialsInErrors(t *testing.T) { + bin, _ := fakeYtdlp(t, "echo \"ERROR: Unable to connect to proxy: $*\" >&2\nexit 1\n") + cfg := config.YtdlpConfig{Proxies: []string{"socks5://user:s3cret@host:1080"}} + + _, err := RunWithProxyFailover(t.Context(), ytdlp.New().SetExecutable(bin), cfg, "URL") + if err == nil { + t.Fatal("want an error") + } + if strings.Contains(err.Error(), "s3cret") { + t.Fatalf("error leaked the proxy password: %v", err) + } +} + +func TestRedactProxy(t *testing.T) { + cases := []struct{ in, want string }{ + {"", "direct"}, + {"http://host:8080", "http://host:8080"}, + {"socks5://user:s3cret@host:1080", "socks5://user:xxxxx@host:1080"}, + {"http://user@host:8080", "http://user@host:8080"}, + {"not a url at all", ""}, + } + for _, tc := range cases { + if got := redactProxy(tc.in); got != tc.want { + t.Errorf("redactProxy(%q) = %q, want %q", tc.in, got, tc.want) + } + } +} + +func TestCollectDownloadedFilesSkipsTempFiles(t *testing.T) { + dir := t.TempDir() + for _, name := range []string{"video.mp4", "video.mp4.part", "video.info.ytdl", "video.temp.mp4.temp", "partial.mp3"} { + if err := os.WriteFile(filepath.Join(dir, name), nil, 0o600); err != nil { + t.Fatal(err) + } + } + got, err := collectDownloadedFiles(dir) + if err != nil { + t.Fatal(err) + } + want := []string{"partial.mp3", "video.mp4"} + if !reflect.DeepEqual(got, want) { + t.Fatalf("collectDownloadedFiles() = %q, want %q", got, want) + } +} diff --git a/docs/content/en/usage/ytdlp.md b/docs/content/en/usage/ytdlp.md index faee2a08..d709725d 100644 --- a/docs/content/en/usage/ytdlp.md +++ b/docs/content/en/usage/ytdlp.md @@ -38,7 +38,7 @@ Common parameters: - `--write-sub`: Download subtitles - `--write-thumbnail`: Download thumbnail -For safety, flags that execute programs, load external configuration, or read and write arbitrary files are rejected: `--exec`, `--exec-before-download`, `--config-location`, `--load-info-json`, `--batch-file`, `--downloader`, `--external-downloader`, `--paths`, `--cache-dir`, `--postprocessor-args`, `--netrc-cmd`, `--plugin-dirs`, `--update` and similar. +For safety, flags that execute programs, load external configuration, or read and write arbitrary files are rejected: `--exec`, `--exec-before-download`, `--config-location`, `--load-info-json`, `--batch-file`, `--downloader`, `--external-downloader`, `--paths`, `--cache-dir`, `--postprocessor-args`, `--netrc-cmd`, `--plugin-dirs`, `--update` and similar. `--proxy`, `--cookies` and `--netrc` are rejected too; set proxies in the settings instead (see below). ## File names @@ -50,4 +50,12 @@ Downloaded files are named after the video title (`%(title)s.%(ext)s`) and saved Only the template part of `-o/--output` is used: the download directory is always managed by the bot, and any relative path in the template is created below it and kept in the storage path. To change the default for every download, set `filename_template` in the [`[ytdlp]` config](../deployment/configuration); `restrict_filenames` there keeps file names ASCII-only, which drops non-latin characters, spaces and `&`. +## Proxies + +Set **Settings → yt-dlp → yt-dlp proxies** to route yt-dlp through HTTP, HTTPS or SOCKS proxies (`http`, `https`, `socks4`, `socks4a`, `socks5`, `socks5h`), one per line. They are separate from the Telegram proxy and the global proxy, which do not reach the yt-dlp process. In a v0 `config.toml`, the same list is `[ytdlp] proxies = [...]` and is imported on first start. + +yt-dlp accepts only one proxy, so the bot rotates for it: proxies are tried in the order listed, each with a full yt-dlp run, and the first that succeeds is used. Blank and duplicate lines are ignored; with no proxies, yt-dlp connects directly. The same list applies to downloads with custom flags and to the media metadata API. + +A URL that fails everywhere, such as a deleted video, costs one run per proxy before the task fails, so list only proxies you expect to work. Cancelling a task stops the rotation. Passwords in proxy URLs are hidden in logs and task errors. + For more parameters, see [yt-dlp documentation](https://github.com/yt-dlp/yt-dlp#usage-and-options). diff --git a/docs/content/zh/usage/ytdlp.md b/docs/content/zh/usage/ytdlp.md index 4ac0afb9..8971bb86 100644 --- a/docs/content/zh/usage/ytdlp.md +++ b/docs/content/zh/usage/ytdlp.md @@ -38,7 +38,7 @@ weight: 7 - `--write-sub`: 下载字幕 - `--write-thumbnail`: 下载缩略图 -出于安全考虑,以下会执行程序、加载外部配置或读写任意文件的参数会被拒绝:`--exec`、`--exec-before-download`、`--config-location`、`--load-info-json`、`--batch-file`、`--downloader`、`--external-downloader`、`--paths`、`--cache-dir`、`--postprocessor-args`、`--netrc-cmd`、`--plugin-dirs`、`--update` 等。 +出于安全考虑,以下会执行程序、加载外部配置或读写任意文件的参数会被拒绝:`--exec`、`--exec-before-download`、`--config-location`、`--load-info-json`、`--batch-file`、`--downloader`、`--external-downloader`、`--paths`、`--cache-dir`、`--postprocessor-args`、`--netrc-cmd`、`--plugin-dirs`、`--update` 等。`--proxy`、`--cookies` 和 `--netrc` 同样会被拒绝;代理请在设置中配置(见下文)。 ## 文件命名 @@ -50,4 +50,12 @@ weight: 7 只使用 `-o/--output` 的模板部分:下载目录始终由 Bot 管理,模板中的相对路径会创建在该目录下,并保留到存储路径中。如需修改所有下载的默认命名,请在 [`[ytdlp]` 配置](../deployment/configuration)中设置 `filename_template`;其中的 `restrict_filenames` 会将文件名限制为 ASCII,去掉非拉丁字符、空格和 `&`。 +## 代理 + +在 **设置 → yt-dlp → yt-dlp 代理** 中填写 HTTP、HTTPS 或 SOCKS 代理(`http`、`https`、`socks4`、`socks4a`、`socks5`、`socks5h`),每行一个。它与 Telegram 代理和全局代理相互独立,后两者不会作用于 yt-dlp 进程。在 v0 的 `config.toml` 中对应 `[ytdlp] proxies = [...]`,首次启动时会被导入。 + +yt-dlp 只接受一个代理,因此由 Bot 负责轮换:按列出的顺序逐个尝试,每个代理完整运行一次 yt-dlp,使用第一个成功的结果。空行和重复项会被忽略;未配置代理时 yt-dlp 直连。该列表同样作用于带自定义参数的下载和媒体元数据 API。 + +对于在所有代理下都失败的链接(如已删除的视频),任务会在每个代理各尝试一次后才失败,因此只列出确实可用的代理。取消任务会立即停止轮换。代理 URL 中的密码会在日志和任务错误中隐藏。 + 更多参数请参考 [yt-dlp 文档](https://github.com/yt-dlp/yt-dlp#usage-and-options). diff --git a/settings/settings.go b/settings/settings.go index c50f1d56..e200e6cb 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -509,6 +509,10 @@ func validateGroup(cfg *config.Config, group string) error { if _, err := log.ParseLevel(strings.TrimSpace(cfg.Log.Level)); err != nil { return fmt.Errorf("invalid log level: %w", err) } + case GroupYtdlp: + if err := cfg.Ytdlp.ValidateProxies(); err != nil { + return err + } case GroupWeb: if _, _, err := net.SplitHostPort(cfg.Web.Listen); err != nil { return fmt.Errorf("invalid listen address: %w", err) diff --git a/web/ui/src/i18n/messages/settings.ts b/web/ui/src/i18n/messages/settings.ts index 0ccd95e4..a6972c4b 100644 --- a/web/ui/src/i18n/messages/settings.ts +++ b/web/ui/src/i18n/messages/settings.ts @@ -79,6 +79,8 @@ export const zhHans = { recode: '转码容器', filenameTemplate: '文件名模板', restrictFilenames: '限制文件名(仅 ASCII)', + proxies: 'yt-dlp 代理', + proxiesHint: '每行一个,按顺序尝试直到成功;留空则直连', }, parser: { pluginEnable: '启用插件', @@ -221,6 +223,8 @@ export const en = { recode: 'Transcode container', filenameTemplate: 'Filename template', restrictFilenames: 'Restrict filenames (ASCII only)', + proxies: 'yt-dlp proxies', + proxiesHint: 'One per line, tried in order until one works; empty connects directly', }, parser: { pluginEnable: 'Enable plugins', diff --git a/web/ui/src/settingsFields.ts b/web/ui/src/settingsFields.ts index ed648369..81c0342c 100644 --- a/web/ui/src/settingsFields.ts +++ b/web/ui/src/settingsFields.ts @@ -199,6 +199,14 @@ export const settingsGroups: SettingsGroupDef[] = [ label: 'settings.ytdlp.restrictFilenames', kind: 'bool', }, + { + path: 'proxies', + label: 'settings.ytdlp.proxies', + kind: 'list', + hint: 'settings.ytdlp.proxiesHint', + placeholder: 'socks5://127.0.0.1:1080', + wide: true, + }, ], }, {