mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-13 03:43:41 +00:00
Compare commits
49
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
44e2272ca4 | ||
|
|
c51c5b284d | ||
|
|
4d65948934 | ||
|
|
c275bc2e46 | ||
|
|
9aae13453a | ||
|
|
d125d4ab75 | ||
|
|
4214765d39 | ||
|
|
47cafb7575 | ||
|
|
de65149b29 | ||
|
|
5462b14705 | ||
|
|
eee39e012f | ||
|
|
12860bb207 | ||
|
|
b86c2c18e7 | ||
|
|
6f20790b17 | ||
|
|
fa622a4063 | ||
|
|
c6619794fc | ||
|
|
87a8f87b70 | ||
|
|
ba84a04a43 | ||
|
|
224b488087 | ||
|
|
7ce4fb7f0e | ||
|
|
8d1ae93b03 | ||
|
|
363153fa33 | ||
|
|
f0dab90e09 | ||
|
|
6e60889ce8 | ||
|
|
88d150b933 | ||
|
|
694b43e3f9 | ||
|
|
fe25ccab1d | ||
|
|
8301b1f9ce | ||
|
|
49aab4ea7c | ||
|
|
5d8267fa56 | ||
|
|
1ab452ced8 | ||
|
|
743f356567 | ||
|
|
b1dd46cc7c | ||
|
|
38750d8505 | ||
|
|
7c1447f659 | ||
|
|
21702d5d46 | ||
|
|
e99ef6c7f4 | ||
|
|
c667f097dc | ||
|
|
46c34f2e92 | ||
|
|
e47a20a1bd | ||
|
|
cee5f3336e | ||
|
|
a760290213 | ||
|
|
a29f4bb42a | ||
|
|
62e8ab3cef | ||
|
|
8fa9a1e902 | ||
|
|
46675f26af | ||
|
|
0d52b840a2 | ||
|
|
091e7b8936 | ||
|
|
ff8f61423e |
No files matched your search
@@ -0,0 +1,2 @@
|
|||||||
|
# Keep upstream plugin sources recognizable and collapsed in GitHub reviews.
|
||||||
|
ui/flutter/third_party/** linguist-vendored whitespace=-blank-at-eol,-blank-at-eof
|
||||||
@@ -106,17 +106,32 @@ jobs:
|
|||||||
if ("${{ matrix.arch }}" -eq "amd64") {
|
if ("${{ matrix.arch }}" -eq "amd64") {
|
||||||
# for amd64
|
# for amd64
|
||||||
$release = "build\windows\x64\runner\Release\"
|
$release = "build\windows\x64\runner\Release\"
|
||||||
$mingw = "C:\Program Files\Git\mingw64\bin"
|
# Use the same toolchain as cgo instead of Git's bundled DLLs.
|
||||||
cp $mingw\libstdc++-6.dll $release
|
$cc = (Get-Command -Name (go env CC) -CommandType Application -ErrorAction Stop | Select-Object -First 1).Source
|
||||||
cp $mingw\libgcc_s_seh-1.dll $release
|
foreach ($dll in @("libstdc++-6.dll", "libgcc_s_seh-1.dll", "libwinpthread-1.dll")) {
|
||||||
|
$runtimePath = [string](& $cc "-print-file-name=$dll")
|
||||||
|
if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($runtimePath)) {
|
||||||
|
throw "Failed to locate $dll using $cc"
|
||||||
|
}
|
||||||
|
$runtimePath = $runtimePath.Trim()
|
||||||
|
# Some MinGW distributions keep runtime DLLs beside gcc.exe.
|
||||||
|
if ($runtimePath -eq $dll) {
|
||||||
|
$runtimePath = Join-Path (Split-Path -Parent $cc) $dll
|
||||||
|
}
|
||||||
|
if (!(Test-Path -LiteralPath $runtimePath -PathType Leaf)) {
|
||||||
|
throw "MinGW runtime $dll was not found by $cc (reported path: $runtimePath)"
|
||||||
|
}
|
||||||
|
Write-Host "Copying MinGW runtime: $runtimePath"
|
||||||
|
Copy-Item -LiteralPath $runtimePath -Destination $release
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
# for ARM64
|
# for ARM64
|
||||||
$release = "build\windows\arm64\runner\Release\"
|
$release = "build\windows\arm64\runner\Release\"
|
||||||
$mingw = "$env:CLANGARM64_ROOT\aarch64-w64-mingw32\bin"
|
$mingw = "$env:CLANGARM64_ROOT\aarch64-w64-mingw32\bin"
|
||||||
cp $mingw\libc++.dll $release
|
cp $mingw\libc++.dll $release
|
||||||
cp $mingw\libunwind.dll $release
|
cp $mingw\libunwind.dll $release
|
||||||
}
|
|
||||||
cp $mingw\libwinpthread-1.dll $release
|
cp $mingw\libwinpthread-1.dll $release
|
||||||
|
}
|
||||||
cp $system\msvcp140.dll $release
|
cp $system\msvcp140.dll $release
|
||||||
cp $system\vcruntime140.dll $release
|
cp $system\vcruntime140.dll $release
|
||||||
cp $system\vcruntime140_1.dll $release
|
cp $system\vcruntime140_1.dll $release
|
||||||
|
|||||||
@@ -69,6 +69,23 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
files: ./coverage.txt
|
files: ./coverage.txt
|
||||||
token: ${{ secrets.CODECOV_UPLOAD_TOKEN }}
|
token: ${{ secrets.CODECOV_UPLOAD_TOKEN }}
|
||||||
|
test-flutter-category:
|
||||||
|
# The settings editor and the backend have to read an extension list the
|
||||||
|
# same way, so both sides run the shared rule table. The Go side is covered
|
||||||
|
# by the test job above, this runs the ui side of it.
|
||||||
|
name: Test shared category rules
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: subosito/flutter-action@v2
|
||||||
|
with:
|
||||||
|
flutter-version: ${{ env.FLUTTER_VERSION }}
|
||||||
|
- name: Run the shared category rule table on the ui side
|
||||||
|
working-directory: ui/flutter
|
||||||
|
run: |
|
||||||
|
flutter pub get
|
||||||
|
flutter gen-l10n
|
||||||
|
flutter test test/api/downloader_config_test.dart
|
||||||
test-webview-integration:
|
test-webview-integration:
|
||||||
name: Test webview integration (${{ matrix.name }})
|
name: Test webview integration (${{ matrix.name }})
|
||||||
strategy:
|
strategy:
|
||||||
@@ -77,6 +94,8 @@ jobs:
|
|||||||
include:
|
include:
|
||||||
- name: Windows
|
- name: Windows
|
||||||
os: windows-latest
|
os: windows-latest
|
||||||
|
- name: Linux
|
||||||
|
os: ubuntu-latest
|
||||||
- name: macOS
|
- name: macOS
|
||||||
os: macos-latest
|
os: macos-latest
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
@@ -89,9 +108,25 @@ jobs:
|
|||||||
if: runner.os == 'macOS'
|
if: runner.os == 'macOS'
|
||||||
with:
|
with:
|
||||||
flutter-version: ${{ env.FLUTTER_VERSION }}
|
flutter-version: ${{ env.FLUTTER_VERSION }}
|
||||||
|
- name: Install Linux WebView dependencies
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
run: |
|
||||||
|
sudo apt-get update -y
|
||||||
|
sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.1-dev xvfb
|
||||||
|
- name: Test webview
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
# Match the full suite's coverage instrumentation and repeat on the same
|
||||||
|
# GTK event loop to catch queued callbacks after engine destruction.
|
||||||
|
run: |
|
||||||
|
PACKAGES=$(go list ./... | awk '!/\/bind\// && (!/\/cmd\// || /\/cmd\/web$/)')
|
||||||
|
go test -c -tags webview -coverpkg=$(echo "$PACKAGES" | paste -sd "," -) -covermode=count -o "$RUNNER_TEMP/goprovider.test" ./internal/webview/goprovider
|
||||||
|
xvfb-run -a "$RUNNER_TEMP/goprovider.test" -test.v -test.count=5 -test.timeout=300s -test.coverprofile="$RUNNER_TEMP/webview-coverage.out"
|
||||||
|
env:
|
||||||
|
CGO_ENABLED: 1
|
||||||
- name: Test webview
|
- name: Test webview
|
||||||
if: runner.os == 'Windows'
|
if: runner.os == 'Windows'
|
||||||
run: go test -tags webview -v -count=1 -timeout 300s ./internal/webview/goprovider/
|
# Repeat close/reopen coverage to catch browser shutdown races.
|
||||||
|
run: go test -tags webview -v -count=3 -timeout 300s ./internal/webview/goprovider/
|
||||||
env:
|
env:
|
||||||
CGO_ENABLED: 1
|
CGO_ENABLED: 1
|
||||||
- name: Test webview
|
- name: Test webview
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ Connect Gopeed to an AI agent and manage downloads with natural language. For ex
|
|||||||
|
|
||||||
Gopeed 2.0.0 is currently in public beta, introducing a redesigned interface, a native communication architecture that connects desktop and mobile clients directly to the Go core through FFI, a more consistent cross-platform experience, improved task management, more flexible API support, and MCP-based AI agent integration. Some features may still be incomplete or unstable, so please try it and report any issues you encounter.
|
Gopeed 2.0.0 is currently in public beta, introducing a redesigned interface, a native communication architecture that connects desktop and mobile clients directly to the Go core through FFI, a more consistent cross-platform experience, improved task management, more flexible API support, and MCP-based AI agent integration. Some features may still be incomplete or unstable, so please try it and report any issues you encounter.
|
||||||
|
|
||||||
- [Download Gopeed 2.0.0 Beta 2](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.2)
|
- [Download Gopeed 2.0.0 Beta 3](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.3)
|
||||||
|
|
||||||
Once the features and stability meet our release standards, we will publish the official Gopeed 2.0.0 release. Beta users will be able to upgrade directly to the final release, while existing stable users will not be automatically moved onto the beta channel.
|
Once the features and stability meet our release standards, we will publish the official Gopeed 2.0.0 release. Beta users will be able to upgrade directly to the final release, while existing stable users will not be automatically moved onto the beta channel.
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -63,7 +63,7 @@ Gopeed を AI Agent と連携すると、自然言語でダウンロードを管
|
|||||||
|
|
||||||
Gopeed 2.0.0 は現在パブリックベータ版です。刷新されたインターフェース、デスクトップおよびモバイルから FFI で Go コアへ直接接続するネイティブ通信アーキテクチャ、より統一されたクロスプラットフォーム体験、強化されたタスク管理、より柔軟な API、MCP による AI Agent 連携機能を導入しています。一部の機能はまだ不完全または不安定な場合がありますので、ぜひお試しいただき、問題があればフィードバックをお寄せください。
|
Gopeed 2.0.0 は現在パブリックベータ版です。刷新されたインターフェース、デスクトップおよびモバイルから FFI で Go コアへ直接接続するネイティブ通信アーキテクチャ、より統一されたクロスプラットフォーム体験、強化されたタスク管理、より柔軟な API、MCP による AI Agent 連携機能を導入しています。一部の機能はまだ不完全または不安定な場合がありますので、ぜひお試しいただき、問題があればフィードバックをお寄せください。
|
||||||
|
|
||||||
- [Gopeed 2.0.0 Beta 2 をダウンロード](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.2)
|
- [Gopeed 2.0.0 Beta 3 をダウンロード](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.3)
|
||||||
|
|
||||||
機能と安定性が正式リリースの基準に達した時点で、Gopeed 2.0.0 正式版を公開します。ベータ版のユーザーは正式版へ直接アップデートできます。既存の安定版ユーザーが自動的にベータチャンネルへ移行することはありません。
|
機能と安定性が正式リリースの基準に達した時点で、Gopeed 2.0.0 正式版を公開します。ベータ版のユーザーは正式版へ直接アップデートできます。既存の安定版ユーザーが自動的にベータチャンネルへ移行することはありません。
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -63,7 +63,7 @@ Sau khi kết nối Gopeed với AI Agent, bạn có thể quản lý tải xu
|
|||||||
|
|
||||||
Gopeed 2.0.0 hiện đang trong giai đoạn beta công khai, với giao diện được thiết kế lại, kiến trúc giao tiếp native kết nối trực tiếp ứng dụng desktop và di động với lõi Go qua FFI, trải nghiệm đa nền tảng nhất quán hơn, khả năng quản lý tác vụ được cải thiện, API linh hoạt hơn và khả năng tích hợp AI Agent qua MCP. Một số tính năng có thể vẫn chưa hoàn thiện hoặc chưa ổn định, vì vậy hãy dùng thử và gửi phản hồi nếu bạn gặp vấn đề.
|
Gopeed 2.0.0 hiện đang trong giai đoạn beta công khai, với giao diện được thiết kế lại, kiến trúc giao tiếp native kết nối trực tiếp ứng dụng desktop và di động với lõi Go qua FFI, trải nghiệm đa nền tảng nhất quán hơn, khả năng quản lý tác vụ được cải thiện, API linh hoạt hơn và khả năng tích hợp AI Agent qua MCP. Một số tính năng có thể vẫn chưa hoàn thiện hoặc chưa ổn định, vì vậy hãy dùng thử và gửi phản hồi nếu bạn gặp vấn đề.
|
||||||
|
|
||||||
- [Tải Gopeed 2.0.0 Beta 2](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.2)
|
- [Tải Gopeed 2.0.0 Beta 3](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.3)
|
||||||
|
|
||||||
Khi các tính năng và độ ổn định đáp ứng tiêu chuẩn phát hành, chúng tôi sẽ phát hành Gopeed 2.0.0 chính thức. Người dùng bản beta có thể nâng cấp trực tiếp lên bản chính thức, trong khi người dùng bản ổn định hiện tại sẽ không tự động được chuyển sang kênh beta.
|
Khi các tính năng và độ ổn định đáp ứng tiêu chuẩn phát hành, chúng tôi sẽ phát hành Gopeed 2.0.0 chính thức. Người dùng bản beta có thể nâng cấp trực tiếp lên bản chính thức, trong khi người dùng bản ổn định hiện tại sẽ không tự động được chuyển sang kênh beta.
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -63,7 +63,7 @@ Gopeed(全称 **Go Speed**,也叫“够快下载器”)是一款使用 Go
|
|||||||
|
|
||||||
Gopeed 2.0.0 目前处于公开 Beta 测试阶段,引入了全新界面、通过 FFI 直连 Go 核心的原生通信架构、更一致的跨平台体验、更完善的任务管理、更灵活的 API,以及用于 AI Agent 接入的 MCP 能力。部分功能仍可能不够完善或稳定,欢迎提前体验并向我们反馈问题。
|
Gopeed 2.0.0 目前处于公开 Beta 测试阶段,引入了全新界面、通过 FFI 直连 Go 核心的原生通信架构、更一致的跨平台体验、更完善的任务管理、更灵活的 API,以及用于 AI Agent 接入的 MCP 能力。部分功能仍可能不够完善或稳定,欢迎提前体验并向我们反馈问题。
|
||||||
|
|
||||||
- [下载 Gopeed 2.0.0 Beta 2](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.2)
|
- [下载 Gopeed 2.0.0 Beta 3](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.3)
|
||||||
|
|
||||||
当功能完整度和稳定性达到正式发布标准后,我们会发布 Gopeed 2.0.0 正式版。已安装 Beta 版本的用户可以直接升级到最终正式版,现有稳定版用户则不会被自动切换到 Beta 渠道。
|
当功能完整度和稳定性达到正式发布标准后,我们会发布 Gopeed 2.0.0 正式版。已安装 Beta 版本的用户可以直接升级到最终正式版,现有稳定版用户则不会被自动切换到 Beta 渠道。
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -63,7 +63,7 @@ Gopeed(**Go Speed** 的縮寫)是一款以 Go 與 Flutter 開發的高速、
|
|||||||
|
|
||||||
Gopeed 2.0.0 目前處於公開 Beta 測試階段,引入了全新介面、透過 FFI 直連 Go 核心的原生通訊架構、更一致的跨平台體驗、更完善的任務管理、更靈活的 API,以及透過 MCP 串接 AI Agent 的能力。部分功能仍可能不夠完善或穩定,歡迎搶先體驗並向我們回報問題。
|
Gopeed 2.0.0 目前處於公開 Beta 測試階段,引入了全新介面、透過 FFI 直連 Go 核心的原生通訊架構、更一致的跨平台體驗、更完善的任務管理、更靈活的 API,以及透過 MCP 串接 AI Agent 的能力。部分功能仍可能不夠完善或穩定,歡迎搶先體驗並向我們回報問題。
|
||||||
|
|
||||||
- [下載 Gopeed 2.0.0 Beta 2](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.2)
|
- [下載 Gopeed 2.0.0 Beta 3](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.3)
|
||||||
|
|
||||||
當功能完整度與穩定性達到正式發布標準後,我們會發布 Gopeed 2.0.0 正式版。已安裝 Beta 版本的使用者可以直接升級至最終正式版,現有穩定版使用者則不會被自動切換至 Beta 頻道。
|
當功能完整度與穩定性達到正式發布標準後,我們會發布 Gopeed 2.0.0 正式版。已安裝 Beta 版本的使用者可以直接升級至最終正式版,現有穩定版使用者則不會被自動切換至 Beta 頻道。
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 391 KiB After Width: | Height: | Size: 1.3 MiB |
Binary file not shown.
|
Before Width: | Height: | Size: 357 KiB After Width: | Height: | Size: 1.3 MiB |
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
nativebridge "github.com/GopeedLab/gopeed/bind/native"
|
nativebridge "github.com/GopeedLab/gopeed/bind/native"
|
||||||
"github.com/GopeedLab/gopeed/internal/webview/rpcprovider"
|
"github.com/GopeedLab/gopeed/internal/webview/rpcprovider"
|
||||||
|
goapi "github.com/GopeedLab/gopeed/pkg/api"
|
||||||
"github.com/GopeedLab/gopeed/pkg/rest"
|
"github.com/GopeedLab/gopeed/pkg/rest"
|
||||||
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||||
)
|
)
|
||||||
@@ -90,3 +91,14 @@ func applyWebViewProvider(config *model.StartConfig) {
|
|||||||
}
|
}
|
||||||
config.WebViewProvider = rpcprovider.New(*config.WebViewRPCConfig)
|
config.WebViewProvider = rpcprovider.New(*config.WebViewRPCConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LiveActivityTaskEventMask() int64 {
|
||||||
|
return int64(
|
||||||
|
goapi.TaskEventDone |
|
||||||
|
goapi.TaskEventError |
|
||||||
|
goapi.TaskEventStart |
|
||||||
|
goapi.TaskEventProgress |
|
||||||
|
goapi.TaskEventPause |
|
||||||
|
goapi.TaskEventDelete,
|
||||||
|
)
|
||||||
|
}
|
||||||
+2
-2
@@ -166,7 +166,7 @@ func runResolve(args []string) error {
|
|||||||
result, err := d.Resolve(&base.Request{
|
result, err := d.Resolve(&base.Request{
|
||||||
URL: url,
|
URL: url,
|
||||||
Labels: map[string]string(labels),
|
Labels: map[string]string(labels),
|
||||||
}, nil)
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -250,7 +250,7 @@ func runDownload(args []string) error {
|
|||||||
result, err := d.Resolve(&base.Request{
|
result, err := d.Resolve(&base.Request{
|
||||||
URL: url,
|
URL: url,
|
||||||
Labels: map[string]string(labels),
|
Labels: map[string]string(labels),
|
||||||
}, nil)
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ type args struct {
|
|||||||
url string
|
url string
|
||||||
connections *int
|
connections *int
|
||||||
dir *string
|
dir *string
|
||||||
|
checksum *string
|
||||||
|
checksumAlgo *string
|
||||||
}
|
}
|
||||||
|
|
||||||
func parse() *args {
|
func parse() *args {
|
||||||
@@ -20,6 +22,8 @@ func parse() *args {
|
|||||||
var args args
|
var args args
|
||||||
args.connections = flag.Int("C", 16, "Concurrent connections.")
|
args.connections = flag.Int("C", 16, "Concurrent connections.")
|
||||||
args.dir = flag.String("D", dir, "Store directory.")
|
args.dir = flag.String("D", dir, "Store directory.")
|
||||||
|
args.checksum = flag.String("checksum", "", "Expected checksum hash (hex encoded).")
|
||||||
|
args.checksumAlgo = flag.String("checksum-algo", "md5", "Checksum algorithm: md5, sha1, or sha256.")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
t := flag.Args()
|
t := flag.Args()
|
||||||
if len(t) > 0 {
|
if len(t) > 0 {
|
||||||
|
|||||||
+16
-1
@@ -19,6 +19,18 @@ func main() {
|
|||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
var checksumOpt *http.ChecksumOption
|
||||||
|
if args.checksum != nil && *args.checksum != "" {
|
||||||
|
algo := "md5"
|
||||||
|
if args.checksumAlgo != nil && *args.checksumAlgo != "" {
|
||||||
|
algo = *args.checksumAlgo
|
||||||
|
}
|
||||||
|
checksumOpt = &http.ChecksumOption{
|
||||||
|
Algorithm: algo,
|
||||||
|
Expected: *args.checksum,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_, err := download.Boot().
|
_, err := download.Boot().
|
||||||
URL(args.url).
|
URL(args.url).
|
||||||
Listener(func(event *download.Event) {
|
Listener(func(event *download.Event) {
|
||||||
@@ -44,7 +56,10 @@ func main() {
|
|||||||
}).
|
}).
|
||||||
Create(&base.Options{
|
Create(&base.Options{
|
||||||
Path: *args.dir,
|
Path: *args.dir,
|
||||||
Extra: http.OptsExtra{Connections: *args.connections},
|
Extra: http.OptsExtra{
|
||||||
|
Connections: *args.connections,
|
||||||
|
Checksum: checksumOpt,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|||||||
+7
-1
@@ -26,7 +26,7 @@ func Start(cfg *model.StartConfig) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if downloadCfg.FirstLoad {
|
if rest.Downloader.IsFirstLoad() {
|
||||||
// Set default download config
|
// Set default download config
|
||||||
if cfg.DownloadConfig != nil {
|
if cfg.DownloadConfig != nil {
|
||||||
cfg.DownloadConfig.Merge(downloadCfg)
|
cfg.DownloadConfig.Merge(downloadCfg)
|
||||||
@@ -51,6 +51,12 @@ func Start(cfg *model.StartConfig) {
|
|||||||
rest.Downloader.PutConfig(downloadCfg)
|
rest.Downloader.PutConfig(downloadCfg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The default download dir is resolved only after the core starts, so
|
||||||
|
// seed the built-in categories now that the dir is known. Init is
|
||||||
|
// idempotent and leaves an existing category list untouched.
|
||||||
|
downloadCfg.Init()
|
||||||
|
rest.Downloader.PutConfig(downloadCfg)
|
||||||
}
|
}
|
||||||
watchExit()
|
watchExit()
|
||||||
|
|
||||||
|
|||||||
@@ -37,15 +37,17 @@ func installByInstaller(killSignalChan chan<- any, packagePath, destDir string)
|
|||||||
}
|
}
|
||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
|
|
||||||
// Find the installer file
|
|
||||||
var installerPath string
|
var installerPath string
|
||||||
for _, file := range reader.File {
|
for _, file := range reader.File {
|
||||||
|
cleanName := filepath.Clean(file.Name)
|
||||||
|
if strings.HasPrefix(cleanName, "..") || filepath.IsAbs(cleanName) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if file.FileInfo().IsDir() {
|
if file.FileInfo().IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract file
|
path := filepath.Join(tempDir, cleanName)
|
||||||
path := filepath.Join(tempDir, file.Name)
|
|
||||||
|
|
||||||
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
|
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -101,7 +103,11 @@ func installByPortable(killSignalChan chan<- any, packagePath, destDir string) e
|
|||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
|
|
||||||
for _, file := range reader.File {
|
for _, file := range reader.File {
|
||||||
path := filepath.Join(destDir, file.Name)
|
cleanName := filepath.Clean(file.Name)
|
||||||
|
if strings.HasPrefix(cleanName, "..") || filepath.IsAbs(cleanName) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
path := filepath.Join(destDir, cleanName)
|
||||||
|
|
||||||
if file.FileInfo().IsDir() {
|
if file.FileInfo().IsDir() {
|
||||||
os.MkdirAll(path, file.Mode())
|
os.MkdirAll(path, file.Mode())
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type args struct {
|
type args struct {
|
||||||
|
TempDir *string `json:"tempDir"`
|
||||||
Address *string `json:"address"`
|
Address *string `json:"address"`
|
||||||
Port *int `json:"port"`
|
Port *int `json:"port"`
|
||||||
Username *string `json:"username"`
|
Username *string `json:"username"`
|
||||||
@@ -54,6 +55,7 @@ func loadCliArgs() *args {
|
|||||||
cfg.ApiToken = flag.String("T", "", "API token, it must be configured when using HTTP API in the case of enabling web authentication")
|
cfg.ApiToken = flag.String("T", "", "API token, it must be configured when using HTTP API in the case of enabling web authentication")
|
||||||
flag.StringVar(cfg.ApiToken, "api-token", "", "API token, it must be configured when using HTTP API in the case of enabling web authentication")
|
flag.StringVar(cfg.ApiToken, "api-token", "", "API token, it must be configured when using HTTP API in the case of enabling web authentication")
|
||||||
flag.BoolVar(&cfg.MCPEnable, "mcp-enable", false, "Enable MCP endpoint")
|
flag.BoolVar(&cfg.MCPEnable, "mcp-enable", false, "Enable MCP endpoint")
|
||||||
|
cfg.TempDir = flag.String("temp-dir", "", "Temporary directory (defaults to the OS temporary directory)")
|
||||||
cfg.StorageDir = flag.String("d", "", "Storage directory")
|
cfg.StorageDir = flag.String("d", "", "Storage directory")
|
||||||
flag.StringVar(cfg.StorageDir, "storage-dir", "", "Storage directory")
|
flag.StringVar(cfg.StorageDir, "storage-dir", "", "Storage directory")
|
||||||
whiteDownloadDirs := flag.String("white-download-dirs", "", "White download directories, comma-separated")
|
whiteDownloadDirs := flag.String("white-download-dirs", "", "White download directories, comma-separated")
|
||||||
@@ -89,6 +91,8 @@ func overrideWithCliArgs(cfg *args, cliConfig *args) {
|
|||||||
cfg.ApiToken = cliConfig.ApiToken
|
cfg.ApiToken = cliConfig.ApiToken
|
||||||
case "mcp-enable":
|
case "mcp-enable":
|
||||||
cfg.MCPEnable = cliConfig.MCPEnable
|
cfg.MCPEnable = cliConfig.MCPEnable
|
||||||
|
case "temp-dir":
|
||||||
|
cfg.TempDir = cliConfig.TempDir
|
||||||
case "d", "storage-dir":
|
case "d", "storage-dir":
|
||||||
cfg.StorageDir = cliConfig.StorageDir
|
cfg.StorageDir = cliConfig.StorageDir
|
||||||
case "white-download-dirs":
|
case "white-download-dirs":
|
||||||
@@ -101,6 +105,9 @@ func overrideWithCliArgs(cfg *args, cliConfig *args) {
|
|||||||
|
|
||||||
// setDefaults sets default values for any unset configuration fields
|
// setDefaults sets default values for any unset configuration fields
|
||||||
func setDefaults(cfg *args, cliConfig *args) {
|
func setDefaults(cfg *args, cliConfig *args) {
|
||||||
|
if cfg.TempDir == nil {
|
||||||
|
cfg.TempDir = cliConfig.TempDir
|
||||||
|
}
|
||||||
if cfg.Address == nil {
|
if cfg.Address == nil {
|
||||||
cfg.Address = cliConfig.Address
|
cfg.Address = cliConfig.Address
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,11 +43,16 @@ func main() {
|
|||||||
storageDir = filepath.Join(filepath.Dir(exe), "storage")
|
storageDir = filepath.Join(filepath.Dir(exe), "storage")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tempDir := ""
|
||||||
|
if args.TempDir != nil {
|
||||||
|
tempDir = *args.TempDir
|
||||||
|
}
|
||||||
cfg := &model.StartConfig{
|
cfg := &model.StartConfig{
|
||||||
Network: "tcp",
|
Network: "tcp",
|
||||||
Address: fmt.Sprintf("%s:%d", *args.Address, *args.Port),
|
Address: fmt.Sprintf("%s:%d", *args.Address, *args.Port),
|
||||||
Storage: model.StorageBolt,
|
Storage: model.StorageBolt,
|
||||||
StorageDir: storageDir,
|
StorageDir: storageDir,
|
||||||
|
TempDir: tempDir,
|
||||||
WhiteDownloadDirs: args.WhiteDownloadDirs,
|
WhiteDownloadDirs: args.WhiteDownloadDirs,
|
||||||
ApiToken: *args.ApiToken,
|
ApiToken: *args.ApiToken,
|
||||||
MCPEnable: args.MCPEnable,
|
MCPEnable: args.MCPEnable,
|
||||||
|
|||||||
@@ -3,13 +3,15 @@ module github.com/GopeedLab/gopeed
|
|||||||
go 1.25.4
|
go 1.25.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/GopeedLab/webview_go v0.0.0-20260423085439-7a2f88b6e9b5
|
codeberg.org/gruf/go-ffmpreg v0.6.23-0.20260827095416-df1836cb5be0
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260918010912-0d4c27bb7914
|
||||||
github.com/anacrolix/torrent v1.60.1-0.20251217073903-486bcbe758e0
|
github.com/anacrolix/torrent v1.60.1-0.20251217073903-486bcbe758e0
|
||||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
|
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
|
||||||
github.com/bodgit/sevenzip v1.6.1
|
github.com/bodgit/sevenzip v1.6.1
|
||||||
github.com/dop251/goja v0.0.0-20260106131823-651366fbe6e3
|
github.com/dop251/goja v0.0.0-20260106131823-651366fbe6e3
|
||||||
github.com/dop251/goja_nodejs v0.0.0-20240728170619-29b559befffc
|
github.com/dop251/goja_nodejs v0.0.0-20240728170619-29b559befffc
|
||||||
github.com/go-git/go-git/v5 v5.8.1
|
github.com/go-git/go-git/v5 v5.8.1
|
||||||
|
github.com/google/jsonschema-go v0.4.3
|
||||||
github.com/gorilla/handlers v1.5.1
|
github.com/gorilla/handlers v1.5.1
|
||||||
github.com/gorilla/mux v1.8.0
|
github.com/gorilla/mux v1.8.0
|
||||||
github.com/imroc/req/v3 v3.52.2
|
github.com/imroc/req/v3 v3.52.2
|
||||||
@@ -21,6 +23,7 @@ require (
|
|||||||
github.com/pkg/errors v0.9.1
|
github.com/pkg/errors v0.9.1
|
||||||
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529
|
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529
|
||||||
github.com/rs/zerolog v1.31.0
|
github.com/rs/zerolog v1.31.0
|
||||||
|
github.com/tetratelabs/wazero v1.12.0
|
||||||
github.com/xiaoqidun/setft v0.0.0-20220310121541-be86327699ad
|
github.com/xiaoqidun/setft v0.0.0-20220310121541-be86327699ad
|
||||||
go.etcd.io/bbolt v1.4.3
|
go.etcd.io/bbolt v1.4.3
|
||||||
golang.org/x/exp v0.0.0-20251219203646-944ab1f22d93
|
golang.org/x/exp v0.0.0-20251219203646-944ab1f22d93
|
||||||
@@ -38,7 +41,6 @@ require (
|
|||||||
github.com/felixge/fgprof v0.9.5 // indirect
|
github.com/felixge/fgprof v0.9.5 // indirect
|
||||||
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
|
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
|
||||||
github.com/google/go-cmp v0.7.0 // indirect
|
github.com/google/go-cmp v0.7.0 // indirect
|
||||||
github.com/google/jsonschema-go v0.4.3 // indirect
|
|
||||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||||
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
|
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
|
||||||
@@ -64,6 +66,7 @@ require (
|
|||||||
go.uber.org/automaxprocs v1.6.0 // indirect
|
go.uber.org/automaxprocs v1.6.0 // indirect
|
||||||
go.uber.org/mock v0.5.1 // indirect
|
go.uber.org/mock v0.5.1 // indirect
|
||||||
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
|
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
|
||||||
|
golang.org/x/mod v0.33.0
|
||||||
golang.org/x/oauth2 v0.35.0 // indirect
|
golang.org/x/oauth2 v0.35.0 // indirect
|
||||||
golang.org/x/sync v0.20.0 // indirect
|
golang.org/x/sync v0.20.0 // indirect
|
||||||
)
|
)
|
||||||
@@ -154,9 +157,8 @@ require (
|
|||||||
go.opentelemetry.io/otel/metric v1.39.0 // indirect
|
go.opentelemetry.io/otel/metric v1.39.0 // indirect
|
||||||
go.opentelemetry.io/otel/trace v1.39.0 // indirect
|
go.opentelemetry.io/otel/trace v1.39.0 // indirect
|
||||||
golang.org/x/crypto v0.48.0 // indirect
|
golang.org/x/crypto v0.48.0 // indirect
|
||||||
golang.org/x/mod v0.33.0 // indirect
|
|
||||||
golang.org/x/net v0.50.0
|
golang.org/x/net v0.50.0
|
||||||
golang.org/x/sys v0.41.0 // indirect
|
golang.org/x/sys v0.44.0 // indirect
|
||||||
golang.org/x/text v0.34.0
|
golang.org/x/text v0.34.0
|
||||||
golang.org/x/time v0.15.0 // indirect
|
golang.org/x/time v0.15.0 // indirect
|
||||||
golang.org/x/tools v0.42.0 // indirect
|
golang.org/x/tools v0.42.0 // indirect
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2k
|
|||||||
cloud.google.com/go/pubsub v1.1.0/go.mod h1:EwwdRX2sKPjnvnqCa270oGRyludottCI76h+R3AArQw=
|
cloud.google.com/go/pubsub v1.1.0/go.mod h1:EwwdRX2sKPjnvnqCa270oGRyludottCI76h+R3AArQw=
|
||||||
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
|
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
|
||||||
cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos=
|
cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos=
|
||||||
|
codeberg.org/gruf/go-ffmpreg v0.6.23-0.20260827095416-df1836cb5be0 h1:AqNFl60o4xhTTOxmzAvSf7pazXHKBGuV5ATw7sETIRA=
|
||||||
|
codeberg.org/gruf/go-ffmpreg v0.6.23-0.20260827095416-df1836cb5be0/go.mod h1:XM6eW8CSgAuZRKRynm5nDiSNedm0rav30IychqygOHE=
|
||||||
crawshaw.io/iox v0.0.0-20181124134642-c51c3df30797/go.mod h1:sXBiorCo8c46JlQV3oXPKINnZ8mcqnye1EkVkqsectk=
|
crawshaw.io/iox v0.0.0-20181124134642-c51c3df30797/go.mod h1:sXBiorCo8c46JlQV3oXPKINnZ8mcqnye1EkVkqsectk=
|
||||||
crawshaw.io/sqlite v0.3.2/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4=
|
crawshaw.io/sqlite v0.3.2/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4=
|
||||||
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
|
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
|
||||||
@@ -23,8 +25,20 @@ filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmG
|
|||||||
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
|
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||||
github.com/GopeedLab/webview_go v0.0.0-20260423085439-7a2f88b6e9b5 h1:86LVTaNDM/j/uTAD2HedkXNO9LIQlt642VAdbU/0D/M=
|
github.com/GopeedLab/webview_go v0.0.0-20260915010614-2567e492369a h1:Vzz+8aMVX9ZPPpkxgxa7mCorrAgdXCZL5EunOpOBvao=
|
||||||
github.com/GopeedLab/webview_go v0.0.0-20260423085439-7a2f88b6e9b5/go.mod h1:FvV19lu9rQ9NcbczN5y8pbWecURRRbWa8tbfFARBDa0=
|
github.com/GopeedLab/webview_go v0.0.0-20260915010614-2567e492369a/go.mod h1:FvV19lu9rQ9NcbczN5y8pbWecURRRbWa8tbfFARBDa0=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260917153919-dcf00737f32a h1:nz1bC6TGwQ1kv/OVLahNZ0EEtb7v5LHfceAkW4MvS6I=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260917153919-dcf00737f32a/go.mod h1:FvV19lu9rQ9NcbczN5y8pbWecURRRbWa8tbfFARBDa0=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260917155039-1ee7ffb4169a h1:yudcfXpipKLnMaaw1losn6YeOBjvj2rAFicfjqYJexY=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260917155039-1ee7ffb4169a/go.mod h1:FvV19lu9rQ9NcbczN5y8pbWecURRRbWa8tbfFARBDa0=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260917155218-bd16da0d6f53 h1:nMSHKhbrEaWPE/x3mYl/270GPjswbUmS+E7yYjly4xk=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260917155218-bd16da0d6f53/go.mod h1:oNO07SCX8xgoHLds2OBwML8TU4xils88vMQ4gadKUHQ=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260917161756-2a4f989dcb1d h1:4fjlhJpBWa6IcdKVPkEMMIDd6Xo9JM09dJHdKU+1V7k=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260917161756-2a4f989dcb1d/go.mod h1:oNO07SCX8xgoHLds2OBwML8TU4xils88vMQ4gadKUHQ=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260917163929-ca26c3e6bcfa h1:VkdBMcNHYTqCMwOOCimA7b3xZKV/qFIGrhh3+vm9n3s=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260917163929-ca26c3e6bcfa/go.mod h1:oNO07SCX8xgoHLds2OBwML8TU4xils88vMQ4gadKUHQ=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260918010912-0d4c27bb7914 h1:9VJQZIZ1R6T4m+4vlNQYNXSPNUv2gf2PiTfIXiQ3MTw=
|
||||||
|
github.com/GopeedLab/webview_go v0.0.0-20260918010912-0d4c27bb7914/go.mod h1:oNO07SCX8xgoHLds2OBwML8TU4xils88vMQ4gadKUHQ=
|
||||||
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
|
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
|
||||||
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
|
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
|
||||||
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
|
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
|
||||||
@@ -552,6 +566,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
|
|||||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
|
github.com/tetratelabs/wazero v1.12.0 h1:DuWcpNu/FzgEXgGBDp8J1Spc+CWOvvtvVyjKlaZopYU=
|
||||||
|
github.com/tetratelabs/wazero v1.12.0/go.mod h1:LvKtzl2RqO4gyF27BiXU+nKAjcV8f38U+kP/q2vgxh0=
|
||||||
github.com/tidwall/btree v1.8.1 h1:27ehoXvm5AG/g+1VxLS1SD3vRhp/H7LuEfwNvddEdmA=
|
github.com/tidwall/btree v1.8.1 h1:27ehoXvm5AG/g+1VxLS1SD3vRhp/H7LuEfwNvddEdmA=
|
||||||
github.com/tidwall/btree v1.8.1/go.mod h1:jBbTdUWhSZClZWoDg54VnvV7/54modSOzDN7VXftj1A=
|
github.com/tidwall/btree v1.8.1/go.mod h1:jBbTdUWhSZClZWoDg54VnvV7/54modSOzDN7VXftj1A=
|
||||||
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
||||||
@@ -732,6 +748,8 @@ golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|||||||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||||
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
|
golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
|
||||||
|
golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/GopeedLab/gopeed/internal/production"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -61,6 +62,7 @@ type Metadata struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Source struct {
|
type Source struct {
|
||||||
|
producer production.Reader
|
||||||
ID string
|
ID string
|
||||||
URL string
|
URL string
|
||||||
ContentType string
|
ContentType string
|
||||||
@@ -359,11 +361,35 @@ func (r *Registry) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
|
if producer, ok := reader.(production.Reader); ok {
|
||||||
|
src.mu.Lock()
|
||||||
|
src.producer = producer
|
||||||
|
src.mu.Unlock()
|
||||||
|
if err := producer.WaitProduction(req.Context()); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if producer.Production() != nil {
|
||||||
|
meta.Size = 0
|
||||||
|
meta.Range = false
|
||||||
|
start = 0
|
||||||
|
end = -1
|
||||||
|
ranged = false
|
||||||
|
src.mu.Lock()
|
||||||
|
src.size = 0
|
||||||
|
src.rangeEnabled = false
|
||||||
|
src.mu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
writeHeaders(w, meta, start, end, ranged)
|
writeHeaders(w, meta, start, end, ranged)
|
||||||
if ranged {
|
if ranged {
|
||||||
w.WriteHeader(http.StatusPartialContent)
|
w.WriteHeader(http.StatusPartialContent)
|
||||||
}
|
}
|
||||||
|
if producer, ok := reader.(production.Reader); ok && producer.Production() != nil {
|
||||||
|
if flusher, ok := w.(http.Flusher); ok {
|
||||||
|
flusher.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
limit := int64(-1)
|
limit := int64(-1)
|
||||||
if end >= start {
|
if end >= start {
|
||||||
limit = end - start + 1
|
limit = end - start + 1
|
||||||
@@ -675,3 +701,21 @@ func randomID(size int) (string, error) {
|
|||||||
}
|
}
|
||||||
return hex.EncodeToString(buf), nil
|
return hex.EncodeToString(buf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Production returns the active opener's input counter, if it generates output.
|
||||||
|
func (r *Registry) Production(raw string) production.Source {
|
||||||
|
if r == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
src, err := r.get(raw)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
src.mu.Lock()
|
||||||
|
reader := src.producer
|
||||||
|
src.mu.Unlock()
|
||||||
|
if reader == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return reader.Production()
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/GopeedLab/gopeed/internal/tempfiles"
|
||||||
"github.com/GopeedLab/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -9,6 +10,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
|
TempFiles *tempfiles.Scope
|
||||||
|
ManagedProduction func(string) bool
|
||||||
|
TempDir string
|
||||||
GetConfig func(v any)
|
GetConfig func(v any)
|
||||||
GetProxy func(requestProxy *base.RequestProxy) func(*http.Request) (*url.URL, error)
|
GetProxy func(requestProxy *base.RequestProxy) func(*http.Request) (*url.URL, error)
|
||||||
FileController
|
FileController
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
// Each download task will have a corresponding Fetcher instance for the management of the download task
|
// Each download task will have a corresponding Fetcher instance for the management of the download task
|
||||||
type Fetcher interface {
|
type Fetcher interface {
|
||||||
Setup(ctl *controller.Controller)
|
Setup(ctl *controller.Controller)
|
||||||
Resolve(req *base.Request, opts *base.Options) error
|
Resolve(req *base.Request) error
|
||||||
Start() error
|
Start() error
|
||||||
// Patch modifies task-specific data based on the protocol.
|
// Patch modifies task-specific data based on the protocol.
|
||||||
// For HTTP: can modify Request info (URL, headers, etc.)
|
// For HTTP: can modify Request info (URL, headers, etc.)
|
||||||
@@ -106,7 +106,13 @@ func (s *SchemeFilter) Match(uri string) bool {
|
|||||||
case FilterTypeUrl:
|
case FilterTypeUrl:
|
||||||
return strings.HasPrefix(uriUpper, patternUpper+":")
|
return strings.HasPrefix(uriUpper, patternUpper+":")
|
||||||
case FilterTypeFile:
|
case FilterTypeFile:
|
||||||
return strings.HasSuffix(uriUpper, "."+patternUpper)
|
// Strip query and fragment before matching so URLs like
|
||||||
|
// https://example.com/video.m3u8?sign=abc still route by extension.
|
||||||
|
clean := uri
|
||||||
|
if idx := strings.IndexAny(clean, "?#"); idx >= 0 {
|
||||||
|
clean = clean[:idx]
|
||||||
|
}
|
||||||
|
return strings.HasSuffix(strings.ToUpper(clean), "."+patternUpper)
|
||||||
case FilterTypeBase64:
|
case FilterTypeBase64:
|
||||||
return strings.HasPrefix(uriUpper, "DATA:"+patternUpper+";BASE64,")
|
return strings.HasPrefix(uriUpper, "DATA:"+patternUpper+";BASE64,")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package fetcher
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TriggerSource string
|
||||||
|
|
||||||
|
const (
|
||||||
|
TriggerScheduled TriggerSource = "scheduled"
|
||||||
|
TriggerManual TriggerSource = "manual"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrJobSkipped = errors.New("protocol job skipped")
|
||||||
|
var ErrConfigChanged = errors.New("protocol configuration changed during execution")
|
||||||
|
|
||||||
|
// ConfigState embeds the common backend-owned state section in a protocol config.
|
||||||
|
// Public config PUTs always retain this section from the latest stored config.
|
||||||
|
type ConfigState[T any] struct {
|
||||||
|
State T `json:"state"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProtocolHost is scoped to one manager. UpdateState runs against the latest
|
||||||
|
// config under Downloader's lock; callbacks must not perform network I/O.
|
||||||
|
type ProtocolHost struct {
|
||||||
|
Read func(any) error
|
||||||
|
UpdateState func(func(config, state json.RawMessage) (any, error)) error
|
||||||
|
Fetch func(context.Context, string, int64, func([]byte) error) ([]byte, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadConfig[T any](host ProtocolHost) (*T, error) {
|
||||||
|
var cfg T
|
||||||
|
if err := host.Read(&cfg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateState gives the callback a detached config snapshot and mutable state.
|
||||||
|
// Only the state is committed; edits to the config snapshot are discarded.
|
||||||
|
func UpdateState[C, S any](host ProtocolHost, update func(*C, *S) error) error {
|
||||||
|
return host.UpdateState(func(rawConfig, rawState json.RawMessage) (any, error) {
|
||||||
|
var cfg C
|
||||||
|
var state S
|
||||||
|
if err := json.Unmarshal(rawConfig, &cfg); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(rawState, &state); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := update(&cfg, &state); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &state, nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
type CronJob struct {
|
||||||
|
Name string
|
||||||
|
Every time.Duration
|
||||||
|
Timeout time.Duration
|
||||||
|
Run func(context.Context, TriggerSource) error
|
||||||
|
}
|
||||||
|
type CronProvider interface{ CronJobs(ProtocolHost) []CronJob }
|
||||||
|
|
||||||
|
// ConfigNormalizer is optional. It owns protocol validation and derived fields.
|
||||||
|
type ConfigNormalizer interface {
|
||||||
|
NormalizeConfig(json.RawMessage) (any, error)
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
# GitHub mirror execution
|
||||||
|
|
||||||
|
This package owns GitHub URL rewriting and execution strategies. It depends only
|
||||||
|
on the standard library and does not depend on Downloader, HTTP clients, Git, or
|
||||||
|
storage. Downloader persists `Config` at its top-level `githubMirror` field;
|
||||||
|
`pkg/base` preserves the existing configuration names through aliases.
|
||||||
|
|
||||||
|
## Race: resource reads
|
||||||
|
|
||||||
|
```go
|
||||||
|
body, err := githubmirror.Race(ctx, cfg.GithubMirror, rawURL,
|
||||||
|
githubmirror.Options[[]byte]{
|
||||||
|
Kind: githubmirror.Resource,
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
},
|
||||||
|
func(ctx context.Context, candidate string) ([]byte, error) {
|
||||||
|
return fetchAndValidate(ctx, client, candidate)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
The callback owns HTTP requests, size limits, and content validation. The first
|
||||||
|
completed valid result wins. Other operations are canceled and drained. There is
|
||||||
|
no HEAD-probe assumption or client-specific HTTP behavior inside the executor.
|
||||||
|
|
||||||
|
## Fallback: repository operations
|
||||||
|
|
||||||
|
```go
|
||||||
|
repo, err := githubmirror.Fallback(ctx, cfg.GithubMirror, officialURL,
|
||||||
|
githubmirror.Options[*git.Repository]{
|
||||||
|
Kind: githubmirror.Repository,
|
||||||
|
},
|
||||||
|
func(ctx context.Context, candidate string) (*git.Repository, error) {
|
||||||
|
// Use a clean temporary checkout for each attempt.
|
||||||
|
return clone(ctx, candidate)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
The official URL is attempted first; mirrors follow their configured order.
|
||||||
|
jsDelivr is excluded for repositories. Stop on the first success. Perform
|
||||||
|
persistent installation or other business state changes after success, rather
|
||||||
|
than inside a potentially repeated attempt.
|
||||||
|
|
||||||
|
## Results with resources
|
||||||
|
|
||||||
|
For temporary files, set `Options[*Download].Discard` to remove the file. Return
|
||||||
|
the partial `*Download` alongside errors, so cleanup also covers failed reads or
|
||||||
|
validation. The executor discards every failure and every race loser exactly
|
||||||
|
once. The caller owns and must track/release the winning result.
|
||||||
|
|
||||||
|
- `Timeout == 0` uses 30 seconds per resource attempt and no overall deadline
|
||||||
|
for repository attempts; a negative timeout is invalid. Extension Git HTTP
|
||||||
|
transport limits connections/TLS handshakes to 10 seconds and resets a 60-second
|
||||||
|
read deadline as data arrives. Slow continuous transfers can finish, while
|
||||||
|
stalled sources fail and allow ordered fallback. Parent cancellation still applies.
|
||||||
|
- Attempts must honor the supplied context; draining waits for them to return.
|
||||||
|
- Attempt contexts are canceled after return, so callbacks must finish reading
|
||||||
|
HTTP bodies before returning. Returning a still-streaming response is unsupported.
|
||||||
|
- Discard callbacks must accept zero values, must not panic, and run serially.
|
||||||
|
- Parent cancellation wins over a result selected while the parent is canceled;
|
||||||
|
the selected result is then discarded as well.
|
||||||
|
- Errors preserve their causes through wrapping/joining for `errors.Is`.
|
||||||
|
- `github.com` and `raw.githubusercontent.com` expand into eligible mirrors.
|
||||||
|
jsDelivr is only eligible for raw source resources. `api.github.com` is never
|
||||||
|
mirrored and is rejected by the public mirror candidates API.
|
||||||
|
- Client application update checks request the official GitHub API directly,
|
||||||
|
falling back to the independent Gopeed website API on request or validation
|
||||||
|
failure. Only package downloads obtain URLs from
|
||||||
|
`POST /api/v1/mirrors/github/candidates`. Candidate generation never performs
|
||||||
|
HTTP requests.
|
||||||
|
- Disabled mirrors and other URLs execute only the original URL.
|
||||||
|
- Config is a caller-owned immutable snapshot for the duration of a call.
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
// Package githubmirror rewrites GitHub URLs and executes mirrored operations.
|
||||||
|
package githubmirror
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Mirror struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
IsBuiltIn bool `json:"isBuiltIn"`
|
||||||
|
IsDeleted bool `json:"isDeleted"`
|
||||||
|
}
|
||||||
|
type Config struct {
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
Mirrors []Mirror `json:"mirrors"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultConfig() *Config {
|
||||||
|
return &Config{Enabled: true, Mirrors: []Mirror{
|
||||||
|
{Type: "jsdelivr", URL: "https://fastly.jsdelivr.net/gh", IsBuiltIn: true},
|
||||||
|
{Type: "ghProxy", URL: "https://fastgit.cc", IsBuiltIn: true},
|
||||||
|
{Type: "ghProxy", URL: "https://gh-proxy.com", IsBuiltIn: true},
|
||||||
|
{Type: "ghProxy", URL: "https://ghfast.top", IsBuiltIn: true},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
func (c *Config) Validate() error {
|
||||||
|
if len(c.Mirrors) > 32 {
|
||||||
|
return fmt.Errorf("too many GitHub mirrors")
|
||||||
|
}
|
||||||
|
for _, mirror := range c.Mirrors {
|
||||||
|
u, err := url.Parse(mirror.URL)
|
||||||
|
if err != nil || u == nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" {
|
||||||
|
return fmt.Errorf("invalid GitHub mirror URL")
|
||||||
|
}
|
||||||
|
if mirror.Type != "jsdelivr" && mirror.Type != "ghProxy" {
|
||||||
|
return fmt.Errorf("invalid GitHub mirror type: %s", mirror.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kind describes what a candidate endpoint must support.
|
||||||
|
type Kind uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
Resource Kind = iota
|
||||||
|
Repository
|
||||||
|
)
|
||||||
|
|
||||||
|
// Candidates preserves the official URL first. jsDelivr is only eligible for
|
||||||
|
// raw source resources; Git operations use mirrors supporting Git Smart HTTP.
|
||||||
|
// GitHub API URLs are never mirrored.
|
||||||
|
func Candidates(c *Config, raw string, kind Kind) []string {
|
||||||
|
result := []string{raw}
|
||||||
|
u, err := url.Parse(raw)
|
||||||
|
if err != nil || c == nil || !c.Enabled || u.User != nil || (u.Scheme != "https" && u.Scheme != "http") {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
if u.Host != "github.com" && u.Host != "raw.githubusercontent.com" {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
seen := map[string]bool{raw: true}
|
||||||
|
for _, mirror := range c.Mirrors {
|
||||||
|
if mirror.IsDeleted {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var candidate string
|
||||||
|
switch mirror.Type {
|
||||||
|
case "ghProxy":
|
||||||
|
candidate = strings.TrimRight(mirror.URL, "/") + "/" + raw
|
||||||
|
case "jsdelivr":
|
||||||
|
if kind == Repository || u.Host != "raw.githubusercontent.com" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
parts := strings.SplitN(strings.TrimPrefix(u.EscapedPath(), "/"), "/", 4)
|
||||||
|
if len(parts) != 4 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
candidate = strings.TrimRight(mirror.URL, "/") + "/" + parts[0] + "/" + parts[1] + "@" + parts[2] + "/" + parts[3]
|
||||||
|
}
|
||||||
|
if candidate != "" && !seen[candidate] {
|
||||||
|
result = append(result, candidate)
|
||||||
|
seen[candidate] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package githubmirror
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMirrorCandidates(t *testing.T) {
|
||||||
|
cfg := &Config{Enabled: true, Mirrors: []Mirror{
|
||||||
|
{Type: "jsdelivr", URL: "https://cdn.example/gh/"},
|
||||||
|
{Type: "ghProxy", URL: "https://mirror.example/"},
|
||||||
|
{Type: "ghProxy", URL: "https://deleted.example", IsDeleted: true},
|
||||||
|
}}
|
||||||
|
source := "https://raw.githubusercontent.com/owner/repo/main/a.txt"
|
||||||
|
want := []string{source, "https://cdn.example/gh/owner/repo@main/a.txt", "https://mirror.example/" + source}
|
||||||
|
if got := Candidates(cfg, source, Resource); !reflect.DeepEqual(got, want) {
|
||||||
|
t.Fatalf("got %v", got)
|
||||||
|
}
|
||||||
|
repo := "https://github.com/owner/repo.git"
|
||||||
|
if got := Candidates(cfg, repo, Repository); !reflect.DeepEqual(got, []string{repo, "https://mirror.example/" + repo}) {
|
||||||
|
t.Fatalf("git candidates %v", got)
|
||||||
|
}
|
||||||
|
for _, raw := range []string{"https://github.com.evil.example/a", "https://example.com/a", "https://user:[email protected]/a"} {
|
||||||
|
if got := Candidates(cfg, raw, Resource); !reflect.DeepEqual(got, []string{raw}) {
|
||||||
|
t.Fatalf("unexpected mirror: %v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
apiURL := "https://api.github.com/repos/GopeedLab/gopeed/releases?per_page=10"
|
||||||
|
for _, kind := range []Kind{Resource, Repository} {
|
||||||
|
if got := Candidates(cfg, apiURL, kind); !reflect.DeepEqual(got, []string{apiURL}) {
|
||||||
|
t.Fatalf("GitHub API must never use mirrors: %v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cfg.Enabled = false
|
||||||
|
if got := Candidates(cfg, source, Resource); !reflect.DeepEqual(got, []string{source}) {
|
||||||
|
t.Fatalf("disabled mirror: %v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
package githubmirror
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Options configures execution, not URL persistence. Discard releases results
|
||||||
|
// from failed attempts and successful race losers, including partial results
|
||||||
|
// returned together with an error. The caller owns the winning result.
|
||||||
|
type Options[T any] struct {
|
||||||
|
Kind Kind
|
||||||
|
// Timeout limits each attempt. Zero uses 30 seconds for resources and no
|
||||||
|
// overall deadline for repositories, whose transport owns idle timeouts.
|
||||||
|
Timeout time.Duration
|
||||||
|
Discard func(T)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempt must honor ctx and return a completed, validated result. Its context
|
||||||
|
// is canceled after it returns, so live response streams must not be returned.
|
||||||
|
// Return any partially acquired resources along with errors so Discard can
|
||||||
|
// release them. Failed attempts must not mutate persistent business state.
|
||||||
|
type Attempt[T any] func(context.Context, string) (T, error)
|
||||||
|
|
||||||
|
const defaultTimeout = 30 * time.Second
|
||||||
|
|
||||||
|
func prepare[T any](ctx context.Context, config *Config, raw string, options Options[T], attempt Attempt[T]) ([]string, time.Duration, error) {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
if raw == "" {
|
||||||
|
return nil, 0, errors.New("empty GitHub source URL")
|
||||||
|
}
|
||||||
|
if options.Kind != Resource && options.Kind != Repository {
|
||||||
|
return nil, 0, errors.New("invalid GitHub resource kind")
|
||||||
|
}
|
||||||
|
if options.Timeout < 0 {
|
||||||
|
return nil, 0, errors.New("invalid GitHub attempt timeout")
|
||||||
|
}
|
||||||
|
if attempt == nil {
|
||||||
|
return nil, 0, errors.New("GitHub mirror attempt is required")
|
||||||
|
}
|
||||||
|
if config != nil {
|
||||||
|
if err := config.Validate(); err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
timeout := options.Timeout
|
||||||
|
if timeout == 0 && options.Kind == Resource {
|
||||||
|
timeout = defaultTimeout
|
||||||
|
}
|
||||||
|
return Candidates(config, raw, options.Kind), timeout, nil
|
||||||
|
}
|
||||||
|
func invoke[T any](ctx context.Context, timeout time.Duration, candidate string, attempt Attempt[T]) (value T, err error) {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
if timeout > 0 {
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
||||||
|
} else {
|
||||||
|
ctx, cancel = context.WithCancel(ctx)
|
||||||
|
}
|
||||||
|
defer cancel()
|
||||||
|
defer func() {
|
||||||
|
if recovered := recover(); recovered != nil {
|
||||||
|
err = fmt.Errorf("GitHub mirror attempt panicked: %v", recovered)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
value, err = attempt(ctx, candidate)
|
||||||
|
if err == nil {
|
||||||
|
err = ctx.Err()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
func discard[T any](options Options[T], value T) {
|
||||||
|
if options.Discard != nil {
|
||||||
|
options.Discard(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Race executes the official URL and eligible mirrors concurrently. The first
|
||||||
|
// completed successful result wins; losing attempts are canceled and drained
|
||||||
|
// before return so no temporary files or other resources outlive the call.
|
||||||
|
// HTTP status/content validation is the attempt's responsibility.
|
||||||
|
func Race[T any](ctx context.Context, config *Config, raw string, options Options[T], attempt Attempt[T]) (T, error) {
|
||||||
|
var zero T
|
||||||
|
candidates, timeout, err := prepare(ctx, config, raw, options, attempt)
|
||||||
|
if err != nil {
|
||||||
|
return zero, err
|
||||||
|
}
|
||||||
|
raceCtx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
type outcome struct {
|
||||||
|
index int
|
||||||
|
value T
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
outcomes := make(chan outcome, len(candidates))
|
||||||
|
for index, candidate := range candidates {
|
||||||
|
go func(index int, candidate string) {
|
||||||
|
value, err := invoke(raceCtx, timeout, candidate, attempt)
|
||||||
|
outcomes <- outcome{index, value, err}
|
||||||
|
}(index, candidate)
|
||||||
|
}
|
||||||
|
var winner T
|
||||||
|
won := false
|
||||||
|
failures := make([]error, len(candidates))
|
||||||
|
for range candidates {
|
||||||
|
outcome := <-outcomes
|
||||||
|
if !won && outcome.err == nil && ctx.Err() == nil {
|
||||||
|
winner = outcome.value
|
||||||
|
won = true
|
||||||
|
cancel()
|
||||||
|
} else {
|
||||||
|
discard(options, outcome.value)
|
||||||
|
if outcome.err != nil {
|
||||||
|
failures[outcome.index] = fmt.Errorf("GitHub source %d: %w", outcome.index+1, outcome.err)
|
||||||
|
}
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
if won {
|
||||||
|
discard(options, winner)
|
||||||
|
}
|
||||||
|
return zero, err
|
||||||
|
}
|
||||||
|
if won {
|
||||||
|
return winner, nil
|
||||||
|
}
|
||||||
|
return zero, fmt.Errorf("all GitHub sources failed: %w", errors.Join(failures...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback executes the official URL first, then eligible mirrors in their
|
||||||
|
// configured order. It stops immediately on success and cleans every failure.
|
||||||
|
func Fallback[T any](ctx context.Context, config *Config, raw string, options Options[T], attempt Attempt[T]) (T, error) {
|
||||||
|
var zero T
|
||||||
|
candidates, timeout, err := prepare(ctx, config, raw, options, attempt)
|
||||||
|
if err != nil {
|
||||||
|
return zero, err
|
||||||
|
}
|
||||||
|
failures := make([]error, 0, len(candidates))
|
||||||
|
for index, candidate := range candidates {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return zero, err
|
||||||
|
}
|
||||||
|
value, err := invoke(ctx, timeout, candidate, attempt)
|
||||||
|
if err == nil && ctx.Err() == nil {
|
||||||
|
return value, nil
|
||||||
|
}
|
||||||
|
discard(options, value)
|
||||||
|
if parentErr := ctx.Err(); parentErr != nil {
|
||||||
|
return zero, parentErr
|
||||||
|
}
|
||||||
|
failures = append(failures, fmt.Errorf("GitHub source %d: %w", index+1, err))
|
||||||
|
}
|
||||||
|
return zero, fmt.Errorf("all GitHub sources failed: %w", errors.Join(failures...))
|
||||||
|
}
|
||||||
@@ -0,0 +1,214 @@
|
|||||||
|
package githubmirror
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"reflect"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const testSource = "https://github.com/owner/repo.git"
|
||||||
|
|
||||||
|
func testConfig() *Config {
|
||||||
|
return &Config{Enabled: true, Mirrors: []Mirror{
|
||||||
|
{Type: "ghProxy", URL: "https://first.example"},
|
||||||
|
{Type: "ghProxy", URL: "https://second.example"},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
func TestRaceWinsAndDrainsOwnedResults(t *testing.T) {
|
||||||
|
config := testConfig()
|
||||||
|
candidates := Candidates(config, testSource, Resource)
|
||||||
|
var started sync.WaitGroup
|
||||||
|
started.Add(len(candidates))
|
||||||
|
discarded := []string{}
|
||||||
|
winner, err := Race(context.Background(), config, testSource, Options[string]{
|
||||||
|
Discard: func(value string) { discarded = append(discarded, value) },
|
||||||
|
}, func(ctx context.Context, address string) (string, error) {
|
||||||
|
started.Done()
|
||||||
|
started.Wait()
|
||||||
|
switch address {
|
||||||
|
case candidates[0]:
|
||||||
|
return address, errors.New("official offline")
|
||||||
|
case candidates[1]:
|
||||||
|
return address, nil
|
||||||
|
default:
|
||||||
|
<-ctx.Done()
|
||||||
|
return address, ctx.Err()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if err != nil || winner != candidates[1] {
|
||||||
|
t.Fatalf("winner=%s err=%v", winner, err)
|
||||||
|
}
|
||||||
|
if len(discarded) != 2 || !contains(discarded, candidates[0]) || !contains(discarded, candidates[2]) {
|
||||||
|
t.Fatalf("discarded %v", discarded)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func contains(values []string, value string) bool {
|
||||||
|
for _, item := range values {
|
||||||
|
if item == value {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
func TestRaceCleansSimultaneousSuccessfulLosers(t *testing.T) {
|
||||||
|
config := testConfig()
|
||||||
|
var started sync.WaitGroup
|
||||||
|
started.Add(3)
|
||||||
|
cleaned := []string{}
|
||||||
|
winner, err := Race(context.Background(), config, testSource, Options[string]{Discard: func(value string) { cleaned = append(cleaned, value) }}, func(_ context.Context, address string) (string, error) {
|
||||||
|
started.Done()
|
||||||
|
started.Wait()
|
||||||
|
return address, nil
|
||||||
|
})
|
||||||
|
if err != nil || winner == "" || len(cleaned) != 2 || contains(cleaned, winner) {
|
||||||
|
t.Fatalf("winner=%s discarded=%v err=%v", winner, cleaned, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestRaceParentCancellationCleansWinnerToo(t *testing.T) {
|
||||||
|
config := testConfig()
|
||||||
|
config.Mirrors = config.Mirrors[:1]
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
cleaned := []string{}
|
||||||
|
winner, err := Race(ctx, config, testSource, Options[string]{Discard: func(value string) { cleaned = append(cleaned, value) }}, func(ctx context.Context, address string) (string, error) {
|
||||||
|
if address == testSource {
|
||||||
|
return address, nil
|
||||||
|
}
|
||||||
|
<-ctx.Done()
|
||||||
|
cancel()
|
||||||
|
return address, ctx.Err()
|
||||||
|
})
|
||||||
|
if !errors.Is(err, context.Canceled) || winner != "" || len(cleaned) != 2 || !contains(cleaned, testSource) {
|
||||||
|
t.Fatalf("winner=%s cleanup=%v err=%v", winner, cleaned, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestRaceTimeoutAndErrorAggregation(t *testing.T) {
|
||||||
|
expected := errors.New("offline")
|
||||||
|
config := testConfig()
|
||||||
|
config.Mirrors = config.Mirrors[:1]
|
||||||
|
discarded := 0
|
||||||
|
_, err := Race(context.Background(), config, testSource, Options[string]{Timeout: 10 * time.Millisecond, Discard: func(string) { discarded++ }}, func(ctx context.Context, address string) (string, error) {
|
||||||
|
if address == testSource {
|
||||||
|
return address, expected
|
||||||
|
}
|
||||||
|
<-ctx.Done()
|
||||||
|
return address, ctx.Err()
|
||||||
|
})
|
||||||
|
if !errors.Is(err, expected) || !errors.Is(err, context.DeadlineExceeded) || discarded != 2 {
|
||||||
|
t.Fatalf("discarded=%d err=%v", discarded, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestFallbackPreservesOrderAndStopsOnSuccess(t *testing.T) {
|
||||||
|
config := testConfig()
|
||||||
|
candidates := Candidates(config, testSource, Repository)
|
||||||
|
tried := []string{}
|
||||||
|
cleaned := []string{}
|
||||||
|
winner, err := Fallback(context.Background(), config, testSource, Options[string]{Kind: Repository, Discard: func(value string) { cleaned = append(cleaned, value) }}, func(_ context.Context, address string) (string, error) {
|
||||||
|
tried = append(tried, address)
|
||||||
|
if address == candidates[1] {
|
||||||
|
return address, nil
|
||||||
|
}
|
||||||
|
return address, errors.New("offline")
|
||||||
|
})
|
||||||
|
if err != nil || winner != candidates[1] || !reflect.DeepEqual(tried, candidates[:2]) || !reflect.DeepEqual(cleaned, candidates[:1]) {
|
||||||
|
t.Fatalf("winner=%s tried=%v discarded=%v err=%v", winner, tried, cleaned, err)
|
||||||
|
}
|
||||||
|
tried = nil
|
||||||
|
_, err = Fallback(context.Background(), config, testSource, Options[string]{Kind: Repository}, func(_ context.Context, address string) (string, error) {
|
||||||
|
tried = append(tried, address)
|
||||||
|
return address, nil
|
||||||
|
})
|
||||||
|
if err != nil || !reflect.DeepEqual(tried, candidates[:1]) {
|
||||||
|
t.Fatalf("tried=%v err=%v", tried, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestFallbackTimeoutContinuesWithFreshContext(t *testing.T) {
|
||||||
|
discarded := []string{}
|
||||||
|
winner, err := Fallback(context.Background(), testConfig(), testSource, Options[string]{Kind: Repository, Timeout: 10 * time.Millisecond, Discard: func(value string) { discarded = append(discarded, value) }}, func(ctx context.Context, address string) (string, error) {
|
||||||
|
if address == testSource {
|
||||||
|
<-ctx.Done()
|
||||||
|
return address, ctx.Err()
|
||||||
|
}
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return address, err
|
||||||
|
}
|
||||||
|
return address, nil
|
||||||
|
})
|
||||||
|
if err != nil || !strings.HasPrefix(winner, "https://first.example/") || !reflect.DeepEqual(discarded, []string{testSource}) {
|
||||||
|
t.Fatalf("winner=%s cleanup=%v err=%v", winner, discarded, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestFallbackCancellationStopsBeforeNextMirror(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
calls := 0
|
||||||
|
cleaned := 0
|
||||||
|
_, err := Fallback(ctx, testConfig(), testSource, Options[string]{Kind: Repository, Discard: func(string) { cleaned++ }}, func(ctx context.Context, address string) (string, error) {
|
||||||
|
calls++
|
||||||
|
cancel()
|
||||||
|
return address, ctx.Err()
|
||||||
|
})
|
||||||
|
if !errors.Is(err, context.Canceled) || calls != 1 || cleaned != 1 {
|
||||||
|
t.Fatalf("calls=%d discarded=%d err=%v", calls, cleaned, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestExecutorsDoNotStartInvalidOrCanceledRequests(t *testing.T) {
|
||||||
|
canceled, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
for _, test := range []struct {
|
||||||
|
ctx context.Context
|
||||||
|
config *Config
|
||||||
|
options Options[string]
|
||||||
|
attempt Attempt[string]
|
||||||
|
}{
|
||||||
|
{ctx: canceled, options: Options[string]{}, attempt: func(context.Context, string) (string, error) { t.Fatal("called after cancellation"); return "", nil }},
|
||||||
|
{ctx: context.Background(), options: Options[string]{Kind: 99}, attempt: func(context.Context, string) (string, error) { t.Fatal("invalid kind"); return "", nil }},
|
||||||
|
{ctx: context.Background(), options: Options[string]{Timeout: -time.Second}, attempt: func(context.Context, string) (string, error) { t.Fatal("invalid timeout"); return "", nil }},
|
||||||
|
{ctx: context.Background(), options: Options[string]{}, attempt: nil},
|
||||||
|
{ctx: context.Background(), config: &Config{Mirrors: []Mirror{{Type: "invalid", URL: "https://mirror.example"}}}, attempt: func(context.Context, string) (string, error) { t.Fatal("invalid config"); return "", nil }},
|
||||||
|
} {
|
||||||
|
if _, err := Race(test.ctx, test.config, testSource, test.options, test.attempt); err == nil {
|
||||||
|
t.Fatal("invalid race accepted")
|
||||||
|
}
|
||||||
|
if _, err := Fallback(test.ctx, test.config, testSource, test.options, test.attempt); err == nil {
|
||||||
|
t.Fatal("invalid fallback accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestAttemptPanicBecomesError(t *testing.T) {
|
||||||
|
attempt := func(context.Context, string) (string, error) { panic("broken operation") }
|
||||||
|
if _, err := Race(context.Background(), nil, testSource, Options[string]{}, attempt); err == nil || !strings.Contains(err.Error(), "broken operation") {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := Fallback(context.Background(), nil, testSource, Options[string]{}, attempt); err == nil || !strings.Contains(err.Error(), "broken operation") {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestNonGithubResourcesAndDisabledMirrorsUseOnlyOriginal(t *testing.T) {
|
||||||
|
for _, test := range []struct {
|
||||||
|
config *Config
|
||||||
|
raw string
|
||||||
|
}{{testConfig(), "https://example.com/resource"}, {&Config{Enabled: false, Mirrors: testConfig().Mirrors}, testSource}} {
|
||||||
|
calls := 0
|
||||||
|
got, err := Race(context.Background(), test.config, test.raw, Options[string]{}, func(_ context.Context, address string) (string, error) { calls++; return address, nil })
|
||||||
|
if err != nil || got != test.raw || calls != 1 {
|
||||||
|
t.Fatalf("got=%s calls=%d err=%v", got, calls, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRepositoryAttemptsHaveNoDefaultDeadline(t *testing.T) {
|
||||||
|
value, err := Fallback(context.Background(), nil, testSource, Options[string]{Kind: Repository}, func(ctx context.Context, raw string) (string, error) {
|
||||||
|
if _, ok := ctx.Deadline(); ok {
|
||||||
|
t.Fatal("repository attempt has a total deadline")
|
||||||
|
}
|
||||||
|
return raw, nil
|
||||||
|
})
|
||||||
|
if err != nil || value != testSource {
|
||||||
|
t.Fatalf("value=%s error=%v", value, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
// Package production associates generated resources with their input progress.
|
||||||
|
package production
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Source counts unique input bytes and total received bytes (including refetches).
|
||||||
|
// Implementations must support concurrent snapshots.
|
||||||
|
type Source interface {
|
||||||
|
Progress() (downloaded, received int64)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reader resolves its optional producer before response headers are sent.
|
||||||
|
// A nil producer preserves ordinary Blob behavior.
|
||||||
|
type Reader interface {
|
||||||
|
Production() Source
|
||||||
|
WaitProduction(context.Context) error
|
||||||
|
}
|
||||||
|
type Registry struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
sources map[string]Source
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Registry) Set(id string, source Source) {
|
||||||
|
r.mu.Lock()
|
||||||
|
defer r.mu.Unlock()
|
||||||
|
if r.sources == nil {
|
||||||
|
r.sources = make(map[string]Source)
|
||||||
|
}
|
||||||
|
r.sources[id] = source
|
||||||
|
}
|
||||||
|
func (r *Registry) Get(id string) Source { r.mu.Lock(); defer r.mu.Unlock(); return r.sources[id] }
|
||||||
|
func (r *Registry) Remove(id string) { r.mu.Lock(); defer r.mu.Unlock(); delete(r.sources, id) }
|
||||||
@@ -1,12 +1,37 @@
|
|||||||
package bt
|
package bt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
|
)
|
||||||
|
|
||||||
|
type trackerSubscriptionStatus struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
TrackerCount int `json:"trackerCount"`
|
||||||
|
LastFetchTime time.Time `json:"lastFetchTime"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type state struct {
|
||||||
|
TrackerSubscriptionStatuses map[string]trackerSubscriptionStatus `json:"trackerSubscriptionStatuses"`
|
||||||
|
TrackerSources map[string][]string `json:"trackerSources"`
|
||||||
|
LastTrackerUpdateTime time.Time `json:"lastTrackerUpdateTime"`
|
||||||
|
}
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
|
fetcher.ConfigState[state]
|
||||||
|
TrackerSubscribeURLs []string `json:"trackerSubscribeUrls"`
|
||||||
|
AutoUpdateTrackers bool `json:"autoUpdateTrackers"`
|
||||||
|
CustomTrackers []string `json:"customTrackers"`
|
||||||
ListenPort int `json:"listenPort"`
|
ListenPort int `json:"listenPort"`
|
||||||
Trackers []string `json:"trackers"`
|
|
||||||
// SeedKeep is always keep seeding after downloading is complete, unless manually stopped.
|
// SeedKeep is always keep seeding after downloading is complete, unless manually stopped.
|
||||||
SeedKeep bool `json:"seedKeep"`
|
SeedKeep bool `json:"seedKeep"`
|
||||||
// SeedRatio is the ratio of uploaded data to downloaded data to seed.
|
// SeedRatio is the ratio of uploaded data to downloaded data to seed.
|
||||||
SeedRatio float64 `json:"seedRatio"`
|
SeedRatio float64 `json:"seedRatio"`
|
||||||
// SeedTime is the time in seconds to seed after downloading is complete.
|
// SeedTime is the time in seconds to seed after downloading is complete.
|
||||||
SeedTime int64 `json:"seedTime"`
|
SeedTime int64 `json:"seedTime"`
|
||||||
|
// DisableDHT and DisablePEX turn off DHT and PEX peer discovery. Like ListenPort,
|
||||||
|
// they are read when the shared client is built.
|
||||||
|
DisableDHT bool `json:"disableDht"`
|
||||||
|
DisablePEX bool `json:"disablePex"`
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,464 @@
|
|||||||
|
package bt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"sync/atomic"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/internal/controller"
|
||||||
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
btprotocol "github.com/GopeedLab/gopeed/pkg/protocol/bt"
|
||||||
|
"github.com/anacrolix/torrent"
|
||||||
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
||||||
|
"golang.org/x/time/rate"
|
||||||
|
)
|
||||||
|
|
||||||
|
type btDownloadHarness struct {
|
||||||
|
*Fetcher
|
||||||
|
pendingRequests atomic.Int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// These tests exercise the BT fetcher, native storage and real peer protocol.
|
||||||
|
// All content is generated locally and served by a loopback seeder.
|
||||||
|
func btDownloadFetcher(t *testing.T, f fetcher.Fetcher) *btDownloadHarness {
|
||||||
|
t.Helper()
|
||||||
|
if f == nil {
|
||||||
|
f = new(FetcherManager).Build()
|
||||||
|
}
|
||||||
|
ctl := controller.NewController()
|
||||||
|
ctl.GetConfig = func(v any) {
|
||||||
|
encoded, err := json.Marshal(new(FetcherManager).DefaultConfig())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(encoded, v); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.Setup(ctl)
|
||||||
|
t.Cleanup(func() {
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if err := closeClient(); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
h := &btDownloadHarness{Fetcher: f.(*Fetcher)}
|
||||||
|
// Keep the test client on loopback and observe real cancellation replies.
|
||||||
|
// Waiting for them avoids the pinned library's immediate cancel/resume
|
||||||
|
// request scheduling bug, without relying on arbitrary sleep durations.
|
||||||
|
cfg = torrent.TestingConfig(t)
|
||||||
|
cfg.DisableTrackers = false
|
||||||
|
cfg.Seed = true
|
||||||
|
cfg.DisableUTP = true
|
||||||
|
cfg.DisableIPv6 = true
|
||||||
|
cfg.DisableWebtorrent = true
|
||||||
|
cfg.KeepAliveTimeout = time.Minute
|
||||||
|
cfg.MaxAllocPeerRequestDataPerConn = 1 << 20
|
||||||
|
cfg.Callbacks.SentRequest = []func(torrent.PeerRequestEvent){func(torrent.PeerRequestEvent) {
|
||||||
|
h.pendingRequests.Add(1)
|
||||||
|
}}
|
||||||
|
cfg.Callbacks.ReadMessage = func(_ *torrent.PeerConn, msg *pp.Message) {
|
||||||
|
if !msg.Keepalive && (msg.Type == pp.Piece || msg.Type == pp.Reject) {
|
||||||
|
h.pendingRequests.Add(-1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
client, err = torrent.NewClient(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
func btResolve(t *testing.T, f *btDownloadHarness, source string) {
|
||||||
|
t.Helper()
|
||||||
|
if err := f.Resolve(&base.Request{URL: source}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func btStart(t *testing.T, f *btDownloadHarness) {
|
||||||
|
t.Helper()
|
||||||
|
if err := f.Start(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func btAwaitDone(t *testing.T, f *btDownloadHarness) {
|
||||||
|
t.Helper()
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() { done <- f.Wait() }()
|
||||||
|
timeout := time.NewTimer(20 * time.Second)
|
||||||
|
defer timeout.Stop()
|
||||||
|
select {
|
||||||
|
case err := <-done:
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("BT download failed: %v", err)
|
||||||
|
}
|
||||||
|
if !f.isDone() {
|
||||||
|
t.Fatal("Wait returned before selected files were verified")
|
||||||
|
}
|
||||||
|
case <-timeout.C:
|
||||||
|
var status strings.Builder
|
||||||
|
client.WriteStatus(&status)
|
||||||
|
t.Log(status.String())
|
||||||
|
t.Fatalf("BT download did not finish: progress=%v stats=%+v pieces=%+v peers=%+v", f.Progress(), f.torrent.Stats(), f.torrent.PieceStateRuns(), f.Stats().Runtime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func btAwaitPartial(t *testing.T, f *btDownloadHarness) {
|
||||||
|
t.Helper()
|
||||||
|
timeout := time.NewTimer(15 * time.Second)
|
||||||
|
defer timeout.Stop()
|
||||||
|
ticker := time.NewTicker(20 * time.Millisecond)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
progress := f.Progress().TotalDownloaded()
|
||||||
|
if progress >= 32*1024 && progress < f.Meta().Res.Size {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case <-timeout.C:
|
||||||
|
t.Fatal("BT transfer did not reach partial progress")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func btPause(t *testing.T, f *btDownloadHarness) {
|
||||||
|
t.Helper()
|
||||||
|
if err := f.Pause(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
timeout := time.NewTimer(5 * time.Second)
|
||||||
|
defer timeout.Stop()
|
||||||
|
ticker := time.NewTicker(10 * time.Millisecond)
|
||||||
|
defer ticker.Stop()
|
||||||
|
for {
|
||||||
|
if f.pendingRequests.Load() == 0 {
|
||||||
|
// The message callback runs under the client lock. Acquiring that
|
||||||
|
// lock ensures the final cancellation reply has finished processing.
|
||||||
|
client.Stats()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
case <-timeout.C:
|
||||||
|
t.Fatal("peer did not acknowledge cancelled requests")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBTDownloadSources(t *testing.T) {
|
||||||
|
for _, kind := range []string{"magnet", "torrent", "file URI", "data URI"} {
|
||||||
|
t.Run(kind, func(t *testing.T) {
|
||||||
|
fixture := newBTFixture(t, map[string][]byte{"": btPayload(96*1024, kind)}, 0)
|
||||||
|
source := fixture.Magnet
|
||||||
|
switch kind {
|
||||||
|
case "torrent":
|
||||||
|
source = fixture.TorrentFile
|
||||||
|
case "file URI":
|
||||||
|
source = fixture.FileURI()
|
||||||
|
case "data URI":
|
||||||
|
source = fixture.DataURI
|
||||||
|
}
|
||||||
|
f := btDownloadFetcher(t, nil)
|
||||||
|
btResolve(t, f, source)
|
||||||
|
if f.Meta().Opts != nil {
|
||||||
|
t.Fatal("Resolve assigned download options")
|
||||||
|
}
|
||||||
|
f.storage.mu.RLock()
|
||||||
|
bound := f.storage.owner != nil
|
||||||
|
f.storage.mu.RUnlock()
|
||||||
|
if bound {
|
||||||
|
t.Fatal("Resolve bound download storage")
|
||||||
|
}
|
||||||
|
resolvedTorrent := f.torrent
|
||||||
|
dir := filepath.Join(t.TempDir(), "chosen-after-resolve")
|
||||||
|
f.Meta().Opts = &base.Options{Path: dir}
|
||||||
|
if _, err := os.Stat(dir); !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("output directory exists before Start: %v", err)
|
||||||
|
}
|
||||||
|
btStart(t, f)
|
||||||
|
if f.torrent != resolvedTorrent {
|
||||||
|
t.Fatal("Start replaced the resolved torrent")
|
||||||
|
}
|
||||||
|
btAwaitDone(t, f)
|
||||||
|
fixture.AssertFiles(t, dir)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBTDirectDownloadWithExtraTrackers(t *testing.T) {
|
||||||
|
for _, source := range []string{"request", "config", "subscription"} {
|
||||||
|
t.Run(source, func(t *testing.T) {
|
||||||
|
fixture := makeBTFixture(t, map[string][]byte{
|
||||||
|
"a.bin": btPayload(20*1024, "direct-a"),
|
||||||
|
"nested/b.bin": btPayload(48*1024, "direct-b"),
|
||||||
|
}, 0, false, false)
|
||||||
|
magnet, err := url.Parse(fixture.Magnet)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
query := magnet.Query()
|
||||||
|
query.Del("tr")
|
||||||
|
query.Del("x.pe")
|
||||||
|
magnet.RawQuery = query.Encode()
|
||||||
|
req := &base.Request{URL: magnet.String()}
|
||||||
|
f := btDownloadFetcher(t, nil)
|
||||||
|
if source == "request" {
|
||||||
|
req.Extra = &btprotocol.ReqExtra{Trackers: []string{fixture.TrackerURL}}
|
||||||
|
} else {
|
||||||
|
backendConfig := new(FetcherManager).DefaultConfig().(*config)
|
||||||
|
if source == "config" {
|
||||||
|
backendConfig.CustomTrackers = []string{fixture.TrackerURL}
|
||||||
|
} else {
|
||||||
|
backendConfig.TrackerSubscribeURLs = []string{"https://subscription.example/list"}
|
||||||
|
backendConfig.State.TrackerSources[backendConfig.TrackerSubscribeURLs[0]] = []string{fixture.TrackerURL}
|
||||||
|
}
|
||||||
|
f.ctl.GetConfig = func(dst any) {
|
||||||
|
raw, err := json.Marshal(backendConfig)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(raw, dst); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// CreateDirect supplies options before Downloader resolves the task.
|
||||||
|
dir := filepath.Join(t.TempDir(), "direct-download")
|
||||||
|
opts := &base.Options{Path: dir, SelectFiles: []int{}}
|
||||||
|
f.Meta().Opts = opts
|
||||||
|
resolved := make(chan error, 1)
|
||||||
|
go func() { resolved <- f.Resolve(req) }()
|
||||||
|
select {
|
||||||
|
case err := <-resolved:
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
f.torrentDropFunc()
|
||||||
|
<-resolved
|
||||||
|
t.Fatal("direct magnet download did not obtain metadata from its extra tracker")
|
||||||
|
}
|
||||||
|
if f.Meta().Opts != opts || len(opts.SelectFiles) != len(fixture.Files) {
|
||||||
|
t.Fatal("resolution lost direct download options or did not select all files")
|
||||||
|
}
|
||||||
|
btStart(t, f)
|
||||||
|
btAwaitDone(t, f)
|
||||||
|
fixture.AssertFiles(t, dir)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBTDownloadSelectedFiles(t *testing.T) {
|
||||||
|
fixture := newBTFixture(t, map[string][]byte{
|
||||||
|
"a.bin": btPayload(20*1024, "a"), "nested/b.bin": btPayload(48*1024, "b"),
|
||||||
|
}, 0)
|
||||||
|
f := btDownloadFetcher(t, nil)
|
||||||
|
dir := t.TempDir()
|
||||||
|
btResolve(t, f, fixture.Magnet)
|
||||||
|
selected := -1
|
||||||
|
for i, file := range f.Meta().Res.Files {
|
||||||
|
if file.Name == "b.bin" {
|
||||||
|
selected = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if selected < 0 {
|
||||||
|
t.Fatal("selected file absent from metadata")
|
||||||
|
}
|
||||||
|
f.Meta().Opts = &base.Options{Path: dir}
|
||||||
|
if err := f.Patch(nil, &base.Options{SelectFiles: []int{selected}}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
btStart(t, f)
|
||||||
|
btAwaitDone(t, f)
|
||||||
|
fixture.AssertFiles(t, dir, filepath.Join("bundle", "nested", "b.bin"))
|
||||||
|
if _, err := os.Stat(filepath.Join(dir, "bundle", "a.bin")); !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("unselected output remains: %v", err)
|
||||||
|
}
|
||||||
|
if f.Meta().Res.Size != 48*1024 {
|
||||||
|
t.Fatal("selected resource size is incorrect")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBTPauseAndResume(t *testing.T) {
|
||||||
|
fixture := newBTFixture(t, map[string][]byte{"": btPayload(512*1024, "pause")}, 64*1024)
|
||||||
|
f := btDownloadFetcher(t, nil)
|
||||||
|
dir := t.TempDir()
|
||||||
|
btResolve(t, f, fixture.Magnet)
|
||||||
|
f.Meta().Opts = &base.Options{Path: dir}
|
||||||
|
btStart(t, f)
|
||||||
|
btAwaitPartial(t, f)
|
||||||
|
btPause(t, f)
|
||||||
|
resolvedTorrent := f.torrent
|
||||||
|
fixture.Limiter.SetLimit(rate.Inf)
|
||||||
|
// Once cancelled requests have settled, no more data should arrive.
|
||||||
|
stats := f.torrent.Stats()
|
||||||
|
readWhilePaused := stats.BytesReadData.Int64()
|
||||||
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
stats = f.torrent.Stats()
|
||||||
|
if stats.BytesReadData.Int64() != readWhilePaused || f.isDone() {
|
||||||
|
t.Fatal("data download continued while paused")
|
||||||
|
}
|
||||||
|
btStart(t, f)
|
||||||
|
if f.torrent != resolvedTorrent {
|
||||||
|
t.Fatal("resume replaced the resolved torrent")
|
||||||
|
}
|
||||||
|
btAwaitDone(t, f)
|
||||||
|
fixture.AssertFiles(t, dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
type btFetcherCheckpoint struct {
|
||||||
|
Meta *fetcher.FetcherMeta `json:"meta"`
|
||||||
|
Data json.RawMessage `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func btStore(t *testing.T, f *btDownloadHarness) []byte {
|
||||||
|
t.Helper()
|
||||||
|
data, err := new(FetcherManager).Store(f.Fetcher)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
encodedData, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
encoded, err := json.Marshal(btFetcherCheckpoint{Meta: f.Meta(), Data: encodedData})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return encoded
|
||||||
|
}
|
||||||
|
|
||||||
|
func btRestore(t *testing.T, encoded []byte) *btDownloadHarness {
|
||||||
|
t.Helper()
|
||||||
|
var checkpoint btFetcherCheckpoint
|
||||||
|
if err := json.Unmarshal(encoded, &checkpoint); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
data, restore := new(FetcherManager).Restore()
|
||||||
|
if err := json.Unmarshal(checkpoint.Data, data); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return btDownloadFetcher(t, restore(checkpoint.Meta, data))
|
||||||
|
}
|
||||||
|
|
||||||
|
func btCloseForRestart(t *testing.T, f *btDownloadHarness) {
|
||||||
|
t.Helper()
|
||||||
|
if err := f.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := new(FetcherManager).Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBTColdStartResume(t *testing.T) {
|
||||||
|
for _, kind := range []string{"v1", "hybrid"} {
|
||||||
|
t.Run(kind, func(t *testing.T) {
|
||||||
|
var fixture *btFixture
|
||||||
|
if kind == "hybrid" {
|
||||||
|
fixture = newBTHybridFixture(t, btPayload(512*1024, "restart-hybrid"))
|
||||||
|
fixture.Limiter.SetLimit(64 * 1024)
|
||||||
|
} else {
|
||||||
|
fixture = newBTFixture(t, map[string][]byte{"": btPayload(512*1024, "restart")}, 64*1024)
|
||||||
|
}
|
||||||
|
f := btDownloadFetcher(t, nil)
|
||||||
|
dir := t.TempDir()
|
||||||
|
btResolve(t, f, fixture.Magnet)
|
||||||
|
f.Meta().Opts = &base.Options{Path: dir}
|
||||||
|
btStart(t, f)
|
||||||
|
btAwaitPartial(t, f)
|
||||||
|
btPause(t, f)
|
||||||
|
checkpoint := btStore(t, f)
|
||||||
|
hash := f.Meta().Res.Hash
|
||||||
|
btCloseForRestart(t, f)
|
||||||
|
seedStats := fixture.Torrent.Stats()
|
||||||
|
sentBeforeRestart := seedStats.BytesWrittenData.Int64()
|
||||||
|
restored := btRestore(t, checkpoint)
|
||||||
|
if restored.Meta().Opts.Path != dir || restored.Meta().Res.Hash != hash {
|
||||||
|
t.Fatal("download directory or hash was lost")
|
||||||
|
}
|
||||||
|
fixture.Limiter.SetLimit(rate.Inf)
|
||||||
|
btStart(t, restored)
|
||||||
|
btAwaitDone(t, restored)
|
||||||
|
fixture.AssertFiles(t, dir)
|
||||||
|
seedStats = fixture.Torrent.Stats()
|
||||||
|
if sent := seedStats.BytesWrittenData.Int64() - sentBeforeRestart; sent >= 512*1024 {
|
||||||
|
t.Fatalf("restart redownloaded the entire file: %d bytes", sent)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBTDownloadHybrid(t *testing.T) {
|
||||||
|
fixture := newBTHybridFixture(t, btPayload(80*1024, "hybrid-v2"))
|
||||||
|
f := btDownloadFetcher(t, nil)
|
||||||
|
dir := t.TempDir()
|
||||||
|
btResolve(t, f, fixture.Magnet)
|
||||||
|
f.Meta().Opts = &base.Options{Path: dir}
|
||||||
|
btStart(t, f)
|
||||||
|
btAwaitDone(t, f)
|
||||||
|
fixture.AssertFiles(t, dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBTRepairCorruptedExistingFile(t *testing.T) {
|
||||||
|
fixture := newBTFixture(t, map[string][]byte{"": btPayload(128*1024, "repair")}, 0)
|
||||||
|
dir := t.TempDir()
|
||||||
|
damaged := append([]byte(nil), fixture.Files["payload.bin"]...)
|
||||||
|
damaged[20000] ^= 0xff
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, "payload.bin"), damaged, 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
f := btDownloadFetcher(t, nil)
|
||||||
|
btResolve(t, f, fixture.Magnet)
|
||||||
|
f.Meta().Opts = &base.Options{Path: dir}
|
||||||
|
btStart(t, f)
|
||||||
|
btAwaitDone(t, f)
|
||||||
|
fixture.AssertFiles(t, dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBTPatchSelectionWhilePaused(t *testing.T) {
|
||||||
|
fixture := newBTFixture(t, map[string][]byte{"a.bin": btPayload(256*1024, "patch-a"), "b.bin": btPayload(256*1024, "patch-b")}, 64*1024)
|
||||||
|
f := btDownloadFetcher(t, nil)
|
||||||
|
dir := t.TempDir()
|
||||||
|
btResolve(t, f, fixture.Magnet)
|
||||||
|
f.Meta().Opts = &base.Options{Path: dir, SelectFiles: []int{0}}
|
||||||
|
btStart(t, f)
|
||||||
|
btAwaitPartial(t, f)
|
||||||
|
btPause(t, f)
|
||||||
|
if err := f.Patch(nil, &base.Options{SelectFiles: []int{1}}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
fixture.Limiter.SetLimit(rate.Inf)
|
||||||
|
btStart(t, f)
|
||||||
|
btAwaitDone(t, f)
|
||||||
|
fixture.AssertFiles(t, dir, filepath.Join("bundle", "b.bin"))
|
||||||
|
for _, suffix := range []string{"", ".part"} {
|
||||||
|
if _, err := os.Stat(filepath.Join(dir, "bundle", "a.bin") + suffix); !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("unselected output remains: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBTDownloadMultiFileWithEmptyFile(t *testing.T) {
|
||||||
|
fixture := newBTFixture(t, map[string][]byte{"empty.txt": {}, "nested/data.bin": btPayload(48*1024, "empty-file")}, 0)
|
||||||
|
f := btDownloadFetcher(t, nil)
|
||||||
|
dir := t.TempDir()
|
||||||
|
btResolve(t, f, fixture.Magnet)
|
||||||
|
f.Meta().Opts = &base.Options{Path: dir}
|
||||||
|
btStart(t, f)
|
||||||
|
btAwaitDone(t, f)
|
||||||
|
fixture.AssertFiles(t, dir)
|
||||||
|
}
|
||||||
+287
-100
File diff suppressed because it is too large.
Load diff
@@ -18,11 +18,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFetcher_Resolve_Torrent(t *testing.T) {
|
func TestFetcher_Resolve_Torrent(t *testing.T) {
|
||||||
doResolve(t, buildFetcher())
|
doResolve(t, buildFetcher(t))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFetcher_Resolve_DataUri_Torrent(t *testing.T) {
|
func TestFetcher_Resolve_DataUri_Torrent(t *testing.T) {
|
||||||
fetcher := buildFetcher()
|
fetcher := buildFetcher(t)
|
||||||
buf, err := os.ReadFile("./testdata/ubuntu-22.04-live-server-amd64.iso.torrent")
|
buf, err := os.ReadFile("./testdata/ubuntu-22.04-live-server-amd64.iso.torrent")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -31,7 +31,7 @@ func TestFetcher_Resolve_DataUri_Torrent(t *testing.T) {
|
|||||||
dataUri := "data:application/x-bittorrent;base64," + base64.StdEncoding.EncodeToString(buf)
|
dataUri := "data:application/x-bittorrent;base64," + base64.StdEncoding.EncodeToString(buf)
|
||||||
err = fetcher.Resolve(&base.Request{
|
err = fetcher.Resolve(&base.Request{
|
||||||
URL: dataUri,
|
URL: dataUri,
|
||||||
}, nil)
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@ func TestFetcher_Resolve_DataUri_Torrent(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestFetcher_Config(t *testing.T) {
|
func TestFetcher_Config(t *testing.T) {
|
||||||
doResolve(t, buildConfigFetcher(nil))
|
doResolve(t, buildConfigFetcher(t, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFetcher_ResolveWithProxy(t *testing.T) {
|
func TestFetcher_ResolveWithProxy(t *testing.T) {
|
||||||
@@ -61,7 +61,7 @@ func TestFetcher_ResolveWithProxy(t *testing.T) {
|
|||||||
proxyListener := test.StartSocks5Server(usr, pwd)
|
proxyListener := test.StartSocks5Server(usr, pwd)
|
||||||
defer proxyListener.Close()
|
defer proxyListener.Close()
|
||||||
|
|
||||||
doResolve(t, buildConfigFetcher(&base.DownloaderProxyConfig{
|
doResolve(t, buildConfigFetcher(t, &base.DownloaderProxyConfig{
|
||||||
Enable: true,
|
Enable: true,
|
||||||
System: false,
|
System: false,
|
||||||
Scheme: "socks5",
|
Scheme: "socks5",
|
||||||
@@ -81,7 +81,7 @@ func doResolve(t *testing.T, fetcher fetcher.Fetcher) {
|
|||||||
"udp://tracker.bitsearch.to:1337/announce",
|
"udp://tracker.bitsearch.to:1337/announce",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, nil)
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -111,7 +111,7 @@ func doResolve(t *testing.T, fetcher fetcher.Fetcher) {
|
|||||||
"udp://tracker.bitsearch.to:1337/announce",
|
"udp://tracker.bitsearch.to:1337/announce",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, nil)
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -145,7 +145,7 @@ func doResolve(t *testing.T, fetcher fetcher.Fetcher) {
|
|||||||
t.Run("Resolve Unclean Torrent", func(t *testing.T) {
|
t.Run("Resolve Unclean Torrent", func(t *testing.T) {
|
||||||
err := fetcher.Resolve(&base.Request{
|
err := fetcher.Resolve(&base.Request{
|
||||||
URL: "./testdata/test.unclean.torrent",
|
URL: "./testdata/test.unclean.torrent",
|
||||||
}, nil)
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Resolve Unclean Torrent Resolve() got = %v, want nil", err)
|
t.Errorf("Resolve Unclean Torrent Resolve() got = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -156,7 +156,7 @@ func doResolve(t *testing.T, fetcher fetcher.Fetcher) {
|
|||||||
uri := "file:///" + file
|
uri := "file:///" + file
|
||||||
err := fetcher.Resolve(&base.Request{
|
err := fetcher.Resolve(&base.Request{
|
||||||
URL: uri,
|
URL: uri,
|
||||||
}, nil)
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Resolve file scheme Torrent Resolve() got = %v, want nil", err)
|
t.Errorf("Resolve file scheme Torrent Resolve() got = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -211,7 +211,7 @@ func TestFetcherManager_ParseName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildFetcher() fetcher.Fetcher {
|
func buildFetcher(t testing.TB) fetcher.Fetcher {
|
||||||
fb := new(FetcherManager)
|
fb := new(FetcherManager)
|
||||||
fetcher := fb.Build()
|
fetcher := fb.Build()
|
||||||
newController := controller.NewController()
|
newController := controller.NewController()
|
||||||
@@ -219,14 +219,15 @@ func buildFetcher() fetcher.Fetcher {
|
|||||||
json.Unmarshal([]byte(test.ToJson(fb.DefaultConfig())), v)
|
json.Unmarshal([]byte(test.ToJson(fb.DefaultConfig())), v)
|
||||||
}
|
}
|
||||||
fetcher.Setup(newController)
|
fetcher.Setup(newController)
|
||||||
|
t.Cleanup(func() { fetcher.Close(); closeClient() })
|
||||||
return fetcher
|
return fetcher
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildConfigFetcher(proxyConfig *base.DownloaderProxyConfig) fetcher.Fetcher {
|
func buildConfigFetcher(t testing.TB, proxyConfig *base.DownloaderProxyConfig) fetcher.Fetcher {
|
||||||
fetcher := new(FetcherManager).Build()
|
fetcher := new(FetcherManager).Build()
|
||||||
newController := controller.NewController()
|
newController := controller.NewController()
|
||||||
mockCfg := config{
|
mockCfg := config{
|
||||||
Trackers: []string{
|
CustomTrackers: []string{
|
||||||
"udp://tracker.birkenwald.de:6969/announce",
|
"udp://tracker.birkenwald.de:6969/announce",
|
||||||
"udp://tracker.bitsearch.to:1337/announce",
|
"udp://tracker.bitsearch.to:1337/announce",
|
||||||
}}
|
}}
|
||||||
@@ -237,29 +238,30 @@ func buildConfigFetcher(proxyConfig *base.DownloaderProxyConfig) fetcher.Fetcher
|
|||||||
return proxyConfig.ToHandler()
|
return proxyConfig.ToHandler()
|
||||||
}
|
}
|
||||||
fetcher.Setup(newController)
|
fetcher.Setup(newController)
|
||||||
|
t.Cleanup(func() { fetcher.Close(); closeClient() })
|
||||||
return fetcher
|
return fetcher
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestFetcher_Patch tests the Patch functionality for BT fetcher.
|
// TestFetcher_Patch tests the Patch functionality for BT fetcher.
|
||||||
// It tests modifying selected files after Resolve (without downloading).
|
// It tests modifying selected files after Resolve (without downloading).
|
||||||
func TestFetcher_Patch(t *testing.T) {
|
func TestFetcher_Patch(t *testing.T) {
|
||||||
f := buildFetcher()
|
f := buildFetcher(t)
|
||||||
|
|
||||||
// Resolve a multi-file torrent
|
// Resolve a multi-file torrent
|
||||||
err := f.Resolve(&base.Request{
|
err := f.Resolve(&base.Request{
|
||||||
URL: "./testdata/test.torrent",
|
URL: "./testdata/test.torrent",
|
||||||
}, nil)
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Resolve failed: %v", err)
|
t.Fatalf("Resolve failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify initial state: 3 files, all selected by default
|
// Resolution describes files without choosing download options.
|
||||||
meta := f.Meta()
|
meta := f.Meta()
|
||||||
if len(meta.Res.Files) != 3 {
|
if len(meta.Res.Files) != 3 {
|
||||||
t.Fatalf("Expected 3 files, got %d", len(meta.Res.Files))
|
t.Fatalf("Expected 3 files, got %d", len(meta.Res.Files))
|
||||||
}
|
}
|
||||||
if len(meta.Opts.SelectFiles) != 3 {
|
if meta.Opts != nil {
|
||||||
t.Fatalf("Expected 3 selected files, got %d", len(meta.Opts.SelectFiles))
|
t.Fatal("Resolve should not assign download options")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Total size of all files: 107484864
|
// Total size of all files: 107484864
|
||||||
@@ -366,3 +368,52 @@ func TestFetcher_Patch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFetcher_PeerDiscoveryConfig(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
stored any
|
||||||
|
wantDisableDHT bool
|
||||||
|
wantDisablePEX bool
|
||||||
|
}{
|
||||||
|
{"default config", new(FetcherManager).DefaultConfig(), false, false},
|
||||||
|
{"stored config without the fields", map[string]any{"listenPort": 0, "seedRatio": 1.0}, false, false},
|
||||||
|
{"both disabled", map[string]any{"disableDht": true, "disablePex": true}, true, true},
|
||||||
|
{"only DHT disabled", map[string]any{"disableDht": true}, true, false},
|
||||||
|
{"only PEX disabled", map[string]any{"disablePex": true}, false, true},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
f := buildFetcherWithConfig(tt.stored).(*Fetcher)
|
||||||
|
if f.config.DisableDHT != tt.wantDisableDHT || f.config.DisablePEX != tt.wantDisablePEX {
|
||||||
|
t.Errorf("config got DisableDHT = %v, DisablePEX = %v, want %v, %v", f.config.DisableDHT, f.config.DisablePEX, tt.wantDisableDHT, tt.wantDisablePEX)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFetcher_PeerDiscoveryDisabledOnClient(t *testing.T) {
|
||||||
|
if err := closeClient(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
f := buildFetcherWithConfig(map[string]any{"disableDht": true, "disablePex": true})
|
||||||
|
if err := f.Resolve(&base.Request{URL: "./testdata/test.torrent"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !cfg.NoDHT || !cfg.DisablePEX {
|
||||||
|
t.Errorf("client config got NoDHT = %v, DisablePEX = %v, want true", cfg.NoDHT, cfg.DisablePEX)
|
||||||
|
}
|
||||||
|
if n := len(client.DhtServers()); n != 0 {
|
||||||
|
t.Errorf("client got %d DHT servers, want 0", n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildFetcherWithConfig(stored any) fetcher.Fetcher {
|
||||||
|
fetcher := new(FetcherManager).Build()
|
||||||
|
newController := controller.NewController()
|
||||||
|
newController.GetConfig = func(v any) {
|
||||||
|
json.Unmarshal([]byte(test.ToJson(stored)), v)
|
||||||
|
}
|
||||||
|
fetcher.Setup(newController)
|
||||||
|
return fetcher
|
||||||
|
}
|
||||||
@@ -0,0 +1,212 @@
|
|||||||
|
package bt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/binary"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/anacrolix/torrent"
|
||||||
|
"github.com/anacrolix/torrent/bencode"
|
||||||
|
"github.com/anacrolix/torrent/merkle"
|
||||||
|
"github.com/anacrolix/torrent/metainfo"
|
||||||
|
"golang.org/x/time/rate"
|
||||||
|
)
|
||||||
|
|
||||||
|
// btFixture serves generated, trackerless content over a real loopback TCP peer.
|
||||||
|
// Its HTTP tracker exercises .torrent downloads; magnet links also carry the
|
||||||
|
// explicit x.pe address.
|
||||||
|
type btFixture struct {
|
||||||
|
MetaInfo *metainfo.MetaInfo
|
||||||
|
Magnet string
|
||||||
|
TorrentFile string
|
||||||
|
DataURI string
|
||||||
|
TrackerURL string
|
||||||
|
Files map[string][]byte // Paths relative to the selected download directory.
|
||||||
|
Seeder *torrent.Client
|
||||||
|
Torrent *torrent.Torrent
|
||||||
|
Limiter *rate.Limiter
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBTFixture(t testing.TB, files map[string][]byte, bytesPerSecond int) *btFixture {
|
||||||
|
return makeBTFixture(t, files, bytesPerSecond, false, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBTHybridFixture(t testing.TB, payload []byte) *btFixture {
|
||||||
|
return makeBTFixture(t, map[string][]byte{"": payload}, 0, true, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeBTFixture(t testing.TB, files map[string][]byte, bytesPerSecond int, v2, private bool) *btFixture {
|
||||||
|
t.Helper()
|
||||||
|
source := t.TempDir()
|
||||||
|
name := "payload.bin"
|
||||||
|
if _, single := files[""]; !single {
|
||||||
|
name = "bundle"
|
||||||
|
}
|
||||||
|
root := filepath.Join(source, name)
|
||||||
|
expected := make(map[string][]byte, len(files))
|
||||||
|
for path, data := range files {
|
||||||
|
target := filepath.Join(root, filepath.FromSlash(path))
|
||||||
|
if err := os.MkdirAll(filepath.Dir(target), 0700); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(target, data, 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
expected[filepath.Join(name, filepath.FromSlash(path))] = append([]byte(nil), data...)
|
||||||
|
}
|
||||||
|
info := metainfo.Info{PieceLength: 16 * 1024}
|
||||||
|
if err := info.BuildFromFilePath(root); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var layers map[string]string
|
||||||
|
if v2 {
|
||||||
|
data := files[""]
|
||||||
|
h := merkle.NewHash()
|
||||||
|
h.Write(data)
|
||||||
|
root := h.Sum(nil)
|
||||||
|
var pieceHashes []byte
|
||||||
|
for off := 0; off < len(data); off += int(info.PieceLength) {
|
||||||
|
end := min(off+int(info.PieceLength), len(data))
|
||||||
|
ph := merkle.NewHash()
|
||||||
|
ph.Write(data[off:end])
|
||||||
|
pieceHashes = append(pieceHashes, ph.SumMinLength(nil, int(info.PieceLength))...)
|
||||||
|
}
|
||||||
|
info = metainfo.Info{Name: name, PieceLength: info.PieceLength, Length: int64(len(data)), Pieces: info.Pieces, MetaVersion: 2, FileTree: metainfo.FileTree{File: metainfo.FileTreeFile{Length: int64(len(data)), PiecesRoot: string(root)}}}
|
||||||
|
layers = map[string]string{string(root): string(pieceHashes)}
|
||||||
|
}
|
||||||
|
// Private torrents avoid public DHT discovery once metadata is available.
|
||||||
|
info.Private = &private
|
||||||
|
raw, err := bencode.Marshal(&info)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
mi := &metainfo.MetaInfo{InfoBytes: raw, PieceLayers: layers}
|
||||||
|
cfg := torrent.TestingConfig(t)
|
||||||
|
cfg.DataDir = source
|
||||||
|
cfg.Seed = true
|
||||||
|
cfg.DisableUTP = true
|
||||||
|
cfg.DisableIPv6 = true
|
||||||
|
cfg.DisableWebtorrent = true
|
||||||
|
cfg.KeepAliveTimeout = time.Minute
|
||||||
|
cfg.MaxAllocPeerRequestDataPerConn = 1 << 20
|
||||||
|
limiter := rate.NewLimiter(rate.Inf, 16*1024)
|
||||||
|
if bytesPerSecond > 0 {
|
||||||
|
limiter.SetLimit(rate.Limit(bytesPerSecond))
|
||||||
|
}
|
||||||
|
cfg.UploadRateLimiter = limiter
|
||||||
|
seed, err := torrent.NewClient(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { seed.Close() })
|
||||||
|
seeded, _, err := seed.AddTorrentSpec(torrent.TorrentSpecFromMetaInfo(mi))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := seeded.VerifyDataContext(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-seeded.Complete().On():
|
||||||
|
case <-ctx.Done():
|
||||||
|
t.Fatal("local seeder did not finish verification")
|
||||||
|
}
|
||||||
|
addr := seed.ListenAddrs()[0].String()
|
||||||
|
host, portString, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
resolved, err := net.ResolveTCPAddr("tcp4", net.JoinHostPort(host, portString))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
compact := make([]byte, 6)
|
||||||
|
copy(compact, resolved.IP.To4())
|
||||||
|
binary.BigEndian.PutUint16(compact[4:], uint16(resolved.Port))
|
||||||
|
tracker := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
response, err := bencode.Marshal(map[string]any{"interval": 3600, "peers": string(compact)})
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Write(response)
|
||||||
|
}))
|
||||||
|
t.Cleanup(tracker.Close)
|
||||||
|
// The pinned library has a hybrid tracker-dispatch assertion bug.
|
||||||
|
// Hybrid fixtures use their explicit loopback peer rather than a tracker.
|
||||||
|
if !v2 {
|
||||||
|
mi.Announce = tracker.URL
|
||||||
|
}
|
||||||
|
var encoded bytes.Buffer
|
||||||
|
if err := mi.Write(&encoded); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
torrentFile := filepath.Join(t.TempDir(), "fixture.torrent")
|
||||||
|
if err := os.WriteFile(torrentFile, encoded.Bytes(), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
m, err := mi.MagnetV2()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
magnet := m.String()
|
||||||
|
u, err := url.Parse(magnet)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
q := u.Query()
|
||||||
|
q.Add("x.pe", addr)
|
||||||
|
u.RawQuery = q.Encode()
|
||||||
|
return &btFixture{MetaInfo: mi, Magnet: u.String(), TorrentFile: torrentFile, DataURI: "data:application/x-bittorrent;base64," + base64.StdEncoding.EncodeToString(encoded.Bytes()), TrackerURL: tracker.URL, Files: expected, Seeder: seed, Torrent: seeded, Limiter: limiter}
|
||||||
|
}
|
||||||
|
|
||||||
|
func btPayload(size int, label string) []byte {
|
||||||
|
data := make([]byte, size)
|
||||||
|
for i := range data {
|
||||||
|
data[i] = byte((i*31 + i/251) % 251)
|
||||||
|
}
|
||||||
|
copy(data, label)
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *btFixture) AssertFiles(t testing.TB, dir string, paths ...string) {
|
||||||
|
t.Helper()
|
||||||
|
if len(paths) == 0 {
|
||||||
|
for path := range f.Files {
|
||||||
|
paths = append(paths, path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, path := range paths {
|
||||||
|
got, err := os.ReadFile(filepath.Join(dir, path))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want, ok := f.Files[path]
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("unknown fixture file %q", path)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(got, want) {
|
||||||
|
t.Fatalf("BT file %s differs: received %d bytes, want %d", path, len(got), len(want))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *btFixture) FileURI() string {
|
||||||
|
path := filepath.ToSlash(f.TorrentFile)
|
||||||
|
if !strings.HasPrefix(path, "/") {
|
||||||
|
path = "/" + path
|
||||||
|
}
|
||||||
|
return (&url.URL{Scheme: "file", Path: path}).String()
|
||||||
|
}
|
||||||
@@ -0,0 +1,367 @@
|
|||||||
|
package bt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/sha1"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
"github.com/anacrolix/torrent"
|
||||||
|
"github.com/anacrolix/torrent/bencode"
|
||||||
|
"github.com/anacrolix/torrent/metainfo"
|
||||||
|
"github.com/anacrolix/torrent/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStartDoesNotPublishUnverifiedCompletion(t *testing.T) {
|
||||||
|
cfg := torrent.TestingConfig(t)
|
||||||
|
cfg.DisableTCP = true
|
||||||
|
cfg.DisableUTP = true
|
||||||
|
c, err := torrent.NewClient(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer c.Close()
|
||||||
|
payload := btPayload(32*1024, "unverified")
|
||||||
|
info := metainfo.Info{Name: "payload.bin", Length: int64(len(payload)), PieceLength: 16 * 1024}
|
||||||
|
for offset := 0; offset < len(payload); offset += int(info.PieceLength) {
|
||||||
|
hash := sha1.Sum(payload[offset : offset+int(info.PieceLength)])
|
||||||
|
info.Pieces = append(info.Pieces, hash[:]...)
|
||||||
|
}
|
||||||
|
raw, err := bencode.Marshal(info)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
store := &deferredFileStorage{}
|
||||||
|
spec := torrent.TorrentSpecFromMetaInfo(&metainfo.MetaInfo{InfoBytes: raw})
|
||||||
|
spec.Storage = store
|
||||||
|
tor, _, err := c.AddTorrentSpec(spec)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
tor.DisallowDataDownload()
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
f := &Fetcher{torrent: tor, storage: store, meta: &fetcher.FetcherMeta{}, data: &fetcherData{}, torrentDropCtx: ctx, torrentDropFunc: cancel, config: &config{SeedKeep: true}}
|
||||||
|
f.torrentReady.Store(true)
|
||||||
|
f.updateRes()
|
||||||
|
dir := t.TempDir()
|
||||||
|
payload[20000] ^= 0xff
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, info.Name), payload, 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
f.meta.Opts = &base.Options{Path: dir}
|
||||||
|
// Retain the original event channel to detect even a transient completion.
|
||||||
|
// With no network transports, the damaged piece cannot have been repaired.
|
||||||
|
completed := tor.Complete().On()
|
||||||
|
if err := f.Start(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-completed:
|
||||||
|
t.Fatal("Start published completion before verifying the existing file")
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStartUsesSelectionSubmittedAfterResolve(t *testing.T) {
|
||||||
|
cfg := torrent.TestingConfig(t)
|
||||||
|
cfg.DataDir = t.TempDir()
|
||||||
|
cfg.DisableTCP = true
|
||||||
|
cfg.DisableUTP = true
|
||||||
|
c, err := torrent.NewClient(cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer c.Close()
|
||||||
|
store := &deferredFileStorage{}
|
||||||
|
mi, err := metainfo.LoadFromFile("testdata/test.torrent")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
spec := torrent.TorrentSpecFromMetaInfo(mi)
|
||||||
|
spec.Storage = store
|
||||||
|
tor, _, err := c.AddTorrentSpec(spec)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
tor.DisallowDataDownload()
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
f := &Fetcher{torrent: tor, storage: store, meta: &fetcher.FetcherMeta{}, data: &fetcherData{}, torrentDropCtx: ctx, torrentDropFunc: cancel, config: &config{SeedKeep: true}}
|
||||||
|
f.torrentReady.Store(true)
|
||||||
|
f.updateRes()
|
||||||
|
f.meta.Opts = &base.Options{Path: t.TempDir(), SelectFiles: []int{2}}
|
||||||
|
if err := f.Start(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for i, file := range tor.Files() {
|
||||||
|
want := torrent.PiecePriorityNone
|
||||||
|
if i == 2 {
|
||||||
|
want = torrent.PiecePriorityNormal
|
||||||
|
}
|
||||||
|
if file.Priority() != want {
|
||||||
|
t.Errorf("file %d priority = %v, want %v", i, file.Priority(), want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(f.data.Progress) != 1 {
|
||||||
|
t.Fatalf("progress tracks %d files, want 1", len(f.data.Progress))
|
||||||
|
}
|
||||||
|
if err := f.Pause(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := f.Start(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if tor.Files()[0].Priority() != torrent.PiecePriorityNone {
|
||||||
|
t.Fatal("resume selected an unselected file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWaitCleansUnselectedFilesFromSharedPiece(t *testing.T) {
|
||||||
|
for _, mode := range []string{"partial", "part only", "both paths", "all", "already removed", "remove error"} {
|
||||||
|
t.Run(mode, func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
keepPath := filepath.Join(dir, "bundle", "keep.txt")
|
||||||
|
skipPath := filepath.Join(dir, "bundle", "nested", "skip.txt")
|
||||||
|
if err := os.MkdirAll(filepath.Dir(skipPath), 0755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(keepPath, []byte("keep"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(skipPath, []byte("skip"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Both files occupy the same piece, so downloading keep.txt also writes skip.txt.
|
||||||
|
hash := sha1.Sum([]byte("keepskip"))
|
||||||
|
info := metainfo.Info{Name: "bundle", PieceLength: 16, Pieces: hash[:], Files: []metainfo.FileInfo{
|
||||||
|
{Length: 4, Path: []string{"keep.txt"}},
|
||||||
|
{Length: 4, Path: []string{"nested", "skip.txt"}},
|
||||||
|
}}
|
||||||
|
storageOpts := storage.NewFileClientOpts{
|
||||||
|
ClientBaseDir: dir,
|
||||||
|
TorrentDirMaker: func(baseDir string, _ *metainfo.Info, _ metainfo.Hash) string { return baseDir },
|
||||||
|
}
|
||||||
|
// VerifyDataContext can return before storage finishes marking the piece
|
||||||
|
// complete. Disable automatic part-file promotion so it cannot rename
|
||||||
|
// the .part fixtures that this test creates to exercise cleanup.
|
||||||
|
storageOpts.UsePartFiles.Set(false)
|
||||||
|
store := storage.NewFileOpts(storageOpts)
|
||||||
|
defer store.Close()
|
||||||
|
config := torrent.TestingConfig(t)
|
||||||
|
config.DisableTCP = true
|
||||||
|
config.DisableUTP = true
|
||||||
|
c, err := torrent.NewClient(config)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer c.Close()
|
||||||
|
spec, err := torrent.TorrentSpecFromMetaInfoErr(&metainfo.MetaInfo{InfoBytes: bencode.MustMarshal(&info)})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
spec.Storage = store
|
||||||
|
tor, _, err := c.AddTorrentSpec(spec)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := tor.VerifyDataContext(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
f := &Fetcher{torrent: tor, meta: &fetcher.FetcherMeta{Opts: &base.Options{Path: dir, SelectFiles: []int{0}}}, data: &fetcherData{}, torrentDropCtx: ctx}
|
||||||
|
f.torrentReady.Store(true)
|
||||||
|
f.updateRes()
|
||||||
|
if mode == "all" {
|
||||||
|
f.meta.Opts.SelectFiles = []int{0, 1}
|
||||||
|
}
|
||||||
|
if !f.isDone() {
|
||||||
|
t.Fatal("selected file is not complete")
|
||||||
|
}
|
||||||
|
if mode == "part only" || mode == "both paths" || mode == "all" {
|
||||||
|
if err := os.WriteFile(skipPath+".part", []byte("skip"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if mode == "part only" || mode == "already removed" || mode == "remove error" {
|
||||||
|
if err := os.Remove(skipPath); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if mode == "remove error" {
|
||||||
|
if err := os.Mkdir(skipPath, 0755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(skipPath, "child"), []byte("preserve"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = f.Wait()
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
t.Fatal(ctx.Err())
|
||||||
|
}
|
||||||
|
if mode == "remove error" {
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("cleanup failure was silently ignored")
|
||||||
|
}
|
||||||
|
} else if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
data, err := os.ReadFile(keepPath)
|
||||||
|
if err != nil || string(data) != "keep" {
|
||||||
|
t.Fatalf("selected file changed: %q, %v", data, err)
|
||||||
|
}
|
||||||
|
if mode == "all" {
|
||||||
|
if _, err := os.Stat(skipPath + ".part"); err != nil {
|
||||||
|
t.Fatalf("selected part file was removed: %v", err)
|
||||||
|
}
|
||||||
|
data, err := os.ReadFile(skipPath)
|
||||||
|
if err != nil || string(data) != "skip" {
|
||||||
|
t.Fatalf("selected file changed: %q, %v", data, err)
|
||||||
|
}
|
||||||
|
} else if mode != "remove error" {
|
||||||
|
for _, removed := range []string{skipPath, skipPath + ".part", filepath.Dir(skipPath)} {
|
||||||
|
if _, err := os.Stat(removed); !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("unselected file or empty directory remains: %s: %v", removed, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRemoveUnselectedFilePrunesOnlyEmptyParents(t *testing.T) {
|
||||||
|
for _, preserveSibling := range []bool{false, true} {
|
||||||
|
t.Run(fmt.Sprint(preserveSibling), func(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
relative := filepath.Join("bundle", "nested", "deep", "skip.txt")
|
||||||
|
name := filepath.Join(root, relative)
|
||||||
|
if err := os.MkdirAll(filepath.Dir(name), 0755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(name+".part", []byte("partial"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
sibling := filepath.Join(root, "bundle", "keep.txt")
|
||||||
|
if preserveSibling {
|
||||||
|
if err := os.WriteFile(sibling, []byte("keep"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := removeUnselectedFile(root, "bundle", relative); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(root, "bundle", "nested")); !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("empty ancestors remain: %v", err)
|
||||||
|
}
|
||||||
|
if preserveSibling {
|
||||||
|
data, err := os.ReadFile(sibling)
|
||||||
|
if err != nil || string(data) != "keep" {
|
||||||
|
t.Fatalf("sibling changed: %q, %v", data, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if info, err := os.Stat(filepath.Join(root, "bundle")); err != nil || !info.IsDir() {
|
||||||
|
t.Fatalf("torrent root directory was removed: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(root); err != nil {
|
||||||
|
t.Fatalf("download directory was removed: %v", err)
|
||||||
|
}
|
||||||
|
// A file directly inside the torrent root must not cause that root to be pruned.
|
||||||
|
rootFile := filepath.Join("bundle", "root.txt")
|
||||||
|
if err := os.WriteFile(filepath.Join(root, rootFile)+".part", []byte("partial"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := removeUnselectedFile(root, "bundle", rootFile); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(root, "bundle")); err != nil {
|
||||||
|
t.Fatalf("torrent root directory was removed after root-level cleanup: %v", err)
|
||||||
|
}
|
||||||
|
// Repeating cleanup after the file and its subdirectories are gone is harmless.
|
||||||
|
if err := removeUnselectedFile(root, "bundle", relative); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCleanupMatchesNamedAndRootlessStorage(t *testing.T) {
|
||||||
|
for _, version := range []int64{1, 2} {
|
||||||
|
for _, layout := range []string{"rootless", "wrapped", "file-tree-root"} {
|
||||||
|
if version == 1 && layout == "file-tree-root" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
t.Run(fmt.Sprintf("v%d/%s", version, layout), func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
info := metainfo.Info{Name: metainfo.NoName, PieceLength: 16384}
|
||||||
|
root := dir
|
||||||
|
if layout == "wrapped" {
|
||||||
|
info.Name = "bundle"
|
||||||
|
root = filepath.Join(dir, "bundle")
|
||||||
|
}
|
||||||
|
if version == 1 {
|
||||||
|
info.Files = []metainfo.FileInfo{{Path: []string{"keep.txt"}}, {Path: []string{"nested", "deep", "skip.txt"}}}
|
||||||
|
} else {
|
||||||
|
info.MetaVersion = 2
|
||||||
|
info.FileTree = metainfo.FileTree{Dir: map[string]metainfo.FileTree{
|
||||||
|
"keep.txt": {},
|
||||||
|
"nested": {Dir: map[string]metainfo.FileTree{"deep": {Dir: map[string]metainfo.FileTree{"skip.txt": {}}}}},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
if layout == "file-tree-root" {
|
||||||
|
info.FileTree = metainfo.FileTree{Dir: map[string]metainfo.FileTree{"tree-root": info.FileTree}}
|
||||||
|
root = filepath.Join(dir, "tree-root")
|
||||||
|
}
|
||||||
|
// Keep the library's default FilePathMaker here to independently verify
|
||||||
|
// that cleanup agrees with its real disk layout, including NoName.
|
||||||
|
store := storage.NewFileOpts(storage.NewFileClientOpts{
|
||||||
|
ClientBaseDir: dir,
|
||||||
|
TorrentDirMaker: func(baseDir string, _ *metainfo.Info, _ metainfo.Hash) string { return baseDir },
|
||||||
|
})
|
||||||
|
defer store.Close()
|
||||||
|
torrentStorage, err := store.OpenTorrent(context.Background(), &info, metainfo.Hash{1})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer torrentStorage.Close()
|
||||||
|
skip := filepath.Join(root, "nested", "deep", "skip.txt")
|
||||||
|
if _, err := os.Stat(skip); err != nil {
|
||||||
|
t.Fatalf("unexpected storage layout: %v", err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(skip+".part", []byte("partial"), 0644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
files := info.UpvertedFiles()
|
||||||
|
boundary, name := torrentFileLayout(&info, files[1])
|
||||||
|
if err := removeUnselectedFile(dir, boundary, name); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(root, "nested")); !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("empty subtree remains: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(root, "keep.txt")); err != nil {
|
||||||
|
t.Fatalf("selected file removed: %v", err)
|
||||||
|
}
|
||||||
|
// Also verify the boundary when the protected directory becomes empty.
|
||||||
|
if err := os.Remove(filepath.Join(root, "keep.txt")); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, file := range files {
|
||||||
|
boundary, name := torrentFileLayout(&info, file)
|
||||||
|
if err := removeUnselectedFile(dir, boundary, name); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if stat, err := os.Stat(root); err != nil || !stat.IsDir() {
|
||||||
|
t.Fatalf("cleanup boundary was removed: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
package bt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
g "github.com/anacrolix/generics"
|
||||||
|
"github.com/anacrolix/torrent/metainfo"
|
||||||
|
"github.com/anacrolix/torrent/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
// deferredFileStorage lets metadata resolve without opening the user's files.
|
||||||
|
// It binds once. After binding, pieces are supplied directly by file storage.
|
||||||
|
type deferredFileStorage struct {
|
||||||
|
mu sync.RWMutex
|
||||||
|
ctx context.Context
|
||||||
|
info *metainfo.Info
|
||||||
|
hash metainfo.Hash
|
||||||
|
knownHashes []bool
|
||||||
|
owner storage.ClientImplCloser
|
||||||
|
torrent storage.TorrentImpl
|
||||||
|
closed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *deferredFileStorage) OpenTorrent(ctx context.Context, info *metainfo.Info, hash metainfo.Hash) (storage.TorrentImpl, error) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if s.closed || s.info != nil {
|
||||||
|
return storage.TorrentImpl{}, errors.New("torrent storage already opened or closed")
|
||||||
|
}
|
||||||
|
s.ctx, s.info, s.hash = ctx, info, hash
|
||||||
|
s.knownHashes = make([]bool, info.NumPieces())
|
||||||
|
return storage.TorrentImpl{
|
||||||
|
PieceWithHash: func(p metainfo.Piece, hash g.Option[[]byte]) storage.PieceImpl {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if s.closed {
|
||||||
|
return metadataPiece{err: os.ErrClosed}
|
||||||
|
}
|
||||||
|
if s.owner == nil {
|
||||||
|
s.knownHashes[p.Index()] = hash.Ok
|
||||||
|
// A missing v2 hash must remain unknown until its arrival changes
|
||||||
|
// completion and updates the library's piece request priorities.
|
||||||
|
return metadataPiece{known: hash.Ok}
|
||||||
|
}
|
||||||
|
if s.torrent.PieceWithHash != nil {
|
||||||
|
return s.torrent.PieceWithHash(p, hash)
|
||||||
|
}
|
||||||
|
return s.torrent.Piece(p)
|
||||||
|
},
|
||||||
|
Close: s.Close,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *deferredFileStorage) bind(opts storage.NewFileClientOpts) ([]bool, error) {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if s.closed {
|
||||||
|
return nil, os.ErrClosed
|
||||||
|
}
|
||||||
|
if s.info == nil {
|
||||||
|
return nil, errors.New("torrent metadata is not ready")
|
||||||
|
}
|
||||||
|
if s.owner != nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if opts.FilePathMaker == nil {
|
||||||
|
opts.FilePathMaker = func(o storage.FilePathMakerOpts) string { _, name := torrentFileLayout(o.Info, *o.File); return name }
|
||||||
|
}
|
||||||
|
owner := storage.NewFileOpts(opts)
|
||||||
|
t, err := owner.OpenTorrent(s.ctx, s.info, s.hash)
|
||||||
|
if err != nil {
|
||||||
|
owner.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.owner, s.torrent = owner, t
|
||||||
|
return append([]bool(nil), s.knownHashes...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *deferredFileStorage) Close() error {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
if s.closed {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
s.closed = true
|
||||||
|
if s.owner == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
if s.torrent.Close != nil {
|
||||||
|
err = s.torrent.Close()
|
||||||
|
}
|
||||||
|
return errors.Join(err, s.owner.Close())
|
||||||
|
}
|
||||||
|
|
||||||
|
type metadataPiece struct {
|
||||||
|
known bool
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p metadataPiece) Completion() storage.Completion {
|
||||||
|
return storage.Completion{Ok: p.known, Err: p.err}
|
||||||
|
}
|
||||||
|
func (p metadataPiece) ReadAt([]byte, int64) (int, error) {
|
||||||
|
return 0, errors.New("download storage is not initialized")
|
||||||
|
}
|
||||||
|
func (p metadataPiece) WriteAt([]byte, int64) (int, error) {
|
||||||
|
return 0, errors.New("download storage is not initialized")
|
||||||
|
}
|
||||||
|
func (p metadataPiece) MarkComplete() error { return errors.New("download storage is not initialized") }
|
||||||
|
func (p metadataPiece) MarkNotComplete() error {
|
||||||
|
return errors.New("download storage is not initialized")
|
||||||
|
}
|
||||||
@@ -0,0 +1,234 @@
|
|||||||
|
package bt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
|
)
|
||||||
|
|
||||||
|
var defaultTrackerSubscribeURLs = []string{
|
||||||
|
"https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all.txt",
|
||||||
|
"https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all_http.txt",
|
||||||
|
"https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all_https.txt",
|
||||||
|
"https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all_ip.txt",
|
||||||
|
"https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all_udp.txt",
|
||||||
|
"https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all_ws.txt",
|
||||||
|
"https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best.txt",
|
||||||
|
"https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best_ip.txt",
|
||||||
|
"https://raw.githubusercontent.com/XIU2/TrackersListCollection/master/all.txt",
|
||||||
|
"https://raw.githubusercontent.com/XIU2/TrackersListCollection/master/best.txt",
|
||||||
|
"https://raw.githubusercontent.com/XIU2/TrackersListCollection/master/http.txt",
|
||||||
|
}
|
||||||
|
|
||||||
|
func validTracker(raw string) bool {
|
||||||
|
u, err := url.Parse(raw)
|
||||||
|
if err != nil || u.Hostname() == "" || u.User != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
switch u.Scheme {
|
||||||
|
case "udp", "http", "https", "ws", "wss":
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
func uniqueTrackers(groups ...[]string) []string {
|
||||||
|
result := []string{}
|
||||||
|
seen := map[string]bool{}
|
||||||
|
for _, group := range groups {
|
||||||
|
for _, raw := range group {
|
||||||
|
value := strings.TrimSpace(raw)
|
||||||
|
if value != "" && !seen[value] {
|
||||||
|
seen[value] = true
|
||||||
|
result = append(result, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// effectiveTrackers derives the client list without persisting duplicate lists.
|
||||||
|
func (c *config) effectiveTrackers() []string {
|
||||||
|
groups := [][]string{c.CustomTrackers}
|
||||||
|
for _, source := range c.TrackerSubscribeURLs {
|
||||||
|
groups = append(groups, c.State.TrackerSources[source])
|
||||||
|
}
|
||||||
|
return uniqueTrackers(groups...)
|
||||||
|
}
|
||||||
|
func (fm *FetcherManager) NormalizeConfig(raw json.RawMessage) (any, error) {
|
||||||
|
c := fm.DefaultConfig().(*config)
|
||||||
|
if err := json.Unmarshal(raw, c); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if c.ListenPort < 0 || c.ListenPort > 65535 || c.SeedRatio < 0 || c.SeedTime < 0 {
|
||||||
|
return nil, errors.New("invalid BT configuration")
|
||||||
|
}
|
||||||
|
if len(c.TrackerSubscribeURLs) > 64 {
|
||||||
|
return nil, errors.New("too many tracker subscriptions")
|
||||||
|
}
|
||||||
|
c.TrackerSubscribeURLs = uniqueTrackers(c.TrackerSubscribeURLs)
|
||||||
|
for _, source := range c.TrackerSubscribeURLs {
|
||||||
|
u, err := url.Parse(source)
|
||||||
|
if err != nil || u == nil || (u.Scheme != "https" && u.Scheme != "http") || u.Hostname() == "" || u.User != nil {
|
||||||
|
return nil, errors.New("invalid tracker subscription URL")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.CustomTrackers = uniqueTrackers(c.CustomTrackers)
|
||||||
|
for _, tracker := range c.CustomTrackers {
|
||||||
|
if !validTracker(tracker) {
|
||||||
|
return nil, fmt.Errorf("invalid tracker URL: %s", tracker)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if c.State.TrackerSources == nil {
|
||||||
|
c.State.TrackerSources = map[string][]string{}
|
||||||
|
}
|
||||||
|
for source := range c.State.TrackerSources {
|
||||||
|
if !slices.Contains(c.TrackerSubscribeURLs, source) {
|
||||||
|
delete(c.State.TrackerSources, source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if c.State.TrackerSubscriptionStatuses == nil {
|
||||||
|
c.State.TrackerSubscriptionStatuses = map[string]trackerSubscriptionStatus{}
|
||||||
|
}
|
||||||
|
for source := range c.State.TrackerSubscriptionStatuses {
|
||||||
|
if !slices.Contains(c.TrackerSubscribeURLs, source) {
|
||||||
|
delete(c.State.TrackerSubscriptionStatuses, source)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
func (fm *FetcherManager) CronJobs(host fetcher.ProtocolHost) []fetcher.CronJob {
|
||||||
|
return []fetcher.CronJob{{
|
||||||
|
Name: "update-trackers",
|
||||||
|
Every: 24 * time.Hour,
|
||||||
|
Timeout: 90 * time.Second,
|
||||||
|
Run: func(ctx context.Context, source fetcher.TriggerSource) error {
|
||||||
|
return fm.updateTrackers(ctx, host, source)
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
func parseTrackers(data []byte) ([]string, error) {
|
||||||
|
scanner := bufio.NewScanner(bytes.NewReader(data))
|
||||||
|
scanner.Buffer(make([]byte, 4096), 64*1024)
|
||||||
|
result := []string{}
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := strings.TrimSpace(strings.TrimPrefix(scanner.Text(), "\ufeff"))
|
||||||
|
if validTracker(line) {
|
||||||
|
result = append(result, line)
|
||||||
|
}
|
||||||
|
if len(result) > 20000 {
|
||||||
|
return nil, errors.New("too many trackers")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(result) == 0 {
|
||||||
|
return nil, errors.New("subscription contains no valid trackers")
|
||||||
|
}
|
||||||
|
return uniqueTrackers(result), nil
|
||||||
|
}
|
||||||
|
func (fm *FetcherManager) updateTrackers(ctx context.Context, host fetcher.ProtocolHost, source fetcher.TriggerSource) error {
|
||||||
|
snapshot, err := fetcher.ReadConfig[config](host)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if (source == fetcher.TriggerScheduled && !snapshot.AutoUpdateTrackers) || len(snapshot.TrackerSubscribeURLs) == 0 {
|
||||||
|
return fetcher.ErrJobSkipped
|
||||||
|
}
|
||||||
|
fetched := map[string][]string{}
|
||||||
|
statuses := map[string]trackerSubscriptionStatus{}
|
||||||
|
var failures []error
|
||||||
|
var mu sync.Mutex
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
slots := make(chan struct{}, 4)
|
||||||
|
for _, address := range snapshot.TrackerSubscribeURLs {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(address string) {
|
||||||
|
defer wg.Done()
|
||||||
|
select {
|
||||||
|
case slots <- struct{}{}:
|
||||||
|
defer func() { <-slots }()
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
data, err := host.Fetch(ctx, address, 2<<20, func(body []byte) error { _, err := parseTrackers(body); return err })
|
||||||
|
var trackers []string
|
||||||
|
if err == nil {
|
||||||
|
trackers, err = parseTrackers(data)
|
||||||
|
}
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
statuses[address] = trackerSubscriptionStatus{Success: err == nil, TrackerCount: len(trackers), LastFetchTime: time.Now().UTC()}
|
||||||
|
if err != nil {
|
||||||
|
failures = append(failures, fmt.Errorf("subscription %s: %w", address, err))
|
||||||
|
} else {
|
||||||
|
fetched[address] = trackers
|
||||||
|
}
|
||||||
|
}(address)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
failures = append(failures, err)
|
||||||
|
}
|
||||||
|
if len(statuses) == 0 {
|
||||||
|
return errors.Join(failures...)
|
||||||
|
}
|
||||||
|
err = fetcher.UpdateState[config, state](host, func(current *config, state *state) error {
|
||||||
|
if !slices.Equal(current.TrackerSubscribeURLs, snapshot.TrackerSubscribeURLs) {
|
||||||
|
return fetcher.ErrConfigChanged
|
||||||
|
}
|
||||||
|
if state.TrackerSources == nil {
|
||||||
|
state.TrackerSources = map[string][]string{}
|
||||||
|
}
|
||||||
|
if state.TrackerSubscriptionStatuses == nil {
|
||||||
|
state.TrackerSubscriptionStatuses = map[string]trackerSubscriptionStatus{}
|
||||||
|
}
|
||||||
|
for address, status := range statuses {
|
||||||
|
state.TrackerSubscriptionStatuses[address] = status
|
||||||
|
}
|
||||||
|
for address, trackers := range fetched {
|
||||||
|
state.TrackerSources[address] = trackers
|
||||||
|
}
|
||||||
|
if len(failures) == 0 {
|
||||||
|
state.LastTrackerUpdateTime = time.Now().UTC()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
current, err := fetcher.ReadConfig[config](host)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fm.applyTrackers(current.effectiveTrackers())
|
||||||
|
return errors.Join(failures...)
|
||||||
|
}
|
||||||
|
func (fm *FetcherManager) applyTrackers(trackers []string) {
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
if client == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
announce := make([][]string, 0, len(trackers))
|
||||||
|
for _, tracker := range trackers {
|
||||||
|
announce = append(announce, []string{tracker})
|
||||||
|
}
|
||||||
|
for _, t := range client.Torrents() {
|
||||||
|
info := t.Info()
|
||||||
|
if info == nil || (info.Private != nil && *info.Private) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
t.AddTrackers(announce)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,217 @@
|
|||||||
|
package bt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"reflect"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
|
)
|
||||||
|
|
||||||
|
func trackerTestHost(t *testing.T, c *config, fetch func(context.Context, string, int64, func([]byte) error) ([]byte, error)) (fetcher.ProtocolHost, *sync.Mutex) {
|
||||||
|
t.Helper()
|
||||||
|
mu := &sync.Mutex{}
|
||||||
|
return fetcher.ProtocolHost{
|
||||||
|
Read: func(dst any) error {
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
raw, err := json.Marshal(c)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return json.Unmarshal(raw, dst)
|
||||||
|
},
|
||||||
|
UpdateState: func(change func(config, state json.RawMessage) (any, error)) error {
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
raw, _ := json.Marshal(c)
|
||||||
|
currentState, _ := json.Marshal(c.State)
|
||||||
|
next, err := change(raw, currentState)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
raw, err = json.Marshal(next)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return json.Unmarshal(raw, &c.State)
|
||||||
|
},
|
||||||
|
Fetch: fetch,
|
||||||
|
}, mu
|
||||||
|
}
|
||||||
|
func TestTrackerRefreshPartialSuccessUsesLatestConfig(t *testing.T) {
|
||||||
|
fm := &FetcherManager{}
|
||||||
|
cfg := fm.DefaultConfig().(*config)
|
||||||
|
cfg.TrackerSubscribeURLs = []string{"https://a.example/list", "https://b.example/list"}
|
||||||
|
cfg.CustomTrackers = []string{"udp://custom.example:80/announce"}
|
||||||
|
cfg.State.TrackerSources = map[string][]string{cfg.TrackerSubscribeURLs[0]: {"udp://old.example:80/announce"}, cfg.TrackerSubscribeURLs[1]: {"udp://cached.example:80/announce"}}
|
||||||
|
host, mu := trackerTestHost(t, cfg, nil)
|
||||||
|
host.Fetch = func(ctx context.Context, address string, _ int64, validate func([]byte) error) ([]byte, error) {
|
||||||
|
if address == "https://b.example/list" {
|
||||||
|
return nil, errors.New("offline")
|
||||||
|
}
|
||||||
|
// Simulate a frontend update while the network request is in flight.
|
||||||
|
mu.Lock()
|
||||||
|
cfg.CustomTrackers = []string{"udp://new-custom.example:80/announce"}
|
||||||
|
mu.Unlock()
|
||||||
|
data := []byte("udp://fresh.example:80/announce\n# ignored\nudp://fresh.example:80/announce\n")
|
||||||
|
return data, validate(data)
|
||||||
|
}
|
||||||
|
err := fm.updateTrackers(context.Background(), host, fetcher.TriggerManual)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("partial failure must be reported")
|
||||||
|
}
|
||||||
|
want := []string{"udp://new-custom.example:80/announce", "udp://fresh.example:80/announce", "udp://cached.example:80/announce"}
|
||||||
|
if !reflect.DeepEqual(cfg.effectiveTrackers(), want) {
|
||||||
|
t.Fatalf("got %v", cfg.effectiveTrackers())
|
||||||
|
}
|
||||||
|
success := cfg.State.TrackerSubscriptionStatuses["https://a.example/list"]
|
||||||
|
failure := cfg.State.TrackerSubscriptionStatuses["https://b.example/list"]
|
||||||
|
if !success.Success || success.TrackerCount != 1 || success.LastFetchTime.IsZero() || failure.Success || failure.TrackerCount != 0 || failure.LastFetchTime.IsZero() {
|
||||||
|
t.Fatalf("unexpected subscription statuses: %+v", cfg.State.TrackerSubscriptionStatuses)
|
||||||
|
}
|
||||||
|
if !cfg.State.LastTrackerUpdateTime.IsZero() {
|
||||||
|
t.Fatal("partial refresh marked fully successful")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestTrackerRefreshDiscardsChangedSubscriptions(t *testing.T) {
|
||||||
|
fm := &FetcherManager{}
|
||||||
|
cfg := fm.DefaultConfig().(*config)
|
||||||
|
cfg.TrackerSubscribeURLs = []string{"https://a.example/list"}
|
||||||
|
host, mu := trackerTestHost(t, cfg, nil)
|
||||||
|
host.Fetch = func(context.Context, string, int64, func([]byte) error) ([]byte, error) {
|
||||||
|
mu.Lock()
|
||||||
|
cfg.TrackerSubscribeURLs = []string{}
|
||||||
|
mu.Unlock()
|
||||||
|
return []byte("udp://fresh.example:80/announce"), nil
|
||||||
|
}
|
||||||
|
if err := fm.updateTrackers(context.Background(), host, fetcher.TriggerManual); !errors.Is(err, fetcher.ErrConfigChanged) {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(cfg.State.TrackerSources) != 0 {
|
||||||
|
t.Fatal("outdated source was committed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestTrackerRefreshManualWorksWhenAutomaticDisabled(t *testing.T) {
|
||||||
|
fm := &FetcherManager{}
|
||||||
|
cfg := fm.DefaultConfig().(*config)
|
||||||
|
cfg.AutoUpdateTrackers = false
|
||||||
|
cfg.TrackerSubscribeURLs = []string{"https://a.example/list"}
|
||||||
|
calls := 0
|
||||||
|
host, _ := trackerTestHost(t, cfg, func(context.Context, string, int64, func([]byte) error) ([]byte, error) {
|
||||||
|
calls++
|
||||||
|
return []byte("udp://fresh.example:80/announce"), nil
|
||||||
|
})
|
||||||
|
if err := fm.updateTrackers(context.Background(), host, fetcher.TriggerScheduled); !errors.Is(err, fetcher.ErrJobSkipped) {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if calls != 0 {
|
||||||
|
t.Fatal("scheduled job fetched while disabled")
|
||||||
|
}
|
||||||
|
if err := fm.updateTrackers(context.Background(), host, fetcher.TriggerManual); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if calls != 1 || cfg.State.LastTrackerUpdateTime.IsZero() {
|
||||||
|
t.Fatal("manual refresh did not succeed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestTrackerConfigPreservesExplicitSettings(t *testing.T) {
|
||||||
|
value, err := (&FetcherManager{}).NormalizeConfig(json.RawMessage(`{
|
||||||
|
"listenPort":6881, "trackerSubscribeUrls":[], "autoUpdateTrackers":false,
|
||||||
|
"customTrackers":["udp://custom.example:80/announce"]
|
||||||
|
}`))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
cfg := value.(*config)
|
||||||
|
if cfg.AutoUpdateTrackers || len(cfg.TrackerSubscribeURLs) != 0 || cfg.ListenPort != 6881 {
|
||||||
|
t.Fatalf("%+v", cfg)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(cfg.effectiveTrackers(), cfg.CustomTrackers) {
|
||||||
|
t.Fatalf("derived trackers: %v", cfg.effectiveTrackers())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseTrackerList(t *testing.T) {
|
||||||
|
got, err := parseTrackers([]byte("\ufeffudp://tracker.example:80/announce\r\n\n<html>error</html>\nhttps://tracker.example/announce\n"))
|
||||||
|
if err != nil || len(got) != 2 {
|
||||||
|
t.Fatalf("%v %v", got, err)
|
||||||
|
}
|
||||||
|
if _, err := parseTrackers([]byte("<html>mirror error</html>")); err == nil {
|
||||||
|
t.Fatal("invalid mirror body accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestTrackerDefaultsRegisterDailyJob(t *testing.T) {
|
||||||
|
jobs := (&FetcherManager{}).CronJobs(fetcher.ProtocolHost{})
|
||||||
|
if len(jobs) != 1 || jobs[0].Name != "update-trackers" || jobs[0].Every != 24*time.Hour {
|
||||||
|
t.Fatal(jobs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrackerRefreshAllFailuresPersistStatusesAndKeepCache(t *testing.T) {
|
||||||
|
fm := &FetcherManager{}
|
||||||
|
cfg := fm.DefaultConfig().(*config)
|
||||||
|
address := "https://custom.example/trackers.txt"
|
||||||
|
cfg.TrackerSubscribeURLs = []string{address}
|
||||||
|
cfg.State.TrackerSources[address] = []string{"udp://cached.example:80/announce"}
|
||||||
|
cfg.State.TrackerSubscriptionStatuses[address] = trackerSubscriptionStatus{Success: true, TrackerCount: 1, LastFetchTime: time.Now().Add(-time.Hour)}
|
||||||
|
host, _ := trackerTestHost(t, cfg, func(context.Context, string, int64, func([]byte) error) ([]byte, error) {
|
||||||
|
return nil, errors.New("offline")
|
||||||
|
})
|
||||||
|
before := time.Now()
|
||||||
|
if err := fm.updateTrackers(context.Background(), host, fetcher.TriggerManual); err == nil {
|
||||||
|
t.Fatal("failure was not reported")
|
||||||
|
}
|
||||||
|
status, ok := cfg.State.TrackerSubscriptionStatuses[address]
|
||||||
|
if !ok || status.Success || status.TrackerCount != 0 || status.LastFetchTime.Before(before) {
|
||||||
|
t.Fatalf("failed fetch status not stored: %+v", status)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(cfg.State.TrackerSources[address], []string{"udp://cached.example:80/announce"}) {
|
||||||
|
t.Fatal("last good cache lost")
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(cfg.effectiveTrackers(), cfg.State.TrackerSources[address]) {
|
||||||
|
t.Fatal("cached trackers not retained")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrackerConfigPrunesRemovedSubscriptions(t *testing.T) {
|
||||||
|
fm := &FetcherManager{}
|
||||||
|
cfg := fm.DefaultConfig().(*config)
|
||||||
|
address := "https://custom.example/trackers.txt"
|
||||||
|
cfg.TrackerSubscribeURLs = []string{}
|
||||||
|
cfg.State.TrackerSources[address] = []string{"udp://cached.example:80/announce"}
|
||||||
|
cfg.State.TrackerSubscriptionStatuses[address] = trackerSubscriptionStatus{Success: true, TrackerCount: 1, LastFetchTime: time.Now()}
|
||||||
|
raw, _ := json.Marshal(cfg)
|
||||||
|
value, err := fm.NormalizeConfig(raw)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
next := value.(*config)
|
||||||
|
if len(next.State.TrackerSources) != 0 || len(next.State.TrackerSubscriptionStatuses) != 0 || len(next.effectiveTrackers()) != 0 {
|
||||||
|
t.Fatal("removed subscription still owns cached results")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTrackerRefreshTimeoutPersistsCompletedAttemptStatus(t *testing.T) {
|
||||||
|
fm := &FetcherManager{}
|
||||||
|
cfg := fm.DefaultConfig().(*config)
|
||||||
|
address := "https://custom.example/trackers.txt"
|
||||||
|
cfg.TrackerSubscribeURLs = []string{address}
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
host, _ := trackerTestHost(t, cfg, func(context.Context, string, int64, func([]byte) error) ([]byte, error) {
|
||||||
|
cancel()
|
||||||
|
return nil, context.DeadlineExceeded
|
||||||
|
})
|
||||||
|
if err := fm.updateTrackers(ctx, host, fetcher.TriggerManual); !errors.Is(err, context.DeadlineExceeded) {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
status, ok := cfg.State.TrackerSubscriptionStatuses[address]
|
||||||
|
if !ok || status.Success || status.TrackerCount != 0 || status.LastFetchTime.IsZero() {
|
||||||
|
t.Fatalf("timeout status lost: %+v", status)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -67,17 +67,14 @@ func (f *Fetcher) Setup(ctl *controller.Controller) {
|
|||||||
f.ctl.GetConfig(&f.config)
|
f.ctl.GetConfig(&f.config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Resolve(req *base.Request, opts *base.Options) error {
|
func (f *Fetcher) Resolve(req *base.Request) error {
|
||||||
link, err := parseLink(req.URL)
|
link, err := parseLink(req.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
f.meta.Req = req
|
f.meta.Req = req
|
||||||
f.meta.Opts = opts
|
|
||||||
if f.meta.Opts == nil {
|
|
||||||
f.meta.Opts = &base.Options{}
|
|
||||||
}
|
|
||||||
f.meta.Res = buildResource(link)
|
f.meta.Res = buildResource(link)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ func TestFetcher_Resolve(t *testing.T) {
|
|||||||
f := (&FetcherManager{}).Build()
|
f := (&FetcherManager{}).Build()
|
||||||
f.Setup(controller.NewController())
|
f.Setup(controller.NewController())
|
||||||
|
|
||||||
err := f.Resolve(&base.Request{URL: testLink}, &base.Options{Path: t.TempDir()})
|
f.Meta().Opts = &base.Options{Path: t.TempDir()}
|
||||||
|
err := f.Resolve(&base.Request{URL: testLink})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Resolve() error = %v", err)
|
t.Fatalf("Resolve() error = %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package hls
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultSegmentConnections = 8
|
||||||
|
defaultMaxRetries = 5
|
||||||
|
defaultTimeoutSeconds = 30
|
||||||
|
|
||||||
|
// maxPrefetchSizeSegments caps the HEAD probing done at resolve time.
|
||||||
|
// Playlists larger than this skip size prefetch entirely.
|
||||||
|
maxPrefetchSizeSegments = 500
|
||||||
|
)
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
SegmentConnections int `json:"segmentConnections"`
|
||||||
|
MaxRetries int `json:"maxRetries"`
|
||||||
|
TimeoutSeconds int `json:"timeoutSeconds"`
|
||||||
|
PrefetchContentLength bool `json:"prefetchContentLength"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *config) normalize() {
|
||||||
|
if c.SegmentConnections <= 0 {
|
||||||
|
c.SegmentConnections = defaultSegmentConnections
|
||||||
|
}
|
||||||
|
if c.MaxRetries < 0 {
|
||||||
|
c.MaxRetries = defaultMaxRetries
|
||||||
|
}
|
||||||
|
if c.TimeoutSeconds <= 0 {
|
||||||
|
c.TimeoutSeconds = defaultTimeoutSeconds
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package hls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// decryptAES128 decrypts an AES-128-CBC encrypted segment and strips PKCS7
|
||||||
|
// padding. data length must be a multiple of the AES block size.
|
||||||
|
func decryptAES128(data, key, iv []byte) ([]byte, error) {
|
||||||
|
if len(key) != 16 {
|
||||||
|
return nil, fmt.Errorf("invalid AES-128 key size %d", len(key))
|
||||||
|
}
|
||||||
|
if len(iv) != 16 {
|
||||||
|
return nil, fmt.Errorf("invalid AES-128 IV size %d", len(iv))
|
||||||
|
}
|
||||||
|
if len(data) == 0 || len(data)%aes.BlockSize != 0 {
|
||||||
|
return nil, fmt.Errorf("invalid AES-128 data size %d", len(data))
|
||||||
|
}
|
||||||
|
block, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
plain := make([]byte, len(data))
|
||||||
|
cipher.NewCBCDecrypter(block, iv).CryptBlocks(plain, data)
|
||||||
|
return pkcs7Unpad(plain, aes.BlockSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return nil, fmt.Errorf("empty plaintext")
|
||||||
|
}
|
||||||
|
pad := int(data[len(data)-1])
|
||||||
|
if pad == 0 || pad > blockSize || pad > len(data) {
|
||||||
|
return nil, fmt.Errorf("invalid PKCS7 padding")
|
||||||
|
}
|
||||||
|
for _, b := range data[len(data)-pad:] {
|
||||||
|
if int(b) != pad {
|
||||||
|
return nil, fmt.Errorf("invalid PKCS7 padding")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data[:len(data)-pad], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// sequenceIV derives the default per-segment IV mandated by RFC 8216: the
|
||||||
|
// segment media sequence number as a 16-byte big-endian integer.
|
||||||
|
func sequenceIV(sequence int64) []byte {
|
||||||
|
iv := make([]byte, 16)
|
||||||
|
iv[8] = byte(sequence >> 56)
|
||||||
|
iv[9] = byte(sequence >> 48)
|
||||||
|
iv[10] = byte(sequence >> 40)
|
||||||
|
iv[11] = byte(sequence >> 32)
|
||||||
|
iv[12] = byte(sequence >> 24)
|
||||||
|
iv[13] = byte(sequence >> 16)
|
||||||
|
iv[14] = byte(sequence >> 8)
|
||||||
|
iv[15] = byte(sequence)
|
||||||
|
return iv
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package hls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/rand"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func pkcs7Pad(data []byte, blockSize int) []byte {
|
||||||
|
pad := blockSize - len(data)%blockSize
|
||||||
|
return append(data, bytes.Repeat([]byte{byte(pad)}, pad)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecryptAES128Roundtrip(t *testing.T) {
|
||||||
|
key := make([]byte, 16)
|
||||||
|
iv := make([]byte, 16)
|
||||||
|
if _, err := rand.Read(key); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := rand.Read(iv); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
plain := []byte("hello hls segment payload, padded to block size!")
|
||||||
|
padded := pkcs7Pad(plain, aes.BlockSize)
|
||||||
|
block, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
encrypted := make([]byte, len(padded))
|
||||||
|
cipher.NewCBCEncrypter(block, iv).CryptBlocks(encrypted, padded)
|
||||||
|
|
||||||
|
got, err := decryptAES128(encrypted, key, iv)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(got, plain) {
|
||||||
|
t.Errorf("roundtrip mismatch: %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecryptAES128InvalidInput(t *testing.T) {
|
||||||
|
if _, err := decryptAES128([]byte{1, 2, 3}, make([]byte, 16), make([]byte, 16)); err == nil {
|
||||||
|
t.Error("non-block-aligned data should fail")
|
||||||
|
}
|
||||||
|
if _, err := decryptAES128(make([]byte, 16), make([]byte, 15), make([]byte, 16)); err == nil {
|
||||||
|
t.Error("short key should fail")
|
||||||
|
}
|
||||||
|
if _, err := decryptAES128(make([]byte, 16), make([]byte, 16), make([]byte, 16)); err == nil {
|
||||||
|
t.Error("all-zero block has invalid padding and should fail")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSequenceIV(t *testing.T) {
|
||||||
|
iv := sequenceIV(1)
|
||||||
|
if len(iv) != 16 || iv[15] != 1 {
|
||||||
|
t.Errorf("sequence 1 IV wrong: %v", iv)
|
||||||
|
}
|
||||||
|
iv = sequenceIV(0x0102030405060708)
|
||||||
|
for i, want := range []byte{1, 2, 3, 4, 5, 6, 7, 8} {
|
||||||
|
if iv[8+i] != want {
|
||||||
|
t.Errorf("sequence IV byte %d: want %d got %d", i, want, iv[8+i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
// Package hls implements native HTTP Live Streaming downloads for m3u8
|
||||||
|
// playlists. A task produces exactly one merged, playable output file:
|
||||||
|
// MPEG-2 TS segments are concatenated as-is, fMP4 streams are merged as
|
||||||
|
// init section followed by the media fragments, and AES-128 encrypted
|
||||||
|
// segments are decrypted before they are stored.
|
||||||
|
//
|
||||||
|
// Supported subset:
|
||||||
|
// - VOD media playlists (EXT-X-ENDLIST present) and master playlists whose
|
||||||
|
// selected variant carries its own audio
|
||||||
|
// - AES-128 with the identity KEYFORMAT, explicit IV or media-sequence
|
||||||
|
// derived IV, including key rotation mid-playlist
|
||||||
|
// - EXT-X-BYTERANGE (including offset inheritance) and fMP4 init sections
|
||||||
|
// via a single, constant EXT-X-MAP
|
||||||
|
//
|
||||||
|
// Everything the merge pipeline cannot reproduce faithfully is rejected with
|
||||||
|
// an explicit error instead of silently producing a broken output file:
|
||||||
|
// - live streams (no EXT-X-ENDLIST) and SAMPLE-AES / DRM encryption
|
||||||
|
// - EXT-X-GAP (would leave a hole) and EXT-X-DISCONTINUITY (broken timeline)
|
||||||
|
// - changing init sections (a repeated EXT-X-MAP is compared by all its
|
||||||
|
// attributes, not just the URI) and encrypted EXT-X-MAP without an
|
||||||
|
// explicit IV
|
||||||
|
// - separate audio renditions referenced by the selected variant (the
|
||||||
|
// merged output would have no audio) and non-identity KEYFORMATs
|
||||||
|
package hls
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,100 @@
|
|||||||
|
package hls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FetcherManager struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FetcherManager) Name() string {
|
||||||
|
return "hls"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filters registers the .m3u8 extension. The manager must be registered BEFORE
|
||||||
|
// the http manager in the downloader config: protocol routing picks the first
|
||||||
|
// manager whose filter matches, and the http filter claims every http(s) URL.
|
||||||
|
func (fm *FetcherManager) Filters() []*fetcher.SchemeFilter {
|
||||||
|
return []*fetcher.SchemeFilter{
|
||||||
|
{
|
||||||
|
Type: fetcher.FilterTypeFile,
|
||||||
|
Pattern: "M3U8",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FetcherManager) Build() fetcher.Fetcher {
|
||||||
|
return &Fetcher{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FetcherManager) ParseName(u string) string {
|
||||||
|
parsed, err := url.Parse(u)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
name := path.Base(parsed.Path)
|
||||||
|
name = strings.TrimSuffix(name, path.Ext(name))
|
||||||
|
if name == "" || name == "/" || name == "." {
|
||||||
|
name = parsed.Hostname()
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FetcherManager) AutoRename() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FetcherManager) DefaultConfig() any {
|
||||||
|
return &config{
|
||||||
|
SegmentConnections: defaultSegmentConnections,
|
||||||
|
MaxRetries: defaultMaxRetries,
|
||||||
|
TimeoutSeconds: defaultTimeoutSeconds,
|
||||||
|
PrefetchContentLength: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FetcherManager) Store(f fetcher.Fetcher) (data any, err error) {
|
||||||
|
_f := f.(*Fetcher)
|
||||||
|
_f.mu.Lock()
|
||||||
|
defer _f.mu.Unlock()
|
||||||
|
if _f.state == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
// The engine's periodic checkpoint calls Store without holding the task
|
||||||
|
// lock and serializes the returned value afterwards, while Resolve/Patch/
|
||||||
|
// Start mutate the live state under the fetcher mutex: return a snapshot
|
||||||
|
// copy taken under that mutex, mirroring the HTTP protocol's Store.
|
||||||
|
return cloneState(_f.state), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func cloneState(state *fetcherState) *fetcherState {
|
||||||
|
cloned := *state
|
||||||
|
cloned.Segments = make([]*Segment, len(state.Segments))
|
||||||
|
for i, seg := range state.Segments {
|
||||||
|
s := *seg
|
||||||
|
cloned.Segments[i] = &s
|
||||||
|
}
|
||||||
|
return &cloned
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FetcherManager) Restore() (v any, f func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher) {
|
||||||
|
return &fetcherState{}, func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher {
|
||||||
|
fb := &FetcherManager{}
|
||||||
|
restored := fb.Build().(*Fetcher)
|
||||||
|
restored.meta = meta
|
||||||
|
if state, ok := v.(*fetcherState); ok && state.MediaURL != "" {
|
||||||
|
restored.state = state
|
||||||
|
}
|
||||||
|
// Setup is invoked by the engine (restoreFetcher) after this builder
|
||||||
|
// returns; ReqExtra parsing happens there as well.
|
||||||
|
return restored
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fm *FetcherManager) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,100 @@
|
|||||||
|
package hls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/http/cookiejar"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/internal/httpclient"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
const defaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
|
||||||
|
|
||||||
|
const maxPlaylistSize = 10 << 20 // 10MB, playlists are small text files
|
||||||
|
|
||||||
|
func (f *Fetcher) timeout() time.Duration {
|
||||||
|
return time.Duration(f.config.TimeoutSeconds) * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildClient builds an HTTP client honoring the task proxy and TLS settings.
|
||||||
|
// The client has no total timeout on purpose: large segments over slow links
|
||||||
|
// must not be aborted; body stalls are handled by stallReader instead. It is
|
||||||
|
// only called while the fetcher is quiescent (Resolve/Start), never from
|
||||||
|
// inside running worker goroutines.
|
||||||
|
func (f *Fetcher) buildClient() *http.Client {
|
||||||
|
var proxy *base.RequestProxy
|
||||||
|
skipVerify := false
|
||||||
|
if req := f.meta.Req; req != nil {
|
||||||
|
proxy = req.Proxy
|
||||||
|
skipVerify = req.SkipVerifyCert
|
||||||
|
}
|
||||||
|
jar, _ := cookiejar.New(nil)
|
||||||
|
client, err := httpclient.NewClient(httpclient.Options{
|
||||||
|
Client: httpclient.ClientOptions{
|
||||||
|
Jar: jar,
|
||||||
|
},
|
||||||
|
Transport: httpclient.TransportOptions{
|
||||||
|
DialContext: (&net.Dialer{
|
||||||
|
Timeout: f.timeout(),
|
||||||
|
}).DialContext,
|
||||||
|
Proxy: f.Ctl.GetProxy(proxy),
|
||||||
|
TLSClientConfig: &tls.Config{
|
||||||
|
InsecureSkipVerify: skipVerify,
|
||||||
|
},
|
||||||
|
TLSHandshakeTimeout: f.timeout(),
|
||||||
|
},
|
||||||
|
Impersonation: httpclient.ImpersonationOptions{
|
||||||
|
Mode: httpclient.ImpersonationAuto,
|
||||||
|
Session: f.impSession,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("build HTTP client: %v", err))
|
||||||
|
}
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildRequest builds a request with task extra headers, the default user
|
||||||
|
// agent and an optional byte range. extra may be nil.
|
||||||
|
func buildRequest(ctx context.Context, extra *ReqExtra, method, u string, body io.Reader, byterange *ByteRange) (*http.Request, error) {
|
||||||
|
req, err := http.NewRequestWithContext(ctx, method, u, body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if extra != nil {
|
||||||
|
for key, value := range extra.Header {
|
||||||
|
req.Header.Set(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if req.Header.Get("User-Agent") == "" {
|
||||||
|
req.Header.Set("User-Agent", defaultUserAgent)
|
||||||
|
}
|
||||||
|
if byterange != nil {
|
||||||
|
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", byterange.Offset, byterange.Offset+byterange.Length-1))
|
||||||
|
}
|
||||||
|
return req, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// stallReader aborts reads that see no data for the configured timeout.
|
||||||
|
type stallReader struct {
|
||||||
|
rc io.ReadCloser
|
||||||
|
timeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *stallReader) Read(p []byte) (int, error) {
|
||||||
|
timer := time.AfterFunc(r.timeout, func() {
|
||||||
|
r.rc.Close()
|
||||||
|
})
|
||||||
|
defer timer.Stop()
|
||||||
|
return r.rc.Read(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *stallReader) Close() error {
|
||||||
|
return r.rc.Close()
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
package hls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// journalFile is the persisted completed-segment ledger. It is bound to the
|
||||||
|
// exact download plan: a journal whose fingerprint does not match the current
|
||||||
|
// plan is discarded instead of being applied to the wrong segments.
|
||||||
|
type journalFile struct {
|
||||||
|
StagingID string `json:"stagingID"`
|
||||||
|
PlanHash string `json:"planHash"`
|
||||||
|
SegmentCount int `json:"segmentCount"`
|
||||||
|
Sizes map[string]int64 `json:"sizes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomStagingID returns a random identifier used to give every task its own
|
||||||
|
// staging folder, stable across app restarts through the persisted state.
|
||||||
|
func randomStagingID() string {
|
||||||
|
buf := make([]byte, 6)
|
||||||
|
if _, err := rand.Read(buf); err != nil {
|
||||||
|
// crypto/rand failing is catastrophic anyway; fall back to address
|
||||||
|
// entropy rather than crashing the fetch.
|
||||||
|
return fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("%p", buf))))[:12]
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// planFingerprint hashes everything that defines the download plan: the media
|
||||||
|
// URL, the ordered segments with their sequence numbers, byte ranges, keys
|
||||||
|
// and init markers. Two tasks resolve to different fingerprints even when
|
||||||
|
// they point at the same playlist URL.
|
||||||
|
func planFingerprint(state *fetcherState) string {
|
||||||
|
h := sha1.New()
|
||||||
|
io.WriteString(h, state.MediaURL+"\n")
|
||||||
|
io.WriteString(h, strconv.Itoa(len(state.Segments))+"\n")
|
||||||
|
for _, seg := range state.Segments {
|
||||||
|
fmt.Fprintf(h, "%d|%s|", seg.Sequence, seg.URI)
|
||||||
|
if seg.Byterange != nil {
|
||||||
|
fmt.Fprintf(h, "%d@%d|", seg.Byterange.Offset, seg.Byterange.Length)
|
||||||
|
}
|
||||||
|
if seg.Key != nil {
|
||||||
|
// The IV is part of the plan: same URI with a rotated IV would
|
||||||
|
// silently decrypt to garbage if the journal were reused.
|
||||||
|
fmt.Fprintf(h, "key=%s iv=%x|", seg.Key.URI, seg.Key.IV)
|
||||||
|
}
|
||||||
|
if seg.IsInit {
|
||||||
|
io.WriteString(h, "init|")
|
||||||
|
}
|
||||||
|
io.WriteString(h, "\n")
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(h.Sum(nil))[:16]
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadJournal reads the journal of a run, discarding anything that cannot be
|
||||||
|
// proven to belong to the current plan: foreign or legacy journals, corrupted
|
||||||
|
// files, and entries whose recorded size no longer matches the plan.
|
||||||
|
func loadJournal(run *hlsRun) error {
|
||||||
|
data, err := os.ReadFile(run.journalPath)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var file journalFile
|
||||||
|
if err := json.Unmarshal(data, &file); err != nil {
|
||||||
|
// A broken or legacy journal must not kill a restartable download.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if file.StagingID != run.stagingID || file.PlanHash != run.planHash || file.SegmentCount != len(run.segs) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for key, size := range file.Sizes {
|
||||||
|
idx, err := strconv.ParseInt(key, 10, 64)
|
||||||
|
if err != nil || idx < 0 || idx >= int64(len(run.segs)) || size <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
run.journal[idx] = size
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// saveJournal atomically rewrites the journal through a temp file + rename so
|
||||||
|
// a crash never leaves a half-written ledger behind.
|
||||||
|
func saveJournal(run *hlsRun) error {
|
||||||
|
file := journalFile{
|
||||||
|
StagingID: run.stagingID,
|
||||||
|
PlanHash: run.planHash,
|
||||||
|
SegmentCount: len(run.segs),
|
||||||
|
Sizes: make(map[string]int64, len(run.journal)),
|
||||||
|
}
|
||||||
|
for idx, size := range run.journal {
|
||||||
|
file.Sizes[strconv.FormatInt(idx, 10)] = size
|
||||||
|
}
|
||||||
|
data, err := json.Marshal(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tmp := run.journalPath + ".tmp"
|
||||||
|
if err := os.WriteFile(tmp, data, 0666); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.Rename(tmp, run.journalPath)
|
||||||
|
}
|
||||||
Loaded 50 of 372 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user