mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-15 04:43:41 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4c2321d686 | ||
|
|
daa8c39dad | ||
|
|
78e23217f9 | ||
|
|
61f62ec39a | ||
|
|
5d76d062ad | ||
|
|
fc489b6b97 | ||
|
|
a5cd53f94c | ||
|
|
847e88881d | ||
|
|
3d20e818e5 | ||
|
|
09f7c88ed3 | ||
|
|
f7b26753c1 | ||
|
|
9704d4af27 | ||
|
|
1907f404ac | ||
|
|
72c5ba7d20 | ||
|
|
5b0254f7c0 | ||
|
|
d36b345f92 | ||
|
|
bc5c818eae | ||
|
|
98bd3305b7 | ||
|
|
3f38848051 | ||
|
|
82d7f8ec81 | ||
|
|
a6981d58e9 | ||
|
|
1b1631b41a | ||
|
|
3eb6df55df | ||
|
|
6de63c3f63 | ||
|
|
969615f353 | ||
|
|
5a88b98314 | ||
|
|
2674b1c39e | ||
|
|
4c90b38247 | ||
|
|
93088b22f1 | ||
|
|
fe90c1d261 |
No files matched your search
+1
-1
@@ -12,4 +12,4 @@
|
|||||||
#custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
#custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||||
# github: monkeyWie
|
# github: monkeyWie
|
||||||
ko_fi: gopeed
|
ko_fi: gopeed
|
||||||
custom: https://docs.gopeed.com/donate.html
|
custom: https://gopeed.com/docs/donate
|
||||||
+11
-15
@@ -14,7 +14,7 @@ on:
|
|||||||
|
|
||||||
env:
|
env:
|
||||||
GO_VERSION: "1.24"
|
GO_VERSION: "1.24"
|
||||||
FLUTTER_VERSION: "3.38.7"
|
FLUTTER_VERSION: "3.41.2"
|
||||||
GA4_MEASUREMENT_ID: ${{ secrets.GA4_MEASUREMENT_ID }}
|
GA4_MEASUREMENT_ID: ${{ secrets.GA4_MEASUREMENT_ID }}
|
||||||
GA4_API_SECRET: ${{ secrets.GA4_API_SECRET }}
|
GA4_API_SECRET: ${{ secrets.GA4_API_SECRET }}
|
||||||
|
|
||||||
@@ -44,15 +44,17 @@ jobs:
|
|||||||
- arch: arm64
|
- arch: arm64
|
||||||
os: windows-11-arm
|
os: windows-11-arm
|
||||||
llvm_ver: "20251202" # Only ARM64
|
llvm_ver: "20251202" # Only ARM64
|
||||||
flutter_channel: "master"
|
flutter_channel: "main"
|
||||||
flutter_version: "8817d45220d" # Only ARM64
|
flutter_version: "7e1c8868"
|
||||||
- arch: amd64
|
- arch: amd64
|
||||||
os: windows-2022
|
os: windows-2022
|
||||||
flutter_channel: "stable"
|
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
- name: Enable long paths for flutter main branch checks
|
||||||
|
run: |
|
||||||
|
git config --global core.longpaths true
|
||||||
|
|
||||||
# Install LLVM environment only on ARM64
|
# Install LLVM environment only on ARM64
|
||||||
- name: Install llvm-mingw-ucrt-aarch64 (ARM64 only)
|
- name: Install llvm-mingw-ucrt-aarch64 (ARM64 only)
|
||||||
@@ -84,7 +86,7 @@ jobs:
|
|||||||
go-version: ${{ env.GO_VERSION }}
|
go-version: ${{ env.GO_VERSION }}
|
||||||
- uses: subosito/flutter-action@v2
|
- uses: subosito/flutter-action@v2
|
||||||
with:
|
with:
|
||||||
channel: ${{ matrix.flutter_channel }}
|
channel: ${{ matrix.flutter_channel || 'stable' }}
|
||||||
flutter-version: ${{ matrix.flutter_version || env.FLUTTER_VERSION }}
|
flutter-version: ${{ matrix.flutter_version || env.FLUTTER_VERSION }}
|
||||||
- name: Build
|
- name: Build
|
||||||
env:
|
env:
|
||||||
@@ -112,6 +114,7 @@ jobs:
|
|||||||
$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\libwinpthread-1.dll $release
|
cp $mingw\libwinpthread-1.dll $release
|
||||||
cp $system\msvcp140.dll $release
|
cp $system\msvcp140.dll $release
|
||||||
@@ -184,7 +187,7 @@ jobs:
|
|||||||
mv "Output\gopeed.exe" "Output\Gopeed-$TAG-windows-${{ matrix.arch }}.exe"
|
mv "Output\gopeed.exe" "Output\Gopeed-$TAG-windows-${{ matrix.arch }}.exe"
|
||||||
Compress-Archive -Path "Output\Gopeed-$TAG-windows-${{ matrix.arch }}.exe" -DestinationPath "Output\Gopeed-$TAG-windows-${{ matrix.arch }}.zip"
|
Compress-Archive -Path "Output\Gopeed-$TAG-windows-${{ matrix.arch }}.exe" -DestinationPath "Output\Gopeed-$TAG-windows-${{ matrix.arch }}.zip"
|
||||||
- name: Upload
|
- name: Upload
|
||||||
uses: shogo82148/actions-upload-release-asset@v1
|
uses: shogo82148/actions-upload-release-asset@v1.9.2
|
||||||
with:
|
with:
|
||||||
upload_url: ${{ needs.get-release.outputs.upload_url }}
|
upload_url: ${{ needs.get-release.outputs.upload_url }}
|
||||||
asset_path: ui/flutter/build/windows/Output/*
|
asset_path: ui/flutter/build/windows/Output/*
|
||||||
@@ -321,7 +324,7 @@ jobs:
|
|||||||
channel: master
|
channel: master
|
||||||
- run: |
|
- run: |
|
||||||
sudo apt update -y
|
sudo apt update -y
|
||||||
sudo apt install -y ninja-build libgtk-3-dev libayatana-appindicator3-1 libayatana-appindicator3-dev rpm patchelf libfuse2 locate imagemagick potrace libkeybinder-3.0-dev
|
sudo apt install -y ninja-build libgtk-3-dev libayatana-appindicator3-1 libayatana-appindicator3-dev rpm patchelf libfuse2 locate libkeybinder-3.0-dev
|
||||||
arch=$(uname -m)
|
arch=$(uname -m)
|
||||||
wget -O appimagetool "https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-${arch}.AppImage"
|
wget -O appimagetool "https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-${arch}.AppImage"
|
||||||
chmod +x appimagetool
|
chmod +x appimagetool
|
||||||
@@ -336,18 +339,11 @@ jobs:
|
|||||||
|
|
||||||
cd ui/flutter
|
cd ui/flutter
|
||||||
|
|
||||||
ICON_PATH=$(find assets -name "logo.png" -o -name "icon.png" | head -n 1)
|
|
||||||
[ -z "$ICON_PATH" ] && ICON_PATH="assets/icon.png" && touch assets/icon.png
|
|
||||||
|
|
||||||
convert "$ICON_PATH" assets/icon.svg
|
|
||||||
ICON_PATH="assets/icon.svg"
|
|
||||||
|
|
||||||
RPM_ARCH=$(uname -m)
|
RPM_ARCH=$(uname -m)
|
||||||
|
|
||||||
mkdir -p linux/packaging/rpm
|
mkdir -p linux/packaging/rpm
|
||||||
cat << EOF > linux/packaging/rpm/make_config.yaml
|
cat << EOF > linux/packaging/rpm/make_config.yaml
|
||||||
display_name: Gopeed
|
display_name: Gopeed
|
||||||
icon: $ICON_PATH
|
icon: assets/icon/icon.svg
|
||||||
summary: High speed downloader.
|
summary: High speed downloader.
|
||||||
description: A high speed downloader that supports all platforms.
|
description: A high speed downloader that supports all platforms.
|
||||||
group: Applications/Internet
|
group: Applications/Internet
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ on:
|
|||||||
|
|
||||||
env:
|
env:
|
||||||
GO_VERSION: "1.24"
|
GO_VERSION: "1.24"
|
||||||
FLUTTER_VERSION: "3.38.7"
|
FLUTTER_VERSION: "3.41.2"
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
# lint:
|
# lint:
|
||||||
@@ -66,21 +66,56 @@ jobs:
|
|||||||
files: ./coverage.txt
|
files: ./coverage.txt
|
||||||
token: ${{ secrets.CODECOV_UPLOAD_TOKEN }}
|
token: ${{ secrets.CODECOV_UPLOAD_TOKEN }}
|
||||||
build-desktop:
|
build-desktop:
|
||||||
name: Build desktop check
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
os: [windows-2022, macos-latest, ubuntu-22.04, ubuntu-22.04-arm]
|
include:
|
||||||
|
- os: windows-2022
|
||||||
|
- os: windows-11-arm
|
||||||
|
llvm_ver: "20251202" # Only ARM64
|
||||||
|
flutter_channel: "main"
|
||||||
|
flutter_version: "7e1c8868"
|
||||||
|
- os: macos-latest
|
||||||
|
- os: ubuntu-22.04
|
||||||
|
- os: ubuntu-22.04-arm
|
||||||
|
flutter_channel: "main"
|
||||||
|
name: Build desktop check (${{ matrix.os }})
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
needs: [test]
|
needs: [test]
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
- name: Enable long paths for flutter main branch checks
|
||||||
|
run: |
|
||||||
|
git config --global core.longpaths true
|
||||||
|
- name: Install llvm-mingw-ucrt-aarch64 (ARM64 only)
|
||||||
|
if: matrix.os == 'windows-11-arm'
|
||||||
|
run: |
|
||||||
|
$ver = "${{ matrix.llvm_ver }}"
|
||||||
|
$url = "https://github.com/mstorsjo/llvm-mingw/releases/download/$ver/llvm-mingw-$ver-ucrt-aarch64.zip"
|
||||||
|
$zip = "$env:RUNNER_TEMP\\llvm.zip"
|
||||||
|
$extract = "$env:RUNNER_TEMP\\extract"
|
||||||
|
$target = "C:\\clangarm64"
|
||||||
|
|
||||||
|
curl -L $url -o $zip
|
||||||
|
rm -r -fo $extract,$target -ea Ignore
|
||||||
|
mkdir $extract | Out-Null
|
||||||
|
|
||||||
|
tar -xf $zip -C $extract
|
||||||
|
mv (Get-ChildItem $extract)[0].FullName $target
|
||||||
|
|
||||||
|
$b = "$target\\bin"
|
||||||
|
"CC=$b\\clang.exe" >> $env:GITHUB_ENV
|
||||||
|
"CXX=$b\\clang++.exe" >> $env:GITHUB_ENV
|
||||||
|
"CLANGARM64_BIN=$b" >> $env:GITHUB_ENV
|
||||||
|
"CGO_ENABLED=1" >> $env:GITHUB_ENV
|
||||||
|
"CLANGARM64_ROOT=$target" >> $env:GITHUB_ENV
|
||||||
|
$b >> $env:GITHUB_PATH
|
||||||
- uses: actions/setup-go@v4
|
- uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: ${{ env.GO_VERSION }}
|
go-version: ${{ env.GO_VERSION }}
|
||||||
- uses: subosito/flutter-action@v2
|
- uses: subosito/flutter-action@v2
|
||||||
with:
|
with:
|
||||||
flutter-version: ${{ env.FLUTTER_VERSION }}
|
channel: ${{ matrix.flutter_channel || 'stable' }}
|
||||||
channel: master
|
flutter-version: ${{ matrix.flutter_version || env.FLUTTER_VERSION }}
|
||||||
- if: runner.os == 'Windows'
|
- if: runner.os == 'Windows'
|
||||||
run: |
|
run: |
|
||||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/GopeedLab/gopeed/bind/desktop
|
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/GopeedLab/gopeed/bind/desktop
|
||||||
|
|||||||
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
FROM golang:1.24.11 AS go
|
FROM golang:1.24.11-alpine3.23 AS go
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY ./go.mod ./go.sum ./
|
COPY ./go.mod ./go.sum ./
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
@@ -8,7 +8,7 @@ RUN CGO_ENABLED=0 go build -tags nosqlite,web \
|
|||||||
-ldflags="-s -w -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION -X github.com/GopeedLab/gopeed/pkg/base.InDocker=true" \
|
-ldflags="-s -w -X github.com/GopeedLab/gopeed/pkg/base.Version=$VERSION -X github.com/GopeedLab/gopeed/pkg/base.InDocker=true" \
|
||||||
-o dist/gopeed github.com/GopeedLab/gopeed/cmd/web
|
-o dist/gopeed github.com/GopeedLab/gopeed/cmd/web
|
||||||
|
|
||||||
FROM alpine:3.18
|
FROM alpine:3.23
|
||||||
LABEL maintainer="monkeyWie"
|
LABEL maintainer="monkeyWie"
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=go /app/dist/gopeed ./
|
COPY --from=go /app/dist/gopeed ./
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
# [](https://gopeed.com)
|
# [](https://gopeed.com)
|
||||||
|
|
||||||
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
||||||
[](https://codecov.io/gh/GopeedLab/gopeed)
|
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||||
[](https://github.com/GopeedLab/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
[](https://github.com/GopeedLab/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
[](https://docs.gopeed.com/donate.html)
|
[](https://gopeed.com/docs/donate)
|
||||||
[](https://raw.githubusercontent.com/GopeedLab/gopeed/main/_docs/img/weixin.png)
|
[](https://raw.githubusercontent.com/GopeedLab/gopeed/main/_docs/img/weixin.png)
|
||||||
[](https://discord.gg/ZUJqJrwCGB)
|
[](https://discord.gg/ZUJqJrwCGB)
|
||||||
|
|
||||||
@@ -16,9 +16,9 @@
|
|||||||
|
|
||||||
## 🚀 Introduction
|
## 🚀 Introduction
|
||||||
|
|
||||||
Gopeed (full name Go Speed), a high-speed downloader developed by `Golang` + `Flutter`, supports (HTTP, BitTorrent, Magnet) protocol, and supports all platforms. In addition to basic download functions, Gopeed is also a highly customizable downloader that supports implementing more features through integration with [APIs](https://docs.gopeed.com/dev-api.html) or installation and development of [extensions](https://docs.gopeed.com/dev-extension.html).
|
Gopeed (full name Go Speed), a high-speed downloader developed by `Golang` + `Flutter`, supports (HTTP, BitTorrent, Magnet, ED2K) protocol, and supports all platforms. In addition to basic download functions, Gopeed is also a highly customizable downloader that supports implementing more features through integration with [APIs](https://gopeed.com/docs/dev-api) or installation and development of [extensions](https://gopeed.com/docs/dev-extension).
|
||||||
|
|
||||||
Visit ✈ [Official Website](https://gopeed.com) | 📖 [Official Docs](https://docs.gopeed.com)
|
Visit ✈ [Official Website](https://gopeed.com) | 📖 [Official Docs](https://gopeed.com/docs)
|
||||||
|
|
||||||
## ⬇️ Download
|
## ⬇️ Download
|
||||||
|
|
||||||
@@ -165,7 +165,7 @@ Visit ✈ [Official Website](https://gopeed.com) | 📖 [Official Docs](https://
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
More about installation, please refer to [Installation](https://docs.gopeed.com/install.html)
|
More about installation, please refer to [Installation](https://gopeed.com/docs/install)
|
||||||
|
|
||||||
### 🛠️ Command tool
|
### 🛠️ Command tool
|
||||||
|
|
||||||
@@ -187,7 +187,7 @@ Follow our WeChat Official Account to get the latest updates and news.
|
|||||||
|
|
||||||
## 💝 Donate
|
## 💝 Donate
|
||||||
|
|
||||||
If you like this project, please consider [donating](https://docs.gopeed.com/donate.html) to support the development of this project, thank you!
|
If you like this project, please consider [donating](https://gopeed.com/docs/donate) to support the development of this project, thank you!
|
||||||
|
|
||||||
## 🖼️ Showcase
|
## 🖼️ Showcase
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -1,10 +1,10 @@
|
|||||||
# [](https://gopeed.com)
|
# [](https://gopeed.com)
|
||||||
|
|
||||||
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
||||||
[](https://codecov.io/gh/GopeedLab/gopeed)
|
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||||
[](https://github.com/GopeedLab/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
[](https://github.com/GopeedLab/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
[](https://docs.gopeed.com/donate.html)
|
[](https://gopeed.com/docs/donate)
|
||||||
[](https://raw.githubusercontent.com/GopeedLab/gopeed/main/_docs/img/weixin.png)
|
[](https://raw.githubusercontent.com/GopeedLab/gopeed/main/_docs/img/weixin.png)
|
||||||
[](https://discord.gg/ZUJqJrwCGB)
|
[](https://discord.gg/ZUJqJrwCGB)
|
||||||
|
|
||||||
@@ -16,9 +16,9 @@
|
|||||||
|
|
||||||
## 🚀 はじめに
|
## 🚀 はじめに
|
||||||
|
|
||||||
Gopeed (正式名 Go Speed) は `Golang` + `Flutter` によって開発された高速ダウンローダーで、(HTTP、BitTorrent、Magnet) プロトコルをサポートし、すべてのプラットフォームをサポートします。基本的なダウンロード機能に加え、[APIs](https://docs.gopeed.com/dev-api.html)との連動や[拡張機能](https://docs.gopeed.com/dev-extension.html)のインストール・開発による追加機能にも対応した、カスタマイズ性の高いダウンローダーです。
|
Gopeed (正式名 Go Speed) は `Golang` + `Flutter` によって開発された高速ダウンローダーで、(HTTP、BitTorrent、Magnet、ED2K) プロトコルをサポートし、すべてのプラットフォームをサポートします。基本的なダウンロード機能に加え、[APIs](https://gopeed.com/docs/dev-api)との連動や[拡張機能](https://gopeed.com/docs/dev-extension)のインストール・開発による追加機能にも対応した、カスタマイズ性の高いダウンローダーです。
|
||||||
|
|
||||||
見て下さい ✈ [公式ウェブサイト](https://gopeed.com) | 📖 [開発ドキュメント](https://docs.gopeed.com)
|
見て下さい ✈ [公式ウェブサイト](https://gopeed.com) | 📖 [開発ドキュメント](https://gopeed.com/docs)
|
||||||
|
|
||||||
## ⬇️ インストール
|
## ⬇️ インストール
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ Gopeed (正式名 Go Speed) は `Golang` + `Flutter` によって開発された
|
|||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
インストールについての詳細は、[インストール](https://docs.gopeed.com/install.html)を参照してください。
|
インストールについての詳細は、[インストール](https://gopeed.com/docs/install)を参照してください。
|
||||||
|
|
||||||
### 🛠️ コマンドツール
|
### 🛠️ コマンドツール
|
||||||
|
|
||||||
@@ -168,7 +168,7 @@ Gopeed (正式名 Go Speed) は `Golang` + `Flutter` によって開発された
|
|||||||
|
|
||||||
## 💝 寄付
|
## 💝 寄付
|
||||||
|
|
||||||
もしこのプロジェクトがお気に召しましたら、このプロジェクトの発展を支援するために[寄付](https://docs.gopeed.com/donate.html)をご検討ください!
|
もしこのプロジェクトがお気に召しましたら、このプロジェクトの発展を支援するために[寄付](https://gopeed.com/docs/donate)をご検討ください!
|
||||||
|
|
||||||
## 🖼️ ショーケース
|
## 🖼️ ショーケース
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -1,10 +1,10 @@
|
|||||||
# [](https://gopeed.com)
|
# [](https://gopeed.com)
|
||||||
|
|
||||||
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
||||||
[](https://codecov.io/gh/GopeedLab/gopeed)
|
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||||
[](https://github.com/GopeedLab/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
[](https://github.com/GopeedLab/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
[](https://docs.gopeed.com/donate.html)
|
[](https://gopeed.com/docs/donate)
|
||||||
[](https://raw.githubusercontent.com/GopeedLab/gopeed/main/_docs/img/weixin.png)
|
[](https://raw.githubusercontent.com/GopeedLab/gopeed/main/_docs/img/weixin.png)
|
||||||
[](https://discord.gg/ZUJqJrwCGB)
|
[](https://discord.gg/ZUJqJrwCGB)
|
||||||
|
|
||||||
@@ -16,9 +16,9 @@
|
|||||||
|
|
||||||
## 🚀 Giới thiệu
|
## 🚀 Giới thiệu
|
||||||
|
|
||||||
Gopeed (tên đầy đủ Go Speed), một công cụ tải xuống tốc độ cao được phát triển bởi `Golang` + `Flutter`, hỗ trợ giao thức (HTTP, BitTorrent, Magnet) và hỗ trợ tất cả các nền tảng. Ngoài các chức năng tải xuống cơ bản, Gopeed còn là một công cụ tải xuống có thể tùy chỉnh cao cho phép triển khai thêm tính năng thông qua việc tích hợp với [APIs](https://docs.gopeed.com/dev-api.html) hoặc cài đặt và phát triển các [tiện ích mở rộng](https://docs.gopeed.com/dev-extension.html).
|
Gopeed (tên đầy đủ Go Speed), một công cụ tải xuống tốc độ cao được phát triển bởi `Golang` + `Flutter`, hỗ trợ giao thức (HTTP, BitTorrent, Magnet, ED2K) và hỗ trợ tất cả các nền tảng. Ngoài các chức năng tải xuống cơ bản, Gopeed còn là một công cụ tải xuống có thể tùy chỉnh cao cho phép triển khai thêm tính năng thông qua việc tích hợp với [APIs](https://gopeed.com/docs/dev-api) hoặc cài đặt và phát triển các [tiện ích mở rộng](https://gopeed.com/docs/dev-extension).
|
||||||
|
|
||||||
Truy cập ✈ [Trang web chính thức](https://gopeed.com) | 📖 [Tài liệu chính thức](https://docs.gopeed.com)
|
Truy cập ✈ [Trang web chính thức](https://gopeed.com) | 📖 [Tài liệu chính thức](https://gopeed.com/docs)
|
||||||
|
|
||||||
## ⬇️ Tải về
|
## ⬇️ Tải về
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ Truy cập ✈ [Trang web chính thức](https://gopeed.com) | 📖 [Tài liệu
|
|||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
Thêm thông tin về cài đặt, vui lòng tham khảo [Cài đặt](https://docs.gopeed.com/install.html)
|
Thêm thông tin về cài đặt, vui lòng tham khảo [Cài đặt](https://gopeed.com/docs/install)
|
||||||
|
|
||||||
### 🛠️ Công cụ lệnh
|
### 🛠️ Công cụ lệnh
|
||||||
|
|
||||||
@@ -174,7 +174,7 @@ Theo dõi tài khoản chính thức để nhận các cập nhật và tin tứ
|
|||||||
|
|
||||||
## 💝 Quyên góp
|
## 💝 Quyên góp
|
||||||
|
|
||||||
Nếu bạn thích dự án này, xin vui lòng xem xét [quyên góp](https://docs.gopeed.com/donate.html) để hỗ trợ phát triển dự án này, cảm ơn bạn!
|
Nếu bạn thích dự án này, xin vui lòng xem xét [quyên góp](https://gopeed.com/docs/donate) để hỗ trợ phát triển dự án này, cảm ơn bạn!
|
||||||
|
|
||||||
## 🖼️ Trưng bày
|
## 🖼️ Trưng bày
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -1,10 +1,10 @@
|
|||||||
# [](https://gopeed.com)
|
# [](https://gopeed.com)
|
||||||
|
|
||||||
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
||||||
[](https://codecov.io/gh/GopeedLab/gopeed)
|
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||||
[](https://github.com/GopeedLab/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
[](https://github.com/GopeedLab/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
[](https://docs.gopeed.com/zh/donate.html)
|
[](https://gopeed.com/docs/donate)
|
||||||
[](https://raw.githubusercontent.com/GopeedLab/gopeed/main/_docs/img/weixin.png)
|
[](https://raw.githubusercontent.com/GopeedLab/gopeed/main/_docs/img/weixin.png)
|
||||||
[](https://discord.gg/ZUJqJrwCGB)
|
[](https://discord.gg/ZUJqJrwCGB)
|
||||||
|
|
||||||
@@ -16,9 +16,9 @@
|
|||||||
|
|
||||||
## 🚀 介绍
|
## 🚀 介绍
|
||||||
|
|
||||||
Gopeed(全称 Go Speed),直译过来中文名叫做`够快下载器`(不是狗屁下载器!),是一款由`Golang`+`Flutter`开发的高速下载器,支持(HTTP、BitTorrent、Magnet)协议下载,并且支持全平台使用。除了基本的下载功能外,Gopeed 还是一款高度可定制化的下载器,支持通过对接[APIs](https://docs.gopeed.com/zh/dev-api.html)或者安装和开发[扩展](https://docs.gopeed.com/zh/dev-extension.html)来实现更多的功能。
|
Gopeed(全称 Go Speed),直译过来中文名叫做`够快下载器`(不是狗屁下载器!),是一款由`Golang`+`Flutter`开发的高速下载器,支持(HTTP、BitTorrent、Magnet、ED2K)协议下载,并且支持全平台使用。除了基本的下载功能外,Gopeed 还是一款高度可定制化的下载器,支持通过对接[APIs](https://gopeed.com/docs/dev-api)或者安装和开发[扩展](https://gopeed.com/docs/dev-extension)来实现更多的功能。
|
||||||
|
|
||||||
访问 ✈ [官方网站](https://gopeed.com/zh-CN) | 📖 [官方文档](https://docs.gopeed.com/zh/)
|
访问 ✈ [官方网站](https://gopeed.com/zh-CN) | 📖 [官方文档](https://gopeed.com/docs)
|
||||||
|
|
||||||
## ⬇️ 下载
|
## ⬇️ 下载
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ Gopeed(全称 Go Speed),直译过来中文名叫做`够快下载器`(不
|
|||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
更多关于安装的内容请参考[安装文档](https://docs.gopeed.com/zh/install.html)
|
更多关于安装的内容请参考[安装文档](https://gopeed.com/docs/install)
|
||||||
|
|
||||||
### 🛠️ 命令行工具
|
### 🛠️ 命令行工具
|
||||||
|
|
||||||
@@ -178,7 +178,7 @@ Gopeed 还提供了浏览器扩展用于接管浏览器下载,支持 Chrome、
|
|||||||
|
|
||||||
## 💝 赞助
|
## 💝 赞助
|
||||||
|
|
||||||
如果觉得项目对你有帮助,请考虑[赞助](https://docs.gopeed.com/zh/donate)以支持这个项目的发展,非常感谢!
|
如果觉得项目对你有帮助,请考虑[赞助](https://gopeed.com/docs/donate)以支持这个项目的发展,非常感谢!
|
||||||
|
|
||||||
## 🖼️ 界面展示
|
## 🖼️ 界面展示
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -1,10 +1,10 @@
|
|||||||
# [](https://gopeed.com)
|
# [](https://gopeed.com)
|
||||||
|
|
||||||
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
[](https://github.com/GopeedLab/gopeed/actions?query=workflow%3Atest)
|
||||||
[](https://codecov.io/gh/GopeedLab/gopeed)
|
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||||
[](https://github.com/GopeedLab/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
[](https://github.com/GopeedLab/gopeed/releases)
|
[](https://github.com/GopeedLab/gopeed/releases)
|
||||||
[](https://docs.gopeed.com/donate.html)
|
[](https://gopeed.com/docs/donate)
|
||||||
[](https://raw.githubusercontent.com/GopeedLab/gopeed/main/_docs/img/weixin.png)
|
[](https://raw.githubusercontent.com/GopeedLab/gopeed/main/_docs/img/weixin.png)
|
||||||
[](https://discord.gg/ZUJqJrwCGB)
|
[](https://discord.gg/ZUJqJrwCGB)
|
||||||
|
|
||||||
@@ -16,9 +16,9 @@
|
|||||||
|
|
||||||
## 🚀 簡介
|
## 🚀 簡介
|
||||||
|
|
||||||
Gopeed(全稱 Go Speed),是一款使用`Golang`+`Flutter`編寫的高速下載軟體,支援(HTTP、BitTorrent、Magnet)協定,同時支援所有的平台。
|
Gopeed(全稱 Go Speed),是一款使用`Golang`+`Flutter`編寫的高速下載軟體,支援(HTTP、BitTorrent、Magnet、ED2K)協定,同時支援所有的平台。
|
||||||
|
|
||||||
前往 ✈ [主頁](https://gopeed.com/zh-CN) | 📖 [文檔](https://docs.gopeed.com/zh/)
|
前往 ✈ [主頁](https://gopeed.com/zh-CN) | 📖 [文檔](https://gopeed.com/docs)
|
||||||
|
|
||||||
## ⬇️ 下載
|
## ⬇️ 下載
|
||||||
|
|
||||||
@@ -157,7 +157,7 @@ Gopeed(全稱 Go Speed),是一款使用`Golang`+`Flutter`編寫的高速
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
更多關於安裝的內容請參考[安裝文檔](https://docs.gopeed.com/zh/install.html)
|
更多關於安裝的內容請參考[安裝文檔](https://gopeed.com/docs/install)
|
||||||
|
|
||||||
### 🛠️ 使用 CLI 安裝
|
### 🛠️ 使用 CLI 安裝
|
||||||
|
|
||||||
@@ -175,7 +175,7 @@ go install github.com/GopeedLab/gopeed/cmd/gopeed@latest
|
|||||||
|
|
||||||
## 💝 贊助
|
## 💝 贊助
|
||||||
|
|
||||||
如果你認為該項目對你有所幫助,請考慮[贊助](https://docs.gopeed.com/zh/donate)以支持該項目的持續發展,謝謝!
|
如果你認為該項目對你有所幫助,請考慮[贊助](https://gopeed.com/docs/donate)以支持該項目的持續發展,謝謝!
|
||||||
|
|
||||||
## 🖼️ 軟體介面
|
## 🖼️ 軟體介面
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 51 KiB |
@@ -0,0 +1,54 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 400" width="1200" height="400">
|
||||||
|
<rect width="1200" height="400" fill="#040605"/>
|
||||||
|
<g fill="#5dc76c">
|
||||||
|
<rect x="-100" y="50" width="150" height="2" opacity="0.6">
|
||||||
|
<animate attributeName="x" from="-200" to="1300" dur="0.8s" repeatCount="indefinite"/>
|
||||||
|
</rect>
|
||||||
|
<rect x="-200" y="150" width="200" height="1" opacity="0.4">
|
||||||
|
<animate attributeName="x" from="-300" to="1300" dur="1.2s" repeatCount="indefinite"/>
|
||||||
|
</rect>
|
||||||
|
<rect x="-50" y="250" width="180" height="3" opacity="0.8">
|
||||||
|
<animate attributeName="x" from="-200" to="1300" dur="0.6s" repeatCount="indefinite"/>
|
||||||
|
</rect>
|
||||||
|
<rect x="-150" y="350" width="220" height="2" opacity="0.5">
|
||||||
|
<animate attributeName="x" from="-300" to="1300" dur="1s" repeatCount="indefinite"/>
|
||||||
|
</rect>
|
||||||
|
<rect x="-100" y="100" width="100" height="1" opacity="0.7">
|
||||||
|
<animate attributeName="x" from="-150" to="1300" dur="0.9s" repeatCount="indefinite"/>
|
||||||
|
</rect>
|
||||||
|
<rect x="-250" y="300" width="250" height="2" opacity="0.3">
|
||||||
|
<animate attributeName="x" from="-400" to="1300" dur="1.5s" repeatCount="indefinite"/>
|
||||||
|
</rect>
|
||||||
|
</g>
|
||||||
|
<defs>
|
||||||
|
<filter id="logoGlow" x="-50%" y="-50%" width="200%" height="200%">
|
||||||
|
<feGaussianBlur stdDeviation="12" result="blur" />
|
||||||
|
<feComposite in="SourceGraphic" in2="blur" operator="over" />
|
||||||
|
</filter>
|
||||||
|
</defs>
|
||||||
|
<g transform="translate(600, 160)">
|
||||||
|
<!-- Floating animation -->
|
||||||
|
<g>
|
||||||
|
<animateTransform attributeName="transform" type="translate" values="0,-8; 0,8; 0,-8" dur="3s" repeatCount="indefinite" />
|
||||||
|
|
||||||
|
<!-- Pulsing glow behind logo -->
|
||||||
|
<circle cx="0" cy="0" r="45" fill="#5dc76c" filter="url(#logoGlow)" opacity="0.3">
|
||||||
|
<animate attributeName="opacity" values="0.1; 0.6; 0.1" dur="2s" repeatCount="indefinite" />
|
||||||
|
<animate attributeName="r" values="40; 65; 40" dur="2s" repeatCount="indefinite" />
|
||||||
|
</circle>
|
||||||
|
|
||||||
|
<!-- Breathing scale animation -->
|
||||||
|
<g>
|
||||||
|
<animateTransform attributeName="transform" type="scale" values="0.95; 1.05; 0.95" dur="2s" repeatCount="indefinite" />
|
||||||
|
<g transform="scale(0.12) translate(-512, -512)">
|
||||||
|
<path style="opacity:0.998" fill="#5dc76c" d="M 951.5,763.5 C 940.739,755.276 929.405,747.776 917.5,741C 908.477,736.319 899.143,732.485 889.5,729.5C 885.844,625.779 840.844,545.613 754.5,489C 674.802,441.453 590.135,430.12 500.5,455C 419.755,480.605 359.588,530.771 320,605.5C 278.585,565.816 230.752,555.649 176.5,575C 131.948,594.935 106.448,629.102 100,677.5C 99.3333,685.833 99.3333,694.167 100,702.5C 102.081,714.824 105.081,726.824 109,738.5C 95.4882,745.492 82.9882,753.826 71.5,763.5C 37.7121,705.639 16.5454,643.306 8,576.5C -9.41698,416.759 35.9164,278.093 144,160.5C 244.661,58.2827 367.161,5.94939 511.5,3.5C 678.975,7.48829 814.142,74.8216 917,205.5C 1011.33,336.426 1040,480.759 1003,638.5C 991.564,682.671 974.397,724.337 951.5,763.5 Z"/>
|
||||||
|
<path style="opacity:0.999" fill="#fefefe" d="M 951.5,763.5 C 949.28,768.272 946.78,772.938 944,777.5C 938.305,786.913 932.138,795.913 925.5,804.5C 915.954,796.888 905.288,791.055 893.5,787C 878.282,782.958 863.115,778.791 848,774.5C 854.013,716.891 841.346,663.891 810,615.5C 768.341,553.228 711.174,512.395 638.5,493C 560.311,479.566 490.311,497.566 428.5,547C 411.963,560.536 397.13,575.703 384,592.5C 366.745,616.923 348.911,640.923 330.5,664.5C 310.135,651.302 290.469,637.135 271.5,622C 238.448,602.307 206.782,604.307 176.5,628C 159.375,642.752 148.875,661.252 145,683.5C 143.968,692.716 144.968,701.716 148,710.5C 154.334,725.339 160.167,740.339 165.5,755.5C 138.118,764.688 114.451,779.688 94.5,800.5C 85.653,788.968 77.9863,776.635 71.5,763.5C 82.9882,753.826 95.4882,745.492 109,738.5C 105.081,726.824 102.081,714.824 100,702.5C 99.3333,694.167 99.3333,685.833 100,677.5C 106.448,629.102 131.948,594.935 176.5,575C 230.752,555.649 278.585,565.816 320,605.5C 359.588,530.771 419.755,480.605 500.5,455C 590.135,430.12 674.802,441.453 754.5,489C 840.844,545.613 885.844,625.779 889.5,729.5C 899.143,732.485 908.477,736.319 917.5,741C 929.405,747.776 940.739,755.276 951.5,763.5 Z"/>
|
||||||
|
<path style="opacity:0.998" fill="#5dc76c" d="M 925.5,804.5 C 850.791,908.608 750.791,975.775 625.5,1006C 609.308,1009.7 592.975,1012.53 576.5,1014.5C 615.708,969.938 655.042,925.438 694.5,881C 664.168,880.5 633.835,880.333 603.5,880.5C 603.5,811.167 603.5,741.833 603.5,672.5C 542.167,672.5 480.833,672.5 419.5,672.5C 419.5,741.5 419.5,810.5 419.5,879.5C 388.493,879.167 357.493,879.5 326.5,880.5C 326.624,881.107 326.957,881.44 327.5,881.5C 366.805,925.808 406.138,970.141 445.5,1014.5C 318.63,996.317 212.796,939.651 128,844.5C 115.85,830.525 104.683,815.859 94.5,800.5C 114.451,779.688 138.118,764.688 165.5,755.5C 160.167,740.339 154.334,725.339 148,710.5C 144.968,701.716 143.968,692.716 145,683.5C 148.875,661.252 159.375,642.752 176.5,628C 206.782,604.307 238.448,602.307 271.5,622C 290.469,637.135 310.135,651.302 330.5,664.5C 348.911,640.923 366.745,616.923 384,592.5C 397.13,575.703 411.963,560.536 428.5,547C 490.311,497.566 560.311,479.566 638.5,493C 711.174,512.395 768.341,553.228 810,615.5C 841.346,663.891 854.013,716.891 848,774.5C 863.115,778.791 878.282,782.958 893.5,787C 905.288,791.055 915.954,796.888 925.5,804.5 Z"/>
|
||||||
|
<path style="opacity:0.999" fill="#fefefe" d="M 576.5,1014.5 C 532.836,1019.82 489.169,1019.82 445.5,1014.5C 406.138,970.141 366.805,925.808 327.5,881.5C 358.167,880.833 388.833,880.167 419.5,879.5C 419.5,810.5 419.5,741.5 419.5,672.5C 480.833,672.5 542.167,672.5 603.5,672.5C 603.5,741.833 603.5,811.167 603.5,880.5C 633.835,880.333 664.168,880.5 694.5,881C 655.042,925.438 615.708,969.938 576.5,1014.5 Z"/>
|
||||||
|
<path style="opacity:1" fill="#ade2b5" d="M 419.5,879.5 C 388.833,880.167 358.167,880.833 327.5,881.5C 326.957,881.44 326.624,881.107 326.5,880.5C 357.493,879.5 388.493,879.167 419.5,879.5 Z"/>
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
<text x="600" y="300" font-family="sans-serif" font-size="72" font-style="italic" font-weight="900" text-anchor="middle" fill="#fff" letter-spacing="8">GOPEED</text>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 6.0 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 309 KiB After Width: | Height: | Size: 1.2 MiB |
@@ -45,6 +45,7 @@ require (
|
|||||||
github.com/klauspost/pgzip v1.2.6 // indirect
|
github.com/klauspost/pgzip v1.2.6 // indirect
|
||||||
github.com/mikelolasagasti/xz v1.0.1 // indirect
|
github.com/mikelolasagasti/xz v1.0.1 // indirect
|
||||||
github.com/minio/minlz v1.0.1 // indirect
|
github.com/minio/minlz v1.0.1 // indirect
|
||||||
|
github.com/monkeyWie/goed2k v0.0.0-20260317100435-7a7575cf2447 // indirect
|
||||||
github.com/nwaples/rardecode/v2 v2.2.0 // indirect
|
github.com/nwaples/rardecode/v2 v2.2.0 // indirect
|
||||||
github.com/onsi/ginkgo/v2 v2.23.4 // indirect
|
github.com/onsi/ginkgo/v2 v2.23.4 // indirect
|
||||||
github.com/pierrec/lz4/v4 v4.1.22 // indirect
|
github.com/pierrec/lz4/v4 v4.1.22 // indirect
|
||||||
|
|||||||
@@ -164,13 +164,9 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
|
|
||||||
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
|
||||||
github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ=
|
github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ=
|
||||||
github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
||||||
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
|
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
|
||||||
github.com/dop251/goja v0.0.0-20240919115326-6c7d1df7ff05 h1:oK4+QcKsczZjHYTHD0JAdkvq5w74JEkG95J0XNBx/BI=
|
|
||||||
github.com/dop251/goja v0.0.0-20240919115326-6c7d1df7ff05/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
|
|
||||||
github.com/dop251/goja v0.0.0-20260106131823-651366fbe6e3 h1:bVp3yUzvSAJzu9GqID+Z96P+eu5TKnIMJSV4QaZMauM=
|
github.com/dop251/goja v0.0.0-20260106131823-651366fbe6e3 h1:bVp3yUzvSAJzu9GqID+Z96P+eu5TKnIMJSV4QaZMauM=
|
||||||
github.com/dop251/goja v0.0.0-20260106131823-651366fbe6e3/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
|
github.com/dop251/goja v0.0.0-20260106131823-651366fbe6e3/go.mod h1:MxLav0peU43GgvwVgNbLAj1s/bSGboKkhuULvq/7hx4=
|
||||||
github.com/dop251/goja_nodejs v0.0.0-20240728170619-29b559befffc h1:MKYt39yZJi0Z9xEeRmDX2L4ocE0ETKcHKw6MVL3R+co=
|
github.com/dop251/goja_nodejs v0.0.0-20240728170619-29b559befffc h1:MKYt39yZJi0Z9xEeRmDX2L4ocE0ETKcHKw6MVL3R+co=
|
||||||
@@ -286,8 +282,6 @@ github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OI
|
|||||||
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||||
github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
|
github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
|
||||||
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
|
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
|
||||||
github.com/google/pprof v0.0.0-20260106004452-d7df1bf2cac7 h1:kmPAX+IJBcUAFTddx2+xC0H7sk2U9ijIIxZLLrPLNng=
|
|
||||||
github.com/google/pprof v0.0.0-20260106004452-d7df1bf2cac7/go.mod h1:67FPmZWbr+KDT/VlpWtw6sO9XSjpJmLuHpoLmWiTGgY=
|
|
||||||
github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83 h1:z2ogiKUYzX5Is6zr/vP9vJGqPwcdqsWjOt+V8J7+bTc=
|
github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83 h1:z2ogiKUYzX5Is6zr/vP9vJGqPwcdqsWjOt+V8J7+bTc=
|
||||||
github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83/go.mod h1:MxpfABSjhmINe3F1It9d+8exIHFvUqtLIRCdOGNXqiI=
|
github.com/google/pprof v0.0.0-20260115054156-294ebfa9ad83/go.mod h1:MxpfABSjhmINe3F1It9d+8exIHFvUqtLIRCdOGNXqiI=
|
||||||
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
|
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
|
||||||
@@ -392,6 +386,10 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
|
|||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
|
github.com/monkeyWie/goed2k v0.0.0-20260317094144-6e18d43056e5 h1:tEOucyKJzFsCz5Gr41jFHj8i2g1zemTyS4uyErJgFHc=
|
||||||
|
github.com/monkeyWie/goed2k v0.0.0-20260317094144-6e18d43056e5/go.mod h1:Ry2y1QlzerUgA1hVmExBdXXzE4Sjk1M7w0nSh6dhDOg=
|
||||||
|
github.com/monkeyWie/goed2k v0.0.0-20260317100435-7a7575cf2447 h1:88kRsNwkDKPA4NzRUWdOoIL7SyMNDwtmAxzShKBvKTI=
|
||||||
|
github.com/monkeyWie/goed2k v0.0.0-20260317100435-7a7575cf2447/go.mod h1:Ry2y1QlzerUgA1hVmExBdXXzE4Sjk1M7w0nSh6dhDOg=
|
||||||
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
|
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
|
||||||
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
|
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
|
||||||
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
|
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
|
||||||
@@ -741,8 +739,6 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
|||||||
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
|
|
||||||
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
|
|
||||||
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
|
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
|
||||||
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
|
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
|
||||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ type Fetcher interface {
|
|||||||
Setup(ctl *controller.Controller)
|
Setup(ctl *controller.Controller)
|
||||||
Resolve(req *base.Request, opts *base.Options) error
|
Resolve(req *base.Request, opts *base.Options) error
|
||||||
Start() error
|
Start() error
|
||||||
|
// Patch modifies task-specific data based on the protocol.
|
||||||
|
// For HTTP: can modify Request info (URL, headers, etc.)
|
||||||
|
// For BT: can modify SelectFiles (via opts.SelectFiles)
|
||||||
|
Patch(req *base.Request, opts *base.Options) error
|
||||||
Pause() error
|
Pause() error
|
||||||
Close() error
|
Close() error
|
||||||
|
|
||||||
@@ -123,6 +127,21 @@ type FetcherManager interface {
|
|||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StatefulFetcherManager is an optional extension for protocols that keep
|
||||||
|
// shared client state outside individual task fetchers.
|
||||||
|
type StatefulFetcherManager interface {
|
||||||
|
SetStateStore(store ProtocolStateStore)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ProtocolStateStore persists shared protocol state for a fetcher manager.
|
||||||
|
// Downloader provides the concrete storage backend, while the protocol decides
|
||||||
|
// when state should be loaded or flushed.
|
||||||
|
type ProtocolStateStore interface {
|
||||||
|
Load(v any) (bool, error)
|
||||||
|
Save(v any) error
|
||||||
|
Delete() error
|
||||||
|
}
|
||||||
|
|
||||||
type DefaultFetcher struct {
|
type DefaultFetcher struct {
|
||||||
Ctl *controller.Controller
|
Ctl *controller.Controller
|
||||||
Meta *FetcherMeta
|
Meta *FetcherMeta
|
||||||
|
|||||||
@@ -232,6 +232,59 @@ func (f *Fetcher) isDone() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Patch modifies the BT task settings.
|
||||||
|
// Invalid file indices are silently ignored.
|
||||||
|
func (f *Fetcher) Patch(req *base.Request, opts *base.Options) error {
|
||||||
|
if opts == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if opts.SelectFiles != nil {
|
||||||
|
selectFiles := opts.SelectFiles
|
||||||
|
|
||||||
|
// Get file count from resource metadata
|
||||||
|
fileCount := 0
|
||||||
|
if f.meta.Res != nil {
|
||||||
|
fileCount = len(f.meta.Res.Files)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter out invalid indices (silently ignore)
|
||||||
|
validSelectFiles := make([]int, 0, len(selectFiles))
|
||||||
|
for _, idx := range selectFiles {
|
||||||
|
if idx >= 0 && idx < fileCount {
|
||||||
|
validSelectFiles = append(validSelectFiles, idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.torrent != nil {
|
||||||
|
files := f.torrent.Files()
|
||||||
|
|
||||||
|
// Cancel all current file downloads first
|
||||||
|
f.torrent.CancelPieces(0, f.torrent.NumPieces())
|
||||||
|
|
||||||
|
// Apply new file selection
|
||||||
|
if len(validSelectFiles) == len(files) {
|
||||||
|
f.torrent.DownloadAll()
|
||||||
|
} else {
|
||||||
|
for _, selectIndex := range validSelectFiles {
|
||||||
|
file := files[selectIndex]
|
||||||
|
file.Download()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
f.meta.Opts.SelectFiles = validSelectFiles
|
||||||
|
// Recalculate the resource size based on new selection
|
||||||
|
if f.meta.Res != nil {
|
||||||
|
f.meta.Res.CalcSize(validSelectFiles)
|
||||||
|
}
|
||||||
|
// Reset progress tracking for new file selection
|
||||||
|
f.data.Progress = make(fetcher.Progress, len(validSelectFiles))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Fetcher) updateRes() {
|
func (f *Fetcher) updateRes() {
|
||||||
res := &base.Resource{
|
res := &base.Resource{
|
||||||
Range: true,
|
Range: true,
|
||||||
|
|||||||
@@ -239,3 +239,130 @@ func buildConfigFetcher(proxyConfig *base.DownloaderProxyConfig) fetcher.Fetcher
|
|||||||
fetcher.Setup(newController)
|
fetcher.Setup(newController)
|
||||||
return fetcher
|
return fetcher
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestFetcher_Patch tests the Patch functionality for BT fetcher.
|
||||||
|
// It tests modifying selected files after Resolve (without downloading).
|
||||||
|
func TestFetcher_Patch(t *testing.T) {
|
||||||
|
f := buildFetcher()
|
||||||
|
|
||||||
|
// Resolve a multi-file torrent
|
||||||
|
err := f.Resolve(&base.Request{
|
||||||
|
URL: "./testdata/test.torrent",
|
||||||
|
}, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Resolve failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify initial state: 3 files, all selected by default
|
||||||
|
meta := f.Meta()
|
||||||
|
if len(meta.Res.Files) != 3 {
|
||||||
|
t.Fatalf("Expected 3 files, got %d", len(meta.Res.Files))
|
||||||
|
}
|
||||||
|
if len(meta.Opts.SelectFiles) != 3 {
|
||||||
|
t.Fatalf("Expected 3 selected files, got %d", len(meta.Opts.SelectFiles))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Total size of all files: 107484864
|
||||||
|
totalSize := int64(107484864)
|
||||||
|
if meta.Res.Size != totalSize {
|
||||||
|
t.Fatalf("Expected total size %d, got %d", totalSize, meta.Res.Size)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("Patch with valid indices", func(t *testing.T) {
|
||||||
|
// Select only file 0 and 2 (c.txt and a.txt)
|
||||||
|
err := f.Patch(nil, &base.Options{
|
||||||
|
SelectFiles: []int{0, 2},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Patch failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
meta := f.Meta()
|
||||||
|
if !reflect.DeepEqual(meta.Opts.SelectFiles, []int{0, 2}) {
|
||||||
|
t.Errorf("Expected SelectFiles [0, 2], got %v", meta.Opts.SelectFiles)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size should be recalculated: c.txt (98501754) + a.txt (78114) = 98579868
|
||||||
|
expectedSize := int64(98501754 + 78114)
|
||||||
|
if meta.Res.Size != expectedSize {
|
||||||
|
t.Errorf("Expected size %d, got %d", expectedSize, meta.Res.Size)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Patch with invalid indices are silently ignored", func(t *testing.T) {
|
||||||
|
// Mix of valid (0, 1) and invalid (-1, 5, 100) indices
|
||||||
|
err := f.Patch(nil, &base.Options{
|
||||||
|
SelectFiles: []int{-1, 0, 5, 1, 100},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Patch should not return error for invalid indices: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
meta := f.Meta()
|
||||||
|
// Only valid indices 0 and 1 should remain
|
||||||
|
if !reflect.DeepEqual(meta.Opts.SelectFiles, []int{0, 1}) {
|
||||||
|
t.Errorf("Expected SelectFiles [0, 1], got %v", meta.Opts.SelectFiles)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size should be: c.txt (98501754) + b.txt (8904996) = 107406750
|
||||||
|
expectedSize := int64(98501754 + 8904996)
|
||||||
|
if meta.Res.Size != expectedSize {
|
||||||
|
t.Errorf("Expected size %d, got %d", expectedSize, meta.Res.Size)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Patch with all invalid indices results in empty selection", func(t *testing.T) {
|
||||||
|
err := f.Patch(nil, &base.Options{
|
||||||
|
SelectFiles: []int{-5, 10, 999},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Patch should not return error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
meta := f.Meta()
|
||||||
|
if len(meta.Opts.SelectFiles) != 0 {
|
||||||
|
t.Errorf("Expected empty SelectFiles, got %v", meta.Opts.SelectFiles)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: CalcSize with empty selectFiles calculates total size of all files
|
||||||
|
// This is by design - empty selection in CalcSize means "all files"
|
||||||
|
// But SelectFiles being empty means no files are selected for download
|
||||||
|
if meta.Res.Size != totalSize {
|
||||||
|
t.Errorf("Expected size %d (CalcSize with empty slice = all files), got %d", totalSize, meta.Res.Size)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Patch with nil opts does nothing", func(t *testing.T) {
|
||||||
|
// First set a known state
|
||||||
|
f.Patch(nil, &base.Options{SelectFiles: []int{1}})
|
||||||
|
prevSelectFiles := f.Meta().Opts.SelectFiles
|
||||||
|
|
||||||
|
// Patch with nil opts
|
||||||
|
err := f.Patch(nil, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Patch with nil opts should not fail: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should remain unchanged
|
||||||
|
if !reflect.DeepEqual(f.Meta().Opts.SelectFiles, prevSelectFiles) {
|
||||||
|
t.Errorf("SelectFiles should remain unchanged after nil opts Patch")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Patch progress array is resized", func(t *testing.T) {
|
||||||
|
btFetcher := f.(*Fetcher)
|
||||||
|
// Initialize progress array
|
||||||
|
btFetcher.data.Progress = make(fetcher.Progress, 3)
|
||||||
|
|
||||||
|
err := f.Patch(nil, &base.Options{
|
||||||
|
SelectFiles: []int{0, 2},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Patch failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(btFetcher.data.Progress) != 2 {
|
||||||
|
t.Errorf("Expected Progress length 2, got %d", len(btFetcher.data.Progress))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package ed2k
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultServerList = "45.82.80.155:5687,176.123.5.89:4725,85.121.5.137:4232,176.123.2.239:4232,145.239.2.134:4661,91.208.162.87:4232,37.15.61.236:4232"
|
||||||
|
defaultServerMet = "ed2k://|serverlist|http://upd.emule-security.org/server.met|/"
|
||||||
|
defaultNodesDat = "https://upd.emule-security.org/nodes.dat"
|
||||||
|
)
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
ListenPort int `json:"listenPort"`
|
||||||
|
UDPPort int `json:"udpPort"`
|
||||||
|
ServerAddr string `json:"serverAddr"`
|
||||||
|
ServerMet string `json:"serverMet"`
|
||||||
|
NodesDat string `json:"nodesDat"`
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,42 @@
|
|||||||
|
package ed2k
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/internal/controller"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
const testLink = "ed2k://|file|cn_windows_10_multi-edition_vl_version_1709_updated_sept_2017_x64_dvd_100090774.iso|4630972416|8867C5E54405FF9452225B66EFEE690A|/"
|
||||||
|
|
||||||
|
func TestFetcher_Resolve(t *testing.T) {
|
||||||
|
f := (&FetcherManager{}).Build()
|
||||||
|
f.Setup(controller.NewController())
|
||||||
|
|
||||||
|
err := f.Resolve(&base.Request{URL: testLink}, &base.Options{Path: t.TempDir()})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Resolve() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
meta := f.Meta()
|
||||||
|
if meta.Res == nil {
|
||||||
|
t.Fatal("Resolve() resource is nil")
|
||||||
|
}
|
||||||
|
if got, want := meta.Res.Hash, "8867C5E54405FF9452225B66EFEE690A"; got != want {
|
||||||
|
t.Fatalf("Resolve() hash = %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
if got, want := meta.Res.Size, int64(4630972416); got != want {
|
||||||
|
t.Fatalf("Resolve() size = %d, want %d", got, want)
|
||||||
|
}
|
||||||
|
if got, want := meta.Res.Files[0].Name, "cn_windows_10_multi-edition_vl_version_1709_updated_sept_2017_x64_dvd_100090774.iso"; got != want {
|
||||||
|
t.Fatalf("Resolve() name = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFetcherManager_ParseName(t *testing.T) {
|
||||||
|
got := (&FetcherManager{}).ParseName(testLink)
|
||||||
|
want := "cn_windows_10_multi-edition_vl_version_1709_updated_sept_2017_x64_dvd_100090774.iso"
|
||||||
|
if got != want {
|
||||||
|
t.Fatalf("ParseName() = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -588,6 +588,10 @@ func (f *Fetcher) Start() error {
|
|||||||
// Already downloading, this is a resume from pause
|
// Already downloading, this is a resume from pause
|
||||||
return f.doStart()
|
return f.doStart()
|
||||||
|
|
||||||
|
case stateError:
|
||||||
|
// Retry after error: reset and restart
|
||||||
|
return f.doStart()
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("cannot start in current state: %v", state)
|
return fmt.Errorf("cannot start in current state: %v", state)
|
||||||
}
|
}
|
||||||
@@ -598,10 +602,32 @@ func (f *Fetcher) doStart() error {
|
|||||||
<-f.resolvedCh
|
<-f.resolvedCh
|
||||||
|
|
||||||
state := f.getState()
|
state := f.getState()
|
||||||
if state == stateDone || state == stateError {
|
if state == stateDone {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If retrying after error, reset connection states for retry
|
||||||
|
if state == stateError {
|
||||||
|
// Drain any pending error from doneCh before retry
|
||||||
|
select {
|
||||||
|
case <-f.doneCh:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
f.connMu.Lock()
|
||||||
|
for _, conn := range f.connections {
|
||||||
|
// Reset connections that can be retried
|
||||||
|
if !conn.Completed && conn.State != connCompleted {
|
||||||
|
f.resetConnectionForRestart(conn)
|
||||||
|
conn.State = connNotStarted
|
||||||
|
conn.failed = false
|
||||||
|
conn.retryTimes = 0
|
||||||
|
conn.lastErr = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f.connMu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
// Open or create target file first (needed for prefetch copy)
|
// Open or create target file first (needed for prefetch copy)
|
||||||
name := f.meta.SingleFilepath()
|
name := f.meta.SingleFilepath()
|
||||||
var err error
|
var err error
|
||||||
@@ -948,6 +974,8 @@ func (f *Fetcher) runConnection(conn *connection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// downloadChunkOnce performs a single HTTP request for the current chunk without retrying.
|
// downloadChunkOnce performs a single HTTP request for the current chunk without retrying.
|
||||||
|
// If the redirect URL fails with an expiration-related error (401, 403, 410),
|
||||||
|
// it will automatically retry with the original URL and update the redirect URL on success.
|
||||||
func (f *Fetcher) downloadChunkOnce(conn *connection, client *http.Client, buf []byte) error {
|
func (f *Fetcher) downloadChunkOnce(conn *connection, client *http.Client, buf []byte) error {
|
||||||
if conn.ctx.Err() != nil {
|
if conn.ctx.Err() != nil {
|
||||||
return conn.ctx.Err()
|
return conn.ctx.Err()
|
||||||
@@ -981,11 +1009,34 @@ func (f *Fetcher) downloadChunkOnce(conn *connection, client *http.Client, buf [
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode != base.HttpCodeOK && resp.StatusCode != base.HttpCodePartialContent {
|
if resp.StatusCode != base.HttpCodeOK && resp.StatusCode != base.HttpCodePartialContent {
|
||||||
return NewRequestError(resp.StatusCode)
|
resp.Body.Close()
|
||||||
|
originalErr := NewRequestError(resp.StatusCode)
|
||||||
|
|
||||||
|
// Check if this might be a redirect URL expiration error
|
||||||
|
// If so, try falling back to the original URL
|
||||||
|
if f.hasRedirectURL() && isRedirectExpiredError(originalErr) {
|
||||||
|
fallbackResp, fallbackErr := f.tryFallbackToOriginalURL(conn.ctx, client, rangeStart, rangeEnd)
|
||||||
|
if fallbackErr == nil && fallbackResp != nil {
|
||||||
|
// Fallback succeeded, use this response instead
|
||||||
|
resp = fallbackResp
|
||||||
|
// Update the redirect URL from the response
|
||||||
|
if resp.Request != nil && resp.Request.URL != nil {
|
||||||
|
f.updateRedirectURL(resp.Request.URL.String())
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Fallback also failed, return the original error
|
||||||
|
if fallbackResp != nil {
|
||||||
|
fallbackResp.Body.Close()
|
||||||
|
}
|
||||||
|
return originalErr
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return originalErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// Record successful connection time for adaptive timeout
|
// Record successful connection time for adaptive timeout
|
||||||
f.updateMaxConnTime(time.Since(connStartTime))
|
f.updateMaxConnTime(time.Since(connStartTime))
|
||||||
@@ -1363,6 +1414,27 @@ func (f *Fetcher) helpOtherConnection(helper *connection) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *Fetcher) resetConnectionForRestart(conn *connection) {
|
||||||
|
if f.meta.Res.Range {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Without range support a new request always starts from byte 0,
|
||||||
|
// so pause/retry must restart instead of continuing from the old offset.
|
||||||
|
if conn.Chunk == nil {
|
||||||
|
conn.Chunk = newChunk(0, 0)
|
||||||
|
} else {
|
||||||
|
conn.Chunk.Begin = 0
|
||||||
|
conn.Chunk.End = 0
|
||||||
|
conn.Chunk.Downloaded = 0
|
||||||
|
}
|
||||||
|
conn.Downloaded = 0
|
||||||
|
conn.Completed = false
|
||||||
|
conn.speed = 0
|
||||||
|
conn.lastSpeedCheck = 0
|
||||||
|
conn.lastSpeedDownload = 0
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Fetcher) resumeConnections() {
|
func (f *Fetcher) resumeConnections() {
|
||||||
// Collect connections to resume while holding the lock
|
// Collect connections to resume while holding the lock
|
||||||
var toResume []*connection
|
var toResume []*connection
|
||||||
@@ -1386,6 +1458,7 @@ func (f *Fetcher) resumeConnections() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
f.resetConnectionForRestart(conn)
|
||||||
// Reset the connection state for resume
|
// Reset the connection state for resume
|
||||||
conn.ctx, conn.cancel = context.WithCancel(f.ctx)
|
conn.ctx, conn.cancel = context.WithCancel(f.ctx)
|
||||||
conn.State = connNotStarted
|
conn.State = connNotStarted
|
||||||
@@ -1425,7 +1498,16 @@ func (f *Fetcher) onDownloadComplete() {
|
|||||||
// Check if all chunks are complete (no remaining bytes)
|
// Check if all chunks are complete (no remaining bytes)
|
||||||
allChunksComplete := true
|
allChunksComplete := true
|
||||||
for _, conn := range f.connections {
|
for _, conn := range f.connections {
|
||||||
if conn.Chunk != nil && conn.Chunk.remain() > 0 && !conn.Completed && conn.State != connCompleted {
|
needsMoreData := false
|
||||||
|
if f.meta.Res.Range {
|
||||||
|
needsMoreData = conn.Chunk != nil && conn.Chunk.remain() > 0
|
||||||
|
} else if f.meta.Res.Size > 0 {
|
||||||
|
needsMoreData = conn.Downloaded < f.meta.Res.Size
|
||||||
|
} else {
|
||||||
|
needsMoreData = !conn.Completed && conn.State != connCompleted
|
||||||
|
}
|
||||||
|
|
||||||
|
if needsMoreData && !conn.Completed && conn.State != connCompleted {
|
||||||
// This connection has remaining work and isn't done
|
// This connection has remaining work and isn't done
|
||||||
// Check if it failed with 403 (server limit) - these can be ignored if other connections completed the work
|
// Check if it failed with 403 (server limit) - these can be ignored if other connections completed the work
|
||||||
if conn.State == connFailed && conn.failed {
|
if conn.State == connFailed && conn.failed {
|
||||||
@@ -1523,6 +1605,60 @@ func (f *Fetcher) checkCompletion() bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Patch modifies the HTTP request information.
|
||||||
|
func (f *Fetcher) Patch(req *base.Request, opts *base.Options) error {
|
||||||
|
// Patch request info
|
||||||
|
if req != nil {
|
||||||
|
if req.URL != "" {
|
||||||
|
f.meta.Req.URL = req.URL
|
||||||
|
// Clear redirect URL when URL is changed, so new requests use the new URL
|
||||||
|
f.updateRedirectURL("")
|
||||||
|
}
|
||||||
|
if req.Extra != nil {
|
||||||
|
if err := base.ParseReqExtra[fhttp.ReqExtra](req); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
patchExtra := req.Extra.(*fhttp.ReqExtra)
|
||||||
|
// Merge Extra fields instead of replacing entirely
|
||||||
|
if f.meta.Req.Extra == nil {
|
||||||
|
f.meta.Req.Extra = &fhttp.ReqExtra{}
|
||||||
|
}
|
||||||
|
existingExtra := f.meta.Req.Extra.(*fhttp.ReqExtra)
|
||||||
|
// Update Method only if non-empty
|
||||||
|
if patchExtra.Method != "" {
|
||||||
|
existingExtra.Method = patchExtra.Method
|
||||||
|
}
|
||||||
|
// Update Body only if non-empty
|
||||||
|
if patchExtra.Body != "" {
|
||||||
|
existingExtra.Body = patchExtra.Body
|
||||||
|
}
|
||||||
|
// Merge Headers: existing keys are overwritten, new keys are added
|
||||||
|
if patchExtra.Header != nil {
|
||||||
|
if existingExtra.Header == nil {
|
||||||
|
existingExtra.Header = make(map[string]string)
|
||||||
|
}
|
||||||
|
for k, v := range patchExtra.Header {
|
||||||
|
existingExtra.Header[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Merge Labels: existing keys are overwritten, new keys are added
|
||||||
|
if req.Labels != nil {
|
||||||
|
if f.meta.Req.Labels == nil {
|
||||||
|
f.meta.Req.Labels = make(map[string]string)
|
||||||
|
}
|
||||||
|
for k, v := range req.Labels {
|
||||||
|
f.meta.Req.Labels[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if req.Proxy != nil {
|
||||||
|
f.meta.Req.Proxy = req.Proxy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Fetcher) Pause() error {
|
func (f *Fetcher) Pause() error {
|
||||||
if f.cancel != nil {
|
if f.cancel != nil {
|
||||||
f.cancel()
|
f.cancel()
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import (
|
|||||||
|
|
||||||
type fetcherData struct {
|
type fetcherData struct {
|
||||||
Connections []*connection
|
Connections []*connection
|
||||||
|
RedirectURL string // Saved redirect URL for resume
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -71,8 +72,12 @@ func (fm *FetcherManager) DefaultConfig() any {
|
|||||||
|
|
||||||
func (fm *FetcherManager) Store(f fetcher.Fetcher) (data any, err error) {
|
func (fm *FetcherManager) Store(f fetcher.Fetcher) (data any, err error) {
|
||||||
_f := f.(*Fetcher)
|
_f := f.(*Fetcher)
|
||||||
|
_f.redirectLock.Lock()
|
||||||
|
redirectURL := _f.redirectURL
|
||||||
|
_f.redirectLock.Unlock()
|
||||||
return &fetcherData{
|
return &fetcherData{
|
||||||
Connections: _f.connections,
|
Connections: _f.connections,
|
||||||
|
RedirectURL: redirectURL,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,6 +92,10 @@ func (fm *FetcherManager) Restore() (v any, f func(meta *fetcher.FetcherMeta, v
|
|||||||
if len(fd.Connections) > 0 {
|
if len(fd.Connections) > 0 {
|
||||||
fetcher.connections = fd.Connections
|
fetcher.connections = fd.Connections
|
||||||
}
|
}
|
||||||
|
// Restore redirect URL for resume
|
||||||
|
if fd.RedirectURL != "" {
|
||||||
|
fetcher.redirectURL = fd.RedirectURL
|
||||||
|
}
|
||||||
return fetcher
|
return fetcher
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -65,14 +65,23 @@ func extractRequestError(err error) *RequestError {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// buildRequest creates an HTTP request using the redirect URL if available.
|
||||||
// HTTP Request/Client Building
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
func (f *Fetcher) buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request, err error) {
|
func (f *Fetcher) buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request, err error) {
|
||||||
|
return f.buildRequestWithURL(ctx, req, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildRequestWithOriginalURL creates an HTTP request using the original URL.
|
||||||
|
// This is used for retrying when the redirect URL has expired.
|
||||||
|
func (f *Fetcher) buildRequestWithOriginalURL(ctx context.Context, req *base.Request) (httpReq *http.Request, err error) {
|
||||||
|
return f.buildRequestWithURL(ctx, req, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildRequestWithURL creates an HTTP request.
|
||||||
|
// If useRedirect is true and a redirect URL exists, it will be used; otherwise the original URL is used.
|
||||||
|
func (f *Fetcher) buildRequestWithURL(ctx context.Context, req *base.Request, useRedirect bool) (httpReq *http.Request, err error) {
|
||||||
var reqUrl string
|
var reqUrl string
|
||||||
f.redirectLock.Lock()
|
f.redirectLock.Lock()
|
||||||
if f.redirectURL != "" {
|
if useRedirect && f.redirectURL != "" {
|
||||||
reqUrl = f.redirectURL
|
reqUrl = f.redirectURL
|
||||||
} else {
|
} else {
|
||||||
reqUrl = req.URL
|
reqUrl = req.URL
|
||||||
@@ -121,6 +130,65 @@ func (f *Fetcher) buildRequest(ctx context.Context, req *base.Request) (httpReq
|
|||||||
return httpReq, nil
|
return httpReq, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// updateRedirectURL updates the redirect URL from the response.
|
||||||
|
// This is called when a request using the original URL succeeds after the redirect URL expired.
|
||||||
|
func (f *Fetcher) updateRedirectURL(url string) {
|
||||||
|
f.redirectLock.Lock()
|
||||||
|
f.redirectURL = url
|
||||||
|
f.redirectLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// hasRedirectURL checks if a redirect URL exists and is different from the original URL.
|
||||||
|
func (f *Fetcher) hasRedirectURL() bool {
|
||||||
|
f.redirectLock.Lock()
|
||||||
|
defer f.redirectLock.Unlock()
|
||||||
|
return f.redirectURL != "" && f.redirectURL != f.meta.Req.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
// isRedirectExpiredError checks if the error indicates that the redirect URL may have expired.
|
||||||
|
// This includes 403 (Forbidden), 401 (Unauthorized), 410 (Gone), and network errors.
|
||||||
|
func isRedirectExpiredError(err error) bool {
|
||||||
|
if err == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for specific HTTP error codes that might indicate URL expiration
|
||||||
|
if re := extractRequestError(err); re != nil {
|
||||||
|
switch re.Code {
|
||||||
|
case 401, 403, 404, 410:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// tryFallbackToOriginalURL attempts to make a request using the original URL
|
||||||
|
// when the redirect URL has expired. Returns the response if successful.
|
||||||
|
func (f *Fetcher) tryFallbackToOriginalURL(ctx context.Context, client *http.Client, rangeStart, rangeEnd int64) (*http.Response, error) {
|
||||||
|
httpReq, err := f.buildRequestWithOriginalURL(ctx, f.meta.Req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.meta.Res.Range && rangeEnd > 0 {
|
||||||
|
httpReq.Header.Set(base.HttpHeaderRange,
|
||||||
|
fmt.Sprintf(base.HttpHeaderRangeFormat, rangeStart, rangeEnd))
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(httpReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != base.HttpCodeOK && resp.StatusCode != base.HttpCodePartialContent {
|
||||||
|
resp.Body.Close()
|
||||||
|
return nil, NewRequestError(resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
// buildClient creates an HTTP client with the default connection timeout.
|
// buildClient creates an HTTP client with the default connection timeout.
|
||||||
// Used for resolve phase where we don't have connection time data yet.
|
// Used for resolve phase where we don't have connection time data yet.
|
||||||
func (f *Fetcher) buildClient() *http.Client {
|
func (f *Fetcher) buildClient() *http.Client {
|
||||||
|
|||||||
@@ -335,6 +335,104 @@ func StartTestOneTimeServer() net.Listener {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StartTestNoRangeSlowServer creates a server that always returns the full file
|
||||||
|
// with Content-Length but does not support Range requests.
|
||||||
|
func StartTestNoRangeSlowServer(delayPerChunk time.Duration) net.Listener {
|
||||||
|
return startTestServer(func(sl *shutdownListener) http.Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/"+BuildName, func(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
writer.Header().Set("Content-Length", fmt.Sprintf("%d", BuildSize))
|
||||||
|
|
||||||
|
file, err := os.Open(BuildFile)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
buf := make([]byte, 256*1024)
|
||||||
|
for !sl.isShutdown {
|
||||||
|
n, readErr := file.Read(buf)
|
||||||
|
if n > 0 {
|
||||||
|
if _, writeErr := writer.Write(buf[:n]); writeErr != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if flusher, ok := writer.(http.Flusher); ok {
|
||||||
|
flusher.Flush()
|
||||||
|
}
|
||||||
|
if delayPerChunk > 0 {
|
||||||
|
time.Sleep(delayPerChunk)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if readErr != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return mux
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartTestExpiringRedirectServer creates a server that simulates expiring redirect URLs.
|
||||||
|
// The original URL redirects to a temporary URL that expires after a specified number of requests.
|
||||||
|
// When the temporary URL expires (returns 403), the client should retry with the original URL
|
||||||
|
// to get a new redirect URL.
|
||||||
|
// Parameters:
|
||||||
|
// - requestsBeforeExpire: number of requests the temporary URL accepts before expiring
|
||||||
|
// - delayPerByte: optional delay per byte for slow transfer (use 0 for no delay)
|
||||||
|
func StartTestExpiringRedirectServer(requestsBeforeExpire int32, delayPerByte time.Duration) net.Listener {
|
||||||
|
var redirectVersion atomic.Int32
|
||||||
|
var requestCount atomic.Int32
|
||||||
|
redirectVersion.Store(1)
|
||||||
|
|
||||||
|
return startTestServer(func(sl *shutdownListener) http.Handler {
|
||||||
|
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
path := request.URL.Path
|
||||||
|
|
||||||
|
// Original URL - redirects to current version of temporary URL
|
||||||
|
if path == "/"+BuildName {
|
||||||
|
// Reset request count when original URL is accessed
|
||||||
|
requestCount.Store(0)
|
||||||
|
// Redirect to versioned temporary URL
|
||||||
|
version := redirectVersion.Load()
|
||||||
|
redirectURL := fmt.Sprintf("/redirect-v%d/%s", version, BuildName)
|
||||||
|
http.Redirect(writer, request, redirectURL, http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Temporary URL handler - matches /redirect-v{N}/... pattern
|
||||||
|
if strings.HasPrefix(path, "/redirect-v") {
|
||||||
|
// Check if the redirect has expired
|
||||||
|
count := requestCount.Add(1)
|
||||||
|
if count > requestsBeforeExpire {
|
||||||
|
// Redirect expired - increment version for next redirect
|
||||||
|
redirectVersion.Add(1)
|
||||||
|
writer.WriteHeader(403)
|
||||||
|
writer.Write([]byte("Redirect URL expired"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serve the file with range support
|
||||||
|
rangeFileHandle(
|
||||||
|
writer,
|
||||||
|
request,
|
||||||
|
nil,
|
||||||
|
func(file *os.File, n int64) {
|
||||||
|
if delayPerByte > 0 {
|
||||||
|
slowCopyNWithDelay(sl, writer, file, n, delayPerByte)
|
||||||
|
} else {
|
||||||
|
io.CopyN(writer, file, n)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not found
|
||||||
|
writer.WriteHeader(404)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// StartTestSlowStartServer creates a server with configurable delay per request
|
// StartTestSlowStartServer creates a server with configurable delay per request
|
||||||
// This allows testing slow-start connection expansion to reach max connections
|
// This allows testing slow-start connection expansion to reach max connections
|
||||||
func StartTestSlowStartServer(delayPerByte time.Duration) net.Listener {
|
func StartTestSlowStartServer(delayPerByte time.Duration) net.Listener {
|
||||||
@@ -496,6 +594,57 @@ func StartTestTemporary500Server(errorDuration time.Duration) net.Listener {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StartTestFailThenRecoverServer creates a server that fails all connections initially,
|
||||||
|
// then recovers after a specified number of failed requests.
|
||||||
|
// This tests the retry functionality when calling Start() again after a download fails.
|
||||||
|
// Parameters:
|
||||||
|
// - failedRequestsBeforeRecover: number of requests that will fail before server recovers
|
||||||
|
//
|
||||||
|
// Note: Uses 416 (Range Not Satisfiable) instead of 500 because 5xx errors are exempt
|
||||||
|
// from failure counting and will retry indefinitely. 416 is counted as a failure.
|
||||||
|
func StartTestFailThenRecoverServer(failedRequestsBeforeRecover int32) net.Listener {
|
||||||
|
var requestCount atomic.Int32
|
||||||
|
|
||||||
|
return startTestServer(func(sl *shutdownListener) http.Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/"+BuildName, func(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
reqNum := requestCount.Add(1)
|
||||||
|
|
||||||
|
// First request (Resolve) always succeeds
|
||||||
|
if reqNum == 1 {
|
||||||
|
rangeFileHandle(
|
||||||
|
writer,
|
||||||
|
request,
|
||||||
|
nil,
|
||||||
|
func(file *os.File, n int64) {
|
||||||
|
io.CopyN(writer, file, n)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subsequent requests fail until we've had enough failures
|
||||||
|
// Use 416 Range Not Satisfiable - this error is counted towards failure limit
|
||||||
|
if reqNum <= failedRequestsBeforeRecover+1 { // +1 because first request is resolve
|
||||||
|
writer.WriteHeader(416)
|
||||||
|
writer.Write([]byte("Range Not Satisfiable"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// After enough failures, server recovers
|
||||||
|
rangeFileHandle(
|
||||||
|
writer,
|
||||||
|
request,
|
||||||
|
nil,
|
||||||
|
func(file *os.File, n int64) {
|
||||||
|
io.CopyN(writer, file, n)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
return mux
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// StartTestRangeBugServer simulate bug server:
|
// StartTestRangeBugServer simulate bug server:
|
||||||
// Don't follow Range request rules, always return more data than range, e.g. Range: bytes=0-100, return 150 bytes
|
// Don't follow Range request rules, always return more data than range, e.g. Range: bytes=0-100, return 150 bytes
|
||||||
func StartTestRangeBugServer() net.Listener {
|
func StartTestRangeBugServer() net.Listener {
|
||||||
@@ -739,6 +888,89 @@ func StartSocks5Server(usr, pwd string) net.Listener {
|
|||||||
return listener
|
return listener
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StartTestPatchURLServer creates a server with two endpoints:
|
||||||
|
// - /bad-url: always returns 404 (simulates a broken download link)
|
||||||
|
// - /good-url: returns the file successfully (simulates a working download link)
|
||||||
|
// This is used to test the Patch functionality where a failed download URL can be
|
||||||
|
// replaced with a working one.
|
||||||
|
func StartTestPatchURLServer() net.Listener {
|
||||||
|
return startTestServer(func(sl *shutdownListener) http.Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
// Bad URL - always fails with 404
|
||||||
|
mux.HandleFunc("/bad-url", func(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
writer.WriteHeader(404)
|
||||||
|
writer.Write([]byte("Not Found"))
|
||||||
|
})
|
||||||
|
// Good URL - returns the file successfully with range support
|
||||||
|
mux.HandleFunc("/good-url", func(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
rangeFileHandle(
|
||||||
|
writer,
|
||||||
|
request,
|
||||||
|
nil,
|
||||||
|
func(file *os.File, n int64) {
|
||||||
|
io.CopyN(writer, file, n)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
return mux
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// StartTestCookieExpiringServer creates a server that simulates cookie expiration during download.
|
||||||
|
// - First request (Resolve): accepts "session=old_token" and succeeds
|
||||||
|
// - Subsequent requests: "session=old_token" is expired (returns 401), only "session=new_token" works
|
||||||
|
// This is used to test the Patch functionality where a cookie expires mid-download,
|
||||||
|
// requiring the user to patch with a new cookie to resume.
|
||||||
|
func StartTestCookieExpiringServer() net.Listener {
|
||||||
|
var requestCount atomic.Int32
|
||||||
|
|
||||||
|
return startTestServer(func(sl *shutdownListener) http.Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/"+BuildName, func(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
reqNum := requestCount.Add(1)
|
||||||
|
cookie := request.Header.Get("Cookie")
|
||||||
|
|
||||||
|
// First request (Resolve): accept old_token
|
||||||
|
if reqNum == 1 {
|
||||||
|
if !strings.Contains(cookie, "session=old_token") {
|
||||||
|
writer.WriteHeader(401)
|
||||||
|
writer.Write([]byte("Unauthorized: Invalid cookie"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Return file info for resolve (with range support)
|
||||||
|
rangeFileHandle(
|
||||||
|
writer,
|
||||||
|
request,
|
||||||
|
nil,
|
||||||
|
func(file *os.File, n int64) {
|
||||||
|
io.CopyN(writer, file, n)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subsequent requests: old_token is expired, only new_token works
|
||||||
|
if strings.Contains(cookie, "session=new_token") {
|
||||||
|
// New token is valid - return the file with range support
|
||||||
|
rangeFileHandle(
|
||||||
|
writer,
|
||||||
|
request,
|
||||||
|
nil,
|
||||||
|
func(file *os.File, n int64) {
|
||||||
|
io.CopyN(writer, file, n)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Old token or invalid token - return 401
|
||||||
|
writer.WriteHeader(401)
|
||||||
|
writer.Write([]byte("Unauthorized: Cookie expired"))
|
||||||
|
})
|
||||||
|
return mux
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func AssertResourceEqual(want, got *base.Resource) bool {
|
func AssertResourceEqual(want, got *base.Resource) bool {
|
||||||
// Ignore ctime
|
// Ignore ctime
|
||||||
if got != nil && len(got.Files) > 0 {
|
if got != nil && len(got.Files) > 0 {
|
||||||
|
|||||||
+15
-2
@@ -78,7 +78,7 @@ func (r *Resource) Validate() error {
|
|||||||
if r.Name == "" {
|
if r.Name == "" {
|
||||||
return fmt.Errorf("invalid resource name")
|
return fmt.Errorf("invalid resource name")
|
||||||
}
|
}
|
||||||
if r.Files == nil || len(r.Files) == 0 {
|
if len(r.Files) == 0 {
|
||||||
return fmt.Errorf("invalid resource files")
|
return fmt.Errorf("invalid resource files")
|
||||||
}
|
}
|
||||||
for _, file := range r.Files {
|
for _, file := range r.Files {
|
||||||
@@ -124,7 +124,7 @@ func (o *Options) InitSelectFiles(fileSize int) {
|
|||||||
// if selectFiles is empty, select all files
|
// if selectFiles is empty, select all files
|
||||||
if len(o.SelectFiles) == 0 {
|
if len(o.SelectFiles) == 0 {
|
||||||
o.SelectFiles = make([]int, fileSize)
|
o.SelectFiles = make([]int, fileSize)
|
||||||
for i := 0; i < fileSize; i++ {
|
for i := range fileSize {
|
||||||
o.SelectFiles[i] = i
|
o.SelectFiles[i] = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,6 +184,7 @@ type DownloaderStoreConfig struct {
|
|||||||
Extra map[string]any `json:"extra"`
|
Extra map[string]any `json:"extra"`
|
||||||
Proxy *DownloaderProxyConfig `json:"proxy"`
|
Proxy *DownloaderProxyConfig `json:"proxy"`
|
||||||
Webhook *WebhookConfig `json:"webhook"` // Webhook is the webhook configuration
|
Webhook *WebhookConfig `json:"webhook"` // Webhook is the webhook configuration
|
||||||
|
Script *ScriptConfig `json:"script"` // Script is the script execution configuration
|
||||||
AutoTorrent *AutoTorrentConfig `json:"autoTorrent"` // AutoTorrent is the auto torrent task creation configuration
|
AutoTorrent *AutoTorrentConfig `json:"autoTorrent"` // AutoTorrent is the auto torrent task creation configuration
|
||||||
Archive *ArchiveConfig `json:"archive"` // Archive is the archive extraction configuration
|
Archive *ArchiveConfig `json:"archive"` // Archive is the archive extraction configuration
|
||||||
AutoDeleteMissingFileTasks bool `json:"autoDeleteMissingFileTasks"` // AutoDeleteMissingFileTasks enables automatic deletion of tasks with missing files
|
AutoDeleteMissingFileTasks bool `json:"autoDeleteMissingFileTasks"` // AutoDeleteMissingFileTasks enables automatic deletion of tasks with missing files
|
||||||
@@ -202,6 +203,9 @@ func (cfg *DownloaderStoreConfig) Init() *DownloaderStoreConfig {
|
|||||||
if cfg.Webhook == nil {
|
if cfg.Webhook == nil {
|
||||||
cfg.Webhook = &WebhookConfig{}
|
cfg.Webhook = &WebhookConfig{}
|
||||||
}
|
}
|
||||||
|
if cfg.Script == nil {
|
||||||
|
cfg.Script = &ScriptConfig{}
|
||||||
|
}
|
||||||
if cfg.AutoTorrent == nil {
|
if cfg.AutoTorrent == nil {
|
||||||
cfg.AutoTorrent = &AutoTorrentConfig{
|
cfg.AutoTorrent = &AutoTorrentConfig{
|
||||||
Enable: false,
|
Enable: false,
|
||||||
@@ -239,6 +243,9 @@ func (cfg *DownloaderStoreConfig) Merge(beforeCfg *DownloaderStoreConfig) *Downl
|
|||||||
if cfg.Webhook == nil {
|
if cfg.Webhook == nil {
|
||||||
cfg.Webhook = beforeCfg.Webhook
|
cfg.Webhook = beforeCfg.Webhook
|
||||||
}
|
}
|
||||||
|
if cfg.Script == nil {
|
||||||
|
cfg.Script = beforeCfg.Script
|
||||||
|
}
|
||||||
if cfg.AutoTorrent == nil {
|
if cfg.AutoTorrent == nil {
|
||||||
cfg.AutoTorrent = beforeCfg.AutoTorrent
|
cfg.AutoTorrent = beforeCfg.AutoTorrent
|
||||||
}
|
}
|
||||||
@@ -254,6 +261,12 @@ type WebhookConfig struct {
|
|||||||
URLs []string `json:"urls"` // URLs is the list of webhook URLs
|
URLs []string `json:"urls"` // URLs is the list of webhook URLs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ScriptConfig is the script execution configuration
|
||||||
|
type ScriptConfig struct {
|
||||||
|
Enable bool `json:"enable"` // Enable is the flag to enable/disable script execution
|
||||||
|
Paths []string `json:"paths"` // Paths is the list of script paths to execute
|
||||||
|
}
|
||||||
|
|
||||||
// AutoTorrentConfig is the auto torrent task creation configuration
|
// AutoTorrentConfig is the auto torrent task creation configuration
|
||||||
type AutoTorrentConfig struct {
|
type AutoTorrentConfig struct {
|
||||||
Enable bool `json:"enable"` // Enable enables automatic BT task creation when downloading .torrent files
|
Enable bool `json:"enable"` // Enable enables automatic BT task creation when downloading .torrent files
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ func TestDownloaderStoreConfig_Init(t *testing.T) {
|
|||||||
ProtocolConfig: map[string]any{},
|
ProtocolConfig: map[string]any{},
|
||||||
Proxy: &DownloaderProxyConfig{},
|
Proxy: &DownloaderProxyConfig{},
|
||||||
Webhook: &WebhookConfig{},
|
Webhook: &WebhookConfig{},
|
||||||
|
Script: &ScriptConfig{},
|
||||||
AutoTorrent: &AutoTorrentConfig{
|
AutoTorrent: &AutoTorrentConfig{
|
||||||
Enable: false,
|
Enable: false,
|
||||||
DeleteAfterDownload: false,
|
DeleteAfterDownload: false,
|
||||||
@@ -39,6 +40,7 @@ func TestDownloaderStoreConfig_Init(t *testing.T) {
|
|||||||
ProtocolConfig: map[string]any{},
|
ProtocolConfig: map[string]any{},
|
||||||
Proxy: &DownloaderProxyConfig{},
|
Proxy: &DownloaderProxyConfig{},
|
||||||
Webhook: &WebhookConfig{},
|
Webhook: &WebhookConfig{},
|
||||||
|
Script: &ScriptConfig{},
|
||||||
AutoTorrent: &AutoTorrentConfig{
|
AutoTorrent: &AutoTorrentConfig{
|
||||||
Enable: false,
|
Enable: false,
|
||||||
DeleteAfterDownload: false,
|
DeleteAfterDownload: false,
|
||||||
@@ -63,6 +65,7 @@ func TestDownloaderStoreConfig_Init(t *testing.T) {
|
|||||||
},
|
},
|
||||||
Proxy: &DownloaderProxyConfig{},
|
Proxy: &DownloaderProxyConfig{},
|
||||||
Webhook: &WebhookConfig{},
|
Webhook: &WebhookConfig{},
|
||||||
|
Script: &ScriptConfig{},
|
||||||
AutoTorrent: &AutoTorrentConfig{
|
AutoTorrent: &AutoTorrentConfig{
|
||||||
Enable: false,
|
Enable: false,
|
||||||
DeleteAfterDownload: false,
|
DeleteAfterDownload: false,
|
||||||
@@ -87,6 +90,7 @@ func TestDownloaderStoreConfig_Init(t *testing.T) {
|
|||||||
Enable: true,
|
Enable: true,
|
||||||
},
|
},
|
||||||
Webhook: &WebhookConfig{},
|
Webhook: &WebhookConfig{},
|
||||||
|
Script: &ScriptConfig{},
|
||||||
AutoTorrent: &AutoTorrentConfig{
|
AutoTorrent: &AutoTorrentConfig{
|
||||||
Enable: false,
|
Enable: false,
|
||||||
DeleteAfterDownload: false,
|
DeleteAfterDownload: false,
|
||||||
@@ -110,6 +114,7 @@ func TestDownloaderStoreConfig_Init(t *testing.T) {
|
|||||||
ProtocolConfig: map[string]any{},
|
ProtocolConfig: map[string]any{},
|
||||||
Proxy: &DownloaderProxyConfig{},
|
Proxy: &DownloaderProxyConfig{},
|
||||||
Webhook: &WebhookConfig{},
|
Webhook: &WebhookConfig{},
|
||||||
|
Script: &ScriptConfig{},
|
||||||
AutoTorrent: &AutoTorrentConfig{
|
AutoTorrent: &AutoTorrentConfig{
|
||||||
Enable: true,
|
Enable: true,
|
||||||
DeleteAfterDownload: true,
|
DeleteAfterDownload: true,
|
||||||
@@ -133,6 +138,7 @@ func TestDownloaderStoreConfig_Init(t *testing.T) {
|
|||||||
ProtocolConfig: map[string]any{},
|
ProtocolConfig: map[string]any{},
|
||||||
Proxy: &DownloaderProxyConfig{},
|
Proxy: &DownloaderProxyConfig{},
|
||||||
Webhook: &WebhookConfig{},
|
Webhook: &WebhookConfig{},
|
||||||
|
Script: &ScriptConfig{},
|
||||||
AutoTorrent: &AutoTorrentConfig{
|
AutoTorrent: &AutoTorrentConfig{
|
||||||
Enable: false,
|
Enable: false,
|
||||||
DeleteAfterDownload: false,
|
DeleteAfterDownload: false,
|
||||||
@@ -154,6 +160,7 @@ func TestDownloaderStoreConfig_Init(t *testing.T) {
|
|||||||
Extra: tt.fields.Extra,
|
Extra: tt.fields.Extra,
|
||||||
Proxy: tt.fields.Proxy,
|
Proxy: tt.fields.Proxy,
|
||||||
Webhook: tt.fields.Webhook,
|
Webhook: tt.fields.Webhook,
|
||||||
|
Script: tt.fields.Script,
|
||||||
AutoTorrent: tt.fields.AutoTorrent,
|
AutoTorrent: tt.fields.AutoTorrent,
|
||||||
Archive: tt.fields.Archive,
|
Archive: tt.fields.Archive,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ const (
|
|||||||
bucketTask = "task"
|
bucketTask = "task"
|
||||||
// task download data bucket
|
// task download data bucket
|
||||||
bucketSave = "save"
|
bucketSave = "save"
|
||||||
|
// protocol-level shared client state bucket
|
||||||
|
bucketProtocolState = "protocol_state"
|
||||||
// downloader config bucket
|
// downloader config bucket
|
||||||
bucketConfig = "config"
|
bucketConfig = "config"
|
||||||
// downloader extension bucket
|
// downloader extension bucket
|
||||||
@@ -141,7 +143,7 @@ func NewDownloader(cfg *DownloaderConfig) *Downloader {
|
|||||||
|
|
||||||
func (d *Downloader) Setup() error {
|
func (d *Downloader) Setup() error {
|
||||||
// setup storage
|
// setup storage
|
||||||
if err := d.storage.Setup([]string{bucketTask, bucketSave, bucketConfig, bucketExtension, bucketExtensionStorage}); err != nil {
|
if err := d.storage.Setup([]string{bucketTask, bucketSave, bucketProtocolState, bucketConfig, bucketExtension, bucketExtensionStorage}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// load config from storage
|
// load config from storage
|
||||||
@@ -165,6 +167,12 @@ func (d *Downloader) Setup() error {
|
|||||||
if _, ok := d.cfg.DownloaderStoreConfig.ProtocolConfig[protocol]; !ok {
|
if _, ok := d.cfg.DownloaderStoreConfig.ProtocolConfig[protocol]; !ok {
|
||||||
d.cfg.DownloaderStoreConfig.ProtocolConfig[protocol] = fm.DefaultConfig()
|
d.cfg.DownloaderStoreConfig.ProtocolConfig[protocol] = fm.DefaultConfig()
|
||||||
}
|
}
|
||||||
|
if sfm, ok := fm.(fetcher.StatefulFetcherManager); ok {
|
||||||
|
sfm.SetStateStore(&protocolStateStore{
|
||||||
|
storage: d.storage,
|
||||||
|
protocol: protocol,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// load tasks from storage
|
// load tasks from storage
|
||||||
@@ -358,10 +366,17 @@ func (d *Downloader) saveTask(task *Task) error {
|
|||||||
d.Logger.Error().Stack().Err(err).Msgf("serialize fetcher failed: %s", task.ID)
|
d.Logger.Error().Stack().Err(err).Msgf("serialize fetcher failed: %s", task.ID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if data != nil {
|
||||||
if err := d.storage.Put(bucketSave, task.ID, data); err != nil {
|
if err := d.storage.Put(bucketSave, task.ID, data); err != nil {
|
||||||
d.Logger.Error().Stack().Err(err).Msgf("persist fetcher failed: %s", task.ID)
|
d.Logger.Error().Stack().Err(err).Msgf("persist fetcher failed: %s", task.ID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if err := d.storage.Delete(bucketSave, task.ID); err != nil {
|
||||||
|
d.Logger.Error().Stack().Err(err).Msgf("clear fetcher state failed: %s", task.ID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
if err := d.storage.Put(bucketTask, task.ID, task); err != nil {
|
if err := d.storage.Put(bucketTask, task.ID, task); err != nil {
|
||||||
d.Logger.Error().Stack().Err(err).Msgf("persist task failed: %s", task.ID)
|
d.Logger.Error().Stack().Err(err).Msgf("persist task failed: %s", task.ID)
|
||||||
return err
|
return err
|
||||||
@@ -480,6 +495,47 @@ func (d *Downloader) Create(rrId string) (taskId string, err error) {
|
|||||||
return d.doCreate(fetcher, nil)
|
return d.doCreate(fetcher, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Patch modifies task-specific data based on the protocol.
|
||||||
|
// For HTTP protocol, it can modify Request info.
|
||||||
|
// For BT protocol, it can modify SelectFiles.
|
||||||
|
func (d *Downloader) Patch(id string, req *base.Request, opts *base.Options) error {
|
||||||
|
task := d.GetTask(id)
|
||||||
|
if task == nil {
|
||||||
|
return ErrTaskNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore fetcher if not loaded
|
||||||
|
if task.fetcher == nil {
|
||||||
|
err := func() error {
|
||||||
|
task.statusLock.Lock()
|
||||||
|
defer task.statusLock.Unlock()
|
||||||
|
|
||||||
|
return d.restoreFetcher(task)
|
||||||
|
}()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the fetcher's Patch method
|
||||||
|
if err := task.fetcher.Patch(req, opts); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update task meta from fetcher
|
||||||
|
task.Meta = task.fetcher.Meta()
|
||||||
|
|
||||||
|
// Save task to storage
|
||||||
|
if err := d.saveTask(task); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emit progress event to notify listeners
|
||||||
|
d.emit(EventKeyProgress, task)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Downloader) Pause(filter *TaskFilter) (err error) {
|
func (d *Downloader) Pause(filter *TaskFilter) (err error) {
|
||||||
if filter == nil || filter.IsEmpty() {
|
if filter == nil || filter.IsEmpty() {
|
||||||
return d.pauseAll()
|
return d.pauseAll()
|
||||||
@@ -779,6 +835,26 @@ func (d *Downloader) Clear() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type protocolStateStore struct {
|
||||||
|
storage Storage
|
||||||
|
protocol string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *protocolStateStore) Load(v any) (bool, error) {
|
||||||
|
return s.storage.Get(bucketProtocolState, s.protocol, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *protocolStateStore) Save(v any) error {
|
||||||
|
if v == nil {
|
||||||
|
return s.Delete()
|
||||||
|
}
|
||||||
|
return s.storage.Put(bucketProtocolState, s.protocol, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *protocolStateStore) Delete() error {
|
||||||
|
return s.storage.Delete(bucketProtocolState, s.protocol)
|
||||||
|
}
|
||||||
|
|
||||||
func (d *Downloader) Listener(fn Listener) {
|
func (d *Downloader) Listener(fn Listener) {
|
||||||
d.listener = fn
|
d.listener = fn
|
||||||
}
|
}
|
||||||
@@ -954,6 +1030,7 @@ func (d *Downloader) watch(task *Task) {
|
|||||||
d.notifyRunning()
|
d.notifyRunning()
|
||||||
d.triggerOnDone(task)
|
d.triggerOnDone(task)
|
||||||
d.triggerWebhooks(WebhookEventDownloadDone, task, nil)
|
d.triggerWebhooks(WebhookEventDownloadDone, task, nil)
|
||||||
|
d.triggerScripts(ScriptEventDownloadDone, task, nil)
|
||||||
|
|
||||||
if e, ok := task.Meta.Opts.Extra.(*http.OptsExtra); ok {
|
if e, ok := task.Meta.Opts.Extra.(*http.OptsExtra); ok {
|
||||||
downloadFilePath := task.Meta.SingleFilepath()
|
downloadFilePath := task.Meta.SingleFilepath()
|
||||||
@@ -1144,7 +1221,7 @@ func (d *Downloader) statusMut(task *Task, fn func() (bool, error)) (bool, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) doStart(task *Task) (err error) {
|
func (d *Downloader) doStart(task *Task) (err error) {
|
||||||
var isCreate bool
|
var needCreate bool
|
||||||
isReturn, err := d.statusMut(task, func() (isReturn bool, err error) {
|
isReturn, err := d.statusMut(task, func() (isReturn bool, err error) {
|
||||||
if task.Status == base.DownloadStatusRunning || task.Status == base.DownloadStatusDone {
|
if task.Status == base.DownloadStatusRunning || task.Status == base.DownloadStatusDone {
|
||||||
isReturn = true
|
isReturn = true
|
||||||
@@ -1156,7 +1233,7 @@ func (d *Downloader) doStart(task *Task) (err error) {
|
|||||||
d.Logger.Error().Stack().Err(err).Msgf("restore fetcher failed, task id: %s", task.ID)
|
d.Logger.Error().Stack().Err(err).Msgf("restore fetcher failed, task id: %s", task.ID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
isCreate = task.Status == base.DownloadStatusReady
|
needCreate = !task.IsCreated
|
||||||
task.updateStatus(base.DownloadStatusRunning)
|
task.updateStatus(base.DownloadStatusRunning)
|
||||||
|
|
||||||
return
|
return
|
||||||
@@ -1182,7 +1259,7 @@ func (d *Downloader) doStart(task *Task) (err error) {
|
|||||||
task.Meta.Res = task.fetcher.Meta().Res
|
task.Meta.Res = task.fetcher.Meta().Res
|
||||||
}
|
}
|
||||||
|
|
||||||
if isCreate {
|
if needCreate {
|
||||||
if task.fetcherManager.AutoRename() {
|
if task.fetcherManager.AutoRename() {
|
||||||
d.checkDuplicateLock.Lock()
|
d.checkDuplicateLock.Lock()
|
||||||
defer d.checkDuplicateLock.Unlock()
|
defer d.checkDuplicateLock.Unlock()
|
||||||
@@ -1206,10 +1283,9 @@ func (d *Downloader) doStart(task *Task) (err error) {
|
|||||||
task.Meta.Opts.Name = newName
|
task.Meta.Opts.Name = newName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
task.IsCreated = true
|
||||||
task.Meta.Res.CalcSize(task.Meta.Opts.SelectFiles)
|
task.Meta.Res.CalcSize(task.Meta.Opts.SelectFiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
task.Progress.Speed = 0
|
task.Progress.Speed = 0
|
||||||
task.timer.Start()
|
task.timer.Start()
|
||||||
if err := task.fetcher.Start(); err != nil {
|
if err := task.fetcher.Start(); err != nil {
|
||||||
@@ -1258,9 +1334,15 @@ func (d *Downloader) doPause(task *Task) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if task.fetcherManager != nil && task.fetcher != nil {
|
||||||
|
if err := d.saveTask(task); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if err := d.storage.Put(bucketTask, task.ID, task.clone()); err != nil {
|
if err := d.storage.Put(bucketTask, task.ID, task.clone()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
d.emit(EventKeyPause, task)
|
d.emit(EventKeyPause, task)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2648,3 +2648,87 @@ func TestDownloader_AutoTorrentDisabled(t *testing.T) {
|
|||||||
t.Error("Torrent file should have been downloaded")
|
t.Error("Torrent file should have been downloaded")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDownloader_PatchTask_HTTP(t *testing.T) {
|
||||||
|
listener := test.StartTestFileServer()
|
||||||
|
defer listener.Close()
|
||||||
|
|
||||||
|
downloader := NewDownloader(nil)
|
||||||
|
if err := downloader.Setup(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer downloader.Clear()
|
||||||
|
|
||||||
|
req := &base.Request{
|
||||||
|
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
|
||||||
|
Extra: &http.OptsExtra{
|
||||||
|
Connections: 2,
|
||||||
|
},
|
||||||
|
Labels: map[string]string{
|
||||||
|
"test": "value1",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
opts := &base.Options{
|
||||||
|
Path: test.Dir,
|
||||||
|
Name: test.DownloadName,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create task but don't start it yet
|
||||||
|
taskId, err := downloader.CreateDirect(req, opts)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pause the task immediately
|
||||||
|
if err := downloader.Pause(&TaskFilter{IDs: []string{taskId}}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Patch the task with new labels
|
||||||
|
patchReq := &base.Request{
|
||||||
|
Labels: map[string]string{
|
||||||
|
"test": "value2",
|
||||||
|
"newKey": "newValue",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := downloader.Patch(taskId, patchReq, nil); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the patch was applied
|
||||||
|
task := downloader.GetTask(taskId)
|
||||||
|
if task == nil {
|
||||||
|
t.Fatal("task not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
if task.Meta.Req.Labels["test"] != "value2" {
|
||||||
|
t.Errorf("PatchTask() label 'test' = %v, want %v", task.Meta.Req.Labels["test"], "value2")
|
||||||
|
}
|
||||||
|
if task.Meta.Req.Labels["newKey"] != "newValue" {
|
||||||
|
t.Errorf("PatchTask() label 'newKey' = %v, want %v", task.Meta.Req.Labels["newKey"], "newValue")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
downloader.Delete(&TaskFilter{IDs: []string{taskId}}, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDownloader_PatchTask_NotFound(t *testing.T) {
|
||||||
|
downloader := NewDownloader(nil)
|
||||||
|
if err := downloader.Setup(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer downloader.Clear()
|
||||||
|
|
||||||
|
// Try to patch a non-existent task
|
||||||
|
patchReq := &base.Request{
|
||||||
|
Labels: map[string]string{
|
||||||
|
"test": "value",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := downloader.Patch("non-existent-id", patchReq, nil)
|
||||||
|
if err != ErrTaskNotFound {
|
||||||
|
t.Errorf("Patch() error = %v, want %v", err, ErrTaskNotFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
+19
-1
@@ -12,6 +12,7 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/mholt/archives"
|
"github.com/mholt/archives"
|
||||||
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
)
|
)
|
||||||
|
|
||||||
// supportedArchiveExtensions contains file extensions supported by mholt/archives library
|
// supportedArchiveExtensions contains file extensions supported by mholt/archives library
|
||||||
@@ -70,6 +71,16 @@ type ArchivePartInfo struct {
|
|||||||
// ExtractProgressCallback is called to report extraction progress
|
// ExtractProgressCallback is called to report extraction progress
|
||||||
type ExtractProgressCallback func(extractedFiles int, totalFiles int, progress int)
|
type ExtractProgressCallback func(extractedFiles int, totalFiles int, progress int)
|
||||||
|
|
||||||
|
// newZipFormat creates a Zip format with proper character encoding support.
|
||||||
|
// It uses GB18030 encoding to handle Chinese characters in filenames that may
|
||||||
|
// be encoded with legacy GBK/GB18030 instead of UTF-8.
|
||||||
|
func newZipFormat() archives.Zip {
|
||||||
|
return archives.Zip{
|
||||||
|
// GB18030 is a superset of GBK and handles Chinese characters correctly
|
||||||
|
TextEncoding: simplifiedchinese.GB18030,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// isArchiveFile checks if a file is a supported archive format
|
// isArchiveFile checks if a file is a supported archive format
|
||||||
func isArchiveFile(filename string) bool {
|
func isArchiveFile(filename string) bool {
|
||||||
lowerName := strings.ToLower(filename)
|
lowerName := strings.ToLower(filename)
|
||||||
@@ -109,7 +120,8 @@ func openArchive(archivePath string, password string) (*archiveInfo, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle password-protected archives
|
// Configure format-specific settings
|
||||||
|
// Handle password-protected archives and character encoding
|
||||||
if password != "" {
|
if password != "" {
|
||||||
if rar, ok := format.(archives.Rar); ok {
|
if rar, ok := format.(archives.Rar); ok {
|
||||||
rar.Password = password
|
rar.Password = password
|
||||||
@@ -121,6 +133,12 @@ func openArchive(archivePath string, password string) (*archiveInfo, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For ZIP files, configure character encoding to handle non-UTF8 filenames
|
||||||
|
// This is essential for Chinese characters encoded in GBK/GB18030
|
||||||
|
if _, ok := format.(archives.Zip); ok {
|
||||||
|
format = newZipFormat()
|
||||||
|
}
|
||||||
|
|
||||||
return &archiveInfo{
|
return &archiveInfo{
|
||||||
file: file,
|
file: file,
|
||||||
stat: stat,
|
stat: stat,
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsArchiveFile(t *testing.T) {
|
func TestIsArchiveFile(t *testing.T) {
|
||||||
@@ -243,6 +245,69 @@ func createTestZipWithMultipleFiles(path string, numFiles int) error {
|
|||||||
return w.Close()
|
return w.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// createTestZipWithChineseFilenames creates a test ZIP file with Chinese filenames encoded in GBK
|
||||||
|
func createTestZipWithChineseFilenames(path string) error {
|
||||||
|
zipFile, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer zipFile.Close()
|
||||||
|
|
||||||
|
w := zip.NewWriter(zipFile)
|
||||||
|
|
||||||
|
// Encode Chinese filenames in GBK (as some legacy Windows applications do)
|
||||||
|
encoder := simplifiedchinese.GBK.NewEncoder()
|
||||||
|
|
||||||
|
// Add a file with Chinese filename
|
||||||
|
chineseFilename := "测试文件.txt"
|
||||||
|
gbkFilename, err := encoder.String(chineseFilename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a FileHeader and manually set the Name with GBK encoding
|
||||||
|
// We need to mark it as non-UTF8 by not setting the UTF-8 flag
|
||||||
|
header := &zip.FileHeader{
|
||||||
|
Name: gbkFilename,
|
||||||
|
Method: zip.Deflate,
|
||||||
|
}
|
||||||
|
// Clear the UTF-8 bit (bit 11) to indicate non-UTF8 encoding
|
||||||
|
header.Flags = 0
|
||||||
|
|
||||||
|
f, err := w.CreateHeader(header)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = f.Write([]byte("这是测试内容"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a file in a subdirectory with Chinese name
|
||||||
|
chineseDirAndFile := "文件夹/中文内容.txt"
|
||||||
|
gbkDirAndFile, err := encoder.String(chineseDirAndFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
header2 := &zip.FileHeader{
|
||||||
|
Name: gbkDirAndFile,
|
||||||
|
Method: zip.Deflate,
|
||||||
|
}
|
||||||
|
header2.Flags = 0
|
||||||
|
|
||||||
|
f2, err := w.CreateHeader(header2)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = f2.Write([]byte("中文子文件内容"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return w.Close()
|
||||||
|
}
|
||||||
|
|
||||||
func TestOpenArchive_NonExistentFile(t *testing.T) {
|
func TestOpenArchive_NonExistentFile(t *testing.T) {
|
||||||
_, err := openArchive("/nonexistent/path/file.zip", "")
|
_, err := openArchive("/nonexistent/path/file.zip", "")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -2382,3 +2447,47 @@ func TestExtractZipMultiPart_Progress(t *testing.T) {
|
|||||||
t.Error("Expected progress callbacks")
|
t.Error("Expected progress callbacks")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test extracting ZIP files with Chinese filenames encoded in GBK/GB18030
|
||||||
|
func TestExtractArchive_ChineseFilenames(t *testing.T) {
|
||||||
|
tempDir, err := os.MkdirTemp("", "extract_chinese_test")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.RemoveAll(tempDir)
|
||||||
|
|
||||||
|
// Create a test ZIP file with Chinese filenames encoded in GBK
|
||||||
|
zipPath := filepath.Join(tempDir, "chinese.zip")
|
||||||
|
destDir := filepath.Join(tempDir, "extracted")
|
||||||
|
|
||||||
|
if err := createTestZipWithChineseFilenames(zipPath); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the archive
|
||||||
|
err = extractArchive(zipPath, destDir, "", nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("extractArchive failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify the extracted files with proper Chinese filenames
|
||||||
|
expectedFiles := []string{
|
||||||
|
filepath.Join(destDir, "测试文件.txt"),
|
||||||
|
filepath.Join(destDir, "文件夹", "中文内容.txt"),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, path := range expectedFiles {
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
t.Errorf("expected file %q not found after extraction", path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify content of the Chinese file
|
||||||
|
content, err := os.ReadFile(filepath.Join(destDir, "测试文件.txt"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if string(content) != "这是测试内容" {
|
||||||
|
t.Errorf("unexpected content: %q", string(content))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,7 +34,7 @@ func extractZipMultiPart(firstPartPath string, destDir string, password string,
|
|||||||
|
|
||||||
// First pass: count files for progress
|
// First pass: count files for progress
|
||||||
totalFiles := 0
|
totalFiles := 0
|
||||||
zip := archives.Zip{}
|
zip := newZipFormat()
|
||||||
err = zip.Extract(context.Background(), io.NewSectionReader(multiReader, 0, totalSize), func(ctx context.Context, fileInfo archives.FileInfo) error {
|
err = zip.Extract(context.Background(), io.NewSectionReader(multiReader, 0, totalSize), func(ctx context.Context, fileInfo archives.FileInfo) error {
|
||||||
if !fileInfo.IsDir() {
|
if !fileInfo.IsDir() {
|
||||||
totalFiles++
|
totalFiles++
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/GopeedLab/gopeed/internal/controller"
|
"github.com/GopeedLab/gopeed/internal/controller"
|
||||||
"github.com/GopeedLab/gopeed/internal/fetcher"
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
"github.com/GopeedLab/gopeed/internal/protocol/bt"
|
"github.com/GopeedLab/gopeed/internal/protocol/bt"
|
||||||
|
"github.com/GopeedLab/gopeed/internal/protocol/ed2k"
|
||||||
"github.com/GopeedLab/gopeed/internal/protocol/http"
|
"github.com/GopeedLab/gopeed/internal/protocol/http"
|
||||||
"github.com/GopeedLab/gopeed/pkg/base"
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
"github.com/GopeedLab/gopeed/pkg/util"
|
"github.com/GopeedLab/gopeed/pkg/util"
|
||||||
@@ -26,6 +27,7 @@ type Task struct {
|
|||||||
Status base.Status `json:"status"`
|
Status base.Status `json:"status"`
|
||||||
Uploading bool `json:"uploading"`
|
Uploading bool `json:"uploading"`
|
||||||
Progress *Progress `json:"progress"`
|
Progress *Progress `json:"progress"`
|
||||||
|
IsCreated bool `json:"isCreated"`
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
UpdatedAt time.Time `json:"updatedAt"`
|
UpdatedAt time.Time `json:"updatedAt"`
|
||||||
|
|
||||||
@@ -48,6 +50,7 @@ func NewTask() *Task {
|
|||||||
Status: base.DownloadStatusReady,
|
Status: base.DownloadStatusReady,
|
||||||
CreatedAt: time.Now(),
|
CreatedAt: time.Now(),
|
||||||
UpdatedAt: time.Now(),
|
UpdatedAt: time.Now(),
|
||||||
|
IsCreated: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,6 +113,14 @@ func (t *Task) updateUploadSpeed(downloaded int64, usedTime float64) int64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func calcSpeed(speedArr *[]int64, downloaded int64, usedTime float64) int64 {
|
func calcSpeed(speedArr *[]int64, downloaded int64, usedTime float64) int64 {
|
||||||
|
if usedTime <= 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if downloaded < 0 {
|
||||||
|
*speedArr = (*speedArr)[:0]
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
*speedArr = append(*speedArr, downloaded)
|
*speedArr = append(*speedArr, downloaded)
|
||||||
// Record last 5 seconds of download speed to calculate the average speed
|
// Record last 5 seconds of download speed to calculate the average speed
|
||||||
if len(*speedArr) > int(5.0/usedTime) {
|
if len(*speedArr) > int(5.0/usedTime) {
|
||||||
@@ -156,6 +167,7 @@ func (cfg *DownloaderConfig) Init() *DownloaderConfig {
|
|||||||
cfg.FetchManagers = []fetcher.FetcherManager{
|
cfg.FetchManagers = []fetcher.FetcherManager{
|
||||||
new(http.FetcherManager),
|
new(http.FetcherManager),
|
||||||
new(bt.FetcherManager),
|
new(bt.FetcherManager),
|
||||||
|
new(ed2k.FetcherManager),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if cfg.RefreshInterval == 0 {
|
if cfg.RefreshInterval == 0 {
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package download
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestCalcSpeedResetOnRollback(t *testing.T) {
|
||||||
|
speedArr := []int64{1024, 2048, 4096}
|
||||||
|
|
||||||
|
if got := calcSpeed(&speedArr, -512, 1); got != 0 {
|
||||||
|
t.Fatalf("calcSpeed() = %d, want 0 after rollback", got)
|
||||||
|
}
|
||||||
|
if len(speedArr) != 0 {
|
||||||
|
t.Fatalf("speed window len = %d, want 0 after rollback", len(speedArr))
|
||||||
|
}
|
||||||
|
|
||||||
|
if got := calcSpeed(&speedArr, 1024, 1); got != 1024 {
|
||||||
|
t.Fatalf("calcSpeed() = %d, want 1024 after reset", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
package download
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ScriptEvent represents the type of script event
|
||||||
|
type ScriptEvent string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ScriptEventDownloadDone ScriptEvent = "DOWNLOAD_DONE"
|
||||||
|
ScriptEventDownloadError ScriptEvent = "DOWNLOAD_ERROR"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ScriptData is the internal data structure for passing script information
|
||||||
|
type ScriptData struct {
|
||||||
|
Event ScriptEvent
|
||||||
|
Time int64 // Unix timestamp in milliseconds
|
||||||
|
Payload *ScriptPayload
|
||||||
|
}
|
||||||
|
|
||||||
|
// ScriptPayload contains the task data
|
||||||
|
type ScriptPayload struct {
|
||||||
|
Task *Task
|
||||||
|
}
|
||||||
|
|
||||||
|
// getScriptPaths extracts script paths from config
|
||||||
|
func (d *Downloader) getScriptPaths() []string {
|
||||||
|
cfg := d.cfg.DownloaderStoreConfig
|
||||||
|
if cfg == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check new script config
|
||||||
|
if cfg.Script != nil && cfg.Script.Enable && len(cfg.Script.Paths) > 0 {
|
||||||
|
paths := make([]string, 0, len(cfg.Script.Paths))
|
||||||
|
for _, path := range cfg.Script.Paths {
|
||||||
|
if path != "" {
|
||||||
|
paths = append(paths, path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(paths) > 0 {
|
||||||
|
return paths
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// executeScriptAtPath executes a single script with the given data
|
||||||
|
// Returns any error that occurred during execution
|
||||||
|
func (d *Downloader) executeScriptAtPath(scriptPath string, data *ScriptData) error {
|
||||||
|
if scriptPath == "" {
|
||||||
|
return fmt.Errorf("script path is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if script file exists
|
||||||
|
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
|
||||||
|
return fmt.Errorf("script file does not exist: %s", scriptPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine the script interpreter based on file extension
|
||||||
|
var cmd *exec.Cmd
|
||||||
|
ext := filepath.Ext(scriptPath)
|
||||||
|
|
||||||
|
switch ext {
|
||||||
|
case ".sh", ".bash":
|
||||||
|
cmd = exec.Command("bash", scriptPath)
|
||||||
|
case ".py":
|
||||||
|
cmd = exec.Command("python3", scriptPath)
|
||||||
|
case ".js":
|
||||||
|
cmd = exec.Command("node", scriptPath)
|
||||||
|
case ".bat", ".cmd":
|
||||||
|
// Windows batch files
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
cmd = exec.Command("cmd", "/c", scriptPath)
|
||||||
|
} else {
|
||||||
|
// Batch files are Windows-specific
|
||||||
|
return fmt.Errorf("batch files (.bat/.cmd) are only supported on Windows")
|
||||||
|
}
|
||||||
|
case ".ps1":
|
||||||
|
// PowerShell scripts
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
cmd = exec.Command("powershell", "-ExecutionPolicy", "Bypass", "-File", scriptPath)
|
||||||
|
} else {
|
||||||
|
// Try pwsh (PowerShell Core) on non-Windows systems
|
||||||
|
cmd = exec.Command("pwsh", "-File", scriptPath)
|
||||||
|
}
|
||||||
|
case "":
|
||||||
|
// No extension, try to execute directly (assumes shebang or executable)
|
||||||
|
cmd = exec.Command(scriptPath)
|
||||||
|
default:
|
||||||
|
// Unknown extension, try to execute directly
|
||||||
|
cmd = exec.Command(scriptPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set environment variables with task information
|
||||||
|
cmd.Env = append(os.Environ(),
|
||||||
|
fmt.Sprintf("GOPEED_EVENT=%s", data.Event),
|
||||||
|
fmt.Sprintf("GOPEED_TASK_ID=%s", data.Payload.Task.ID),
|
||||||
|
fmt.Sprintf("GOPEED_TASK_NAME=%s", data.Payload.Task.Name()),
|
||||||
|
fmt.Sprintf("GOPEED_TASK_STATUS=%s", data.Payload.Task.Status),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Add task path using the same logic as task deletion
|
||||||
|
if data.Payload.Task.Meta != nil && data.Payload.Task.Meta.Res != nil {
|
||||||
|
var taskPath string
|
||||||
|
if data.Payload.Task.Meta.Res.Name != "" {
|
||||||
|
// Multi-file task (folder)
|
||||||
|
taskPath = data.Payload.Task.Meta.FolderPath()
|
||||||
|
} else {
|
||||||
|
// Single file task
|
||||||
|
taskPath = data.Payload.Task.Meta.SingleFilepath()
|
||||||
|
}
|
||||||
|
cmd.Env = append(cmd.Env,
|
||||||
|
fmt.Sprintf("GOPEED_TASK_PATH=%s", taskPath),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start and wait for the command to complete (no timeout)
|
||||||
|
return cmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
// triggerScripts executes all configured scripts
|
||||||
|
func (d *Downloader) triggerScripts(event ScriptEvent, task *Task, err error) {
|
||||||
|
paths := d.getScriptPaths()
|
||||||
|
if len(paths) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := &ScriptData{
|
||||||
|
Event: event,
|
||||||
|
Time: time.Now().UnixMilli(),
|
||||||
|
Payload: &ScriptPayload{
|
||||||
|
Task: task.clone(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
go d.executeScripts(paths, data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Downloader) executeScripts(paths []string, data *ScriptData) {
|
||||||
|
for _, path := range paths {
|
||||||
|
if path == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
go func(scriptPath string) {
|
||||||
|
err := d.executeScriptAtPath(scriptPath, data)
|
||||||
|
if err != nil {
|
||||||
|
d.Logger.Warn().Err(err).Str("path", scriptPath).Msg("script: failed to execute")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d.Logger.Debug().Str("path", scriptPath).Msg("script: executed successfully")
|
||||||
|
}(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,203 @@
|
|||||||
|
package download
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestScript_NoScriptConfigured(t *testing.T) {
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
// Create a mock task
|
||||||
|
task := NewTask()
|
||||||
|
task.Protocol = "http"
|
||||||
|
task.Meta = &mockFetcherMeta
|
||||||
|
|
||||||
|
// Trigger script (should not panic with no scripts configured)
|
||||||
|
downloader.triggerScripts(ScriptEventDownloadDone, task, nil)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScript_GetScriptPaths_EmptyConfig(t *testing.T) {
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
paths := downloader.getScriptPaths()
|
||||||
|
if paths != nil {
|
||||||
|
t.Errorf("Expected nil, got %v", paths)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScript_GetScriptPaths_NoScriptConfig(t *testing.T) {
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
cfg, _ := downloader.GetConfig()
|
||||||
|
cfg.Script = nil
|
||||||
|
downloader.PutConfig(cfg)
|
||||||
|
|
||||||
|
paths := downloader.getScriptPaths()
|
||||||
|
if paths != nil {
|
||||||
|
t.Errorf("Expected nil, got %v", paths)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScript_GetScriptPaths_DisabledScript(t *testing.T) {
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
cfg, _ := downloader.GetConfig()
|
||||||
|
cfg.Script = &base.ScriptConfig{
|
||||||
|
Enable: false,
|
||||||
|
Paths: []string{"/path/to/script.sh"},
|
||||||
|
}
|
||||||
|
downloader.PutConfig(cfg)
|
||||||
|
|
||||||
|
paths := downloader.getScriptPaths()
|
||||||
|
if paths != nil {
|
||||||
|
t.Errorf("Expected nil for disabled script, got %v", paths)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScript_GetScriptPaths_EmptyPaths(t *testing.T) {
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
cfg, _ := downloader.GetConfig()
|
||||||
|
cfg.Script = &base.ScriptConfig{
|
||||||
|
Enable: true,
|
||||||
|
Paths: []string{},
|
||||||
|
}
|
||||||
|
downloader.PutConfig(cfg)
|
||||||
|
|
||||||
|
paths := downloader.getScriptPaths()
|
||||||
|
if paths != nil {
|
||||||
|
t.Errorf("Expected nil for empty paths, got %v", paths)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScript_GetScriptPaths_WithEmptyStrings(t *testing.T) {
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
cfg, _ := downloader.GetConfig()
|
||||||
|
cfg.Script = &base.ScriptConfig{
|
||||||
|
Enable: true,
|
||||||
|
Paths: []string{"/path/to/script1.sh", "", "/path/to/script2.sh", ""},
|
||||||
|
}
|
||||||
|
downloader.PutConfig(cfg)
|
||||||
|
|
||||||
|
paths := downloader.getScriptPaths()
|
||||||
|
if len(paths) != 2 {
|
||||||
|
t.Errorf("Expected 2 valid paths (ignoring empty strings), got %d: %v", len(paths), paths)
|
||||||
|
}
|
||||||
|
if paths[0] != "/path/to/script1.sh" || paths[1] != "/path/to/script2.sh" {
|
||||||
|
t.Errorf("Paths don't match expected values: %v", paths)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScript_ExecuteScriptAtPath_EmptyPath(t *testing.T) {
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
data := &ScriptData{
|
||||||
|
Event: ScriptEventDownloadDone,
|
||||||
|
Time: time.Now().UnixMilli(),
|
||||||
|
}
|
||||||
|
|
||||||
|
err := downloader.executeScriptAtPath("", data)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected error for empty path")
|
||||||
|
}
|
||||||
|
if err.Error() != "script path is empty" {
|
||||||
|
t.Errorf("Expected 'script path is empty' error, got: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScript_ExecuteScriptAtPath_NonExistentFile(t *testing.T) {
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
data := &ScriptData{
|
||||||
|
Event: ScriptEventDownloadDone,
|
||||||
|
Time: time.Now().UnixMilli(),
|
||||||
|
}
|
||||||
|
|
||||||
|
err := downloader.executeScriptAtPath("/non/existent/script.sh", data)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Expected error for non-existent script")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDownloadDoneTask(t *testing.T, downloadDir, fileName string) (*Task, string) {
|
||||||
|
t.Helper()
|
||||||
|
content := []byte("downloaded file")
|
||||||
|
task := NewTask()
|
||||||
|
task.Protocol = "http"
|
||||||
|
task.Status = base.DownloadStatusDone
|
||||||
|
task.Meta = &fetcher.FetcherMeta{
|
||||||
|
Req: &base.Request{
|
||||||
|
URL: "https://example.com/" + fileName,
|
||||||
|
},
|
||||||
|
Opts: &base.Options{
|
||||||
|
Name: fileName,
|
||||||
|
Path: filepath.ToSlash(downloadDir),
|
||||||
|
},
|
||||||
|
Res: &base.Resource{
|
||||||
|
Size: int64(len(content)),
|
||||||
|
Files: []*base.FileInfo{
|
||||||
|
{Name: fileName, Size: int64(len(content))},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
filePath := task.Meta.SingleFilepath()
|
||||||
|
filePathOS := filepath.FromSlash(filePath)
|
||||||
|
if err := os.MkdirAll(filepath.Dir(filePathOS), 0755); err != nil {
|
||||||
|
t.Fatalf("Failed to create download dir: %v", err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filePathOS, content, 0644); err != nil {
|
||||||
|
t.Fatalf("Failed to create download file: %v", err)
|
||||||
|
}
|
||||||
|
return task, filePath
|
||||||
|
}
|
||||||
|
|
||||||
|
func waitForFile(t *testing.T, path string, timeout time.Duration) {
|
||||||
|
t.Helper()
|
||||||
|
deadline := time.Now().Add(timeout)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
if _, err := os.Stat(path); err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
t.Fatalf("Timeout waiting for file: %s", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTestScriptPath(t *testing.T, name string) string {
|
||||||
|
t.Helper()
|
||||||
|
path := filepath.Join("testdata", "scripts", name)
|
||||||
|
if _, err := os.Stat(path); err != nil {
|
||||||
|
t.Fatalf("Missing test script %s: %v", path, err)
|
||||||
|
}
|
||||||
|
return path
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureScriptExecutable(t *testing.T, scriptPath string) {
|
||||||
|
t.Helper()
|
||||||
|
if filepath.Ext(scriptPath) != ".sh" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := os.Chmod(scriptPath, 0755); err != nil {
|
||||||
|
t.Fatalf("Failed to chmod script: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupScriptTest(t *testing.T, fn func(downloader *Downloader)) {
|
||||||
|
defaultDownloader.Setup()
|
||||||
|
defaultDownloader.cfg.StorageDir = ".test_storage"
|
||||||
|
defaultDownloader.cfg.DownloadDir = ".test_download"
|
||||||
|
defer func() {
|
||||||
|
defaultDownloader.Clear()
|
||||||
|
os.RemoveAll(defaultDownloader.cfg.StorageDir)
|
||||||
|
os.RemoveAll(defaultDownloader.cfg.DownloadDir)
|
||||||
|
}()
|
||||||
|
fn(defaultDownloader)
|
||||||
|
}
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
//go:build !windows
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package download
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestScript_TriggerOnDone_MoveFile(t *testing.T) {
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
downloadDir := filepath.Join(tmpDir, "downloads")
|
||||||
|
destDir := filepath.Join(tmpDir, "moved")
|
||||||
|
task, taskPath := createDownloadDoneTask(t, downloadDir, "test.txt")
|
||||||
|
srcPath := filepath.FromSlash(taskPath)
|
||||||
|
destFile := filepath.Join(destDir, "test.txt")
|
||||||
|
|
||||||
|
scriptPath := getTestScriptPath(t, "move.sh")
|
||||||
|
ensureScriptExecutable(t, scriptPath)
|
||||||
|
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
cfg, _ := downloader.GetConfig()
|
||||||
|
cfg.Script = &base.ScriptConfig{
|
||||||
|
Enable: true,
|
||||||
|
Paths: []string{scriptPath},
|
||||||
|
}
|
||||||
|
downloader.PutConfig(cfg)
|
||||||
|
|
||||||
|
t.Setenv("GOPEED_TEST_DEST_DIR", destDir)
|
||||||
|
downloader.triggerScripts(ScriptEventDownloadDone, task, nil)
|
||||||
|
|
||||||
|
waitForFile(t, destFile, 3*time.Second)
|
||||||
|
if _, err := os.Stat(srcPath); !os.IsNotExist(err) {
|
||||||
|
t.Errorf("Expected source file to be moved, but it still exists: %s", srcPath)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScript_MultipleScripts(t *testing.T) {
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
outputFile1 := filepath.Join(tmpDir, "output1.txt")
|
||||||
|
outputFile2 := filepath.Join(tmpDir, "output2.txt")
|
||||||
|
scriptPath1 := getTestScriptPath(t, "write_output1.sh")
|
||||||
|
ensureScriptExecutable(t, scriptPath1)
|
||||||
|
scriptPath2 := getTestScriptPath(t, "write_output2.sh")
|
||||||
|
ensureScriptExecutable(t, scriptPath2)
|
||||||
|
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
cfg, _ := downloader.GetConfig()
|
||||||
|
cfg.Script = &base.ScriptConfig{
|
||||||
|
Enable: true,
|
||||||
|
Paths: []string{scriptPath1, scriptPath2},
|
||||||
|
}
|
||||||
|
downloader.PutConfig(cfg)
|
||||||
|
|
||||||
|
downloadDir := filepath.Join(tmpDir, "downloads")
|
||||||
|
task, _ := createDownloadDoneTask(t, downloadDir, "multi.txt")
|
||||||
|
|
||||||
|
t.Setenv("GOPEED_TEST_OUTPUT_FILE_1", outputFile1)
|
||||||
|
t.Setenv("GOPEED_TEST_OUTPUT_FILE_2", outputFile2)
|
||||||
|
downloader.triggerScripts(ScriptEventDownloadDone, task, nil)
|
||||||
|
|
||||||
|
waitForFile(t, outputFile1, 3*time.Second)
|
||||||
|
waitForFile(t, outputFile2, 3*time.Second)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScript_EnvironmentVariables(t *testing.T) {
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
scriptPath := getTestScriptPath(t, "env_dump.sh")
|
||||||
|
ensureScriptExecutable(t, scriptPath)
|
||||||
|
outputFile := filepath.Join(tmpDir, "env_output.txt")
|
||||||
|
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
cfg, _ := downloader.GetConfig()
|
||||||
|
cfg.Script = &base.ScriptConfig{
|
||||||
|
Enable: true,
|
||||||
|
Paths: []string{scriptPath},
|
||||||
|
}
|
||||||
|
downloader.PutConfig(cfg)
|
||||||
|
|
||||||
|
downloadDir := filepath.Join(tmpDir, "downloads")
|
||||||
|
task, taskPath := createDownloadDoneTask(t, downloadDir, "env.txt")
|
||||||
|
|
||||||
|
t.Setenv("GOPEED_TEST_OUTPUT_FILE", outputFile)
|
||||||
|
downloader.triggerScripts(ScriptEventDownloadDone, task, nil)
|
||||||
|
|
||||||
|
waitForFile(t, outputFile, 3*time.Second)
|
||||||
|
content, err := os.ReadFile(outputFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to read output file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output := string(content)
|
||||||
|
if !strings.Contains(output, "GOPEED_EVENT=DOWNLOAD_DONE") {
|
||||||
|
t.Errorf("Expected GOPEED_EVENT in output, got: %s", output)
|
||||||
|
}
|
||||||
|
if !strings.Contains(output, "GOPEED_TASK_ID="+task.ID) {
|
||||||
|
t.Errorf("Expected GOPEED_TASK_ID in output, got: %s", output)
|
||||||
|
}
|
||||||
|
if !strings.Contains(output, "GOPEED_TASK_NAME="+task.Name()) {
|
||||||
|
t.Errorf("Expected GOPEED_TASK_NAME in output, got: %s", output)
|
||||||
|
}
|
||||||
|
if !strings.Contains(output, "GOPEED_TASK_STATUS="+string(task.Status)) {
|
||||||
|
t.Errorf("Expected GOPEED_TASK_STATUS in output, got: %s", output)
|
||||||
|
}
|
||||||
|
if !strings.Contains(output, "GOPEED_TASK_PATH="+taskPath) {
|
||||||
|
t.Errorf("Expected GOPEED_TASK_PATH in output, got: %s", output)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
//go:build windows
|
||||||
|
// +build windows
|
||||||
|
|
||||||
|
package download
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/base"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestScript_TriggerOnDone_MoveFile(t *testing.T) {
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
downloadDir := filepath.Join(tmpDir, "downloads")
|
||||||
|
destDir := filepath.Join(tmpDir, "moved")
|
||||||
|
task, taskPath := createDownloadDoneTask(t, downloadDir, "test.txt")
|
||||||
|
srcPath := filepath.FromSlash(taskPath)
|
||||||
|
destFile := filepath.Join(destDir, "test.txt")
|
||||||
|
|
||||||
|
scriptPath := getTestScriptPath(t, "move.bat")
|
||||||
|
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
cfg, _ := downloader.GetConfig()
|
||||||
|
cfg.Script = &base.ScriptConfig{
|
||||||
|
Enable: true,
|
||||||
|
Paths: []string{scriptPath},
|
||||||
|
}
|
||||||
|
downloader.PutConfig(cfg)
|
||||||
|
|
||||||
|
t.Setenv("GOPEED_TEST_DEST_DIR", destDir)
|
||||||
|
downloader.triggerScripts(ScriptEventDownloadDone, task, nil)
|
||||||
|
|
||||||
|
waitForFile(t, destFile, 5*time.Second)
|
||||||
|
if _, err := os.Stat(srcPath); !os.IsNotExist(err) {
|
||||||
|
t.Errorf("Expected source file to be moved, but it still exists: %s", srcPath)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScript_MultipleScripts(t *testing.T) {
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
outputFile1 := filepath.Join(tmpDir, "output1.txt")
|
||||||
|
outputFile2 := filepath.Join(tmpDir, "output2.txt")
|
||||||
|
scriptPath1 := getTestScriptPath(t, "write_output1.bat")
|
||||||
|
scriptPath2 := getTestScriptPath(t, "write_output2.bat")
|
||||||
|
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
cfg, _ := downloader.GetConfig()
|
||||||
|
cfg.Script = &base.ScriptConfig{
|
||||||
|
Enable: true,
|
||||||
|
Paths: []string{scriptPath1, scriptPath2},
|
||||||
|
}
|
||||||
|
downloader.PutConfig(cfg)
|
||||||
|
|
||||||
|
downloadDir := filepath.Join(tmpDir, "downloads")
|
||||||
|
task, _ := createDownloadDoneTask(t, downloadDir, "multi.txt")
|
||||||
|
|
||||||
|
t.Setenv("GOPEED_TEST_OUTPUT_FILE_1", outputFile1)
|
||||||
|
t.Setenv("GOPEED_TEST_OUTPUT_FILE_2", outputFile2)
|
||||||
|
downloader.triggerScripts(ScriptEventDownloadDone, task, nil)
|
||||||
|
|
||||||
|
waitForFile(t, outputFile1, 5*time.Second)
|
||||||
|
waitForFile(t, outputFile2, 5*time.Second)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScript_EnvironmentVariables(t *testing.T) {
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
scriptPath := getTestScriptPath(t, "env_dump.bat")
|
||||||
|
outputFile := filepath.Join(tmpDir, "env_output.txt")
|
||||||
|
|
||||||
|
setupScriptTest(t, func(downloader *Downloader) {
|
||||||
|
cfg, _ := downloader.GetConfig()
|
||||||
|
cfg.Script = &base.ScriptConfig{
|
||||||
|
Enable: true,
|
||||||
|
Paths: []string{scriptPath},
|
||||||
|
}
|
||||||
|
downloader.PutConfig(cfg)
|
||||||
|
|
||||||
|
downloadDir := filepath.Join(tmpDir, "downloads")
|
||||||
|
task, taskPath := createDownloadDoneTask(t, downloadDir, "env.txt")
|
||||||
|
|
||||||
|
t.Setenv("GOPEED_TEST_OUTPUT_FILE", outputFile)
|
||||||
|
downloader.triggerScripts(ScriptEventDownloadDone, task, nil)
|
||||||
|
|
||||||
|
waitForFile(t, outputFile, 5*time.Second)
|
||||||
|
content, err := os.ReadFile(outputFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to read output file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
output := string(content)
|
||||||
|
if !strings.Contains(output, "GOPEED_EVENT=DOWNLOAD_DONE") {
|
||||||
|
t.Errorf("Expected GOPEED_EVENT in output, got: %s", output)
|
||||||
|
}
|
||||||
|
if !strings.Contains(output, "GOPEED_TASK_ID="+task.ID) {
|
||||||
|
t.Errorf("Expected GOPEED_TASK_ID in output, got: %s", output)
|
||||||
|
}
|
||||||
|
if !strings.Contains(output, "GOPEED_TASK_NAME="+task.Name()) {
|
||||||
|
t.Errorf("Expected GOPEED_TASK_NAME in output, got: %s", output)
|
||||||
|
}
|
||||||
|
if !strings.Contains(output, "GOPEED_TASK_STATUS="+string(task.Status)) {
|
||||||
|
t.Errorf("Expected GOPEED_TASK_STATUS in output, got: %s", output)
|
||||||
|
}
|
||||||
|
if !strings.Contains(output, "GOPEED_TASK_PATH="+taskPath) {
|
||||||
|
t.Errorf("Expected GOPEED_TASK_PATH in output, got: %s", output)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
|
||||||
|
if "%GOPEED_TEST_OUTPUT_FILE%"=="" exit /b 2
|
||||||
|
echo GOPEED_EVENT=%GOPEED_EVENT% > "%GOPEED_TEST_OUTPUT_FILE%"
|
||||||
|
echo GOPEED_TASK_ID=%GOPEED_TASK_ID% >> "%GOPEED_TEST_OUTPUT_FILE%"
|
||||||
|
echo GOPEED_TASK_NAME=%GOPEED_TASK_NAME% >> "%GOPEED_TEST_OUTPUT_FILE%"
|
||||||
|
echo GOPEED_TASK_STATUS=%GOPEED_TASK_STATUS% >> "%GOPEED_TEST_OUTPUT_FILE%"
|
||||||
|
echo GOPEED_TASK_PATH=%GOPEED_TASK_PATH% >> "%GOPEED_TEST_OUTPUT_FILE%"
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
if [ -z "$GOPEED_TEST_OUTPUT_FILE" ]; then
|
||||||
|
echo "GOPEED_TEST_OUTPUT_FILE is empty" >&2
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "GOPEED_EVENT=$GOPEED_EVENT" > "$GOPEED_TEST_OUTPUT_FILE"
|
||||||
|
echo "GOPEED_TASK_ID=$GOPEED_TASK_ID" >> "$GOPEED_TEST_OUTPUT_FILE"
|
||||||
|
echo "GOPEED_TASK_NAME=$GOPEED_TASK_NAME" >> "$GOPEED_TEST_OUTPUT_FILE"
|
||||||
|
echo "GOPEED_TASK_STATUS=$GOPEED_TASK_STATUS" >> "$GOPEED_TEST_OUTPUT_FILE"
|
||||||
|
echo "GOPEED_TASK_PATH=$GOPEED_TASK_PATH" >> "$GOPEED_TEST_OUTPUT_FILE"
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
|
||||||
|
if "%GOPEED_TEST_DEST_DIR%"=="" exit /b 2
|
||||||
|
set "SRC=%GOPEED_TASK_PATH:/=\%"
|
||||||
|
set "DEST=%GOPEED_TEST_DEST_DIR%"
|
||||||
|
if not exist "%DEST%" mkdir "%DEST%"
|
||||||
|
move /Y "%SRC%" "%DEST%\" >nul
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
if [ -z "$GOPEED_TEST_DEST_DIR" ]; then
|
||||||
|
echo "GOPEED_TEST_DEST_DIR is empty" >&2
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p "$GOPEED_TEST_DEST_DIR"
|
||||||
|
mv "$GOPEED_TASK_PATH" "$GOPEED_TEST_DEST_DIR/"
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
|
||||||
|
if "%GOPEED_TEST_OUTPUT_FILE_1%"=="" exit /b 2
|
||||||
|
echo Script 1 > "%GOPEED_TEST_OUTPUT_FILE_1%"
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
if [ -z "$GOPEED_TEST_OUTPUT_FILE_1" ]; then
|
||||||
|
echo "GOPEED_TEST_OUTPUT_FILE_1 is empty" >&2
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Script 1" > "$GOPEED_TEST_OUTPUT_FILE_1"
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
|
||||||
|
if "%GOPEED_TEST_OUTPUT_FILE_2%"=="" exit /b 2
|
||||||
|
echo Script 2 > "%GOPEED_TEST_OUTPUT_FILE_2%"
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
if [ -z "$GOPEED_TEST_OUTPUT_FILE_2" ]; then
|
||||||
|
echo "GOPEED_TEST_OUTPUT_FILE_2 is empty" >&2
|
||||||
|
exit 2
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Script 2" > "$GOPEED_TEST_OUTPUT_FILE_2"
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package ed2k
|
||||||
|
|
||||||
|
type Stats struct {
|
||||||
|
State string `json:"state"`
|
||||||
|
Paused bool `json:"paused"`
|
||||||
|
ActivePeers int `json:"activePeers"`
|
||||||
|
TotalPeers int `json:"totalPeers"`
|
||||||
|
DownloadRate int `json:"downloadRate"`
|
||||||
|
Upload int64 `json:"upload"`
|
||||||
|
UploadRate int `json:"uploadRate"`
|
||||||
|
TotalDone int64 `json:"totalDone"`
|
||||||
|
TotalReceived int64 `json:"totalReceived"`
|
||||||
|
TotalWanted int64 `json:"totalWanted"`
|
||||||
|
}
|
||||||
@@ -74,6 +74,28 @@ func CreateTaskBatch(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PatchTask(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
taskId := vars["id"]
|
||||||
|
if taskId == "" {
|
||||||
|
WriteJson(w, model.NewErrorResult("param invalid: id", model.CodeInvalidParam))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var req model.ResolveTask
|
||||||
|
if ReadJson(r, w, &req) {
|
||||||
|
if err := Downloader.Patch(taskId, req.Req, req.Opts); err != nil {
|
||||||
|
if err == download.ErrTaskNotFound {
|
||||||
|
WriteJson(w, model.NewErrorResult("task not found", model.CodeTaskNotFound))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
WriteJson(w, model.NewErrorResult(err.Error()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
WriteJson(w, model.NewNilResult())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func PauseTask(w http.ResponseWriter, r *http.Request) {
|
func PauseTask(w http.ResponseWriter, r *http.Request) {
|
||||||
filter, errResult := parseIdFilter(r)
|
filter, errResult := parseIdFilter(r)
|
||||||
if errResult != nil {
|
if errResult != nil {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ type ResolveTask struct {
|
|||||||
Req *base.Request `json:"req"`
|
Req *base.Request `json:"req"`
|
||||||
Opts *base.Options `json:"opts"`
|
Opts *base.Options `json:"opts"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateTask struct {
|
type CreateTask struct {
|
||||||
Rid string `json:"rid"`
|
Rid string `json:"rid"`
|
||||||
|
|
||||||
|
|||||||
+12
-10
@@ -8,12 +8,6 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/GopeedLab/gopeed/pkg/download"
|
|
||||||
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
|
||||||
"github.com/GopeedLab/gopeed/pkg/util"
|
|
||||||
"github.com/gorilla/handlers"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net"
|
"net"
|
||||||
@@ -24,6 +18,13 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/download"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||||
|
"github.com/GopeedLab/gopeed/pkg/util"
|
||||||
|
"github.com/gorilla/handlers"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -122,6 +123,7 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
|||||||
r.Methods(http.MethodPost).Path("/api/v1/resolve").HandlerFunc(Resolve)
|
r.Methods(http.MethodPost).Path("/api/v1/resolve").HandlerFunc(Resolve)
|
||||||
r.Methods(http.MethodPost).Path("/api/v1/tasks").HandlerFunc(CreateTask)
|
r.Methods(http.MethodPost).Path("/api/v1/tasks").HandlerFunc(CreateTask)
|
||||||
r.Methods(http.MethodPost).Path("/api/v1/tasks/batch").HandlerFunc(CreateTaskBatch)
|
r.Methods(http.MethodPost).Path("/api/v1/tasks/batch").HandlerFunc(CreateTaskBatch)
|
||||||
|
r.Methods(http.MethodPatch).Path("/api/v1/tasks/{id}").HandlerFunc(PatchTask)
|
||||||
r.Methods(http.MethodPut).Path("/api/v1/tasks/{id}/pause").HandlerFunc(PauseTask)
|
r.Methods(http.MethodPut).Path("/api/v1/tasks/{id}/pause").HandlerFunc(PauseTask)
|
||||||
r.Methods(http.MethodPut).Path("/api/v1/tasks/pause").HandlerFunc(PauseTasks)
|
r.Methods(http.MethodPut).Path("/api/v1/tasks/pause").HandlerFunc(PauseTasks)
|
||||||
r.Methods(http.MethodPut).Path("/api/v1/tasks/{id}/continue").HandlerFunc(ContinueTask)
|
r.Methods(http.MethodPut).Path("/api/v1/tasks/{id}/continue").HandlerFunc(ContinueTask)
|
||||||
@@ -145,9 +147,9 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
|||||||
r.Path("/api/v1/proxy").HandlerFunc(DoProxy)
|
r.Path("/api/v1/proxy").HandlerFunc(DoProxy)
|
||||||
|
|
||||||
enableApiToken := startCfg.ApiToken != ""
|
enableApiToken := startCfg.ApiToken != ""
|
||||||
enableBasicAuth := startCfg.WebEnable && startCfg.WebAuth != nil
|
enableWebAuth := startCfg.WebEnable && startCfg.WebAuth != nil
|
||||||
if startCfg.WebEnable {
|
if startCfg.WebEnable {
|
||||||
if enableBasicAuth {
|
if enableWebAuth {
|
||||||
r.Methods(http.MethodPost).Path("/api/web/login").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
r.Methods(http.MethodPost).Path("/api/web/login").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
var loginReq model.WebAuth
|
var loginReq model.WebAuth
|
||||||
if ReadJson(r, w, &loginReq) {
|
if ReadJson(r, w, &loginReq) {
|
||||||
@@ -172,7 +174,7 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
|||||||
r.PathPrefix("/fs/extensions").Handler(http.FileServer(new(extensionFileSystem)))
|
r.PathPrefix("/fs/extensions").Handler(http.FileServer(new(extensionFileSystem)))
|
||||||
r.PathPrefix("/").Handler(gzipMiddleware(http.FileServer(newEmbedCacheFileSystem(http.FS(startCfg.WebFS)))))
|
r.PathPrefix("/").Handler(gzipMiddleware(http.FileServer(newEmbedCacheFileSystem(http.FS(startCfg.WebFS)))))
|
||||||
}
|
}
|
||||||
if enableApiToken || enableBasicAuth {
|
if enableApiToken || enableWebAuth {
|
||||||
writeUnauthorized := func(w http.ResponseWriter, r *http.Request) {
|
writeUnauthorized := func(w http.ResponseWriter, r *http.Request) {
|
||||||
WriteStatusJson(w, http.StatusUnauthorized, model.NewErrorResult("unauthorized", model.CodeUnauthorized))
|
WriteStatusJson(w, http.StatusUnauthorized, model.NewErrorResult("unauthorized", model.CodeUnauthorized))
|
||||||
}
|
}
|
||||||
@@ -193,7 +195,7 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if enableBasicAuth {
|
if enableWebAuth {
|
||||||
if !strings.HasPrefix(r.URL.Path, "/api/") || r.URL.Path == "/api/web/login" {
|
if !strings.HasPrefix(r.URL.Path, "/api/") || r.URL.Path == "/api/web/login" {
|
||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
|
|||||||
Loaded 50 of 95 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user