mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-10 10:30:28 +00:00
Compare commits
55
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e5ac13cfdc | ||
|
|
073e1413ab | ||
|
|
04b9aa18ec | ||
|
|
08fe4eef4e | ||
|
|
8f9bac9e6b | ||
|
|
bb17c7d48a | ||
|
|
75ffe70a4d | ||
|
|
36f9f65384 | ||
|
|
5f89ae3516 | ||
|
|
0cc2b690f8 | ||
|
|
b991482617 | ||
|
|
1f8906f952 | ||
|
|
0efc85d3c0 | ||
|
|
481675107f | ||
|
|
6568498850 | ||
|
|
67814348dc | ||
|
|
53d13dfe28 | ||
|
|
d511e66d56 | ||
|
|
16f2d30476 | ||
|
|
aafd039205 | ||
|
|
647e161431 | ||
|
|
8f3cf35162 | ||
|
|
f2145de571 | ||
|
|
39cd9034cd | ||
|
|
177c225f16 | ||
|
|
3be8db664c | ||
|
|
9b704f6f14 | ||
|
|
f49e2510f5 | ||
|
|
c5c8bb2bf5 | ||
|
|
962ff4dc98 | ||
|
|
59f3aa7028 | ||
|
|
cfdf87c3b8 | ||
|
|
99924296ef | ||
|
|
9d3221ebbb | ||
|
|
7b7327ffb3 | ||
|
|
fdfc6902a7 | ||
|
|
9fb3758203 | ||
|
|
e2ac8ce64b | ||
|
|
c01d11305c | ||
|
|
def54f8998 | ||
|
|
a205c0b8d7 | ||
|
|
ae70dc240c | ||
|
|
d7f6c3b206 | ||
|
|
a83e38dac4 | ||
|
|
d46c06f08d | ||
|
|
a61cdbed67 | ||
|
|
daa8c39dad | ||
|
|
a5cd53f94c | ||
|
|
847e88881d | ||
|
|
3d20e818e5 | ||
|
|
09f7c88ed3 | ||
|
|
f7b26753c1 | ||
|
|
9704d4af27 | ||
|
|
1907f404ac | ||
|
|
72c5ba7d20 |
No files matched your search
+1
-1
@@ -12,4 +12,4 @@
|
||||
#custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
|
||||
# github: monkeyWie
|
||||
ko_fi: gopeed
|
||||
custom: https://docs.gopeed.com/donate.html
|
||||
custom: https://gopeed.com/docs/donate
|
||||
+35
-28
@@ -13,8 +13,8 @@ on:
|
||||
default: "false"
|
||||
|
||||
env:
|
||||
GO_VERSION: "1.24"
|
||||
FLUTTER_VERSION: "3.41.2"
|
||||
GO_VERSION: "1.25.4"
|
||||
FLUTTER_VERSION: "3.41.6"
|
||||
GA4_MEASUREMENT_ID: ${{ secrets.GA4_MEASUREMENT_ID }}
|
||||
GA4_API_SECRET: ${{ secrets.GA4_API_SECRET }}
|
||||
|
||||
@@ -81,7 +81,7 @@ jobs:
|
||||
"CLANGARM64_ROOT=$target" >> $env:GITHUB_ENV
|
||||
$b >> $env:GITHUB_PATH
|
||||
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: subosito/flutter-action@v2
|
||||
@@ -93,7 +93,7 @@ jobs:
|
||||
VERSION: ${{ needs.get-release.outputs.version }}
|
||||
GOARCH: ${{ matrix.arch }}
|
||||
run: |
|
||||
Invoke-WebRequest -Uri "https://github.com/jrsoftware/issrc/raw/main/Files/Languages/Unofficial/ChineseSimplified.isl" -OutFile "ChineseSimplified.isl"
|
||||
Invoke-WebRequest -Uri "https://github.com/jrsoftware/issrc/raw/main/Files/Languages/ChineseSimplified.isl" -OutFile "ChineseSimplified.isl"
|
||||
mv ChineseSimplified.isl "C:\Program Files (x86)\Inno Setup 6\Languages\"
|
||||
|
||||
go build -tags nosqlite -ldflags="-w -s -X github.com/GopeedLab/gopeed/pkg/base.Version=$env:VERSION" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/GopeedLab/gopeed/bind/desktop
|
||||
@@ -187,7 +187,7 @@ jobs:
|
||||
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"
|
||||
- name: Upload
|
||||
uses: shogo82148/actions-upload-release-asset@v1
|
||||
uses: shogo82148/actions-upload-release-asset@v1.9.2
|
||||
with:
|
||||
upload_url: ${{ needs.get-release.outputs.upload_url }}
|
||||
asset_path: ui/flutter/build/windows/Output/*
|
||||
@@ -198,7 +198,7 @@ jobs:
|
||||
needs: [get-release]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- name: Build
|
||||
@@ -224,7 +224,7 @@ jobs:
|
||||
needs: [get-release, build-macos-amd64-lib]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: actions/setup-node@v3
|
||||
@@ -315,7 +315,7 @@ jobs:
|
||||
needs: [get-release]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: subosito/flutter-action@v2
|
||||
@@ -324,7 +324,7 @@ jobs:
|
||||
channel: master
|
||||
- run: |
|
||||
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 libwebkit2gtk-4.1-dev libayatana-appindicator3-1 libayatana-appindicator3-dev rpm patchelf libfuse2 locate libkeybinder-3.0-dev
|
||||
arch=$(uname -m)
|
||||
wget -O appimagetool "https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-${arch}.AppImage"
|
||||
chmod +x appimagetool
|
||||
@@ -339,18 +339,11 @@ jobs:
|
||||
|
||||
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)
|
||||
|
||||
mkdir -p linux/packaging/rpm
|
||||
cat << EOF > linux/packaging/rpm/make_config.yaml
|
||||
display_name: Gopeed
|
||||
icon: $ICON_PATH
|
||||
icon: assets/icon/icon.svg
|
||||
summary: High speed downloader.
|
||||
description: A high speed downloader that supports all platforms.
|
||||
group: Applications/Internet
|
||||
@@ -414,13 +407,17 @@ jobs:
|
||||
overwrite: true
|
||||
build-snap:
|
||||
if: ${{ github.event.inputs.platform == 'all' || github.event.inputs.platform == 'snap' }}
|
||||
runs-on: ubuntu-latest
|
||||
runs-on: ubuntu-24.04
|
||||
needs: [get-release]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- name: Install Snap host build dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y --no-install-recommends pkg-config libgtk-3-dev libwebkit2gtk-4.1-dev
|
||||
- name: Setup LXD
|
||||
uses: canonical/[email protected]
|
||||
with:
|
||||
@@ -484,9 +481,12 @@ jobs:
|
||||
\$CRAFT_PART_INSTALL/usr/bin/flutter doctor
|
||||
build-packages:
|
||||
- clang
|
||||
- llvm-18
|
||||
- lld
|
||||
- cmake
|
||||
- curl
|
||||
- libgtk-3-dev
|
||||
- libwebkit2gtk-4.1-dev
|
||||
- libkeybinder-3.0-dev
|
||||
- ninja-build
|
||||
- unzip
|
||||
@@ -502,6 +502,7 @@ jobs:
|
||||
source: .
|
||||
plugin: nil
|
||||
stage-packages:
|
||||
- libwebkit2gtk-4.1-0
|
||||
- libgtk-3-dev
|
||||
- libkeybinder-3.0-dev
|
||||
- libappindicator3-dev
|
||||
@@ -510,6 +511,9 @@ jobs:
|
||||
set +e
|
||||
dart pub get
|
||||
set -eux
|
||||
llvm_bin_dir="\$(dirname "\$(readlink -f "\$(command -v clang)")")"
|
||||
test -x "\$llvm_bin_dir/llvm-ar"
|
||||
test -x "\$llvm_bin_dir/ld.lld"
|
||||
flutter build linux --dart-define="UPDATE_CHANNEL=linuxSnap" --dart-define="GA4_MEASUREMENT_ID=$GA4_MEASUREMENT_ID" --dart-define="GA4_API_SECRET=$GA4_API_SECRET"
|
||||
mkdir -p \$CRAFT_PART_INSTALL/bin/
|
||||
cp -r build/linux/*/release/bundle/* \$CRAFT_PART_INSTALL/bin/
|
||||
@@ -552,7 +556,7 @@ jobs:
|
||||
needs: [get-release]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: actions/setup-java@v3
|
||||
@@ -590,11 +594,11 @@ jobs:
|
||||
overwrite: true
|
||||
build-ios:
|
||||
if: ${{ github.event.inputs.platform == 'all' || github.event.inputs.platform == 'ios' }}
|
||||
runs-on: macos-14
|
||||
runs-on: macos-26
|
||||
needs: [get-release]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: subosito/flutter-action@v2
|
||||
@@ -627,7 +631,7 @@ jobs:
|
||||
needs: [get-release]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: subosito/flutter-action@v2
|
||||
@@ -692,12 +696,15 @@ jobs:
|
||||
VERSION: ${{ needs.get-release.outputs.version }}
|
||||
run: |
|
||||
sudo apt update -y
|
||||
sudo apt install -y pv bsdmainutils
|
||||
wget -O qdk2_0.32.bionic_amd64.deb "https://github.com/qnap-dev/qdk2/releases/download/v0.32/qdk2_0.32.bionic_amd64.deb"
|
||||
dpkg -X qdk2_0.32.bionic_amd64.deb qdk2 # Direct installs will fail due to missing dependencies!
|
||||
[[ -d qdk2 ]] || exit 1
|
||||
qdk_version=2.5.3
|
||||
qdk_package="qdk_${qdk_version}_amd64.deb"
|
||||
curl --fail --location --retry 3 --retry-all-errors \
|
||||
--output "${qdk_package}" \
|
||||
"https://github.com/qnap-dev/QDK/releases/download/v${qdk_version}/${qdk_package}"
|
||||
echo "17b3841b7d4590a4ee025844ba583304b5e3c497d9fa8934d5175131d3908022 ${qdk_package}" | sha256sum --check --strict
|
||||
sudo apt-get install -y "./${qdk_package}"
|
||||
command -v qbuild
|
||||
|
||||
export PATH=$(pwd)/qdk2/usr/bin:$(pwd)/qdk2/usr/share/qdk2/QDK/bin:${PATH}
|
||||
wget -O Gopeed.template.tar.gz "https://github.com/GopeedLab/QpkgBuild/raw/refs/heads/master/template/Gopeed.template.tar.gz"
|
||||
tar -zxf Gopeed.template.tar.gz
|
||||
[[ -d Gopeed ]] || exit 1
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
name: l10n
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
paths:
|
||||
- "ui/flutter/lib/l10n/**"
|
||||
- "ui/flutter/l10n.yaml"
|
||||
- ".github/workflows/scripts/check_l10n.dart"
|
||||
- ".github/workflows/l10n.yml"
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
paths:
|
||||
- "ui/flutter/lib/l10n/**"
|
||||
- "ui/flutter/l10n.yaml"
|
||||
- ".github/workflows/scripts/check_l10n.dart"
|
||||
- ".github/workflows/l10n.yml"
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
concurrency:
|
||||
group: l10n-${{ github.event.pull_request.number || github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
env:
|
||||
FLUTTER_VERSION: "3.41.6"
|
||||
|
||||
jobs:
|
||||
validate:
|
||||
name: Validate Flutter translations
|
||||
runs-on: ubuntu-latest
|
||||
defaults:
|
||||
run:
|
||||
working-directory: ui/flutter
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: subosito/flutter-action@v2
|
||||
with:
|
||||
flutter-version: ${{ env.FLUTTER_VERSION }}
|
||||
- run: flutter pub get
|
||||
- name: Validate ARB catalogs
|
||||
run: dart run ../../.github/workflows/scripts/check_l10n.dart
|
||||
- name: Generate localizations
|
||||
run: flutter gen-l10n
|
||||
- name: Analyze generated localization integration
|
||||
run: flutter analyze
|
||||
@@ -17,7 +17,7 @@ on:
|
||||
- "Dockerfile"
|
||||
|
||||
env:
|
||||
GO_VERSION: "1.24"
|
||||
GO_VERSION: "1.25.4"
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
@@ -31,7 +31,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: release-drafter/release-drafter@v5
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
// Validate Flutter ARB translation catalogs before localization code generation.
|
||||
//
|
||||
// Usage (from ui/flutter):
|
||||
// dart run ../../.github/workflows/scripts/check_l10n.dart
|
||||
//
|
||||
// What it checks:
|
||||
// - Every ARB file uses the repository's canonical two-space JSON formatting.
|
||||
// - Every app_<locale>.arb file declares the matching @@locale value.
|
||||
// - Message values are not empty (warning only).
|
||||
// - Every locale has the same message keys as app_en.arb (warning only).
|
||||
// - Every translation preserves the placeholder variables used by app_en.arb
|
||||
// (warning only).
|
||||
//
|
||||
// Notes:
|
||||
// - This script does not judge translation quality.
|
||||
// - It does not generate localization code or fully parse ICU MessageFormat.
|
||||
// `flutter gen-l10n` runs immediately afterwards in CI to validate ICU syntax
|
||||
// and generate app_localizations*.dart.
|
||||
// - The script exits with a failure only for malformed JSON, invalid ARB
|
||||
// formatting, or a mismatched @@locale value. Translation completeness is
|
||||
// reported as warnings; generation and analysis remain the compile-time gate.
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
/// Runs repository-specific checks that `flutter gen-l10n` does not enforce.
|
||||
void main() {
|
||||
final directory = Directory('lib/l10n');
|
||||
final templateFile = File('${directory.path}/app_en.arb');
|
||||
final template = _readArb(templateFile);
|
||||
final templateKeys = _messageKeys(template);
|
||||
var failed = false;
|
||||
|
||||
for (final file in directory.listSync().whereType<File>().where(
|
||||
(file) => file.path.endsWith('.arb'),
|
||||
)) {
|
||||
final messages = _readArb(file);
|
||||
if (!_hasCanonicalFormat(file, messages)) {
|
||||
stderr.writeln(
|
||||
'${file.path}: ARB formatting is not canonical (use two-space JSON indentation and a trailing newline)',
|
||||
);
|
||||
failed = true;
|
||||
}
|
||||
final expectedLocale = file.uri.pathSegments.last
|
||||
.replaceFirst('app_', '')
|
||||
.replaceFirst('.arb', '');
|
||||
if (messages['@@locale'] != expectedLocale) {
|
||||
stderr.writeln('${file.path}: @@locale must be $expectedLocale');
|
||||
failed = true;
|
||||
}
|
||||
final keys = _messageKeys(messages);
|
||||
final empty =
|
||||
keys.where((key) => (messages[key] as String).trim().isEmpty).toList()
|
||||
..sort();
|
||||
if (empty.isNotEmpty) {
|
||||
_warning('${file.path}: empty messages $empty');
|
||||
}
|
||||
if (file.path == templateFile.path) continue;
|
||||
final missing = templateKeys.difference(keys);
|
||||
final extra = keys.difference(templateKeys);
|
||||
if (missing.isNotEmpty || extra.isNotEmpty) {
|
||||
_warning(
|
||||
'${file.path}: missing ${missing.toList()..sort()}, extra ${extra.toList()..sort()}',
|
||||
);
|
||||
}
|
||||
|
||||
for (final key in templateKeys.intersection(keys)) {
|
||||
final expected = _placeholders(template[key] as String);
|
||||
final actual = _placeholders(messages[key] as String);
|
||||
if (!_sameSet(expected, actual)) {
|
||||
_warning(
|
||||
'${file.path}: $key placeholders $actual do not match $expected',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (failed) exitCode = 1;
|
||||
}
|
||||
|
||||
/// Reads one ARB file as its top-level JSON object.
|
||||
Map<String, Object?> _readArb(File file) =>
|
||||
jsonDecode(file.readAsStringSync()) as Map<String, Object?>;
|
||||
|
||||
/// Checks the exact JSON layout so whitespace-only formatting drift fails CI.
|
||||
bool _hasCanonicalFormat(File file, Map<String, Object?> arb) {
|
||||
const encoder = JsonEncoder.withIndent(' ');
|
||||
return file.readAsStringSync() == '${encoder.convert(arb)}\n';
|
||||
}
|
||||
|
||||
/// Returns user-facing message keys and excludes ARB metadata keys (`@`/`@@`).
|
||||
Set<String> _messageKeys(Map<String, Object?> arb) =>
|
||||
arb.keys.where((key) => !key.startsWith('@')).toSet();
|
||||
|
||||
/// Extracts both simple placeholders (`{name}`) and ICU selector variables.
|
||||
///
|
||||
/// For example, this returns `count` from
|
||||
/// `{count, plural, =1{1 file} other{{count} files}}`.
|
||||
Set<String> _placeholders(String message) => RegExp(
|
||||
r'\{([A-Za-z_][A-Za-z0-9_]*)(?=\s*(?:\}|,\s*(?:plural|select|selectordinal)\s*,))',
|
||||
).allMatches(message).map((match) => match.group(1)!).toSet();
|
||||
|
||||
/// Compares sets without depending on iteration order.
|
||||
bool _sameSet(Set<String> left, Set<String> right) =>
|
||||
left.length == right.length && left.containsAll(right);
|
||||
|
||||
void _warning(String message) => stderr.writeln('warning: $message');
|
||||
@@ -10,7 +10,7 @@
|
||||
// (including the common pattern where Flutter concatenates the base URL with a relative path)
|
||||
// - Download them into: build/web/assets/gstatic/<path>
|
||||
// - Rewrite "https://fonts.gstatic.com/s/" -> "assets/gstatic/" so runtime loads locally
|
||||
// - Only download font subsets needed by supported locales (parsed from message.dart)
|
||||
// - Only download font subsets needed by supported locales (parsed from lib/l10n/*.arb)
|
||||
//
|
||||
// Notes:
|
||||
// - This script DOES NOT call `flutter build web`. It only patches the output.
|
||||
@@ -23,15 +23,18 @@ import 'dart:io';
|
||||
/// Uses language code prefix to automatically detect the script system.
|
||||
/// Returns empty set if base fonts (Latin/Cyrillic/Greek) are sufficient.
|
||||
Set<String> _getFontFamiliesForLocale(String locale) {
|
||||
final lang = locale.toLowerCase().split('_').first;
|
||||
final normalizedLocale = locale.toLowerCase().replaceAll('-', '_');
|
||||
final lang = normalizedLocale.split('_').first;
|
||||
|
||||
// CJK languages - need specific large font files
|
||||
if (lang == 'zh') {
|
||||
// Simplified vs Traditional Chinese
|
||||
if (locale.contains('cn') || locale.contains('sg')) {
|
||||
return {'notosanssc'}; // Simplified Chinese
|
||||
if (normalizedLocale.contains('_tw') ||
|
||||
normalizedLocale.contains('_hk') ||
|
||||
normalizedLocale.contains('_mo')) {
|
||||
return {'notosanstc', 'notosanshk'}; // Traditional Chinese (TW, HK, MO)
|
||||
}
|
||||
return {'notosanstc', 'notosanshk'}; // Traditional Chinese (TW, HK, MO)
|
||||
return {'notosanssc'}; // Simplified Chinese, including generic zh
|
||||
}
|
||||
if (lang == 'ja') return {'notosansjp'}; // Japanese
|
||||
if (lang == 'ko') return {'notosanskr'}; // Korean
|
||||
@@ -95,29 +98,25 @@ const _baseFontFamilies = <String>{
|
||||
// to reduce bundle size. Add them back if emoji support is needed.
|
||||
};
|
||||
|
||||
/// Parse supported locales from message.dart file.
|
||||
/// Reads the import statements and extracts locale codes like 'zh_cn', 'en_us', etc.
|
||||
Set<String> _parseSupportedLocales(File messageFile) {
|
||||
/// Parse supported locales from Flutter gen-l10n ARB filenames.
|
||||
/// Filenames follow app_<locale>.arb, for example app_zh_CN.arb or app_en.arb.
|
||||
Set<String> _parseSupportedLocales(Directory l10nDirectory) {
|
||||
final locales = <String>{};
|
||||
|
||||
if (!messageFile.existsSync()) {
|
||||
_fail('Warning: message.dart not found, only base fonts will be used');
|
||||
if (!l10nDirectory.existsSync()) {
|
||||
_fail('Localization directory not found: ${l10nDirectory.path}');
|
||||
}
|
||||
|
||||
final content = messageFile.readAsStringSync();
|
||||
|
||||
// Match import statements like: import 'langs/zh_cn.dart';
|
||||
final importRegex = RegExp(r"import\s+'langs/(\w+)\.dart'");
|
||||
for (final match in importRegex.allMatches(content)) {
|
||||
final locale = match.group(1);
|
||||
if (locale != null) {
|
||||
locales.add(locale);
|
||||
}
|
||||
final arbName = RegExp(r'^app_(.+)\.arb$');
|
||||
for (final entity in l10nDirectory.listSync()) {
|
||||
if (entity is! File) continue;
|
||||
final filename = entity.uri.pathSegments.last;
|
||||
final locale = arbName.firstMatch(filename)?.group(1);
|
||||
if (locale != null && locale.isNotEmpty) locales.add(locale);
|
||||
}
|
||||
|
||||
if (locales.isEmpty) {
|
||||
_fail(
|
||||
'Warning: No locales found in message.dart, only base fonts will be used');
|
||||
_fail('No app_<locale>.arb files found in ${l10nDirectory.path}');
|
||||
}
|
||||
|
||||
return locales;
|
||||
@@ -139,7 +138,9 @@ Set<String> _getRequiredFontFamilies(Set<String> locales) {
|
||||
/// Font paths have the format: "fontfamily/version/filename.ext"
|
||||
/// Example: "notosanssc/v36/xxx.ttf" -> font family is "notosanssc"
|
||||
bool _fontMatchesRequiredFamilies(
|
||||
String fontPath, Set<String> requiredFamilies) {
|
||||
String fontPath,
|
||||
Set<String> requiredFamilies,
|
||||
) {
|
||||
// Extract font family from path (first segment before '/')
|
||||
final slashIndex = fontPath.indexOf('/');
|
||||
if (slashIndex == -1) {
|
||||
@@ -158,10 +159,9 @@ Future<void> main(List<String> args) async {
|
||||
// but will also try to locate pubspec.yaml by walking up.
|
||||
final flutterDir = _findFlutterProjectRoot(Directory.current);
|
||||
|
||||
// Parse supported locales from message.dart
|
||||
final messageFile =
|
||||
File(_join(flutterDir.path, 'lib', 'i18n', 'message.dart'));
|
||||
final supportedLocales = _parseSupportedLocales(messageFile);
|
||||
// Parse supported locales from Flutter gen-l10n ARB files.
|
||||
final l10nDirectory = Directory(_join(flutterDir.path, 'lib', 'l10n'));
|
||||
final supportedLocales = _parseSupportedLocales(l10nDirectory);
|
||||
stdout.writeln('Supported locales: ${supportedLocales.join(', ')}');
|
||||
|
||||
// Get required font families based on supported locales
|
||||
@@ -203,7 +203,8 @@ Future<void> main(List<String> args) async {
|
||||
if (p.contains('://') || p.startsWith('data:')) continue;
|
||||
if (p.startsWith('/') ||
|
||||
p.startsWith('assets/') ||
|
||||
p.startsWith('packages/')) continue;
|
||||
p.startsWith('packages/'))
|
||||
continue;
|
||||
if (p.contains('..')) continue;
|
||||
relAssetsUnderS.add(p);
|
||||
}
|
||||
|
||||
+90
-15
@@ -28,8 +28,8 @@ on:
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
GO_VERSION: "1.24"
|
||||
FLUTTER_VERSION: "3.41.2"
|
||||
GO_VERSION: "1.25.4"
|
||||
FLUTTER_VERSION: "3.41.6"
|
||||
|
||||
jobs:
|
||||
# lint:
|
||||
@@ -50,14 +50,18 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- name: Install webview dependencies
|
||||
run: |
|
||||
sudo apt-get update -y
|
||||
sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.1-dev xvfb
|
||||
- name: Run Unit tests.
|
||||
run: |
|
||||
# Get packages for testing and coverage (including cmd/web for flags_test.go)
|
||||
PACKAGES=$(go list ./... | grep -v /bind/ | grep -v -E '/cmd/(?!web)')
|
||||
go test -v -coverpkg=$(echo "$PACKAGES" | paste -sd "," -) -covermode=count -coverprofile=coverage.txt $PACKAGES
|
||||
PACKAGES=$(go list ./... | awk '!/\/bind\// && (!/\/cmd\// || /\/cmd\/web$/)')
|
||||
env CGO_ENABLED=1 xvfb-run -a go test -tags webview -v -coverpkg=$(echo "$PACKAGES" | paste -sd "," -) -covermode=count -coverprofile=coverage.txt $PACKAGES
|
||||
# Filter out main.go from coverage report to avoid lowering coverage percentage
|
||||
grep -v "main.go" coverage.txt > coverage_filtered.txt || true
|
||||
mv coverage_filtered.txt coverage.txt
|
||||
@@ -65,6 +69,77 @@ jobs:
|
||||
with:
|
||||
files: ./coverage.txt
|
||||
token: ${{ secrets.CODECOV_UPLOAD_TOKEN }}
|
||||
test-webview-integration:
|
||||
name: Test webview integration (${{ matrix.name }})
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
- name: Windows
|
||||
os: windows-latest
|
||||
- name: macOS
|
||||
os: macos-latest
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: subosito/flutter-action@v2
|
||||
if: runner.os == 'macOS'
|
||||
with:
|
||||
flutter-version: ${{ env.FLUTTER_VERSION }}
|
||||
- name: Test webview
|
||||
if: runner.os == 'Windows'
|
||||
run: go test -tags webview -v -count=1 -timeout 300s ./internal/webview/goprovider/
|
||||
env:
|
||||
CGO_ENABLED: 1
|
||||
- name: Test webview
|
||||
if: runner.os == 'macOS'
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
SOCKET_PATH="$HOME/Library/Application Support/com.gopeed.gopeed/gopeed_webview.sock"
|
||||
LOG_PATH="$RUNNER_TEMP/gopeed-webview-rpc.log"
|
||||
rm -f "$SOCKET_PATH"
|
||||
|
||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/macos/Frameworks/libgopeed.dylib github.com/GopeedLab/gopeed/bind/desktop
|
||||
|
||||
cd ui/flutter
|
||||
flutter pub get
|
||||
flutter run --machine -d macos >"$LOG_PATH" 2>&1 &
|
||||
FLUTTER_PID=$!
|
||||
cd ../..
|
||||
|
||||
cleanup() {
|
||||
kill "$FLUTTER_PID" >/dev/null 2>&1 || true
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
FLUTTER_STARTED=false
|
||||
for _ in $(seq 1 600); do
|
||||
if grep -Eq '"event"[[:space:]]*:[[:space:]]*"app\.started"' "$LOG_PATH"; then
|
||||
FLUTTER_STARTED=true
|
||||
break
|
||||
fi
|
||||
if ! kill -0 "$FLUTTER_PID" >/dev/null 2>&1; then
|
||||
echo "Flutter exited before reporting app.started"
|
||||
cat "$LOG_PATH"
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
if [[ "$FLUTTER_STARTED" != true ]]; then
|
||||
echo "Timed out waiting for Flutter to report app.started"
|
||||
cat "$LOG_PATH"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! go test -tags webview -v -count=1 -timeout 600s ./internal/webview/rpcprovider/ -args -webview-rpc-network unix -webview-rpc-address "$SOCKET_PATH"; then
|
||||
cat "$LOG_PATH"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
build-desktop:
|
||||
strategy:
|
||||
matrix:
|
||||
@@ -80,7 +155,7 @@ jobs:
|
||||
flutter_channel: "main"
|
||||
name: Build desktop check (${{ matrix.os }})
|
||||
runs-on: ${{ matrix.os }}
|
||||
needs: [test]
|
||||
needs: [ test ]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Enable long paths for flutter main branch checks
|
||||
@@ -109,7 +184,7 @@ jobs:
|
||||
"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@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: subosito/flutter-action@v2
|
||||
@@ -129,7 +204,7 @@ jobs:
|
||||
- if: runner.os == 'Linux'
|
||||
run: |
|
||||
sudo apt-get update -y
|
||||
sudo apt-get install -y ninja-build libgtk-3-dev libayatana-appindicator3-dev libkeybinder-3.0-dev
|
||||
sudo apt-get install -y ninja-build libgtk-3-dev libwebkit2gtk-4.1-dev libayatana-appindicator3-dev libkeybinder-3.0-dev
|
||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/GopeedLab/gopeed/bind/desktop
|
||||
cd ui/flutter
|
||||
flutter build linux
|
||||
@@ -138,11 +213,11 @@ jobs:
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [macos-14, ubuntu-latest]
|
||||
needs: [test]
|
||||
os: [ macos-26, ubuntu-latest ]
|
||||
needs: [ test ]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: subosito/flutter-action@v2
|
||||
@@ -171,10 +246,10 @@ jobs:
|
||||
build-web:
|
||||
name: Build web check
|
||||
runs-on: ubuntu-latest
|
||||
needs: [test]
|
||||
needs: [ test ]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: subosito/flutter-action@v2
|
||||
@@ -192,10 +267,10 @@ jobs:
|
||||
build-docker:
|
||||
name: Build docker check
|
||||
runs-on: ubuntu-latest
|
||||
needs: [test]
|
||||
needs: [ test ]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-go@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ env.GO_VERSION }}
|
||||
- uses: subosito/flutter-action@v2
|
||||
|
||||
+54
-5
@@ -17,11 +17,60 @@ start the front-end flutter project in `debug` mode to run.
|
||||
|
||||
## Translation
|
||||
|
||||
The internationalization files of Gopeed are located in the `ui/flutter/lib/i18n/langs` directory.
|
||||
You only need to add the corresponding language file in this directory.
|
||||
Flutter translations are ARB files in `ui/flutter/lib/l10n`. Use `app_en.arb` as the source template,
|
||||
then edit or add `app_<locale>.arb` (for example, `app_de.arb` or `app_zh_TW.arb`). Every locale must
|
||||
contain the same message keys as the English template.
|
||||
|
||||
Please refer to `en_us.dart` for translation.
|
||||
words prefixed with `@` are not meant to be translated.
|
||||
Translate message values only, and do not rename variables inside braces such as `{count}` and `{name}`.
|
||||
Do not translate metadata keys prefixed with `@`. Submit only the ARB source files you changed; do not generate or commit
|
||||
`app_localizations*.dart`. The pull-request CI automatically validates the catalogs, generates the
|
||||
localization code, and checks its integration.
|
||||
|
||||
The following examples show both the ARB definition and the text users see after the app supplies values.
|
||||
|
||||
1. Insert a variable into a message
|
||||
|
||||
ARB definition:
|
||||
|
||||
```json
|
||||
"welcomeUser": "Hola, {name}"
|
||||
```
|
||||
|
||||
Rendered result: when `name = Ana`, users see “Hola, Ana”. You may reorder the sentence, but keep `{name}`.
|
||||
|
||||
2. Display different text for different quantities
|
||||
|
||||
ARB definition:
|
||||
|
||||
```json
|
||||
"fileCount": "{count, plural, =0{No hay archivos} =1{1 archivo} other{{count} archivos}}"
|
||||
```
|
||||
|
||||
Rendered results:
|
||||
|
||||
- `count = 0` → “No hay archivos”
|
||||
- `count = 1` → “1 archivo”
|
||||
- `count = 3` → “3 archivos”
|
||||
|
||||
`=0`, `=1`, and `other` mean zero, one, and every other quantity. Translate only the user-visible text
|
||||
inside each branch.
|
||||
|
||||
3. Display different text for different values
|
||||
|
||||
ARB definition:
|
||||
|
||||
```json
|
||||
"taskState": "{state, select, running{En curso} paused{En pausa} other{Desconocido}}"
|
||||
```
|
||||
|
||||
Rendered results:
|
||||
|
||||
- `state = running` → “En curso”
|
||||
- `state = paused` → “En pausa”
|
||||
- Any other value → “Desconocido”
|
||||
|
||||
`running`, `paused`, and `other` are values supplied by the app. Do not translate them; translate only
|
||||
the display text inside the following braces.
|
||||
|
||||
## flutter development
|
||||
|
||||
@@ -31,4 +80,4 @@ Turn on build_runner watcher if you want to edit api/models:
|
||||
|
||||
```
|
||||
flutter pub run build_runner watch
|
||||
```
|
||||
```
|
||||
+51
-3
@@ -17,10 +17,58 @@
|
||||
|
||||
## 翻訳
|
||||
|
||||
Gopeed の国際化ファイルは `ui/flutter/lib/i18n/langs` ディレクトリにあります。
|
||||
このディレクトリに対応する言語ファイルを追加するだけでよいです。
|
||||
Flutter の翻訳ファイルは `ui/flutter/lib/l10n` にあります。`app_en.arb` を原文テンプレートとして、
|
||||
`app_<locale>.arb`(例: `app_de.arb`、`app_zh_TW.arb`)を編集または追加してください。すべての言語で
|
||||
英語テンプレートと同じメッセージ key が必要です。
|
||||
|
||||
翻訳については `en_us.dart` を参照してください。
|
||||
翻訳するのはメッセージ値だけです。`{count}` や `{name}` など、中括弧内の変数名は変更しないでください。
|
||||
`@` で始まるメタデータ key は翻訳しません。変更した ARB ソースファイルだけをコミットし、
|
||||
`app_localizations*.dart` は生成もコミットも不要です。PR の CI が全言語を検証し、国際化コードを生成して
|
||||
統合結果を確認します。
|
||||
|
||||
以下の例では、ARB に記述する定義と、アプリが値を渡した後にユーザーが実際に見る表示を示します。
|
||||
|
||||
1. メッセージに変数を挿入する
|
||||
|
||||
ARB 定義:
|
||||
|
||||
```json
|
||||
"welcomeUser": "こんにちは、{name}"
|
||||
```
|
||||
|
||||
最終表示:`name = 太郎` の場合は「こんにちは、太郎」と表示されます。語順は変更できますが、`{name}` は残します。
|
||||
|
||||
2. 数量に応じて表示を切り替える
|
||||
|
||||
ARB 定義:
|
||||
|
||||
```json
|
||||
"fileCount": "{count, plural, =0{ファイルなし} =1{1 個のファイル} other{{count} 個のファイル}}"
|
||||
```
|
||||
|
||||
最終表示:
|
||||
|
||||
- `count = 0` →「ファイルなし」
|
||||
- `count = 1` →「1 個のファイル」
|
||||
- `count = 3` →「3 個のファイル」
|
||||
|
||||
`=0`、`=1`、`other` はそれぞれ 0、1、その他の数量を表します。各分岐の中で画面に表示される文字だけを翻訳します。
|
||||
|
||||
3. 変数の値に応じて表示を切り替える
|
||||
|
||||
ARB 定義:
|
||||
|
||||
```json
|
||||
"taskState": "{state, select, running{実行中} paused{一時停止} other{不明}}"
|
||||
```
|
||||
|
||||
最終表示:
|
||||
|
||||
- `state = running` →「実行中」
|
||||
- `state = paused` →「一時停止」
|
||||
- その他の値 →「不明」
|
||||
|
||||
`running`、`paused`、`other` はアプリが渡す分岐値なので翻訳せず、その後の中括弧内にある表示文字だけを翻訳します。
|
||||
|
||||
## flutter での開発
|
||||
|
||||
|
||||
+53
-3
@@ -17,10 +17,60 @@ khởi động dự án flutter frontend trong chế độ `debug` để chạy.
|
||||
|
||||
## Dịch thuật
|
||||
|
||||
Các tệp quốc tế hóa của Gopeed được đặt trong thư mục `ui/flutter/lib/i18n/langs`.
|
||||
Bạn chỉ cần thêm tệp ngôn ngữ tương ứng trong thư mục này.
|
||||
Các bản dịch Flutter là tệp ARB trong `ui/flutter/lib/l10n`. Hãy dùng `app_en.arb` làm mẫu nguồn,
|
||||
sau đó sửa hoặc thêm `app_<locale>.arb` (ví dụ `app_de.arb` hoặc `app_zh_TW.arb`). Mỗi ngôn ngữ phải
|
||||
có cùng các khóa thông điệp như mẫu tiếng Anh.
|
||||
|
||||
Vui lòng tham khảo `en_us.dart` để biết cách dịch thuật.
|
||||
Chỉ dịch giá trị thông điệp; không đổi tên biến trong dấu ngoặc nhọn như `{count}` và `{name}`. Không dịch khóa
|
||||
siêu dữ liệu bắt đầu bằng `@`. Chỉ commit các tệp nguồn ARB đã thay đổi; không cần tạo hoặc commit
|
||||
`app_localizations*.dart`. CI của pull request sẽ tự động
|
||||
kiểm tra tất cả ngôn ngữ, tạo mã bản địa hóa và xác minh việc tích hợp.
|
||||
|
||||
Các ví dụ sau hiển thị cả định nghĩa trong ARB và nội dung người dùng thực sự thấy sau khi ứng dụng truyền giá trị.
|
||||
|
||||
1. Chèn biến vào thông điệp
|
||||
|
||||
Định nghĩa ARB:
|
||||
|
||||
```json
|
||||
"welcomeUser": "Xin chào, {name}"
|
||||
```
|
||||
|
||||
Kết quả hiển thị: khi `name = An`, người dùng thấy “Xin chào, An”. Có thể đổi trật tự câu nhưng phải giữ `{name}`.
|
||||
|
||||
2. Hiển thị nội dung khác nhau theo số lượng
|
||||
|
||||
Định nghĩa ARB:
|
||||
|
||||
```json
|
||||
"fileCount": "{count, plural, =0{Không có tệp} =1{1 tệp} other{{count} tệp}}"
|
||||
```
|
||||
|
||||
Kết quả hiển thị:
|
||||
|
||||
- `count = 0` → “Không có tệp”
|
||||
- `count = 1` → “1 tệp”
|
||||
- `count = 3` → “3 tệp”
|
||||
|
||||
`=0`, `=1` và `other` lần lượt biểu thị số lượng bằng 0, bằng 1 và các số lượng còn lại. Chỉ dịch phần chữ
|
||||
hiển thị cho người dùng bên trong mỗi nhánh.
|
||||
|
||||
3. Hiển thị nội dung khác nhau theo giá trị biến
|
||||
|
||||
Định nghĩa ARB:
|
||||
|
||||
```json
|
||||
"taskState": "{state, select, running{Đang chạy} paused{Đã tạm dừng} other{Không xác định}}"
|
||||
```
|
||||
|
||||
Kết quả hiển thị:
|
||||
|
||||
- `state = running` → “Đang chạy”
|
||||
- `state = paused` → “Đã tạm dừng”
|
||||
- Giá trị khác → “Không xác định”
|
||||
|
||||
`running`, `paused` và `other` là các giá trị do ứng dụng truyền vào nên không được dịch; chỉ dịch phần chữ
|
||||
hiển thị trong dấu ngoặc theo sau chúng.
|
||||
|
||||
## Phát triển flutter
|
||||
|
||||
|
||||
+50
-4
@@ -13,10 +13,57 @@ PR,合并到 `main` 分支。
|
||||
flutter 项目即可运行。
|
||||
|
||||
## 翻译
|
||||
|
||||
Gopeed 的国际化文件位于 `ui/flutter/lib/i18n/langs` 目录下,只需要在该目录下添加对应的语言文件即可。
|
||||
|
||||
请注意以 `en_us.dart` 为参照进行翻译。
|
||||
Flutter 国际化文件位于 `ui/flutter/lib/l10n`,请以 `app_en.arb` 为源模板,修改或新增
|
||||
`app_<locale>.arb`(例如 `app_de.arb` 或 `app_zh_TW.arb`)。每个语种必须与英文模板包含相同的文案 key。
|
||||
|
||||
只翻译文案值,不要修改 `{count}`、`{name}` 等花括号中的变量名。以 `@` 开头的元数据 key 不需要翻译。
|
||||
提交时只包含修改过的 ARB 源文件,不需要生成或提交 `app_localizations*.dart`。PR 的 CI 会自动校验所有语种、
|
||||
生成国际化代码并检查集成结果。
|
||||
|
||||
下面的例子同时给出 ARB 中填写的文案定义,以及程序传值后用户最终看到的内容。
|
||||
|
||||
1. 在文案中插入变量
|
||||
|
||||
ARB 定义:
|
||||
|
||||
```json
|
||||
"welcomeUser": "你好,{name}"
|
||||
```
|
||||
|
||||
最终显示:当 `name = 小明` 时,显示“你好,小明”。翻译时可以调整整句话,但必须保留 `{name}`。
|
||||
|
||||
2. 根据数量显示不同文案
|
||||
|
||||
ARB 定义:
|
||||
|
||||
```json
|
||||
"fileCount": "{count, plural, =0{没有文件} =1{1 个文件} other{{count} 个文件}}"
|
||||
```
|
||||
|
||||
最终显示:
|
||||
|
||||
- `count = 0` → “没有文件”
|
||||
- `count = 1` → “1 个文件”
|
||||
- `count = 3` → “3 个文件”
|
||||
|
||||
`=0`、`=1`、`other` 分别表示数量为 0、数量为 1 和其他数量。只翻译每个分支花括号内显示给用户的文字。
|
||||
|
||||
3. 根据变量值显示不同文案
|
||||
|
||||
ARB 定义:
|
||||
|
||||
```json
|
||||
"taskState": "{state, select, running{运行中} paused{已暂停} other{未知状态}}"
|
||||
```
|
||||
|
||||
最终显示:
|
||||
|
||||
- `state = running` → “运行中”
|
||||
- `state = paused` → “已暂停”
|
||||
- 其他值 → “未知状态”
|
||||
|
||||
`running`、`paused`、`other` 是程序传入的分支值,不能翻译;只翻译它们后面花括号内的显示文字。
|
||||
|
||||
## flutter开发
|
||||
|
||||
@@ -27,4 +74,3 @@ Gopeed 的国际化文件位于 `ui/flutter/lib/i18n/langs` 目录下,只需
|
||||
```
|
||||
flutter pub run build_runner watch
|
||||
```
|
||||
|
||||
+49
-3
@@ -11,11 +11,57 @@
|
||||
建議使用 web 端進行開發與調試,首先啟動服務,使用指令 `go run cmd/api/main.go` 啟動 ,該服務默認連接埠為 `9999`,接著以 `debug` 模式啟動前端 flutter 項目即可。
|
||||
|
||||
## 翻譯
|
||||
|
||||
Gopeed 的翻譯文件位於 `ui/flutter/lib/i18n/langs` 目錄中,只需要修改或新建翻譯文件即可。
|
||||
|
||||
Flutter 翻譯檔位於 `ui/flutter/lib/l10n`,請以 `app_en.arb` 為來源範本,修改或新增
|
||||
`app_<locale>.arb`(例如 `app_de.arb` 或 `app_zh_TW.arb`)。每個語言都必須與英文範本包含相同的訊息 key。
|
||||
|
||||
請以 `en_us.dart` 作為參照。
|
||||
只翻譯訊息值,不要修改 `{count}`、`{name}` 等大括號中的變數名稱。以 `@` 開頭的中繼資料 key 不需要翻譯。
|
||||
提交時只需包含修改過的 ARB 來源檔,不需要產生或提交 `app_localizations*.dart`。PR 的 CI
|
||||
會自動驗證所有語言、產生國際化程式碼並檢查整合結果。
|
||||
|
||||
以下範例同時列出 ARB 中填寫的訊息定義,以及程式傳值後使用者最終看到的內容。
|
||||
|
||||
1. 在訊息中插入變數
|
||||
|
||||
ARB 定義:
|
||||
|
||||
```json
|
||||
"welcomeUser": "你好,{name}"
|
||||
```
|
||||
|
||||
最終顯示:當 `name = 小明` 時,顯示「你好,小明」。翻譯時可以調整整句話,但必須保留 `{name}`。
|
||||
|
||||
2. 依數量顯示不同訊息
|
||||
|
||||
ARB 定義:
|
||||
|
||||
```json
|
||||
"fileCount": "{count, plural, =0{沒有檔案} =1{1 個檔案} other{{count} 個檔案}}"
|
||||
```
|
||||
|
||||
最終顯示:
|
||||
|
||||
- `count = 0` →「沒有檔案」
|
||||
- `count = 1` →「1 個檔案」
|
||||
- `count = 3` →「3 個檔案」
|
||||
|
||||
`=0`、`=1`、`other` 分別代表數量為 0、數量為 1 和其他數量。只翻譯各分支大括號內顯示給使用者的文字。
|
||||
|
||||
3. 依變數值顯示不同訊息
|
||||
|
||||
ARB 定義:
|
||||
|
||||
```json
|
||||
"taskState": "{state, select, running{執行中} paused{已暫停} other{未知狀態}}"
|
||||
```
|
||||
|
||||
最終顯示:
|
||||
|
||||
- `state = running` →「執行中」
|
||||
- `state = paused` →「已暫停」
|
||||
- 其他值 →「未知狀態」
|
||||
|
||||
`running`、`paused`、`other` 是程式傳入的分支值,不能翻譯;只翻譯其後大括號內的顯示文字。
|
||||
|
||||
## flutter開發
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
FROM golang:1.24.11-alpine3.23 AS go
|
||||
FROM golang:1.25.4-alpine3.22 AS go
|
||||
WORKDIR /app
|
||||
COPY ./go.mod ./go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
@@ -4,172 +4,77 @@
|
||||
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||
[](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://discord.gg/ZUJqJrwCGB)
|
||||
|
||||
<a href="https://trendshift.io/repositories/7953" target="_blank"><img src="https://trendshift.io/api/badge/repositories/7953" alt="GopeedLab%2Fgopeed | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
||||
|
||||
[](https://ko-fi.com/R6R6IJGN6)
|
||||
|
||||
[English](/README.md) | [中文](/README_zh-CN.md) | [日本語](/README_ja-JP.md) | [正體中文](/README_zh-TW.md) | [Tiếng Việt](/README_vi-VN.md)
|
||||
|
||||
## 🚀 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 (short for **Go Speed**) is a fast, modern, free, and open-source download manager built with Go and Flutter. It supports HTTP, HTTPS, BitTorrent, magnet links, and ed2k on desktop, mobile, and the web.
|
||||
|
||||
Visit ✈ [Official Website](https://gopeed.com) | 📖 [Official Docs](https://docs.gopeed.com)
|
||||
Beyond core download management, Gopeed offers browser integration, JavaScript extensions, a REST API, a CLI, and a self-hosted web UI for customization and automation.
|
||||
|
||||
Visit ✈ [Official Website](https://gopeed.com)
|
||||
|
||||

|
||||
|
||||
## ✨ Features
|
||||
|
||||
- ⚡ **High-speed downloads** — combine concurrent tasks, multi-connection HTTP transfers, and peer-to-peer BitTorrent downloads to make the most of your bandwidth.
|
||||
- 🧲 **Multiple protocols** — download HTTP/HTTPS files, torrents, magnet links, and ed2k resources from a single app.
|
||||
- 🌱 **Full-featured BitTorrent** — use DHT peer discovery, uTP transport, Web Seeds, selective file downloads, tracker management, peer and piece statistics, and ratio- or time-based seeding limits.
|
||||
- 📋 **Flexible task management** — pause, resume, retry, run batch operations, search, filter by status, organize with categories, and recover tasks after a restart.
|
||||
- 🪶 **Lightweight native experience** — the main interface is rendered natively with Flutter. No Electron. No WebView shell. Enjoy a smaller footprint, lower overhead, and responsive performance.
|
||||
- 💻 **Cross-platform** — available for Windows, macOS, Linux, Android, iOS, and the web, with Docker and QNAP deployment options.
|
||||
- 🎨 **Customizable appearance** — follow your system theme or choose light or dark mode, with eight accent colors.
|
||||
- 📐 **Responsive interface** — task lists, navigation, settings, and detail views adapt to phones, tablets, and resizable desktop windows.
|
||||
- 🗣️ **Available in 20+ languages** — including English, Simplified and Traditional Chinese, Japanese, Korean, and many more.
|
||||
- 🌐 **Browser integration** — send downloads from Chrome, Edge, Firefox, and other compatible browsers directly to Gopeed.
|
||||
- 🧩 **JavaScript extensions** — add support for video platforms, AI model hubs, cloud storage services, and other download sources.
|
||||
- 🤖 **AI integration** — use Gopeed's MCP endpoint to connect compatible AI agents and create, inspect, or manage downloads with natural language.
|
||||
- 🔌 **Automation-ready** — integrate with Gopeed through its REST API, CLI, authenticated web UI, webhooks, and post-download scripts.
|
||||
- 🛠️ **Built-in essentials** — customize headers and the User-Agent, use proxies and GitHub mirrors, receive notifications, and extract archives automatically.
|
||||
|
||||
## 🤖 AI Integration
|
||||
|
||||
Connect Gopeed to an AI agent and manage downloads with natural language. For example, you can say:
|
||||
|
||||
> Download the latest Gopeed client for Windows.
|
||||
|
||||
| Tool | Description |
|
||||
| --- | --- |
|
||||
| `resolve_task` | Resolve a download URL or URI and return its resource metadata and files before creating a task. |
|
||||
| `create_task` | Create and start a task from a resolved resource ID or a direct download request. |
|
||||
| `list_tasks` | List tasks, optionally filtering them by ID or status. |
|
||||
| `get_task` | Get the request, resource, options, and current progress for one task. |
|
||||
| `get_task_status` | Get lightweight runtime status and per-file progress for one task. |
|
||||
| `get_task_stats` | Get protocol-specific statistics, including HTTP connections or BitTorrent peers and seeding data. |
|
||||
| `pause_task` | Pause a task. |
|
||||
| `continue_task` | Continue a paused or failed task. |
|
||||
| `delete_task` | Delete a task, optionally deleting its downloaded files. |
|
||||
|
||||
## ⬇️ Download
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td rowspan="4">🪟 Windows</td>
|
||||
<td rowspan="2"><code>EXE</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>Portable</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-amd64-portable.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-arm64-portable.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="3">🍎 MacOS</td>
|
||||
<td rowspan="3"><code>DMG</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos-amd64.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos-arm64.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="6">🐧 Linux</td>
|
||||
<td><code>Flathub</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://flathub.org/apps/com.gopeed.Gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>SNAP</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://snapcraft.io/gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>DEB</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-amd64.deb">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-arm64.deb">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>AppImage</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-amd64.AppImage">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-arm64.AppImage">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="4">🤖 Android</td>
|
||||
<td rowspan="4"><code>APK</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>armeabi-v7a</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-armeabi-v7a.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64-v8a</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-arm64-v8a.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>x86_64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-x86_64.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>📱 iOS</td>
|
||||
<td><code>IPA</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-ios.ipa">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>🐳 Docker</td>
|
||||
<td>-</td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://hub.docker.com/r/liwei2633/gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2">💾 Qnap</td>
|
||||
<td rowspan="2"><code>QPKG</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-$version-qnap-amd64.qpkg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-$version-qnap-arm64.qpkg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="8">🌐 Web</td>
|
||||
<td rowspan="3"><code>Windows</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>386</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-386.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>MacOS</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-macos-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-macos-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="3"><code>Linux</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>386</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-386.zip">📥</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
### 🧪 Gopeed 2.0.0 Beta
|
||||
|
||||
More about installation, please refer to [Installation](https://docs.gopeed.com/install.html)
|
||||
Gopeed 2.0.0 is currently in public beta, introducing a redesigned interface, a native communication architecture that connects desktop and mobile clients directly to the Go core through FFI, a more consistent cross-platform experience, improved task management, more flexible API support, and MCP-based AI agent integration. Some features may still be incomplete or unstable, so please try it and report any issues you encounter.
|
||||
|
||||
### 🛠️ Command tool
|
||||
- [Download Gopeed 2.0.0 Beta 2](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.2)
|
||||
|
||||
use `go install`:
|
||||
Once the features and stability meet our release standards, we will publish the official Gopeed 2.0.0 release. Beta users will be able to upgrade directly to the final release, while existing stable users will not be automatically moved onto the beta channel.
|
||||
|
||||
### Stable release
|
||||
|
||||
- [Official Download](https://gopeed.com)
|
||||
- [GitHub Releases](https://github.com/GopeedLab/gopeed/releases/latest)
|
||||
|
||||
### 🛠️ Command-line tool
|
||||
|
||||
Install the CLI with `go install`:
|
||||
|
||||
```bash
|
||||
go install github.com/GopeedLab/gopeed/cmd/gopeed@latest
|
||||
@@ -177,32 +82,28 @@ go install github.com/GopeedLab/gopeed/cmd/gopeed@latest
|
||||
|
||||
## 🔌 Browser Extension
|
||||
|
||||
Gopeed also provides a browser extension to take over browser downloads, supporting browsers such as Chrome, Edge, Firefox, etc., please refer to: [https://github.com/GopeedLab/browser-extension](https://github.com/GopeedLab/browser-extension)
|
||||
Use the Gopeed browser extension to send downloads from Chrome, Edge, Firefox, and other compatible browsers directly to Gopeed: [GopeedLab/browser-extension](https://github.com/GopeedLab/browser-extension)
|
||||
|
||||
## 📱 WeChat Official Account
|
||||
|
||||
Follow our WeChat Official Account to get the latest updates and news.
|
||||
Follow Gopeed's official WeChat account for updates and news.
|
||||
|
||||
<img src="_docs/img/weixin.png" width="200" />
|
||||
|
||||
## 💝 Donate
|
||||
|
||||
If you like this project, please consider [donating](https://docs.gopeed.com/donate.html) to support the development of this project, thank you!
|
||||
|
||||
## 🖼️ Showcase
|
||||
|
||||

|
||||
If Gopeed is useful to you, please consider [supporting its development](https://gopeed.com/docs/donate). Thank you!
|
||||
|
||||
## 👨💻 Development
|
||||
|
||||
This project is divided into two parts, the front end uses `flutter`, the back end uses `Golang`, and the two sides communicate through the `http` protocol. On the unix system, `unix socket` is used, and on the windows system, `tcp` protocol is used.
|
||||
Gopeed consists of a Flutter front end and a Go back end. They communicate over HTTP, using Unix sockets on Unix-like systems and TCP on Windows.
|
||||
|
||||
> The front code is located in the `ui/flutter` directory.
|
||||
> The front-end source is located in the `ui/flutter` directory.
|
||||
|
||||
### 🌍 Environment
|
||||
|
||||
1. Golang 1.24+
|
||||
2. Flutter 3.38+
|
||||
1. Go 1.25+
|
||||
2. Flutter 3.41+
|
||||
|
||||
### 📋 Clone
|
||||
|
||||
@@ -212,17 +113,17 @@ git clone [email protected]:GopeedLab/gopeed.git
|
||||
|
||||
### 🤝 Contributing
|
||||
|
||||
Please refer to [CONTRIBUTING.md](/CONTRIBUTING.md)
|
||||
See [CONTRIBUTING.md](/CONTRIBUTING.md).
|
||||
|
||||
### 🏗️ Build
|
||||
|
||||
#### Desktop
|
||||
|
||||
First, you need to configure the environment according to the official [Flutter desktop website documention](https://docs.flutter.dev/development/platform-integration/desktop), then you will need to ensure the cgo environment is set up accordingly. For detailed instructions on setting up the cgo environment, please refer to relevant resources available online.
|
||||
Set up Flutter desktop development using the official [Flutter desktop guide](https://docs.flutter.dev/development/platform-integration/desktop), and make sure a working C toolchain is available for cgo. Then run the commands for your platform.
|
||||
|
||||
command:
|
||||
Commands:
|
||||
|
||||
- windows
|
||||
- Windows
|
||||
|
||||
```bash
|
||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/GopeedLab/gopeed/bind/desktop
|
||||
@@ -230,7 +131,7 @@ cd ui/flutter
|
||||
flutter build windows
|
||||
```
|
||||
|
||||
- macos
|
||||
- macOS
|
||||
|
||||
```bash
|
||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/macos/Frameworks/libgopeed.dylib github.com/GopeedLab/gopeed/bind/desktop
|
||||
@@ -238,7 +139,7 @@ cd ui/flutter
|
||||
flutter build macos
|
||||
```
|
||||
|
||||
- linux
|
||||
- Linux
|
||||
|
||||
```bash
|
||||
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/GopeedLab/gopeed/bind/desktop
|
||||
@@ -248,7 +149,7 @@ flutter build linux
|
||||
|
||||
#### Mobile
|
||||
|
||||
Same as before, you also need to prepare the `cgo` environment, and then install `gomobile`:
|
||||
Mobile builds also require a working cgo toolchain. Install and initialize `gomobile`:
|
||||
|
||||
```bash
|
||||
go install golang.org/x/mobile/cmd/gomobile@latest
|
||||
@@ -256,9 +157,9 @@ go get golang.org/x/mobile/bind
|
||||
gomobile init
|
||||
```
|
||||
|
||||
command:
|
||||
Commands:
|
||||
|
||||
- android
|
||||
- Android
|
||||
|
||||
```bash
|
||||
gomobile bind -tags nosqlite -ldflags="-w -s -checklinkname=0" -o ui/flutter/android/app/libs/libgopeed.aar -target=android -androidapi 21 -javapkg="com.gopeed" github.com/GopeedLab/gopeed/bind/mobile
|
||||
@@ -266,7 +167,7 @@ cd ui/flutter
|
||||
flutter build apk
|
||||
```
|
||||
|
||||
- ios
|
||||
- iOS
|
||||
|
||||
```bash
|
||||
gomobile bind -tags nosqlite -ldflags="-w -s" -o ui/flutter/ios/Frameworks/Libgopeed.xcframework -target=ios github.com/GopeedLab/gopeed/bind/mobile
|
||||
@@ -276,7 +177,7 @@ flutter build ios --no-codesign
|
||||
|
||||
#### Web
|
||||
|
||||
command:
|
||||
Build the web app and server:
|
||||
|
||||
```bash
|
||||
cd ui/flutter
|
||||
|
||||
+57
-147
@@ -4,159 +4,73 @@
|
||||
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||
[](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://discord.gg/ZUJqJrwCGB)
|
||||
|
||||
<a href="https://trendshift.io/repositories/7953" target="_blank"><img src="https://trendshift.io/api/badge/repositories/7953" alt="GopeedLab%2Fgopeed | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
||||
|
||||
[](https://ko-fi.com/R6R6IJGN6)
|
||||
|
||||
[English](/README.md) | [中文](/README_zh-CN.md) | [日本語](/README_ja-JP.md) | [正體中文](/README_zh-TW.md) | [Tiếng Việt](/README_vi-VN.md)
|
||||
|
||||
## 🚀 はじめに
|
||||
|
||||
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** の略)は、Go と Flutter で開発された高速でモダンな無料オープンソース・ダウンロードマネージャーです。HTTP、HTTPS、BitTorrent、Magnet、ed2k に対応し、デスクトップ、モバイル、Web で利用できます。
|
||||
|
||||
見て下さい ✈ [公式ウェブサイト](https://gopeed.com) | 📖 [開発ドキュメント](https://docs.gopeed.com)
|
||||
ブラウザー連携、JavaScript 拡張、REST API、CLI、セルフホスト可能な Web UI により、日常利用から高度な自動化まで幅広く対応します。
|
||||
|
||||
✈ [公式サイト](https://gopeed.com)
|
||||
|
||||

|
||||
|
||||
## ✨ 主な機能
|
||||
|
||||
- ⚡ **高速ダウンロード** — 複数タスク、HTTP のマルチ接続分割転送、BitTorrent の P2P ダウンロードで帯域を最大限に活用します。
|
||||
- 🧲 **マルチプロトコル** — HTTP、HTTPS、BitTorrent、Magnet、ed2k を一つの画面で管理します。
|
||||
- 🌱 **充実した BT 機能** — DHT によるピア検出、uTP 転送、Web Seed、ファイル選択、Tracker 管理、Peer/ピース統計、共有率または時間によるシード制御。
|
||||
- 📋 **タスク管理** — 一時停止、再開、再試行、一括操作、検索、状態フィルター、カテゴリ、起動時復元。
|
||||
- 🪶 **軽量なネイティブ体験** — メイン UI は Flutter でネイティブ描画。Electron 非採用。WebView ラッパーでもありません。パッケージサイズと実行時オーバーヘッドを抑え、軽快な応答性を実現します。
|
||||
- 💻 **クロスプラットフォーム** — Windows、macOS、Linux、Android、iOS、Web、Docker、QNAP に対応。
|
||||
- 🎨 **カスタマイズ可能なテーマ** — システム連動、ライト、ダークの各モードと 8 色のアクセントカラーに対応。
|
||||
- 📐 **レスポンシブ UI** — タスクリスト、ナビゲーション、設定、詳細画面がスマートフォン、タブレット、サイズ変更可能なデスクトップウィンドウに適応。
|
||||
- 🗣️ **20 以上の UI 言語** — 日本語、英語、簡体字・繁体字中国語、韓国語など多くの言語に対応。
|
||||
- 🌐 **ブラウザー連携** — Chrome、Edge、Firefox などのダウンロードを Gopeed に送信できます。
|
||||
- 🧩 **JavaScript 拡張** — 動画サイト、AI モデルハブ、クラウドストレージなどのダウンロード元を追加できます。
|
||||
- 🤖 **AI 連携** — MCP インターフェースを通じて対応する AI Agent と連携し、自然言語でダウンロードタスクを作成・確認・管理できます。
|
||||
- 🔌 **オープンなインターフェース** — REST API、CLI、認証付き Web UI、Webhook、完了後スクリプトに対応。
|
||||
- 🛠️ **便利な内蔵機能** — カスタム Header/User-Agent、Proxy、GitHub ミラー、通知、自動展開。
|
||||
|
||||
## 🤖 AI 連携
|
||||
|
||||
Gopeed を AI Agent と連携すると、自然言語でダウンロードを管理できます。たとえば、AI Agent に次のように依頼できます:
|
||||
|
||||
> 最新の Gopeed Windows クライアントをダウンロードして
|
||||
|
||||
| Tool | 説明 |
|
||||
| --- | --- |
|
||||
| `resolve_task` | ダウンロード URL または URI を解析し、タスク作成前にリソース情報とファイル一覧を返します。 |
|
||||
| `create_task` | 解析済みのリソース ID または直接のダウンロードリクエストからタスクを作成して開始します。 |
|
||||
| `list_tasks` | タスクを一覧表示し、必要に応じて ID または状態で絞り込みます。 |
|
||||
| `get_task` | 一つのタスクについて、リクエスト、リソース、オプション、現在の進捗を取得します。 |
|
||||
| `get_task_status` | 一つのタスクの簡易的な実行状態とファイル別の進捗を取得します。 |
|
||||
| `get_task_stats` | HTTP 接続数や BitTorrent の Peer・シード情報など、プロトコル固有の統計を取得します。 |
|
||||
| `pause_task` | タスクを一時停止します。 |
|
||||
| `continue_task` | 一時停止または失敗したタスクを再開します。 |
|
||||
| `delete_task` | タスクを削除し、必要に応じてダウンロード済みファイルも削除します。 |
|
||||
|
||||
## ⬇️ インストール
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td rowspan="2">🪟 Windows</td>
|
||||
<td><code>EXE</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>Portable</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-amd64-portable.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="3">🍎 MacOS</td>
|
||||
<td rowspan="3"><code>DMG</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos-amd64.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos-arm64.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="6">🐧 Linux</td>
|
||||
<td><code>Flathub</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://flathub.org/apps/com.gopeed.Gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>SNAP</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://snapcraft.io/gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>DEB</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-amd64.deb">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-arm64.deb">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>AppImage</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-amd64.AppImage">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-arm64.AppImage">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="4">🤖 Android</td>
|
||||
<td rowspan="4"><code>APK</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>armeabi-v7a</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-armeabi-v7a.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64-v8a</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-arm64-v8a.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>x86_64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-x86_64.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>📱 iOS</td>
|
||||
<td><code>IPA</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-ios.ipa">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>🐳 Docker</td>
|
||||
<td>-</td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://hub.docker.com/r/liwei2633/gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2">💾 Qnap</td>
|
||||
<td rowspan="2"><code>QPKG</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-$version-qnap-amd64.qpkg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-$version-qnap-arm64.qpkg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="8">🌐 Web</td>
|
||||
<td rowspan="3"><code>Windows</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>386</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-386.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>MacOS</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-macos-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-macos-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="3"><code>Linux</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>386</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-386.zip">📥</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
インストールについての詳細は、[インストール](https://docs.gopeed.com/install.html)を参照してください。
|
||||
### 🧪 Gopeed 2.0.0 Beta
|
||||
|
||||
Gopeed 2.0.0 は現在パブリックベータ版です。刷新されたインターフェース、デスクトップおよびモバイルから FFI で Go コアへ直接接続するネイティブ通信アーキテクチャ、より統一されたクロスプラットフォーム体験、強化されたタスク管理、より柔軟な API、MCP による AI Agent 連携機能を導入しています。一部の機能はまだ不完全または不安定な場合がありますので、ぜひお試しいただき、問題があればフィードバックをお寄せください。
|
||||
|
||||
- [Gopeed 2.0.0 Beta 2 をダウンロード](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.2)
|
||||
|
||||
機能と安定性が正式リリースの基準に達した時点で、Gopeed 2.0.0 正式版を公開します。ベータ版のユーザーは正式版へ直接アップデートできます。既存の安定版ユーザーが自動的にベータチャンネルへ移行することはありません。
|
||||
|
||||
### 安定版
|
||||
|
||||
- [公式サイトからダウンロード](https://gopeed.com)
|
||||
- [GitHub Releases](https://github.com/GopeedLab/gopeed/releases/latest)
|
||||
|
||||
### 🛠️ コマンドツール
|
||||
|
||||
@@ -168,11 +82,7 @@ Gopeed (正式名 Go Speed) は `Golang` + `Flutter` によって開発された
|
||||
|
||||
## 💝 寄付
|
||||
|
||||
もしこのプロジェクトがお気に召しましたら、このプロジェクトの発展を支援するために[寄付](https://docs.gopeed.com/donate.html)をご検討ください!
|
||||
|
||||
## 🖼️ ショーケース
|
||||
|
||||

|
||||
もしこのプロジェクトがお気に召しましたら、このプロジェクトの発展を支援するために[寄付](https://gopeed.com/docs/donate)をご検討ください!
|
||||
|
||||
## 👨💻 開発
|
||||
|
||||
@@ -182,8 +92,8 @@ Gopeed (正式名 Go Speed) は `Golang` + `Flutter` によって開発された
|
||||
|
||||
### 🌍 環境
|
||||
|
||||
1. Go 言語 1.24+
|
||||
2. Flutter 3.38+
|
||||
1. Go 言語 1.25+
|
||||
2. Flutter 3.41+
|
||||
|
||||
### 📋 クローン
|
||||
|
||||
|
||||
+57
-147
@@ -4,159 +4,73 @@
|
||||
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||
[](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://discord.gg/ZUJqJrwCGB)
|
||||
|
||||
<a href="https://trendshift.io/repositories/7953" target="_blank"><img src="https://trendshift.io/api/badge/repositories/7953" alt="GopeedLab%2Fgopeed | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
||||
|
||||
[](https://ko-fi.com/R6R6IJGN6)
|
||||
|
||||
[English](/README.md) | [中文](/README_zh-CN.md) | [日本語](/README_ja-JP.md) | [正體中文](/README_zh-TW.md) | [Tiếng Việt](/README_vi-VN.md)
|
||||
|
||||
## 🚀 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 (viết tắt của **Go Speed**) là trình quản lý tải xuống nhanh, hiện đại, miễn phí và mã nguồn mở, được xây dựng bằng Go và Flutter. Gopeed hỗ trợ HTTP, HTTPS, BitTorrent, Magnet và ed2k trên máy tính, thiết bị di động và Web.
|
||||
|
||||
Truy cập ✈ [Trang web chính thức](https://gopeed.com) | 📖 [Tài liệu chính thức](https://docs.gopeed.com)
|
||||
Ngoài việc quản lý tải xuống hằng ngày, Gopeed còn hỗ trợ tích hợp trình duyệt, tiện ích JavaScript, REST API, CLI và Web UI có thể tự lưu trữ để mở rộng và tự động hóa quy trình.
|
||||
|
||||
Truy cập ✈ [Trang web chính thức](https://gopeed.com)
|
||||
|
||||

|
||||
|
||||
## ✨ Tính năng chính
|
||||
|
||||
- ⚡ **Tải xuống tốc độ cao** — kết hợp nhiều tác vụ, truyền HTTP phân đoạn qua nhiều kết nối và tải BitTorrent P2P để tận dụng tối đa băng thông.
|
||||
- 🧲 **Đa giao thức** — quản lý HTTP, HTTPS, BitTorrent, Magnet và ed2k trong một giao diện.
|
||||
- 🌱 **Bộ công cụ BT đầy đủ** — hỗ trợ khám phá Peer qua DHT, truyền tải uTP, Web Seed, chọn tệp, quản lý Tracker, xem Peer/mảnh và kiểm soát seed theo tỷ lệ hoặc thời gian.
|
||||
- 📋 **Quản lý tác vụ thực tế** — tạm dừng, tiếp tục, thử lại, thao tác hàng loạt, tìm kiếm, bộ lọc, danh mục và khôi phục khi khởi động.
|
||||
- 🪶 **Trải nghiệm native gọn nhẹ** — giao diện chính được Flutter kết xuất native. Không Electron. Không vỏ bọc WebView. Kích thước nhỏ hơn, ít tốn tài nguyên hơn và phản hồi nhanh hơn.
|
||||
- 💻 **Đa nền tảng** — Windows, macOS, Linux, Android, iOS, Web, Docker và QNAP.
|
||||
- 🎨 **Giao diện tùy biến** — hỗ trợ chế độ theo hệ thống, sáng, tối và 8 màu nhấn.
|
||||
- 📐 **Bố cục thích ứng** — danh sách tác vụ, điều hướng, cài đặt và màn hình chi tiết tự điều chỉnh cho điện thoại, máy tính bảng và cửa sổ desktop có thể thay đổi kích thước.
|
||||
- 🗣️ **Hơn 20 ngôn ngữ giao diện** — gồm tiếng Việt, tiếng Anh, tiếng Trung giản thể và phồn thể, tiếng Nhật, tiếng Hàn cùng nhiều ngôn ngữ khác.
|
||||
- 🌐 **Tích hợp trình duyệt** — gửi tải xuống từ Chrome, Edge, Firefox và các trình duyệt tương thích sang Gopeed.
|
||||
- 🧩 **Tiện ích JavaScript** — thêm nguồn tải từ nền tảng video, kho mô hình AI, lưu trữ đám mây và nhiều dịch vụ khác.
|
||||
- 🤖 **Tích hợp AI** — cung cấp giao diện MCP để kết nối với các AI Agent tương thích và tạo, kiểm tra hoặc quản lý tác vụ tải xuống bằng ngôn ngữ tự nhiên.
|
||||
- 🔌 **Giao diện mở** — REST API, CLI, Web UI có xác thực, webhook và script sau khi tải.
|
||||
- 🛠️ **Công cụ tích hợp** — Header/User-Agent tùy chỉnh, proxy, mirror GitHub, thông báo và tự động giải nén.
|
||||
|
||||
## 🤖 Tích hợp AI
|
||||
|
||||
Sau khi kết nối Gopeed với AI Agent, bạn có thể quản lý tải xuống bằng ngôn ngữ tự nhiên. Ví dụ, hãy nói với AI Agent:
|
||||
|
||||
> Hãy tải xuống phiên bản Gopeed mới nhất dành cho Windows
|
||||
|
||||
| Tool | Mô tả |
|
||||
| --- | --- |
|
||||
| `resolve_task` | Phân tích URL hoặc URI tải xuống và trả về thông tin tài nguyên cùng danh sách tệp trước khi tạo tác vụ. |
|
||||
| `create_task` | Tạo và bắt đầu tác vụ từ ID tài nguyên đã phân tích hoặc từ yêu cầu tải xuống trực tiếp. |
|
||||
| `list_tasks` | Liệt kê tác vụ và có thể lọc theo ID hoặc trạng thái. |
|
||||
| `get_task` | Lấy yêu cầu, tài nguyên, tùy chọn và tiến độ hiện tại của một tác vụ. |
|
||||
| `get_task_status` | Lấy trạng thái chạy rút gọn và tiến độ của từng tệp trong một tác vụ. |
|
||||
| `get_task_stats` | Lấy số liệu thống kê theo giao thức, gồm kết nối HTTP hoặc Peer và dữ liệu seed của BitTorrent. |
|
||||
| `pause_task` | Tạm dừng tác vụ. |
|
||||
| `continue_task` | Tiếp tục tác vụ đã tạm dừng hoặc bị lỗi. |
|
||||
| `delete_task` | Xóa tác vụ và có thể xóa cả các tệp đã tải xuống. |
|
||||
|
||||
## ⬇️ Tải về
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td rowspan="2">🪟 Windows</td>
|
||||
<td><code>EXE</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>Portable</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-amd64-portable.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="3">🍎 MacOS</td>
|
||||
<td rowspan="3"><code>DMG</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos-amd64.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos-arm64.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="6">🐧 Linux</td>
|
||||
<td><code>Flathub</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://flathub.org/apps/com.gopeed.Gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>SNAP</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://snapcraft.io/gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>DEB</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-amd64.deb">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-arm64.deb">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>AppImage</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-amd64.AppImage">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-arm64.AppImage">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="4">🤖 Android</td>
|
||||
<td rowspan="4"><code>APK</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>armeabi-v7a</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-armeabi-v7a.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64-v8a</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-arm64-v8a.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>x86_64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-x86_64.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>📱 iOS</td>
|
||||
<td><code>IPA</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-ios.ipa">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>🐳 Docker</td>
|
||||
<td>-</td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://hub.docker.com/r/liwei2633/gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2">💾 Qnap</td>
|
||||
<td rowspan="2"><code>QPKG</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-$version-qnap-amd64.qpkg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-$version-qnap-arm64.qpkg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="8">🌐 Web</td>
|
||||
<td rowspan="3"><code>Windows</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>386</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-386.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>MacOS</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-macos-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-macos-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="3"><code>Linux</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>386</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-386.zip">📥</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</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)
|
||||
### 🧪 Gopeed 2.0.0 Beta
|
||||
|
||||
Gopeed 2.0.0 hiện đang trong giai đoạn beta công khai, với giao diện được thiết kế lại, kiến trúc giao tiếp native kết nối trực tiếp ứng dụng desktop và di động với lõi Go qua FFI, trải nghiệm đa nền tảng nhất quán hơn, khả năng quản lý tác vụ được cải thiện, API linh hoạt hơn và khả năng tích hợp AI Agent qua MCP. Một số tính năng có thể vẫn chưa hoàn thiện hoặc chưa ổn định, vì vậy hãy dùng thử và gửi phản hồi nếu bạn gặp vấn đề.
|
||||
|
||||
- [Tải Gopeed 2.0.0 Beta 2](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.2)
|
||||
|
||||
Khi các tính năng và độ ổn định đáp ứng tiêu chuẩn phát hành, chúng tôi sẽ phát hành Gopeed 2.0.0 chính thức. Người dùng bản beta có thể nâng cấp trực tiếp lên bản chính thức, trong khi người dùng bản ổn định hiện tại sẽ không tự động được chuyển sang kênh beta.
|
||||
|
||||
### Bản ổn định
|
||||
|
||||
- [Tải xuống từ trang web chính thức](https://gopeed.com)
|
||||
- [GitHub Releases](https://github.com/GopeedLab/gopeed/releases/latest)
|
||||
|
||||
### 🛠️ Công cụ lệnh
|
||||
|
||||
@@ -174,11 +88,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
|
||||
|
||||
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!
|
||||
|
||||
## 🖼️ Trưng bày
|
||||
|
||||

|
||||
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!
|
||||
|
||||
## 👨💻 Development
|
||||
|
||||
@@ -188,8 +98,8 @@ Dự án này được chia thành hai phần, phần giao diện sử dụng `f
|
||||
|
||||
### 🌍 Environment
|
||||
|
||||
1. Golang 1.24+
|
||||
2. Flutter 3.38+
|
||||
1. Golang 1.25+
|
||||
2. Flutter 3.41+
|
||||
|
||||
### 📋 Clone
|
||||
|
||||
|
||||
+57
-147
@@ -4,159 +4,73 @@
|
||||
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||
[](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://discord.gg/ZUJqJrwCGB)
|
||||
|
||||
<a href="https://trendshift.io/repositories/7953" target="_blank"><img src="https://trendshift.io/api/badge/repositories/7953" alt="GopeedLab%2Fgopeed | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
||||
|
||||
[](https://ko-fi.com/R6R6IJGN6)
|
||||
|
||||
[English](/README.md) | [中文](/README_zh-CN.md) | [日本語](/README_ja-JP.md) | [正體中文](/README_zh-TW.md) | [Tiếng Việt](/README_vi-VN.md)
|
||||
|
||||
## 🚀 介绍
|
||||
|
||||
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**,也叫“够快下载器”)是一款使用 Go 和 Flutter 开发的高速、现代、免费开源下载器,支持 HTTP、HTTPS、BitTorrent、磁力链接和 ed2k,并覆盖桌面端、移动端与 Web。
|
||||
|
||||
访问 ✈ [官方网站](https://gopeed.com/zh-CN) | 📖 [官方文档](https://docs.gopeed.com/zh/)
|
||||
除了日常下载任务管理,Gopeed 还提供浏览器接管、JavaScript 扩展、REST API、命令行工具和可自托管 Web UI,方便高级用户扩展和自动化自己的下载流程。
|
||||
|
||||
访问 ✈ [官方网站](https://gopeed.com)
|
||||
|
||||

|
||||
|
||||
## ✨ 主要特性
|
||||
|
||||
- ⚡ **高速下载** — 多任务并发、HTTP 多连接分段传输和 BitTorrent P2P 下载,充分利用可用带宽。
|
||||
- 🧲 **多协议支持** — 在同一个界面管理 HTTP、HTTPS、BitTorrent、磁力链接和 ed2k。
|
||||
- 🌱 **完整 BT 能力** — 支持 DHT 节点发现、uTP 传输、Web Seed、按文件选择下载、Tracker 管理、Peer/分片统计,以及按分享率或时间控制做种。
|
||||
- 📋 **实用任务管理** — 暂停、续传、重试、批量操作、搜索、状态筛选、下载分类和启动恢复。
|
||||
- 🪶 **轻量原生体验** — 主界面使用 Flutter 原生渲染,非 Electron,非 WebView 的套壳,安装包更轻量、运行开销更低、响应更流畅。
|
||||
- 💻 **全平台覆盖** — 支持 Windows、macOS、Linux、Android、iOS 和 Web,并提供 Docker、QNAP 部署方式。
|
||||
- 🎨 **个性化主题** — 支持跟随系统、浅色、深色三种模式,以及 8 种主题强调色。
|
||||
- 📐 **响应式界面** — 任务列表、导航、设置和详情视图会针对手机、平板及可调整大小的桌面窗口自动布局。
|
||||
- 🗣️ **20+ 种界面语言** — 支持简体中文、繁体中文、英语、日语、韩语等多种语言。
|
||||
- 🌐 **浏览器接管** — 支持 Chrome、Edge、Firefox 等兼容浏览器,将下载任务直接发送到 Gopeed。
|
||||
- 🧩 **JavaScript 扩展** — 扩展视频网站、AI 模型站、网盘等更多下载源。
|
||||
- 🤖 **AI 接入** — 开放 MCP 接口,可接入 AI Agent,通过自然语言创建、查询和管理下载任务。
|
||||
- 🔌 **开放接口** — 通过 REST API、CLI、带身份认证的 Web UI、Webhook 和下载后脚本进行自动化。
|
||||
- 🛠️ **常用内置能力** — 自定义 Header 与 User-Agent、代理、GitHub 镜像、通知和自动解压。
|
||||
|
||||
## 🤖 AI 接入
|
||||
|
||||
将 Gopeed 接入 AI Agent 后,即可通过自然语言管理下载。例如,直接对 AI Agent 说:
|
||||
|
||||
> 帮我下载最新的 Gopeed Windows 客户端
|
||||
|
||||
| Tool | 功能说明 |
|
||||
| --- | --- |
|
||||
| `resolve_task` | 解析下载 URL 或 URI,在创建任务前返回资源信息及文件列表。 |
|
||||
| `create_task` | 使用已解析的资源 ID 或直接下载请求创建并启动任务。 |
|
||||
| `list_tasks` | 查询任务列表,并可按任务 ID 或状态筛选。 |
|
||||
| `get_task` | 获取单个任务的请求、资源、选项及当前进度等完整信息。 |
|
||||
| `get_task_status` | 获取单个任务的轻量运行状态和各文件下载进度。 |
|
||||
| `get_task_stats` | 获取协议相关统计信息,包括 HTTP 连接或 BitTorrent Peer 与做种数据。 |
|
||||
| `pause_task` | 暂停任务。 |
|
||||
| `continue_task` | 继续已暂停或失败的任务。 |
|
||||
| `delete_task` | 删除任务,并可选择同时删除已下载文件。 |
|
||||
|
||||
## ⬇️ 下载
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td rowspan="2">🪟 Windows</td>
|
||||
<td><code>EXE</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>Portable</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-amd64-portable.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="3">🍎 MacOS</td>
|
||||
<td rowspan="3"><code>DMG</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos-amd64.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos-arm64.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="6">🐧 Linux</td>
|
||||
<td><code>Flathub</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://flathub.org/apps/com.gopeed.Gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>SNAP</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://snapcraft.io/gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>DEB</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-amd64.deb">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-arm64.deb">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>AppImage</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-amd64.AppImage">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-arm64.AppImage">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="4">🤖 Android</td>
|
||||
<td rowspan="4"><code>APK</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>armeabi-v7a</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-armeabi-v7a.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64-v8a</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-arm64-v8a.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>x86_64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-x86_64.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>📱 iOS</td>
|
||||
<td><code>IPA</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-ios.ipa">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>🐳 Docker</td>
|
||||
<td>-</td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://hub.docker.com/r/liwei2633/gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2">💾 Qnap</td>
|
||||
<td rowspan="2"><code>QPKG</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-$version-qnap-amd64.qpkg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-$version-qnap-arm64.qpkg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="8">🌐 Web</td>
|
||||
<td rowspan="3"><code>Windows</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>386</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-386.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>MacOS</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-macos-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-macos-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="3"><code>Linux</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>386</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-386.zip">📥</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
更多关于安装的内容请参考[安装文档](https://docs.gopeed.com/zh/install.html)
|
||||
### 🧪 Gopeed 2.0.0 Beta
|
||||
|
||||
Gopeed 2.0.0 目前处于公开 Beta 测试阶段,引入了全新界面、通过 FFI 直连 Go 核心的原生通信架构、更一致的跨平台体验、更完善的任务管理、更灵活的 API,以及用于 AI Agent 接入的 MCP 能力。部分功能仍可能不够完善或稳定,欢迎提前体验并向我们反馈问题。
|
||||
|
||||
- [下载 Gopeed 2.0.0 Beta 2](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.2)
|
||||
|
||||
当功能完整度和稳定性达到正式发布标准后,我们会发布 Gopeed 2.0.0 正式版。已安装 Beta 版本的用户可以直接升级到最终正式版,现有稳定版用户则不会被自动切换到 Beta 渠道。
|
||||
|
||||
### 正式稳定版
|
||||
|
||||
- [官方网站下载](https://gopeed.com)
|
||||
- [GitHub Releases](https://github.com/GopeedLab/gopeed/releases/latest)
|
||||
|
||||
### 🛠️ 命令行工具
|
||||
|
||||
@@ -178,11 +92,7 @@ Gopeed 还提供了浏览器扩展用于接管浏览器下载,支持 Chrome、
|
||||
|
||||
## 💝 赞助
|
||||
|
||||
如果觉得项目对你有帮助,请考虑[赞助](https://docs.gopeed.com/zh/donate)以支持这个项目的发展,非常感谢!
|
||||
|
||||
## 🖼️ 界面展示
|
||||
|
||||

|
||||
如果觉得项目对你有帮助,请考虑[赞助](https://gopeed.com/docs/donate)以支持这个项目的发展,非常感谢!
|
||||
|
||||
## 👨💻 开发
|
||||
|
||||
@@ -192,8 +102,8 @@ Gopeed 还提供了浏览器扩展用于接管浏览器下载,支持 Chrome、
|
||||
|
||||
### 🌍 环境要求
|
||||
|
||||
1. Golang 1.24+
|
||||
2. Flutter 3.38+
|
||||
1. Golang 1.25+
|
||||
2. Flutter 3.41+
|
||||
|
||||
### 📋 克隆项目
|
||||
|
||||
|
||||
+56
-147
@@ -4,160 +4,73 @@
|
||||
[](https://codecov.io/gh/GopeedLab/gopeed)
|
||||
[](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://discord.gg/ZUJqJrwCGB)
|
||||
|
||||
<a href="https://trendshift.io/repositories/7953" target="_blank"><img src="https://trendshift.io/api/badge/repositories/7953" alt="GopeedLab%2Fgopeed | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
||||
|
||||
[](https://ko-fi.com/R6R6IJGN6)
|
||||
|
||||
[English](/README.md) | [中文](/README_zh-CN.md) | [日本語](/README_ja-JP.md) | [正體中文](/README_zh-TW.md) | [Tiếng Việt](/README_vi-VN.md)
|
||||
|
||||
## 🚀 簡介
|
||||
|
||||
Gopeed(全稱 Go Speed),是一款使用`Golang`+`Flutter`編寫的高速下載軟體,支援(HTTP、BitTorrent、Magnet)協定,同時支援所有的平台。
|
||||
Gopeed(**Go Speed** 的縮寫)是一款以 Go 與 Flutter 開發的高速、現代化、免費開源下載器,支援 HTTP、HTTPS、BitTorrent、磁力連結與 ed2k,並涵蓋桌面、行動裝置及 Web。
|
||||
|
||||
前往 ✈ [主頁](https://gopeed.com/zh-CN) | 📖 [文檔](https://docs.gopeed.com/zh/)
|
||||
除了日常任務管理,Gopeed 也提供瀏覽器整合、JavaScript 擴充、REST API、命令列工具及可自行託管的 Web UI,方便進階使用者擴充與自動化下載流程。
|
||||
|
||||
瀏覽 ✈ [官方網站](https://gopeed.com)
|
||||
|
||||

|
||||
|
||||
## ✨ 主要功能
|
||||
|
||||
- ⚡ **高速下載** — 多任務並行、HTTP 多連線分段傳輸與 BitTorrent P2P 下載,充分利用可用頻寬。
|
||||
- 🧲 **多協定支援** — 在同一介面管理 HTTP、HTTPS、BitTorrent、磁力連結與 ed2k。
|
||||
- 🌱 **完整 BT 功能** — 支援 DHT 節點探索、uTP 傳輸、Web Seed、逐檔選擇、Tracker 管理、Peer/分片統計,以及依分享率或時間控制做種。
|
||||
- 📋 **實用任務管理** — 暫停、續傳、重試、批次操作、搜尋、狀態篩選、分類及啟動恢復。
|
||||
- 🪶 **輕量原生體驗** — 主介面使用 Flutter 原生渲染,非 Electron,非 WebView 套殼,安裝包更輕量、執行開銷更低、操作回應更流暢。
|
||||
- 💻 **跨平台** — 支援 Windows、macOS、Linux、Android、iOS、Web、Docker 與 QNAP。
|
||||
- 🎨 **個人化主題** — 支援跟隨系統、淺色、深色模式及 8 種主題強調色。
|
||||
- 📐 **響應式介面** — 任務列表、導覽、設定與詳細資訊會配合手機、平板及可調整大小的桌面視窗自動配置。
|
||||
- 🗣️ **20+ 種介面語言** — 支援繁體中文、簡體中文、英文、日文、韓文等多種語言。
|
||||
- 🌐 **瀏覽器整合** — 可接管 Chrome、Edge、Firefox 等相容瀏覽器的下載。
|
||||
- 🧩 **JavaScript 擴充** — 新增影音網站、AI 模型平台、雲端儲存等下載來源。
|
||||
- 🤖 **AI 整合** — 提供 MCP 介面,可串接相容的 AI Agent,以自然語言建立、查詢及管理下載任務。
|
||||
- 🔌 **開放介面** — 支援 REST API、CLI、認證 Web UI、Webhook 及下載後腳本。
|
||||
- 🛠️ **實用內建能力** — 自訂 Header/User-Agent、Proxy、GitHub 鏡像、通知及自動解壓縮。
|
||||
|
||||
## 🤖 AI 整合
|
||||
|
||||
將 Gopeed 與 AI Agent 串接後,即可透過自然語言管理下載。例如,直接對 AI Agent 說:
|
||||
|
||||
> 幫我下載最新的 Gopeed Windows 用戶端
|
||||
|
||||
| Tool | 功能說明 |
|
||||
| --- | --- |
|
||||
| `resolve_task` | 解析下載 URL 或 URI,在建立任務前回傳資源資訊及檔案清單。 |
|
||||
| `create_task` | 使用已解析的資源 ID 或直接下載請求建立並啟動任務。 |
|
||||
| `list_tasks` | 查詢任務清單,並可依任務 ID 或狀態篩選。 |
|
||||
| `get_task` | 取得單一任務的請求、資源、選項及目前進度等完整資訊。 |
|
||||
| `get_task_status` | 取得單一任務的簡要執行狀態及各檔案下載進度。 |
|
||||
| `get_task_stats` | 取得協定相關統計資訊,包括 HTTP 連線或 BitTorrent Peer 與做種資料。 |
|
||||
| `pause_task` | 暫停任務。 |
|
||||
| `continue_task` | 繼續已暫停或失敗的任務。 |
|
||||
| `delete_task` | 刪除任務,並可選擇同時刪除已下載檔案。 |
|
||||
|
||||
## ⬇️ 下載
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td rowspan="2">🪟 Windows</td>
|
||||
<td><code>EXE</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>Portable</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-windows-amd64-portable.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="3">🍎 MacOS</td>
|
||||
<td rowspan="3"><code>DMG</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos-amd64.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-macos-arm64.dmg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="6">🐧 Linux</td>
|
||||
<td><code>Flathub</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://flathub.org/apps/com.gopeed.Gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>SNAP</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://snapcraft.io/gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>DEB</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-amd64.deb">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-arm64.deb">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>AppImage</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-amd64.AppImage">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-linux-arm64.AppImage">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="4">🤖 Android</td>
|
||||
<td rowspan="4"><code>APK</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>armeabi-v7a</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-armeabi-v7a.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64-v8a</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-arm64-v8a.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>x86_64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-android-x86_64.apk">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>📱 iOS</td>
|
||||
<td><code>IPA</code></td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=Gopeed-$version-ios.ipa">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>🐳 Docker</td>
|
||||
<td>-</td>
|
||||
<td>universal</td>
|
||||
<td><a href="https://hub.docker.com/r/liwei2633/gopeed">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2">💾 Qnap</td>
|
||||
<td rowspan="2"><code>QPKG</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-$version-qnap-amd64.qpkg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-$version-qnap-arm64.qpkg">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="8">🌐 Web</td>
|
||||
<td rowspan="3"><code>Windows</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>386</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-windows-386.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="2"><code>MacOS</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-macos-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-macos-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td rowspan="3"><code>Linux</code></td>
|
||||
<td>amd64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-amd64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>arm64</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-arm64.zip">📥</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>386</td>
|
||||
<td><a href="https://gopeed.com/api/download?tpl=gopeed-web-$version-linux-386.zip">📥</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
### 🧪 Gopeed 2.0.0 Beta
|
||||
|
||||
更多關於安裝的內容請參考[安裝文檔](https://docs.gopeed.com/zh/install.html)
|
||||
Gopeed 2.0.0 目前處於公開 Beta 測試階段,引入了全新介面、透過 FFI 直連 Go 核心的原生通訊架構、更一致的跨平台體驗、更完善的任務管理、更靈活的 API,以及透過 MCP 串接 AI Agent 的能力。部分功能仍可能不夠完善或穩定,歡迎搶先體驗並向我們回報問題。
|
||||
|
||||
- [下載 Gopeed 2.0.0 Beta 2](https://github.com/GopeedLab/gopeed/releases/tag/v2.0.0-beta.2)
|
||||
|
||||
當功能完整度與穩定性達到正式發布標準後,我們會發布 Gopeed 2.0.0 正式版。已安裝 Beta 版本的使用者可以直接升級至最終正式版,現有穩定版使用者則不會被自動切換至 Beta 頻道。
|
||||
|
||||
### 正式穩定版
|
||||
|
||||
- [官方網站下載](https://gopeed.com)
|
||||
- [GitHub Releases](https://github.com/GopeedLab/gopeed/releases/latest)
|
||||
|
||||
### 🛠️ 使用 CLI 安裝
|
||||
|
||||
@@ -175,11 +88,7 @@ go install github.com/GopeedLab/gopeed/cmd/gopeed@latest
|
||||
|
||||
## 💝 贊助
|
||||
|
||||
如果你認為該項目對你有所幫助,請考慮[贊助](https://docs.gopeed.com/zh/donate)以支持該項目的持續發展,謝謝!
|
||||
|
||||
## 🖼️ 軟體介面
|
||||
|
||||

|
||||
如果你認為該項目對你有所幫助,請考慮[贊助](https://gopeed.com/docs/donate)以支持該項目的持續發展,謝謝!
|
||||
|
||||
## 👨💻 開發
|
||||
|
||||
@@ -189,8 +98,8 @@ go install github.com/GopeedLab/gopeed/cmd/gopeed@latest
|
||||
|
||||
### 🌍 開發環境
|
||||
|
||||
1. Golang 1.24+
|
||||
2. Flutter 3.38+
|
||||
1. Golang 1.25+
|
||||
2. Flutter 3.41+
|
||||
|
||||
### 📋 克隆項目
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 74 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 77 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 391 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 357 KiB |
+111
-1
@@ -1,8 +1,28 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef void (*TaskEventCallback)(char* payload);
|
||||
|
||||
typedef void (*InvokeResultCallback)(uint64_t request_id, int success, char* payload);
|
||||
|
||||
static void callTaskEventCallback(uintptr_t callback, char* payload) {
|
||||
((TaskEventCallback)callback)(payload);
|
||||
}
|
||||
|
||||
static void callInvokeResultCallback(uintptr_t callback, uint64_t request_id, int success, char* payload) {
|
||||
((InvokeResultCallback)callback)(request_id, success, payload);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
nativebridge "github.com/GopeedLab/gopeed/bind/native"
|
||||
"github.com/GopeedLab/gopeed/pkg/rest"
|
||||
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||
)
|
||||
@@ -16,14 +36,104 @@ func Start(cfg *C.char) (int, *C.char) {
|
||||
return 0, C.CString(err.Error())
|
||||
}
|
||||
config.ProductionMode = true
|
||||
config.NativeMode = true
|
||||
applyWebViewProvider(&config)
|
||||
realPort, err := rest.Start(&config)
|
||||
if err != nil {
|
||||
return 0, C.CString(err.Error())
|
||||
}
|
||||
nativebridge.ResumeInvokes()
|
||||
return realPort, nil
|
||||
}
|
||||
|
||||
//export Stop
|
||||
func Stop() {
|
||||
rest.Stop()
|
||||
nativebridge.Stop()
|
||||
}
|
||||
|
||||
//export GetAPIServerState
|
||||
func GetAPIServerState() *C.char {
|
||||
return C.CString(apiServerResult(rest.GetAPIServerState()))
|
||||
}
|
||||
|
||||
//export StartAPIServer
|
||||
func StartAPIServer() *C.char {
|
||||
return C.CString(apiServerResult(rest.StartAPIServer()))
|
||||
}
|
||||
|
||||
//export StopAPIServer
|
||||
func StopAPIServer() *C.char {
|
||||
return C.CString(apiServerResult(rest.StopAPIServer()))
|
||||
}
|
||||
|
||||
//export RestartAPIServer
|
||||
func RestartAPIServer() *C.char {
|
||||
return C.CString(apiServerResult(rest.RestartAPIServer()))
|
||||
}
|
||||
|
||||
func apiServerResult(state *model.APIServerState, err error) string {
|
||||
result := &model.APIServerOperationResult{State: state}
|
||||
if err != nil {
|
||||
result.Error = err.Error()
|
||||
}
|
||||
data, marshalErr := json.Marshal(result)
|
||||
if marshalErr != nil {
|
||||
return fmt.Sprintf(`{"state":null,"error":%q}`, marshalErr.Error())
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
//export InvokeAsync
|
||||
func InvokeAsync(
|
||||
method *C.char,
|
||||
path *C.char,
|
||||
query *C.char,
|
||||
body *C.char,
|
||||
requestID C.ulonglong,
|
||||
callback C.uintptr_t,
|
||||
) {
|
||||
if callback == 0 {
|
||||
return
|
||||
}
|
||||
complete := func(result string, err error) {
|
||||
success := C.int(1)
|
||||
payload := result
|
||||
if err != nil {
|
||||
success = 0
|
||||
payload = err.Error()
|
||||
}
|
||||
C.callInvokeResultCallback(
|
||||
callback,
|
||||
C.uint64_t(requestID),
|
||||
success,
|
||||
C.CString(payload),
|
||||
)
|
||||
}
|
||||
nativebridge.InvokeAsync(
|
||||
C.GoString(method),
|
||||
C.GoString(path),
|
||||
C.GoString(query),
|
||||
C.GoString(body),
|
||||
complete,
|
||||
)
|
||||
}
|
||||
|
||||
//export SubscribeTaskEvents
|
||||
func SubscribeTaskEvents(mask C.ulonglong, callback C.uintptr_t) {
|
||||
if mask == 0 || callback == 0 {
|
||||
rest.SubscribeTaskEvents(0, nil)
|
||||
return
|
||||
}
|
||||
rest.SubscribeTaskEvents(uint64(mask), func(payload string) {
|
||||
// Ownership crosses the asynchronous Dart callback boundary. Dart frees
|
||||
// this string with FreeCString after decoding it.
|
||||
C.callTaskEventCallback(callback, C.CString(payload))
|
||||
})
|
||||
}
|
||||
|
||||
//export FreeCString
|
||||
func FreeCString(value *C.char) {
|
||||
if value != nil {
|
||||
C.free(unsafe.Pointer(value))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
//go:build darwin
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/GopeedLab/gopeed/internal/webview/rpcprovider"
|
||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||
)
|
||||
|
||||
func applyWebViewProvider(config *model.StartConfig) {
|
||||
if config == nil {
|
||||
return
|
||||
}
|
||||
config.WebViewProvider = newDesktopWebViewProvider(config.WebViewRPCConfig)
|
||||
}
|
||||
|
||||
func newDesktopWebViewProvider(rpcCfg *enginewebview.RPCConfig) enginewebview.Provider {
|
||||
if rpcCfg != nil && rpcCfg.Enabled() {
|
||||
return rpcprovider.New(*rpcCfg)
|
||||
}
|
||||
return enginewebview.NewUnavailableProvider()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
//go:build !darwin
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/GopeedLab/gopeed/internal/webview/goprovider"
|
||||
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||
)
|
||||
|
||||
func applyWebViewProvider(config *model.StartConfig) {
|
||||
if config == nil {
|
||||
return
|
||||
}
|
||||
config.WebViewProvider = goprovider.New()
|
||||
}
|
||||
+72
-2
@@ -4,6 +4,9 @@ package libgopeed
|
||||
import "C"
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
nativebridge "github.com/GopeedLab/gopeed/bind/native"
|
||||
"github.com/GopeedLab/gopeed/internal/webview/rpcprovider"
|
||||
"github.com/GopeedLab/gopeed/pkg/rest"
|
||||
"github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||
)
|
||||
@@ -14,9 +17,76 @@ func Start(cfg string) (int, error) {
|
||||
return 0, err
|
||||
}
|
||||
config.ProductionMode = true
|
||||
return rest.Start(&config)
|
||||
config.NativeMode = true
|
||||
applyWebViewProvider(&config)
|
||||
port, err := rest.Start(&config)
|
||||
if err == nil {
|
||||
nativebridge.ResumeInvokes()
|
||||
}
|
||||
return port, err
|
||||
}
|
||||
|
||||
func Stop() {
|
||||
rest.Stop()
|
||||
nativebridge.Stop()
|
||||
}
|
||||
|
||||
func GetAPIServerState() string {
|
||||
return apiServerResult(rest.GetAPIServerState())
|
||||
}
|
||||
|
||||
func StartAPIServer() string {
|
||||
return apiServerResult(rest.StartAPIServer())
|
||||
}
|
||||
|
||||
func StopAPIServer() string {
|
||||
return apiServerResult(rest.StopAPIServer())
|
||||
}
|
||||
|
||||
func RestartAPIServer() string {
|
||||
return apiServerResult(rest.RestartAPIServer())
|
||||
}
|
||||
|
||||
func apiServerResult(state *model.APIServerState, err error) string {
|
||||
result := &model.APIServerOperationResult{State: state}
|
||||
if err != nil {
|
||||
result.Error = err.Error()
|
||||
}
|
||||
data, _ := json.Marshal(result)
|
||||
return string(data)
|
||||
}
|
||||
|
||||
type InvokeResultListener interface {
|
||||
OnResult(requestID int64, success bool, payload string)
|
||||
}
|
||||
|
||||
func InvokeAsync(method, path, query, body string, requestID int64, listener InvokeResultListener) {
|
||||
if listener == nil {
|
||||
return
|
||||
}
|
||||
nativebridge.InvokeAsync(method, path, query, body, func(result string, err error) {
|
||||
if err != nil {
|
||||
listener.OnResult(requestID, false, err.Error())
|
||||
return
|
||||
}
|
||||
listener.OnResult(requestID, true, result)
|
||||
})
|
||||
}
|
||||
|
||||
type TaskEventListener interface {
|
||||
OnTaskEvent(payload string)
|
||||
}
|
||||
|
||||
func SubscribeTaskEvents(mask int64, listener TaskEventListener) {
|
||||
if mask == 0 || listener == nil {
|
||||
rest.SubscribeTaskEvents(0, nil)
|
||||
return
|
||||
}
|
||||
rest.SubscribeTaskEvents(uint64(mask), listener.OnTaskEvent)
|
||||
}
|
||||
|
||||
func applyWebViewProvider(config *model.StartConfig) {
|
||||
if config == nil || config.WebViewRPCConfig == nil || !config.WebViewRPCConfig.Enabled() {
|
||||
return
|
||||
}
|
||||
config.WebViewProvider = rpcprovider.New(*config.WebViewRPCConfig)
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
// Package nativebridge contains transport-neutral behavior shared by the
|
||||
// Desktop C FFI and gomobile bridge adapters.
|
||||
package nativebridge
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/GopeedLab/gopeed/pkg/rest"
|
||||
)
|
||||
|
||||
// StopTimeout bounds native shutdown even when an in-process request never returns.
|
||||
const StopTimeout = 3 * time.Second
|
||||
|
||||
var errNativeBridgeStopping = errors.New("native bridge is stopping")
|
||||
|
||||
type invokeRequest struct {
|
||||
method string
|
||||
path string
|
||||
query string
|
||||
body string
|
||||
}
|
||||
|
||||
type invokeTask struct {
|
||||
id uint64
|
||||
request invokeRequest
|
||||
complete func(result string, err error)
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func (t *invokeTask) finish(result string, err error) {
|
||||
t.once.Do(func() {
|
||||
completeCallback(t.complete, result, err)
|
||||
})
|
||||
}
|
||||
|
||||
// InvokeAsync starts an in-process API request and completes it through the
|
||||
// supplied callback. Desktop and mobile adapters share this executor.
|
||||
func InvokeAsync(method, path, query, body string, callback func(result string, err error)) {
|
||||
if callback == nil {
|
||||
return
|
||||
}
|
||||
task := invokeTask{
|
||||
request: invokeRequest{
|
||||
method: method,
|
||||
path: path,
|
||||
query: query,
|
||||
body: body,
|
||||
},
|
||||
complete: callback,
|
||||
}
|
||||
if !getInvokeExecutor().submit(task) {
|
||||
completeCallback(callback, "", errNativeBridgeStopping)
|
||||
}
|
||||
}
|
||||
|
||||
// ResumeInvokes enables request submission after the native runtime starts.
|
||||
func ResumeInvokes() {
|
||||
getInvokeExecutor().resume()
|
||||
}
|
||||
|
||||
// PauseInvokesAndWait prevents new submissions and gives running requests a
|
||||
// bounded opportunity to finish before a native runtime is stopped.
|
||||
// Requests still pending after StopTimeout are failed and detached so shutdown
|
||||
// can continue without invoking their callbacks a second time.
|
||||
func PauseInvokesAndWait() bool {
|
||||
return getInvokeExecutor().pauseAndWait(StopTimeout)
|
||||
}
|
||||
|
||||
// Stop pauses native requests and shuts down the shared runtime, but never
|
||||
// keeps the host application waiting longer than StopTimeout. Cleanup that
|
||||
// cannot finish within the deadline is allowed to continue in the background.
|
||||
func Stop() {
|
||||
stopWithin(getInvokeExecutor(), rest.Stop, StopTimeout)
|
||||
}
|
||||
|
||||
func stopWithin(executor *invokeExecutor, cleanup func(), timeout time.Duration) {
|
||||
deadline := time.Now().Add(timeout)
|
||||
executor.pauseAndWait(time.Until(deadline))
|
||||
|
||||
stopped := make(chan struct{})
|
||||
go func() {
|
||||
defer close(stopped)
|
||||
cleanup()
|
||||
}()
|
||||
|
||||
remaining := time.Until(deadline)
|
||||
if remaining <= 0 {
|
||||
return
|
||||
}
|
||||
timer := time.NewTimer(remaining)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-stopped:
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
invokeExecutorOnce sync.Once
|
||||
sharedInvoker *invokeExecutor
|
||||
)
|
||||
|
||||
func getInvokeExecutor() *invokeExecutor {
|
||||
invokeExecutorOnce.Do(func() {
|
||||
sharedInvoker = newInvokeExecutor(func(request invokeRequest) string {
|
||||
return rest.Dispatch(request.method, request.path, request.query, request.body)
|
||||
})
|
||||
})
|
||||
return sharedInvoker
|
||||
}
|
||||
|
||||
// invokeExecutor starts every native API request in its own goroutine, matching
|
||||
// net/http's request concurrency. It only tracks them to support bounded drain
|
||||
// during shutdown; one stuck request must never prevent another from starting.
|
||||
type invokeExecutor struct {
|
||||
mu sync.Mutex
|
||||
closed bool
|
||||
accepting bool
|
||||
nextID uint64
|
||||
pending map[uint64]*invokeTask
|
||||
drained chan struct{}
|
||||
running sync.WaitGroup
|
||||
handler func(invokeRequest) string
|
||||
}
|
||||
|
||||
func newInvokeExecutor(handler func(invokeRequest) string) *invokeExecutor {
|
||||
drained := make(chan struct{})
|
||||
close(drained)
|
||||
return &invokeExecutor{
|
||||
handler: handler,
|
||||
accepting: true,
|
||||
pending: make(map[uint64]*invokeTask),
|
||||
drained: drained,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *invokeExecutor) submit(task invokeTask) bool {
|
||||
e.mu.Lock()
|
||||
if e.closed || !e.accepting {
|
||||
e.mu.Unlock()
|
||||
return false
|
||||
}
|
||||
if len(e.pending) == 0 {
|
||||
e.drained = make(chan struct{})
|
||||
}
|
||||
taskID := e.nextID
|
||||
e.nextID++
|
||||
task.id = taskID
|
||||
taskPtr := &task
|
||||
e.pending[taskID] = taskPtr
|
||||
e.running.Add(1)
|
||||
e.mu.Unlock()
|
||||
|
||||
go e.run(taskPtr)
|
||||
return true
|
||||
}
|
||||
|
||||
func (e *invokeExecutor) resume() {
|
||||
e.mu.Lock()
|
||||
e.accepting = true
|
||||
e.mu.Unlock()
|
||||
}
|
||||
|
||||
func (e *invokeExecutor) pauseAndWait(timeout time.Duration) bool {
|
||||
e.mu.Lock()
|
||||
e.accepting = false
|
||||
if len(e.pending) == 0 {
|
||||
e.mu.Unlock()
|
||||
return true
|
||||
}
|
||||
drained := e.drained
|
||||
e.mu.Unlock()
|
||||
|
||||
timer := time.NewTimer(timeout)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-drained:
|
||||
return true
|
||||
case <-timer.C:
|
||||
}
|
||||
|
||||
e.mu.Lock()
|
||||
if len(e.pending) == 0 {
|
||||
e.mu.Unlock()
|
||||
return true
|
||||
}
|
||||
pending := make([]*invokeTask, 0, len(e.pending))
|
||||
for _, task := range e.pending {
|
||||
pending = append(pending, task)
|
||||
}
|
||||
e.pending = make(map[uint64]*invokeTask)
|
||||
close(e.drained)
|
||||
e.mu.Unlock()
|
||||
|
||||
for _, task := range pending {
|
||||
task.finish("", errNativeBridgeStopping)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *invokeExecutor) close() {
|
||||
e.mu.Lock()
|
||||
e.closed = true
|
||||
e.accepting = false
|
||||
e.mu.Unlock()
|
||||
e.running.Wait()
|
||||
}
|
||||
|
||||
func (e *invokeExecutor) run(task *invokeTask) {
|
||||
defer e.running.Done()
|
||||
result, err := e.execute(task.request)
|
||||
task.finish(result, err)
|
||||
e.mu.Lock()
|
||||
if _, exists := e.pending[task.id]; exists {
|
||||
delete(e.pending, task.id)
|
||||
if len(e.pending) == 0 {
|
||||
close(e.drained)
|
||||
}
|
||||
}
|
||||
e.mu.Unlock()
|
||||
}
|
||||
|
||||
func (e *invokeExecutor) execute(request invokeRequest) (result string, err error) {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
err = fmt.Errorf("invoke panic: %v", recovered)
|
||||
}
|
||||
}()
|
||||
return e.handler(request), nil
|
||||
}
|
||||
|
||||
func completeCallback(callback func(string, error), result string, err error) {
|
||||
defer func() {
|
||||
_ = recover()
|
||||
}()
|
||||
callback(result, err)
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
package nativebridge
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestInvokeExecutorStartsEveryRequestIndependently(t *testing.T) {
|
||||
const taskCount = 64
|
||||
release := make(chan struct{})
|
||||
started := make(chan struct{}, taskCount)
|
||||
executor := newInvokeExecutor(func(request invokeRequest) string {
|
||||
started <- struct{}{}
|
||||
<-release
|
||||
return request.path
|
||||
})
|
||||
|
||||
var completed sync.WaitGroup
|
||||
completed.Add(taskCount)
|
||||
for i := range taskCount {
|
||||
accepted := executor.submit(invokeTask{
|
||||
request: invokeRequest{path: strconv.Itoa(i)},
|
||||
complete: func(string, error) {
|
||||
completed.Done()
|
||||
},
|
||||
})
|
||||
if !accepted {
|
||||
t.Fatalf("request %d was rejected", i)
|
||||
}
|
||||
}
|
||||
|
||||
// Every handler must start before any of them is released. A worker pool
|
||||
// would stall here once all of its workers were occupied.
|
||||
for range taskCount {
|
||||
select {
|
||||
case <-started:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("a request waited behind other blocked requests")
|
||||
}
|
||||
}
|
||||
|
||||
close(release)
|
||||
completed.Wait()
|
||||
executor.close()
|
||||
}
|
||||
|
||||
func TestInvokeExecutorConvertsPanicsToErrors(t *testing.T) {
|
||||
executor := newInvokeExecutor(func(invokeRequest) string {
|
||||
panic("boom")
|
||||
})
|
||||
completed := make(chan error, 1)
|
||||
executor.submit(invokeTask{complete: func(_ string, err error) {
|
||||
completed <- err
|
||||
}})
|
||||
select {
|
||||
case err := <-completed:
|
||||
if err == nil || err.Error() != "invoke panic: boom" {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("panic result was not completed")
|
||||
}
|
||||
executor.close()
|
||||
}
|
||||
|
||||
func TestInvokeExecutorSurvivesCallbackPanic(t *testing.T) {
|
||||
executor := newInvokeExecutor(func(request invokeRequest) string {
|
||||
return request.path
|
||||
})
|
||||
executor.submit(invokeTask{complete: func(string, error) {
|
||||
panic("callback failed")
|
||||
}})
|
||||
|
||||
completed := make(chan string, 1)
|
||||
executor.submit(invokeTask{
|
||||
request: invokeRequest{path: "next"},
|
||||
complete: func(result string, _ error) {
|
||||
completed <- result
|
||||
},
|
||||
})
|
||||
select {
|
||||
case result := <-completed:
|
||||
if result != "next" {
|
||||
t.Fatalf("unexpected result: %q", result)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("worker stopped after callback panic")
|
||||
}
|
||||
executor.close()
|
||||
}
|
||||
|
||||
func TestInvokeExecutorPausesAndDrains(t *testing.T) {
|
||||
release := make(chan struct{})
|
||||
started := make(chan struct{})
|
||||
executor := newInvokeExecutor(func(request invokeRequest) string {
|
||||
if request.path == "running" {
|
||||
close(started)
|
||||
<-release
|
||||
}
|
||||
return request.path
|
||||
})
|
||||
completed := make(chan struct{})
|
||||
if !executor.submit(invokeTask{
|
||||
request: invokeRequest{path: "running"},
|
||||
complete: func(string, error) {
|
||||
close(completed)
|
||||
},
|
||||
}) {
|
||||
t.Fatal("initial task was rejected")
|
||||
}
|
||||
<-started
|
||||
|
||||
drained := make(chan struct{})
|
||||
go func() {
|
||||
executor.pauseAndWait(time.Second)
|
||||
close(drained)
|
||||
}()
|
||||
deadline := time.Now().Add(time.Second)
|
||||
for {
|
||||
executor.mu.Lock()
|
||||
paused := !executor.accepting
|
||||
executor.mu.Unlock()
|
||||
if paused {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("executor did not pause")
|
||||
}
|
||||
runtime.Gosched()
|
||||
}
|
||||
select {
|
||||
case <-drained:
|
||||
t.Fatal("pause returned before the running request completed")
|
||||
default:
|
||||
}
|
||||
if executor.submit(invokeTask{}) {
|
||||
t.Fatal("paused executor accepted a new request")
|
||||
}
|
||||
|
||||
close(release)
|
||||
<-completed
|
||||
select {
|
||||
case <-drained:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("pause did not return after pending requests drained")
|
||||
}
|
||||
|
||||
executor.resume()
|
||||
resumed := make(chan struct{})
|
||||
if !executor.submit(invokeTask{complete: func(string, error) {
|
||||
close(resumed)
|
||||
}}) {
|
||||
t.Fatal("resumed executor rejected a request")
|
||||
}
|
||||
<-resumed
|
||||
executor.close()
|
||||
}
|
||||
|
||||
func TestInvokeExecutorPauseTimesOutAndFailsPendingRequests(t *testing.T) {
|
||||
release := make(chan struct{})
|
||||
started := make(chan struct{})
|
||||
executor := newInvokeExecutor(func(request invokeRequest) string {
|
||||
started <- struct{}{}
|
||||
<-release
|
||||
return request.path
|
||||
})
|
||||
|
||||
errors := make(chan error, 2)
|
||||
for _, path := range []string{"first", "second"} {
|
||||
if !executor.submit(invokeTask{
|
||||
request: invokeRequest{path: path},
|
||||
complete: func(_ string, err error) {
|
||||
errors <- err
|
||||
},
|
||||
}) {
|
||||
t.Fatalf("request %q was rejected", path)
|
||||
}
|
||||
}
|
||||
for range 2 {
|
||||
<-started
|
||||
}
|
||||
|
||||
const timeout = 30 * time.Millisecond
|
||||
startedAt := time.Now()
|
||||
if executor.pauseAndWait(timeout) {
|
||||
t.Fatal("pause unexpectedly reported a complete drain")
|
||||
}
|
||||
if elapsed := time.Since(startedAt); elapsed < timeout || elapsed > 10*timeout {
|
||||
t.Fatalf("pause returned after %s, want close to %s", elapsed, timeout)
|
||||
}
|
||||
for range 2 {
|
||||
select {
|
||||
case err := <-errors:
|
||||
if err == nil || err.Error() != errNativeBridgeStopping.Error() {
|
||||
t.Fatalf("unexpected timeout error: %v", err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("pending request was not failed after the timeout")
|
||||
}
|
||||
}
|
||||
|
||||
close(release)
|
||||
executor.close()
|
||||
select {
|
||||
case err := <-errors:
|
||||
t.Fatalf("request callback ran more than once: %v", err)
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func TestStopWithinUsesOneDeadlineForRequestsAndCleanup(t *testing.T) {
|
||||
releaseRequest := make(chan struct{})
|
||||
requestStarted := make(chan struct{})
|
||||
executor := newInvokeExecutor(func(invokeRequest) string {
|
||||
close(requestStarted)
|
||||
<-releaseRequest
|
||||
return "done"
|
||||
})
|
||||
requestResult := make(chan error, 1)
|
||||
executor.submit(invokeTask{complete: func(_ string, err error) {
|
||||
requestResult <- err
|
||||
}})
|
||||
<-requestStarted
|
||||
|
||||
releaseCleanup := make(chan struct{})
|
||||
cleanupStarted := make(chan struct{})
|
||||
cleanup := func() {
|
||||
close(cleanupStarted)
|
||||
<-releaseCleanup
|
||||
}
|
||||
|
||||
const timeout = 40 * time.Millisecond
|
||||
startedAt := time.Now()
|
||||
stopWithin(executor, cleanup, timeout)
|
||||
if elapsed := time.Since(startedAt); elapsed < timeout || elapsed > 10*timeout {
|
||||
t.Fatalf("stop returned after %s, want close to %s", elapsed, timeout)
|
||||
}
|
||||
select {
|
||||
case <-cleanupStarted:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("cleanup was not started after the request deadline")
|
||||
}
|
||||
select {
|
||||
case err := <-requestResult:
|
||||
if err == nil || err.Error() != errNativeBridgeStopping.Error() {
|
||||
t.Fatalf("unexpected request result: %v", err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("timed-out request was not failed")
|
||||
}
|
||||
|
||||
close(releaseRequest)
|
||||
close(releaseCleanup)
|
||||
executor.close()
|
||||
}
|
||||
@@ -0,0 +1,723 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/GopeedLab/gopeed/internal/webview/goprovider"
|
||||
"github.com/GopeedLab/gopeed/internal/webview/rpcprovider"
|
||||
"github.com/GopeedLab/gopeed/pkg/base"
|
||||
"github.com/GopeedLab/gopeed/pkg/download"
|
||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||
)
|
||||
|
||||
type kvFlag map[string]string
|
||||
|
||||
type webViewConfig struct {
|
||||
provider string
|
||||
network string
|
||||
address string
|
||||
token string
|
||||
}
|
||||
|
||||
func (l *kvFlag) String() string {
|
||||
if l == nil {
|
||||
return ""
|
||||
}
|
||||
data, _ := json.Marshal(map[string]string(*l))
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func (l *kvFlag) Set(value string) error {
|
||||
if *l == nil {
|
||||
*l = make(map[string]string)
|
||||
}
|
||||
idx := -1
|
||||
for i := 0; i < len(value); i++ {
|
||||
if value[i] == '=' {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if idx <= 0 {
|
||||
return fmt.Errorf("invalid label %q, expected key=value", value)
|
||||
}
|
||||
(*l)[value[:idx]] = value[idx+1:]
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
os.Exit(goprovider.RunMainThreadLoop(run))
|
||||
}
|
||||
|
||||
func run() int {
|
||||
if len(os.Args) < 2 {
|
||||
usage()
|
||||
return 2
|
||||
}
|
||||
|
||||
switch os.Args[1] {
|
||||
case "resolve":
|
||||
if err := runResolve(os.Args[2:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
return 1
|
||||
}
|
||||
case "download":
|
||||
if err := runDownload(os.Args[2:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
return 1
|
||||
}
|
||||
case "eval":
|
||||
if err := runEval(os.Args[2:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
return 1
|
||||
}
|
||||
case "-h", "--help", "help":
|
||||
usage()
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "unknown subcommand %q\n", os.Args[1])
|
||||
usage()
|
||||
return 2
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintf(os.Stderr, `gopeed extmock CLI
|
||||
|
||||
Usage:
|
||||
extmock resolve [flags] <extension-dir> <url>
|
||||
extmock download [flags] <extension-dir> <url> <output-dir>
|
||||
extmock eval [flags] [script-file]
|
||||
|
||||
Flags:
|
||||
-label key=value Request label, repeatable
|
||||
-setting key=value Extension setting override, repeatable
|
||||
-webview-provider WebView provider: go|rpc
|
||||
-webview-network RPC network when provider=rpc: unix|tcp
|
||||
-webview-address RPC address when provider=rpc
|
||||
-webview-token RPC bearer token when provider=rpc
|
||||
-verb Print download progress while running
|
||||
-pretty Pretty-print JSON output
|
||||
|
||||
Examples:
|
||||
extmock resolve ./ https://www.google.com
|
||||
extmock download ./ https://example.com/video /tmp/extmock-download
|
||||
extmock resolve ./ https://example.com/video -label env=test -setting token=abc
|
||||
extmock eval ./test.js
|
||||
extmock eval -e "new Blob(['hello']).size"
|
||||
`)
|
||||
}
|
||||
|
||||
func runResolve(args []string) error {
|
||||
fs := flag.NewFlagSet("resolve", flag.ContinueOnError)
|
||||
fs.SetOutput(os.Stderr)
|
||||
var labels kvFlag
|
||||
var settings kvFlag
|
||||
var pretty bool
|
||||
cfg := webViewConfig{}
|
||||
fs.Var(&labels, "label", "Request label in key=value form (repeatable)")
|
||||
fs.Var(&settings, "setting", "Extension setting override in key=value form (repeatable)")
|
||||
fs.StringVar(&cfg.provider, "webview-provider", "", "WebView provider: go|rpc")
|
||||
fs.StringVar(&cfg.network, "webview-network", "", "WebView RPC network: unix|tcp")
|
||||
fs.StringVar(&cfg.address, "webview-address", "", "WebView RPC address")
|
||||
fs.StringVar(&cfg.token, "webview-token", "", "WebView RPC bearer token")
|
||||
fs.BoolVar(&pretty, "pretty", false, "Pretty-print JSON output")
|
||||
|
||||
flagArgs, positionalArgs, err := splitResolveArgs(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := fs.Parse(flagArgs); err != nil {
|
||||
return err
|
||||
}
|
||||
if fs.NArg() != 0 {
|
||||
return fmt.Errorf("unexpected trailing args: %v", fs.Args())
|
||||
}
|
||||
if len(positionalArgs) != 2 {
|
||||
return fmt.Errorf("resolve expects exactly 2 args: <extension-dir> <url>")
|
||||
}
|
||||
|
||||
extDir, err := filepath.Abs(positionalArgs[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
url := positionalArgs[1]
|
||||
|
||||
d, cleanup, err := newExtMockDownloader(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
ext, err := d.InstallExtensionByFolder(extDir, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(settings) > 0 {
|
||||
if err := d.UpdateExtensionSettings(ext.Identity, toAnyMap(settings)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
result, err := d.Resolve(&base.Request{
|
||||
URL: url,
|
||||
Labels: map[string]string(labels),
|
||||
}, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return printJSON(result, pretty)
|
||||
}
|
||||
|
||||
type downloadResult struct {
|
||||
Resolve *download.ResolveResult `json:"resolve"`
|
||||
OutputDir string `json:"outputDir"`
|
||||
Files []*downloadedFileResult `json:"files"`
|
||||
}
|
||||
|
||||
type downloadedFileResult struct {
|
||||
Index int `json:"index"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
TaskID string `json:"taskId"`
|
||||
Status base.Status `json:"status"`
|
||||
Size int64 `json:"size"`
|
||||
Req *base.Request `json:"req,omitempty"`
|
||||
}
|
||||
|
||||
func runDownload(args []string) error {
|
||||
fs := flag.NewFlagSet("download", flag.ContinueOnError)
|
||||
fs.SetOutput(os.Stderr)
|
||||
var labels kvFlag
|
||||
var settings kvFlag
|
||||
var pretty bool
|
||||
var verbose bool
|
||||
cfg := webViewConfig{}
|
||||
fs.Var(&labels, "label", "Request label in key=value form (repeatable)")
|
||||
fs.Var(&settings, "setting", "Extension setting override in key=value form (repeatable)")
|
||||
fs.StringVar(&cfg.provider, "webview-provider", "", "WebView provider: go|rpc")
|
||||
fs.StringVar(&cfg.network, "webview-network", "", "WebView RPC network: unix|tcp")
|
||||
fs.StringVar(&cfg.address, "webview-address", "", "WebView RPC address")
|
||||
fs.StringVar(&cfg.token, "webview-token", "", "WebView RPC bearer token")
|
||||
fs.BoolVar(&verbose, "verb", false, "Print download progress while running")
|
||||
fs.BoolVar(&verbose, "verbose", false, "Print download progress while running")
|
||||
fs.BoolVar(&pretty, "pretty", false, "Pretty-print JSON output")
|
||||
|
||||
flagArgs, positionalArgs, err := splitDownloadArgs(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := fs.Parse(flagArgs); err != nil {
|
||||
return err
|
||||
}
|
||||
if fs.NArg() != 0 {
|
||||
return fmt.Errorf("unexpected trailing args: %v", fs.Args())
|
||||
}
|
||||
if len(positionalArgs) != 3 {
|
||||
return fmt.Errorf("download expects exactly 3 args: <extension-dir> <url> <output-dir>")
|
||||
}
|
||||
|
||||
extDir, err := filepath.Abs(positionalArgs[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
url := positionalArgs[1]
|
||||
outputDir, err := filepath.Abs(positionalArgs[2])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d, cleanup, err := newExtMockDownloader(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
ext, err := d.InstallExtensionByFolder(extDir, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(settings) > 0 {
|
||||
if err := d.UpdateExtensionSettings(ext.Identity, toAnyMap(settings)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
result, err := d.Resolve(&base.Request{
|
||||
URL: url,
|
||||
Labels: map[string]string(labels),
|
||||
}, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result == nil || result.Res == nil {
|
||||
return fmt.Errorf("resolve returned empty resource")
|
||||
}
|
||||
if len(result.Res.Files) == 0 {
|
||||
return fmt.Errorf("resolve returned no files")
|
||||
}
|
||||
|
||||
resourceOutputDir := outputDir
|
||||
if result.Res.Name != "" {
|
||||
resourceOutputDir = filepath.Join(outputDir, result.Res.Name)
|
||||
}
|
||||
if err := os.MkdirAll(resourceOutputDir, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
waiter := newTaskTerminalWaiter(d)
|
||||
summary := &downloadResult{
|
||||
Resolve: result,
|
||||
OutputDir: resourceOutputDir,
|
||||
Files: make([]*downloadedFileResult, 0, len(result.Res.Files)),
|
||||
}
|
||||
|
||||
for idx, file := range result.Res.Files {
|
||||
if file == nil {
|
||||
return fmt.Errorf("file %d is nil", idx)
|
||||
}
|
||||
if file.Req == nil {
|
||||
return fmt.Errorf("file %d request is nil", idx)
|
||||
}
|
||||
|
||||
targetDir := resourceOutputDir
|
||||
if file.Path != "" {
|
||||
targetDir = filepath.Join(resourceOutputDir, file.Path)
|
||||
}
|
||||
if err := os.MkdirAll(targetDir, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
taskID, err := d.CreateDirect(file.Req, &base.Options{
|
||||
Path: targetDir,
|
||||
Name: file.Name,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("create task for %q failed: %w", file.Name, err)
|
||||
}
|
||||
waiter.Register(taskID)
|
||||
|
||||
summary.Files = append(summary.Files, &downloadedFileResult{
|
||||
Index: idx,
|
||||
Name: file.Name,
|
||||
Path: filepath.Join(targetDir, file.Name),
|
||||
TaskID: taskID,
|
||||
Req: file.Req,
|
||||
})
|
||||
}
|
||||
|
||||
var stopVerbose func()
|
||||
if verbose {
|
||||
stopVerbose = startVerboseTaskListener(d, summary.Files)
|
||||
defer stopVerbose()
|
||||
}
|
||||
|
||||
for _, file := range summary.Files {
|
||||
task, err := waiter.Wait(file.TaskID, 5*time.Minute)
|
||||
if err != nil {
|
||||
return fmt.Errorf("download %q failed: %w", file.Name, err)
|
||||
}
|
||||
file.Status = task.Status
|
||||
if info, statErr := os.Stat(file.Path); statErr == nil {
|
||||
file.Size = info.Size()
|
||||
}
|
||||
}
|
||||
|
||||
return printJSON(summary, pretty)
|
||||
}
|
||||
|
||||
func runEval(args []string) error {
|
||||
fs := flag.NewFlagSet("eval", flag.ContinueOnError)
|
||||
fs.SetOutput(os.Stderr)
|
||||
var (
|
||||
script string
|
||||
pretty bool
|
||||
)
|
||||
cfg := webViewConfig{}
|
||||
fs.StringVar(&script, "e", "", "Inline JavaScript to execute")
|
||||
fs.StringVar(&script, "eval", "", "Inline JavaScript to execute")
|
||||
fs.StringVar(&cfg.provider, "webview-provider", "", "WebView provider: go|rpc")
|
||||
fs.StringVar(&cfg.network, "webview-network", "", "WebView RPC network: unix|tcp")
|
||||
fs.StringVar(&cfg.address, "webview-address", "", "WebView RPC address")
|
||||
fs.StringVar(&cfg.token, "webview-token", "", "WebView RPC bearer token")
|
||||
fs.BoolVar(&pretty, "pretty", false, "Pretty-print JSON output")
|
||||
|
||||
flagArgs, positionalArgs, err := splitEvalArgs(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := fs.Parse(flagArgs); err != nil {
|
||||
return err
|
||||
}
|
||||
if fs.NArg() != 0 {
|
||||
return fmt.Errorf("unexpected trailing args: %v", fs.Args())
|
||||
}
|
||||
if script == "" && len(positionalArgs) == 0 {
|
||||
return fmt.Errorf("eval expects inline code via -e/--eval or a script file path")
|
||||
}
|
||||
if script != "" && len(positionalArgs) > 0 {
|
||||
return fmt.Errorf("eval accepts either inline code or a script file path, not both")
|
||||
}
|
||||
if len(positionalArgs) > 1 {
|
||||
return fmt.Errorf("eval expects at most 1 script file path")
|
||||
}
|
||||
|
||||
d, cleanup, err := newExtMockDownloader(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
runtime, err := d.NewExtensionEngine(newExtMockExtension(), map[string]any{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer runtime.Close()
|
||||
|
||||
var value any
|
||||
if script != "" {
|
||||
value, err = runtime.Eval(script)
|
||||
} else {
|
||||
scriptPath, pathErr := filepath.Abs(positionalArgs[0])
|
||||
if pathErr != nil {
|
||||
return pathErr
|
||||
}
|
||||
value, err = runtime.EvalFile(scriptPath)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return printJSON(value, pretty)
|
||||
}
|
||||
|
||||
func splitResolveArgs(args []string) ([]string, []string, error) {
|
||||
var flagArgs []string
|
||||
var positionalArgs []string
|
||||
for i := 0; i < len(args); i++ {
|
||||
arg := args[i]
|
||||
switch {
|
||||
case arg == "-pretty":
|
||||
flagArgs = append(flagArgs, arg)
|
||||
case arg == "-webview-provider", arg == "-webview-network", arg == "-webview-address", arg == "-webview-token":
|
||||
if i+1 >= len(args) {
|
||||
return nil, nil, fmt.Errorf("flag %q requires a value", arg)
|
||||
}
|
||||
flagArgs = append(flagArgs, arg, args[i+1])
|
||||
i++
|
||||
case arg == "-label":
|
||||
if i+1 >= len(args) {
|
||||
return nil, nil, fmt.Errorf("flag %q requires a value", arg)
|
||||
}
|
||||
flagArgs = append(flagArgs, arg, args[i+1])
|
||||
i++
|
||||
case arg == "-setting":
|
||||
if i+1 >= len(args) {
|
||||
return nil, nil, fmt.Errorf("flag %q requires a value", arg)
|
||||
}
|
||||
flagArgs = append(flagArgs, arg, args[i+1])
|
||||
i++
|
||||
case len(arg) > len("-label=") && arg[:len("-label=")] == "-label=":
|
||||
flagArgs = append(flagArgs, arg)
|
||||
case len(arg) > len("-setting=") && arg[:len("-setting=")] == "-setting=":
|
||||
flagArgs = append(flagArgs, arg)
|
||||
case hasFlagValue(arg, "-webview-provider"), hasFlagValue(arg, "-webview-network"),
|
||||
hasFlagValue(arg, "-webview-address"), hasFlagValue(arg, "-webview-token"):
|
||||
flagArgs = append(flagArgs, arg)
|
||||
case len(arg) > 0 && arg[0] == '-':
|
||||
return nil, nil, fmt.Errorf("unknown flag %q", arg)
|
||||
default:
|
||||
positionalArgs = append(positionalArgs, arg)
|
||||
}
|
||||
}
|
||||
return flagArgs, positionalArgs, nil
|
||||
}
|
||||
|
||||
func splitDownloadArgs(args []string) ([]string, []string, error) {
|
||||
var flagArgs []string
|
||||
var positionalArgs []string
|
||||
for i := 0; i < len(args); i++ {
|
||||
arg := args[i]
|
||||
switch {
|
||||
case arg == "-pretty", arg == "-verb", arg == "-verbose":
|
||||
flagArgs = append(flagArgs, arg)
|
||||
case arg == "-webview-provider", arg == "-webview-network", arg == "-webview-address", arg == "-webview-token":
|
||||
if i+1 >= len(args) {
|
||||
return nil, nil, fmt.Errorf("flag %q requires a value", arg)
|
||||
}
|
||||
flagArgs = append(flagArgs, arg, args[i+1])
|
||||
i++
|
||||
case arg == "-label":
|
||||
if i+1 >= len(args) {
|
||||
return nil, nil, fmt.Errorf("flag %q requires a value", arg)
|
||||
}
|
||||
flagArgs = append(flagArgs, arg, args[i+1])
|
||||
i++
|
||||
case arg == "-setting":
|
||||
if i+1 >= len(args) {
|
||||
return nil, nil, fmt.Errorf("flag %q requires a value", arg)
|
||||
}
|
||||
flagArgs = append(flagArgs, arg, args[i+1])
|
||||
i++
|
||||
case len(arg) > len("-label=") && arg[:len("-label=")] == "-label=":
|
||||
flagArgs = append(flagArgs, arg)
|
||||
case len(arg) > len("-setting=") && arg[:len("-setting=")] == "-setting=":
|
||||
flagArgs = append(flagArgs, arg)
|
||||
case hasFlagValue(arg, "-webview-provider"), hasFlagValue(arg, "-webview-network"),
|
||||
hasFlagValue(arg, "-webview-address"), hasFlagValue(arg, "-webview-token"):
|
||||
flagArgs = append(flagArgs, arg)
|
||||
case len(arg) > 0 && arg[0] == '-':
|
||||
return nil, nil, fmt.Errorf("unknown flag %q", arg)
|
||||
default:
|
||||
positionalArgs = append(positionalArgs, arg)
|
||||
}
|
||||
}
|
||||
return flagArgs, positionalArgs, nil
|
||||
}
|
||||
|
||||
func splitEvalArgs(args []string) ([]string, []string, error) {
|
||||
var flagArgs []string
|
||||
var positionalArgs []string
|
||||
for i := 0; i < len(args); i++ {
|
||||
arg := args[i]
|
||||
switch {
|
||||
case arg == "-pretty":
|
||||
flagArgs = append(flagArgs, arg)
|
||||
case arg == "-webview-provider", arg == "-webview-network", arg == "-webview-address", arg == "-webview-token":
|
||||
if i+1 >= len(args) {
|
||||
return nil, nil, fmt.Errorf("flag %q requires a value", arg)
|
||||
}
|
||||
flagArgs = append(flagArgs, arg, args[i+1])
|
||||
i++
|
||||
case arg == "-e", arg == "--eval":
|
||||
if i+1 >= len(args) {
|
||||
return nil, nil, fmt.Errorf("flag %q requires a value", arg)
|
||||
}
|
||||
flagArgs = append(flagArgs, arg, args[i+1])
|
||||
i++
|
||||
case hasFlagValue(arg, "-e"), hasFlagValue(arg, "--eval"),
|
||||
hasFlagValue(arg, "-webview-provider"), hasFlagValue(arg, "-webview-network"),
|
||||
hasFlagValue(arg, "-webview-address"), hasFlagValue(arg, "-webview-token"):
|
||||
flagArgs = append(flagArgs, arg)
|
||||
case len(arg) > 0 && arg[0] == '-':
|
||||
return nil, nil, fmt.Errorf("unknown flag %q", arg)
|
||||
default:
|
||||
positionalArgs = append(positionalArgs, arg)
|
||||
}
|
||||
}
|
||||
return flagArgs, positionalArgs, nil
|
||||
}
|
||||
|
||||
func hasFlagValue(arg string, prefix string) bool {
|
||||
return len(arg) > len(prefix)+1 && arg[:len(prefix)+1] == prefix+"="
|
||||
}
|
||||
|
||||
func toAnyMap(values kvFlag) map[string]any {
|
||||
result := make(map[string]any, len(values))
|
||||
for key, value := range values {
|
||||
result[key] = value
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func newExtMockDownloader(cfg webViewConfig) (*download.Downloader, func(), error) {
|
||||
webViewProvider, err := newExtMockWebViewProvider(cfg)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
d := download.NewDownloader(&download.DownloaderConfig{
|
||||
Storage: download.NewMemStorage(),
|
||||
WebViewProvider: webViewProvider,
|
||||
})
|
||||
if err := d.Setup(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cleanup := func() {
|
||||
_ = d.Clear()
|
||||
}
|
||||
return d, cleanup, nil
|
||||
}
|
||||
|
||||
func newExtMockExtension() *download.Extension {
|
||||
return &download.Extension{
|
||||
Name: "extmock",
|
||||
Author: "gopeed",
|
||||
Title: "Gopeed ExtMock Runtime",
|
||||
Version: "0.0.0",
|
||||
DevMode: true,
|
||||
}
|
||||
}
|
||||
|
||||
func newExtMockWebViewProvider(cfg webViewConfig) (enginewebview.Provider, error) {
|
||||
switch cfg.provider {
|
||||
case "", "go":
|
||||
return goprovider.New(), nil
|
||||
case "rpc":
|
||||
rpcCfg, err := resolveRPCConfig(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rpcprovider.New(rpcCfg), nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported webview provider %q", cfg.provider)
|
||||
}
|
||||
}
|
||||
|
||||
func resolveRPCConfig(cfg webViewConfig) (enginewebview.RPCConfig, error) {
|
||||
network := cfg.network
|
||||
address := cfg.address
|
||||
if network == "" && address == "" {
|
||||
defaultAddress, err := defaultWebViewRPCUnixSocket()
|
||||
if err != nil {
|
||||
return enginewebview.RPCConfig{}, err
|
||||
}
|
||||
network = "unix"
|
||||
address = defaultAddress
|
||||
}
|
||||
if network == "" || address == "" {
|
||||
return enginewebview.RPCConfig{}, fmt.Errorf("webview rpc requires both network and address")
|
||||
}
|
||||
return enginewebview.RPCConfig{
|
||||
Network: network,
|
||||
Address: address,
|
||||
Token: cfg.token,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func defaultWebViewRPCUnixSocket() (string, error) {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(homeDir, "gopeed_webview.sock"), nil
|
||||
}
|
||||
|
||||
type taskTerminalWaiter struct {
|
||||
downloader *download.Downloader
|
||||
}
|
||||
|
||||
func newTaskTerminalWaiter(d *download.Downloader) *taskTerminalWaiter {
|
||||
return &taskTerminalWaiter{downloader: d}
|
||||
}
|
||||
|
||||
func (w *taskTerminalWaiter) Register(id string) {
|
||||
}
|
||||
|
||||
func (w *taskTerminalWaiter) Wait(id string, timeout time.Duration) (*download.Task, error) {
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
if task, ok := w.currentTerminalTask(id); ok {
|
||||
return task, w.taskError(task)
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
return nil, fmt.Errorf("timeout waiting for task %s", id)
|
||||
}
|
||||
|
||||
func (w *taskTerminalWaiter) currentTerminalTask(id string) (*download.Task, bool) {
|
||||
task := w.downloader.GetTask(id)
|
||||
if task == nil {
|
||||
return nil, false
|
||||
}
|
||||
if task.Status != base.DownloadStatusDone && task.Status != base.DownloadStatusError {
|
||||
return nil, false
|
||||
}
|
||||
return task, true
|
||||
}
|
||||
|
||||
func (w *taskTerminalWaiter) taskError(task *download.Task) error {
|
||||
if task == nil {
|
||||
return fmt.Errorf("task is nil")
|
||||
}
|
||||
if task.Status == base.DownloadStatusError {
|
||||
return fmt.Errorf("task %s finished with status %s", task.ID, task.Status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func startVerboseTaskListener(d *download.Downloader, files []*downloadedFileResult) func() {
|
||||
tracked := make(map[string]string, len(files))
|
||||
for _, file := range files {
|
||||
tracked[file.TaskID] = file.Name
|
||||
}
|
||||
d.Listener(func(event *download.Event) {
|
||||
if event == nil || event.Task == nil {
|
||||
return
|
||||
}
|
||||
name, ok := tracked[event.Task.ID]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
switch event.Key {
|
||||
case download.EventKeyStart, download.EventKeyProgress, download.EventKeyDone, download.EventKeyError:
|
||||
printVerboseTaskEvent(event, name)
|
||||
}
|
||||
})
|
||||
return func() {}
|
||||
}
|
||||
|
||||
func printVerboseTaskEvent(event *download.Event, name string) {
|
||||
task := event.Task
|
||||
ts := time.Now().Format("15:04:05.000")
|
||||
if task == nil {
|
||||
return
|
||||
}
|
||||
total := int64(0)
|
||||
if task.Meta != nil && task.Meta.Res != nil {
|
||||
total = task.Meta.Res.Size
|
||||
}
|
||||
downloaded := int64(0)
|
||||
speed := int64(0)
|
||||
if task.Progress != nil {
|
||||
downloaded = task.Progress.Downloaded
|
||||
speed = task.Progress.Speed
|
||||
}
|
||||
if event.Key == download.EventKeyError && event.Err != nil {
|
||||
fmt.Fprintf(
|
||||
os.Stderr,
|
||||
"[%s] event=%s task=%s status=%s downloaded=%d total=%d speed=%d name=%q err=%v\n",
|
||||
ts,
|
||||
event.Key,
|
||||
task.ID,
|
||||
task.Status,
|
||||
downloaded,
|
||||
total,
|
||||
speed,
|
||||
name,
|
||||
event.Err,
|
||||
)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(
|
||||
os.Stderr,
|
||||
"[%s] event=%s task=%s status=%s downloaded=%d total=%d speed=%d name=%q\n",
|
||||
ts,
|
||||
event.Key,
|
||||
task.ID,
|
||||
task.Status,
|
||||
downloaded,
|
||||
total,
|
||||
speed,
|
||||
name,
|
||||
)
|
||||
}
|
||||
|
||||
func printJSON(v any, pretty bool) error {
|
||||
var (
|
||||
data []byte
|
||||
err error
|
||||
)
|
||||
if pretty {
|
||||
data, err = json.MarshalIndent(v, "", " ")
|
||||
} else {
|
||||
data, err = json.Marshal(v)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
return nil
|
||||
}
|
||||
+4
-6
@@ -99,12 +99,10 @@ var apiMap = map[string]func(message *Message) (data any, err error){
|
||||
return
|
||||
}
|
||||
|
||||
silent := false
|
||||
if v, ok := message.Meta["silent"]; ok {
|
||||
silent, _ = v.(bool)
|
||||
}
|
||||
|
||||
if err := wakeup(silent); err != nil {
|
||||
// Task creation owns its presentation: silent requests stay in the
|
||||
// background, while non-silent requests open a dedicated create window.
|
||||
// Always bootstrap the main process hidden so it never flashes first.
|
||||
if err := wakeup(true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
+24
-9
@@ -18,6 +18,7 @@ type args struct {
|
||||
Username *string `json:"username"`
|
||||
Password *string `json:"password"`
|
||||
ApiToken *string `json:"apiToken"`
|
||||
MCPEnable bool `json:"mcpEnable"`
|
||||
StorageDir *string `json:"storageDir"`
|
||||
WhiteDownloadDirs []string `json:"whiteDownloadDirs"`
|
||||
// DownloadConfig when the first time to start the server, it will be configured as initial value
|
||||
@@ -43,13 +44,21 @@ func parse() *args {
|
||||
func loadCliArgs() *args {
|
||||
cfg := &args{}
|
||||
cfg.Address = flag.String("A", "0.0.0.0", "Bind Address")
|
||||
flag.StringVar(cfg.Address, "address", "0.0.0.0", "Bind Address")
|
||||
cfg.Port = flag.Int("P", 9999, "Bind Port")
|
||||
flag.IntVar(cfg.Port, "port", 9999, "Bind Port")
|
||||
cfg.Username = flag.String("u", "gopeed", "Web Authentication Username")
|
||||
flag.StringVar(cfg.Username, "username", "gopeed", "Web Authentication Username")
|
||||
cfg.Password = flag.String("p", "", "Web Authentication Password, if no password is set, web authentication will not be enabled")
|
||||
flag.StringVar(cfg.Password, "password", "", "Web Authentication Password, if no password is set, web authentication will not be enabled")
|
||||
cfg.ApiToken = flag.String("T", "", "API token, it must be configured when using HTTP API in the case of enabling web authentication")
|
||||
flag.StringVar(cfg.ApiToken, "api-token", "", "API token, it must be configured when using HTTP API in the case of enabling web authentication")
|
||||
flag.BoolVar(&cfg.MCPEnable, "mcp-enable", false, "Enable MCP endpoint")
|
||||
cfg.StorageDir = flag.String("d", "", "Storage directory")
|
||||
whiteDownloadDirs := flag.String("w", "", "White download directories, comma-separated")
|
||||
flag.StringVar(cfg.StorageDir, "storage-dir", "", "Storage directory")
|
||||
whiteDownloadDirs := flag.String("white-download-dirs", "", "White download directories, comma-separated")
|
||||
cfg.configPath = flag.String("c", "./config.json", "Config file path")
|
||||
flag.StringVar(cfg.configPath, "config", "./config.json", "Config file path")
|
||||
flag.Parse()
|
||||
|
||||
// Parse white download directories from comma-separated string
|
||||
@@ -68,21 +77,23 @@ func loadCliArgs() *args {
|
||||
func overrideWithCliArgs(cfg *args, cliConfig *args) {
|
||||
flag.Visit(func(f *flag.Flag) {
|
||||
switch f.Name {
|
||||
case "A":
|
||||
case "A", "address":
|
||||
cfg.Address = cliConfig.Address
|
||||
case "P":
|
||||
case "P", "port":
|
||||
cfg.Port = cliConfig.Port
|
||||
case "u":
|
||||
case "u", "username":
|
||||
cfg.Username = cliConfig.Username
|
||||
case "p":
|
||||
case "p", "password":
|
||||
cfg.Password = cliConfig.Password
|
||||
case "T":
|
||||
case "T", "api-token":
|
||||
cfg.ApiToken = cliConfig.ApiToken
|
||||
case "d":
|
||||
case "mcp-enable":
|
||||
cfg.MCPEnable = cliConfig.MCPEnable
|
||||
case "d", "storage-dir":
|
||||
cfg.StorageDir = cliConfig.StorageDir
|
||||
case "w":
|
||||
case "white-download-dirs":
|
||||
cfg.WhiteDownloadDirs = cliConfig.WhiteDownloadDirs
|
||||
case "c":
|
||||
case "c", "config":
|
||||
cfg.configPath = cliConfig.configPath
|
||||
}
|
||||
})
|
||||
@@ -194,6 +205,10 @@ func loadEnvVars(cfg *args) {
|
||||
}
|
||||
field.Set(reflect.ValueOf(dirs))
|
||||
}
|
||||
} else if field.Kind() == reflect.Bool {
|
||||
if boolVal, err := strconv.ParseBool(envValue); err == nil {
|
||||
field.SetBool(boolVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+85
-18
@@ -1,7 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
@@ -10,6 +12,50 @@ import (
|
||||
"github.com/GopeedLab/gopeed/pkg/base"
|
||||
)
|
||||
|
||||
func TestLoadCliArgsLongNames(t *testing.T) {
|
||||
originalCommandLine := flag.CommandLine
|
||||
originalArgs := os.Args
|
||||
t.Cleanup(func() {
|
||||
flag.CommandLine = originalCommandLine
|
||||
os.Args = originalArgs
|
||||
})
|
||||
|
||||
flags := flag.NewFlagSet("gopeed-web-test", flag.ContinueOnError)
|
||||
flags.SetOutput(io.Discard)
|
||||
flag.CommandLine = flags
|
||||
os.Args = []string{
|
||||
"gopeed-web",
|
||||
"--address", "127.0.0.2",
|
||||
"--port", "8080",
|
||||
"--username", "admin",
|
||||
"--password", "password",
|
||||
"--api-token", "token",
|
||||
"--mcp-enable",
|
||||
"--storage-dir", "/tmp/gopeed",
|
||||
"--white-download-dirs", "/tmp/one,/tmp/two",
|
||||
"--config", "/tmp/gopeed.json",
|
||||
}
|
||||
|
||||
cfg := loadCliArgs()
|
||||
if *cfg.Address != "127.0.0.2" || *cfg.Port != 8080 || *cfg.Username != "admin" ||
|
||||
*cfg.Password != "password" || *cfg.ApiToken != "token" || !cfg.MCPEnable ||
|
||||
*cfg.StorageDir != "/tmp/gopeed" || !reflect.DeepEqual(cfg.WhiteDownloadDirs, []string{"/tmp/one", "/tmp/two"}) ||
|
||||
*cfg.configPath != "/tmp/gopeed.json" {
|
||||
t.Fatalf("unexpected long-name CLI config: %+v", cfg)
|
||||
}
|
||||
|
||||
for _, name := range []string{"A", "P", "u", "p", "T", "d", "c"} {
|
||||
if flags.Lookup(name) == nil {
|
||||
t.Errorf("legacy short flag %q is missing", name)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"M", "w"} {
|
||||
if flags.Lookup(name) != nil {
|
||||
t.Errorf("new option unexpectedly has short flag %q", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaults(t *testing.T) {
|
||||
// Create mock CLI configuration with command line default values
|
||||
cliDefaults := &args{
|
||||
@@ -165,14 +211,15 @@ func TestOverrideWithCliArgs(t *testing.T) {
|
||||
t.Run("flag override documentation", func(t *testing.T) {
|
||||
// Document the expected behavior for each flag:
|
||||
flagBehaviors := map[string]string{
|
||||
"A": "Should override cfg.Address with cliConfig.Address",
|
||||
"P": "Should override cfg.Port with cliConfig.Port",
|
||||
"u": "Should override cfg.Username with cliConfig.Username",
|
||||
"p": "Should override cfg.Password with cliConfig.Password",
|
||||
"T": "Should override cfg.ApiToken with cliConfig.ApiToken",
|
||||
"d": "Should override cfg.StorageDir with cliConfig.StorageDir",
|
||||
"w": "Should override cfg.WhiteDownloadDirs with cliConfig.WhiteDownloadDirs",
|
||||
"c": "Should override cfg.configPath with cliConfig.configPath",
|
||||
"A/address": "Should override cfg.Address with cliConfig.Address",
|
||||
"P/port": "Should override cfg.Port with cliConfig.Port",
|
||||
"u/username": "Should override cfg.Username with cliConfig.Username",
|
||||
"p/password": "Should override cfg.Password with cliConfig.Password",
|
||||
"T/api-token": "Should override cfg.ApiToken with cliConfig.ApiToken",
|
||||
"mcp-enable": "Should override cfg.MCPEnable with cliConfig.MCPEnable",
|
||||
"d/storage-dir": "Should override cfg.StorageDir with cliConfig.StorageDir",
|
||||
"white-download-dirs": "Should override cfg.WhiteDownloadDirs with cliConfig.WhiteDownloadDirs",
|
||||
"c/config": "Should override cfg.configPath with cliConfig.configPath",
|
||||
}
|
||||
|
||||
t.Log("Expected flag override behaviors:")
|
||||
@@ -187,21 +234,23 @@ func TestOverrideWithCliArgs(t *testing.T) {
|
||||
mockFlagVisit := func(config *args, cliConfig *args, flagName string) {
|
||||
// Simulate the switch statement in overrideWithCliArgs
|
||||
switch flagName {
|
||||
case "A":
|
||||
case "A", "address":
|
||||
config.Address = cliConfig.Address
|
||||
case "P":
|
||||
case "P", "port":
|
||||
config.Port = cliConfig.Port
|
||||
case "u":
|
||||
case "u", "username":
|
||||
config.Username = cliConfig.Username
|
||||
case "p":
|
||||
case "p", "password":
|
||||
config.Password = cliConfig.Password
|
||||
case "T":
|
||||
case "T", "api-token":
|
||||
config.ApiToken = cliConfig.ApiToken
|
||||
case "d":
|
||||
case "mcp-enable":
|
||||
config.MCPEnable = cliConfig.MCPEnable
|
||||
case "d", "storage-dir":
|
||||
config.StorageDir = cliConfig.StorageDir
|
||||
case "w":
|
||||
case "white-download-dirs":
|
||||
config.WhiteDownloadDirs = cliConfig.WhiteDownloadDirs
|
||||
case "c":
|
||||
case "c", "config":
|
||||
config.configPath = cliConfig.configPath
|
||||
default:
|
||||
t.Errorf("Unknown flag: %s", flagName)
|
||||
@@ -285,6 +334,20 @@ func TestOverrideWithCliArgs(t *testing.T) {
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
flagName: "mcp-enable",
|
||||
setupConfig: func() *args {
|
||||
return &args{MCPEnable: false}
|
||||
},
|
||||
setupCliConfig: func() *args {
|
||||
return &args{MCPEnable: true}
|
||||
},
|
||||
verify: func(t *testing.T, cfg *args) {
|
||||
if !cfg.MCPEnable {
|
||||
t.Error("MCPEnable flag override failed: got false, want true")
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
flagName: "d",
|
||||
setupConfig: func() *args {
|
||||
@@ -300,7 +363,7 @@ func TestOverrideWithCliArgs(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
flagName: "w",
|
||||
flagName: "white-download-dirs",
|
||||
setupConfig: func() *args {
|
||||
return &args{WhiteDownloadDirs: []string{"/original/dir1", "/original/dir2"}}
|
||||
},
|
||||
@@ -402,6 +465,7 @@ func TestLoadConfigFile(t *testing.T) {
|
||||
"username": "testuser",
|
||||
"password": "testpass",
|
||||
"apiToken": "testtoken",
|
||||
"mcpEnable": true,
|
||||
"storageDir": "/test/storage",
|
||||
"downloadConfig": {
|
||||
"downloadDir": "/test/downloads",
|
||||
@@ -415,6 +479,7 @@ func TestLoadConfigFile(t *testing.T) {
|
||||
Username: stringPtr("testuser"),
|
||||
Password: stringPtr("testpass"),
|
||||
ApiToken: stringPtr("testtoken"),
|
||||
MCPEnable: true,
|
||||
StorageDir: stringPtr("/test/storage"),
|
||||
DownloadConfig: &base.DownloaderStoreConfig{
|
||||
DownloadDir: "/test/downloads",
|
||||
@@ -476,7 +541,7 @@ func TestLoadEnvVars(t *testing.T) {
|
||||
originalEnv := make(map[string]string)
|
||||
envKeys := []string{
|
||||
"GOPEED_ADDRESS", "GOPEED_PORT", "GOPEED_USERNAME",
|
||||
"GOPEED_PASSWORD", "GOPEED_APITOKEN", "GOPEED_STORAGEDIR",
|
||||
"GOPEED_PASSWORD", "GOPEED_APITOKEN", "GOPEED_MCPENABLE", "GOPEED_STORAGEDIR",
|
||||
"GOPEED_DOWNLOADCONFIG", "GOPEED_WHITEDOWNLOADDIRS",
|
||||
}
|
||||
for _, key := range envKeys {
|
||||
@@ -508,6 +573,7 @@ func TestLoadEnvVars(t *testing.T) {
|
||||
"GOPEED_USERNAME": "envuser",
|
||||
"GOPEED_PASSWORD": "envpass",
|
||||
"GOPEED_APITOKEN": "envtoken",
|
||||
"GOPEED_MCPENABLE": "true",
|
||||
"GOPEED_STORAGEDIR": "/env/storage",
|
||||
},
|
||||
expected: &args{
|
||||
@@ -516,6 +582,7 @@ func TestLoadEnvVars(t *testing.T) {
|
||||
Username: stringPtr("envuser"),
|
||||
Password: stringPtr("envpass"),
|
||||
ApiToken: stringPtr("envtoken"),
|
||||
MCPEnable: true,
|
||||
StorageDir: stringPtr("/env/storage"),
|
||||
},
|
||||
},
|
||||
|
||||
@@ -50,6 +50,7 @@ func main() {
|
||||
StorageDir: storageDir,
|
||||
WhiteDownloadDirs: args.WhiteDownloadDirs,
|
||||
ApiToken: *args.ApiToken,
|
||||
MCPEnable: args.MCPEnable,
|
||||
DownloadConfig: args.DownloadConfig,
|
||||
ProductionMode: true,
|
||||
WebEnable: true,
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
module github.com/GopeedLab/gopeed
|
||||
|
||||
go 1.24.9
|
||||
|
||||
toolchain go1.24.11
|
||||
go 1.25.4
|
||||
|
||||
require (
|
||||
github.com/GopeedLab/webview_go v0.0.0-20260423085439-7a2f88b6e9b5
|
||||
github.com/anacrolix/torrent v1.60.1-0.20251217073903-486bcbe758e0
|
||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
|
||||
github.com/bodgit/sevenzip v1.6.1
|
||||
@@ -17,6 +16,8 @@ require (
|
||||
github.com/matoous/go-nanoid/v2 v2.0.0
|
||||
github.com/mattn/go-ieproxy v0.0.12
|
||||
github.com/mholt/archives v0.1.5
|
||||
github.com/modelcontextprotocol/go-sdk v1.7.0
|
||||
github.com/monkeyWie/goed2k v0.0.0-20260317100435-7a7575cf2447
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529
|
||||
github.com/rs/zerolog v1.31.0
|
||||
@@ -37,6 +38,7 @@ require (
|
||||
github.com/felixge/fgprof v0.9.5 // indirect
|
||||
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
|
||||
github.com/google/go-cmp v0.7.0 // indirect
|
||||
github.com/google/jsonschema-go v0.4.3 // indirect
|
||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
|
||||
@@ -52,21 +54,25 @@ require (
|
||||
github.com/quic-go/qpack v0.5.1 // indirect
|
||||
github.com/quic-go/quic-go v0.51.0 // indirect
|
||||
github.com/refraction-networking/utls v1.6.7 // indirect
|
||||
github.com/segmentio/asm v1.1.3 // indirect
|
||||
github.com/segmentio/encoding v0.5.4 // indirect
|
||||
github.com/sorairolake/lzip-go v0.3.8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/ulikunitz/xz v0.5.15 // indirect
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.uber.org/automaxprocs v1.6.0 // indirect
|
||||
go.uber.org/mock v0.5.1 // indirect
|
||||
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
|
||||
golang.org/x/sync v0.19.0 // indirect
|
||||
golang.org/x/oauth2 v0.35.0 // indirect
|
||||
golang.org/x/sync v0.20.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
dario.cat/mergo v1.0.0 // indirect
|
||||
github.com/Microsoft/go-winio v0.6.1
|
||||
github.com/ProtonMail/go-crypto v0.0.0-20230717121422-5aa5874ade95 // indirect
|
||||
github.com/RoaringBitmap/roaring v1.9.4 // indirect
|
||||
github.com/RoaringBitmap/roaring v1.9.4
|
||||
github.com/acomagu/bufpipe v1.0.4 // indirect
|
||||
github.com/alecthomas/atomic v0.1.0-alpha2 // indirect
|
||||
github.com/anacrolix/chansync v0.7.0 // indirect
|
||||
@@ -147,13 +153,13 @@ require (
|
||||
go.opentelemetry.io/otel v1.39.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.39.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.39.0 // indirect
|
||||
golang.org/x/crypto v0.46.0 // indirect
|
||||
golang.org/x/mod v0.31.0 // indirect
|
||||
golang.org/x/net v0.48.0 // indirect
|
||||
golang.org/x/sys v0.40.0 // indirect
|
||||
golang.org/x/text v0.33.0
|
||||
golang.org/x/time v0.14.0 // indirect
|
||||
golang.org/x/tools v0.40.0 // indirect
|
||||
golang.org/x/crypto v0.48.0 // indirect
|
||||
golang.org/x/mod v0.33.0 // indirect
|
||||
golang.org/x/net v0.50.0
|
||||
golang.org/x/sys v0.41.0 // indirect
|
||||
golang.org/x/text v0.34.0
|
||||
golang.org/x/time v0.15.0 // indirect
|
||||
golang.org/x/tools v0.42.0 // indirect
|
||||
gopkg.in/warnings.v0 v0.1.2 // indirect
|
||||
lukechampine.com/blake3 v1.4.1 // indirect
|
||||
modernc.org/libc v1.67.4 // indirect
|
||||
|
||||
@@ -23,6 +23,8 @@ filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmG
|
||||
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/GopeedLab/webview_go v0.0.0-20260423085439-7a2f88b6e9b5 h1:86LVTaNDM/j/uTAD2HedkXNO9LIQlt642VAdbU/0D/M=
|
||||
github.com/GopeedLab/webview_go v0.0.0-20260423085439-7a2f88b6e9b5/go.mod h1:FvV19lu9rQ9NcbczN5y8pbWecURRRbWa8tbfFARBDa0=
|
||||
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
|
||||
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
|
||||
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
|
||||
@@ -242,6 +244,8 @@ github.com/gobwas/ws v1.2.1/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/K
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
@@ -277,6 +281,8 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/jsonschema-go v0.4.3 h1:/DBOLZTfDow7pe2GmaJNhltueGTtDKICi8V8p+DQPd0=
|
||||
github.com/google/jsonschema-go v0.4.3/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=
|
||||
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
||||
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
|
||||
@@ -382,10 +388,14 @@ github.com/minio/minlz v1.0.1 h1:OUZUzXcib8diiX+JYxyRLIdomyZYzHct6EShOKtQY2A=
|
||||
github.com/minio/minlz v1.0.1/go.mod h1:qT0aEB35q79LLornSzeDH75LBf3aH1MV+jB5w9Wasec=
|
||||
github.com/minio/sha256-simd v1.0.1 h1:6kaan5IFmwTNynnKKpDHe6FWHohJOHhCPchzK49dzMM=
|
||||
github.com/minio/sha256-simd v1.0.1/go.mod h1:Pz6AKMiUdngCLpeTL/RJY1M9rUuPMYujV5xJjtbRSN8=
|
||||
github.com/modelcontextprotocol/go-sdk v1.7.0 h1:yqjY2dsbKAC0LSuWZVBMrHgiG8ukXv6NRo0JiALay44=
|
||||
github.com/modelcontextprotocol/go-sdk v1.7.0/go.mod h1:dL7u98E/zjJTGzEq+j30jQ8K2k1mb6LeAH4inEcSGts=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/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 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
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/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
|
||||
github.com/mschoch/smat v0.0.0-20160514031455-90eadee771ae/go.mod h1:qAyveg+e4CE+eKJXWVjKXM4ck2QobLqTDytGJbLLhJg=
|
||||
@@ -503,6 +513,10 @@ github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWR
|
||||
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
|
||||
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 h1:GHRpF1pTW19a8tTFrMLUcfWwyC0pnifVo2ClaLq+hP8=
|
||||
github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46/go.mod h1:uAQ5PCi+MFsC7HjREoAz1BU+Mq60+05gifQSsHSDG/8=
|
||||
github.com/segmentio/asm v1.1.3 h1:WM03sfUOENvvKexOLp+pCqgb/WDjsi7EK8gIsICtzhc=
|
||||
github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=
|
||||
github.com/segmentio/encoding v0.5.4 h1:OW1VRern8Nw6ITAtwSZ7Idrl3MXCFwXHPgqESYfvNt0=
|
||||
github.com/segmentio/encoding v0.5.4/go.mod h1:HS1ZKa3kSN32ZHVZ7ZLPLXWvOVIiZtyJnO1gPH1sKt0=
|
||||
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
||||
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
@@ -556,6 +570,8 @@ github.com/xiaoqidun/setft v0.0.0-20220310121541-be86327699ad h1:QtuWk6dsrNXc/ug
|
||||
github.com/xiaoqidun/setft v0.0.0-20220310121541-be86327699ad/go.mod h1:Jj8p9bgKGTPQ+M8CdUMS9p7Mmdoxa3OAcAjJQBu0CcI=
|
||||
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
|
||||
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
|
||||
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.etcd.io/bbolt v1.4.3 h1:dEadXpI6G79deX5prL3QRNP6JB8UxVkqo4UPnHaNXJo=
|
||||
@@ -592,8 +608,8 @@ golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0
|
||||
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
|
||||
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
|
||||
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
|
||||
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
|
||||
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
|
||||
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
|
||||
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
|
||||
@@ -626,8 +642,8 @@ golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
|
||||
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI=
|
||||
golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg=
|
||||
golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8=
|
||||
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
@@ -655,13 +671,15 @@ golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
|
||||
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
|
||||
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
|
||||
golang.org/x/net v0.50.0 h1:ucWh9eiCGyDR3vtzso0WMQinm2Dnt8cFMuQa9K33J60=
|
||||
golang.org/x/net v0.50.0/go.mod h1:UgoSli3F/pBgdJBHCTc+tp3gmrU4XswgGRgtnwWTfyM=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.35.0 h1:Mv2mzuHuZuY2+bkyWXIHMfhNdJAdwW3FuWeCPYN5GVQ=
|
||||
golang.org/x/oauth2 v0.35.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -671,8 +689,8 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
||||
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
||||
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
@@ -712,8 +730,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
|
||||
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k=
|
||||
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||
@@ -721,8 +739,8 @@ golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
|
||||
golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q=
|
||||
golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg=
|
||||
golang.org/x/term v0.40.0 h1:36e4zGLqU4yhjlmxEaagx2KuYbJq3EwY8K943ZsHcvg=
|
||||
golang.org/x/term v0.40.0/go.mod h1:w2P8uVp06p2iyKKuvXIm7N/y0UCRt3UfJTfZ7oOpglM=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
@@ -735,12 +753,12 @@ 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.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.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
|
||||
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
|
||||
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
|
||||
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
|
||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI=
|
||||
golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4=
|
||||
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
||||
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
||||
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
@@ -769,8 +787,8 @@ golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56/go.mod h1:TB2adYChydJhpapK
|
||||
golang.org/x/tools v0.1.8-0.20211029000441-d6a9af8af023/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA=
|
||||
golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc=
|
||||
golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k=
|
||||
golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
||||
@@ -0,0 +1,677 @@
|
||||
package blob
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const urlPathPrefix = "/__blob/"
|
||||
|
||||
const rangeSourceFailureLimit = 2
|
||||
|
||||
// unclaimedSourceTTL bounds how long a session-backed source may keep its
|
||||
// engine alive without ever being claimed by a download task.
|
||||
var unclaimedSourceTTL = 10 * time.Minute
|
||||
|
||||
var (
|
||||
ErrInvalidURL = errors.New("invalid blob url")
|
||||
ErrInvalidOptions = errors.New("invalid blob options")
|
||||
ErrSourceNotFound = errors.New("blob source not found")
|
||||
ErrSourceRevoked = errors.New("blob source revoked")
|
||||
ErrSourceClosed = errors.New("blob source closed")
|
||||
ErrRangeNotAllowed = errors.New("blob range not allowed")
|
||||
)
|
||||
|
||||
type SessionRef interface {
|
||||
Retain()
|
||||
Release()
|
||||
}
|
||||
|
||||
type OpenRequest struct {
|
||||
Offset int64
|
||||
End int64
|
||||
}
|
||||
|
||||
type OpenFunc func(ctx context.Context, req OpenRequest) (io.ReadCloser, error)
|
||||
|
||||
type CreateOptions struct {
|
||||
ContentType string
|
||||
Size int64
|
||||
Range bool
|
||||
Session SessionRef
|
||||
}
|
||||
|
||||
type Metadata struct {
|
||||
ContentType string
|
||||
Size int64
|
||||
Range bool
|
||||
}
|
||||
|
||||
type Source struct {
|
||||
ID string
|
||||
URL string
|
||||
ContentType string
|
||||
|
||||
mu sync.Mutex
|
||||
size int64
|
||||
rangeEnabled bool
|
||||
revoked bool
|
||||
taskRefs int
|
||||
readErr error
|
||||
rangeFailures int
|
||||
session SessionRef
|
||||
open OpenFunc
|
||||
unclaimed *time.Timer
|
||||
}
|
||||
|
||||
type Registry struct {
|
||||
dir string
|
||||
|
||||
mu sync.RWMutex
|
||||
sources map[string]*Source
|
||||
|
||||
serverMu sync.Mutex
|
||||
listener net.Listener
|
||||
server *http.Server
|
||||
baseURL string
|
||||
}
|
||||
|
||||
func NewRegistry(dir string) *Registry {
|
||||
return &Registry{
|
||||
dir: dir,
|
||||
sources: make(map[string]*Source),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Registry) Dir() string {
|
||||
if r == nil {
|
||||
return ""
|
||||
}
|
||||
return r.dir
|
||||
}
|
||||
|
||||
func (r *Registry) IsURL(raw string) bool {
|
||||
if r == nil {
|
||||
return false
|
||||
}
|
||||
src, err := r.get(raw)
|
||||
return err == nil && src != nil
|
||||
}
|
||||
|
||||
func (r *Registry) CreateBlob(data []byte, contentType string) (string, error) {
|
||||
buf := append([]byte(nil), data...)
|
||||
return r.CreateOpener(func(ctx context.Context, req OpenRequest) (io.ReadCloser, error) {
|
||||
if req.Offset < 0 || req.Offset > int64(len(buf)) {
|
||||
return nil, ErrSourceNotFound
|
||||
}
|
||||
end := int64(len(buf))
|
||||
if req.End >= req.Offset && req.End+1 < end {
|
||||
end = req.End + 1
|
||||
}
|
||||
return io.NopCloser(bytes.NewReader(buf[req.Offset:end])), nil
|
||||
}, &CreateOptions{
|
||||
ContentType: contentType,
|
||||
Size: int64(len(buf)),
|
||||
Range: len(buf) > 0,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Registry) CreateOpener(open OpenFunc, opts *CreateOptions) (string, error) {
|
||||
if r == nil {
|
||||
return "", ErrSourceNotFound
|
||||
}
|
||||
if open == nil {
|
||||
return "", ErrInvalidOptions
|
||||
}
|
||||
if opts == nil {
|
||||
opts = &CreateOptions{}
|
||||
}
|
||||
if opts.Size < 0 {
|
||||
opts.Size = 0
|
||||
}
|
||||
if opts.Range && opts.Size <= 0 {
|
||||
return "", fmt.Errorf("%w: range requires positive size", ErrInvalidOptions)
|
||||
}
|
||||
// Keep server generation selection and source insertion atomic with Close.
|
||||
r.serverMu.Lock()
|
||||
baseURL, err := r.ensureServerLocked()
|
||||
if err != nil {
|
||||
r.serverMu.Unlock()
|
||||
return "", err
|
||||
}
|
||||
id, err := randomID(18)
|
||||
if err != nil {
|
||||
r.serverMu.Unlock()
|
||||
return "", err
|
||||
}
|
||||
if opts.Session != nil {
|
||||
opts.Session.Retain()
|
||||
}
|
||||
srcURL := fmt.Sprintf("%s%s", baseURL, id)
|
||||
src := &Source{
|
||||
ID: id,
|
||||
URL: srcURL,
|
||||
ContentType: opts.ContentType,
|
||||
size: opts.Size,
|
||||
rangeEnabled: opts.Range,
|
||||
session: opts.Session,
|
||||
open: open,
|
||||
}
|
||||
r.mu.Lock()
|
||||
r.sources[id] = src
|
||||
r.mu.Unlock()
|
||||
r.serverMu.Unlock()
|
||||
if opts.Session != nil && unclaimedSourceTTL > 0 {
|
||||
src.mu.Lock()
|
||||
if !src.revoked {
|
||||
src.unclaimed = time.AfterFunc(unclaimedSourceTTL, func() {
|
||||
r.expireUnclaimed(src)
|
||||
})
|
||||
}
|
||||
src.mu.Unlock()
|
||||
}
|
||||
return src.URL, nil
|
||||
}
|
||||
|
||||
func (r *Registry) Metadata(raw string) (Metadata, error) {
|
||||
src, err := r.get(raw)
|
||||
if err != nil {
|
||||
return Metadata{}, err
|
||||
}
|
||||
src.mu.Lock()
|
||||
defer src.mu.Unlock()
|
||||
return Metadata{
|
||||
ContentType: src.ContentType,
|
||||
Size: src.size,
|
||||
Range: src.rangeEnabled,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Acquire retains a source for a task. A source remains available until every
|
||||
// task that acquired it calls Release, unless it is explicitly revoked.
|
||||
func (r *Registry) Acquire(raw string) error {
|
||||
src, err := r.get(raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return src.acquireTask()
|
||||
}
|
||||
|
||||
// Release releases a task's reference to a source. Releasing the final task
|
||||
// reference revokes the source. Sources that have never been acquired remain
|
||||
// registered until Revoke or Registry.Close is called; session-backed sources
|
||||
// are also bounded by the unclaimed-source TTL.
|
||||
func (r *Registry) Release(raw string) error {
|
||||
src, err := r.get(raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
session, last, err := src.releaseTask()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !last {
|
||||
return nil
|
||||
}
|
||||
r.deleteSource(src)
|
||||
if session != nil {
|
||||
session.Release()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SourceError returns the first error encountered while opening or reading a
|
||||
// source. Invalid and missing sources have no source error. HTTP response write
|
||||
// errors are deliberately not recorded here.
|
||||
func (r *Registry) SourceError(raw string) error {
|
||||
src, err := r.get(raw)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return src.sourceError()
|
||||
}
|
||||
|
||||
func (r *Registry) Revoke(raw string) error {
|
||||
src, err := r.get(raw)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.removeSource(src)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry) Close() error {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
r.serverMu.Lock()
|
||||
server := r.server
|
||||
listener := r.listener
|
||||
r.server = nil
|
||||
r.listener = nil
|
||||
r.baseURL = ""
|
||||
r.mu.Lock()
|
||||
sources := make([]*Source, 0, len(r.sources))
|
||||
for _, src := range r.sources {
|
||||
sources = append(sources, src)
|
||||
}
|
||||
r.sources = make(map[string]*Source)
|
||||
r.mu.Unlock()
|
||||
r.serverMu.Unlock()
|
||||
if server != nil {
|
||||
_ = server.Close()
|
||||
}
|
||||
if listener != nil {
|
||||
_ = listener.Close()
|
||||
}
|
||||
for _, src := range sources {
|
||||
src.close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ensureServerLocked returns the current server URL or starts a new server.
|
||||
// The caller must hold serverMu.
|
||||
func (r *Registry) ensureServerLocked() (string, error) {
|
||||
if r.baseURL != "" {
|
||||
return r.baseURL, nil
|
||||
}
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
r.listener = listener
|
||||
r.baseURL = "http://" + listener.Addr().String() + urlPathPrefix
|
||||
server := &http.Server{Handler: r}
|
||||
r.server = server
|
||||
go func() {
|
||||
if err := server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
_ = r.Close()
|
||||
}
|
||||
}()
|
||||
return r.baseURL, nil
|
||||
}
|
||||
|
||||
func (r *Registry) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
if req.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", "GET")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
id := parseRequest(req)
|
||||
if id == "" {
|
||||
http.NotFound(w, req)
|
||||
return
|
||||
}
|
||||
src, err := r.getByID(id)
|
||||
if err != nil {
|
||||
http.NotFound(w, req)
|
||||
return
|
||||
}
|
||||
meta, open, session := src.acquireOpen()
|
||||
if open == nil {
|
||||
http.NotFound(w, req)
|
||||
return
|
||||
}
|
||||
if session != nil {
|
||||
defer session.Release()
|
||||
}
|
||||
if src.sourceError() != nil {
|
||||
http.Error(w, "blob source unavailable", http.StatusGone)
|
||||
return
|
||||
}
|
||||
start, end, ranged, err := parseRange(req.Header.Get("Range"), meta.Size, meta.Range)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusRequestedRangeNotSatisfiable)
|
||||
return
|
||||
}
|
||||
if !meta.Range {
|
||||
start, end, ranged = 0, -1, false
|
||||
}
|
||||
reader, err := open(req.Context(), OpenRequest{
|
||||
Offset: start,
|
||||
End: end,
|
||||
})
|
||||
if err != nil {
|
||||
if req.Context().Err() == nil || !errors.Is(err, req.Context().Err()) {
|
||||
src.recordSourceError(err, meta.Range)
|
||||
}
|
||||
http.Error(w, "blob source unavailable", http.StatusGone)
|
||||
return
|
||||
}
|
||||
if reader == nil {
|
||||
err = fmt.Errorf("%w: opener returned a nil reader", ErrSourceClosed)
|
||||
src.recordSourceError(err, meta.Range)
|
||||
http.Error(w, "blob source unavailable", http.StatusGone)
|
||||
return
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
writeHeaders(w, meta, start, end, ranged)
|
||||
if ranged {
|
||||
w.WriteHeader(http.StatusPartialContent)
|
||||
}
|
||||
limit := int64(-1)
|
||||
if end >= start {
|
||||
limit = end - start + 1
|
||||
} else if meta.Size > 0 {
|
||||
limit = meta.Size
|
||||
}
|
||||
_, readErr, writeErr := copyWithFlush(w, reader, limit)
|
||||
if readErr != nil {
|
||||
if ctxErr := req.Context().Err(); ctxErr == nil || !errors.Is(readErr, ctxErr) {
|
||||
src.recordSourceError(readErr, meta.Range)
|
||||
}
|
||||
} else if writeErr == nil && req.Context().Err() == nil {
|
||||
src.recordSourceSuccess(meta.Range)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Source) acquireOpen() (Metadata, OpenFunc, SessionRef) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.revoked {
|
||||
return Metadata{}, nil, nil
|
||||
}
|
||||
if s.session != nil {
|
||||
// The source's own retain guarantees that the session cannot become
|
||||
// closed while this additional active-reader retain is acquired.
|
||||
s.session.Retain()
|
||||
}
|
||||
return Metadata{
|
||||
ContentType: s.ContentType,
|
||||
Size: s.size,
|
||||
Range: s.rangeEnabled,
|
||||
}, s.open, s.session
|
||||
}
|
||||
|
||||
func (s *Source) acquireTask() error {
|
||||
s.mu.Lock()
|
||||
if s.revoked {
|
||||
s.mu.Unlock()
|
||||
return ErrSourceRevoked
|
||||
}
|
||||
s.taskRefs++
|
||||
timer := s.unclaimed
|
||||
s.unclaimed = nil
|
||||
s.mu.Unlock()
|
||||
if timer != nil {
|
||||
timer.Stop()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Source) releaseTask() (session SessionRef, last bool, err error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.revoked {
|
||||
return nil, false, ErrSourceRevoked
|
||||
}
|
||||
if s.taskRefs == 0 {
|
||||
return nil, false, ErrSourceClosed
|
||||
}
|
||||
s.taskRefs--
|
||||
if s.taskRefs > 0 {
|
||||
return nil, false, nil
|
||||
}
|
||||
s.revoked = true
|
||||
s.open = nil
|
||||
session = s.session
|
||||
s.session = nil
|
||||
return session, true, nil
|
||||
}
|
||||
|
||||
func (s *Source) sourceError() error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.readErr
|
||||
}
|
||||
|
||||
func (s *Source) recordSourceError(err error, ranged bool) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
s.mu.Lock()
|
||||
if s.readErr == nil && ranged {
|
||||
s.rangeFailures++
|
||||
if s.rangeFailures < rangeSourceFailureLimit {
|
||||
s.mu.Unlock()
|
||||
return
|
||||
}
|
||||
}
|
||||
if s.readErr == nil {
|
||||
s.readErr = err
|
||||
}
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Source) recordSourceSuccess(ranged bool) {
|
||||
if !ranged {
|
||||
return
|
||||
}
|
||||
s.mu.Lock()
|
||||
if s.readErr == nil {
|
||||
s.rangeFailures = 0
|
||||
}
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Source) close() {
|
||||
s.mu.Lock()
|
||||
alreadyRevoked := s.revoked
|
||||
s.revoked = true
|
||||
s.open = nil
|
||||
session := s.session
|
||||
s.session = nil
|
||||
timer := s.unclaimed
|
||||
s.unclaimed = nil
|
||||
s.mu.Unlock()
|
||||
if timer != nil {
|
||||
timer.Stop()
|
||||
}
|
||||
if !alreadyRevoked && session != nil {
|
||||
session.Release()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Source) expireUnclaimed() (SessionRef, bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.unclaimed = nil
|
||||
if s.revoked || s.taskRefs > 0 {
|
||||
return nil, false
|
||||
}
|
||||
s.revoked = true
|
||||
s.open = nil
|
||||
session := s.session
|
||||
s.session = nil
|
||||
return session, true
|
||||
}
|
||||
|
||||
func (r *Registry) expireUnclaimed(src *Source) {
|
||||
session, expired := src.expireUnclaimed()
|
||||
if !expired {
|
||||
return
|
||||
}
|
||||
r.deleteSource(src)
|
||||
if session != nil {
|
||||
session.Release()
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Registry) removeSource(src *Source) {
|
||||
src.close()
|
||||
r.deleteSource(src)
|
||||
}
|
||||
|
||||
func (r *Registry) deleteSource(src *Source) {
|
||||
r.mu.Lock()
|
||||
if current := r.sources[src.ID]; current == src {
|
||||
delete(r.sources, src.ID)
|
||||
}
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
func writeHeaders(w http.ResponseWriter, meta Metadata, start, end int64, ranged bool) {
|
||||
if meta.ContentType != "" {
|
||||
w.Header().Set("Content-Type", meta.ContentType)
|
||||
}
|
||||
if meta.Range {
|
||||
w.Header().Set("Accept-Ranges", "bytes")
|
||||
}
|
||||
if meta.Size > 0 {
|
||||
if ranged {
|
||||
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, meta.Size))
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(end-start+1, 10))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(meta.Size, 10))
|
||||
}
|
||||
}
|
||||
|
||||
func copyWithFlush(w http.ResponseWriter, reader io.Reader, limit int64) (written int64, readErr error, writeErr error) {
|
||||
if limit >= 0 {
|
||||
reader = io.LimitReader(reader, limit)
|
||||
}
|
||||
buf := make([]byte, 32*1024)
|
||||
flusher, canFlush := w.(http.Flusher)
|
||||
for {
|
||||
nr, er := reader.Read(buf)
|
||||
if er != nil && !errors.Is(er, io.EOF) {
|
||||
readErr = er
|
||||
}
|
||||
if nr > 0 {
|
||||
nw, ew := w.Write(buf[:nr])
|
||||
if nw > 0 {
|
||||
written += int64(nw)
|
||||
if canFlush {
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
if ew != nil {
|
||||
return written, readErr, ew
|
||||
}
|
||||
if nr != nw {
|
||||
return written, readErr, io.ErrShortWrite
|
||||
}
|
||||
}
|
||||
if er != nil {
|
||||
if errors.Is(er, io.EOF) {
|
||||
if limit >= 0 && written < limit {
|
||||
return written, io.ErrUnexpectedEOF, nil
|
||||
}
|
||||
return written, nil, nil
|
||||
}
|
||||
return written, readErr, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func parseRange(header string, size int64, rangeEnabled bool) (start int64, end int64, ranged bool, err error) {
|
||||
if header == "" || !rangeEnabled {
|
||||
return 0, -1, false, nil
|
||||
}
|
||||
if size <= 0 {
|
||||
return 0, 0, false, ErrRangeNotAllowed
|
||||
}
|
||||
if !strings.HasPrefix(header, "bytes=") {
|
||||
return 0, 0, false, fmt.Errorf("unsupported range")
|
||||
}
|
||||
parts := strings.SplitN(strings.TrimPrefix(header, "bytes="), "-", 2)
|
||||
if len(parts) != 2 || parts[0] == "" {
|
||||
return 0, 0, false, fmt.Errorf("unsupported range")
|
||||
}
|
||||
start, err = strconv.ParseInt(parts[0], 10, 64)
|
||||
if err != nil || start < 0 {
|
||||
return 0, 0, false, fmt.Errorf("invalid range")
|
||||
}
|
||||
if start >= size {
|
||||
return 0, 0, false, fmt.Errorf("range out of bounds")
|
||||
}
|
||||
end = size - 1
|
||||
if parts[1] != "" {
|
||||
end, err = strconv.ParseInt(parts[1], 10, 64)
|
||||
if err != nil || end < start {
|
||||
return 0, 0, false, fmt.Errorf("invalid range")
|
||||
}
|
||||
if end >= size {
|
||||
end = size - 1
|
||||
}
|
||||
}
|
||||
return start, end, true, nil
|
||||
}
|
||||
|
||||
func (r *Registry) get(raw string) (*Source, error) {
|
||||
id, ok := r.parseURL(raw)
|
||||
if !ok {
|
||||
return nil, ErrInvalidURL
|
||||
}
|
||||
src, err := r.getByID(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
src.mu.Lock()
|
||||
revoked := src.revoked
|
||||
src.mu.Unlock()
|
||||
if revoked {
|
||||
return nil, ErrSourceRevoked
|
||||
}
|
||||
return src, nil
|
||||
}
|
||||
|
||||
func (r *Registry) getByID(id string) (*Source, error) {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
src := r.sources[id]
|
||||
if src == nil {
|
||||
return nil, ErrSourceNotFound
|
||||
}
|
||||
return src, nil
|
||||
}
|
||||
|
||||
func (r *Registry) parseURL(raw string) (id string, ok bool) {
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil || u.Scheme != "http" || !strings.HasPrefix(u.Path, urlPathPrefix) {
|
||||
return "", false
|
||||
}
|
||||
r.serverMu.Lock()
|
||||
baseURL := r.baseURL
|
||||
r.serverMu.Unlock()
|
||||
if baseURL == "" || !strings.HasPrefix(raw, baseURL) {
|
||||
return "", false
|
||||
}
|
||||
id = strings.TrimPrefix(u.Path, urlPathPrefix)
|
||||
if id == "" || path.Base(id) != id {
|
||||
return "", false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
func parseRequest(req *http.Request) string {
|
||||
id := strings.TrimPrefix(req.URL.Path, urlPathPrefix)
|
||||
if id == "" || id == req.URL.Path || path.Base(id) != id {
|
||||
return ""
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func randomID(size int) (string, error) {
|
||||
buf := make([]byte, size)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(buf), nil
|
||||
}
|
||||
@@ -0,0 +1,898 @@
|
||||
package blob
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type terminalErrorReader struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (r *terminalErrorReader) Read([]byte) (int, error) {
|
||||
return 0, r.err
|
||||
}
|
||||
|
||||
type contextErrorReader struct {
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (r *contextErrorReader) Read([]byte) (int, error) {
|
||||
<-r.ctx.Done()
|
||||
return 0, r.ctx.Err()
|
||||
}
|
||||
|
||||
func (r *contextErrorReader) Close() error { return nil }
|
||||
|
||||
type failingResponseWriter struct {
|
||||
header http.Header
|
||||
err error
|
||||
}
|
||||
|
||||
func (w *failingResponseWriter) Header() http.Header {
|
||||
return w.header
|
||||
}
|
||||
|
||||
func (w *failingResponseWriter) Write([]byte) (int, error) {
|
||||
return 0, w.err
|
||||
}
|
||||
|
||||
func (w *failingResponseWriter) WriteHeader(int) {}
|
||||
|
||||
type countingSession struct {
|
||||
mu sync.Mutex
|
||||
refs int
|
||||
zero chan struct{}
|
||||
zeroOnce sync.Once
|
||||
}
|
||||
|
||||
func newCountingSession() *countingSession {
|
||||
return &countingSession{zero: make(chan struct{})}
|
||||
}
|
||||
|
||||
func (s *countingSession) Retain() {
|
||||
s.mu.Lock()
|
||||
s.refs++
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *countingSession) Release() {
|
||||
s.mu.Lock()
|
||||
s.refs--
|
||||
refs := s.refs
|
||||
s.mu.Unlock()
|
||||
if refs == 0 {
|
||||
s.zeroOnce.Do(func() { close(s.zero) })
|
||||
}
|
||||
}
|
||||
|
||||
func (s *countingSession) RefCount() int {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.refs
|
||||
}
|
||||
|
||||
func setUnclaimedSourceTTLForTest(t *testing.T, ttl time.Duration) {
|
||||
t.Helper()
|
||||
previous := unclaimedSourceTTL
|
||||
unclaimedSourceTTL = ttl
|
||||
t.Cleanup(func() {
|
||||
unclaimedSourceTTL = previous
|
||||
})
|
||||
}
|
||||
|
||||
func TestRegistryBlobHTTPGetAndRange(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
url, err := registry.CreateBlob([]byte("hello world"), "text/plain")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !registry.IsURL(url) {
|
||||
t.Fatal("expected registry to recognize created url")
|
||||
}
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("unexpected GET status: %d", resp.StatusCode)
|
||||
}
|
||||
if got := resp.Header.Get("Content-Length"); got != "11" {
|
||||
t.Fatalf("unexpected content length: %q", got)
|
||||
}
|
||||
if got := resp.Header.Get("Accept-Ranges"); got != "bytes" {
|
||||
t.Fatalf("unexpected accept ranges: %q", got)
|
||||
}
|
||||
if got := resp.Header.Get("Content-Type"); !strings.HasPrefix(got, "text/plain") {
|
||||
t.Fatalf("unexpected content type: %q", got)
|
||||
}
|
||||
if string(body) != "hello world" {
|
||||
t.Fatalf("unexpected GET body: %q", string(body))
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Range", "bytes=6-10")
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusPartialContent {
|
||||
t.Fatalf("unexpected range status: %d", resp.StatusCode)
|
||||
}
|
||||
if got := resp.Header.Get("Content-Range"); got != "bytes 6-10/11" {
|
||||
t.Fatalf("unexpected content range: %q", got)
|
||||
}
|
||||
if string(body) != "world" {
|
||||
t.Fatalf("unexpected range body: %q", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryEmptyBlob(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
url, err := registry.CreateBlob(nil, "application/octet-stream")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
meta, err := registry.Metadata(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if meta.Size != 0 || meta.Range {
|
||||
t.Fatalf("unexpected empty blob metadata: %#v", meta)
|
||||
}
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, readErr := io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if readErr != nil {
|
||||
t.Fatal(readErr)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("unexpected status: %d", resp.StatusCode)
|
||||
}
|
||||
if len(body) != 0 {
|
||||
t.Fatalf("unexpected body: %q", body)
|
||||
}
|
||||
if got := resp.Header.Get("Content-Length"); got != "0" {
|
||||
t.Fatalf("unexpected content length: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryOpenerSizeWithoutRange(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
payload := []byte("hello opener")
|
||||
url, err := registry.CreateOpener(func(ctx context.Context, req OpenRequest) (io.ReadCloser, error) {
|
||||
if req.Offset != 0 || req.End != -1 {
|
||||
t.Fatalf("unexpected non-range request: %#v", req)
|
||||
}
|
||||
return io.NopCloser(bytes.NewReader(payload)), nil
|
||||
}, &CreateOptions{
|
||||
ContentType: "text/plain",
|
||||
Size: int64(len(payload)),
|
||||
Range: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Range", "bytes=6-11")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("unexpected status: %d", resp.StatusCode)
|
||||
}
|
||||
if got := resp.Header.Get("Content-Length"); got != "12" {
|
||||
t.Fatalf("unexpected content length: %q", got)
|
||||
}
|
||||
if got := resp.Header.Get("Accept-Ranges"); got != "" {
|
||||
t.Fatalf("unexpected accept ranges: %q", got)
|
||||
}
|
||||
if string(body) != string(payload) {
|
||||
t.Fatalf("unexpected body: %q", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryOpenerRangeConcurrentRequests(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
payload := []byte("abcdefghijklmnopqrstuvwxyz")
|
||||
var mu sync.Mutex
|
||||
var calls []OpenRequest
|
||||
url, err := registry.CreateOpener(func(ctx context.Context, req OpenRequest) (io.ReadCloser, error) {
|
||||
mu.Lock()
|
||||
calls = append(calls, req)
|
||||
mu.Unlock()
|
||||
|
||||
start := int(req.Offset)
|
||||
end := len(payload)
|
||||
if req.End >= 0 {
|
||||
end = int(req.End) + 1
|
||||
}
|
||||
if start < 0 || start > end || end > len(payload) {
|
||||
return nil, errors.New("bad test range")
|
||||
}
|
||||
return io.NopCloser(bytes.NewReader(payload[start:end])), nil
|
||||
}, &CreateOptions{
|
||||
Size: int64(len(payload)),
|
||||
Range: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ranges := []struct {
|
||||
header string
|
||||
want string
|
||||
}{
|
||||
{header: "bytes=0-4", want: "abcde"},
|
||||
{header: "bytes=10-15", want: "klmnop"},
|
||||
{header: "bytes=20-25", want: "uvwxyz"},
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
for _, item := range ranges {
|
||||
item := item
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
req.Header.Set("Range", item.header)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if resp.StatusCode != http.StatusPartialContent {
|
||||
t.Errorf("unexpected status for %s: %d", item.header, resp.StatusCode)
|
||||
}
|
||||
if string(body) != item.want {
|
||||
t.Errorf("unexpected body for %s: %q", item.header, string(body))
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(calls) != len(ranges) {
|
||||
t.Fatalf("unexpected opener call count: %d", len(calls))
|
||||
}
|
||||
seen := map[OpenRequest]bool{}
|
||||
for _, call := range calls {
|
||||
seen[call] = true
|
||||
}
|
||||
for _, want := range []OpenRequest{{Offset: 0, End: 4}, {Offset: 10, End: 15}, {Offset: 20, End: 25}} {
|
||||
if !seen[want] {
|
||||
t.Fatalf("missing opener request %#v, got %#v", want, calls)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryRejectsRangeWithoutSize(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
_, err := registry.CreateOpener(func(ctx context.Context, req OpenRequest) (io.ReadCloser, error) {
|
||||
return io.NopCloser(strings.NewReader("")), nil
|
||||
}, &CreateOptions{Range: true})
|
||||
if !errors.Is(err, ErrInvalidOptions) {
|
||||
t.Fatalf("expected invalid options error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryTracksSourceErrors(t *testing.T) {
|
||||
t.Run("reader error", func(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
boom := errors.New("reader failed")
|
||||
url, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
return io.NopCloser(io.MultiReader(strings.NewReader("partial"), &terminalErrorReader{err: boom})), nil
|
||||
}, &CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, readErr := io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if readErr != nil {
|
||||
t.Fatalf("the HTTP transport should end normally, got %v", readErr)
|
||||
}
|
||||
if string(body) != "partial" {
|
||||
t.Fatalf("unexpected partial body: %q", body)
|
||||
}
|
||||
if got := registry.SourceError(url); !errors.Is(got, boom) {
|
||||
t.Fatalf("expected source error %v, got %v", boom, got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("short known-size reader", func(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
url, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
return io.NopCloser(strings.NewReader("short")), nil
|
||||
}, &CreateOptions{Size: 10})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _ = io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if got := registry.SourceError(url); !errors.Is(got, io.ErrUnexpectedEOF) {
|
||||
t.Fatalf("expected unexpected EOF, got %v", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("range reader error becomes terminal", func(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
boom := errors.New("range reader failed")
|
||||
url, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
return io.NopCloser(io.MultiReader(strings.NewReader("partial"), &terminalErrorReader{err: boom})), nil
|
||||
}, &CreateOptions{Size: 10, Range: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for attempt := 0; attempt < rangeSourceFailureLimit; attempt++ {
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Range", "bytes=0-9")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _ = io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
if got := registry.SourceError(url); !errors.Is(got, boom) {
|
||||
t.Fatalf("expected range source error %v, got %v", boom, got)
|
||||
}
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusGone {
|
||||
t.Fatalf("expected terminal source status %d, got %d", http.StatusGone, resp.StatusCode)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("short range reader", func(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
url, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
return io.NopCloser(strings.NewReader("short")), nil
|
||||
}, &CreateOptions{Size: 10, Range: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for attempt := 0; attempt < rangeSourceFailureLimit; attempt++ {
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Range", "bytes=0-9")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _ = io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
if got := registry.SourceError(url); !errors.Is(got, io.ErrUnexpectedEOF) {
|
||||
t.Fatalf("expected range unexpected EOF, got %v", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("successful range retry clears transient failure", func(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
attempt := 0
|
||||
boom := errors.New("transient range failure")
|
||||
url, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
attempt++
|
||||
if attempt == 1 {
|
||||
return io.NopCloser(&terminalErrorReader{err: boom}), nil
|
||||
}
|
||||
return io.NopCloser(strings.NewReader("0123456789")), nil
|
||||
}, &CreateOptions{Size: 10, Range: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Range", "bytes=0-9")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, _ = io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
if got := registry.SourceError(url); got != nil {
|
||||
t.Fatalf("successful retry left transient source error: %v", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("request cancellation is not a source error", func(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
opened := make(chan struct{})
|
||||
url, err := registry.CreateOpener(func(ctx context.Context, _ OpenRequest) (io.ReadCloser, error) {
|
||||
close(opened)
|
||||
return &contextErrorReader{ctx: ctx}, nil
|
||||
}, &CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
resp, _ := http.DefaultClient.Do(req)
|
||||
if resp != nil {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
close(done)
|
||||
}()
|
||||
<-opened
|
||||
cancel()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("cancelled request did not finish")
|
||||
}
|
||||
if got := registry.SourceError(url); got != nil {
|
||||
t.Fatalf("request cancellation was recorded as source error: %v", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("nil reader", func(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
url, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
return nil, nil
|
||||
}, &CreateOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusGone {
|
||||
t.Fatalf("expected nil reader status %d, got %d", http.StatusGone, resp.StatusCode)
|
||||
}
|
||||
if got := registry.SourceError(url); !errors.Is(got, ErrSourceClosed) {
|
||||
t.Fatalf("expected closed source error, got %v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestRegistryDoesNotRecordResponseWriteError(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
url, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
return io.NopCloser(strings.NewReader("payload")), nil
|
||||
}, &CreateOptions{Size: 7})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writerErr := errors.New("client connection closed")
|
||||
registry.ServeHTTP(&failingResponseWriter{header: make(http.Header), err: writerErr}, req)
|
||||
if got := registry.SourceError(url); got != nil {
|
||||
t.Fatalf("response write error was recorded as a source error: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryTaskReferencesShareSource(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
url, err := registry.CreateBlob([]byte("shared"), "text/plain")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := registry.Acquire(url); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := registry.Acquire(url); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := registry.Release(url); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !registry.IsURL(url) {
|
||||
t.Fatal("the first task release revoked a source still used by another task")
|
||||
}
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, readErr := io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
if readErr != nil || string(body) != "shared" {
|
||||
t.Fatalf("unexpected shared response body %q, error %v", body, readErr)
|
||||
}
|
||||
|
||||
if err := registry.Release(url); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if registry.IsURL(url) {
|
||||
t.Fatal("the final task release did not revoke the source")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryUnclaimedSessionSourceExpires(t *testing.T) {
|
||||
setUnclaimedSourceTTLForTest(t, 25*time.Millisecond)
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
session := newCountingSession()
|
||||
url, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
return io.NopCloser(strings.NewReader("unused")), nil
|
||||
}, &CreateOptions{Size: 6, Session: session})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := session.RefCount(); got != 1 {
|
||||
t.Fatalf("unexpected initial session ref count: %d", got)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-session.zero:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("unclaimed source did not expire")
|
||||
}
|
||||
if registry.IsURL(url) {
|
||||
t.Fatal("expired source is still registered")
|
||||
}
|
||||
if got := session.RefCount(); got != 0 {
|
||||
t.Fatalf("expired source retained its session: %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryAcquireStopsUnclaimedSourceExpiration(t *testing.T) {
|
||||
const ttl = 25 * time.Millisecond
|
||||
setUnclaimedSourceTTLForTest(t, ttl)
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
session := newCountingSession()
|
||||
url, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
return io.NopCloser(strings.NewReader("claimed")), nil
|
||||
}, &CreateOptions{Size: 7, Session: session})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := registry.Acquire(url); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
select {
|
||||
case <-session.zero:
|
||||
t.Fatal("claimed source expired")
|
||||
case <-time.After(4 * ttl):
|
||||
}
|
||||
if !registry.IsURL(url) {
|
||||
t.Fatal("claimed source was removed")
|
||||
}
|
||||
if err := registry.Release(url); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-session.zero:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("final task release did not release the session")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryUnclaimedExpirationKeepsActiveReaderSession(t *testing.T) {
|
||||
setUnclaimedSourceTTLForTest(t, 100*time.Millisecond)
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
session := newCountingSession()
|
||||
reader, writer := io.Pipe()
|
||||
opened := make(chan struct{})
|
||||
url, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
close(opened)
|
||||
return reader, nil
|
||||
}, &CreateOptions{Size: 7, Session: session})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
type result struct {
|
||||
body []byte
|
||||
err error
|
||||
}
|
||||
resultCh := make(chan result, 1)
|
||||
go func() {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
resultCh <- result{err: err}
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
resultCh <- result{body: body, err: err}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-opened:
|
||||
case got := <-resultCh:
|
||||
t.Fatalf("request ended before the opener ran: body %q, error %v", got.body, got.err)
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("active request did not open")
|
||||
}
|
||||
deadline := time.NewTimer(2 * time.Second)
|
||||
ticker := time.NewTicker(5 * time.Millisecond)
|
||||
defer deadline.Stop()
|
||||
defer ticker.Stop()
|
||||
for registry.IsURL(url) {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
case <-deadline.C:
|
||||
t.Fatal("unclaimed source did not expire while its reader was active")
|
||||
}
|
||||
}
|
||||
if got := session.RefCount(); got != 1 {
|
||||
t.Fatalf("expiration released the active-reader session ref: %d", got)
|
||||
}
|
||||
if _, err := writer.Write([]byte("payload")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = writer.Close()
|
||||
|
||||
select {
|
||||
case got := <-resultCh:
|
||||
if got.err != nil || string(got.body) != "payload" {
|
||||
t.Fatalf("active response was interrupted: body %q, error %v", got.body, got.err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("active response did not finish after source expiration")
|
||||
}
|
||||
select {
|
||||
case <-session.zero:
|
||||
if got := session.RefCount(); got != 0 {
|
||||
t.Fatalf("unexpected final session ref count: %d", got)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("active-reader session ref was not released")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryCloseCancelsUnclaimedExpiration(t *testing.T) {
|
||||
const ttl = 25 * time.Millisecond
|
||||
setUnclaimedSourceTTLForTest(t, ttl)
|
||||
registry := NewRegistry(t.TempDir())
|
||||
|
||||
session := newCountingSession()
|
||||
_, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
return io.NopCloser(strings.NewReader("unused")), nil
|
||||
}, &CreateOptions{Size: 6, Session: session})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := registry.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-session.zero:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("registry close did not release the session")
|
||||
}
|
||||
<-time.After(4 * ttl)
|
||||
if got := session.RefCount(); got != 0 {
|
||||
t.Fatalf("expiration released the session again after close: %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryRevokeKeepsActiveReaderSession(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
session := newCountingSession()
|
||||
reader, writer := io.Pipe()
|
||||
opened := make(chan struct{})
|
||||
url, err := registry.CreateOpener(func(context.Context, OpenRequest) (io.ReadCloser, error) {
|
||||
close(opened)
|
||||
return reader, nil
|
||||
}, &CreateOptions{Size: 7, Session: session})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
type result struct {
|
||||
body []byte
|
||||
err error
|
||||
}
|
||||
resultCh := make(chan result, 1)
|
||||
go func() {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
resultCh <- result{err: err}
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
_ = resp.Body.Close()
|
||||
resultCh <- result{body: body, err: err}
|
||||
}()
|
||||
|
||||
<-opened
|
||||
if got := session.RefCount(); got != 2 {
|
||||
t.Fatalf("expected source and active-reader session refs, got %d", got)
|
||||
}
|
||||
if err := registry.Revoke(url); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := session.RefCount(); got != 1 {
|
||||
t.Fatalf("revoke released the active-reader session ref: %d", got)
|
||||
}
|
||||
if _, err := writer.Write([]byte("payload")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = writer.Close()
|
||||
|
||||
select {
|
||||
case got := <-resultCh:
|
||||
if got.err != nil || string(got.body) != "payload" {
|
||||
t.Fatalf("active response was interrupted: body %q, error %v", got.body, got.err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("active response did not finish after revoke")
|
||||
}
|
||||
select {
|
||||
case <-session.zero:
|
||||
if got := session.RefCount(); got != 0 {
|
||||
t.Fatalf("unexpected final session ref count: %d", got)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("active-reader session ref was not released")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistryHTTPErrorStatuses(t *testing.T) {
|
||||
registry := NewRegistry(t.TempDir())
|
||||
defer registry.Close()
|
||||
|
||||
url, err := registry.CreateBlob([]byte("secret"), "text/plain")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
badURL := url + "-missing"
|
||||
resp, err := http.Get(badURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("unexpected missing source status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodHead, url, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusMethodNotAllowed {
|
||||
t.Fatalf("unexpected method status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
req, err = http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Range", "bytes=99-100")
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusRequestedRangeNotSatisfiable {
|
||||
t.Fatalf("unexpected invalid range status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
if err := registry.Revoke(url); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err = http.Get(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusNotFound {
|
||||
t.Fatalf("unexpected revoked source status: %d", resp.StatusCode)
|
||||
}
|
||||
if registry.IsURL(url) {
|
||||
t.Fatal("revoked source should not be usable")
|
||||
}
|
||||
}
|
||||
@@ -21,8 +21,9 @@ type Fetcher interface {
|
||||
Pause() error
|
||||
Close() error
|
||||
|
||||
// Stats refreshes health statistics and returns the latest information
|
||||
Stats() any
|
||||
// Stats returns both the durable protocol snapshot and transient runtime
|
||||
// telemetry. Downloader persists Snapshot and keeps Runtime in memory only.
|
||||
Stats() *Stats
|
||||
// Meta returns the meta information of the download.
|
||||
Meta() *FetcherMeta
|
||||
// Progress returns the progress of the download.
|
||||
@@ -31,6 +32,14 @@ type Fetcher interface {
|
||||
Wait() error
|
||||
}
|
||||
|
||||
// Stats separates protocol information that remains meaningful after a task
|
||||
// stops from telemetry that is valid only while the fetcher is active.
|
||||
// Protocols own both payload schemas; Downloader treats them as opaque values.
|
||||
type Stats struct {
|
||||
Snapshot any `json:"snapshot"`
|
||||
Runtime any `json:"runtime"`
|
||||
}
|
||||
|
||||
type Uploader interface {
|
||||
Upload() error
|
||||
UploadedBytes() int64
|
||||
@@ -127,6 +136,21 @@ type FetcherManager interface {
|
||||
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 {
|
||||
Ctl *controller.Controller
|
||||
Meta *FetcherMeta
|
||||
|
||||
@@ -1,6 +1,23 @@
|
||||
package fetcher
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStatsMarshalRawSnapshotWithoutRuntime(t *testing.T) {
|
||||
stats := &Stats{
|
||||
Snapshot: json.RawMessage(`{"value":1}`),
|
||||
}
|
||||
|
||||
data, err := json.Marshal(stats)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal() error = %v", err)
|
||||
}
|
||||
if got, want := string(data), `{"snapshot":{"value":1},"runtime":null}`; got != want {
|
||||
t.Fatalf("Marshal() = %s, want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemeFilter_Match(t *testing.T) {
|
||||
type fields struct {
|
||||
|
||||
@@ -0,0 +1,346 @@
|
||||
package httpclient
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/imroc/req/v3"
|
||||
)
|
||||
|
||||
type ImpersonationMode string
|
||||
|
||||
const (
|
||||
ImpersonationNative ImpersonationMode = "native"
|
||||
ImpersonationFixed ImpersonationMode = "fixed"
|
||||
ImpersonationAuto ImpersonationMode = "auto"
|
||||
)
|
||||
|
||||
type Browser string
|
||||
|
||||
const (
|
||||
BrowserChrome Browser = "chrome"
|
||||
BrowserFirefox Browser = "firefox"
|
||||
BrowserSafari Browser = "safari"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Client ClientOptions
|
||||
Transport TransportOptions
|
||||
Impersonation ImpersonationOptions
|
||||
}
|
||||
|
||||
type ClientOptions struct {
|
||||
Jar http.CookieJar
|
||||
CheckRedirect func(req *http.Request, via []*http.Request) error
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
type TransportOptions struct {
|
||||
Proxy func(*http.Request) (*url.URL, error)
|
||||
DialContext func(ctx context.Context, network, address string) (net.Conn, error)
|
||||
TLSClientConfig *tls.Config
|
||||
TLSHandshakeTimeout time.Duration
|
||||
}
|
||||
|
||||
type ImpersonationOptions struct {
|
||||
Mode ImpersonationMode
|
||||
Browser Browser
|
||||
Session *ImpersonationSession
|
||||
}
|
||||
|
||||
type ImpersonationSession struct {
|
||||
mu sync.RWMutex
|
||||
selections map[string]impersonationSelection
|
||||
}
|
||||
|
||||
type impersonationSelection struct {
|
||||
browser Browser
|
||||
fallbackDisabled bool
|
||||
}
|
||||
|
||||
func NewImpersonationSession() *ImpersonationSession {
|
||||
return &ImpersonationSession{
|
||||
selections: make(map[string]impersonationSelection),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ImpersonationSession) load(url string) (impersonationSelection, bool) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
selection, ok := s.selections[url]
|
||||
return selection, ok
|
||||
}
|
||||
|
||||
func (s *ImpersonationSession) store(url string, browser Browser) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.selections[url] = impersonationSelection{browser: browser}
|
||||
}
|
||||
|
||||
func (s *ImpersonationSession) disableFallback(url string) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.selections[url] = impersonationSelection{fallbackDisabled: true}
|
||||
}
|
||||
|
||||
func (s *ImpersonationSession) Clear() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
clear(s.selections)
|
||||
}
|
||||
|
||||
func NewClient(options Options) (*http.Client, error) {
|
||||
var transport http.RoundTripper
|
||||
switch options.Impersonation.Mode {
|
||||
case "", ImpersonationNative:
|
||||
transport = newNativeTransport(options.Transport)
|
||||
case ImpersonationFixed:
|
||||
browserTransport, err := newBrowserTransport(options.Transport, options.Impersonation.Browser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
transport = browserTransport
|
||||
case ImpersonationAuto:
|
||||
browserTransports := make(map[Browser]http.RoundTripper, 3)
|
||||
for _, browser := range []Browser{BrowserChrome, BrowserFirefox, BrowserSafari} {
|
||||
browserTransport, err := newBrowserTransport(options.Transport, browser)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
browserTransports[browser] = browserTransport
|
||||
}
|
||||
transport = &adaptiveTransport{
|
||||
native: newNativeTransport(options.Transport),
|
||||
browsers: browserTransports,
|
||||
jar: options.Client.Jar,
|
||||
session: options.Impersonation.Session,
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported impersonation mode %q", options.Impersonation.Mode)
|
||||
}
|
||||
|
||||
return &http.Client{
|
||||
Transport: transport,
|
||||
Jar: options.Client.Jar,
|
||||
CheckRedirect: options.Client.CheckRedirect,
|
||||
Timeout: options.Client.Timeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func newNativeTransport(options TransportOptions) *http.Transport {
|
||||
var tlsConfig *tls.Config
|
||||
if options.TLSClientConfig != nil {
|
||||
tlsConfig = options.TLSClientConfig.Clone()
|
||||
}
|
||||
return &http.Transport{
|
||||
Proxy: options.Proxy,
|
||||
DialContext: options.DialContext,
|
||||
TLSClientConfig: tlsConfig,
|
||||
TLSHandshakeTimeout: options.TLSHandshakeTimeout,
|
||||
ForceAttemptHTTP2: true,
|
||||
}
|
||||
}
|
||||
|
||||
func newBrowserTransport(options TransportOptions, browser Browser) (http.RoundTripper, error) {
|
||||
client := req.NewClient()
|
||||
client.SetProxy(options.Proxy)
|
||||
if options.DialContext != nil {
|
||||
client.SetDial(options.DialContext)
|
||||
}
|
||||
client.SetTLSHandshakeTimeout(options.TLSHandshakeTimeout)
|
||||
if options.TLSClientConfig != nil {
|
||||
tlsConfig := options.TLSClientConfig.Clone()
|
||||
if len(tlsConfig.NextProtos) == 0 {
|
||||
tlsConfig.NextProtos = []string{"h2", "http/1.1"}
|
||||
}
|
||||
client.SetTLSClientConfig(tlsConfig)
|
||||
}
|
||||
|
||||
switch browser {
|
||||
case BrowserChrome:
|
||||
client.ImpersonateChrome()
|
||||
case BrowserFirefox:
|
||||
client.ImpersonateFirefox()
|
||||
case BrowserSafari:
|
||||
client.ImpersonateSafari()
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported browser %q", browser)
|
||||
}
|
||||
|
||||
return &defaultHeaderTransport{
|
||||
next: client.GetClient().Transport,
|
||||
defaults: client.Headers.Clone(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type defaultHeaderTransport struct {
|
||||
next http.RoundTripper
|
||||
defaults http.Header
|
||||
}
|
||||
|
||||
func (t *defaultHeaderTransport) RoundTrip(request *http.Request) (*http.Response, error) {
|
||||
request = request.Clone(request.Context())
|
||||
request.Header = request.Header.Clone()
|
||||
for key, values := range t.defaults {
|
||||
if len(request.Header.Values(key)) == 0 {
|
||||
request.Header[key] = append([]string(nil), values...)
|
||||
}
|
||||
}
|
||||
return t.next.RoundTrip(request)
|
||||
}
|
||||
|
||||
func (t *defaultHeaderTransport) CloseIdleConnections() {
|
||||
closeIdleConnections(t.next)
|
||||
}
|
||||
|
||||
type adaptiveTransport struct {
|
||||
native http.RoundTripper
|
||||
browsers map[Browser]http.RoundTripper
|
||||
jar http.CookieJar
|
||||
session *ImpersonationSession
|
||||
}
|
||||
|
||||
func (t *adaptiveTransport) RoundTrip(request *http.Request) (*http.Response, error) {
|
||||
cacheKey := requestCacheKey(request.URL)
|
||||
if t.session != nil {
|
||||
if selected, ok := t.session.load(cacheKey); ok {
|
||||
if selected.fallbackDisabled {
|
||||
return t.native.RoundTrip(request)
|
||||
}
|
||||
return t.browsers[selected.browser].RoundTrip(request)
|
||||
}
|
||||
}
|
||||
|
||||
response, err := t.native.RoundTrip(request)
|
||||
if !shouldFallback(response, err) {
|
||||
return response, err
|
||||
}
|
||||
|
||||
retry, ok := cloneRequestForRetry(request)
|
||||
if !ok {
|
||||
return response, err
|
||||
}
|
||||
if response != nil {
|
||||
applyResponseCookies(t.jar, request, retry, response)
|
||||
_ = response.Body.Close()
|
||||
}
|
||||
|
||||
browser := browserFromUserAgent(request.UserAgent())
|
||||
response, err = t.browsers[browser].RoundTrip(retry)
|
||||
if t.session != nil {
|
||||
if isSuccessfulFallback(response, err) {
|
||||
t.session.store(cacheKey, browser)
|
||||
} else if err == nil && response != nil {
|
||||
t.session.disableFallback(cacheKey)
|
||||
}
|
||||
}
|
||||
return response, err
|
||||
}
|
||||
|
||||
func requestCacheKey(requestURL *url.URL) string {
|
||||
cacheURL := *requestURL
|
||||
cacheURL.Fragment = ""
|
||||
cacheURL.RawFragment = ""
|
||||
return cacheURL.String()
|
||||
}
|
||||
|
||||
func applyResponseCookies(jar http.CookieJar, request, retry *http.Request, response *http.Response) {
|
||||
if jar == nil {
|
||||
return
|
||||
}
|
||||
jar.SetCookies(request.URL, response.Cookies())
|
||||
jarCookies := jar.Cookies(request.URL)
|
||||
jarCookieNames := make(map[string]struct{}, len(jarCookies))
|
||||
for _, cookie := range jarCookies {
|
||||
jarCookieNames[cookie.Name] = struct{}{}
|
||||
}
|
||||
existingCookies := retry.Cookies()
|
||||
retry.Header.Del("Cookie")
|
||||
for _, cookie := range existingCookies {
|
||||
if _, replaced := jarCookieNames[cookie.Name]; !replaced {
|
||||
retry.AddCookie(cookie)
|
||||
}
|
||||
}
|
||||
for _, cookie := range jarCookies {
|
||||
retry.AddCookie(cookie)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *adaptiveTransport) CloseIdleConnections() {
|
||||
closeIdleConnections(t.native)
|
||||
for _, transport := range t.browsers {
|
||||
closeIdleConnections(transport)
|
||||
}
|
||||
}
|
||||
|
||||
func closeIdleConnections(transport http.RoundTripper) {
|
||||
if closer, ok := transport.(interface{ CloseIdleConnections() }); ok {
|
||||
closer.CloseIdleConnections()
|
||||
}
|
||||
}
|
||||
|
||||
func shouldFallback(response *http.Response, err error) bool {
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
if response == nil {
|
||||
return false
|
||||
}
|
||||
return response.StatusCode == http.StatusForbidden ||
|
||||
strings.EqualFold(response.Header.Get("Cf-Mitigated"), "challenge")
|
||||
}
|
||||
|
||||
func isSuccessfulFallback(response *http.Response, err error) bool {
|
||||
return err == nil && response != nil &&
|
||||
response.StatusCode >= http.StatusOK &&
|
||||
response.StatusCode < http.StatusBadRequest
|
||||
}
|
||||
|
||||
func cloneRequestForRetry(request *http.Request) (*http.Request, bool) {
|
||||
if !isIdempotent(request) {
|
||||
return nil, false
|
||||
}
|
||||
retry := request.Clone(request.Context())
|
||||
if request.Body == nil || request.Body == http.NoBody {
|
||||
return retry, true
|
||||
}
|
||||
if request.GetBody == nil {
|
||||
return nil, false
|
||||
}
|
||||
body, err := request.GetBody()
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
retry.Body = body
|
||||
return retry, true
|
||||
}
|
||||
|
||||
func isIdempotent(request *http.Request) bool {
|
||||
switch request.Method {
|
||||
case http.MethodGet, http.MethodHead, http.MethodOptions, http.MethodTrace:
|
||||
return true
|
||||
}
|
||||
return request.Header.Get("Idempotency-Key") != "" ||
|
||||
request.Header.Get("X-Idempotency-Key") != ""
|
||||
}
|
||||
|
||||
func browserFromUserAgent(userAgent string) Browser {
|
||||
userAgent = strings.ToLower(userAgent)
|
||||
switch {
|
||||
case strings.Contains(userAgent, "firefox"):
|
||||
return BrowserFirefox
|
||||
case strings.Contains(userAgent, "safari") &&
|
||||
!strings.Contains(userAgent, "chrome") &&
|
||||
!strings.Contains(userAgent, "chromium"):
|
||||
return BrowserSafari
|
||||
default:
|
||||
return BrowserChrome
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,962 @@
|
||||
package httpclient_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/GopeedLab/gopeed/internal/httpclient"
|
||||
"github.com/GopeedLab/gopeed/internal/httpclient/httpclienttest"
|
||||
)
|
||||
|
||||
func TestFixedChromeUsesBrowserTLSFingerprintAndHTTP2(t *testing.T) {
|
||||
server := httpclienttest.NewFingerprintServer(t, httpclienttest.FingerprintServerOptions{
|
||||
RequiredProfile: httpclienttest.ProfileAnyBrowser,
|
||||
})
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Transport: httpclient.TransportOptions{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationFixed,
|
||||
Browser: httpclient.BrowserChrome,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
response, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", response.StatusCode, http.StatusOK)
|
||||
}
|
||||
if response.ProtoMajor != 2 {
|
||||
t.Fatalf("protocol = %q, want HTTP/2", response.Proto)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoFallsBackFromNativeToBrowserFingerprint(t *testing.T) {
|
||||
server := httpclienttest.NewFingerprintServer(t, httpclienttest.FingerprintServerOptions{
|
||||
RequiredProfile: httpclienttest.ProfileAnyBrowser,
|
||||
})
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Transport: httpclient.TransportOptions{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationAuto,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
request, err := http.NewRequest(http.MethodGet, server.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest() error = %v", err)
|
||||
}
|
||||
request.Header.Set("User-Agent", "Mozilla/5.0 Chrome/120.0.0.0 Safari/537.36")
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
t.Fatalf("Do() error = %v", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.ProtoMajor != 2 {
|
||||
t.Fatalf("protocol = %q, want HTTP/2", response.Proto)
|
||||
}
|
||||
if got, want := server.Profiles(), []httpclienttest.Profile{
|
||||
httpclienttest.ProfileNative,
|
||||
httpclienttest.ProfileChrome,
|
||||
}; !slices.Equal(got, want) {
|
||||
t.Fatalf("ClientHello profiles = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoChoosesBrowserFingerprintFromUserAgent(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
userAgent string
|
||||
wantBrowser httpclient.Browser
|
||||
}{
|
||||
{
|
||||
name: "chrome",
|
||||
userAgent: "Mozilla/5.0 Chrome/120.0.0.0 Safari/537.36",
|
||||
wantBrowser: httpclient.BrowserChrome,
|
||||
},
|
||||
{
|
||||
name: "firefox",
|
||||
userAgent: "Mozilla/5.0 Firefox/120.0",
|
||||
wantBrowser: httpclient.BrowserFirefox,
|
||||
},
|
||||
{
|
||||
name: "safari",
|
||||
userAgent: "Mozilla/5.0 Version/16.6 Safari/605.1.15",
|
||||
wantBrowser: httpclient.BrowserSafari,
|
||||
},
|
||||
{
|
||||
name: "unknown defaults to chrome",
|
||||
userAgent: "Gopeed/1.0",
|
||||
wantBrowser: httpclient.BrowserChrome,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
server := httpclienttest.NewFingerprintServer(t, httpclienttest.FingerprintServerOptions{
|
||||
RequiredProfile: httpclienttest.Profile(test.wantBrowser),
|
||||
})
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Transport: httpclient.TransportOptions{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
Impersonation: httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationAuto},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
request, err := http.NewRequest(http.MethodGet, server.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest() error = %v", err)
|
||||
}
|
||||
request.Header.Set("User-Agent", test.userAgent)
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
t.Fatalf("Do() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
|
||||
if response.ProtoMajor != 2 {
|
||||
t.Fatalf("protocol = %q, want HTTP/2", response.Proto)
|
||||
}
|
||||
if got, want := server.Profiles(), []httpclienttest.Profile{
|
||||
httpclienttest.ProfileNative,
|
||||
httpclienttest.Profile(test.wantBrowser),
|
||||
}; !slices.Equal(got, want) {
|
||||
t.Fatalf("profiles = %v, want %v", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoFallsBackOnForbiddenAndPinsBrowserPerURL(t *testing.T) {
|
||||
var requests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
requests.Add(1)
|
||||
if request.Header.Get("Sec-Ch-Ua") == "" {
|
||||
http.Error(w, "browser required", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationAuto,
|
||||
Session: httpclient.NewImpersonationSession(),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
for range 2 {
|
||||
request, err := http.NewRequest(http.MethodGet, server.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest() error = %v", err)
|
||||
}
|
||||
request.Header.Set("User-Agent", "Mozilla/5.0 Chrome/120.0.0.0 Safari/537.36")
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
t.Fatalf("Do() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", response.StatusCode, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
if requests.Load() != 3 {
|
||||
t.Fatalf("requests = %d, want 3 (native + fallback + pinned)", requests.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoDisablesFallbackWhenBrowserReturnsHTTPFailure(t *testing.T) {
|
||||
var requests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
requests.Add(1)
|
||||
if request.Header.Get("Sec-Ch-Ua") == "" {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationAuto,
|
||||
Session: httpclient.NewImpersonationSession(),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
wantStatuses := []int{http.StatusNotFound, http.StatusForbidden}
|
||||
for _, wantStatus := range wantStatuses {
|
||||
response, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != wantStatus {
|
||||
t.Fatalf("status = %d, want %d", response.StatusCode, wantStatus)
|
||||
}
|
||||
}
|
||||
|
||||
if requests.Load() != 3 {
|
||||
t.Fatalf("requests = %d, want 3 (native + one fallback + native)", requests.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoDoesNotDisableFallbackAfterTransportFailure(t *testing.T) {
|
||||
server := httpclienttest.NewFingerprintServer(t, httpclienttest.FingerprintServerOptions{
|
||||
RequiredProfile: httpclienttest.Profile("unsupported"),
|
||||
})
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Transport: httpclient.TransportOptions{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationAuto,
|
||||
Session: httpclient.NewImpersonationSession(),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
for range 2 {
|
||||
if _, err := client.Get(server.URL); err == nil {
|
||||
t.Fatal("Get() error = nil, want TLS failure")
|
||||
}
|
||||
}
|
||||
if got, want := server.Profiles(), []httpclienttest.Profile{
|
||||
httpclienttest.ProfileNative,
|
||||
httpclienttest.ProfileChrome,
|
||||
httpclienttest.ProfileNative,
|
||||
httpclienttest.ProfileChrome,
|
||||
}; !slices.Equal(got, want) {
|
||||
t.Fatalf("ClientHello profiles = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoWithoutSessionDoesNotCacheSelectedBrowser(t *testing.T) {
|
||||
var requests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
requests.Add(1)
|
||||
if request.Header.Get("Sec-Ch-Ua") == "" {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationAuto},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
for range 2 {
|
||||
response, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
}
|
||||
|
||||
if requests.Load() != 4 {
|
||||
t.Fatalf("requests = %d, want 4 (native + fallback without caching)", requests.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoSharesSelectedBrowserAcrossClientsInSession(t *testing.T) {
|
||||
var requests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
requests.Add(1)
|
||||
if request.Header.Get("Sec-Ch-Ua") == "" {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
session := httpclient.NewImpersonationSession()
|
||||
|
||||
for range 2 {
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationAuto,
|
||||
Session: session,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
request, err := http.NewRequest(http.MethodGet, server.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest() error = %v", err)
|
||||
}
|
||||
request.Header.Set("User-Agent", "Mozilla/5.0 Chrome/120.0.0.0 Safari/537.36")
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
t.Fatalf("Do() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
}
|
||||
|
||||
if requests.Load() != 3 {
|
||||
t.Fatalf("requests = %d, want 3 (native + fallback + session pinned)", requests.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoDoesNotShareSelectedBrowserAcrossSessions(t *testing.T) {
|
||||
var requests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
requests.Add(1)
|
||||
if request.Header.Get("Sec-Ch-Ua") == "" {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
for range 2 {
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationAuto,
|
||||
Session: httpclient.NewImpersonationSession(),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
response, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
}
|
||||
|
||||
if requests.Load() != 4 {
|
||||
t.Fatalf("requests = %d, want 4 (native + fallback per session)", requests.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoCachesSelectedBrowserByFullURL(t *testing.T) {
|
||||
var publicBrowserRequests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
browserRequest := request.Header.Get("Sec-Ch-Ua") != ""
|
||||
switch request.URL.Query().Get("access") {
|
||||
case "protected":
|
||||
if !browserRequest {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
case "public":
|
||||
if browserRequest {
|
||||
publicBrowserRequests.Add(1)
|
||||
}
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationAuto,
|
||||
Session: httpclient.NewImpersonationSession(),
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
for range 2 {
|
||||
response, err := client.Get(server.URL + "/file?access=protected")
|
||||
if err != nil {
|
||||
t.Fatalf("protected Get() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
}
|
||||
response, err := client.Get(server.URL + "/file?access=public")
|
||||
if err != nil {
|
||||
t.Fatalf("public Get() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
|
||||
if publicBrowserRequests.Load() != 0 {
|
||||
t.Fatalf("public browser requests = %d, want 0", publicBrowserRequests.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestImpersonationSessionClearRemovesSelections(t *testing.T) {
|
||||
var requests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
requests.Add(1)
|
||||
if request.Header.Get("Sec-Ch-Ua") == "" {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
session := httpclient.NewImpersonationSession()
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationAuto,
|
||||
Session: session,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
for range 2 {
|
||||
response, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
session.Clear()
|
||||
}
|
||||
|
||||
if requests.Load() != 4 {
|
||||
t.Fatalf("requests = %d, want 4 (native + fallback after each clear)", requests.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlobalAutoSelectionDoesNotAffectNativeMode(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
if request.Header.Get("Sec-Ch-Ua") == "" {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
autoClient, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationAuto},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient(auto) error = %v", err)
|
||||
}
|
||||
autoResponse, err := autoClient.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("auto Get() error = %v", err)
|
||||
}
|
||||
autoResponse.Body.Close()
|
||||
if autoResponse.StatusCode != http.StatusOK {
|
||||
t.Fatalf("auto status = %d, want %d", autoResponse.StatusCode, http.StatusOK)
|
||||
}
|
||||
|
||||
nativeClient, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationNative},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient(native) error = %v", err)
|
||||
}
|
||||
nativeResponse, err := nativeClient.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("native Get() error = %v", err)
|
||||
}
|
||||
nativeResponse.Body.Close()
|
||||
if nativeResponse.StatusCode != http.StatusForbidden {
|
||||
t.Fatalf("native status = %d, want %d", nativeResponse.StatusCode, http.StatusForbidden)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoAppliesFallbackResponseCookiesBeforeRetry(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
if request.Header.Get("Sec-Ch-Ua") == "" {
|
||||
http.SetCookie(w, &http.Cookie{Name: "challenge", Value: "passed", Path: "/"})
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
cookie, err := request.Cookie("challenge")
|
||||
if err != nil || cookie.Value != "passed" {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("cookiejar.New() error = %v", err)
|
||||
}
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Client: httpclient.ClientOptions{Jar: jar},
|
||||
Impersonation: httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationAuto},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
response, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", response.StatusCode, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoDoesNotRetryRequestWithNonReplayableBody(t *testing.T) {
|
||||
var requests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
requests.Add(1)
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationAuto},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
bodyReader, bodyWriter := io.Pipe()
|
||||
t.Cleanup(func() {
|
||||
bodyReader.Close()
|
||||
bodyWriter.Close()
|
||||
})
|
||||
go func() {
|
||||
_, _ = bodyWriter.Write([]byte("payload"))
|
||||
_ = bodyWriter.Close()
|
||||
}()
|
||||
request, err := http.NewRequest(http.MethodPost, server.URL, bodyReader)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest() error = %v", err)
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
t.Fatalf("Do() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if requests.Load() != 1 {
|
||||
t.Fatalf("requests = %d, want 1", requests.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoDoesNotRetryReplayablePost(t *testing.T) {
|
||||
var requests atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
requests.Add(1)
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationAuto},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
request, err := http.NewRequest(http.MethodPost, server.URL, strings.NewReader("payload"))
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest() error = %v", err)
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
t.Fatalf("Do() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if requests.Load() != 1 {
|
||||
t.Fatalf("requests = %d, want 1", requests.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeUsesStandardTransportWithHTTP2(t *testing.T) {
|
||||
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
server.EnableHTTP2 = true
|
||||
server.StartTLS()
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Transport: httpclient.TransportOptions{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationNative,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
response, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.ProtoMajor != 2 {
|
||||
t.Fatalf("protocol = %q, want HTTP/2", response.Proto)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientOptionsPreserveCookiesAcrossRedirects(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
switch request.URL.Path {
|
||||
case "/start":
|
||||
http.SetCookie(w, &http.Cookie{Name: "session", Value: "gopeed", Path: "/"})
|
||||
http.Redirect(w, request, "/download", http.StatusFound)
|
||||
case "/download":
|
||||
cookie, err := request.Cookie("session")
|
||||
if err != nil || cookie.Value != "gopeed" {
|
||||
http.Error(w, "cookie missing", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
default:
|
||||
http.NotFound(w, request)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("cookiejar.New() error = %v", err)
|
||||
}
|
||||
var redirects atomic.Int32
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Client: httpclient.ClientOptions{
|
||||
Jar: jar,
|
||||
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
|
||||
redirects.Add(1)
|
||||
return nil
|
||||
},
|
||||
},
|
||||
Impersonation: httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationNative},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
response, err := client.Get(server.URL + "/start")
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", response.StatusCode, http.StatusOK)
|
||||
}
|
||||
if redirects.Load() != 1 {
|
||||
t.Fatalf("redirect calls = %d, want 1", redirects.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientTimeoutCancelsRequest(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, request *http.Request) {
|
||||
<-request.Context().Done()
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Client: httpclient.ClientOptions{Timeout: 50 * time.Millisecond},
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationNative,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
_, err = client.Get(server.URL)
|
||||
if err == nil {
|
||||
t.Fatal("Get() error = nil, want timeout")
|
||||
}
|
||||
var networkError net.Error
|
||||
if !errors.As(err, &networkError) || !networkError.Timeout() {
|
||||
t.Fatalf("Get() error = %v, want net.Error timeout", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransportOptionsUseProxyInNativeAndFixedModes(t *testing.T) {
|
||||
proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
if request.URL.Host != "download.example" {
|
||||
http.Error(w, "unexpected target", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
t.Cleanup(proxy.Close)
|
||||
proxyURL, err := url.Parse(proxy.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("url.Parse() error = %v", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
impersonation httpclient.ImpersonationOptions
|
||||
}{
|
||||
{
|
||||
name: "native",
|
||||
impersonation: httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationNative},
|
||||
},
|
||||
{
|
||||
name: "fixed chrome",
|
||||
impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationFixed,
|
||||
Browser: httpclient.BrowserChrome,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Transport: httpclient.TransportOptions{Proxy: http.ProxyURL(proxyURL)},
|
||||
Impersonation: test.impersonation,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
response, err := client.Get("http://download.example/file")
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", response.StatusCode, http.StatusOK)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransportOptionsUseDialContextInNativeAndFixedModes(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
impersonation httpclient.ImpersonationOptions
|
||||
}{
|
||||
{
|
||||
name: "native",
|
||||
impersonation: httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationNative},
|
||||
},
|
||||
{
|
||||
name: "fixed chrome",
|
||||
impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationFixed,
|
||||
Browser: httpclient.BrowserChrome,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var calls atomic.Int32
|
||||
dialer := &net.Dialer{}
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Transport: httpclient.TransportOptions{
|
||||
DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
calls.Add(1)
|
||||
return dialer.DialContext(ctx, network, address)
|
||||
},
|
||||
},
|
||||
Impersonation: test.impersonation,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
response, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if calls.Load() == 0 {
|
||||
t.Fatal("DialContext() was not called")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTransportOptionsApplyTLSHandshakeTimeoutInNativeAndFixedModes(t *testing.T) {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("net.Listen() error = %v", err)
|
||||
}
|
||||
closed := make(chan struct{})
|
||||
t.Cleanup(func() {
|
||||
close(closed)
|
||||
listener.Close()
|
||||
})
|
||||
go func() {
|
||||
for {
|
||||
connection, err := listener.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
<-closed
|
||||
connection.Close()
|
||||
}()
|
||||
}
|
||||
}()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
impersonation httpclient.ImpersonationOptions
|
||||
}{
|
||||
{
|
||||
name: "native",
|
||||
impersonation: httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationNative},
|
||||
},
|
||||
{
|
||||
name: "fixed chrome",
|
||||
impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationFixed,
|
||||
Browser: httpclient.BrowserChrome,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Client: httpclient.ClientOptions{Timeout: time.Second},
|
||||
Transport: httpclient.TransportOptions{
|
||||
TLSHandshakeTimeout: 50 * time.Millisecond,
|
||||
},
|
||||
Impersonation: test.impersonation,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
started := time.Now()
|
||||
_, err = client.Get("https://" + listener.Addr().String())
|
||||
if err == nil {
|
||||
t.Fatal("Get() error = nil, want TLS handshake timeout")
|
||||
}
|
||||
if elapsed := time.Since(started); elapsed >= 500*time.Millisecond {
|
||||
t.Fatalf("TLS handshake took %s, want less than 500ms", elapsed)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFixedBrowserAddsProfileHeadersWithoutOverwritingExplicitHeaders(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
w.Header().Set("Seen-User-Agent", request.UserAgent())
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationFixed,
|
||||
Browser: httpclient.BrowserChrome,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
|
||||
response, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if userAgent := response.Header.Get("Seen-User-Agent"); !strings.Contains(userAgent, "Chrome/120") {
|
||||
t.Fatalf("profile User-Agent = %q, want Chrome/120", userAgent)
|
||||
}
|
||||
|
||||
request, err := http.NewRequest(http.MethodGet, server.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest() error = %v", err)
|
||||
}
|
||||
request.Header.Set("User-Agent", "Gopeed-Custom/1.0")
|
||||
response, err = client.Do(request)
|
||||
if err != nil {
|
||||
t.Fatalf("Do() error = %v", err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if userAgent := response.Header.Get("Seen-User-Agent"); userAgent != "Gopeed-Custom/1.0" {
|
||||
t.Fatalf("explicit User-Agent = %q, want Gopeed-Custom/1.0", userAgent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFixedClientCloseIdleConnectionsReachesBrowserTransport(t *testing.T) {
|
||||
var connections atomic.Int32
|
||||
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
server.Config.ConnState = func(_ net.Conn, state http.ConnState) {
|
||||
if state == http.StateNew {
|
||||
connections.Add(1)
|
||||
}
|
||||
}
|
||||
server.Start()
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationFixed,
|
||||
Browser: httpclient.BrowserChrome,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient() error = %v", err)
|
||||
}
|
||||
for attempt := range 2 {
|
||||
response, err := client.Get(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Get() error = %v", err)
|
||||
}
|
||||
_, _ = io.Copy(io.Discard, response.Body)
|
||||
response.Body.Close()
|
||||
if attempt == 0 {
|
||||
client.CloseIdleConnections()
|
||||
}
|
||||
}
|
||||
|
||||
if connections.Load() != 2 {
|
||||
t.Fatalf("connections = %d, want 2", connections.Load())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package httpclienttest
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Profile string
|
||||
|
||||
const (
|
||||
ProfileNative Profile = "native"
|
||||
ProfileChrome Profile = "chrome"
|
||||
ProfileFirefox Profile = "firefox"
|
||||
ProfileSafari Profile = "safari"
|
||||
ProfileAnyBrowser Profile = "browser"
|
||||
)
|
||||
|
||||
type FingerprintServerOptions struct {
|
||||
RequiredProfile Profile
|
||||
Handler http.Handler
|
||||
}
|
||||
|
||||
type FingerprintServer struct {
|
||||
*httptest.Server
|
||||
|
||||
mu sync.Mutex
|
||||
profiles []Profile
|
||||
}
|
||||
|
||||
func NewFingerprintServer(t testing.TB, options FingerprintServerOptions) *FingerprintServer {
|
||||
t.Helper()
|
||||
|
||||
handler := options.Handler
|
||||
if handler == nil {
|
||||
handler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
}
|
||||
server := &FingerprintServer{
|
||||
Server: httptest.NewUnstartedServer(handler),
|
||||
}
|
||||
server.EnableHTTP2 = true
|
||||
server.Config.ErrorLog = log.New(io.Discard, "", 0)
|
||||
server.TLS = &tls.Config{
|
||||
GetConfigForClient: func(hello *tls.ClientHelloInfo) (*tls.Config, error) {
|
||||
profile := classifyProfile(hello.CipherSuites)
|
||||
server.mu.Lock()
|
||||
server.profiles = append(server.profiles, profile)
|
||||
server.mu.Unlock()
|
||||
if profileMatches(profile, options.RequiredProfile) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.New("required TLS fingerprint was not presented")
|
||||
},
|
||||
}
|
||||
server.StartTLS()
|
||||
t.Cleanup(server.Close)
|
||||
return server
|
||||
}
|
||||
|
||||
func (s *FingerprintServer) Profiles() []Profile {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return append([]Profile(nil), s.profiles...)
|
||||
}
|
||||
|
||||
func profileMatches(actual, required Profile) bool {
|
||||
if required == ProfileAnyBrowser {
|
||||
return actual != ProfileNative
|
||||
}
|
||||
return actual == required
|
||||
}
|
||||
|
||||
func classifyProfile(cipherSuites []uint16) Profile {
|
||||
if len(cipherSuites) >= 5 && isGREASE(cipherSuites[0]) {
|
||||
switch cipherSuites[4] {
|
||||
case tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
|
||||
return ProfileChrome
|
||||
case tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
|
||||
return ProfileSafari
|
||||
}
|
||||
}
|
||||
if len(cipherSuites) >= 3 &&
|
||||
cipherSuites[0] == tls.TLS_AES_128_GCM_SHA256 &&
|
||||
cipherSuites[1] == tls.TLS_CHACHA20_POLY1305_SHA256 &&
|
||||
cipherSuites[2] == tls.TLS_AES_256_GCM_SHA384 {
|
||||
return ProfileFirefox
|
||||
}
|
||||
return ProfileNative
|
||||
}
|
||||
|
||||
func isGREASE(value uint16) bool {
|
||||
return value&0x0f0f == 0x0a0a && byte(value) == byte(value>>8)
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/GopeedLab/gopeed/pkg/base"
|
||||
"github.com/GopeedLab/gopeed/pkg/protocol/bt"
|
||||
"github.com/GopeedLab/gopeed/pkg/util"
|
||||
"github.com/RoaringBitmap/roaring"
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"github.com/anacrolix/torrent/storage"
|
||||
@@ -162,20 +163,209 @@ func (f *Fetcher) Meta() *fetcher.FetcherMeta {
|
||||
return f.meta
|
||||
}
|
||||
|
||||
func (f *Fetcher) Stats() any {
|
||||
func (f *Fetcher) Stats() *fetcher.Stats {
|
||||
var stats torrent.TorrentStats
|
||||
var pieceMap *base.PieceMap
|
||||
pieceMapReady := false
|
||||
peers := make([]*base.PeerStats, 0)
|
||||
if f.torrent != nil {
|
||||
stats = f.torrent.Stats()
|
||||
pieceMap, pieceMapReady = buildBTPieceMap(f.torrent)
|
||||
peers = buildBTPeers(f.torrent)
|
||||
} else {
|
||||
stats = torrent.TorrentStats{}
|
||||
}
|
||||
return &bt.Stats{
|
||||
TotalPeers: stats.TotalPeers,
|
||||
ActivePeers: stats.ActivePeers,
|
||||
ConnectedSeeders: stats.ConnectedSeeders,
|
||||
SeedBytes: f.data.SeedBytes,
|
||||
SeedRatio: f.seedRadio(),
|
||||
SeedTime: f.data.SeedTime,
|
||||
var snapshot any
|
||||
if pieceMapReady {
|
||||
snapshot = &bt.StatsSnapshot{
|
||||
SeedBytes: f.data.SeedBytes,
|
||||
SeedRatio: f.seedRadio(),
|
||||
SeedTime: f.data.SeedTime,
|
||||
PieceMap: pieceMap.Clone(),
|
||||
}
|
||||
}
|
||||
// When completion is temporarily unknown while anacrolix restores storage
|
||||
// or verifies pieces, the nil Snapshot tells Downloader to retain the last
|
||||
// trusted bitset instead of replacing it with an all-empty map.
|
||||
return &fetcher.Stats{
|
||||
Snapshot: snapshot,
|
||||
Runtime: &bt.StatsRuntime{
|
||||
TotalPeers: stats.TotalPeers,
|
||||
ActivePeers: stats.ActivePeers,
|
||||
ConnectedSeeders: stats.ConnectedSeeders,
|
||||
ConnectedLeechers: stats.ActivePeers - stats.ConnectedSeeders,
|
||||
Peers: peers,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// buildBTPieceMap converts anacrolix's ordered completion runs to Gopeed's
|
||||
// one-bit map. ready is false until storage knows the completion state of every
|
||||
// piece; this prevents initial hash checking from briefly replacing a restored
|
||||
// map with an all-empty value.
|
||||
func buildBTPieceMap(t *torrent.Torrent) (pieceMap *base.PieceMap, ready bool) {
|
||||
info := t.Info()
|
||||
if info == nil {
|
||||
return nil, false
|
||||
}
|
||||
pieceMap = base.NewPieceMap(info.NumPieces(), info.PieceLength)
|
||||
ready = applyBTPieceRuns(pieceMap, t.PieceStateRuns())
|
||||
return pieceMap, ready
|
||||
}
|
||||
|
||||
func applyBTPieceRuns(pieceMap *base.PieceMap, runs []torrent.PieceStateRun) (ready bool) {
|
||||
ready = true
|
||||
pieceIndex := 0
|
||||
for _, run := range runs {
|
||||
if !run.Ok {
|
||||
ready = false
|
||||
}
|
||||
completed := run.Ok && run.Complete
|
||||
for range run.Length {
|
||||
if pieceIndex >= pieceMap.PieceCount() {
|
||||
return ready
|
||||
}
|
||||
_ = pieceMap.Update(pieceIndex, completed)
|
||||
pieceIndex++
|
||||
}
|
||||
}
|
||||
return ready && pieceIndex == pieceMap.PieceCount()
|
||||
}
|
||||
|
||||
func buildBTPeers(t *torrent.Torrent) []*base.PeerStats {
|
||||
connections := t.PeerConns()
|
||||
totalPieces := 0
|
||||
var missingWantedPieces *roaring.Bitmap
|
||||
// Magnet torrents can connect to peers before their metadata arrives. Keep
|
||||
// those runtime peer stats, but leave completion and relevance unavailable
|
||||
// until the metadata provides a trusted total piece count.
|
||||
if info := t.Info(); info != nil {
|
||||
totalPieces = info.NumPieces()
|
||||
missingWantedPieces = btMissingWantedPieces(t.PieceStateRuns(), totalPieces)
|
||||
}
|
||||
peers := make([]*base.PeerStats, 0, len(connections))
|
||||
for _, connection := range connections {
|
||||
peerStats := connection.Stats()
|
||||
peers = append(peers, &base.PeerStats{
|
||||
Address: safeBTPeerAddress(connection),
|
||||
Client: btPeerClient(connection),
|
||||
DownloadSpeed: int64(peerStats.DownloadRate),
|
||||
UploadSpeed: int64(peerStats.LastWriteUploadRate),
|
||||
PieceCount: peerStats.RemotePieceCount,
|
||||
Completion: btPeerCompletion(peerStats.RemotePieceCount, totalPieces),
|
||||
Relevance: btPeerRelevance(connection.PeerPieces(), missingWantedPieces),
|
||||
Source: normalizeBTPeerSource(connection.Discovery),
|
||||
Transport: normalizeBTTransport(connection.Network),
|
||||
})
|
||||
}
|
||||
return peers
|
||||
}
|
||||
|
||||
// btPeerCompletion converts anacrolix's remote piece count into the ratio the
|
||||
// API exposes. A nil value distinguishes unavailable metadata from a real 0%.
|
||||
func btPeerCompletion(pieceCount, totalPieces int) *float64 {
|
||||
if totalPieces <= 0 {
|
||||
return nil
|
||||
}
|
||||
if pieceCount < 0 {
|
||||
pieceCount = 0
|
||||
} else if pieceCount > totalPieces {
|
||||
pieceCount = totalPieces
|
||||
}
|
||||
completion := float64(pieceCount) / float64(totalPieces)
|
||||
return &completion
|
||||
}
|
||||
|
||||
// btMissingWantedPieces builds the denominator used by peer relevance. Pieces
|
||||
// belonging only to skipped files are excluded because their priority is none.
|
||||
func btMissingWantedPieces(runs []torrent.PieceStateRun, totalPieces int) *roaring.Bitmap {
|
||||
if totalPieces <= 0 {
|
||||
return nil
|
||||
}
|
||||
missing := roaring.New()
|
||||
pieceIndex := 0
|
||||
for _, run := range runs {
|
||||
for range run.Length {
|
||||
if pieceIndex >= totalPieces {
|
||||
return nil
|
||||
}
|
||||
if run.Priority != torrent.PiecePriorityNone && !(run.Ok && run.Complete) {
|
||||
missing.Add(uint32(pieceIndex))
|
||||
}
|
||||
pieceIndex++
|
||||
}
|
||||
}
|
||||
if pieceIndex != totalPieces {
|
||||
return nil
|
||||
}
|
||||
return missing
|
||||
}
|
||||
|
||||
// btPeerRelevance reports how much of the local wanted, missing set the remote
|
||||
// peer can provide. A zero value is meaningful; nil means there is no denominator.
|
||||
func btPeerRelevance(peerPieces, missingWantedPieces *roaring.Bitmap) *float64 {
|
||||
if peerPieces == nil || missingWantedPieces == nil || missingWantedPieces.IsEmpty() {
|
||||
return nil
|
||||
}
|
||||
relevantPieces := roaring.And(peerPieces, missingWantedPieces).GetCardinality()
|
||||
relevance := float64(relevantPieces) / float64(missingWantedPieces.GetCardinality())
|
||||
return &relevance
|
||||
}
|
||||
|
||||
// A uTP socket may close between PeerConns and this snapshot. Some uTP address
|
||||
// implementations panic after close, so one disappearing address must not make
|
||||
// the entire stats endpoint fail.
|
||||
func safeBTPeerAddress(connection *torrent.PeerConn) (address string) {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
address = ""
|
||||
}
|
||||
}()
|
||||
if connection.RemoteAddr == nil {
|
||||
return ""
|
||||
}
|
||||
return connection.RemoteAddr.String()
|
||||
}
|
||||
|
||||
func btPeerClient(connection *torrent.PeerConn) string {
|
||||
value := connection.PeerClientName.Load()
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprint(value)
|
||||
}
|
||||
|
||||
func normalizeBTPeerSource(source torrent.PeerSource) string {
|
||||
switch source {
|
||||
case torrent.PeerSourceTracker:
|
||||
return "tracker"
|
||||
case torrent.PeerSourceDhtGetPeers, torrent.PeerSourceDhtAnnouncePeer:
|
||||
return "dht"
|
||||
case torrent.PeerSourcePex:
|
||||
return "pex"
|
||||
case torrent.PeerSourceIncoming:
|
||||
return "incoming"
|
||||
case torrent.PeerSourceDirect:
|
||||
return "direct"
|
||||
case torrent.PeerSourceUtHolepunch:
|
||||
return "holepunch"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeBTTransport(network string) string {
|
||||
network = strings.ToLower(network)
|
||||
switch {
|
||||
case strings.Contains(network, "webrtc"):
|
||||
return "webrtc"
|
||||
case strings.Contains(network, "udp"):
|
||||
// anacrolix labels uTP sockets with their underlying udp network.
|
||||
return "utp"
|
||||
case strings.Contains(network, "tcp"):
|
||||
return "tcp"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,199 @@
|
||||
package bt
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||
"github.com/GopeedLab/gopeed/pkg/base"
|
||||
protocolbt "github.com/GopeedLab/gopeed/pkg/protocol/bt"
|
||||
"github.com/RoaringBitmap/roaring"
|
||||
"github.com/anacrolix/torrent"
|
||||
"github.com/anacrolix/torrent/storage"
|
||||
)
|
||||
|
||||
func TestStatsSeparatesSnapshotAndRuntime(t *testing.T) {
|
||||
frame := (&Fetcher{
|
||||
meta: &fetcher.FetcherMeta{Res: &base.Resource{Size: 256}},
|
||||
data: &fetcherData{SeedBytes: 128, SeedTime: 64},
|
||||
}).Stats()
|
||||
if frame.Snapshot != nil {
|
||||
t.Fatalf("snapshot = %T, want nil until piece completion is known", frame.Snapshot)
|
||||
}
|
||||
runtime, ok := frame.Runtime.(*protocolbt.StatsRuntime)
|
||||
if !ok {
|
||||
t.Fatalf("runtime type = %T", frame.Runtime)
|
||||
}
|
||||
if len(runtime.Peers) != 0 || runtime.TotalPeers != 0 {
|
||||
t.Fatalf("unexpected runtime: %+v", runtime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatsBeforeTorrentMetadataIsAvailable(t *testing.T) {
|
||||
config := torrent.TestingConfig(t)
|
||||
config.DisableTCP = true
|
||||
config.DisableUTP = true
|
||||
client, err := torrent.NewClient(config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
torrentWithoutMetadata, err := client.AddMagnet(
|
||||
"magnet:?xt=urn:btih:0123456789abcdef0123456789abcdef01234567",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info := torrentWithoutMetadata.Info(); info != nil {
|
||||
t.Fatalf("torrent info = %+v, want nil before metadata is available", info)
|
||||
}
|
||||
|
||||
frame := (&Fetcher{
|
||||
torrent: torrentWithoutMetadata,
|
||||
meta: &fetcher.FetcherMeta{},
|
||||
data: &fetcherData{},
|
||||
}).Stats()
|
||||
if frame.Snapshot != nil {
|
||||
t.Fatalf("snapshot = %T, want nil before metadata is available", frame.Snapshot)
|
||||
}
|
||||
runtime, ok := frame.Runtime.(*protocolbt.StatsRuntime)
|
||||
if !ok {
|
||||
t.Fatalf("runtime type = %T", frame.Runtime)
|
||||
}
|
||||
if len(runtime.Peers) != 0 || runtime.TotalPeers != 0 {
|
||||
t.Fatalf("unexpected runtime before metadata is available: %+v", runtime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyBTPieceRunsPublishesVerifiedCompletionOnly(t *testing.T) {
|
||||
pieceMap := base.NewPieceMap(4, 1024)
|
||||
ready := applyBTPieceRuns(pieceMap, []torrent.PieceStateRun{
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(true, true)}, Length: 1},
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(true, false), Partial: true}, Length: 2},
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(false, false), Hashing: true}, Length: 1},
|
||||
})
|
||||
if ready {
|
||||
t.Fatal("piece map became ready while one completion is unknown")
|
||||
}
|
||||
want := []bool{true, false, false, false}
|
||||
for index, expected := range want {
|
||||
got, err := pieceMap.Get(index)
|
||||
if err != nil || got != expected {
|
||||
t.Fatalf("piece %d = %v, %v; want %v", index, got, err, expected)
|
||||
}
|
||||
}
|
||||
|
||||
knownMap := base.NewPieceMap(2, 1024)
|
||||
if !applyBTPieceRuns(knownMap, []torrent.PieceStateRun{
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(true, true)}, Length: 1},
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(true, false)}, Length: 1},
|
||||
}) {
|
||||
t.Fatal("fully known completion map was not ready")
|
||||
}
|
||||
if applyBTPieceRuns(base.NewPieceMap(2, 1024), []torrent.PieceStateRun{
|
||||
{PieceState: torrent.PieceState{Completion: torrentCompletion(true, true)}, Length: 1},
|
||||
}) {
|
||||
t.Fatal("incomplete piece runs were published")
|
||||
}
|
||||
}
|
||||
|
||||
func torrentCompletion(ok, complete bool) storage.Completion {
|
||||
return storage.Completion{Ok: ok, Complete: complete}
|
||||
}
|
||||
|
||||
func TestNormalizeBTPeerMetadata(t *testing.T) {
|
||||
if got := normalizeBTPeerSource(torrent.PeerSourceDhtGetPeers); got != "dht" {
|
||||
t.Fatalf("DHT source = %q", got)
|
||||
}
|
||||
if got := normalizeBTPeerSource(torrent.PeerSourcePex); got != "pex" {
|
||||
t.Fatalf("PEX source = %q", got)
|
||||
}
|
||||
transports := map[string]string{
|
||||
"udp4": "utp",
|
||||
"udp6": "utp",
|
||||
"tcp4": "tcp",
|
||||
"tcp6": "tcp",
|
||||
"webrtc": "webrtc",
|
||||
"unix": "unknown",
|
||||
}
|
||||
for network, want := range transports {
|
||||
if got := normalizeBTTransport(network); got != want {
|
||||
t.Errorf("normalizeBTTransport(%q) = %q, want %q", network, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBTPeerCompletion(t *testing.T) {
|
||||
if got := btPeerCompletion(1, 4); got == nil || *got != 0.25 {
|
||||
t.Fatalf("completion = %v, want 0.25", got)
|
||||
}
|
||||
if got := btPeerCompletion(5, 4); got == nil || *got != 1 {
|
||||
t.Fatalf("clamped completion = %v, want 1", got)
|
||||
}
|
||||
if got := btPeerCompletion(-1, 4); got == nil || *got != 0 {
|
||||
t.Fatalf("negative completion = %v, want 0", got)
|
||||
}
|
||||
if got := btPeerCompletion(0, 0); got != nil {
|
||||
t.Fatalf("completion without metadata = %v, want nil", *got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBTPeerRelevanceUsesWantedMissingPieces(t *testing.T) {
|
||||
missing := btMissingWantedPieces([]torrent.PieceStateRun{
|
||||
{
|
||||
PieceState: torrent.PieceState{
|
||||
Completion: torrentCompletion(true, true),
|
||||
Priority: torrent.PiecePriorityNormal,
|
||||
},
|
||||
Length: 2,
|
||||
},
|
||||
{
|
||||
PieceState: torrent.PieceState{
|
||||
Completion: torrentCompletion(true, false),
|
||||
Priority: torrent.PiecePriorityNormal,
|
||||
},
|
||||
Length: 2,
|
||||
},
|
||||
{
|
||||
PieceState: torrent.PieceState{
|
||||
Completion: torrentCompletion(true, false),
|
||||
Priority: torrent.PiecePriorityNone,
|
||||
},
|
||||
Length: 2,
|
||||
},
|
||||
}, 6)
|
||||
if missing == nil || !missing.Equals(roaring.BitmapOf(2, 3)) {
|
||||
t.Fatalf("missing wanted pieces = %v, want {2, 3}", missing)
|
||||
}
|
||||
|
||||
if got := btPeerRelevance(roaring.BitmapOf(0, 2, 4), missing); got == nil || *got != 0.5 {
|
||||
t.Fatalf("relevance = %v, want 0.5", got)
|
||||
}
|
||||
if got := btPeerRelevance(roaring.BitmapOf(0, 1, 4), missing); got == nil || *got != 0 {
|
||||
t.Fatalf("zero relevance = %v, want 0", got)
|
||||
}
|
||||
if got := btPeerRelevance(roaring.BitmapOf(2, 3), roaring.New()); got != nil {
|
||||
t.Fatalf("relevance with no missing pieces = %v, want nil", *got)
|
||||
}
|
||||
if got := btMissingWantedPieces(nil, 6); got != nil {
|
||||
t.Fatalf("incomplete piece states = %v, want nil", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBTPeerRelevanceTracksRemainingDownload(t *testing.T) {
|
||||
missing := roaring.BitmapOf(90, 91, 92, 93, 94, 95, 96, 97, 98, 99)
|
||||
if got := btPeerRelevance(missing.Clone(), missing); got == nil || *got != 1 {
|
||||
t.Fatalf("peer covering every missing piece = %v, want 1", got)
|
||||
}
|
||||
|
||||
oneRelevantPiece := roaring.BitmapOf(90)
|
||||
if got := btPeerRelevance(oneRelevantPiece, missing); got == nil || *got != 0.1 {
|
||||
t.Fatalf("peer covering one of ten missing pieces = %v, want 0.1", got)
|
||||
}
|
||||
|
||||
missingAfterTransfer := missing.Clone()
|
||||
missingAfterTransfer.Remove(90)
|
||||
if got := btPeerRelevance(oneRelevantPiece, missingAfterTransfer); got == nil || *got != 0 {
|
||||
t.Fatalf("peer after transferring its only relevant piece = %v, want 0", got)
|
||||
}
|
||||
}
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -0,0 +1,558 @@
|
||||
package ed2k
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/GopeedLab/gopeed/internal/controller"
|
||||
"github.com/GopeedLab/gopeed/internal/fetcher"
|
||||
"github.com/GopeedLab/gopeed/pkg/base"
|
||||
ped2k "github.com/GopeedLab/gopeed/pkg/protocol/ed2k"
|
||||
"github.com/monkeyWie/goed2k"
|
||||
gprotocol "github.com/monkeyWie/goed2k/protocol"
|
||||
)
|
||||
|
||||
type clientStateStore struct {
|
||||
store fetcher.ProtocolStateStore
|
||||
}
|
||||
|
||||
func (s *clientStateStore) Load() (*goed2k.ClientState, error) {
|
||||
if s == nil || s.store == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var state goed2k.ClientState
|
||||
exist, err := s.store.Load(&state)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exist {
|
||||
return nil, nil
|
||||
}
|
||||
return &state, nil
|
||||
}
|
||||
|
||||
func (s *clientStateStore) Save(state *goed2k.ClientState) error {
|
||||
if s == nil || s.store == nil {
|
||||
return nil
|
||||
}
|
||||
if state == nil {
|
||||
return s.store.Delete()
|
||||
}
|
||||
return s.store.Save(state)
|
||||
}
|
||||
|
||||
type Fetcher struct {
|
||||
ctl *controller.Controller
|
||||
config *config
|
||||
|
||||
manager *FetcherManager
|
||||
meta *fetcher.FetcherMeta
|
||||
handle goed2k.TransferHandle
|
||||
|
||||
waitCtx context.Context
|
||||
waitCancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (f *Fetcher) Setup(ctl *controller.Controller) {
|
||||
f.ctl = ctl
|
||||
if f.meta == nil {
|
||||
f.meta = &fetcher.FetcherMeta{}
|
||||
}
|
||||
f.waitCtx, f.waitCancel = context.WithCancel(context.Background())
|
||||
f.ctl.GetConfig(&f.config)
|
||||
}
|
||||
|
||||
func (f *Fetcher) Resolve(req *base.Request, opts *base.Options) error {
|
||||
link, err := parseLink(req.URL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.meta.Req = req
|
||||
f.meta.Opts = opts
|
||||
if f.meta.Opts == nil {
|
||||
f.meta.Opts = &base.Options{}
|
||||
}
|
||||
f.meta.Res = buildResource(link)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Fetcher) Start() error {
|
||||
link, err := parseLink(f.meta.Req.URL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if f.meta.Res == nil {
|
||||
f.meta.Res = buildResource(link)
|
||||
}
|
||||
|
||||
client, err := f.getClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetPath := f.meta.SingleFilepath()
|
||||
if err := os.MkdirAll(filepath.Dir(targetPath), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
handle := client.FindTransfer(link.Hash)
|
||||
if handle.IsValid() {
|
||||
f.handle = handle
|
||||
if handle.IsPaused() {
|
||||
if err := client.ResumeTransfer(handle.GetHash()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
atp := goed2k.AddTransferParams{
|
||||
Hash: link.Hash,
|
||||
CreateTime: time.Now().UnixMilli(),
|
||||
Size: link.NumberValue,
|
||||
FilePath: targetPath,
|
||||
}
|
||||
handle, err = client.AddTransfer(atp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.handle = handle
|
||||
if handle.IsValid() && handle.IsPaused() {
|
||||
if err := client.ResumeTransfer(handle.GetHash()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Fetcher) Patch(req *base.Request, opts *base.Options) error {
|
||||
handle := f.currentHandle()
|
||||
|
||||
if opts != nil && (opts.Name != "" || opts.Path != "") && handle.IsValid() {
|
||||
return errors.New("cannot change ed2k target path after transfer started")
|
||||
}
|
||||
if req != nil && req.URL != "" && handle.IsValid() {
|
||||
return errors.New("cannot change ed2k link after transfer started")
|
||||
}
|
||||
|
||||
if req != nil {
|
||||
if req.URL != "" {
|
||||
link, err := parseLink(req.URL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.meta.Req.URL = req.URL
|
||||
f.meta.Res = buildResource(link)
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
if opts != nil {
|
||||
if opts.Name != "" {
|
||||
f.meta.Opts.Name = opts.Name
|
||||
}
|
||||
if opts.Path != "" {
|
||||
f.meta.Opts.Path = opts.Path
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Fetcher) Pause() error {
|
||||
handle := f.currentHandle()
|
||||
if !handle.IsValid() {
|
||||
return nil
|
||||
}
|
||||
client, err := f.getClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := client.PauseTransfer(handle.GetHash()); err != nil {
|
||||
return err
|
||||
}
|
||||
f.handle = handle
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Fetcher) Close() error {
|
||||
if f.waitCancel != nil {
|
||||
f.waitCancel()
|
||||
}
|
||||
handle := f.currentHandle()
|
||||
if !handle.IsValid() {
|
||||
return nil
|
||||
}
|
||||
client, err := f.getClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.handle = handle
|
||||
return client.RemoveTransfer(handle.GetHash(), false)
|
||||
}
|
||||
|
||||
func (f *Fetcher) Meta() *fetcher.FetcherMeta {
|
||||
return f.meta
|
||||
}
|
||||
|
||||
func (f *Fetcher) Stats() *fetcher.Stats {
|
||||
handle := f.currentHandle()
|
||||
if !handle.IsValid() {
|
||||
return &fetcher.Stats{
|
||||
Runtime: &ped2k.StatsRuntime{
|
||||
Peers: make([]*base.PeerStats, 0),
|
||||
},
|
||||
}
|
||||
}
|
||||
status := handle.GetStatus()
|
||||
pieceMap := buildED2KPieceMap(handle.PieceSnapshots())
|
||||
mapReady := pieceMap != nil && status.State != goed2k.LoadingResumeData && status.State != goed2k.Verifying
|
||||
var snapshot any
|
||||
if mapReady {
|
||||
snapshot = &ped2k.StatsSnapshot{
|
||||
Upload: status.Upload,
|
||||
TotalDone: status.TotalDone,
|
||||
TotalReceived: status.TotalReceived,
|
||||
TotalWanted: status.TotalWanted,
|
||||
PieceMap: pieceMap.Clone(),
|
||||
}
|
||||
}
|
||||
// Resume loading and verification are transitional. A nil Snapshot retains
|
||||
// the last trusted completion bitset until goed2k publishes a stable map.
|
||||
return &fetcher.Stats{
|
||||
Snapshot: snapshot,
|
||||
Runtime: &ped2k.StatsRuntime{
|
||||
State: string(status.State),
|
||||
Paused: status.Paused,
|
||||
ActivePeers: handle.ActiveConnections(),
|
||||
TotalPeers: status.NumPeers,
|
||||
DownloadRate: status.DownloadRate,
|
||||
UploadRate: status.UploadRate,
|
||||
Peers: buildED2KPeers(handle.GetPeersInfo()),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func buildED2KPieceMap(snapshots []goed2k.PieceSnapshot) *base.PieceMap {
|
||||
if len(snapshots) == 0 {
|
||||
return nil
|
||||
}
|
||||
pieceMap := base.NewPieceMap(len(snapshots), goed2k.PieceSize)
|
||||
for _, snapshot := range snapshots {
|
||||
if snapshot.Index < 0 || snapshot.Index >= pieceMap.PieceCount() {
|
||||
continue
|
||||
}
|
||||
_ = pieceMap.Update(snapshot.Index, snapshot.State == goed2k.PieceSnapshotFinished)
|
||||
}
|
||||
return pieceMap
|
||||
}
|
||||
|
||||
func buildED2KPeers(peerInfos []goed2k.PeerInfo) []*base.PeerStats {
|
||||
peers := make([]*base.PeerStats, 0, len(peerInfos))
|
||||
for _, peerInfo := range peerInfos {
|
||||
peers = append(peers, &base.PeerStats{
|
||||
Address: peerInfo.Endpoint.String(),
|
||||
Client: ed2kPeerClient(peerInfo),
|
||||
DownloadSpeed: int64(peerInfo.PayloadDownloadSpeed),
|
||||
UploadSpeed: int64(peerInfo.PayloadUploadSpeed),
|
||||
PieceCount: peerInfo.RemotePieces.Count(),
|
||||
Source: peerInfo.SourceString(),
|
||||
Transport: "tcp",
|
||||
})
|
||||
}
|
||||
return peers
|
||||
}
|
||||
|
||||
func ed2kPeerClient(peerInfo goed2k.PeerInfo) string {
|
||||
return strings.TrimSpace(peerInfo.ModName + " " + peerInfo.StrModVersion)
|
||||
}
|
||||
|
||||
func (f *Fetcher) Progress() fetcher.Progress {
|
||||
handle := f.currentHandle()
|
||||
if !handle.IsValid() {
|
||||
return fetcher.Progress{0}
|
||||
}
|
||||
return fetcher.Progress{handle.GetStatus().TotalReceived}
|
||||
}
|
||||
|
||||
func (f *Fetcher) Wait() error {
|
||||
client, err := f.getClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hash, err := f.hash()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
handle := f.currentHandle()
|
||||
if handle.IsValid() && handle.IsFinished() {
|
||||
return nil
|
||||
}
|
||||
|
||||
progressCh, cancel := client.SubscribeTransferProgress()
|
||||
defer cancel()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-f.waitCtx.Done():
|
||||
return nil
|
||||
case event, ok := <-progressCh:
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
for _, transfer := range event.Transfers {
|
||||
if transfer.Hash.Compare(hash) != 0 {
|
||||
continue
|
||||
}
|
||||
// Removal can happen during task deletion or client shutdown, both of
|
||||
// which should unblock Wait without treating it as a download failure.
|
||||
if transfer.Removed || transfer.State == goed2k.Finished {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Fetcher) getClient() (*goed2k.Client, error) {
|
||||
if f.manager == nil {
|
||||
f.manager = &FetcherManager{}
|
||||
}
|
||||
return f.manager.initClient(f.config)
|
||||
}
|
||||
|
||||
func (f *Fetcher) currentHandle() goed2k.TransferHandle {
|
||||
if f.handle.IsValid() {
|
||||
return f.handle
|
||||
}
|
||||
if f.manager == nil {
|
||||
return f.handle
|
||||
}
|
||||
client := f.manager.currentClient()
|
||||
if client == nil {
|
||||
return f.handle
|
||||
}
|
||||
hash, err := f.hash()
|
||||
if err != nil {
|
||||
return f.handle
|
||||
}
|
||||
handle := client.FindTransfer(hash)
|
||||
if handle.IsValid() {
|
||||
f.handle = handle
|
||||
}
|
||||
return f.handle
|
||||
}
|
||||
|
||||
func (f *Fetcher) hash() (gprotocol.Hash, error) {
|
||||
if f.meta == nil || f.meta.Req == nil {
|
||||
return gprotocol.Invalid, errors.New("ed2k link is empty")
|
||||
}
|
||||
link, err := parseLink(f.meta.Req.URL)
|
||||
if err != nil {
|
||||
return gprotocol.Invalid, err
|
||||
}
|
||||
return link.Hash, nil
|
||||
}
|
||||
|
||||
type FetcherManager struct {
|
||||
mu sync.Mutex
|
||||
client *goed2k.Client
|
||||
stateStore *clientStateStore
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) SetStateStore(store fetcher.ProtocolStateStore) {
|
||||
fm.mu.Lock()
|
||||
defer fm.mu.Unlock()
|
||||
|
||||
if fm.stateStore == nil {
|
||||
fm.stateStore = &clientStateStore{}
|
||||
}
|
||||
fm.stateStore.store = store
|
||||
if fm.client != nil {
|
||||
fm.client.SetStateStore(fm.stateStore)
|
||||
}
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) Name() string {
|
||||
return "ed2k"
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) Filters() []*fetcher.SchemeFilter {
|
||||
return []*fetcher.SchemeFilter{
|
||||
{
|
||||
Type: fetcher.FilterTypeUrl,
|
||||
Pattern: "ED2K",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) Build() fetcher.Fetcher {
|
||||
return &Fetcher{manager: fm}
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) ParseName(u string) string {
|
||||
link, err := parseLink(u)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return link.StringValue
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) AutoRename() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) DefaultConfig() any {
|
||||
return &config{
|
||||
ListenPort: 0,
|
||||
UDPPort: 0,
|
||||
ServerAddr: defaultServerList,
|
||||
ServerMet: defaultServerMet,
|
||||
NodesDat: defaultNodesDat,
|
||||
}
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) Store(f fetcher.Fetcher) (any, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) Restore() (v any, f func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher) {
|
||||
return nil, func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher {
|
||||
return &Fetcher{
|
||||
manager: fm,
|
||||
meta: meta,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) Close() error {
|
||||
fm.mu.Lock()
|
||||
defer fm.mu.Unlock()
|
||||
|
||||
if fm.client != nil {
|
||||
fm.client.Close()
|
||||
fm.client = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseLink(raw string) (goed2k.EMuleLink, error) {
|
||||
link, err := goed2k.ParseEMuleLink(raw)
|
||||
if err != nil {
|
||||
return goed2k.EMuleLink{}, err
|
||||
}
|
||||
if link.Type != goed2k.LinkFile {
|
||||
return goed2k.EMuleLink{}, errors.New("unsupported ed2k link type")
|
||||
}
|
||||
return link, nil
|
||||
}
|
||||
|
||||
func buildResource(link goed2k.EMuleLink) *base.Resource {
|
||||
return &base.Resource{
|
||||
Size: link.NumberValue,
|
||||
Range: false,
|
||||
Hash: link.Hash.String(),
|
||||
Files: []*base.FileInfo{
|
||||
{
|
||||
Name: link.StringValue,
|
||||
Size: link.NumberValue,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func splitCommaList(value string) []string {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return nil
|
||||
}
|
||||
parts := strings.Split(value, ",")
|
||||
out := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
part = strings.TrimSpace(part)
|
||||
if part != "" {
|
||||
out = append(out, part)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) getStateStoreLocked() *clientStateStore {
|
||||
if fm.stateStore == nil {
|
||||
fm.stateStore = &clientStateStore{}
|
||||
}
|
||||
return fm.stateStore
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) currentClient() *goed2k.Client {
|
||||
fm.mu.Lock()
|
||||
defer fm.mu.Unlock()
|
||||
return fm.client
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) initClient(cfg *config) (*goed2k.Client, error) {
|
||||
fm.mu.Lock()
|
||||
defer fm.mu.Unlock()
|
||||
|
||||
if fm.client != nil {
|
||||
return fm.client, nil
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = fm.DefaultConfig().(*config)
|
||||
}
|
||||
|
||||
settings := goed2k.NewSettings()
|
||||
settings.ListenPort = cfg.ListenPort
|
||||
settings.UDPPort = cfg.UDPPort
|
||||
settings.EnableDHT = true
|
||||
settings.EnableUPnP = true
|
||||
settings.ReconnectToServer = true
|
||||
|
||||
client := goed2k.NewClient(settings)
|
||||
client.SetStateStore(fm.getStateStoreLocked())
|
||||
if err := client.LoadState(""); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := client.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fm.client = client
|
||||
// Bootstrap is best-effort: downloads can still proceed later even if
|
||||
// server list or DHT initialization fails during startup.
|
||||
bootstrapClient(client, cfg)
|
||||
return fm.client, nil
|
||||
}
|
||||
|
||||
func bootstrapClient(client *goed2k.Client, cfg *config) {
|
||||
for _, serverAddr := range splitCommaList(cfg.ServerAddr) {
|
||||
go func(serverAddr string) {
|
||||
_ = client.Connect(serverAddr)
|
||||
}(serverAddr)
|
||||
}
|
||||
for _, source := range splitCommaList(cfg.ServerMet) {
|
||||
go func(source string) {
|
||||
_ = client.ConnectServerMet(source)
|
||||
}(source)
|
||||
}
|
||||
if cfg.NodesDat != "" {
|
||||
go func() {
|
||||
_ = client.LoadDHTNodesDat(cfg.NodesDat)
|
||||
}()
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package ed2k
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
ped2k "github.com/GopeedLab/gopeed/pkg/protocol/ed2k"
|
||||
"github.com/monkeyWie/goed2k"
|
||||
"github.com/monkeyWie/goed2k/protocol"
|
||||
)
|
||||
|
||||
func TestStatsSeparatesSnapshotAndRuntimeWithoutHandle(t *testing.T) {
|
||||
frame := (&Fetcher{}).Stats()
|
||||
if frame.Snapshot != nil {
|
||||
t.Fatalf("snapshot = %T, want nil without a valid handle", frame.Snapshot)
|
||||
}
|
||||
runtime, ok := frame.Runtime.(*ped2k.StatsRuntime)
|
||||
if !ok {
|
||||
t.Fatalf("runtime type = %T", frame.Runtime)
|
||||
}
|
||||
if runtime.Peers == nil || len(runtime.Peers) != 0 {
|
||||
t.Fatalf("unexpected runtime peers: %+v", runtime.Peers)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildED2KPieceMapPreservesIndexes(t *testing.T) {
|
||||
snapshots := []goed2k.PieceSnapshot{
|
||||
{Index: 0, State: goed2k.PieceSnapshotFinished},
|
||||
{Index: 1, State: goed2k.PieceSnapshotMissing},
|
||||
{Index: 2, State: goed2k.PieceSnapshotDownloading},
|
||||
{Index: 3, State: goed2k.PieceSnapshotFinished},
|
||||
}
|
||||
pieceMap := buildED2KPieceMap(snapshots)
|
||||
if pieceMap == nil {
|
||||
t.Fatal("buildED2KPieceMap returned nil")
|
||||
}
|
||||
want := []bool{true, false, false, true}
|
||||
for index, expected := range want {
|
||||
got, err := pieceMap.Get(index)
|
||||
if err != nil || got != expected {
|
||||
t.Fatalf("piece %d = %v, %v; want %v", index, got, err, expected)
|
||||
}
|
||||
}
|
||||
if pieceMap.CompletedPieces() != 2 {
|
||||
t.Fatalf("CompletedPieces() = %d, want 2", pieceMap.CompletedPieces())
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildED2KPeers(t *testing.T) {
|
||||
remotePieces := protocol.NewBitField(4)
|
||||
remotePieces.SetBit(0)
|
||||
remotePieces.SetBit(2)
|
||||
endpoint, err := protocol.EndpointFromString("127.0.0.1", 4662)
|
||||
if err != nil {
|
||||
t.Fatalf("EndpointFromString: %v", err)
|
||||
}
|
||||
peers := buildED2KPeers([]goed2k.PeerInfo{{
|
||||
PayloadDownloadSpeed: 2048,
|
||||
PayloadUploadSpeed: 1024,
|
||||
RemotePieces: remotePieces,
|
||||
Endpoint: endpoint,
|
||||
ModName: "aMule",
|
||||
StrModVersion: "2.3.3",
|
||||
SourceFlag: int(goed2k.PeerServer | goed2k.PeerDHT),
|
||||
}})
|
||||
if len(peers) != 1 {
|
||||
t.Fatalf("len(peers) = %d, want 1", len(peers))
|
||||
}
|
||||
peer := peers[0]
|
||||
if peer.Client != "aMule 2.3.3" || peer.PieceCount != 2 || peer.Source != "server|kad" || peer.Transport != "tcp" {
|
||||
t.Fatalf("unexpected peer: %+v", peer)
|
||||
}
|
||||
}
|
||||
+844
-120
File diff suppressed because it is too large.
Load diff
@@ -14,8 +14,15 @@ import (
|
||||
// ============================================================================
|
||||
|
||||
type fetcherData struct {
|
||||
Connections []*connection
|
||||
RedirectURL string // Saved redirect URL for resume
|
||||
Connections []*connection
|
||||
RedirectURL string // Saved redirect URL for resume
|
||||
IfRange string // Strong ETag or Last-Modified validator for safe resume
|
||||
RangeReprobeEligible bool // Origin advertised Range before falling back to sequential mode
|
||||
RangeValidatorPinned bool // Recovered Range mode must keep the same If-Range validator
|
||||
SequentialSizeUnknown bool // Interrupted chunked restart must retry from byte zero
|
||||
Range *bool // Authoritative Range mode; nil for records saved by older versions
|
||||
ResourceSize *int64 // Authoritative resource size from the same snapshot as Connections
|
||||
FileSize *int64 // Authoritative single-file size; nil for older records
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -75,12 +82,73 @@ func (fm *FetcherManager) Store(f fetcher.Fetcher) (data any, err error) {
|
||||
_f.redirectLock.Lock()
|
||||
redirectURL := _f.redirectURL
|
||||
_f.redirectLock.Unlock()
|
||||
|
||||
// Build an immutable snapshot while holding the same lock used by download
|
||||
// workers to update Range mode and connection progress. The storage layer
|
||||
// marshals this value after Store returns, so returning live pointers would
|
||||
// otherwise race with the active download and could persist a mixed state.
|
||||
_f.connMu.Lock()
|
||||
connections := snapshotConnectionsLocked(_f.connections)
|
||||
ifRange := _f.ifRange
|
||||
rangeReprobeEligible := _f.rangeReprobeEligible
|
||||
rangeValidatorPinned := _f.rangeValidatorPinned
|
||||
sequentialSizeUnknown := _f.sequentialSizeUnknown
|
||||
var rangeMode *bool
|
||||
var resourceSize *int64
|
||||
var fileSize *int64
|
||||
if _f.meta != nil && _f.meta.Res != nil {
|
||||
value := _f.meta.Res.Range
|
||||
rangeMode = &value
|
||||
size := _f.meta.Res.Size
|
||||
resourceSize = &size
|
||||
if len(_f.meta.Res.Files) > 0 && _f.meta.Res.Files[0] != nil {
|
||||
size := _f.meta.Res.Files[0].Size
|
||||
fileSize = &size
|
||||
}
|
||||
}
|
||||
_f.connMu.Unlock()
|
||||
|
||||
return &fetcherData{
|
||||
Connections: _f.connections,
|
||||
RedirectURL: redirectURL,
|
||||
Connections: connections,
|
||||
RedirectURL: redirectURL,
|
||||
IfRange: ifRange,
|
||||
RangeReprobeEligible: rangeReprobeEligible,
|
||||
RangeValidatorPinned: rangeValidatorPinned,
|
||||
SequentialSizeUnknown: sequentialSizeUnknown,
|
||||
Range: rangeMode,
|
||||
ResourceSize: resourceSize,
|
||||
FileSize: fileSize,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// snapshotConnectionsLocked copies only the serialized connection state and
|
||||
// deep-copies chunks. The caller must hold the fetcher's connMu.
|
||||
func snapshotConnectionsLocked(connections []*connection) []*connection {
|
||||
snapshot := make([]*connection, len(connections))
|
||||
for i, conn := range connections {
|
||||
if conn == nil {
|
||||
continue
|
||||
}
|
||||
copyConn := &connection{
|
||||
ID: conn.ID,
|
||||
Role: conn.Role,
|
||||
State: conn.State,
|
||||
Downloaded: conn.Downloaded,
|
||||
Completed: conn.Completed,
|
||||
}
|
||||
if conn.Chunk != nil {
|
||||
copyChunk := *conn.Chunk
|
||||
if conn.Chunk.End != nil {
|
||||
end := *conn.Chunk.End
|
||||
copyChunk.End = &end
|
||||
}
|
||||
copyConn.Chunk = ©Chunk
|
||||
}
|
||||
snapshot[i] = copyConn
|
||||
}
|
||||
return snapshot
|
||||
}
|
||||
|
||||
func (fm *FetcherManager) Restore() (v any, f func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher) {
|
||||
return &fetcherData{}, func(meta *fetcher.FetcherMeta, v any) fetcher.Fetcher {
|
||||
fd := v.(*fetcherData)
|
||||
@@ -96,6 +164,22 @@ func (fm *FetcherManager) Restore() (v any, f func(meta *fetcher.FetcherMeta, v
|
||||
if fd.RedirectURL != "" {
|
||||
fetcher.redirectURL = fd.RedirectURL
|
||||
}
|
||||
fetcher.ifRange = fd.IfRange
|
||||
fetcher.rangeReprobeEligible = fd.RangeReprobeEligible
|
||||
fetcher.rangeValidatorPinned = fd.RangeValidatorPinned
|
||||
fetcher.sequentialSizeUnknown = fd.SequentialSizeUnknown
|
||||
if fd.Range != nil && fetcher.meta.Res != nil {
|
||||
// Recovery-critical resource fields live in the same persisted snapshot
|
||||
// as Connections, making them authoritative over task metadata that may
|
||||
// have been saved just before or after this record.
|
||||
fetcher.meta.Res.Range = *fd.Range
|
||||
}
|
||||
if fd.ResourceSize != nil && fetcher.meta.Res != nil {
|
||||
fetcher.meta.Res.Size = *fd.ResourceSize
|
||||
}
|
||||
if fd.FileSize != nil && fetcher.meta.Res != nil && len(fetcher.meta.Res.Files) > 0 && fetcher.meta.Res.Files[0] != nil {
|
||||
fetcher.meta.Res.Files[0].Size = *fd.FileSize
|
||||
}
|
||||
return fetcher
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -136,3 +136,52 @@ func TestUnescapeHTMLEntities(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendFilenameExtension(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
filename string
|
||||
contentType string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "adds extension from content type",
|
||||
filename: "download",
|
||||
contentType: "image/png",
|
||||
want: "download.png",
|
||||
},
|
||||
{
|
||||
name: "parses MIME parameters",
|
||||
filename: "download",
|
||||
contentType: "image/png; charset=binary",
|
||||
want: "download.png",
|
||||
},
|
||||
{
|
||||
name: "preserves existing extension",
|
||||
filename: "download.zip",
|
||||
contentType: "application/zip",
|
||||
want: "download.zip",
|
||||
},
|
||||
{
|
||||
name: "ignores invalid content type",
|
||||
filename: "download",
|
||||
contentType: "not a content type",
|
||||
want: "download",
|
||||
},
|
||||
{
|
||||
name: "ignores unknown content type",
|
||||
filename: "download",
|
||||
contentType: "application/x-gopeed-unknown",
|
||||
want: "download",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := appendFilenameExtension(tt.filename, tt.contentType)
|
||||
if got != tt.want {
|
||||
t.Errorf("appendFilenameExtension() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -12,10 +12,12 @@ import (
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/GopeedLab/gopeed/internal/httpclient"
|
||||
"github.com/GopeedLab/gopeed/pkg/base"
|
||||
fhttp "github.com/GopeedLab/gopeed/pkg/protocol/http"
|
||||
"github.com/GopeedLab/gopeed/pkg/util"
|
||||
@@ -26,6 +28,45 @@ type RequestError struct {
|
||||
Code int
|
||||
}
|
||||
|
||||
// extractIfRangeValidator returns only validators that are strong enough for
|
||||
// If-Range. Any entity-tag header blocks Last-Modified fallback: a weak or
|
||||
// malformed ETag does not make the date validator strong.
|
||||
func extractIfRangeValidator(header http.Header) string {
|
||||
etag := strings.TrimSpace(header.Get(base.HttpHeaderETag))
|
||||
if etag != "" {
|
||||
if isStrongETag(etag) {
|
||||
return etag
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
lastModified := strings.TrimSpace(header.Get(base.HttpHeaderLastModified))
|
||||
date := strings.TrimSpace(header.Get("Date"))
|
||||
if lastModified == "" || date == "" {
|
||||
return ""
|
||||
}
|
||||
modifiedAt, modifiedErr := http.ParseTime(lastModified)
|
||||
responseAt, responseErr := http.ParseTime(date)
|
||||
if modifiedErr != nil || responseErr != nil || responseAt.Sub(modifiedAt) < time.Second {
|
||||
return ""
|
||||
}
|
||||
return lastModified
|
||||
}
|
||||
|
||||
func isStrongETag(value string) bool {
|
||||
if len(value) < 2 || value[0] != '"' || value[len(value)-1] != '"' {
|
||||
return false
|
||||
}
|
||||
for i := 1; i < len(value)-1; i++ {
|
||||
b := value[i]
|
||||
if b == 0x21 || (b >= 0x23 && b <= 0x7e) || b >= 0x80 {
|
||||
continue
|
||||
}
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func NewRequestError(code int) *RequestError {
|
||||
return &RequestError{Code: code}
|
||||
}
|
||||
@@ -165,15 +206,17 @@ func isRedirectExpiredError(err error) bool {
|
||||
|
||||
// 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) {
|
||||
func (f *Fetcher) tryFallbackToOriginalURL(ctx context.Context, client *http.Client, rangeStart int64, rangeEnd *int64, rangeRequested bool, ifRange string) (*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))
|
||||
if rangeRequested {
|
||||
httpReq.Header.Set(base.HttpHeaderRange, formatRangeHeader(rangeStart, rangeEnd))
|
||||
if ifRange != "" {
|
||||
httpReq.Header.Set(base.HttpHeaderIfRange, ifRange)
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := client.Do(httpReq)
|
||||
@@ -189,6 +232,13 @@ func (f *Fetcher) tryFallbackToOriginalURL(ctx context.Context, client *http.Cli
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func formatRangeHeader(start int64, end *int64) string {
|
||||
if end == nil {
|
||||
return fmt.Sprintf("bytes=%d-", start)
|
||||
}
|
||||
return fmt.Sprintf(base.HttpHeaderRangeFormat, start, *end)
|
||||
}
|
||||
|
||||
// buildClient creates an HTTP client with the default connection timeout.
|
||||
// Used for resolve phase where we don't have connection time data yet.
|
||||
func (f *Fetcher) buildClient() *http.Client {
|
||||
@@ -216,21 +266,30 @@ func (f *Fetcher) buildFastFailClient() *http.Client {
|
||||
|
||||
// buildClientWithTimeout creates an HTTP client with the specified connection timeout.
|
||||
func (f *Fetcher) buildClientWithTimeout(timeout time.Duration) *http.Client {
|
||||
transport := &http.Transport{
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: timeout,
|
||||
}).DialContext,
|
||||
Proxy: f.ctl.GetProxy(f.meta.Req.Proxy),
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: f.meta.Req.SkipVerifyCert,
|
||||
},
|
||||
TLSHandshakeTimeout: timeout,
|
||||
}
|
||||
jar, _ := cookiejar.New(nil)
|
||||
return &http.Client{
|
||||
Transport: transport,
|
||||
Jar: jar,
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Client: httpclient.ClientOptions{
|
||||
Jar: jar,
|
||||
},
|
||||
Transport: httpclient.TransportOptions{
|
||||
DialContext: (&net.Dialer{
|
||||
Timeout: timeout,
|
||||
}).DialContext,
|
||||
Proxy: f.ctl.GetProxy(f.meta.Req.Proxy),
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: f.meta.Req.SkipVerifyCert,
|
||||
},
|
||||
TLSHandshakeTimeout: timeout,
|
||||
},
|
||||
Impersonation: httpclient.ImpersonationOptions{
|
||||
Mode: httpclient.ImpersonationAuto,
|
||||
Session: f.impersonationSession,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("build HTTP client: %v", err))
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
@@ -256,6 +315,28 @@ func parseFilename(contentDisposition string) string {
|
||||
return parseFilenameFallback(contentDisposition)
|
||||
}
|
||||
|
||||
// appendFilenameExtension adds a MIME-derived extension when the resolved
|
||||
// filename has no usable extension. Some image hosts use opaque path segments
|
||||
// that look like extensions, even though the response Content-Type is the
|
||||
// only reliable source for the file type.
|
||||
func appendFilenameExtension(filename, contentType string) string {
|
||||
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil {
|
||||
return filename
|
||||
}
|
||||
|
||||
extensions, err := mime.ExtensionsByType(mediaType)
|
||||
if err != nil || len(extensions) == 0 {
|
||||
return filename
|
||||
}
|
||||
|
||||
ext := path.Ext(filename)
|
||||
if len(ext) > 1 && len(ext) <= 20 {
|
||||
return filename
|
||||
}
|
||||
return filename + extensions[0]
|
||||
}
|
||||
|
||||
// parseFilenameExtended handles RFC 5987 extended notation (filename*=)
|
||||
// Format: filename*=charset'language'value (e.g., UTF-8”%E6%B5%8B%E8%AF%95.zip)
|
||||
func parseFilenameExtended(cd string) string {
|
||||
|
||||
+88
-19
@@ -335,6 +335,82 @@ 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
|
||||
})
|
||||
}
|
||||
|
||||
// StartTestIgnoredRangeServer advertises byte-range support but ignores Range
|
||||
// requests and always returns the full representation with 200 OK.
|
||||
func StartTestIgnoredRangeServer(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))
|
||||
writer.Header().Set("Accept-Ranges", "bytes")
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
if flusher, ok := writer.(http.Flusher); ok {
|
||||
flusher.Flush()
|
||||
}
|
||||
|
||||
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 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
|
||||
@@ -396,9 +472,8 @@ func StartTestExpiringRedirectServer(requestsBeforeExpire int32, delayPerByte ti
|
||||
})
|
||||
}
|
||||
|
||||
// StartTestSlowStartServer creates a server with configurable delay per request
|
||||
// This allows testing slow-start connection expansion to reach max connections
|
||||
func StartTestSlowStartServer(delayPerByte time.Duration) net.Listener {
|
||||
// StartTestLowSpeedServer creates a server with configurable delay per request
|
||||
func StartTestLowSpeedServer(delayPerByte time.Duration) net.Listener {
|
||||
return startTestServer(func(sl *shutdownListener) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/"+BuildName, func(writer http.ResponseWriter, request *http.Request) {
|
||||
@@ -447,10 +522,9 @@ func slowCopyNWithDelay(sl *shutdownListener, dst io.Writer, src io.Reader, n in
|
||||
}
|
||||
}
|
||||
|
||||
// StartTestLimitServer connections limit server
|
||||
func StartTestLimitServer(maxConnections int32, delay int64) net.Listener {
|
||||
// StartTestConLimitServer connections limit server
|
||||
func StartTestConLimitServer(maxConnections int32) net.Listener {
|
||||
var connections atomic.Int32
|
||||
var slowOnce atomic.Bool
|
||||
|
||||
return startTestServer(func(sl *shutdownListener) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
@@ -463,19 +537,11 @@ func StartTestLimitServer(maxConnections int32, delay int64) net.Listener {
|
||||
writer.WriteHeader(403)
|
||||
return
|
||||
}
|
||||
|
||||
// First request intentionally delays the first write to trigger a single read timeout,
|
||||
// subsequent requests respond at normal speed.
|
||||
useInitialDelay := delay > 0 && !slowOnce.Swap(true)
|
||||
rangeFileHandle(
|
||||
writer,
|
||||
request,
|
||||
nil,
|
||||
func(file *os.File, n int64) {
|
||||
if useInitialDelay {
|
||||
slowCopyAfterDelay(sl, writer, file, n, delay)
|
||||
return
|
||||
}
|
||||
slowCopyN(sl, writer, file, n, 0)
|
||||
},
|
||||
)
|
||||
@@ -674,10 +740,13 @@ func rangeFileHandle(writer http.ResponseWriter, request *http.Request, modifyEn
|
||||
writer.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
end, err := strconv.ParseInt(s[1], 10, 64)
|
||||
if err != nil {
|
||||
writer.WriteHeader(400)
|
||||
return
|
||||
end := int64(BuildSize - 1)
|
||||
if s[1] != "" {
|
||||
end, err = strconv.ParseInt(s[1], 10, 64)
|
||||
if err != nil {
|
||||
writer.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
}
|
||||
if start < 0 || end < 0 || start > end {
|
||||
writer.WriteHeader(400)
|
||||
@@ -843,7 +912,7 @@ func StartSocks5Server(usr, pwd string) net.Listener {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0") // 你可以根据需要更改监听地址
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
//go:build darwin
|
||||
|
||||
package goprovider
|
||||
|
||||
var mainThreadTasks chan func()
|
||||
|
||||
func RunMainThreadLoop(run func() int) int {
|
||||
if mainThreadTasks != nil {
|
||||
return run()
|
||||
}
|
||||
|
||||
mainThreadTasks = make(chan func())
|
||||
resultCh := make(chan int, 1)
|
||||
|
||||
go func() {
|
||||
resultCh <- run()
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case task := <-mainThreadTasks:
|
||||
task()
|
||||
case code := <-resultCh:
|
||||
mainThreadTasks = nil
|
||||
return code
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func postMainThreadTask(task func()) bool {
|
||||
if mainThreadTasks == nil {
|
||||
return false
|
||||
}
|
||||
mainThreadTasks <- task
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
//go:build !darwin
|
||||
|
||||
package goprovider
|
||||
|
||||
func RunMainThreadLoop(run func() int) int {
|
||||
return run()
|
||||
}
|
||||
|
||||
func postMainThreadTask(func()) bool {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,612 @@
|
||||
//go:build cgo
|
||||
|
||||
package goprovider
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
runtimepkg "runtime"
|
||||
|
||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||
webview "github.com/GopeedLab/webview_go"
|
||||
)
|
||||
|
||||
type Provider struct{}
|
||||
|
||||
func New() enginewebview.Provider {
|
||||
return &Provider{}
|
||||
}
|
||||
|
||||
func (p *Provider) IsAvailable() bool {
|
||||
return webview.IsAvailable()
|
||||
}
|
||||
|
||||
func (p *Provider) Open(opts enginewebview.OpenOptions) (enginewebview.Page, error) {
|
||||
pw := newPageWrapper(opts)
|
||||
if err := pw.start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pw, nil
|
||||
}
|
||||
|
||||
type pageWrapper struct {
|
||||
opts enginewebview.OpenOptions
|
||||
|
||||
callbackName string
|
||||
readyID string
|
||||
|
||||
view webview.WebView
|
||||
|
||||
mu sync.Mutex
|
||||
pending map[string]chan evalResult
|
||||
url string
|
||||
closed bool
|
||||
|
||||
ready chan error
|
||||
done chan struct{}
|
||||
loads chan string
|
||||
}
|
||||
|
||||
type evalMessage struct {
|
||||
ID string `json:"id"`
|
||||
Ready bool `json:"ready,omitempty"`
|
||||
Load bool `json:"load,omitempty"`
|
||||
URL string `json:"url,omitempty"`
|
||||
OK bool `json:"ok,omitempty"`
|
||||
Value any `json:"value,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type evalResult struct {
|
||||
value any
|
||||
err error
|
||||
}
|
||||
|
||||
type nativeCookieJSON struct {
|
||||
Name string `json:"name"`
|
||||
Value string `json:"value"`
|
||||
Domain string `json:"domain"`
|
||||
Path string `json:"path"`
|
||||
Expires string `json:"expires"`
|
||||
Secure bool `json:"secure"`
|
||||
HTTPOnly bool `json:"httpOnly"`
|
||||
}
|
||||
|
||||
var providerSeq atomic.Uint64
|
||||
|
||||
// Keep native page creation aligned with the provider availability budget used
|
||||
// by the integration contract. Cold WebView2 startup can exceed ten seconds.
|
||||
const startupTimeout = time.Minute
|
||||
|
||||
func newPageWrapper(opts enginewebview.OpenOptions) *pageWrapper {
|
||||
seq := providerSeq.Add(1)
|
||||
return &pageWrapper{
|
||||
opts: opts,
|
||||
callbackName: fmt.Sprintf("__gopeedWebViewCallback_%d", seq),
|
||||
readyID: fmt.Sprintf("__ready__%d", seq),
|
||||
pending: make(map[string]chan evalResult),
|
||||
ready: make(chan error, 1),
|
||||
done: make(chan struct{}),
|
||||
loads: make(chan string, 8),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pageWrapper) start() error {
|
||||
startedAt := time.Now()
|
||||
run := func() {
|
||||
runtimepkg.LockOSThread()
|
||||
defer runtimepkg.UnlockOSThread()
|
||||
|
||||
var w webview.WebView
|
||||
if p.opts.Headless {
|
||||
w = webview.NewHeadless(p.opts.Debug)
|
||||
} else {
|
||||
w = webview.New(p.opts.Debug)
|
||||
}
|
||||
p.view = w
|
||||
w.SetTitle(firstNonEmpty(p.opts.Title, "Gopeed WebView"))
|
||||
w.SetSize(defaultWindowDimension(p.opts.Width, 1280), defaultWindowDimension(p.opts.Height, 800), webview.HintNone)
|
||||
|
||||
if p.opts.UserAgent != "" {
|
||||
type userAgentSetter interface {
|
||||
SetUserAgent(userAgent string)
|
||||
}
|
||||
if setter, ok := w.(userAgentSetter); ok {
|
||||
setter.SetUserAgent(p.opts.UserAgent)
|
||||
}
|
||||
}
|
||||
w.Init(buildLoadNotifyScript(p.callbackName))
|
||||
|
||||
if err := w.Bind(p.callbackName, func(payload string) error {
|
||||
return p.handleCallback(payload)
|
||||
}); err != nil {
|
||||
p.ready <- err
|
||||
return
|
||||
}
|
||||
|
||||
applyWindowOptions(w, p.opts)
|
||||
w.SetHtml(buildBootstrapHTML(p.callbackName, p.readyID))
|
||||
w.Run()
|
||||
w.Destroy()
|
||||
close(p.done)
|
||||
}
|
||||
|
||||
if !postMainThreadTask(run) {
|
||||
go run()
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-p.ready:
|
||||
p.tracef("open %dms", time.Since(startedAt).Milliseconds())
|
||||
return err
|
||||
case <-time.After(startupTimeout):
|
||||
p.tracef("open timeout %dms", time.Since(startedAt).Milliseconds())
|
||||
return fmt.Errorf("webview startup timeout")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pageWrapper) AddInitScript(script string) error {
|
||||
return p.dispatch(func(w webview.WebView) error {
|
||||
w.Init(script)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (p *pageWrapper) Goto(url string, opts enginewebview.GotoOptions) error {
|
||||
startedAt := time.Now()
|
||||
p.drainLoads()
|
||||
if err := p.dispatch(func(w webview.WebView) error {
|
||||
p.mu.Lock()
|
||||
p.url = url
|
||||
p.mu.Unlock()
|
||||
w.Navigate(url)
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
timeout := 30 * time.Second
|
||||
if opts.TimeoutMS > 0 {
|
||||
timeout = time.Duration(opts.TimeoutMS) * time.Millisecond
|
||||
}
|
||||
waitUntil := opts.WaitUntil
|
||||
if waitUntil == "" {
|
||||
waitUntil = "load"
|
||||
}
|
||||
err := p.waitForNavigation(url, timeout, waitUntil)
|
||||
if err != nil {
|
||||
p.tracef("goto failed %dms url=%s err=%v", time.Since(startedAt).Milliseconds(), url, err)
|
||||
return err
|
||||
}
|
||||
p.tracef("goto %dms url=%s", time.Since(startedAt).Milliseconds(), url)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pageWrapper) Execute(expression string, args ...any) (any, error) {
|
||||
startedAt := time.Now()
|
||||
if args == nil {
|
||||
args = []any{}
|
||||
}
|
||||
argsJSON, err := json.Marshal(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reqID := fmt.Sprintf("req-%d", providerSeq.Add(1))
|
||||
ch := make(chan evalResult, 1)
|
||||
|
||||
p.mu.Lock()
|
||||
if p.closed {
|
||||
p.mu.Unlock()
|
||||
return nil, fmt.Errorf("webview page is closed")
|
||||
}
|
||||
p.pending[reqID] = ch
|
||||
p.mu.Unlock()
|
||||
|
||||
script := buildExecuteScript(p.callbackName, reqID, expression, string(argsJSON))
|
||||
if err := p.dispatch(func(w webview.WebView) error {
|
||||
w.Eval(script)
|
||||
return nil
|
||||
}); err != nil {
|
||||
p.mu.Lock()
|
||||
delete(p.pending, reqID)
|
||||
p.mu.Unlock()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := <-ch
|
||||
if result.err != nil {
|
||||
p.tracef("execute failed %dms err=%v", time.Since(startedAt).Milliseconds(), result.err)
|
||||
return result.value, result.err
|
||||
}
|
||||
p.tracef("execute %dms", time.Since(startedAt).Milliseconds())
|
||||
return result.value, nil
|
||||
}
|
||||
|
||||
func (p *pageWrapper) GetCookies() ([]enginewebview.Cookie, error) {
|
||||
cookies, err := p.nativeCookies()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]enginewebview.Cookie, 0, len(cookies))
|
||||
for _, cookie := range cookies {
|
||||
item := enginewebview.Cookie{
|
||||
Name: cookie.Name,
|
||||
Value: cookie.Value,
|
||||
Domain: cookie.Domain,
|
||||
Path: cookie.Path,
|
||||
Secure: cookie.Secure,
|
||||
HTTPOnly: cookie.HTTPOnly,
|
||||
}
|
||||
if cookie.Expires != "" {
|
||||
if parsed, err := time.Parse(time.RFC3339Nano, cookie.Expires); err == nil {
|
||||
item.Expires = parsed
|
||||
}
|
||||
}
|
||||
result = append(result, item)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p *pageWrapper) SetCookie(cookie enginewebview.Cookie) error {
|
||||
type cookieSetter interface {
|
||||
SetCookie(cookie webview.Cookie) error
|
||||
}
|
||||
return p.dispatch(func(w webview.WebView) error {
|
||||
setter, ok := w.(cookieSetter)
|
||||
if !ok {
|
||||
return fmt.Errorf("native cookie access is unavailable")
|
||||
}
|
||||
return setter.SetCookie(webview.Cookie{
|
||||
Name: cookie.Name,
|
||||
Value: cookie.Value,
|
||||
Domain: cookie.Domain,
|
||||
Path: cookie.Path,
|
||||
Expires: cookie.Expires,
|
||||
Secure: cookie.Secure,
|
||||
HTTPOnly: cookie.HTTPOnly,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (p *pageWrapper) DeleteCookie(cookie enginewebview.Cookie) error {
|
||||
type cookieDeleter interface {
|
||||
DeleteCookie(name string, domain string, path string) error
|
||||
}
|
||||
return p.dispatch(func(w webview.WebView) error {
|
||||
deleter, ok := w.(cookieDeleter)
|
||||
if !ok {
|
||||
return fmt.Errorf("native cookie access is unavailable")
|
||||
}
|
||||
return deleter.DeleteCookie(cookie.Name, cookie.Domain, cookie.Path)
|
||||
})
|
||||
}
|
||||
|
||||
func (p *pageWrapper) ClearCookies() error {
|
||||
type cookieClearer interface {
|
||||
ClearCookies() error
|
||||
}
|
||||
return p.dispatch(func(w webview.WebView) error {
|
||||
clearer, ok := w.(cookieClearer)
|
||||
if !ok {
|
||||
return fmt.Errorf("native cookie access is unavailable")
|
||||
}
|
||||
return clearer.ClearCookies()
|
||||
})
|
||||
}
|
||||
|
||||
func (p *pageWrapper) Close() error {
|
||||
p.mu.Lock()
|
||||
if p.closed {
|
||||
p.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
p.closed = true
|
||||
w := p.view
|
||||
wait := p.done
|
||||
p.mu.Unlock()
|
||||
|
||||
if w != nil {
|
||||
// PostQuitMessage (used by the Windows WebView2 terminate_impl)
|
||||
// only takes effect on the thread that owns the message loop.
|
||||
// Dispatch the termination to the webview's UI thread so that
|
||||
// the quit message lands on the correct queue.
|
||||
terminateDone := make(chan struct{})
|
||||
w.Dispatch(func() {
|
||||
w.Terminate()
|
||||
close(terminateDone)
|
||||
})
|
||||
<-terminateDone
|
||||
}
|
||||
<-wait
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pageWrapper) tracef(format string, args ...any) {
|
||||
if !strings.Contains(p.opts.Title, "youtube-sabr") {
|
||||
return
|
||||
}
|
||||
fmt.Printf("goprovider "+format+"\n", args...)
|
||||
}
|
||||
|
||||
func (p *pageWrapper) waitForNavigation(targetURL string, timeout time.Duration, waitUntil string) error {
|
||||
deadline := time.Now().Add(timeout)
|
||||
ticker := time.NewTicker(100 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-p.loads:
|
||||
if waitUntil == "load" {
|
||||
return nil
|
||||
}
|
||||
case <-ticker.C:
|
||||
state, err := p.navigationState()
|
||||
if err == nil && state.ready(targetURL, waitUntil) {
|
||||
return nil
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
return fmt.Errorf("webview navigation timeout")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type navigationState struct {
|
||||
URL string `json:"url"`
|
||||
ReadyState string `json:"readyState"`
|
||||
}
|
||||
|
||||
func (s navigationState) ready(targetURL string, waitUntil string) bool {
|
||||
if s.URL == "" || s.URL == "about:blank" {
|
||||
return false
|
||||
}
|
||||
switch waitUntil {
|
||||
case "domcontentloaded":
|
||||
if s.ReadyState == "loading" || s.ReadyState == "" {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
if s.ReadyState != "complete" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if targetURL == "" {
|
||||
return true
|
||||
}
|
||||
if s.URL == targetURL {
|
||||
return true
|
||||
}
|
||||
target, err := parseURL(targetURL)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
current, err := parseURL(s.URL)
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
if current.Scheme == target.Scheme && current.Host == target.Host && current.Path == target.Path {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func parseURL(raw string) (*url.URL, error) {
|
||||
return url.Parse(raw)
|
||||
}
|
||||
|
||||
func (p *pageWrapper) navigationState() (navigationState, error) {
|
||||
value, err := p.Execute(`() => ({
|
||||
url: String(location.href || ""),
|
||||
readyState: document.readyState || "",
|
||||
})`)
|
||||
if err != nil {
|
||||
return navigationState{}, err
|
||||
}
|
||||
stateMap, ok := value.(map[string]any)
|
||||
if !ok {
|
||||
return navigationState{}, fmt.Errorf("invalid navigation state")
|
||||
}
|
||||
state := navigationState{}
|
||||
if urlValue, ok := stateMap["url"].(string); ok {
|
||||
state.URL = urlValue
|
||||
}
|
||||
if readyValue, ok := stateMap["readyState"].(string); ok {
|
||||
state.ReadyState = readyValue
|
||||
}
|
||||
return state, nil
|
||||
}
|
||||
|
||||
func (p *pageWrapper) dispatch(fn func(w webview.WebView) error) error {
|
||||
p.mu.Lock()
|
||||
if p.closed {
|
||||
p.mu.Unlock()
|
||||
return fmt.Errorf("webview page is closed")
|
||||
}
|
||||
w := p.view
|
||||
p.mu.Unlock()
|
||||
if w == nil {
|
||||
return fmt.Errorf("webview is not initialized")
|
||||
}
|
||||
|
||||
done := make(chan error, 1)
|
||||
w.Dispatch(func() {
|
||||
done <- fn(w)
|
||||
})
|
||||
return <-done
|
||||
}
|
||||
|
||||
func (p *pageWrapper) nativeCookies() ([]nativeCookieJSON, error) {
|
||||
type cookieGetter interface {
|
||||
GetCookies(url string) ([]webview.Cookie, error)
|
||||
}
|
||||
|
||||
var cookies []webview.Cookie
|
||||
err := p.dispatch(func(w webview.WebView) error {
|
||||
getter, ok := w.(cookieGetter)
|
||||
if !ok {
|
||||
return fmt.Errorf("native cookie access is unavailable")
|
||||
}
|
||||
p.mu.Lock()
|
||||
url := p.url
|
||||
p.mu.Unlock()
|
||||
if url == "" {
|
||||
url = "about:blank"
|
||||
}
|
||||
var err error
|
||||
cookies, err = getter.GetCookies(url)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]nativeCookieJSON, 0, len(cookies))
|
||||
for _, cookie := range cookies {
|
||||
item := nativeCookieJSON{
|
||||
Name: cookie.Name,
|
||||
Value: cookie.Value,
|
||||
Domain: cookie.Domain,
|
||||
Path: cookie.Path,
|
||||
Secure: cookie.Secure,
|
||||
HTTPOnly: cookie.HTTPOnly,
|
||||
}
|
||||
if !cookie.Expires.IsZero() {
|
||||
item.Expires = cookie.Expires.UTC().Format(time.RFC3339Nano)
|
||||
}
|
||||
result = append(result, item)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p *pageWrapper) handleCallback(payload string) error {
|
||||
var msg evalMessage
|
||||
if err := json.Unmarshal([]byte(payload), &msg); err != nil {
|
||||
return err
|
||||
}
|
||||
if msg.Ready && msg.ID == p.readyID {
|
||||
select {
|
||||
case p.ready <- nil:
|
||||
default:
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if msg.Load {
|
||||
select {
|
||||
case p.loads <- msg.URL:
|
||||
default:
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
p.mu.Lock()
|
||||
ch := p.pending[msg.ID]
|
||||
delete(p.pending, msg.ID)
|
||||
p.mu.Unlock()
|
||||
if ch == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !msg.OK {
|
||||
ch <- evalResult{err: errors.New(strings.TrimSpace(msg.Error))}
|
||||
return nil
|
||||
}
|
||||
ch <- evalResult{value: msg.Value}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pageWrapper) drainLoads() {
|
||||
for {
|
||||
select {
|
||||
case <-p.loads:
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func buildBootstrapHTML(callbackName string, readyID string) string {
|
||||
return fmt.Sprintf(`<!doctype html>
|
||||
<html>
|
||||
<head><meta charset="utf-8"></head>
|
||||
<body>
|
||||
<script>
|
||||
window.addEventListener("load", () => {
|
||||
const callback = globalThis[%q];
|
||||
if (typeof callback === "function") {
|
||||
callback(JSON.stringify({ id: %q, ready: true }));
|
||||
}
|
||||
}, { once: true });
|
||||
</script>
|
||||
</body>
|
||||
</html>`, callbackName, readyID)
|
||||
}
|
||||
|
||||
func buildExecuteScript(callbackName string, reqID string, expression string, argsJSON string) string {
|
||||
callbackJSON, _ := json.Marshal(callbackName)
|
||||
reqJSON, _ := json.Marshal(reqID)
|
||||
exprJSON, _ := json.Marshal(expression)
|
||||
argsLiteral := argsJSON
|
||||
if argsLiteral == "" || argsLiteral == "null" {
|
||||
argsLiteral = "[]"
|
||||
}
|
||||
return fmt.Sprintf(`(() => {
|
||||
const __cb = globalThis[%s];
|
||||
const __id = %s;
|
||||
const __expression = %s;
|
||||
const __args = %s;
|
||||
Promise.resolve()
|
||||
.then(async () => {
|
||||
const __target = (0, eval)(__expression);
|
||||
const __value = await (typeof __target === "function" ? __target(...__args) : __target);
|
||||
return { id: __id, ok: true, value: __value ?? null };
|
||||
})
|
||||
.catch((error) => ({
|
||||
id: __id,
|
||||
ok: false,
|
||||
error: [
|
||||
error && error.name ? error.name : "Error",
|
||||
error && error.message ? error.message : String(error),
|
||||
error && error.stack ? error.stack : "",
|
||||
].filter(Boolean).join(": ").replace(": @", "\n@"),
|
||||
}))
|
||||
.then((message) => __cb(JSON.stringify(message)));
|
||||
})();`, string(callbackJSON), string(reqJSON), string(exprJSON), argsLiteral)
|
||||
}
|
||||
|
||||
func buildLoadNotifyScript(callbackName string) string {
|
||||
callbackJSON, _ := json.Marshal(callbackName)
|
||||
return fmt.Sprintf(`(() => {
|
||||
window.addEventListener("load", () => {
|
||||
const __cb = globalThis[%s];
|
||||
if (typeof __cb !== "function") {
|
||||
return;
|
||||
}
|
||||
__cb(JSON.stringify({
|
||||
load: true,
|
||||
url: String(location.href || ""),
|
||||
}));
|
||||
}, { once: true });
|
||||
})();`, string(callbackJSON))
|
||||
}
|
||||
|
||||
func defaultWindowDimension(value int, fallback int) int {
|
||||
if value > 0 {
|
||||
return value
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, value := range values {
|
||||
if strings.TrimSpace(value) != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
//go:build cgo && webview && !darwin
|
||||
|
||||
package goprovider
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/GopeedLab/gopeed/internal/webview/integrationtest"
|
||||
)
|
||||
|
||||
func TestProviderContract(t *testing.T) {
|
||||
integrationtest.RunProviderContract(t, New(), integrationtest.ContractOptions{
|
||||
CookieDomainMode: integrationtest.CookieDomainModeRequired,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//go:build !cgo
|
||||
|
||||
package goprovider
|
||||
|
||||
import enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||
|
||||
func New() enginewebview.Provider {
|
||||
return enginewebview.NewUnavailableProvider()
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//go:build cgo && darwin
|
||||
|
||||
package goprovider
|
||||
|
||||
/*
|
||||
#cgo darwin CFLAGS: -x objective-c
|
||||
#cgo darwin LDFLAGS: -framework Cocoa
|
||||
|
||||
#include <Cocoa/Cocoa.h>
|
||||
|
||||
static void gopeedApplyHeadlessWindowOptions(void *windowPtr, int headless) {
|
||||
if (windowPtr == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSWindow *window = (__bridge NSWindow *)windowPtr;
|
||||
if (headless) {
|
||||
[window orderOut:nil];
|
||||
}
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||
webview "github.com/GopeedLab/webview_go"
|
||||
)
|
||||
|
||||
func applyWindowOptions(w webview.WebView, opts enginewebview.OpenOptions) {
|
||||
C.gopeedApplyHeadlessWindowOptions(w.Window(), C.int(boolToInt(opts.Headless)))
|
||||
}
|
||||
|
||||
func boolToInt(value bool) int {
|
||||
if value {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
//go:build cgo && !darwin
|
||||
|
||||
package goprovider
|
||||
|
||||
import (
|
||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||
webview "github.com/GopeedLab/webview_go"
|
||||
)
|
||||
|
||||
func applyWindowOptions(_ webview.WebView, _ enginewebview.OpenOptions) {}
|
||||
@@ -0,0 +1,382 @@
|
||||
package integrationtest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||
)
|
||||
|
||||
const TestUserAgent = "GopeedWebViewIntegration/1.0"
|
||||
|
||||
// Providers may need to initialize an external host or a native browser runtime
|
||||
// before the first page can be created.
|
||||
const defaultAvailabilityTimeout = time.Minute
|
||||
|
||||
type ContractOptions struct {
|
||||
AvailabilityTimeout time.Duration
|
||||
CookieDomainMode CookieDomainMode
|
||||
CookieTestURL string
|
||||
}
|
||||
|
||||
type CookieDomainMode int
|
||||
|
||||
const (
|
||||
CookieDomainModeRequired CookieDomainMode = iota
|
||||
CookieDomainModeOmit
|
||||
)
|
||||
|
||||
func RunProviderContract(t *testing.T, provider enginewebview.Provider, opts ContractOptions) {
|
||||
t.Helper()
|
||||
|
||||
timeout := opts.AvailabilityTimeout
|
||||
if timeout <= 0 {
|
||||
timeout = defaultAvailabilityTimeout
|
||||
}
|
||||
waitForProviderAvailable(t, provider, timeout)
|
||||
|
||||
page, server, testURL := openTestPage(t, provider, enginewebview.OpenOptions{
|
||||
Headless: true,
|
||||
Title: "Gopeed WebView Integration",
|
||||
Width: 640,
|
||||
Height: 480,
|
||||
UserAgent: TestUserAgent,
|
||||
})
|
||||
|
||||
if err := page.Goto(testURL, map[string]any{"timeoutMs": 10000}); err != nil {
|
||||
t.Fatalf("goto failed: %v", err)
|
||||
}
|
||||
|
||||
ua, err := page.Execute(`() => navigator.userAgent`)
|
||||
if err != nil {
|
||||
t.Fatalf("navigator.userAgent execute failed: %v", err)
|
||||
}
|
||||
if ua != TestUserAgent {
|
||||
t.Fatalf("unexpected user agent: got %v want %q", ua, TestUserAgent)
|
||||
}
|
||||
|
||||
if err := page.Focus("#name"); err != nil {
|
||||
t.Fatalf("focus failed: %v", err)
|
||||
}
|
||||
activeElement, err := page.Execute(`() => document.activeElement && document.activeElement.id`)
|
||||
if err != nil {
|
||||
t.Fatalf("activeElement execute failed: %v", err)
|
||||
}
|
||||
if activeElement != "name" {
|
||||
t.Fatalf("expected active element to be #name, got %v", activeElement)
|
||||
}
|
||||
|
||||
if err := page.Type("#name", "Gopeed", map[string]any{"delay": 5}); err != nil {
|
||||
t.Fatalf("type failed: %v", err)
|
||||
}
|
||||
typeState, err := page.Execute(`() => ({
|
||||
value: document.querySelector('#name').value,
|
||||
mirror: document.querySelector('#name-value').textContent,
|
||||
inputCount: window.__inputEvents.length
|
||||
})`)
|
||||
if err != nil {
|
||||
t.Fatalf("type state execute failed: %v", err)
|
||||
}
|
||||
stateMap, ok := typeState.(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected type state payload: %T", typeState)
|
||||
}
|
||||
if stateMap["value"] != "Gopeed" || stateMap["mirror"] != "Gopeed" {
|
||||
t.Fatalf("unexpected type state: %#v", stateMap)
|
||||
}
|
||||
if stateMap["inputCount"] != float64(len("Gopeed")) {
|
||||
t.Fatalf("unexpected input event count: %#v", stateMap["inputCount"])
|
||||
}
|
||||
|
||||
if err := page.Click("#clicker"); err != nil {
|
||||
t.Fatalf("click failed: %v", err)
|
||||
}
|
||||
clickCount, err := page.Execute(`() => document.querySelector('#click-count').textContent`)
|
||||
if err != nil {
|
||||
t.Fatalf("click count execute failed: %v", err)
|
||||
}
|
||||
if clickCount != "1" {
|
||||
t.Fatalf("expected click count to be 1, got %v", clickCount)
|
||||
}
|
||||
|
||||
found, err := page.WaitForSelector("#delayed", map[string]any{
|
||||
"timeoutMs": 5000,
|
||||
"pollIntervalMs": 25,
|
||||
"visible": true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("waitForSelector failed: %v", err)
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("expected delayed selector to appear")
|
||||
}
|
||||
|
||||
value, err := page.WaitForFunction(`() => window.__readyValue`, map[string]any{
|
||||
"timeoutMs": 5000,
|
||||
"pollIntervalMs": 25,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("waitForFunction failed: %v", err)
|
||||
}
|
||||
readyValue, ok := value.(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected waitForFunction value: %T", value)
|
||||
}
|
||||
if readyValue["status"] != "ready" {
|
||||
t.Fatalf("unexpected waitForFunction payload: %#v", readyValue)
|
||||
}
|
||||
|
||||
found, err = page.WaitForSelector("#missing", map[string]any{
|
||||
"timeoutMs": 200,
|
||||
"pollIntervalMs": 25,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("waitForSelector timeout case failed: %v", err)
|
||||
}
|
||||
if found {
|
||||
t.Fatal("expected missing selector wait to time out")
|
||||
}
|
||||
|
||||
cookieTestURL := opts.CookieTestURL
|
||||
if cookieTestURL == "" {
|
||||
cookieTestURL = testURL
|
||||
}
|
||||
runCookieLifecycleAssertions(t, page, server, cookieTestURL, opts.CookieDomainMode)
|
||||
}
|
||||
|
||||
func waitForProviderAvailable(t *testing.T, provider enginewebview.Provider, timeout time.Duration) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(timeout)
|
||||
for time.Now().Before(deadline) {
|
||||
if provider != nil && provider.IsAvailable() {
|
||||
return
|
||||
}
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("provider did not become available within %s", timeout)
|
||||
}
|
||||
|
||||
func openTestPage(t *testing.T, provider enginewebview.Provider, opts enginewebview.OpenOptions) (*enginewebview.PageHandle, *httptest.Server, string) {
|
||||
t.Helper()
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
switch r.URL.Path {
|
||||
case "/next":
|
||||
fmt.Fprint(w, `<!DOCTYPE html>
|
||||
<html>
|
||||
<head><title>Next Page</title></head>
|
||||
<body>
|
||||
<h1 id="next-page">Navigation complete</h1>
|
||||
</body>
|
||||
</html>`)
|
||||
default:
|
||||
fmt.Fprint(w, `<!DOCTYPE html>
|
||||
<html>
|
||||
<head><title>Test Page</title></head>
|
||||
<body>
|
||||
<input id="name" type="text" />
|
||||
<div id="name-value"></div>
|
||||
<button id="clicker" type="button">Click me</button>
|
||||
<div id="click-count">0</div>
|
||||
<button id="nav-btn" type="button">Navigate</button>
|
||||
<script>
|
||||
window.__inputEvents = [];
|
||||
window.__readyValue = null;
|
||||
const nameInput = document.getElementById('name');
|
||||
const nameValue = document.getElementById('name-value');
|
||||
nameInput.addEventListener('input', () => {
|
||||
nameValue.textContent = nameInput.value;
|
||||
window.__inputEvents.push(nameInput.value);
|
||||
});
|
||||
document.getElementById('clicker').addEventListener('click', () => {
|
||||
const counter = document.getElementById('click-count');
|
||||
counter.textContent = String(Number(counter.textContent || '0') + 1);
|
||||
});
|
||||
document.getElementById('nav-btn').addEventListener('click', () => {
|
||||
setTimeout(() => {
|
||||
window.location.href = '/next';
|
||||
}, 50);
|
||||
});
|
||||
setTimeout(() => {
|
||||
const marker = document.createElement('div');
|
||||
marker.id = 'delayed';
|
||||
marker.textContent = 'Loaded later';
|
||||
document.body.appendChild(marker);
|
||||
}, 150);
|
||||
setTimeout(() => {
|
||||
window.__readyValue = { status: 'ready', source: 'timer' };
|
||||
}, 200);
|
||||
</script>
|
||||
</body>
|
||||
</html>`)
|
||||
}
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
runtime := enginewebview.NewRuntime(provider, true)
|
||||
page, err := runtime.Open(map[string]any{
|
||||
"headless": opts.Headless,
|
||||
"debug": opts.Debug,
|
||||
"title": opts.Title,
|
||||
"width": opts.Width,
|
||||
"height": opts.Height,
|
||||
"userAgent": opts.UserAgent,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open webview: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = runtime.Close()
|
||||
})
|
||||
|
||||
return page, server, server.URL
|
||||
}
|
||||
|
||||
func runCookieLifecycleAssertions(t *testing.T, page *enginewebview.PageHandle, _ *httptest.Server, testURL string, domainMode CookieDomainMode) {
|
||||
t.Helper()
|
||||
|
||||
parsedURL, err := url.Parse(testURL)
|
||||
if err != nil {
|
||||
t.Fatalf("parse cookie test url failed: %v", err)
|
||||
}
|
||||
cookieDomain := parsedURL.Hostname()
|
||||
|
||||
if err := page.ClearCookies(); err != nil {
|
||||
t.Fatalf("clearCookies before cookie assertions failed: %v", err)
|
||||
}
|
||||
|
||||
visibleCookie := map[string]any{
|
||||
"name": "visible",
|
||||
"value": "1",
|
||||
"path": "/",
|
||||
}
|
||||
secretCookie := map[string]any{
|
||||
"name": "secret",
|
||||
"value": "2",
|
||||
"path": "/",
|
||||
"httpOnly": true,
|
||||
"expires": time.Now().Add(time.Hour).UTC().Format(time.RFC3339Nano),
|
||||
}
|
||||
deleteCookie := map[string]any{
|
||||
"name": "visible",
|
||||
"path": "/",
|
||||
}
|
||||
if domainMode == CookieDomainModeRequired {
|
||||
visibleCookie["domain"] = cookieDomain
|
||||
secretCookie["domain"] = cookieDomain
|
||||
deleteCookie["domain"] = cookieDomain
|
||||
}
|
||||
|
||||
if err := page.SetCookie(visibleCookie); err != nil {
|
||||
t.Fatalf("set visible cookie failed: %v", err)
|
||||
}
|
||||
if err := page.SetCookie(secretCookie); err != nil {
|
||||
t.Fatalf("set secret cookie failed: %v", err)
|
||||
}
|
||||
|
||||
if err := page.Goto(testURL, map[string]any{"timeoutMs": 10000}); err != nil {
|
||||
t.Fatalf("goto after setCookie failed: %v", err)
|
||||
}
|
||||
|
||||
documentCookie, err := page.Execute(`() => document.cookie`)
|
||||
if err != nil {
|
||||
t.Fatalf("document.cookie execute failed: %v", err)
|
||||
}
|
||||
documentCookieText, ok := documentCookie.(string)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected document.cookie payload: %T", documentCookie)
|
||||
}
|
||||
if !strings.Contains(documentCookieText, "visible=1") {
|
||||
t.Fatalf("expected visible cookie in document.cookie, got %q", documentCookieText)
|
||||
}
|
||||
if strings.Contains(documentCookieText, "secret=2") {
|
||||
t.Fatalf("expected httpOnly cookie to be hidden from document.cookie, got %q", documentCookieText)
|
||||
}
|
||||
|
||||
cookies, err := page.GetCookies()
|
||||
if err != nil {
|
||||
t.Fatalf("getCookies failed: %v", err)
|
||||
}
|
||||
if len(cookies) < 2 {
|
||||
t.Fatalf("expected at least 2 cookies, got %#v", cookies)
|
||||
}
|
||||
if !hasCookie(cookies, "visible", "1") {
|
||||
t.Fatalf("expected visible cookie in native cookie store, got %#v", cookies)
|
||||
}
|
||||
if !hasCookie(cookies, "secret", "2") {
|
||||
t.Fatalf("expected secret cookie in native cookie store, got %#v", cookies)
|
||||
}
|
||||
|
||||
if err := page.DeleteCookie(deleteCookie); err != nil {
|
||||
t.Fatalf("deleteCookie failed: %v", err)
|
||||
}
|
||||
if err := page.Goto(testURL, map[string]any{"timeoutMs": 10000}); err != nil {
|
||||
t.Fatalf("goto after deleteCookie failed: %v", err)
|
||||
}
|
||||
|
||||
documentCookie, err = page.Execute(`() => document.cookie`)
|
||||
if err != nil {
|
||||
t.Fatalf("document.cookie execute after delete failed: %v", err)
|
||||
}
|
||||
documentCookieText, ok = documentCookie.(string)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected document.cookie payload after delete: %T", documentCookie)
|
||||
}
|
||||
if strings.Contains(documentCookieText, "visible=1") {
|
||||
t.Fatalf("expected visible cookie to be deleted, got %q", documentCookieText)
|
||||
}
|
||||
if strings.Contains(documentCookieText, "secret=2") {
|
||||
t.Fatalf("expected httpOnly cookie to remain hidden after delete, got %q", documentCookieText)
|
||||
}
|
||||
|
||||
cookies, err = page.GetCookies()
|
||||
if err != nil {
|
||||
t.Fatalf("getCookies after delete failed: %v", err)
|
||||
}
|
||||
if hasCookie(cookies, "visible", "1") {
|
||||
t.Fatalf("expected visible cookie to be removed from native store, got %#v", cookies)
|
||||
}
|
||||
if !hasCookie(cookies, "secret", "2") {
|
||||
t.Fatalf("expected secret cookie to remain in native store after delete, got %#v", cookies)
|
||||
}
|
||||
|
||||
if err := page.ClearCookies(); err != nil {
|
||||
t.Fatalf("clearCookies failed: %v", err)
|
||||
}
|
||||
if err := page.Goto(testURL, map[string]any{"timeoutMs": 10000}); err != nil {
|
||||
t.Fatalf("goto after clearCookies failed: %v", err)
|
||||
}
|
||||
documentCookie, err = page.Execute(`() => document.cookie`)
|
||||
if err != nil {
|
||||
t.Fatalf("document.cookie execute after clear failed: %v", err)
|
||||
}
|
||||
documentCookieText, ok = documentCookie.(string)
|
||||
if !ok {
|
||||
t.Fatalf("unexpected document.cookie payload after clear: %T", documentCookie)
|
||||
}
|
||||
if documentCookieText != "" {
|
||||
t.Fatalf("expected document.cookie to be empty after clear, got %q", documentCookieText)
|
||||
}
|
||||
cookies, err = page.GetCookies()
|
||||
if err != nil {
|
||||
t.Fatalf("getCookies after clear failed: %v", err)
|
||||
}
|
||||
if len(cookies) != 0 {
|
||||
t.Fatalf("expected cookie store to be empty after clear, got %#v", cookies)
|
||||
}
|
||||
}
|
||||
|
||||
func hasCookie(cookies []enginewebview.Cookie, name string, value string) bool {
|
||||
for _, cookie := range cookies {
|
||||
if cookie.Name == name && cookie.Value == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,488 @@
|
||||
# rpcprovider
|
||||
|
||||
`rpcprovider` is the RPC-backed implementation of Gopeed's `webview.Provider` interface.
|
||||
|
||||
It is intended for environments where WebView capability is owned by another process, most notably mobile hosts. This package does not implement a WebView itself. Instead, it translates the `pkg/download/engine/webview` page API into single-request `POST + JSON` calls sent to a local host-side RPC service.
|
||||
|
||||
## When to use it
|
||||
|
||||
Use `rpcprovider` for entrypoints such as `bind/mobile`, where the Go process cannot directly own a native WebView.
|
||||
|
||||
Do not use it for desktop or `cmd/web` local WebView integration. Those entrypoints should use `internal/webview/goprovider` instead.
|
||||
|
||||
## Configuration
|
||||
|
||||
The provider is configured through `pkg/download/engine/webview.RPCConfig`:
|
||||
|
||||
```go
|
||||
type RPCConfig struct {
|
||||
Network string `json:"network"`
|
||||
Address string `json:"address"`
|
||||
Token string `json:"token,omitempty"`
|
||||
}
|
||||
```
|
||||
|
||||
Notes:
|
||||
|
||||
- `Network` currently supports `tcp` and `unix`.
|
||||
- `Address` examples:
|
||||
- `127.0.0.1:38765`
|
||||
- `/path/to/webview.sock`
|
||||
- `Token` is optional. When set, the client sends `Authorization: Bearer <token>`.
|
||||
|
||||
Example:
|
||||
|
||||
```go
|
||||
provider := rpcprovider.New(webview.RPCConfig{
|
||||
Network: "tcp",
|
||||
Address: "127.0.0.1:38765",
|
||||
Token: "secret-token",
|
||||
})
|
||||
```
|
||||
|
||||
## Transport
|
||||
|
||||
- Endpoint: `POST /webview`
|
||||
- Content type: `application/json`
|
||||
- One request, one response
|
||||
- No `id` field yet
|
||||
- The envelope is intentionally close to JSON-RPC so it can evolve later without renaming the methods or reshaping the payloads
|
||||
|
||||
Request example:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "page.execute",
|
||||
"params": {
|
||||
"pageId": "page-1",
|
||||
"expression": "document.title",
|
||||
"args": []
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Successful response:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": {
|
||||
"title": "Example"
|
||||
},
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
Failure response:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": null,
|
||||
"error": {
|
||||
"code": "PAGE_NOT_FOUND",
|
||||
"message": "page not found"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Supported methods
|
||||
|
||||
- `webview.isAvailable`
|
||||
- `page.open`
|
||||
- `page.addInitScript`
|
||||
- `page.goto`
|
||||
- `page.execute`
|
||||
- `page.getCookies`
|
||||
- `page.setCookie`
|
||||
- `page.deleteCookie`
|
||||
- `page.clearCookies`
|
||||
- `page.close`
|
||||
|
||||
The canonical wire models for these methods are defined in:
|
||||
|
||||
- [pkg/download/engine/webview/rpc.go](../../../pkg/download/engine/webview/rpc.go)
|
||||
- [pkg/download/engine/webview/runtime.go](../../../pkg/download/engine/webview/runtime.go)
|
||||
|
||||
The README is only a guide. The Go source above is the authoritative protocol reference.
|
||||
|
||||
## Method reference
|
||||
|
||||
Each method below uses the common envelope:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "<method-name>",
|
||||
"params": { ... }
|
||||
}
|
||||
```
|
||||
|
||||
and returns:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": ...,
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
or:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": null,
|
||||
"error": {
|
||||
"code": "<ERROR_CODE>",
|
||||
"message": "<message>"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### `webview.isAvailable`
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "webview.isAvailable",
|
||||
"params": {}
|
||||
}
|
||||
```
|
||||
|
||||
Success:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": {
|
||||
"available": true
|
||||
},
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
### `page.open`
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "page.open",
|
||||
"params": {
|
||||
"headless": true,
|
||||
"debug": false,
|
||||
"title": "Gopeed WebView",
|
||||
"width": 1280,
|
||||
"height": 720,
|
||||
"userAgent": "Mozilla/5.0 ..."
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Success:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": {
|
||||
"pageId": "page-1"
|
||||
},
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
### `page.addInitScript`
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "page.addInitScript",
|
||||
"params": {
|
||||
"pageId": "page-1",
|
||||
"script": "window.__READY__ = true;"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Success:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": {},
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
### `page.goto`
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "page.goto",
|
||||
"params": {
|
||||
"pageId": "page-1",
|
||||
"url": "https://example.com",
|
||||
"timeoutMs": 15000,
|
||||
"waitUntil": "domcontentloaded"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Success:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": {},
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
`waitUntil` currently supports `load` and `domcontentloaded`. The default is `load`.
|
||||
|
||||
### `page.execute`
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "page.execute",
|
||||
"params": {
|
||||
"pageId": "page-1",
|
||||
"expression": "document.title",
|
||||
"args": []
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Success:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": "Example Domain",
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
### `page.getCookies`
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "page.getCookies",
|
||||
"params": {
|
||||
"pageId": "page-1"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Success:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": [
|
||||
{
|
||||
"name": "session",
|
||||
"value": "abc",
|
||||
"domain": ".example.com",
|
||||
"path": "/",
|
||||
"expires": "2026-03-26T12:00:00Z",
|
||||
"secure": true,
|
||||
"httpOnly": true
|
||||
}
|
||||
],
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
### `page.setCookie`
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "page.setCookie",
|
||||
"params": {
|
||||
"pageId": "page-1",
|
||||
"cookie": {
|
||||
"name": "session",
|
||||
"value": "abc",
|
||||
"domain": ".example.com",
|
||||
"path": "/",
|
||||
"expires": "2026-03-26T12:00:00Z",
|
||||
"secure": true,
|
||||
"httpOnly": true
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Success:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": {},
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
### `page.deleteCookie`
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "page.deleteCookie",
|
||||
"params": {
|
||||
"pageId": "page-1",
|
||||
"cookie": {
|
||||
"name": "session",
|
||||
"domain": ".example.com",
|
||||
"path": "/"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Success:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": {},
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
### `page.clearCookies`
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "page.clearCookies",
|
||||
"params": {
|
||||
"pageId": "page-1"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Success:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": {},
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
### `page.close`
|
||||
|
||||
Request:
|
||||
|
||||
```json
|
||||
{
|
||||
"method": "page.close",
|
||||
"params": {
|
||||
"pageId": "page-1"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Success:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": {},
|
||||
"error": null
|
||||
}
|
||||
```
|
||||
|
||||
## Method semantics
|
||||
|
||||
- `page.open`
|
||||
- Creates a page session
|
||||
- Returns `pageId`
|
||||
- `page.addInitScript`
|
||||
- Registers a page initialization script
|
||||
- `page.goto`
|
||||
- Goes to the target URL
|
||||
- The server should return only after navigation completes or fails
|
||||
- `page.execute`
|
||||
- Executes an expression or serialized function body
|
||||
- The returned value must be JSON-serializable
|
||||
- `page.getCookies`
|
||||
- Returns cookies from the underlying native WebView cookie store
|
||||
- `page.setCookie`
|
||||
- Inserts or updates a cookie
|
||||
- `page.deleteCookie`
|
||||
- Deletes a cookie using `name/domain/path`
|
||||
- `page.clearCookies`
|
||||
- Clears the current cookie store
|
||||
- `page.close`
|
||||
- Closes the page and releases related resources
|
||||
|
||||
## Cookie model
|
||||
|
||||
Cookies use `runtime/webview.Cookie`.
|
||||
|
||||
Example payload:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "session",
|
||||
"value": "abc",
|
||||
"domain": ".example.com",
|
||||
"path": "/",
|
||||
"expires": "2026-03-26T12:00:00Z",
|
||||
"secure": true,
|
||||
"httpOnly": true
|
||||
}
|
||||
```
|
||||
|
||||
Notes:
|
||||
|
||||
- `expires` uses RFC3339 / RFC3339Nano string form.
|
||||
- If the host platform supports a full native cookie store, it should return fields such as `httpOnly` and `secure`.
|
||||
- `setCookie -> goto` is a valid pattern for preloading authenticated session state.
|
||||
|
||||
## Error codes
|
||||
|
||||
The current error code set is defined in `pkg/download/engine/webview/rpc.go`:
|
||||
|
||||
- `INVALID_REQUEST`
|
||||
- `UNKNOWN_METHOD`
|
||||
- `UNAVAILABLE`
|
||||
- `BROWSER_NOT_FOUND`
|
||||
- `PAGE_NOT_FOUND`
|
||||
- `NAVIGATION_FAILED`
|
||||
- `EVALUATION_FAILED`
|
||||
- `TIMEOUT`
|
||||
- `INTERNAL_ERROR`
|
||||
|
||||
`BROWSER_NOT_FOUND` is a historical leftover from an earlier shape of the protocol. The current protocol no longer has a separate browser model, so this can be cleaned up later if the protocol is tightened further.
|
||||
|
||||
## Current protocol boundary
|
||||
|
||||
This protocol is intentionally limited:
|
||||
|
||||
- Single-direction request/response only
|
||||
- Not REST-style resource routing
|
||||
- No bidirectional events
|
||||
- No streaming event subscriptions
|
||||
- No host-initiated push channel
|
||||
|
||||
If the protocol needs to evolve toward full JSON-RPC later, the intended migration path is to keep the existing `method/params/result/error` structure and add:
|
||||
|
||||
- `id`
|
||||
- notifications
|
||||
- a bidirectional transport model
|
||||
|
||||
## Host implementation guidance
|
||||
|
||||
A host implementation should usually:
|
||||
|
||||
- Listen on a local `tcp` address or `unix socket`
|
||||
- Expose `POST /webview`
|
||||
- Validate the bearer token if configured
|
||||
- Maintain `pageId -> native webview/session`
|
||||
- Own page lifecycle, cleanup, and timeout handling
|
||||
|
||||
With that split, Gopeed stays a pure RPC client and does not need to know how the host's native WebView is implemented.
|
||||
@@ -0,0 +1,195 @@
|
||||
package rpcprovider
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||
)
|
||||
|
||||
type Provider struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
func New(cfg enginewebview.RPCConfig) enginewebview.Provider {
|
||||
return &Provider{
|
||||
client: NewClient(cfg),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Provider) IsAvailable() bool {
|
||||
if p == nil || p.client == nil || !p.client.Enabled() {
|
||||
return false
|
||||
}
|
||||
var result enginewebview.IsAvailableResult
|
||||
if err := p.client.Call(enginewebview.MethodIsAvailable, enginewebview.IsAvailableParams{}, &result); err != nil {
|
||||
return false
|
||||
}
|
||||
return result.Available
|
||||
}
|
||||
|
||||
func (p *Provider) Open(opts enginewebview.OpenOptions) (enginewebview.Page, error) {
|
||||
if p == nil || p.client == nil || !p.IsAvailable() {
|
||||
return nil, enginewebview.ErrUnavailable
|
||||
}
|
||||
var result enginewebview.PageOpenResult
|
||||
if err := p.client.Call(enginewebview.MethodPageOpen, enginewebview.NewPageOpenParams(opts), &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &page{
|
||||
client: p.client,
|
||||
id: result.PageID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type page struct {
|
||||
client *Client
|
||||
id string
|
||||
}
|
||||
|
||||
func (p *page) AddInitScript(script string) error {
|
||||
return p.client.Call(enginewebview.MethodPageAddInitScript, enginewebview.PageAddInitScriptParams{
|
||||
PageID: p.id,
|
||||
Script: script,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func (p *page) Goto(url string, opts enginewebview.GotoOptions) error {
|
||||
return p.client.Call(enginewebview.MethodPageGoto, enginewebview.NewPageGotoParams(p.id, url, opts), nil)
|
||||
}
|
||||
|
||||
func (p *page) Execute(expression string, args ...any) (any, error) {
|
||||
var result any
|
||||
if err := p.client.Call(enginewebview.MethodPageExecute, enginewebview.PageExecuteParams{
|
||||
PageID: p.id,
|
||||
Expression: expression,
|
||||
Args: args,
|
||||
}, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p *page) GetCookies() ([]enginewebview.Cookie, error) {
|
||||
var result []enginewebview.Cookie
|
||||
if err := p.client.Call(enginewebview.MethodPageGetCookies, enginewebview.PageGetCookiesParams{
|
||||
PageID: p.id,
|
||||
}, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result == nil {
|
||||
return []enginewebview.Cookie{}, nil
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p *page) SetCookie(cookie enginewebview.Cookie) error {
|
||||
return p.client.Call(enginewebview.MethodPageSetCookie, enginewebview.PageSetCookieParams{
|
||||
PageID: p.id,
|
||||
Cookie: cookie,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func (p *page) DeleteCookie(cookie enginewebview.Cookie) error {
|
||||
return p.client.Call(enginewebview.MethodPageDeleteCookie, enginewebview.PageDeleteCookieParams{
|
||||
PageID: p.id,
|
||||
Cookie: cookie,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func (p *page) ClearCookies() error {
|
||||
return p.client.Call(enginewebview.MethodPageClearCookies, enginewebview.PageClearCookiesParams{
|
||||
PageID: p.id,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
func (p *page) Close() error {
|
||||
return p.client.Call(enginewebview.MethodPageClose, enginewebview.PageCloseParams{
|
||||
PageID: p.id,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
endpoint string
|
||||
token string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func NewClient(cfg enginewebview.RPCConfig) *Client {
|
||||
client := &Client{token: cfg.Token}
|
||||
if !cfg.Enabled() {
|
||||
return client
|
||||
}
|
||||
switch strings.ToLower(cfg.Network) {
|
||||
case "unix":
|
||||
client.endpoint = "http://unix" + enginewebview.RPCEndpointPath
|
||||
client.http = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||
var d net.Dialer
|
||||
return d.DialContext(ctx, "unix", cfg.Address)
|
||||
},
|
||||
},
|
||||
}
|
||||
default:
|
||||
client.endpoint = "http://" + cfg.Address + enginewebview.RPCEndpointPath
|
||||
client.http = &http.Client{}
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
func (c *Client) Enabled() bool {
|
||||
return c != nil && c.endpoint != "" && c.http != nil
|
||||
}
|
||||
|
||||
func (c *Client) Call(method string, params any, result any) error {
|
||||
if !c.Enabled() {
|
||||
return enginewebview.ErrUnavailable
|
||||
}
|
||||
payload, err := json.Marshal(enginewebview.RPCRequest{
|
||||
Method: method,
|
||||
Params: params,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, c.endpoint, bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if c.token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("webview rpc status %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var body enginewebview.RPCResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
||||
return err
|
||||
}
|
||||
if body.Error != nil {
|
||||
return body.Error
|
||||
}
|
||||
if result == nil {
|
||||
return nil
|
||||
}
|
||||
if len(body.Result) == 0 || string(body.Result) == "null" {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(body.Result, result)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//go:build webview && darwin
|
||||
|
||||
package rpcprovider
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/GopeedLab/gopeed/internal/webview/integrationtest"
|
||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||
)
|
||||
|
||||
var (
|
||||
webViewRPCNetwork = flag.String("webview-rpc-network", "unix", "WebView RPC network for integration tests")
|
||||
webViewRPCAddress = flag.String("webview-rpc-address", defaultWebViewRPCAddress(), "WebView RPC address for integration tests")
|
||||
webViewRPCToken = flag.String("webview-rpc-token", "", "WebView RPC token for integration tests")
|
||||
)
|
||||
|
||||
func TestProviderContract(t *testing.T) {
|
||||
provider := New(enginewebview.RPCConfig{
|
||||
Network: *webViewRPCNetwork,
|
||||
Address: *webViewRPCAddress,
|
||||
Token: *webViewRPCToken,
|
||||
})
|
||||
integrationtest.RunProviderContract(t, provider, integrationtest.ContractOptions{
|
||||
CookieDomainMode: integrationtest.CookieDomainModeRequired,
|
||||
CookieTestURL: "https://example.com/",
|
||||
})
|
||||
}
|
||||
|
||||
func defaultWebViewRPCAddress() string {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return filepath.Join(homeDir, "Library", "Application Support", "com.gopeed.gopeed", "gopeed_webview.sock")
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package rpcprovider
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||
)
|
||||
|
||||
func TestProviderIsAvailableOverUnixSocket(t *testing.T) {
|
||||
socketPath := filepath.Join("/tmp", "gopeed-rpcwebview-"+t.Name()+".sock")
|
||||
listener, err := net.Listen("unix", socketPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer listener.Close()
|
||||
defer os.Remove(socketPath)
|
||||
|
||||
server := &http.Server{
|
||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var req enginewebview.RPCRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if req.Method != enginewebview.MethodIsAvailable {
|
||||
t.Fatalf("unexpected unix method: %s", req.Method)
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(map[string]any{
|
||||
"result": enginewebview.IsAvailableResult{Available: true},
|
||||
"error": nil,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}),
|
||||
}
|
||||
defer server.Close()
|
||||
go server.Serve(listener)
|
||||
|
||||
provider := New(enginewebview.RPCConfig{
|
||||
Network: "unix",
|
||||
Address: socketPath,
|
||||
})
|
||||
if !provider.IsAvailable() {
|
||||
t.Fatal("expected unix provider to be available")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package rpcprovider
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
enginewebview "github.com/GopeedLab/gopeed/pkg/download/engine/webview"
|
||||
)
|
||||
|
||||
func TestProviderLifecycleOverHTTP(t *testing.T) {
|
||||
var calls []enginewebview.RPCRequest
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
t.Fatalf("unexpected method: %s", r.Method)
|
||||
}
|
||||
if r.URL.Path != enginewebview.RPCEndpointPath {
|
||||
t.Fatalf("unexpected path: %s", r.URL.Path)
|
||||
}
|
||||
if got := r.Header.Get("Authorization"); got != "Bearer secret-token" {
|
||||
t.Fatalf("unexpected auth header: %q", got)
|
||||
}
|
||||
var req enginewebview.RPCRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
calls = append(calls, req)
|
||||
|
||||
var body any
|
||||
switch req.Method {
|
||||
case enginewebview.MethodIsAvailable:
|
||||
body = map[string]any{
|
||||
"result": enginewebview.IsAvailableResult{Available: true},
|
||||
"error": nil,
|
||||
}
|
||||
case enginewebview.MethodPageOpen:
|
||||
body = map[string]any{
|
||||
"result": enginewebview.PageOpenResult{PageID: "page-open-1"},
|
||||
"error": nil,
|
||||
}
|
||||
case enginewebview.MethodPageAddInitScript, enginewebview.MethodPageGoto:
|
||||
body = map[string]any{
|
||||
"result": enginewebview.EmptyResult{},
|
||||
"error": nil,
|
||||
}
|
||||
case enginewebview.MethodPageExecute:
|
||||
body = map[string]any{
|
||||
"result": map[string]any{"title": "Example"},
|
||||
"error": nil,
|
||||
}
|
||||
case enginewebview.MethodPageClose:
|
||||
body = map[string]any{
|
||||
"result": enginewebview.EmptyResult{},
|
||||
"error": nil,
|
||||
}
|
||||
default:
|
||||
t.Fatalf("unexpected method: %s", req.Method)
|
||||
}
|
||||
if err := json.NewEncoder(w).Encode(body); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := enginewebview.RPCConfig{
|
||||
Network: "tcp",
|
||||
Address: server.Listener.Addr().String(),
|
||||
Token: "secret-token",
|
||||
}
|
||||
provider := New(cfg)
|
||||
if !provider.IsAvailable() {
|
||||
t.Fatal("expected provider to be available")
|
||||
}
|
||||
|
||||
opener, ok := provider.(enginewebview.Opener)
|
||||
if !ok {
|
||||
t.Fatal("expected provider to implement runtime webview opener")
|
||||
}
|
||||
opened, err := opener.Open(enginewebview.OpenOptions{
|
||||
Headless: true,
|
||||
Width: 1280,
|
||||
Height: 720,
|
||||
Title: "Gopeed",
|
||||
UserAgent: "UA",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := opened.AddInitScript("window.__TEST__ = true"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := opened.Goto("https://example.com", enginewebview.GotoOptions{
|
||||
TimeoutMS: 3210,
|
||||
WaitUntil: "domcontentloaded",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value, err := opened.Execute("document.title")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, ok := value.(map[string]any)
|
||||
if !ok || got["title"] != "Example" {
|
||||
t.Fatalf("unexpected evaluate result: %#v", value)
|
||||
}
|
||||
if err := opened.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expectedMethods := []string{
|
||||
enginewebview.MethodIsAvailable,
|
||||
enginewebview.MethodIsAvailable,
|
||||
enginewebview.MethodPageOpen,
|
||||
enginewebview.MethodPageAddInitScript,
|
||||
enginewebview.MethodPageGoto,
|
||||
enginewebview.MethodPageExecute,
|
||||
enginewebview.MethodPageClose,
|
||||
}
|
||||
if len(calls) != len(expectedMethods) {
|
||||
t.Fatalf("unexpected call count: got %d want %d", len(calls), len(expectedMethods))
|
||||
}
|
||||
for i, method := range expectedMethods {
|
||||
if calls[i].Method != method {
|
||||
t.Fatalf("unexpected method at %d: got %s want %s", i, calls[i].Method, method)
|
||||
}
|
||||
}
|
||||
gotoParamsJSON, err := json.Marshal(calls[4].Params)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var gotoParams enginewebview.PageGotoParams
|
||||
if err := json.Unmarshal(gotoParamsJSON, &gotoParams); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if gotoParams.WaitUntil != "domcontentloaded" {
|
||||
t.Fatalf("unexpected waitUntil: %q", gotoParams.WaitUntil)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderLaunchUnavailableWithoutEndpoint(t *testing.T) {
|
||||
provider := New(enginewebview.RPCConfig{})
|
||||
if provider.IsAvailable() {
|
||||
t.Fatal("expected empty provider to be unavailable")
|
||||
}
|
||||
opener, ok := provider.(enginewebview.Opener)
|
||||
if !ok {
|
||||
t.Fatal("expected provider to implement runtime webview opener")
|
||||
}
|
||||
if _, err := opener.Open(enginewebview.OpenOptions{}); err == nil || err.Error() != enginewebview.ErrUnavailable.Error() {
|
||||
t.Fatalf("unexpected launch error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageSetCookieParamsOmitsZeroExpires(t *testing.T) {
|
||||
params := enginewebview.PageSetCookieParams{
|
||||
PageID: "page-1",
|
||||
Cookie: enginewebview.Cookie{
|
||||
Name: "visible",
|
||||
Value: "1",
|
||||
Domain: "example.com",
|
||||
Path: "/",
|
||||
},
|
||||
}
|
||||
payload, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(payload) == "" {
|
||||
t.Fatal("expected non-empty json payload")
|
||||
}
|
||||
if got := string(payload); strings.Contains(got, `"expires"`) {
|
||||
t.Fatalf("unexpected zero expires in payload: %s", got)
|
||||
}
|
||||
|
||||
params.Cookie.Expires = time.Now().UTC().Truncate(time.Second)
|
||||
payload, err = json.Marshal(params)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := string(payload); !strings.Contains(got, `"expires"`) {
|
||||
t.Fatalf("expected expires in payload: %s", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sync"
|
||||
|
||||
"github.com/GopeedLab/gopeed/pkg/download"
|
||||
)
|
||||
|
||||
type TaskEventMask uint64
|
||||
|
||||
const (
|
||||
TaskEventDone TaskEventMask = 1 << iota
|
||||
TaskEventError
|
||||
)
|
||||
|
||||
type TaskEvent struct {
|
||||
Type string `json:"type"`
|
||||
TaskID string `json:"taskId"`
|
||||
Name string `json:"name,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type TaskEventListener func(payload string)
|
||||
|
||||
type taskEventSubscription struct {
|
||||
mu sync.RWMutex
|
||||
mask TaskEventMask
|
||||
listener TaskEventListener
|
||||
}
|
||||
|
||||
func (s *Service) SubscribeTaskEvents(mask TaskEventMask, listener TaskEventListener) {
|
||||
s.taskEvents.mu.Lock()
|
||||
defer s.taskEvents.mu.Unlock()
|
||||
s.taskEvents.mask = mask
|
||||
s.taskEvents.listener = listener
|
||||
}
|
||||
|
||||
func (s *Service) emitTaskEvent(event *download.Event) {
|
||||
if event == nil || event.Task == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var mask TaskEventMask
|
||||
switch event.Key {
|
||||
case download.EventKeyDone:
|
||||
mask = TaskEventDone
|
||||
case download.EventKeyError:
|
||||
mask = TaskEventError
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
s.taskEvents.mu.RLock()
|
||||
if s.taskEvents.mask&mask == 0 || s.taskEvents.listener == nil {
|
||||
s.taskEvents.mu.RUnlock()
|
||||
return
|
||||
}
|
||||
listener := s.taskEvents.listener
|
||||
s.taskEvents.mu.RUnlock()
|
||||
|
||||
payload := TaskEvent{
|
||||
Type: "task." + string(event.Key),
|
||||
TaskID: event.Task.ID,
|
||||
}
|
||||
if event.Task.Meta != nil {
|
||||
payload.Name = event.Task.Name()
|
||||
}
|
||||
if event.Err != nil {
|
||||
payload.Error = event.Err.Error()
|
||||
}
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// A platform callback must never be able to crash a download worker.
|
||||
func() {
|
||||
defer func() { _ = recover() }()
|
||||
listener(string(data))
|
||||
}()
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/GopeedLab/gopeed/pkg/download"
|
||||
)
|
||||
|
||||
func TestTaskEventSubscriptionFiltersMask(t *testing.T) {
|
||||
service := &Service{}
|
||||
var payloads []string
|
||||
service.SubscribeTaskEvents(TaskEventDone, func(payload string) {
|
||||
payloads = append(payloads, payload)
|
||||
})
|
||||
|
||||
task := &download.Task{ID: "task-1"}
|
||||
service.emitTaskEvent(&download.Event{Key: download.EventKeyError, Task: task})
|
||||
service.emitTaskEvent(&download.Event{Key: download.EventKeyDone, Task: task})
|
||||
|
||||
if len(payloads) != 1 {
|
||||
t.Fatalf("got %d callbacks, want 1", len(payloads))
|
||||
}
|
||||
var event TaskEvent
|
||||
if err := json.Unmarshal([]byte(payloads[0]), &event); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if event.Type != "task.done" || event.TaskID != task.ID {
|
||||
t.Fatalf("unexpected event: %#v", event)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskEventListenerPanicIsContained(t *testing.T) {
|
||||
service := &Service{}
|
||||
service.SubscribeTaskEvents(TaskEventDone, func(string) { panic("callback failed") })
|
||||
service.emitTaskEvent(&download.Event{Key: download.EventKeyDone, Task: &download.Task{ID: "task-1"}})
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
pathpkg "path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HandlerFunc func(*Context) *Response
|
||||
|
||||
type RouteSpec struct {
|
||||
Method string
|
||||
Pattern string
|
||||
Handler HandlerFunc
|
||||
parts []routePart
|
||||
}
|
||||
|
||||
type routePart struct {
|
||||
literal string
|
||||
param string
|
||||
}
|
||||
|
||||
func newRoute(method, pattern string, handler HandlerFunc) RouteSpec {
|
||||
pattern = cleanPath(pattern)
|
||||
return RouteSpec{
|
||||
Method: strings.ToUpper(method),
|
||||
Pattern: pattern,
|
||||
Handler: handler,
|
||||
parts: parseRouteParts(pattern),
|
||||
}
|
||||
}
|
||||
|
||||
func validateRoutes(routes []RouteSpec) error {
|
||||
patterns := make(map[string]struct{}, len(routes))
|
||||
for _, route := range routes {
|
||||
if route.Method == "" || route.Pattern == "" || route.Handler == nil {
|
||||
return fmt.Errorf("invalid API route: %+v", route)
|
||||
}
|
||||
key := route.Method + " " + route.Pattern
|
||||
if _, exists := patterns[key]; exists {
|
||||
return fmt.Errorf("duplicate API route: %s", key)
|
||||
}
|
||||
patterns[key] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseRouteParts(pattern string) []routePart {
|
||||
pattern = strings.Trim(pattern, "/")
|
||||
if pattern == "" {
|
||||
return nil
|
||||
}
|
||||
segments := strings.Split(pattern, "/")
|
||||
parts := make([]routePart, 0, len(segments))
|
||||
for _, segment := range segments {
|
||||
if strings.HasPrefix(segment, "{") && strings.HasSuffix(segment, "}") {
|
||||
name := strings.TrimSuffix(strings.TrimPrefix(segment, "{"), "}")
|
||||
parts = append(parts, routePart{param: name})
|
||||
continue
|
||||
}
|
||||
parts = append(parts, routePart{literal: segment})
|
||||
}
|
||||
return parts
|
||||
}
|
||||
|
||||
func (r RouteSpec) Match(rawPath string) (map[string]string, bool) {
|
||||
path := strings.Trim(cleanPath(rawPath), "/")
|
||||
if path == "" && len(r.parts) == 0 {
|
||||
return map[string]string{}, true
|
||||
}
|
||||
segments := strings.Split(path, "/")
|
||||
if len(segments) != len(r.parts) {
|
||||
return nil, false
|
||||
}
|
||||
params := make(map[string]string)
|
||||
for index, part := range r.parts {
|
||||
if part.param != "" {
|
||||
params[part.param] = segments[index]
|
||||
continue
|
||||
}
|
||||
if part.literal != segments[index] {
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
return params, true
|
||||
}
|
||||
|
||||
func cleanPath(rawPath string) string {
|
||||
clean := pathpkg.Clean("/" + strings.TrimSpace(rawPath))
|
||||
if clean == "." {
|
||||
return "/"
|
||||
}
|
||||
return clean
|
||||
}
|
||||
@@ -0,0 +1,457 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/GopeedLab/gopeed/pkg/base"
|
||||
"github.com/GopeedLab/gopeed/pkg/download"
|
||||
restmodel "github.com/GopeedLab/gopeed/pkg/rest/model"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Method string
|
||||
Path string
|
||||
Query url.Values
|
||||
Body []byte
|
||||
Headers http.Header
|
||||
PathParams map[string]string
|
||||
}
|
||||
|
||||
func (c *Context) PathParam(name string) string {
|
||||
if c == nil || c.PathParams == nil {
|
||||
return ""
|
||||
}
|
||||
return c.PathParams[name]
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
StatusCode int
|
||||
Body any
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Downloader *download.Downloader
|
||||
routes []RouteSpec
|
||||
taskEvents taskEventSubscription
|
||||
}
|
||||
|
||||
func NewService(downloader *download.Downloader) (*Service, error) {
|
||||
if downloader == nil {
|
||||
return nil, fmt.Errorf("downloader is nil")
|
||||
}
|
||||
service := &Service{Downloader: downloader}
|
||||
service.routes = []RouteSpec{
|
||||
newRoute(http.MethodGet, "/api/v1/info", service.info),
|
||||
newRoute(http.MethodPost, "/api/v1/resolve", service.resolve),
|
||||
newRoute(http.MethodPost, "/api/v1/tasks", service.createTask),
|
||||
newRoute(http.MethodPost, "/api/v1/tasks/batch", service.createTaskBatch),
|
||||
newRoute(http.MethodPatch, "/api/v1/tasks/{id}", service.patchTask),
|
||||
newRoute(http.MethodPut, "/api/v1/tasks/{id}/pause", service.pauseTask),
|
||||
newRoute(http.MethodPut, "/api/v1/tasks/pause", service.pauseTasks),
|
||||
newRoute(http.MethodPut, "/api/v1/tasks/{id}/continue", service.continueTask),
|
||||
newRoute(http.MethodPut, "/api/v1/tasks/continue", service.continueTasks),
|
||||
newRoute(http.MethodDelete, "/api/v1/tasks/{id}", service.deleteTask),
|
||||
newRoute(http.MethodDelete, "/api/v1/tasks", service.deleteTasks),
|
||||
newRoute(http.MethodGet, "/api/v1/tasks/{id}", service.getTask),
|
||||
newRoute(http.MethodGet, "/api/v1/tasks/{id}/status", service.getTaskStatus),
|
||||
newRoute(http.MethodGet, "/api/v1/tasks", service.getTasks),
|
||||
newRoute(http.MethodGet, "/api/v1/tasks/{id}/stats", service.getStats),
|
||||
newRoute(http.MethodGet, "/api/v1/config", service.getConfig),
|
||||
newRoute(http.MethodPut, "/api/v1/config", service.putConfig),
|
||||
newRoute(http.MethodPost, "/api/v1/extensions", service.installExtension),
|
||||
newRoute(http.MethodGet, "/api/v1/extensions", service.getExtensions),
|
||||
newRoute(http.MethodGet, "/api/v1/extensions/{identity}", service.getExtension),
|
||||
newRoute(http.MethodPut, "/api/v1/extensions/{identity}/settings", service.updateExtensionSettings),
|
||||
newRoute(http.MethodPut, "/api/v1/extensions/{identity}/switch", service.switchExtension),
|
||||
newRoute(http.MethodDelete, "/api/v1/extensions/{identity}", service.deleteExtension),
|
||||
newRoute(http.MethodGet, "/api/v1/extensions/{identity}/update", service.updateCheckExtension),
|
||||
newRoute(http.MethodPost, "/api/v1/extensions/{identity}/update", service.updateExtension),
|
||||
newRoute(http.MethodPost, "/api/v1/webhook/test", service.testWebhook),
|
||||
}
|
||||
if err := validateRoutes(service.routes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
downloader.Listener(service.emitTaskEvent)
|
||||
return service, nil
|
||||
}
|
||||
|
||||
func (s *Service) RouteSpecs() []RouteSpec {
|
||||
return append([]RouteSpec(nil), s.routes...)
|
||||
}
|
||||
|
||||
func (s *Service) Dispatch(method, rawPath string, query url.Values, body []byte, headers http.Header) (response *Response) {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
response = newErrorResponse(fmt.Sprintf("%v", recovered))
|
||||
}
|
||||
}()
|
||||
|
||||
method = strings.ToUpper(strings.TrimSpace(method))
|
||||
path := cleanPath(rawPath)
|
||||
for _, route := range s.routes {
|
||||
if route.Method != method {
|
||||
continue
|
||||
}
|
||||
pathParams, matched := route.Match(path)
|
||||
if !matched {
|
||||
continue
|
||||
}
|
||||
return route.Handler(&Context{
|
||||
Method: method,
|
||||
Path: path,
|
||||
Query: query,
|
||||
Body: body,
|
||||
Headers: headers,
|
||||
PathParams: pathParams,
|
||||
})
|
||||
}
|
||||
return newStatusResponse(http.StatusNotFound, restmodel.NewErrorResult("not found"))
|
||||
}
|
||||
|
||||
func (s *Service) JSON(method, rawPath string, query url.Values, body []byte, headers http.Header) (int, []byte) {
|
||||
response := s.Dispatch(method, rawPath, query, body, headers)
|
||||
data, err := json.Marshal(response.Body)
|
||||
if err != nil {
|
||||
response = newErrorResponse(err.Error())
|
||||
data, _ = json.Marshal(response.Body)
|
||||
}
|
||||
return response.StatusCode, data
|
||||
}
|
||||
|
||||
func (s *Service) info(_ *Context) *Response {
|
||||
return newOKResponse(map[string]any{
|
||||
"version": base.Version,
|
||||
"runtime": runtime.Version(),
|
||||
"os": runtime.GOOS,
|
||||
"arch": runtime.GOARCH,
|
||||
"inDocker": base.InDocker == "true",
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) resolve(ctx *Context) *Response {
|
||||
var request restmodel.ResolveTask
|
||||
if response := decodeRequest(ctx.Body, &request); response != nil {
|
||||
return response
|
||||
}
|
||||
result, err := s.Downloader.Resolve(request.Req, request.Opts)
|
||||
if err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newOKResponse(result)
|
||||
}
|
||||
|
||||
func (s *Service) createTask(ctx *Context) *Response {
|
||||
var request restmodel.CreateTask
|
||||
if response := decodeRequest(ctx.Body, &request); response != nil {
|
||||
return response
|
||||
}
|
||||
var (
|
||||
taskID string
|
||||
err error
|
||||
)
|
||||
if request.Rid != "" {
|
||||
taskID, err = s.Downloader.Create(request.Rid)
|
||||
} else if request.Req != nil {
|
||||
taskID, err = s.Downloader.CreateDirect(request.Req, request.Opts)
|
||||
} else {
|
||||
return newErrorResponse("param invalid: rid or req", restmodel.CodeInvalidParam)
|
||||
}
|
||||
if err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newOKResponse(taskID)
|
||||
}
|
||||
|
||||
func (s *Service) createTaskBatch(ctx *Context) *Response {
|
||||
var request base.CreateTaskBatch
|
||||
if response := decodeRequest(ctx.Body, &request); response != nil {
|
||||
return response
|
||||
}
|
||||
if len(request.Reqs) == 0 {
|
||||
return newErrorResponse("param invalid: reqs", restmodel.CodeInvalidParam)
|
||||
}
|
||||
taskIDs, err := s.Downloader.CreateDirectBatch(&request)
|
||||
if err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newOKResponse(taskIDs)
|
||||
}
|
||||
|
||||
func (s *Service) patchTask(ctx *Context) *Response {
|
||||
taskID := ctx.PathParam("id")
|
||||
if taskID == "" {
|
||||
return invalidIDResponse()
|
||||
}
|
||||
var request restmodel.ResolveTask
|
||||
if response := decodeRequest(ctx.Body, &request); response != nil {
|
||||
return response
|
||||
}
|
||||
if err := s.Downloader.Patch(taskID, request.Req, request.Opts); err != nil {
|
||||
if err == download.ErrTaskNotFound {
|
||||
return taskNotFoundResponse()
|
||||
}
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newNilResponse()
|
||||
}
|
||||
|
||||
func (s *Service) pauseTask(ctx *Context) *Response {
|
||||
return s.applyTaskAction(ctx, s.Downloader.Pause)
|
||||
}
|
||||
|
||||
func (s *Service) pauseTasks(ctx *Context) *Response {
|
||||
return applyFilterAction(taskFilterFromQuery(ctx.Query), s.Downloader.Pause)
|
||||
}
|
||||
|
||||
func (s *Service) continueTask(ctx *Context) *Response {
|
||||
return s.applyTaskAction(ctx, s.Downloader.Continue)
|
||||
}
|
||||
|
||||
func (s *Service) continueTasks(ctx *Context) *Response {
|
||||
return applyFilterAction(taskFilterFromQuery(ctx.Query), s.Downloader.Continue)
|
||||
}
|
||||
|
||||
func (s *Service) applyTaskAction(ctx *Context, action func(*download.TaskFilter) error) *Response {
|
||||
taskID := ctx.PathParam("id")
|
||||
if taskID == "" {
|
||||
return invalidIDResponse()
|
||||
}
|
||||
return applyFilterAction(&download.TaskFilter{IDs: []string{taskID}}, action)
|
||||
}
|
||||
|
||||
func applyFilterAction(filter *download.TaskFilter, action func(*download.TaskFilter) error) *Response {
|
||||
if err := action(filter); err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newNilResponse()
|
||||
}
|
||||
|
||||
func (s *Service) deleteTask(ctx *Context) *Response {
|
||||
taskID := ctx.PathParam("id")
|
||||
if taskID == "" {
|
||||
return invalidIDResponse()
|
||||
}
|
||||
return deleteTasks(s.Downloader, &download.TaskFilter{IDs: []string{taskID}}, ctx.Query.Get("force") == "true")
|
||||
}
|
||||
|
||||
func (s *Service) deleteTasks(ctx *Context) *Response {
|
||||
return deleteTasks(s.Downloader, taskFilterFromQuery(ctx.Query), ctx.Query.Get("force") == "true")
|
||||
}
|
||||
|
||||
func deleteTasks(downloader *download.Downloader, filter *download.TaskFilter, force bool) *Response {
|
||||
if err := downloader.Delete(filter, force); err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newNilResponse()
|
||||
}
|
||||
|
||||
func (s *Service) getTask(ctx *Context) *Response {
|
||||
taskID := ctx.PathParam("id")
|
||||
if taskID == "" {
|
||||
return invalidIDResponse()
|
||||
}
|
||||
task := s.Downloader.GetTask(taskID)
|
||||
if task == nil {
|
||||
return taskNotFoundResponse()
|
||||
}
|
||||
return newOKResponse(task)
|
||||
}
|
||||
|
||||
func (s *Service) getTaskStatus(ctx *Context) *Response {
|
||||
taskID := ctx.PathParam("id")
|
||||
if taskID == "" {
|
||||
return invalidIDResponse()
|
||||
}
|
||||
status, err := s.Downloader.RuntimeStatus(taskID)
|
||||
if err != nil {
|
||||
if err == download.ErrTaskNotFound {
|
||||
return taskNotFoundResponse()
|
||||
}
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newOKResponse(status)
|
||||
}
|
||||
|
||||
func (s *Service) getTasks(ctx *Context) *Response {
|
||||
return newOKResponse(s.Downloader.GetTasksByFilter(taskFilterFromQuery(ctx.Query)))
|
||||
}
|
||||
|
||||
func (s *Service) getStats(ctx *Context) *Response {
|
||||
taskID := ctx.PathParam("id")
|
||||
if taskID == "" {
|
||||
return invalidIDResponse()
|
||||
}
|
||||
stats, err := s.Downloader.Stats(taskID)
|
||||
if err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newOKResponse(stats)
|
||||
}
|
||||
|
||||
func (s *Service) getConfig(_ *Context) *Response {
|
||||
config, err := s.Downloader.GetConfig()
|
||||
if err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newOKResponse(config)
|
||||
}
|
||||
|
||||
func (s *Service) putConfig(ctx *Context) *Response {
|
||||
var config base.DownloaderStoreConfig
|
||||
if response := decodeRequest(ctx.Body, &config); response != nil {
|
||||
return response
|
||||
}
|
||||
if err := s.Downloader.PutConfig(&config); err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newNilResponse()
|
||||
}
|
||||
|
||||
func (s *Service) installExtension(ctx *Context) *Response {
|
||||
var request restmodel.InstallExtension
|
||||
if response := decodeRequest(ctx.Body, &request); response != nil {
|
||||
return response
|
||||
}
|
||||
var (
|
||||
extension *download.Extension
|
||||
err error
|
||||
)
|
||||
if request.DevMode {
|
||||
extension, err = s.Downloader.InstallExtensionByFolder(request.URL, true)
|
||||
} else {
|
||||
extension, err = s.Downloader.InstallExtensionByGit(request.URL)
|
||||
}
|
||||
if err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newOKResponse(extension.Identity)
|
||||
}
|
||||
|
||||
func (s *Service) getExtensions(_ *Context) *Response {
|
||||
return newOKResponse(s.Downloader.GetExtensions())
|
||||
}
|
||||
|
||||
func (s *Service) getExtension(ctx *Context) *Response {
|
||||
identity := ctx.PathParam("identity")
|
||||
if identity == "" {
|
||||
return newErrorResponse("param invalid: identity", restmodel.CodeInvalidParam)
|
||||
}
|
||||
extension, err := s.Downloader.GetExtension(identity)
|
||||
if err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newOKResponse(extension)
|
||||
}
|
||||
|
||||
func (s *Service) updateExtensionSettings(ctx *Context) *Response {
|
||||
identity := ctx.PathParam("identity")
|
||||
var request restmodel.UpdateExtensionSettings
|
||||
if response := decodeRequest(ctx.Body, &request); response != nil {
|
||||
return response
|
||||
}
|
||||
if err := s.Downloader.UpdateExtensionSettings(identity, request.Settings); err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newNilResponse()
|
||||
}
|
||||
|
||||
func (s *Service) switchExtension(ctx *Context) *Response {
|
||||
identity := ctx.PathParam("identity")
|
||||
var request restmodel.SwitchExtension
|
||||
if response := decodeRequest(ctx.Body, &request); response != nil {
|
||||
return response
|
||||
}
|
||||
if err := s.Downloader.SwitchExtension(identity, request.Status); err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newNilResponse()
|
||||
}
|
||||
|
||||
func (s *Service) deleteExtension(ctx *Context) *Response {
|
||||
if err := s.Downloader.DeleteExtension(ctx.PathParam("identity")); err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newNilResponse()
|
||||
}
|
||||
|
||||
func (s *Service) updateCheckExtension(ctx *Context) *Response {
|
||||
version, err := s.Downloader.UpgradeCheckExtension(ctx.PathParam("identity"))
|
||||
if err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newOKResponse(&restmodel.UpdateCheckExtensionResp{NewVersion: version})
|
||||
}
|
||||
|
||||
func (s *Service) updateExtension(ctx *Context) *Response {
|
||||
if err := s.Downloader.UpgradeExtension(ctx.PathParam("identity")); err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newNilResponse()
|
||||
}
|
||||
|
||||
func (s *Service) testWebhook(ctx *Context) *Response {
|
||||
var request restmodel.TestWebhookReq
|
||||
if response := decodeRequest(ctx.Body, &request); response != nil {
|
||||
return response
|
||||
}
|
||||
if err := s.Downloader.TestWebhookUrl(request.URL); err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return newNilResponse()
|
||||
}
|
||||
|
||||
func decodeRequest(body []byte, target any) *Response {
|
||||
if err := json.NewDecoder(bytes.NewReader(body)).Decode(target); err != nil {
|
||||
return newErrorResponse(err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func taskFilterFromQuery(query url.Values) *download.TaskFilter {
|
||||
if query == nil {
|
||||
query = url.Values{}
|
||||
}
|
||||
return &download.TaskFilter{
|
||||
IDs: query["id"],
|
||||
Statuses: convertStatuses(query["status"]),
|
||||
NotStatuses: convertStatuses(query["notStatus"]),
|
||||
}
|
||||
}
|
||||
|
||||
func convertStatuses(values []string) []base.Status {
|
||||
statuses := make([]base.Status, 0, len(values))
|
||||
for _, value := range values {
|
||||
statuses = append(statuses, base.Status(value))
|
||||
}
|
||||
return statuses
|
||||
}
|
||||
|
||||
func invalidIDResponse() *Response {
|
||||
return newErrorResponse("param invalid: id", restmodel.CodeInvalidParam)
|
||||
}
|
||||
|
||||
func taskNotFoundResponse() *Response {
|
||||
return newErrorResponse("task not found", restmodel.CodeTaskNotFound)
|
||||
}
|
||||
|
||||
func newOKResponse(data any) *Response {
|
||||
return &Response{StatusCode: http.StatusOK, Body: restmodel.NewOkResult(data)}
|
||||
}
|
||||
|
||||
func newNilResponse() *Response {
|
||||
return &Response{StatusCode: http.StatusOK, Body: restmodel.NewNilResult()}
|
||||
}
|
||||
|
||||
func newErrorResponse(message string, code ...restmodel.RespCode) *Response {
|
||||
return &Response{StatusCode: http.StatusOK, Body: restmodel.NewErrorResult(message, code...)}
|
||||
}
|
||||
|
||||
func newStatusResponse(statusCode int, body any) *Response {
|
||||
return &Response{StatusCode: statusCode, Body: body}
|
||||
}
|
||||
@@ -23,6 +23,8 @@ const (
|
||||
HttpHeaderContentDisposition = "Content-Disposition"
|
||||
HttpHeaderUserAgent = "User-Agent"
|
||||
HttpHeaderLastModified = "Last-Modified"
|
||||
HttpHeaderETag = "ETag"
|
||||
HttpHeaderIfRange = "If-Range"
|
||||
|
||||
HttpHeaderBytes = "bytes"
|
||||
HttpHeaderRangeFormat = "bytes=%d-%d"
|
||||
|
||||
+46
-2
@@ -14,8 +14,9 @@ import (
|
||||
|
||||
// Request download request
|
||||
type Request struct {
|
||||
URL string `json:"url"`
|
||||
Extra any `json:"extra"`
|
||||
RawURL string `json:"rawUrl"`
|
||||
URL string `json:"url"`
|
||||
Extra any `json:"extra"`
|
||||
// Labels is used to mark the download task
|
||||
Labels map[string]string `json:"labels"`
|
||||
// Proxy is special proxy config for request
|
||||
@@ -24,6 +25,24 @@ type Request struct {
|
||||
SkipVerifyCert bool `json:"skipVerifyCert"`
|
||||
}
|
||||
|
||||
// SetLabels replaces all labels on the request.
|
||||
func (r *Request) SetLabels(labels map[string]string) {
|
||||
r.Labels = labels
|
||||
}
|
||||
|
||||
// PutLabel sets a label on the request.
|
||||
func (r *Request) PutLabel(key, value string) {
|
||||
if r.Labels == nil {
|
||||
r.Labels = make(map[string]string)
|
||||
}
|
||||
r.Labels[key] = value
|
||||
}
|
||||
|
||||
// DelLabel deletes a label from the request.
|
||||
func (r *Request) DelLabel(key string) {
|
||||
delete(r.Labels, key)
|
||||
}
|
||||
|
||||
func (r *Request) Validate() error {
|
||||
if r.URL == "" {
|
||||
return fmt.Errorf("invalid request url")
|
||||
@@ -114,6 +133,8 @@ type Options struct {
|
||||
Name string `json:"name"`
|
||||
// Download file path
|
||||
Path string `json:"path"`
|
||||
// Use this path as the default download directory after creation succeeds
|
||||
AsDefaultPath bool `json:"asDefaultPath,omitempty"`
|
||||
// Select file indexes to download
|
||||
SelectFiles []int `json:"selectFiles"`
|
||||
// Extra info for specific fetcher
|
||||
@@ -187,6 +208,8 @@ type DownloaderStoreConfig struct {
|
||||
Script *ScriptConfig `json:"script"` // Script is the script execution configuration
|
||||
AutoTorrent *AutoTorrentConfig `json:"autoTorrent"` // AutoTorrent is the auto torrent task creation configuration
|
||||
Archive *ArchiveConfig `json:"archive"` // Archive is the archive extraction configuration
|
||||
API *APIServerConfig `json:"api"` // API is the optional REST server configuration
|
||||
AutoStartTasks bool `json:"autoStartTasks"` // AutoStartTasks continues all unfinished tasks when the backend starts
|
||||
AutoDeleteMissingFileTasks bool `json:"autoDeleteMissingFileTasks"` // AutoDeleteMissingFileTasks enables automatic deletion of tasks with missing files
|
||||
}
|
||||
|
||||
@@ -252,6 +275,27 @@ func (cfg *DownloaderStoreConfig) Merge(beforeCfg *DownloaderStoreConfig) *Downl
|
||||
if cfg.Archive == nil {
|
||||
cfg.Archive = beforeCfg.Archive
|
||||
}
|
||||
if cfg.API == nil {
|
||||
cfg.API = beforeCfg.API
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
type APIServerConfig struct {
|
||||
Enable bool `json:"enable"`
|
||||
MCPEnable bool `json:"mcpEnable"`
|
||||
Network string `json:"network"`
|
||||
Address string `json:"address"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
func (cfg *APIServerConfig) Init() *APIServerConfig {
|
||||
if cfg.Network == "" {
|
||||
cfg.Network = "tcp"
|
||||
}
|
||||
if cfg.Address == "" {
|
||||
cfg.Address = "127.0.0.1:9999"
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/bits"
|
||||
)
|
||||
|
||||
const (
|
||||
// PieceMapEncoding identifies the one-bit wire format used by PieceMap.
|
||||
// One byte stores eight ordered pieces, least-significant bit first: piece
|
||||
// 0 uses bit 0, piece 1 uses bit 1, and so on. A set bit means the piece is
|
||||
// locally complete and verified; a clear bit means it is not complete.
|
||||
PieceMapEncoding = "bitset-v1"
|
||||
|
||||
piecesPerByte = 8
|
||||
)
|
||||
|
||||
var ErrPieceIndexOutOfRange = errors.New("piece index out of range")
|
||||
|
||||
// PieceMap stores the verified completion state of ordered pieces as a bitset.
|
||||
// It deliberately excludes scheduler states such as downloading, hashing and
|
||||
// failed: those states are transient and are not stable across pause/restart.
|
||||
//
|
||||
// Fields are private so PieceCount, CompletedPieces and packed bytes remain
|
||||
// consistent. Use Add, Delete, Update and Get for mutation, and Bytes when a
|
||||
// binary copy is required. JSON serializes Data as Base64.
|
||||
type PieceMap struct {
|
||||
pieceCount int
|
||||
pieceSize int64
|
||||
completedPieces int
|
||||
data []byte
|
||||
}
|
||||
|
||||
// NewPieceMap creates a map containing pieceCount incomplete pieces.
|
||||
func NewPieceMap(pieceCount int, pieceSize int64) *PieceMap {
|
||||
if pieceCount < 0 {
|
||||
pieceCount = 0
|
||||
}
|
||||
return &PieceMap{
|
||||
pieceCount: pieceCount,
|
||||
pieceSize: pieceSize,
|
||||
data: make([]byte, packedPieceByteCount(pieceCount)),
|
||||
}
|
||||
}
|
||||
|
||||
// NewPieceMapFromBytes restores a bitset-v1 map. Input bytes are copied and
|
||||
// unused high bits in the last byte are cleared.
|
||||
func NewPieceMapFromBytes(pieceCount int, pieceSize int64, data []byte) (*PieceMap, error) {
|
||||
if pieceCount < 0 {
|
||||
return nil, fmt.Errorf("%w: negative piece count", ErrPieceIndexOutOfRange)
|
||||
}
|
||||
expectedBytes := packedPieceByteCount(pieceCount)
|
||||
if len(data) != expectedBytes {
|
||||
return nil, fmt.Errorf("invalid piece data length: got %d, want %d", len(data), expectedBytes)
|
||||
}
|
||||
|
||||
pieceMap := &PieceMap{
|
||||
pieceCount: pieceCount,
|
||||
pieceSize: pieceSize,
|
||||
data: append([]byte(nil), data...),
|
||||
}
|
||||
pieceMap.clearUnusedBits()
|
||||
for _, value := range pieceMap.data {
|
||||
pieceMap.completedPieces += bits.OnesCount8(value)
|
||||
}
|
||||
return pieceMap, nil
|
||||
}
|
||||
|
||||
// Clone returns an independent ordered snapshot.
|
||||
func (m *PieceMap) Clone() *PieceMap {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
return &PieceMap{
|
||||
pieceCount: m.pieceCount,
|
||||
pieceSize: m.pieceSize,
|
||||
completedPieces: m.completedPieces,
|
||||
data: append([]byte(nil), m.data...),
|
||||
}
|
||||
}
|
||||
|
||||
// Add appends a piece while preserving all existing indexes.
|
||||
func (m *PieceMap) Add(completed bool) {
|
||||
if m.pieceCount%piecesPerByte == 0 {
|
||||
m.data = append(m.data, 0)
|
||||
}
|
||||
m.pieceCount++
|
||||
if completed {
|
||||
m.setCompleted(m.pieceCount-1, true)
|
||||
m.completedPieces++
|
||||
}
|
||||
}
|
||||
|
||||
// Delete removes a piece and shifts every following piece one index left.
|
||||
func (m *PieceMap) Delete(index int) error {
|
||||
if !m.validIndex(index) {
|
||||
return ErrPieceIndexOutOfRange
|
||||
}
|
||||
if m.completedAt(index) {
|
||||
m.completedPieces--
|
||||
}
|
||||
for current := index; current < m.pieceCount-1; current++ {
|
||||
m.setCompleted(current, m.completedAt(current+1))
|
||||
}
|
||||
m.pieceCount--
|
||||
m.data = m.data[:packedPieceByteCount(m.pieceCount)]
|
||||
m.clearUnusedBits()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update changes whether the piece at index is complete and verified.
|
||||
func (m *PieceMap) Update(index int, completed bool) error {
|
||||
if !m.validIndex(index) {
|
||||
return ErrPieceIndexOutOfRange
|
||||
}
|
||||
previous := m.completedAt(index)
|
||||
if previous == completed {
|
||||
return nil
|
||||
}
|
||||
if completed {
|
||||
m.completedPieces++
|
||||
} else {
|
||||
m.completedPieces--
|
||||
}
|
||||
m.setCompleted(index, completed)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get reports whether the exact piece index is complete and verified.
|
||||
func (m *PieceMap) Get(index int) (bool, error) {
|
||||
if !m.validIndex(index) {
|
||||
return false, ErrPieceIndexOutOfRange
|
||||
}
|
||||
return m.completedAt(index), nil
|
||||
}
|
||||
|
||||
// Bytes returns an independent copy of the packed bitset-v1 bytes.
|
||||
func (m *PieceMap) Bytes() []byte {
|
||||
return append([]byte(nil), m.data...)
|
||||
}
|
||||
|
||||
func (m *PieceMap) PieceCount() int { return m.pieceCount }
|
||||
func (m *PieceMap) PieceSize() int64 { return m.pieceSize }
|
||||
func (m *PieceMap) CompletedPieces() int { return m.completedPieces }
|
||||
|
||||
// MarshalJSON publishes the compact bytes without exposing mutable storage.
|
||||
func (m PieceMap) MarshalJSON() ([]byte, error) {
|
||||
type pieceMapJSON struct {
|
||||
Encoding string `json:"encoding"`
|
||||
PieceCount int `json:"pieceCount"`
|
||||
PieceSize int64 `json:"pieceSize"`
|
||||
CompletedPieces int `json:"completedPieces"`
|
||||
Data []byte `json:"data"`
|
||||
}
|
||||
return json.Marshal(pieceMapJSON{
|
||||
Encoding: PieceMapEncoding,
|
||||
PieceCount: m.pieceCount,
|
||||
PieceSize: m.pieceSize,
|
||||
CompletedPieces: m.completedPieces,
|
||||
Data: m.data,
|
||||
})
|
||||
}
|
||||
|
||||
func (m *PieceMap) validIndex(index int) bool {
|
||||
return m != nil && index >= 0 && index < m.pieceCount
|
||||
}
|
||||
|
||||
func (m *PieceMap) completedAt(index int) bool {
|
||||
byteIndex, mask := piecePosition(index)
|
||||
return m.data[byteIndex]&mask != 0
|
||||
}
|
||||
|
||||
func (m *PieceMap) setCompleted(index int, completed bool) {
|
||||
byteIndex, mask := piecePosition(index)
|
||||
if completed {
|
||||
m.data[byteIndex] |= mask
|
||||
} else {
|
||||
m.data[byteIndex] &^= mask
|
||||
}
|
||||
}
|
||||
|
||||
func (m *PieceMap) clearUnusedBits() {
|
||||
if m.pieceCount == 0 || m.pieceCount%piecesPerByte == 0 {
|
||||
return
|
||||
}
|
||||
usedBits := uint(m.pieceCount % piecesPerByte)
|
||||
m.data[len(m.data)-1] &= byte(1<<usedBits) - 1
|
||||
}
|
||||
|
||||
func piecePosition(index int) (byteIndex int, mask byte) {
|
||||
return index / piecesPerByte, byte(1 << uint(index%piecesPerByte))
|
||||
}
|
||||
|
||||
func packedPieceByteCount(pieceCount int) int {
|
||||
return (pieceCount + piecesPerByte - 1) / piecesPerByte
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPieceMapCRUDAndBytes(t *testing.T) {
|
||||
pieceMap := NewPieceMap(4, 16*1024)
|
||||
_ = pieceMap.Update(0, true)
|
||||
_ = pieceMap.Update(2, true)
|
||||
|
||||
if got, want := pieceMap.Bytes(), []byte{0b00000101}; len(got) != 1 || got[0] != want[0] {
|
||||
t.Fatalf("Bytes() = %08b, want %08b", got, want)
|
||||
}
|
||||
if got := pieceMap.CompletedPieces(); got != 2 {
|
||||
t.Fatalf("CompletedPieces() = %d, want 2", got)
|
||||
}
|
||||
|
||||
pieceMap.Add(true)
|
||||
if got := pieceMap.PieceCount(); got != 5 {
|
||||
t.Fatalf("PieceCount() = %d, want 5", got)
|
||||
}
|
||||
if err := pieceMap.Delete(1); err != nil {
|
||||
t.Fatalf("Delete: %v", err)
|
||||
}
|
||||
wantAfterDelete := []bool{true, true, false, true}
|
||||
for index, want := range wantAfterDelete {
|
||||
got, err := pieceMap.Get(index)
|
||||
if err != nil || got != want {
|
||||
t.Fatalf("Get(%d) = %v, %v; want %v", index, got, err, want)
|
||||
}
|
||||
}
|
||||
if got := pieceMap.CompletedPieces(); got != 3 {
|
||||
t.Fatalf("CompletedPieces() after delete = %d, want 3", got)
|
||||
}
|
||||
|
||||
bytesCopy := pieceMap.Bytes()
|
||||
bytesCopy[0] = 0
|
||||
got, _ := pieceMap.Get(0)
|
||||
if !got {
|
||||
t.Fatal("Bytes returned mutable internal storage")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPieceMapValidationAndRestore(t *testing.T) {
|
||||
pieceMap, err := NewPieceMapFromBytes(10, 1024, []byte{0b10010001, 0xff})
|
||||
if err != nil {
|
||||
t.Fatalf("NewPieceMapFromBytes: %v", err)
|
||||
}
|
||||
// Only the low positions for pieces 8 and 9 belong to the map.
|
||||
if got, want := pieceMap.Bytes()[1], byte(0b00000011); got != want {
|
||||
t.Fatalf("last byte = %08b, want %08b", got, want)
|
||||
}
|
||||
if pieceMap.CompletedPieces() != 5 {
|
||||
t.Fatalf("CompletedPieces() = %d, want 5", pieceMap.CompletedPieces())
|
||||
}
|
||||
|
||||
if _, err := NewPieceMapFromBytes(10, 1024, []byte{0}); err == nil {
|
||||
t.Fatal("expected invalid byte length error")
|
||||
}
|
||||
if err := pieceMap.Update(10, true); !errors.Is(err, ErrPieceIndexOutOfRange) {
|
||||
t.Fatalf("Update out of range error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPieceMapJSON(t *testing.T) {
|
||||
pieceMap := NewPieceMap(4, 4096)
|
||||
_ = pieceMap.Update(0, true)
|
||||
_ = pieceMap.Update(2, true)
|
||||
|
||||
data, err := json.Marshal(pieceMap)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal: %v", err)
|
||||
}
|
||||
var got map[string]any
|
||||
if err := json.Unmarshal(data, &got); err != nil {
|
||||
t.Fatalf("Unmarshal JSON: %v", err)
|
||||
}
|
||||
if got["encoding"] != PieceMapEncoding || got["data"] != "BQ==" {
|
||||
t.Fatalf("unexpected JSON: %s", data)
|
||||
}
|
||||
if got["pieceCount"] != float64(4) || got["pieceSize"] != float64(4096) || got["completedPieces"] != float64(2) {
|
||||
t.Fatalf("unexpected metadata: %s", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPieceMapCloneIsIndependent(t *testing.T) {
|
||||
pieceMap := NewPieceMap(4, 4096)
|
||||
_ = pieceMap.Update(0, true)
|
||||
snapshot := pieceMap.Clone()
|
||||
_ = snapshot.Update(1, true)
|
||||
|
||||
if got, _ := pieceMap.Get(1); got {
|
||||
t.Fatal("Clone mutated the source map")
|
||||
}
|
||||
if snapshot.CompletedPieces() != 2 {
|
||||
t.Fatalf("clone completed pieces = %d, want 2", snapshot.CompletedPieces())
|
||||
}
|
||||
if (*PieceMap)(nil).Clone() != nil {
|
||||
t.Fatal("nil map clone must stay nil")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package base
|
||||
|
||||
// PeerStats is the common row returned by protocols that expose connected
|
||||
// peers. Speeds contain payload bytes per second rather than protocol overhead.
|
||||
type PeerStats struct {
|
||||
Address string `json:"address"`
|
||||
Client string `json:"client"`
|
||||
DownloadSpeed int64 `json:"downloadSpeed"`
|
||||
UploadSpeed int64 `json:"uploadSpeed"`
|
||||
PieceCount int `json:"pieceCount"`
|
||||
// Completion is the known remote data completion ratio in the range 0–1.
|
||||
// It is omitted when a protocol cannot determine the remote total.
|
||||
Completion *float64 `json:"completion,omitempty"`
|
||||
// Relevance is the ratio of locally wanted, missing pieces that this peer
|
||||
// claims to have. It is omitted when the local missing set is unavailable or empty.
|
||||
Relevance *float64 `json:"relevance,omitempty"`
|
||||
Source string `json:"source"`
|
||||
// Transport is the active connection transport, for example tcp or utp.
|
||||
// It is intentionally separate from Source, which describes peer discovery.
|
||||
Transport string `json:"transport"`
|
||||
}
|
||||
+908
-166
File diff suppressed because it is too large.
Load diff
+1263
-40
File diff suppressed because it is too large.
Load diff
+169
-96
@@ -3,16 +3,19 @@ package engine
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/GopeedLab/gopeed/pkg/base"
|
||||
gojaerror "github.com/GopeedLab/gopeed/pkg/download/engine/inject/error"
|
||||
fetchapi "github.com/GopeedLab/gopeed/pkg/download/engine/inject/fetch"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/file"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/formdata"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/stream"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/vm"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/xhr"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/dop251/goja_nodejs/eventloop"
|
||||
gojaurl "github.com/dop251/goja_nodejs/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
//go:embed polyfill/out/index.js
|
||||
@@ -21,96 +24,143 @@ var polyfillScript string
|
||||
type Engine struct {
|
||||
loop *eventloop.EventLoop
|
||||
|
||||
closeOnce sync.Once
|
||||
cleanupMu sync.Mutex
|
||||
cleanups []func()
|
||||
|
||||
Runtime *goja.Runtime
|
||||
}
|
||||
|
||||
type JSFunction func(goja.FunctionCall) goja.Value
|
||||
|
||||
// RunString executes the script and returns the go type value
|
||||
// if script result is promise, it will be resolved
|
||||
func (e *Engine) RunString(script string) (value any, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = r.(error)
|
||||
}
|
||||
}()
|
||||
|
||||
var result goja.Value
|
||||
e.loop.Run(func(runtime *goja.Runtime) {
|
||||
result, err = runtime.RunString(script)
|
||||
if err == nil {
|
||||
go e.await(result)
|
||||
}
|
||||
return e.runOnLoop(func(runtime *goja.Runtime) (goja.Value, error) {
|
||||
return runtime.RunString(script)
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return resolveResult(result)
|
||||
}
|
||||
|
||||
// CallFunction calls the function and returns the go type value
|
||||
// if function result is promise, it will be resolved
|
||||
func (e *Engine) CallFunction(fn goja.Callable, args ...any) (value any, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = r.(error)
|
||||
func (e *Engine) CallFunction(fn any, args ...any) (value any, err error) {
|
||||
return e.runOnLoop(func(runtime *goja.Runtime) (goja.Value, error) {
|
||||
var jsArgs []goja.Value
|
||||
for _, arg := range args {
|
||||
jsArgs = append(jsArgs, runtime.ToValue(arg))
|
||||
}
|
||||
}()
|
||||
|
||||
var result goja.Value
|
||||
e.loop.Run(func(runtime *goja.Runtime) {
|
||||
if args == nil {
|
||||
result, err = fn(nil)
|
||||
} else {
|
||||
var jsArgs []goja.Value
|
||||
for _, arg := range args {
|
||||
jsArgs = append(jsArgs, runtime.ToValue(arg))
|
||||
switch f := fn.(type) {
|
||||
case goja.Callable:
|
||||
if args == nil {
|
||||
return f(nil)
|
||||
}
|
||||
result, err = fn(nil, jsArgs...)
|
||||
}
|
||||
if err == nil {
|
||||
go e.await(result)
|
||||
return f(nil, jsArgs...)
|
||||
case JSFunction:
|
||||
return callExportedFunction(func(call goja.FunctionCall) goja.Value {
|
||||
return f(call)
|
||||
}, jsArgs...)
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported function type: %T", fn)
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return resolveResult(result)
|
||||
}
|
||||
|
||||
// loop.Run will hang if the script result has a non-stop code, such as setInterval.
|
||||
// This method will stop the event loop when the promise result is resolved.
|
||||
func (e *Engine) await(value any) {
|
||||
if value == nil {
|
||||
return
|
||||
func (e *Engine) runOnLoop(fn func(runtime *goja.Runtime) (goja.Value, error)) (any, error) {
|
||||
type result struct {
|
||||
value any
|
||||
err error
|
||||
}
|
||||
|
||||
if v, ok := value.(goja.Value); ok {
|
||||
// if result is promise, wait for it to be resolved
|
||||
if p, ok := v.Export().(*goja.Promise); ok {
|
||||
if p.State() != goja.PromiseStatePending {
|
||||
ch := make(chan result, 1)
|
||||
ok := e.loop.RunOnLoop(func(runtime *goja.Runtime) {
|
||||
var once sync.Once
|
||||
sendResult := func(res result) {
|
||||
once.Do(func() {
|
||||
ch <- res
|
||||
})
|
||||
}
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
switch v := r.(type) {
|
||||
case error:
|
||||
sendResult(result{err: v})
|
||||
case goja.Value:
|
||||
sendResult(result{err: exportJSError(v)})
|
||||
default:
|
||||
sendResult(result{err: fmt.Errorf("panic: %v", r)})
|
||||
}
|
||||
}
|
||||
}()
|
||||
value, err := fn(runtime)
|
||||
if err != nil {
|
||||
sendResult(result{err: err})
|
||||
return
|
||||
}
|
||||
if p, ok := value.Export().(*goja.Promise); ok {
|
||||
switch p.State() {
|
||||
case goja.PromiseStateFulfilled:
|
||||
sendResult(result{value: exportJSValue(p.Result())})
|
||||
return
|
||||
case goja.PromiseStateRejected:
|
||||
sendResult(result{err: exportJSError(p.Result())})
|
||||
return
|
||||
}
|
||||
|
||||
// check promise state every 100 milliseconds, until it is resolved
|
||||
for {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
if p.State() == goja.PromiseStatePending {
|
||||
continue
|
||||
}
|
||||
break
|
||||
promiseObj := value.ToObject(runtime)
|
||||
thenVal := promiseObj.Get("then")
|
||||
thenFn, ok := goja.AssertFunction(thenVal)
|
||||
if !ok {
|
||||
sendResult(result{err: errors.New("promise.then is not callable")})
|
||||
return
|
||||
}
|
||||
|
||||
// stop the event loop
|
||||
e.loop.StopNoWait()
|
||||
onFulfilled := runtime.ToValue(func(call goja.FunctionCall) goja.Value {
|
||||
sendResult(result{value: exportJSValue(call.Argument(0))})
|
||||
return goja.Undefined()
|
||||
})
|
||||
onRejected := runtime.ToValue(func(call goja.FunctionCall) goja.Value {
|
||||
sendResult(result{err: exportJSError(call.Argument(0))})
|
||||
return goja.Undefined()
|
||||
})
|
||||
if _, err := thenFn(promiseObj, onFulfilled, onRejected); err != nil {
|
||||
sendResult(result{err: err})
|
||||
}
|
||||
return
|
||||
}
|
||||
sendResult(result{value: exportJSValue(value)})
|
||||
})
|
||||
if !ok {
|
||||
return nil, errors.New("engine loop terminated")
|
||||
}
|
||||
res := <-ch
|
||||
if res.err != nil {
|
||||
return nil, res.err
|
||||
}
|
||||
return res.value, nil
|
||||
}
|
||||
|
||||
func (e *Engine) Close() {
|
||||
e.loop.StopNoWait()
|
||||
e.closeOnce.Do(func() {
|
||||
e.cleanupMu.Lock()
|
||||
cleanups := e.cleanups
|
||||
e.cleanups = nil
|
||||
e.cleanupMu.Unlock()
|
||||
for _, cleanup := range cleanups {
|
||||
cleanup()
|
||||
}
|
||||
e.loop.Terminate()
|
||||
})
|
||||
}
|
||||
|
||||
func (e *Engine) addCleanup(cleanup func()) {
|
||||
if cleanup == nil {
|
||||
return
|
||||
}
|
||||
e.cleanupMu.Lock()
|
||||
e.cleanups = append(e.cleanups, cleanup)
|
||||
e.cleanupMu.Unlock()
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
ProxyConfig *base.DownloaderProxyConfig
|
||||
ProxyConfig *base.DownloaderProxyConfig
|
||||
StreamConfig *stream.Config
|
||||
}
|
||||
|
||||
func NewEngine(cfg *Config) *Engine {
|
||||
@@ -121,7 +171,10 @@ func NewEngine(cfg *Config) *Engine {
|
||||
engine := &Engine{
|
||||
loop: loop,
|
||||
}
|
||||
loop.Run(func(runtime *goja.Runtime) {
|
||||
loop.Start()
|
||||
done := make(chan struct{})
|
||||
loop.RunOnLoop(func(runtime *goja.Runtime) {
|
||||
defer close(done)
|
||||
engine.Runtime = runtime
|
||||
runtime.SetFieldNameMapper(goja.TagFieldNameMapper("json", true))
|
||||
vm.Enable(runtime)
|
||||
@@ -132,12 +185,6 @@ func NewEngine(cfg *Config) *Engine {
|
||||
if err := file.Enable(runtime); err != nil {
|
||||
return
|
||||
}
|
||||
if err := formdata.Enable(runtime); err != nil {
|
||||
return
|
||||
}
|
||||
if err := xhr.Enable(runtime, cfg.ProxyConfig.ToHandler()); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err := runtime.RunString(polyfillScript); err != nil {
|
||||
return
|
||||
}
|
||||
@@ -153,8 +200,21 @@ func NewEngine(cfg *Config) *Engine {
|
||||
if _, err := runtime.RunString("global.location = new URL('http://localhost');"); err != nil {
|
||||
return
|
||||
}
|
||||
if err := stream.Enable(runtime, loop, cfg.StreamConfig); err != nil {
|
||||
return
|
||||
}
|
||||
if err := fetchapi.Enable(runtime, loop, &fetchapi.Config{
|
||||
ProxyHandler: cfg.ProxyConfig.ToHandler(),
|
||||
RegisterCleanup: engine.addCleanup,
|
||||
}); err != nil {
|
||||
return
|
||||
}
|
||||
if err := xhr.Enable(runtime); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
})
|
||||
<-done
|
||||
return engine
|
||||
}
|
||||
|
||||
@@ -163,32 +223,45 @@ func Run(script string) (value any, err error) {
|
||||
return engine.RunString(script)
|
||||
}
|
||||
|
||||
// if the value is Promise, it will be resolved and return the result.
|
||||
func resolveResult(value goja.Value) (any, error) {
|
||||
export := value.Export()
|
||||
switch export.(type) {
|
||||
case *goja.Promise:
|
||||
p := export.(*goja.Promise)
|
||||
switch p.State() {
|
||||
case goja.PromiseStatePending:
|
||||
return nil, nil
|
||||
case goja.PromiseStateFulfilled:
|
||||
return p.Result().Export(), nil
|
||||
case goja.PromiseStateRejected:
|
||||
if err, ok := p.Result().Export().(error); ok {
|
||||
return nil, err
|
||||
} else {
|
||||
stack := p.Result().String()
|
||||
result := p.Result()
|
||||
if ro, ok := result.(*goja.Object); ok {
|
||||
stackVal := ro.Get("stack")
|
||||
if stackVal != nil && stackVal.String() != "" {
|
||||
stack = stackVal.String()
|
||||
}
|
||||
}
|
||||
return nil, errors.New(stack)
|
||||
}
|
||||
func exportJSValue(value goja.Value) any {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
return value.Export()
|
||||
}
|
||||
|
||||
func exportJSError(value goja.Value) error {
|
||||
if value == nil || goja.IsUndefined(value) || goja.IsNull(value) {
|
||||
return errors.New("promise rejected")
|
||||
}
|
||||
if err, ok := value.Export().(error); ok {
|
||||
return err
|
||||
}
|
||||
stack := value.String()
|
||||
if ro, ok := value.(*goja.Object); ok {
|
||||
stackVal := ro.Get("stack")
|
||||
if stackVal != nil && stackVal.String() != "" {
|
||||
stack = stackVal.String()
|
||||
}
|
||||
}
|
||||
return export, nil
|
||||
return errors.New(stack)
|
||||
}
|
||||
|
||||
func callExportedFunction(fn func(goja.FunctionCall) goja.Value, args ...goja.Value) (ret goja.Value, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
switch v := r.(type) {
|
||||
case error:
|
||||
err = v
|
||||
case goja.Value:
|
||||
err = exportJSError(v)
|
||||
default:
|
||||
err = fmt.Errorf("panic: %v", r)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return fn(goja.FunctionCall{
|
||||
This: nil,
|
||||
Arguments: args,
|
||||
}), nil
|
||||
}
|
||||
@@ -26,7 +26,11 @@ func TestPolyfill(t *testing.T) {
|
||||
doTestPolyfill(t, "MessageError")
|
||||
doTestPolyfill(t, "XMLHttpRequest")
|
||||
doTestPolyfill(t, "Blob")
|
||||
doTestPolyfill(t, "Event")
|
||||
doTestPolyfill(t, "EventTarget")
|
||||
doTestPolyfill(t, "FormData")
|
||||
doTestPolyfill(t, "AbortController")
|
||||
doTestPolyfill(t, "AbortSignal")
|
||||
doTestPolyfill(t, "TextDecoder")
|
||||
doTestPolyfill(t, "TextEncoder")
|
||||
doTestPolyfill(t, "fetch")
|
||||
@@ -36,13 +40,75 @@ func TestPolyfill(t *testing.T) {
|
||||
func TestError(t *testing.T) {
|
||||
engine := NewEngine(nil)
|
||||
_, err := engine.RunString(`
|
||||
throw new MessageError('test');
|
||||
throw new MessageError('test');
|
||||
`)
|
||||
if me, ok := gojautil.AssertError[*gojaerror.MessageError](err); !ok {
|
||||
t.Fatalf("expect MessageError, but got %v", me)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallFunction_DetachedAsyncWorkDoesNotBlockReturn(t *testing.T) {
|
||||
engine := NewEngine(nil)
|
||||
defer engine.Close()
|
||||
if _, err := engine.RunString(`
|
||||
globalThis.__done = false;
|
||||
async function testDetached() {
|
||||
(async () => {
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
globalThis.__done = true;
|
||||
})();
|
||||
return "ok";
|
||||
}
|
||||
`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fn, ok := goja.AssertFunction(engine.Runtime.Get("testDetached"))
|
||||
if !ok {
|
||||
t.Fatal("testDetached is not callable")
|
||||
}
|
||||
startedAt := time.Now()
|
||||
value, err := engine.CallFunction(fn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
elapsed := time.Since(startedAt)
|
||||
if elapsed > 200*time.Millisecond {
|
||||
t.Fatalf("detached async work blocked return for %s", elapsed)
|
||||
}
|
||||
if value != "ok" {
|
||||
t.Fatalf("unexpected return value: %#v", value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCallFunction_AwaitedPromiseBlocksUntilSettled(t *testing.T) {
|
||||
engine := NewEngine(nil)
|
||||
defer engine.Close()
|
||||
if _, err := engine.RunString(`
|
||||
async function testAwaited() {
|
||||
await new Promise((resolve) => setTimeout(resolve, 120));
|
||||
return "ok";
|
||||
}
|
||||
`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fn, ok := goja.AssertFunction(engine.Runtime.Get("testAwaited"))
|
||||
if !ok {
|
||||
t.Fatal("testAwaited is not callable")
|
||||
}
|
||||
startedAt := time.Now()
|
||||
value, err := engine.CallFunction(fn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
elapsed := time.Since(startedAt)
|
||||
if elapsed < 100*time.Millisecond {
|
||||
t.Fatalf("awaited promise returned too early: %s", elapsed)
|
||||
}
|
||||
if value != "ok" {
|
||||
t.Fatalf("unexpected return value: %#v", value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetch(t *testing.T) {
|
||||
server := startServer()
|
||||
defer server.Close()
|
||||
@@ -64,6 +130,50 @@ async function testText(){
|
||||
return await resp.text();
|
||||
}
|
||||
|
||||
async function testResponseBodyGetReader() {
|
||||
const resp = await fetch(host+'/text', {
|
||||
method: 'POST',
|
||||
body: 'stream-body'
|
||||
});
|
||||
if (!resp.body || typeof resp.body.getReader !== 'function') {
|
||||
throw new Error('response.body.getReader is not available');
|
||||
}
|
||||
const reader = resp.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let result = '';
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
result += decoder.decode(value, { stream: true });
|
||||
}
|
||||
result += decoder.decode();
|
||||
return result;
|
||||
}
|
||||
|
||||
async function testStreamingFetchResponseBody() {
|
||||
const startedAt = Date.now();
|
||||
const resp = await fetch(host+'/stream');
|
||||
const resolvedAt = Date.now() - startedAt;
|
||||
const reader = resp.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
const first = await reader.read();
|
||||
const firstAt = Date.now() - startedAt;
|
||||
const second = await reader.read();
|
||||
const secondAt = Date.now() - startedAt;
|
||||
let result = '';
|
||||
result += decoder.decode(first.value, { stream: true });
|
||||
result += decoder.decode(second.value, { stream: true });
|
||||
result += decoder.decode();
|
||||
return {
|
||||
resolvedAt,
|
||||
firstAt,
|
||||
secondAt,
|
||||
text: result
|
||||
};
|
||||
}
|
||||
|
||||
async function testOctetStream(file){
|
||||
const resp = await fetch(host+'/octetStream',{
|
||||
method: 'POST',
|
||||
@@ -251,6 +361,32 @@ async function testFingerprintSafari(){
|
||||
t.Fatalf("testText failed, want %s, got %s", "test", result)
|
||||
}
|
||||
|
||||
result, err = callTestFun(engine, "testResponseBodyGetReader")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result != "stream-body" {
|
||||
t.Fatalf("testResponseBodyGetReader failed, want %s, got %s", "stream-body", result)
|
||||
}
|
||||
|
||||
result, err = callTestFun(engine, "testStreamingFetchResponseBody")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
streaming := result.(map[string]any)
|
||||
if streaming["text"] != "chunk-1chunk-2" {
|
||||
t.Fatalf("testStreamingFetchResponseBody failed, want %s, got %v", "chunk-1chunk-2", streaming["text"])
|
||||
}
|
||||
if int(streaming["resolvedAt"].(int64)) >= 120 {
|
||||
t.Fatalf("fetch resolved too late, got %dms", streaming["resolvedAt"].(int64))
|
||||
}
|
||||
if int(streaming["firstAt"].(int64)) >= 120 {
|
||||
t.Fatalf("first chunk arrived too late, got %dms", streaming["firstAt"].(int64))
|
||||
}
|
||||
if int(streaming["secondAt"].(int64)) < 120 {
|
||||
t.Fatalf("second chunk arrived too early, got %dms", streaming["secondAt"].(int64))
|
||||
}
|
||||
|
||||
func() {
|
||||
jsFile, _, md5 := buildFile(t, engine.Runtime)
|
||||
result, err = callTestFun(engine, "testOctetStream", jsFile)
|
||||
@@ -464,6 +600,18 @@ func startServer() net.Listener {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(buf)
|
||||
})
|
||||
mux.HandleFunc("/stream", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if flusher, ok := w.(http.Flusher); ok {
|
||||
_, _ = w.Write([]byte("chunk-1"))
|
||||
flusher.Flush()
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
_, _ = w.Write([]byte("chunk-2"))
|
||||
flusher.Flush()
|
||||
return
|
||||
}
|
||||
_, _ = w.Write([]byte("chunk-1chunk-2"))
|
||||
})
|
||||
mux.HandleFunc("/octetStream", func(w http.ResponseWriter, r *http.Request) {
|
||||
md5 := calcMd5(r.Body)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
package engine_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine"
|
||||
)
|
||||
|
||||
func TestFetchDoesNotDependOnXMLHttpRequest(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/target", func(w http.ResponseWriter, request *http.Request) {
|
||||
w.Header().Set("X-Method", request.Method)
|
||||
_, _ = w.Write([]byte("ok"))
|
||||
})
|
||||
mux.HandleFunc("/redirect", func(w http.ResponseWriter, request *http.Request) {
|
||||
http.Redirect(w, request, "/target", http.StatusFound)
|
||||
})
|
||||
server := httptest.NewServer(mux)
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
runtime := engine.NewEngine(nil)
|
||||
t.Cleanup(runtime.Close)
|
||||
|
||||
value, err := runtime.RunString(fmt.Sprintf(`
|
||||
(async () => {
|
||||
globalThis.XMLHttpRequest = function () {
|
||||
throw new Error("fetch used XMLHttpRequest");
|
||||
};
|
||||
|
||||
const get = await fetch(%q);
|
||||
const head = await fetch(%q, { method: "HEAD" });
|
||||
const manual = await fetch(%q, { redirect: "manual" });
|
||||
let redirectError = false;
|
||||
try {
|
||||
await fetch(%q, { redirect: "error" });
|
||||
} catch (_) {
|
||||
redirectError = true;
|
||||
}
|
||||
|
||||
return JSON.stringify({
|
||||
get: await get.text(),
|
||||
headStatus: head.status,
|
||||
headMethod: head.headers.get("x-method"),
|
||||
manualStatus: manual.status,
|
||||
manualLocation: manual.headers.get("location"),
|
||||
redirectError,
|
||||
});
|
||||
})()
|
||||
`, server.URL+"/target", server.URL+"/target", server.URL+"/redirect", server.URL+"/redirect"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const want = `{"get":"ok","headStatus":200,"headMethod":"HEAD","manualStatus":302,"manualLocation":"/target","redirectError":true}`
|
||||
if value != want {
|
||||
t.Fatalf("unexpected fetch result: got %#v want %s", value, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestXMLHttpRequestUsesFetch(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("X-Test", "value")
|
||||
_, _ = w.Write([]byte("payload"))
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
runtime := engine.NewEngine(nil)
|
||||
t.Cleanup(runtime.Close)
|
||||
|
||||
value, err := runtime.RunString(fmt.Sprintf(`
|
||||
(async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
let fetchCalls = 0;
|
||||
globalThis.fetch = function (...args) {
|
||||
fetchCalls++;
|
||||
return originalFetch(...args);
|
||||
};
|
||||
|
||||
const result = await new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const states = [];
|
||||
xhr.onreadystatechange = () => states.push(xhr.readyState);
|
||||
xhr.onerror = () => reject(new Error("XHR failed"));
|
||||
xhr.onload = () => resolve({
|
||||
status: xhr.status,
|
||||
text: xhr.responseText,
|
||||
header: xhr.getResponseHeader("x-test"),
|
||||
states,
|
||||
});
|
||||
xhr.open("GET", %q);
|
||||
xhr.send();
|
||||
});
|
||||
result.fetchCalls = fetchCalls;
|
||||
return JSON.stringify(result);
|
||||
})()
|
||||
`, server.URL))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const want = `{"status":200,"text":"payload","header":"value","states":[1,2,3,4],"fetchCalls":1}`
|
||||
if value != want {
|
||||
t.Fatalf("unexpected XMLHttpRequest result: got %#v want %s", value, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchResponseAndRequestCloneHaveIndependentBodies(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
||||
if request.Method == http.MethodPost {
|
||||
_, _ = response.Write([]byte("posted"))
|
||||
return
|
||||
}
|
||||
_, _ = response.Write([]byte("payload"))
|
||||
}))
|
||||
t.Cleanup(server.Close)
|
||||
|
||||
runtime := engine.NewEngine(nil)
|
||||
t.Cleanup(runtime.Close)
|
||||
|
||||
value, err := runtime.RunString(fmt.Sprintf(`
|
||||
(async () => {
|
||||
const response = await fetch(%q);
|
||||
const responseClone = response.clone();
|
||||
const responseBodies = await Promise.all([response.text(), responseClone.text()]);
|
||||
|
||||
const request = new Request(%q, { method: "POST", body: "request-body" });
|
||||
const requestClone = request.clone();
|
||||
const requestBodies = await Promise.all([request.text(), requestClone.text()]);
|
||||
|
||||
return JSON.stringify({ responseBodies, requestBodies });
|
||||
})()
|
||||
`, server.URL, server.URL))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const want = `{"responseBodies":["payload","payload"],"requestBodies":["request-body","request-body"]}`
|
||||
if value != want {
|
||||
t.Fatalf("unexpected clone result: got %#v want %s", value, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFetchFormDataUsesWebEntryListSemantics(t *testing.T) {
|
||||
runtime := engine.NewEngine(nil)
|
||||
t.Cleanup(runtime.Close)
|
||||
|
||||
value, err := runtime.RunString(`
|
||||
(() => {
|
||||
const data = new FormData();
|
||||
data.append("a", "one");
|
||||
data.append("a", 2);
|
||||
data.append("b", "three");
|
||||
const beforeSet = Array.from(data.entries());
|
||||
data.set("a", "four");
|
||||
const afterSet = Array.from(data);
|
||||
const all = data.getAll("a");
|
||||
data.delete("b");
|
||||
return JSON.stringify({ beforeSet, afterSet, all, hasB: data.has("b") });
|
||||
})()
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const want = `{"beforeSet":[["a","one"],["a","2"],["b","three"]],"afterSet":[["a","four"],["b","three"]],"all":["four"],"hasB":false}`
|
||||
if value != want {
|
||||
t.Fatalf("unexpected FormData result: got %#v want %s", value, want)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,898 @@
|
||||
(function () {
|
||||
const fetchOpen = globalThis.__gopeed_fetch_open;
|
||||
const fetchRead = globalThis.__gopeed_fetch_read;
|
||||
const fetchClose = globalThis.__gopeed_fetch_close;
|
||||
const fetchAbort = globalThis.__gopeed_fetch_abort;
|
||||
|
||||
class FormData {
|
||||
constructor(form) {
|
||||
if (form !== undefined) throw new TypeError("FormData HTML form construction is not available in an extension worker");
|
||||
this._entries = [];
|
||||
}
|
||||
|
||||
_normalizeValue(value) {
|
||||
if (typeof Blob !== "undefined" && value instanceof Blob) return value;
|
||||
if (value && typeof value === "object" && "name" in value && "size" in value) return value;
|
||||
return String(value);
|
||||
}
|
||||
|
||||
append(name, value) {
|
||||
this._entries.push([String(name), this._normalizeValue(value)]);
|
||||
}
|
||||
|
||||
delete(name) {
|
||||
name = String(name);
|
||||
this._entries = this._entries.filter((entry) => entry[0] !== name);
|
||||
}
|
||||
|
||||
get(name) {
|
||||
name = String(name);
|
||||
const entry = this._entries.find((item) => item[0] === name);
|
||||
return entry ? entry[1] : null;
|
||||
}
|
||||
|
||||
getAll(name) {
|
||||
name = String(name);
|
||||
return this._entries.filter((entry) => entry[0] === name).map((entry) => entry[1]);
|
||||
}
|
||||
|
||||
has(name) {
|
||||
name = String(name);
|
||||
return this._entries.some((entry) => entry[0] === name);
|
||||
}
|
||||
|
||||
set(name, value) {
|
||||
name = String(name);
|
||||
value = this._normalizeValue(value);
|
||||
const first = this._entries.findIndex((entry) => entry[0] === name);
|
||||
if (first < 0) {
|
||||
this._entries.push([name, value]);
|
||||
return;
|
||||
}
|
||||
this._entries[first] = [name, value];
|
||||
this._entries = this._entries.filter((entry, index) => index === first || entry[0] !== name);
|
||||
}
|
||||
|
||||
_iterator(kind) {
|
||||
const data = this;
|
||||
let index = 0;
|
||||
return {
|
||||
next() {
|
||||
if (index >= data._entries.length) return { value: undefined, done: true };
|
||||
const entry = data._entries[index++];
|
||||
if (kind === "key") return { value: entry[0], done: false };
|
||||
if (kind === "value") return { value: entry[1], done: false };
|
||||
return { value: [entry[0], entry[1]], done: false };
|
||||
},
|
||||
[Symbol.iterator]() { return this; },
|
||||
};
|
||||
}
|
||||
|
||||
entries() { return this._iterator("entry"); }
|
||||
keys() { return this._iterator("key"); }
|
||||
values() { return this._iterator("value"); }
|
||||
[Symbol.iterator]() { return this.entries(); }
|
||||
|
||||
forEach(callback, thisArg) {
|
||||
for (const [name, value] of this) callback.call(thisArg, value, name, this);
|
||||
}
|
||||
}
|
||||
Object.defineProperty(FormData.prototype, Symbol.toStringTag, { configurable: true, value: "FormData" });
|
||||
globalThis.FormData = FormData;
|
||||
|
||||
const headerNamePattern = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
||||
const headersIteratorBase = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
|
||||
const headersIteratorPrototype = Object.create(headersIteratorBase);
|
||||
|
||||
function normalizeHeaderName(name) {
|
||||
if (typeof name === "symbol") throw new TypeError("HTTP header names must be ByteStrings");
|
||||
name = String(name);
|
||||
for (let index = 0; index < name.length; index++) {
|
||||
if (name.charCodeAt(index) > 0xFF) throw new TypeError("HTTP header names must be ByteStrings");
|
||||
}
|
||||
if (!headerNamePattern.test(name)) throw new TypeError("Invalid HTTP header name");
|
||||
return name.toLowerCase();
|
||||
}
|
||||
|
||||
function normalizeHeaderValue(value) {
|
||||
value = String(value);
|
||||
for (let index = 0; index < value.length; index++) {
|
||||
if (value.charCodeAt(index) > 0xFF) throw new TypeError("HTTP header values must be ByteStrings");
|
||||
}
|
||||
value = value.replace(/^[\t\n\r ]+|[\t\n\r ]+$/g, "");
|
||||
if (/[\0\n\r]/.test(value)) throw new TypeError("Invalid HTTP header value");
|
||||
return value;
|
||||
}
|
||||
|
||||
function normalizeStatusText(value) {
|
||||
value = String(value);
|
||||
for (let index = 0; index < value.length; index++) {
|
||||
const code = value.charCodeAt(index);
|
||||
if (code > 0xFF || (code !== 0x09 && (code < 0x20 || code === 0x7F))) {
|
||||
throw new TypeError("Invalid response status text");
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function sortedHeaderEntries(headers) {
|
||||
const result = [];
|
||||
for (const name of Array.from(headers._values.keys()).sort()) {
|
||||
const values = headers._values.get(name);
|
||||
if (name === "set-cookie") {
|
||||
for (const value of values) result.push([name, value]);
|
||||
} else {
|
||||
result.push([name, values.join(", ")]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Object.defineProperty(headersIteratorPrototype, "next", {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
writable: true,
|
||||
value: function () {
|
||||
const entries = sortedHeaderEntries(this._headers);
|
||||
if (this._index >= entries.length) return { value: undefined, done: true };
|
||||
const entry = entries[this._index++];
|
||||
if (this._kind === "key") return { value: entry[0], done: false };
|
||||
if (this._kind === "value") return { value: entry[1], done: false };
|
||||
return { value: entry, done: false };
|
||||
},
|
||||
});
|
||||
Object.defineProperty(headersIteratorPrototype, Symbol.iterator, {
|
||||
configurable: true,
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
value: function () { return this; },
|
||||
});
|
||||
|
||||
function createHeadersIterator(headers, kind) {
|
||||
const iterator = Object.create(headersIteratorPrototype);
|
||||
Object.defineProperties(iterator, {
|
||||
_headers: { value: headers },
|
||||
_kind: { value: kind },
|
||||
_index: { value: 0, writable: true },
|
||||
});
|
||||
return iterator;
|
||||
}
|
||||
|
||||
function forbiddenRequestHeader(name, value) {
|
||||
if (name.startsWith("proxy-") || name.startsWith("sec-")) return true;
|
||||
if ([
|
||||
"accept-charset", "accept-encoding", "access-control-request-headers",
|
||||
"access-control-request-method", "connection", "content-length", "cookie",
|
||||
"cookie2", "date", "dnt", "expect", "host", "keep-alive", "origin",
|
||||
"permissions-policy", "referer", "te", "trailer", "transfer-encoding",
|
||||
"set-cookie", "upgrade", "via",
|
||||
].includes(name)) return true;
|
||||
if (["x-http-method", "x-http-method-override", "x-method-override"].includes(name)) {
|
||||
return String(value).split(",").some((method) => {
|
||||
method = method.trim().toUpperCase();
|
||||
return method === "CONNECT" || method === "TRACE" || method === "TRACK";
|
||||
});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function noCORSSafelistedRequestHeader(name, value) {
|
||||
if (value.length > 128) return false;
|
||||
const hasUnsafeByte = /[\x00-\x08\x0A-\x1F\x7F"():<>?@\[\\\]{}]/.test(value);
|
||||
if (name === "accept") return !hasUnsafeByte;
|
||||
if (name === "accept-language" || name === "content-language") {
|
||||
return /^[0-9A-Za-z *,\-.;=]*$/.test(value);
|
||||
}
|
||||
if (name === "content-type") {
|
||||
if (hasUnsafeByte) return false;
|
||||
const essence = value.split(";", 1)[0].trim().toLowerCase();
|
||||
return essence === "application/x-www-form-urlencoded" ||
|
||||
essence === "multipart/form-data" || essence === "text/plain";
|
||||
}
|
||||
if (name === "range") return /^bytes=[0-9]+-[0-9]*$/.test(value);
|
||||
return false;
|
||||
}
|
||||
|
||||
function blockedByGuard(headers, name, value) {
|
||||
if (headers._guard === "immutable") throw new TypeError("Headers are immutable");
|
||||
if (headers._guard === "request") return forbiddenRequestHeader(name, value);
|
||||
if (headers._guard === "request-no-cors") {
|
||||
return forbiddenRequestHeader(name, value) || !noCORSSafelistedRequestHeader(name, value);
|
||||
}
|
||||
if (headers._guard === "response") return name === "set-cookie" || name === "set-cookie2";
|
||||
return false;
|
||||
}
|
||||
|
||||
class Headers {
|
||||
constructor(init, guard) {
|
||||
this._values = new Map();
|
||||
this._guard = guard || "none";
|
||||
if (init === undefined) return;
|
||||
if (init === null || (typeof init !== "object" && typeof init !== "function")) {
|
||||
throw new TypeError("Headers init must be a sequence or record");
|
||||
}
|
||||
const iterator = init[Symbol.iterator];
|
||||
if (typeof iterator === "function") {
|
||||
for (const pair of init) {
|
||||
if (pair == null || typeof pair[Symbol.iterator] !== "function") {
|
||||
throw new TypeError("Header pair must be iterable");
|
||||
}
|
||||
const values = Array.from(pair);
|
||||
if (values.length !== 2) throw new TypeError("Header pair must contain exactly two items");
|
||||
this.append(values[0], values[1]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (const name of Reflect.ownKeys(init)) {
|
||||
const descriptor = Reflect.getOwnPropertyDescriptor(init, name);
|
||||
if (!descriptor || !descriptor.enumerable) continue;
|
||||
const normalizedName = normalizeHeaderName(name);
|
||||
this.append(normalizedName, init[name]);
|
||||
}
|
||||
}
|
||||
|
||||
append(name, value) {
|
||||
name = normalizeHeaderName(name);
|
||||
value = normalizeHeaderValue(value);
|
||||
const values = this._values.get(name);
|
||||
const combinedValue = values && name !== "set-cookie" ? values.join(", ") + ", " + value : value;
|
||||
if (blockedByGuard(this, name, combinedValue)) return;
|
||||
if (values) values.push(value);
|
||||
else this._values.set(name, [value]);
|
||||
}
|
||||
|
||||
delete(name) {
|
||||
name = normalizeHeaderName(name);
|
||||
if (this._guard === "immutable") throw new TypeError("Headers are immutable");
|
||||
if (this._guard === "request-no-cors") {
|
||||
if (forbiddenRequestHeader(name, "") || !["accept", "accept-language", "content-language", "content-type", "range"].includes(name)) return;
|
||||
this._values.delete(name);
|
||||
return;
|
||||
}
|
||||
if (blockedByGuard(this, name, "")) return;
|
||||
this._values.delete(name);
|
||||
}
|
||||
|
||||
get(name) {
|
||||
const values = this._values.get(normalizeHeaderName(name));
|
||||
return values ? values.join(", ") : null;
|
||||
}
|
||||
|
||||
getSetCookie() {
|
||||
const values = this._values.get("set-cookie");
|
||||
return values ? values.slice() : [];
|
||||
}
|
||||
|
||||
has(name) {
|
||||
return this._values.has(normalizeHeaderName(name));
|
||||
}
|
||||
|
||||
set(name, value) {
|
||||
name = normalizeHeaderName(name);
|
||||
value = normalizeHeaderValue(value);
|
||||
if (blockedByGuard(this, name, value)) return;
|
||||
this._values.set(name, [value]);
|
||||
}
|
||||
|
||||
keys() { return createHeadersIterator(this, "key"); }
|
||||
values() { return createHeadersIterator(this, "value"); }
|
||||
entries() { return createHeadersIterator(this, "entry"); }
|
||||
[Symbol.iterator]() { return this.entries(); }
|
||||
|
||||
forEach(callback, thisArg) {
|
||||
for (const [name, value] of this) callback.call(thisArg, value, name, this);
|
||||
}
|
||||
}
|
||||
|
||||
globalThis.Headers = Headers;
|
||||
|
||||
const forbiddenMethods = new Set(["CONNECT", "TRACE", "TRACK"]);
|
||||
|
||||
function normalizeMethod(method) {
|
||||
method = String(method);
|
||||
if (!headerNamePattern.test(method)) throw new TypeError("Invalid HTTP method");
|
||||
method = method.toUpperCase();
|
||||
if (forbiddenMethods.has(method)) throw new TypeError("Forbidden HTTP method");
|
||||
return method;
|
||||
}
|
||||
|
||||
function resolveURL(value) {
|
||||
const base = globalThis.location && globalThis.location.href ? globalThis.location.href : "http://localhost/";
|
||||
const parsed = new URL(String(value), base);
|
||||
if (parsed.username || parsed.password) throw new TypeError("Request URLs cannot contain credentials");
|
||||
return parsed.href;
|
||||
}
|
||||
|
||||
function normalizeEnum(value, allowed, name) {
|
||||
value = String(value);
|
||||
if (!allowed.includes(value)) throw new TypeError("Invalid Request " + name);
|
||||
return value;
|
||||
}
|
||||
|
||||
function normalizeReferrer(value) {
|
||||
value = String(value);
|
||||
if (value === "") return "no-referrer";
|
||||
if (value === "about:client") return value;
|
||||
return resolveURL(value);
|
||||
}
|
||||
|
||||
function copyArrayBuffer(value) {
|
||||
if (value instanceof ArrayBuffer) return value.slice(0);
|
||||
return value.buffer.slice(value.byteOffset, value.byteOffset + value.byteLength);
|
||||
}
|
||||
|
||||
function initializeBody(owner, body) {
|
||||
owner._bodyInit = body == null ? null : body;
|
||||
owner._bodyText = null;
|
||||
owner._bodyArrayBuffer = null;
|
||||
owner._bodyBlob = null;
|
||||
owner._bodyFormData = null;
|
||||
owner._bodyFormDataBoundary = "";
|
||||
owner._bodyStream = null;
|
||||
owner._bodyUsed = false;
|
||||
if (body == null) return;
|
||||
if (typeof ReadableStream !== "undefined" && body instanceof ReadableStream) {
|
||||
owner._bodyStream = body;
|
||||
body.__gopeedMarkBodyUsed = () => { owner._bodyUsed = true; };
|
||||
} else if (typeof body === "string") {
|
||||
owner._bodyText = body;
|
||||
} else if (typeof Blob !== "undefined" && body instanceof Blob) {
|
||||
owner._bodyBlob = body;
|
||||
} else if (typeof FormData !== "undefined" && body instanceof FormData) {
|
||||
owner._bodyFormData = body;
|
||||
const suffix = crypto.randomUUID ? crypto.randomUUID() : String(Date.now()) + String(Math.random()).slice(2);
|
||||
owner._bodyFormDataBoundary = "----gopeed-" + suffix;
|
||||
} else if (body instanceof ArrayBuffer || (ArrayBuffer.isView && ArrayBuffer.isView(body))) {
|
||||
owner._bodyArrayBuffer = copyArrayBuffer(body);
|
||||
} else if (typeof URLSearchParams !== "undefined" && body instanceof URLSearchParams) {
|
||||
owner._bodyText = body.toString();
|
||||
} else {
|
||||
owner._bodyText = String(body);
|
||||
}
|
||||
}
|
||||
|
||||
function bodyContentType(owner) {
|
||||
if (owner._bodyText != null) {
|
||||
if (typeof URLSearchParams !== "undefined" && owner._bodyInit instanceof URLSearchParams) {
|
||||
return "application/x-www-form-urlencoded;charset=UTF-8";
|
||||
}
|
||||
return "text/plain;charset=UTF-8";
|
||||
}
|
||||
if (owner._bodyBlob && owner._bodyBlob.type) return owner._bodyBlob.type;
|
||||
if (owner._bodyFormData) return "multipart/form-data; boundary=" + owner._bodyFormDataBoundary;
|
||||
return "";
|
||||
}
|
||||
|
||||
function decodeFormComponent(value) {
|
||||
return decodeURIComponent(String(value).replace(/\+/g, " "));
|
||||
}
|
||||
|
||||
function installBodyMethods(prototype) {
|
||||
Object.defineProperties(prototype, {
|
||||
body: {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get() {
|
||||
if (this._bodyInit == null && !this.__gopeedBodyStream) return null;
|
||||
if (this.__gopeedBodyStream) return this.__gopeedBodyStream;
|
||||
if (!this._bodyStream) this._bodyStream = createBodyReadableStream(this);
|
||||
return this._bodyStream;
|
||||
},
|
||||
},
|
||||
bodyUsed: {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get() { return Boolean(this._bodyUsed || this.__gopeedBodyConsumed); },
|
||||
},
|
||||
});
|
||||
prototype.text = async function () {
|
||||
if (this.bodyUsed) throw new TypeError("Already read");
|
||||
if (this.body == null) return "";
|
||||
return readAllFromStream(this.body, true);
|
||||
};
|
||||
prototype.arrayBuffer = async function () {
|
||||
if (this.bodyUsed) throw new TypeError("Already read");
|
||||
if (this.body == null) return new ArrayBuffer(0);
|
||||
const bytes = await readAllFromStream(this.body, false);
|
||||
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
||||
};
|
||||
prototype.bytes = async function () {
|
||||
if (this.bodyUsed) throw new TypeError("Already read");
|
||||
if (this.body == null) return new Uint8Array(0);
|
||||
return readAllFromStream(this.body, false);
|
||||
};
|
||||
prototype.blob = async function () {
|
||||
const data = await this.arrayBuffer();
|
||||
return new Blob([data], { type: this.headers.get("content-type") || "" });
|
||||
};
|
||||
prototype.json = async function () { return JSON.parse(await this.text()); };
|
||||
prototype.formData = async function () {
|
||||
const contentType = this.headers.get("content-type") || "";
|
||||
const essence = contentType.split(";", 1)[0].trim().toLowerCase();
|
||||
if (essence === "multipart/form-data" && this._bodyFormData) {
|
||||
if (this.bodyUsed) throw new TypeError("Already read");
|
||||
await readAllFromStream(this.body, false);
|
||||
const result = new FormData();
|
||||
for (const entry of Array.from(this._bodyFormData.entries())) result.append(entry[0], entry[1]);
|
||||
return result;
|
||||
}
|
||||
if (essence !== "application/x-www-form-urlencoded") {
|
||||
throw new TypeError("Body is not URL-encoded form data");
|
||||
}
|
||||
if (this.body == null) return new FormData();
|
||||
const text = await this.text();
|
||||
const result = new FormData();
|
||||
for (const entry of text.split("&")) {
|
||||
if (!entry) continue;
|
||||
const parts = entry.split("=");
|
||||
const name = parts.shift();
|
||||
result.append(decodeFormComponent(name), decodeFormComponent(parts.join("=")));
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
function transferReadableStream(stream) {
|
||||
let reader = null;
|
||||
return new ReadableStream({
|
||||
async pull(controller) {
|
||||
if (!reader) reader = stream.getReader();
|
||||
const result = await reader.read();
|
||||
if (result.done) {
|
||||
reader.releaseLock();
|
||||
controller.close();
|
||||
} else {
|
||||
controller.enqueue(result.value);
|
||||
}
|
||||
},
|
||||
cancel(reason) {
|
||||
if (!reader) reader = stream.getReader();
|
||||
return reader.cancel(reason);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
class Request {
|
||||
constructor(input, init) {
|
||||
if (arguments.length < 1) throw new TypeError("Request requires an input");
|
||||
init = init || {};
|
||||
if (init.window !== undefined && init.window !== null) throw new TypeError("Request window must be null");
|
||||
const source = input instanceof Request ? input : null;
|
||||
const bodyWasOverridden = Object.prototype.hasOwnProperty.call(init, "body");
|
||||
if (source && source.bodyUsed && !bodyWasOverridden) throw new TypeError("Request body is already used");
|
||||
this._url = source ? source.url : resolveURL(input);
|
||||
this._method = normalizeMethod(init.method !== undefined ? init.method : (source ? source.method : "GET"));
|
||||
this._destination = "";
|
||||
this._referrer = init.referrer !== undefined ? normalizeReferrer(init.referrer) : (source ? source.referrer : "about:client");
|
||||
this._referrerPolicy = normalizeEnum(init.referrerPolicy !== undefined ? init.referrerPolicy : (source ? source.referrerPolicy : ""), ["", "no-referrer", "no-referrer-when-downgrade", "origin", "origin-when-cross-origin", "same-origin", "strict-origin", "strict-origin-when-cross-origin", "unsafe-url"], "referrerPolicy");
|
||||
this._mode = normalizeEnum(init.mode !== undefined ? init.mode : (source ? source.mode : "cors"), ["same-origin", "no-cors", "cors"], "mode");
|
||||
this._credentials = normalizeEnum(init.credentials !== undefined ? init.credentials : (source ? source.credentials : "same-origin"), ["omit", "same-origin", "include"], "credentials");
|
||||
this._cache = normalizeEnum(init.cache !== undefined ? init.cache : (source ? source.cache : "default"), ["default", "no-store", "reload", "no-cache", "force-cache", "only-if-cached"], "cache");
|
||||
this._redirect = normalizeEnum(init.redirect !== undefined ? init.redirect : (source ? source.redirect : "follow"), ["follow", "error", "manual"], "redirect");
|
||||
if (this._mode === "no-cors" && !["GET", "HEAD", "POST"].includes(this._method)) {
|
||||
throw new TypeError("no-cors requests require a CORS-safelisted method");
|
||||
}
|
||||
if (this._cache === "only-if-cached" && this._mode !== "same-origin") {
|
||||
throw new TypeError("only-if-cached requires same-origin mode");
|
||||
}
|
||||
this._integrity = init.integrity !== undefined ? String(init.integrity) : (source ? source.integrity : "");
|
||||
this._keepalive = init.keepalive !== undefined ? Boolean(init.keepalive) : (source ? source.keepalive : false);
|
||||
this._priority = normalizeEnum(init.priority !== undefined ? init.priority : (source ? source._priority : "auto"), ["high", "low", "auto"], "priority");
|
||||
this._signal = init.signal || (source && source.signal) || new AbortController().signal;
|
||||
if (init.duplex !== undefined && String(init.duplex) !== "half") {
|
||||
throw new TypeError("Request duplex must be 'half'");
|
||||
}
|
||||
this._duplex = "half";
|
||||
const headerInit = init.headers !== undefined ? init.headers : (source ? source.headers : undefined);
|
||||
this._headers = new Headers(headerInit, this._mode === "no-cors" ? "request-no-cors" : "request");
|
||||
let body = init.body !== undefined ? init.body : (source ? source._bodyInit : null);
|
||||
if (source && !bodyWasOverridden && source.body) body = transferReadableStream(source.body);
|
||||
const bodyIsStream = typeof ReadableStream !== "undefined" && body instanceof ReadableStream;
|
||||
if (bodyIsStream && (body.locked || body._disturbed)) {
|
||||
throw new TypeError("ReadableStream body is locked or disturbed");
|
||||
}
|
||||
if (bodyIsStream && init.body !== undefined && init.duplex === undefined) {
|
||||
throw new TypeError("ReadableStream request bodies require duplex: 'half'");
|
||||
}
|
||||
if (bodyIsStream && this._keepalive) {
|
||||
throw new TypeError("keepalive requests cannot have a ReadableStream body");
|
||||
}
|
||||
if (source && !bodyWasOverridden && source.body && (source.body.locked || source.body._disturbed)) {
|
||||
throw new TypeError("Request body is locked or disturbed");
|
||||
}
|
||||
if ((this._method === "GET" || this._method === "HEAD") && body != null) {
|
||||
throw new TypeError("Body not allowed for GET or HEAD requests");
|
||||
}
|
||||
initializeBody(this, body);
|
||||
const contentType = bodyContentType(this);
|
||||
if (contentType && !this._headers.has("content-type")) this._headers.set("content-type", contentType);
|
||||
if (source && source.body) source._bodyUsed = true;
|
||||
}
|
||||
|
||||
get method() { return this._method; }
|
||||
get url() { return this._url; }
|
||||
get headers() { return this._headers; }
|
||||
get destination() { return this._destination; }
|
||||
get referrer() { return this._referrer; }
|
||||
get referrerPolicy() { return this._referrerPolicy; }
|
||||
get mode() { return this._mode; }
|
||||
get credentials() { return this._credentials; }
|
||||
get cache() { return this._cache; }
|
||||
get redirect() { return this._redirect; }
|
||||
get integrity() { return this._integrity; }
|
||||
get keepalive() { return this._keepalive; }
|
||||
get signal() { return this._signal; }
|
||||
get isReloadNavigation() { return false; }
|
||||
get isHistoryNavigation() { return false; }
|
||||
get duplex() { return this._duplex; }
|
||||
clone() {
|
||||
if (this.bodyUsed) throw new TypeError("Request body is already used");
|
||||
let body = null;
|
||||
if (this.body) {
|
||||
const branches = this.body.tee();
|
||||
this._bodyInit = branches[0];
|
||||
this._bodyStream = branches[0];
|
||||
if (this.__gopeedBodyStream) this.__gopeedBodyStream = branches[0];
|
||||
branches[0].__gopeedMarkBodyUsed = () => { this._bodyUsed = true; };
|
||||
body = branches[1];
|
||||
}
|
||||
return new Request(this.url, {
|
||||
method: this.method,
|
||||
headers: this.headers,
|
||||
body,
|
||||
mode: this.mode,
|
||||
credentials: this.credentials,
|
||||
cache: this.cache,
|
||||
redirect: this.redirect,
|
||||
referrer: this.referrer,
|
||||
referrerPolicy: this.referrerPolicy,
|
||||
integrity: this.integrity,
|
||||
keepalive: this.keepalive,
|
||||
signal: this.signal,
|
||||
duplex: "half",
|
||||
});
|
||||
}
|
||||
}
|
||||
installBodyMethods(Request.prototype);
|
||||
|
||||
const nullBodyStatuses = new Set([101, 103, 204, 205, 304]);
|
||||
|
||||
class Response {
|
||||
constructor(body, init) {
|
||||
init = init || {};
|
||||
const status = init.status === undefined ? 200 : Number(init.status);
|
||||
if (!Number.isInteger(status) || status < 200 || status > 599) {
|
||||
throw new RangeError("Invalid response status");
|
||||
}
|
||||
if (body != null && nullBodyStatuses.has(status)) {
|
||||
throw new TypeError("Response with null-body status cannot have a body");
|
||||
}
|
||||
const statusText = init.statusText === undefined ? "" : normalizeStatusText(init.statusText);
|
||||
this._type = "default";
|
||||
this._url = init.url ? String(init.url) : "";
|
||||
this._redirected = Boolean(init.redirected);
|
||||
this._status = status;
|
||||
this._statusText = statusText;
|
||||
this._headers = new Headers(init.headers, "response");
|
||||
if (typeof ReadableStream !== "undefined" && body instanceof ReadableStream && (body.locked || body._disturbed)) {
|
||||
throw new TypeError("ReadableStream body is locked or disturbed");
|
||||
}
|
||||
initializeBody(this, body == null ? null : body);
|
||||
const contentType = bodyContentType(this);
|
||||
if (contentType && !this._headers.has("content-type")) this._headers.set("content-type", contentType);
|
||||
}
|
||||
|
||||
get type() { return this._type; }
|
||||
get url() { return this._url; }
|
||||
get redirected() { return this._redirected; }
|
||||
get status() { return this._status; }
|
||||
get ok() { return this._status >= 200 && this._status <= 299; }
|
||||
get statusText() { return this._statusText; }
|
||||
get headers() { return this._headers; }
|
||||
clone() {
|
||||
if (this.bodyUsed) throw new TypeError("Response body is already used");
|
||||
let body = null;
|
||||
if (this.body) {
|
||||
const branches = this.body.tee();
|
||||
this._bodyInit = branches[0];
|
||||
this._bodyStream = branches[0];
|
||||
if (this.__gopeedBodyStream) this.__gopeedBodyStream = branches[0];
|
||||
branches[0].__gopeedMarkBodyUsed = () => { this._bodyUsed = true; };
|
||||
body = branches[1];
|
||||
}
|
||||
return new Response(body, {
|
||||
status: this.status,
|
||||
statusText: this.statusText,
|
||||
headers: this.headers,
|
||||
url: this.url,
|
||||
redirected: this.redirected,
|
||||
});
|
||||
}
|
||||
|
||||
static error() {
|
||||
const response = Object.create(Response.prototype);
|
||||
response._type = "error";
|
||||
response._url = "";
|
||||
response._redirected = false;
|
||||
response._status = 0;
|
||||
response._statusText = "";
|
||||
response._headers = new Headers(undefined, "immutable");
|
||||
initializeBody(response, null);
|
||||
return response;
|
||||
}
|
||||
|
||||
static redirect(url, status) {
|
||||
status = status === undefined ? 302 : Number(status);
|
||||
if (![301, 302, 303, 307, 308].includes(status)) throw new RangeError("Invalid redirect status");
|
||||
return new Response(null, { status, headers: { location: resolveURL(url) } });
|
||||
}
|
||||
|
||||
static json(data, init) {
|
||||
const body = JSON.stringify(data);
|
||||
if (body === undefined) throw new TypeError("Value is not JSON serializable");
|
||||
init = Object.assign({}, init || {});
|
||||
const headers = new Headers(init.headers);
|
||||
if (!headers.has("content-type")) headers.set("content-type", "application/json");
|
||||
init.headers = headers;
|
||||
return new Response(body, init);
|
||||
}
|
||||
}
|
||||
installBodyMethods(Response.prototype);
|
||||
|
||||
globalThis.Request = Request;
|
||||
globalThis.Response = Response;
|
||||
|
||||
function toUint8Array(chunk) {
|
||||
if (chunk == null) return new Uint8Array(0);
|
||||
if (chunk instanceof Uint8Array) return chunk;
|
||||
if (typeof chunk === "string") return new TextEncoder().encode(chunk);
|
||||
if (chunk instanceof ArrayBuffer) return new Uint8Array(chunk);
|
||||
if (typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(chunk)) {
|
||||
return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
||||
}
|
||||
if (typeof Blob !== "undefined" && chunk instanceof Blob && chunk._buffer instanceof Uint8Array) {
|
||||
return chunk._buffer;
|
||||
}
|
||||
return new Uint8Array(0);
|
||||
}
|
||||
|
||||
function createBodyReadableStream(owner) {
|
||||
const stream = new ReadableStream({
|
||||
start(controller) {
|
||||
Promise.resolve().then(async () => {
|
||||
if (owner._noBody) {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
if (owner._bodyArrayBuffer) {
|
||||
controller.enqueue(toUint8Array(owner._bodyArrayBuffer));
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
if (owner._bodyBlob) {
|
||||
const data = await owner._bodyBlob.arrayBuffer();
|
||||
controller.enqueue(new Uint8Array(data));
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
if (owner._bodyFormData) {
|
||||
const boundary = owner._bodyFormDataBoundary;
|
||||
const entries = Array.from(owner._bodyFormData.entries());
|
||||
if (entries.length === 0) {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
let serialized = "";
|
||||
for (const entry of entries) {
|
||||
const name = String(entry[0]).replace(/\r|\n|"/g, (character) =>
|
||||
character === "\r" ? "%0D" : character === "\n" ? "%0A" : "%22");
|
||||
serialized += "--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + name + "\"\r\n\r\n";
|
||||
serialized += String(entry[1]) + "\r\n";
|
||||
}
|
||||
serialized += "--" + boundary + "--\r\n";
|
||||
controller.enqueue(toUint8Array(serialized));
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
if (owner._bodyText != null) {
|
||||
controller.enqueue(toUint8Array(owner._bodyText));
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
if (owner._bodyInit != null) controller.enqueue(toUint8Array(owner._bodyInit));
|
||||
controller.close();
|
||||
}).catch((error) => controller.error(error));
|
||||
},
|
||||
});
|
||||
stream.__gopeedMarkBodyUsed = () => { owner._bodyUsed = true; };
|
||||
return stream;
|
||||
}
|
||||
|
||||
async function readAllFromStream(stream, asText) {
|
||||
if (!stream) return asText ? "" : new Uint8Array(0);
|
||||
const reader = stream.getReader();
|
||||
const chunks = [];
|
||||
let total = 0;
|
||||
try {
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
if (!(value instanceof Uint8Array)) {
|
||||
throw new TypeError("Fetch body stream chunks must be Uint8Array values");
|
||||
}
|
||||
const chunk = value;
|
||||
chunks.push(chunk);
|
||||
total += chunk.byteLength;
|
||||
}
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
}
|
||||
const merged = new Uint8Array(total);
|
||||
let offset = 0;
|
||||
for (const chunk of chunks) {
|
||||
merged.set(chunk, offset);
|
||||
offset += chunk.byteLength;
|
||||
}
|
||||
return asText ? new TextDecoder().decode(merged) : merged;
|
||||
}
|
||||
|
||||
function attachResponseStreaming(response, stream) {
|
||||
response.__gopeedBodyStream = stream;
|
||||
response.__gopeedBodyConsumed = false;
|
||||
const ensureUnused = () => {
|
||||
if (response.__gopeedBodyConsumed) throw new TypeError("Already read");
|
||||
};
|
||||
const markBodyUsed = () => {
|
||||
ensureUnused();
|
||||
response.__gopeedBodyConsumed = true;
|
||||
response.bodyUsed = true;
|
||||
};
|
||||
if (stream) {
|
||||
stream.__gopeedMarkBodyUsed = () => {
|
||||
if (!response.__gopeedBodyConsumed) {
|
||||
response.__gopeedBodyConsumed = true;
|
||||
response.bodyUsed = true;
|
||||
}
|
||||
};
|
||||
}
|
||||
response.text = async function () {
|
||||
markBodyUsed();
|
||||
return readAllFromStream(this.body, true);
|
||||
};
|
||||
response.arrayBuffer = async function () {
|
||||
markBodyUsed();
|
||||
const bytes = await readAllFromStream(this.body, false);
|
||||
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
||||
};
|
||||
response.bytes = async function () {
|
||||
markBodyUsed();
|
||||
return readAllFromStream(this.body, false);
|
||||
};
|
||||
response.blob = async function () {
|
||||
markBodyUsed();
|
||||
const contentType = this.headers && this.headers.get ? (this.headers.get("content-type") || "") : "";
|
||||
const bytes = await readAllFromStream(this.body, false);
|
||||
return new Blob([bytes], { type: contentType });
|
||||
};
|
||||
response.json = async function () {
|
||||
return JSON.parse(await this.text());
|
||||
};
|
||||
return response;
|
||||
}
|
||||
|
||||
if (typeof globalThis.Response === "function") {
|
||||
const responseProto = globalThis.Response.prototype;
|
||||
const bodyDescriptor = Object.getOwnPropertyDescriptor(responseProto, "body");
|
||||
if (!bodyDescriptor || typeof bodyDescriptor.get !== "function") {
|
||||
Object.defineProperty(responseProto, "body", {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get() {
|
||||
if (!this.__gopeedBodyStream) this.__gopeedBodyStream = createBodyReadableStream(this);
|
||||
return this.__gopeedBodyStream;
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof fetchOpen !== "function") return;
|
||||
|
||||
globalThis.fetch = async function (input, init) {
|
||||
const request = new Request(input, init);
|
||||
const requestID = crypto.randomUUID ? crypto.randomUUID() : String(Date.now()) + "-" + String(Math.random());
|
||||
const signal = request.signal;
|
||||
let abortHandler = null;
|
||||
const cleanupAbort = () => {
|
||||
if (signal && abortHandler && typeof signal.removeEventListener === "function") {
|
||||
signal.removeEventListener("abort", abortHandler);
|
||||
}
|
||||
abortHandler = null;
|
||||
};
|
||||
if (signal) {
|
||||
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
|
||||
abortHandler = () => fetchAbort(requestID, signal.reason == null ? "" : String(signal.reason));
|
||||
signal.addEventListener("abort", abortHandler, { once: true });
|
||||
}
|
||||
|
||||
let body = null;
|
||||
if (request._bodyStream && !request._bodyFormData) {
|
||||
const bytes = await readAllFromStream(request._bodyStream, false);
|
||||
body = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
||||
} else if (request._bodyFormData) {
|
||||
body = request._bodyFormData._entries;
|
||||
} else if (request._bodyArrayBuffer) {
|
||||
body = request._bodyArrayBuffer;
|
||||
} else if (request._bodyBlob) {
|
||||
body = await request._bodyBlob.arrayBuffer();
|
||||
} else if (request._bodyInit != null && typeof request._bodyInit === "object") {
|
||||
body = request._bodyInit;
|
||||
} else if (request._bodyText != null) {
|
||||
body = request._bodyText;
|
||||
} else if (request._bodyInit != null) {
|
||||
body = request._bodyInit;
|
||||
}
|
||||
|
||||
const headers = [];
|
||||
request.headers.forEach((value, key) => headers.push([key, value]));
|
||||
let metadata;
|
||||
try {
|
||||
metadata = await fetchOpen({
|
||||
id: requestID,
|
||||
url: request.url,
|
||||
method: request.method,
|
||||
headers,
|
||||
body,
|
||||
bodyType: request._bodyFormData ? "formdata" : "",
|
||||
bodyBoundary: request._bodyFormDataBoundary,
|
||||
redirect: request.redirect,
|
||||
credentials: request.credentials,
|
||||
});
|
||||
} catch (error) {
|
||||
cleanupAbort();
|
||||
if (signal && signal.aborted) throw new DOMException("Aborted", "AbortError");
|
||||
throw error instanceof Error ? error : new TypeError(String(error));
|
||||
}
|
||||
|
||||
const stream = new ReadableStream({
|
||||
async pull(controller) {
|
||||
let chunk;
|
||||
try {
|
||||
chunk = await fetchRead(metadata.id, 64 * 1024);
|
||||
} catch (error) {
|
||||
fetchClose(metadata.id);
|
||||
cleanupAbort();
|
||||
controller.error(error instanceof Error ? error : new TypeError(String(error)));
|
||||
return;
|
||||
}
|
||||
if (chunk == null) {
|
||||
fetchClose(metadata.id);
|
||||
cleanupAbort();
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
const bytes = chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
|
||||
if (bytes.byteLength === 0) {
|
||||
fetchClose(metadata.id);
|
||||
cleanupAbort();
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
controller.enqueue(bytes);
|
||||
},
|
||||
cancel(reason) {
|
||||
cleanupAbort();
|
||||
fetchAbort(metadata.id, reason == null ? "" : String(reason));
|
||||
},
|
||||
});
|
||||
const response = new Response(null, {
|
||||
status: metadata.status,
|
||||
statusText: metadata.statusText,
|
||||
headers: metadata.headers,
|
||||
url: metadata.url,
|
||||
redirected: metadata.redirected,
|
||||
});
|
||||
response._type = "basic";
|
||||
response._headers._guard = "immutable";
|
||||
return attachResponseStreaming(response, stream);
|
||||
};
|
||||
})();
|
||||
@@ -0,0 +1,619 @@
|
||||
package fetch
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/GopeedLab/gopeed/internal/httpclient"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/file"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/util"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/dop251/goja_nodejs/eventloop"
|
||||
"golang.org/x/net/publicsuffix"
|
||||
)
|
||||
|
||||
const FingerprintMagicKey = "__gopeed_fetch_fingerprint"
|
||||
|
||||
//go:embed fetch.js
|
||||
var script string
|
||||
|
||||
type Config struct {
|
||||
ProxyHandler func(*http.Request) (*url.URL, error)
|
||||
RegisterCleanup func(func())
|
||||
}
|
||||
|
||||
func Enable(runtime *goja.Runtime, loop *eventloop.EventLoop, cfg *Config) error {
|
||||
if cfg == nil {
|
||||
cfg = &Config{}
|
||||
}
|
||||
registry := newRegistry()
|
||||
if cfg.RegisterCleanup != nil {
|
||||
cfg.RegisterCleanup(registry.CloseAll)
|
||||
}
|
||||
if err := runtime.Set("__gopeed_setFingerprint", func(fingerprint string) {
|
||||
_ = runtime.Set(FingerprintMagicKey, fingerprint)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.Set("__gopeed_fetch_open", func(call goja.FunctionCall) goja.Value {
|
||||
request, err := exportRequest(runtime, call.Argument(0))
|
||||
if err != nil {
|
||||
panic(runtime.NewGoError(err))
|
||||
}
|
||||
fingerprint := util.SafeGet[string](runtime, FingerprintMagicKey)
|
||||
promise, resolve, reject := runtime.NewPromise()
|
||||
go func() {
|
||||
metadata, err := registry.Open(fingerprint, cfg.ProxyHandler, request)
|
||||
ok := loop.RunOnLoop(func(runtime *goja.Runtime) {
|
||||
if err != nil {
|
||||
reject(runtime.NewGoError(err))
|
||||
return
|
||||
}
|
||||
resolve(runtime.ToValue(metadata))
|
||||
})
|
||||
if !ok && metadata != nil {
|
||||
registry.Close(metadata.ID)
|
||||
}
|
||||
}()
|
||||
return runtime.ToValue(promise)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.Set("__gopeed_fetch_read", func(call goja.FunctionCall) goja.Value {
|
||||
id := call.Argument(0).String()
|
||||
chunkSize := int(call.Argument(1).ToInteger())
|
||||
promise, resolve, reject := runtime.NewPromise()
|
||||
go func() {
|
||||
chunk, done, err := registry.Read(id, chunkSize)
|
||||
ok := loop.RunOnLoop(func(runtime *goja.Runtime) {
|
||||
if err != nil {
|
||||
reject(runtime.NewGoError(err))
|
||||
return
|
||||
}
|
||||
if done {
|
||||
resolve(goja.Null())
|
||||
return
|
||||
}
|
||||
resolve(runtime.ToValue(runtime.NewArrayBuffer(chunk)))
|
||||
})
|
||||
if !ok {
|
||||
registry.Close(id)
|
||||
}
|
||||
}()
|
||||
return runtime.ToValue(promise)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.Set("__gopeed_fetch_close", func(id string) {
|
||||
registry.Close(id)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.Set("__gopeed_fetch_abort", func(id, reason string) {
|
||||
registry.Abort(id, reason)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := runtime.RunString(script)
|
||||
return err
|
||||
}
|
||||
|
||||
type request struct {
|
||||
ID string
|
||||
URL string
|
||||
Method string
|
||||
Headers [][2]string
|
||||
Body any
|
||||
Redirect string
|
||||
Credentials string
|
||||
}
|
||||
|
||||
type responseMetadata struct {
|
||||
ID string `json:"id"`
|
||||
Status int `json:"status"`
|
||||
StatusText string `json:"statusText"`
|
||||
URL string `json:"url"`
|
||||
Redirected bool `json:"redirected"`
|
||||
Headers [][2]string `json:"headers"`
|
||||
}
|
||||
|
||||
type formDataEntry struct {
|
||||
name string
|
||||
value any
|
||||
}
|
||||
|
||||
type formDataSnapshot struct {
|
||||
entries []formDataEntry
|
||||
boundary string
|
||||
}
|
||||
|
||||
func exportRequest(runtime *goja.Runtime, value goja.Value) (*request, error) {
|
||||
obj := value.ToObject(runtime)
|
||||
if obj == nil {
|
||||
return nil, fmt.Errorf("invalid fetch request")
|
||||
}
|
||||
result := &request{
|
||||
ID: obj.Get("id").String(),
|
||||
URL: obj.Get("url").String(),
|
||||
Method: obj.Get("method").String(),
|
||||
Redirect: obj.Get("redirect").String(),
|
||||
Credentials: obj.Get("credentials").String(),
|
||||
}
|
||||
if result.Method == "" {
|
||||
result.Method = http.MethodGet
|
||||
}
|
||||
if headersValue := obj.Get("headers"); headersValue != nil && !goja.IsUndefined(headersValue) && !goja.IsNull(headersValue) {
|
||||
if exported, ok := headersValue.Export().([]any); ok {
|
||||
for _, item := range exported {
|
||||
pair, ok := item.([]any)
|
||||
if !ok || len(pair) != 2 {
|
||||
continue
|
||||
}
|
||||
result.Headers = append(result.Headers, [2]string{fmt.Sprint(pair[0]), fmt.Sprint(pair[1])})
|
||||
}
|
||||
}
|
||||
}
|
||||
bodyValue := obj.Get("body")
|
||||
if bodyValue != nil && !goja.IsUndefined(bodyValue) && !goja.IsNull(bodyValue) {
|
||||
var body any
|
||||
var err error
|
||||
if obj.Get("bodyType").String() == "formdata" {
|
||||
body, err = snapshotFormData(bodyValue.Export())
|
||||
} else {
|
||||
body, err = snapshotBody(bodyValue.Export())
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if snapshot, ok := body.(*formDataSnapshot); ok {
|
||||
snapshot.boundary = obj.Get("bodyBoundary").String()
|
||||
}
|
||||
result.Body = body
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// snapshotBody runs on the JavaScript event loop. Go request goroutines must
|
||||
// not retain mutable ArrayBuffer or FormData storage owned by goja.
|
||||
func snapshotBody(body any) (any, error) {
|
||||
switch value := body.(type) {
|
||||
case nil, string:
|
||||
return value, nil
|
||||
case []byte:
|
||||
return append([]byte(nil), value...), nil
|
||||
case goja.ArrayBuffer:
|
||||
return append([]byte(nil), value.Bytes()...), nil
|
||||
case *file.File:
|
||||
return snapshotFile(value), nil
|
||||
default:
|
||||
if bytesValue, ok := value.(interface{ Bytes() []byte }); ok {
|
||||
return append([]byte(nil), bytesValue.Bytes()...), nil
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
}
|
||||
|
||||
func snapshotFormData(body any) (*formDataSnapshot, error) {
|
||||
entries, ok := body.([]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid FormData entry list %T", body)
|
||||
}
|
||||
snapshot := &formDataSnapshot{entries: make([]formDataEntry, 0, len(entries))}
|
||||
for _, item := range entries {
|
||||
pair, ok := item.([]any)
|
||||
if !ok || len(pair) != 2 {
|
||||
return nil, fmt.Errorf("invalid FormData entry %T", item)
|
||||
}
|
||||
name, ok := pair[0].(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid FormData field name %T", pair[0])
|
||||
}
|
||||
value := pair[1]
|
||||
if formFile, ok := value.(*file.File); ok {
|
||||
value = snapshotFile(formFile)
|
||||
}
|
||||
snapshot.entries = append(snapshot.entries, formDataEntry{name: name, value: value})
|
||||
}
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
func snapshotFile(source *file.File) *file.File {
|
||||
if source == nil {
|
||||
return nil
|
||||
}
|
||||
return &file.File{
|
||||
Reader: source.Reader,
|
||||
Closer: source.Closer,
|
||||
Name: source.Name,
|
||||
Size: source.Size,
|
||||
}
|
||||
}
|
||||
|
||||
type registry struct {
|
||||
mu sync.Mutex
|
||||
operations map[string]*operation
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
jar http.CookieJar
|
||||
closed bool
|
||||
}
|
||||
|
||||
type operation struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
readMu sync.Mutex
|
||||
stateMu sync.Mutex
|
||||
body io.ReadCloser
|
||||
pending error
|
||||
closed bool
|
||||
closeOnce sync.Once
|
||||
}
|
||||
|
||||
func newRegistry() *registry {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
||||
return ®istry{
|
||||
operations: make(map[string]*operation),
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
jar: jar,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *registry) Open(fingerprint string, proxyHandler func(*http.Request) (*url.URL, error), metadata *request) (*responseMetadata, error) {
|
||||
ctx, cancel := context.WithCancel(r.ctx)
|
||||
op := &operation{ctx: ctx, cancel: cancel}
|
||||
if err := r.add(metadata.ID, op); err != nil {
|
||||
cancel()
|
||||
return nil, err
|
||||
}
|
||||
keepOperation := false
|
||||
defer func() {
|
||||
if !keepOperation {
|
||||
r.Close(metadata.ID)
|
||||
}
|
||||
}()
|
||||
|
||||
contentType, body, contentLength, err := buildBody(metadata.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if metadata.Method == http.MethodGet || metadata.Method == http.MethodHead {
|
||||
body = nil
|
||||
contentLength = 0
|
||||
}
|
||||
httpRequest, err := http.NewRequestWithContext(ctx, metadata.Method, metadata.URL, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, header := range metadata.Headers {
|
||||
httpRequest.Header.Add(header[0], header[1])
|
||||
}
|
||||
if host := httpRequest.Header.Get("Host"); host != "" {
|
||||
httpRequest.Host = host
|
||||
}
|
||||
if body != nil {
|
||||
if contentType != "" && !hasHeader(metadata.Headers, "Content-Type") {
|
||||
httpRequest.Header.Set("Content-Type", contentType)
|
||||
}
|
||||
httpRequest.ContentLength = contentLength
|
||||
}
|
||||
|
||||
var jar http.CookieJar
|
||||
if metadata.Credentials != "omit" {
|
||||
jar = r.jar
|
||||
}
|
||||
redirected := false
|
||||
client, err := httpclient.NewClient(httpclient.Options{
|
||||
Client: httpclient.ClientOptions{
|
||||
Jar: jar,
|
||||
CheckRedirect: func(_ *http.Request, via []*http.Request) error {
|
||||
switch metadata.Redirect {
|
||||
case "manual":
|
||||
return http.ErrUseLastResponse
|
||||
case "error":
|
||||
return fmt.Errorf("redirect failed")
|
||||
default:
|
||||
redirected = len(via) > 0
|
||||
if len(via) > 20 {
|
||||
return fmt.Errorf("too many redirects")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
},
|
||||
},
|
||||
Transport: httpclient.TransportOptions{Proxy: proxyHandler},
|
||||
Impersonation: impersonationOptions(fingerprint),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
response, err := client.Do(httpRequest)
|
||||
if err != nil {
|
||||
var networkError net.Error
|
||||
if errors.As(err, &networkError) && networkError.Timeout() {
|
||||
return nil, fmt.Errorf("Network request timed out")
|
||||
}
|
||||
return nil, fmt.Errorf("Network request failed: %w", err)
|
||||
}
|
||||
if !op.setBody(response.Body) {
|
||||
return nil, context.Canceled
|
||||
}
|
||||
keepOperation = true
|
||||
result := &responseMetadata{
|
||||
ID: metadata.ID,
|
||||
Status: response.StatusCode,
|
||||
StatusText: http.StatusText(response.StatusCode),
|
||||
URL: metadata.URL,
|
||||
}
|
||||
if response.Request != nil && response.Request.URL != nil {
|
||||
responseURL := *response.Request.URL
|
||||
responseURL.Fragment = ""
|
||||
result.URL = responseURL.String()
|
||||
}
|
||||
result.Redirected = redirected
|
||||
for key, values := range response.Header {
|
||||
for _, value := range values {
|
||||
result.Headers = append(result.Headers, [2]string{key, value})
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *registry) add(id string, op *operation) error {
|
||||
if id == "" {
|
||||
return fmt.Errorf("fetch request ID is required")
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.closed {
|
||||
return context.Canceled
|
||||
}
|
||||
if _, exists := r.operations[id]; exists {
|
||||
return fmt.Errorf("duplicate fetch request ID %q", id)
|
||||
}
|
||||
r.operations[id] = op
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *registry) Read(id string, chunkSize int) ([]byte, bool, error) {
|
||||
op := r.get(id)
|
||||
if op == nil {
|
||||
return nil, true, nil
|
||||
}
|
||||
chunk, done, err := op.read(chunkSize)
|
||||
if done || err != nil {
|
||||
r.Close(id)
|
||||
}
|
||||
return chunk, done, err
|
||||
}
|
||||
|
||||
func (r *registry) Close(id string) {
|
||||
r.mu.Lock()
|
||||
op := r.operations[id]
|
||||
delete(r.operations, id)
|
||||
r.mu.Unlock()
|
||||
if op != nil {
|
||||
op.close()
|
||||
}
|
||||
}
|
||||
|
||||
func (r *registry) Abort(id, _ string) {
|
||||
r.Close(id)
|
||||
}
|
||||
|
||||
func (r *registry) CloseAll() {
|
||||
r.mu.Lock()
|
||||
if r.closed {
|
||||
r.mu.Unlock()
|
||||
return
|
||||
}
|
||||
r.closed = true
|
||||
operations := r.operations
|
||||
r.operations = make(map[string]*operation)
|
||||
r.mu.Unlock()
|
||||
r.cancel()
|
||||
for _, op := range operations {
|
||||
op.close()
|
||||
}
|
||||
}
|
||||
|
||||
func (r *registry) get(id string) *operation {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.operations[id]
|
||||
}
|
||||
|
||||
func (o *operation) setBody(body io.ReadCloser) bool {
|
||||
o.stateMu.Lock()
|
||||
defer o.stateMu.Unlock()
|
||||
if o.closed {
|
||||
_ = body.Close()
|
||||
return false
|
||||
}
|
||||
o.body = body
|
||||
return true
|
||||
}
|
||||
|
||||
func (o *operation) read(chunkSize int) ([]byte, bool, error) {
|
||||
o.readMu.Lock()
|
||||
defer o.readMu.Unlock()
|
||||
o.stateMu.Lock()
|
||||
body := o.body
|
||||
pending := o.pending
|
||||
o.pending = nil
|
||||
closed := o.closed
|
||||
o.stateMu.Unlock()
|
||||
if pending != nil {
|
||||
if errors.Is(pending, io.EOF) {
|
||||
return nil, true, nil
|
||||
}
|
||||
return nil, false, pending
|
||||
}
|
||||
if closed || body == nil {
|
||||
return nil, true, nil
|
||||
}
|
||||
if chunkSize <= 0 {
|
||||
chunkSize = 64 * 1024
|
||||
}
|
||||
buffer := make([]byte, chunkSize)
|
||||
n, err := body.Read(buffer)
|
||||
if n > 0 {
|
||||
if err != nil {
|
||||
o.stateMu.Lock()
|
||||
o.pending = err
|
||||
o.stateMu.Unlock()
|
||||
}
|
||||
return buffer[:n], false, nil
|
||||
}
|
||||
if errors.Is(err, io.EOF) {
|
||||
return nil, true, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
return []byte{}, false, nil
|
||||
}
|
||||
|
||||
func (o *operation) close() {
|
||||
o.closeOnce.Do(func() {
|
||||
o.cancel()
|
||||
o.stateMu.Lock()
|
||||
o.closed = true
|
||||
body := o.body
|
||||
o.stateMu.Unlock()
|
||||
if body != nil {
|
||||
_ = body.Close()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func buildBody(body any) (string, io.Reader, int64, error) {
|
||||
switch value := body.(type) {
|
||||
case nil:
|
||||
return "", nil, 0, nil
|
||||
case string:
|
||||
return "text/plain;charset=UTF-8", strings.NewReader(value), int64(len(value)), nil
|
||||
case []byte:
|
||||
return "", bytes.NewReader(value), int64(len(value)), nil
|
||||
case goja.ArrayBuffer:
|
||||
data := value.Bytes()
|
||||
return "", bytes.NewReader(data), int64(len(data)), nil
|
||||
case *file.File:
|
||||
return "application/octet-stream", value.Reader, value.Size, nil
|
||||
case *formDataSnapshot:
|
||||
return buildMultipartBody(value)
|
||||
default:
|
||||
if bytesValue, ok := value.(interface{ Bytes() []byte }); ok {
|
||||
data := bytesValue.Bytes()
|
||||
return "", bytes.NewReader(data), int64(len(data)), nil
|
||||
}
|
||||
data := fmt.Sprint(value)
|
||||
return "", strings.NewReader(data), int64(len(data)), nil
|
||||
}
|
||||
}
|
||||
|
||||
type countingWriter struct {
|
||||
n int64
|
||||
}
|
||||
|
||||
func (w *countingWriter) Write(data []byte) (int, error) {
|
||||
w.n += int64(len(data))
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
func buildMultipartBody(snapshot *formDataSnapshot) (string, io.Reader, int64, error) {
|
||||
boundary := snapshot.boundary
|
||||
if boundary == "" {
|
||||
boundaryWriter := multipart.NewWriter(io.Discard)
|
||||
boundary = boundaryWriter.Boundary()
|
||||
_ = boundaryWriter.Close()
|
||||
}
|
||||
counter := &countingWriter{}
|
||||
statWriter := multipart.NewWriter(counter)
|
||||
if err := statWriter.SetBoundary(boundary); err != nil {
|
||||
return "", nil, 0, err
|
||||
}
|
||||
for _, entry := range snapshot.entries {
|
||||
switch value := entry.value.(type) {
|
||||
case string:
|
||||
if err := statWriter.WriteField(entry.name, value); err != nil {
|
||||
return "", nil, 0, err
|
||||
}
|
||||
case *file.File:
|
||||
if _, err := statWriter.CreateFormFile(entry.name, value.Name); err != nil {
|
||||
return "", nil, 0, err
|
||||
}
|
||||
counter.n += value.Size
|
||||
}
|
||||
}
|
||||
if err := statWriter.Close(); err != nil {
|
||||
return "", nil, 0, err
|
||||
}
|
||||
|
||||
reader, writer := io.Pipe()
|
||||
multipartWriter := multipart.NewWriter(writer)
|
||||
if err := multipartWriter.SetBoundary(boundary); err != nil {
|
||||
_ = reader.Close()
|
||||
_ = writer.CloseWithError(err)
|
||||
return "", nil, 0, err
|
||||
}
|
||||
go func() {
|
||||
for _, entry := range snapshot.entries {
|
||||
var err error
|
||||
switch value := entry.value.(type) {
|
||||
case string:
|
||||
err = multipartWriter.WriteField(entry.name, value)
|
||||
case *file.File:
|
||||
var part io.Writer
|
||||
part, err = multipartWriter.CreateFormFile(entry.name, value.Name)
|
||||
if err == nil {
|
||||
_, err = io.Copy(part, value)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
_ = writer.CloseWithError(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := multipartWriter.Close(); err != nil {
|
||||
_ = writer.CloseWithError(err)
|
||||
return
|
||||
}
|
||||
_ = writer.Close()
|
||||
}()
|
||||
return multipartWriter.FormDataContentType(), reader, counter.n, nil
|
||||
}
|
||||
|
||||
func hasHeader(headers [][2]string, key string) bool {
|
||||
for _, header := range headers {
|
||||
if strings.EqualFold(header[0], key) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func impersonationOptions(fingerprint string) httpclient.ImpersonationOptions {
|
||||
switch fingerprint {
|
||||
case "chrome":
|
||||
return httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationFixed, Browser: httpclient.BrowserChrome}
|
||||
case "firefox":
|
||||
return httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationFixed, Browser: httpclient.BrowserFirefox}
|
||||
case "safari":
|
||||
return httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationFixed, Browser: httpclient.BrowserSafari}
|
||||
default:
|
||||
return httpclient.ImpersonationOptions{Mode: httpclient.ImpersonationNative}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package formdata
|
||||
|
||||
import "github.com/dop251/goja"
|
||||
|
||||
type FormData struct {
|
||||
data map[string]any
|
||||
}
|
||||
|
||||
func (fd *FormData) Append(name string, value any) {
|
||||
fd.data[name] = value
|
||||
}
|
||||
|
||||
func (fd *FormData) Delete(name string) {
|
||||
delete(fd.data, name)
|
||||
}
|
||||
|
||||
func (fd *FormData) Entries() []any {
|
||||
var entries []any
|
||||
for k, v := range fd.data {
|
||||
entries = append(entries, []any{k, v})
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
func (fd *FormData) Get(name string) any {
|
||||
return fd.data[name]
|
||||
}
|
||||
|
||||
func (fd *FormData) GetAll(name string) []any {
|
||||
return []any{fd.data[name]}
|
||||
}
|
||||
|
||||
func (fd *FormData) Has(name string) bool {
|
||||
_, ok := fd.data[name]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (fd *FormData) Keys() []string {
|
||||
var keys []string
|
||||
for k := range fd.data {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
func (fd *FormData) Set(name string, value any) {
|
||||
fd.data[name] = value
|
||||
}
|
||||
|
||||
func (fd *FormData) Values() []any {
|
||||
var values []any
|
||||
for _, v := range fd.data {
|
||||
values = append(values, v)
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func Enable(runtime *goja.Runtime) error {
|
||||
file := runtime.ToValue(func(call goja.ConstructorCall) *goja.Object {
|
||||
instance := &FormData{
|
||||
data: make(map[string]any),
|
||||
}
|
||||
instanceValue := runtime.ToValue(instance).(*goja.Object)
|
||||
instanceValue.SetPrototype(call.This.Prototype())
|
||||
return instanceValue
|
||||
})
|
||||
return runtime.Set("FormData", file)
|
||||
}
|
||||
@@ -0,0 +1,477 @@
|
||||
package stream
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/dop251/goja_nodejs/eventloop"
|
||||
)
|
||||
|
||||
//go:embed stream.js
|
||||
var script string
|
||||
|
||||
type Config struct {
|
||||
CreateObjectURL func(opts *ObjectURLOptions, open ObjectURLOpener) (string, error)
|
||||
RevokeObjectURL func(url string) error
|
||||
}
|
||||
|
||||
type ObjectURLOptions struct {
|
||||
ContentType string
|
||||
Size int64
|
||||
Range bool
|
||||
}
|
||||
|
||||
type ObjectURLOpenRequest struct {
|
||||
Offset int64
|
||||
End int64
|
||||
}
|
||||
|
||||
type ObjectURLOpener func(ctx context.Context, req ObjectURLOpenRequest) (io.ReadCloser, error)
|
||||
|
||||
func Enable(runtime *goja.Runtime, loop *eventloop.EventLoop, cfg *Config) error {
|
||||
if cfg == nil {
|
||||
cfg = &Config{}
|
||||
}
|
||||
blobPipes := newBlobPipeRegistry()
|
||||
if err := runtime.Set("__gopeed_create_blob_object_url", func(call goja.FunctionCall) goja.Value {
|
||||
if cfg.CreateObjectURL == nil {
|
||||
panic(runtime.NewGoError(fmt.Errorf("blob object url handler not configured")))
|
||||
}
|
||||
openValue := call.Argument(0)
|
||||
if _, ok := goja.AssertFunction(openValue); !ok {
|
||||
panic(runtime.NewGoError(fmt.Errorf("blob opener must be callable")))
|
||||
}
|
||||
optsObj := call.Argument(1).ToObject(runtime)
|
||||
size := int64(0)
|
||||
contentType := ""
|
||||
rangeEnabled := false
|
||||
if optsObj != nil {
|
||||
size = optsObj.Get("size").ToInteger()
|
||||
contentType = optsObj.Get("contentType").String()
|
||||
rangeEnabled = optsObj.Get("range").ToBoolean()
|
||||
}
|
||||
if size < 0 {
|
||||
size = 0
|
||||
}
|
||||
opts := &ObjectURLOptions{
|
||||
ContentType: contentType,
|
||||
Size: size,
|
||||
Range: rangeEnabled,
|
||||
}
|
||||
url, err := cfg.CreateObjectURL(opts, func(ctx context.Context, req ObjectURLOpenRequest) (io.ReadCloser, error) {
|
||||
return openBlobObjectURLReader(ctx, loop, blobPipes, openValue, req)
|
||||
})
|
||||
if err != nil {
|
||||
panic(runtime.NewGoError(err))
|
||||
}
|
||||
return runtime.ToValue(url)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.Set("__gopeed_revoke_object_url", func(url string) {
|
||||
if cfg.RevokeObjectURL == nil {
|
||||
panic(runtime.NewGoError(fmt.Errorf("blob revoke handler not configured")))
|
||||
}
|
||||
if err := cfg.RevokeObjectURL(url); err != nil {
|
||||
panic(runtime.NewGoError(err))
|
||||
}
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.Set("__gopeed_blob_pipe_chunk", func(call goja.FunctionCall) goja.Value {
|
||||
id := call.Argument(0).String()
|
||||
ok := blobPipes.Push(id, exportBytes(call.Argument(1)))
|
||||
return runtime.ToValue(ok)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.Set("__gopeed_blob_pipe_close", func(id string) {
|
||||
blobPipes.Close(id, nil)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.Set("__gopeed_blob_pipe_error", func(id string, message string) {
|
||||
blobPipes.Close(id, errors.New(message))
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := runtime.RunString(script)
|
||||
return err
|
||||
}
|
||||
|
||||
func openBlobObjectURLReader(ctx context.Context, loop *eventloop.EventLoop, pipes *blobPipeRegistry, openValue goja.Value, req ObjectURLOpenRequest) (io.ReadCloser, error) {
|
||||
if pipes == nil {
|
||||
return nil, fmt.Errorf("blob pipe registry not configured")
|
||||
}
|
||||
pipe := newBlobPipe(ctx, loop, pipes)
|
||||
pipes.Add(pipe)
|
||||
_, err := runOnLoop(loop, func(runtime *goja.Runtime) (goja.Value, error) {
|
||||
fnVal := runtime.Get("__gopeed_blob_pipe_source")
|
||||
fn, ok := goja.AssertFunction(fnVal)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("blob pipe helper is not callable")
|
||||
}
|
||||
request := map[string]any{
|
||||
"offset": req.Offset,
|
||||
"end": req.End,
|
||||
}
|
||||
return fn(nil, openValue, runtime.ToValue(request), runtime.ToValue(pipe.id))
|
||||
})
|
||||
if err != nil {
|
||||
pipe.Close()
|
||||
return nil, err
|
||||
}
|
||||
return pipe, nil
|
||||
}
|
||||
|
||||
type blobPipeRegistry struct {
|
||||
mu sync.Mutex
|
||||
pipes map[string]*blobPipe
|
||||
next atomic.Uint64
|
||||
}
|
||||
|
||||
func newBlobPipeRegistry() *blobPipeRegistry {
|
||||
return &blobPipeRegistry{
|
||||
pipes: map[string]*blobPipe{},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *blobPipeRegistry) Add(pipe *blobPipe) {
|
||||
r.mu.Lock()
|
||||
r.pipes[pipe.id] = pipe
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
func (r *blobPipeRegistry) Remove(id string) {
|
||||
r.mu.Lock()
|
||||
delete(r.pipes, id)
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
func (r *blobPipeRegistry) Push(id string, chunk []byte) bool {
|
||||
r.mu.Lock()
|
||||
pipe := r.pipes[id]
|
||||
r.mu.Unlock()
|
||||
if pipe == nil {
|
||||
return false
|
||||
}
|
||||
return pipe.Push(chunk)
|
||||
}
|
||||
|
||||
func (r *blobPipeRegistry) Close(id string, err error) {
|
||||
r.mu.Lock()
|
||||
pipe := r.pipes[id]
|
||||
r.mu.Unlock()
|
||||
if pipe != nil {
|
||||
pipe.CloseWithError(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *blobPipeRegistry) NewID() string {
|
||||
return fmt.Sprintf("pipe-%d", r.next.Add(1))
|
||||
}
|
||||
|
||||
type blobPipeItem struct {
|
||||
chunk []byte
|
||||
err error
|
||||
done bool
|
||||
}
|
||||
|
||||
type blobPipe struct {
|
||||
id string
|
||||
loop *eventloop.EventLoop
|
||||
registry *blobPipeRegistry
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
ch chan blobPipeItem
|
||||
|
||||
mu sync.Mutex
|
||||
buf []byte
|
||||
closed bool
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
func newBlobPipe(ctx context.Context, loop *eventloop.EventLoop, registry *blobPipeRegistry) *blobPipe {
|
||||
pipeCtx, cancel := context.WithCancel(ctx)
|
||||
return &blobPipe{
|
||||
id: registry.NewID(),
|
||||
loop: loop,
|
||||
registry: registry,
|
||||
ctx: pipeCtx,
|
||||
cancel: cancel,
|
||||
ch: make(chan blobPipeItem, 8),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *blobPipe) Push(chunk []byte) bool {
|
||||
if len(chunk) == 0 {
|
||||
return true
|
||||
}
|
||||
buf := append([]byte(nil), chunk...)
|
||||
select {
|
||||
case p.ch <- blobPipeItem{chunk: buf}:
|
||||
return true
|
||||
case <-p.ctx.Done():
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (p *blobPipe) CloseWithError(err error) {
|
||||
p.once.Do(func() {
|
||||
p.registry.Remove(p.id)
|
||||
select {
|
||||
case p.ch <- blobPipeItem{done: true, err: err}:
|
||||
case <-p.ctx.Done():
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (p *blobPipe) Read(dst []byte) (int, error) {
|
||||
p.mu.Lock()
|
||||
if len(p.buf) > 0 {
|
||||
n := copy(dst, p.buf)
|
||||
p.buf = p.buf[n:]
|
||||
p.mu.Unlock()
|
||||
return n, nil
|
||||
}
|
||||
p.mu.Unlock()
|
||||
|
||||
select {
|
||||
case item := <-p.ch:
|
||||
if item.done {
|
||||
if item.err != nil {
|
||||
return 0, item.err
|
||||
}
|
||||
return 0, io.EOF
|
||||
}
|
||||
n := copy(dst, item.chunk)
|
||||
if n < len(item.chunk) {
|
||||
p.mu.Lock()
|
||||
p.buf = append(p.buf, item.chunk[n:]...)
|
||||
p.mu.Unlock()
|
||||
}
|
||||
return n, nil
|
||||
case <-p.ctx.Done():
|
||||
return 0, p.ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func (p *blobPipe) Close() error {
|
||||
p.mu.Lock()
|
||||
if p.closed {
|
||||
p.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
p.closed = true
|
||||
p.mu.Unlock()
|
||||
p.registry.Remove(p.id)
|
||||
p.cancel()
|
||||
p.cancelSourceReader()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *blobPipe) cancelSourceReader() {
|
||||
if p.loop == nil {
|
||||
return
|
||||
}
|
||||
// Invoke the JavaScript cancellation before returning from Close. The
|
||||
// source session may be released immediately after the HTTP handler closes
|
||||
// this reader, so merely queueing the callback could let the engine stop
|
||||
// before reader.cancel() is called.
|
||||
_, _ = runOnLoop(p.loop, func(runtime *goja.Runtime) (goja.Value, error) {
|
||||
fnVal := runtime.Get("__gopeed_blob_cancel_pipe_source")
|
||||
fn, ok := goja.AssertFunction(fnVal)
|
||||
if !ok {
|
||||
return goja.Undefined(), nil
|
||||
}
|
||||
_, err := fn(nil, runtime.ToValue(p.id), runtime.ToValue("blob request closed"))
|
||||
return goja.Undefined(), err
|
||||
})
|
||||
}
|
||||
|
||||
func exportBytes(value goja.Value) []byte {
|
||||
if value == nil || goja.IsNull(value) || goja.IsUndefined(value) {
|
||||
return nil
|
||||
}
|
||||
if ab, ok := value.Export().(goja.ArrayBuffer); ok {
|
||||
return ab.Bytes()
|
||||
}
|
||||
if b, ok := value.Export().([]byte); ok {
|
||||
return b
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type blobObjectURLReader struct {
|
||||
ctx context.Context
|
||||
loop *eventloop.EventLoop
|
||||
id string
|
||||
|
||||
mu sync.Mutex
|
||||
buf []byte
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (r *blobObjectURLReader) Read(p []byte) (int, error) {
|
||||
r.mu.Lock()
|
||||
if len(r.buf) > 0 {
|
||||
n := copy(p, r.buf)
|
||||
r.buf = r.buf[n:]
|
||||
r.mu.Unlock()
|
||||
return n, nil
|
||||
}
|
||||
closed := r.closed
|
||||
r.mu.Unlock()
|
||||
if closed {
|
||||
return 0, io.EOF
|
||||
}
|
||||
if err := r.ctx.Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
chunkSize := len(p)
|
||||
if chunkSize <= 0 {
|
||||
chunkSize = 32 * 1024
|
||||
}
|
||||
value, err := runOnLoop(r.loop, func(runtime *goja.Runtime) (goja.Value, error) {
|
||||
fnVal := runtime.Get("__gopeed_blob_read_source")
|
||||
fn, ok := goja.AssertFunction(fnVal)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("blob read helper is not callable")
|
||||
}
|
||||
return fn(nil, runtime.ToValue(r.id), runtime.ToValue(chunkSize))
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if goja.IsNull(value) || goja.IsUndefined(value) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
var chunk []byte
|
||||
if ab, ok := value.Export().(goja.ArrayBuffer); ok {
|
||||
chunk = ab.Bytes()
|
||||
} else if b, ok := value.Export().([]byte); ok {
|
||||
chunk = b
|
||||
} else {
|
||||
return 0, fmt.Errorf("blob read helper returned %T", value.Export())
|
||||
}
|
||||
if len(chunk) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
n := copy(p, chunk)
|
||||
if n < len(chunk) {
|
||||
r.mu.Lock()
|
||||
r.buf = append(r.buf, chunk[n:]...)
|
||||
r.mu.Unlock()
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r *blobObjectURLReader) Close() error {
|
||||
r.mu.Lock()
|
||||
if r.closed {
|
||||
r.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
r.closed = true
|
||||
r.mu.Unlock()
|
||||
_, err := runOnLoop(r.loop, func(runtime *goja.Runtime) (goja.Value, error) {
|
||||
fnVal := runtime.Get("__gopeed_blob_close_source")
|
||||
fn, ok := goja.AssertFunction(fnVal)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
return fn(nil, runtime.ToValue(r.id))
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func runOnLoop(loop *eventloop.EventLoop, fn func(runtime *goja.Runtime) (goja.Value, error)) (goja.Value, error) {
|
||||
type result struct {
|
||||
value goja.Value
|
||||
err error
|
||||
}
|
||||
ch := make(chan result, 1)
|
||||
ok := loop.RunOnLoop(func(runtime *goja.Runtime) {
|
||||
send := func(value goja.Value, err error) {
|
||||
select {
|
||||
case ch <- result{value: value, err: err}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
switch v := r.(type) {
|
||||
case error:
|
||||
send(nil, v)
|
||||
case goja.Value:
|
||||
send(nil, exportJSError(v))
|
||||
default:
|
||||
send(nil, fmt.Errorf("panic: %v", r))
|
||||
}
|
||||
}
|
||||
}()
|
||||
value, err := fn(runtime)
|
||||
if err != nil {
|
||||
send(nil, err)
|
||||
return
|
||||
}
|
||||
if promise, ok := value.Export().(*goja.Promise); ok {
|
||||
switch promise.State() {
|
||||
case goja.PromiseStateFulfilled:
|
||||
send(promise.Result(), nil)
|
||||
return
|
||||
case goja.PromiseStateRejected:
|
||||
send(nil, exportJSError(promise.Result()))
|
||||
return
|
||||
default:
|
||||
thenVal := value.ToObject(runtime).Get("then")
|
||||
thenFn, ok := goja.AssertFunction(thenVal)
|
||||
if !ok {
|
||||
send(nil, errors.New("promise.then is not callable"))
|
||||
return
|
||||
}
|
||||
onFulfilled := runtime.ToValue(func(call goja.FunctionCall) goja.Value {
|
||||
send(call.Argument(0), nil)
|
||||
return goja.Undefined()
|
||||
})
|
||||
onRejected := runtime.ToValue(func(call goja.FunctionCall) goja.Value {
|
||||
send(nil, exportJSError(call.Argument(0)))
|
||||
return goja.Undefined()
|
||||
})
|
||||
if _, err := thenFn(value, onFulfilled, onRejected); err != nil {
|
||||
send(nil, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
send(value, nil)
|
||||
})
|
||||
if !ok {
|
||||
return nil, errors.New("engine loop terminated")
|
||||
}
|
||||
res := <-ch
|
||||
return res.value, res.err
|
||||
}
|
||||
|
||||
func exportJSError(value goja.Value) error {
|
||||
if value == nil || goja.IsUndefined(value) || goja.IsNull(value) {
|
||||
return errors.New("promise rejected")
|
||||
}
|
||||
if err, ok := value.Export().(error); ok {
|
||||
return err
|
||||
}
|
||||
stack := value.String()
|
||||
if ro, ok := value.(*goja.Object); ok {
|
||||
stackVal := ro.Get("stack")
|
||||
if stackVal != nil && stackVal.String() != "" {
|
||||
stack = stackVal.String()
|
||||
}
|
||||
}
|
||||
return errors.New(stack)
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
package stream_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/stream"
|
||||
)
|
||||
|
||||
type createdObjectURL struct {
|
||||
opts *stream.ObjectURLOptions
|
||||
open stream.ObjectURLOpener
|
||||
}
|
||||
|
||||
func TestResponseBlobObjectURLReadsResponseStream(t *testing.T) {
|
||||
const payload = "response blob payload"
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
_, _ = io.WriteString(w, payload[:8])
|
||||
w.(http.Flusher).Flush() // force an unknown-length/chunked response
|
||||
_, _ = io.WriteString(w, payload[8:])
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
created := make(chan createdObjectURL, 1)
|
||||
engine, cleanup := newStreamTestEngine(t, created)
|
||||
defer func() {
|
||||
cleanup()
|
||||
engine.Close()
|
||||
}()
|
||||
|
||||
value, err := engine.RunString(fmt.Sprintf(`
|
||||
(async () => {
|
||||
const response = await fetch(%q);
|
||||
const blob = await response.blob();
|
||||
return __gopeed_blob_create_object_url(blob);
|
||||
})()
|
||||
`, server.URL))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value != "blob:test-1" {
|
||||
t.Fatalf("unexpected object URL: %#v", value)
|
||||
}
|
||||
|
||||
object := <-created
|
||||
if object.opts.Size != int64(len(payload)) || object.opts.ContentType != "text/plain" {
|
||||
t.Fatalf("unexpected object URL options: %#v", object.opts)
|
||||
}
|
||||
if !object.opts.Range {
|
||||
t.Fatal("materialized response Blob should advertise range support")
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
reader, err := object.open(context.Background(), stream.ObjectURLOpenRequest{Offset: 0, End: -1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, readErr := io.ReadAll(reader)
|
||||
_ = reader.Close()
|
||||
if readErr != nil {
|
||||
t.Fatal(readErr)
|
||||
}
|
||||
if got := string(data); got != payload {
|
||||
t.Fatalf("unexpected response Blob data on open %d: %q", i+1, got)
|
||||
}
|
||||
}
|
||||
rangeReader, err := object.open(context.Background(), stream.ObjectURLOpenRequest{Offset: 9, End: 12})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rangeData, readErr := io.ReadAll(rangeReader)
|
||||
_ = rangeReader.Close()
|
||||
if readErr != nil {
|
||||
t.Fatal(readErr)
|
||||
}
|
||||
if got, want := string(rangeData), payload[9:13]; got != want {
|
||||
t.Fatalf("unexpected materialized Blob range: got %q want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseBlobRejectsTruncatedResponse(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Length", "64")
|
||||
_, _ = io.WriteString(w, "partial")
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
created := make(chan createdObjectURL, 1)
|
||||
engine, cleanup := newStreamTestEngine(t, created)
|
||||
defer func() {
|
||||
cleanup()
|
||||
engine.Close()
|
||||
}()
|
||||
|
||||
value, err := engine.RunString(fmt.Sprintf(`
|
||||
(async () => {
|
||||
const response = await fetch(%q);
|
||||
try {
|
||||
await response.blob();
|
||||
return "resolved";
|
||||
} catch (_) {
|
||||
return "rejected";
|
||||
}
|
||||
})()
|
||||
`, server.URL))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value != "rejected" {
|
||||
t.Fatalf("truncated response unexpectedly materialized a Blob: %#v", value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyBlobObjectURL(t *testing.T) {
|
||||
created := make(chan createdObjectURL, 1)
|
||||
engine, cleanup := newStreamTestEngine(t, created)
|
||||
defer func() {
|
||||
cleanup()
|
||||
engine.Close()
|
||||
}()
|
||||
|
||||
value, err := engine.RunString(`
|
||||
__gopeed_blob_create_object_url(new Blob([], { type: "application/empty" }))
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value != "blob:test-1" {
|
||||
t.Fatalf("unexpected object URL: %#v", value)
|
||||
}
|
||||
|
||||
object := <-created
|
||||
if object.opts.Size != 0 || object.opts.Range {
|
||||
t.Fatalf("unexpected empty Blob options: %#v", object.opts)
|
||||
}
|
||||
reader, err := object.open(context.Background(), stream.ObjectURLOpenRequest{Offset: 0, End: -1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer reader.Close()
|
||||
data, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(data) != 0 {
|
||||
t.Fatalf("expected empty Blob, got %q", data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBlobObjectURLReaderCloseCancelsPendingJSReader(t *testing.T) {
|
||||
created := make(chan createdObjectURL, 1)
|
||||
engine, cleanup := newStreamTestEngine(t, created)
|
||||
defer func() {
|
||||
cleanup()
|
||||
engine.Close()
|
||||
}()
|
||||
|
||||
value, err := engine.RunString(`
|
||||
globalThis.__blobPipeCancelState = {
|
||||
readCalls: 0,
|
||||
cancelCalls: 0,
|
||||
releaseCalls: 0,
|
||||
cancelReason: "",
|
||||
resolveRead: null,
|
||||
};
|
||||
const pendingSource = {
|
||||
getReader() {
|
||||
return {
|
||||
read() {
|
||||
__blobPipeCancelState.readCalls++;
|
||||
return new Promise((resolve) => {
|
||||
__blobPipeCancelState.resolveRead = resolve;
|
||||
});
|
||||
},
|
||||
cancel(reason) {
|
||||
__blobPipeCancelState.cancelCalls++;
|
||||
__blobPipeCancelState.cancelReason = String(reason);
|
||||
if (__blobPipeCancelState.resolveRead) {
|
||||
__blobPipeCancelState.resolveRead({ done: true, value: undefined });
|
||||
}
|
||||
return Promise.resolve();
|
||||
},
|
||||
releaseLock() {
|
||||
__blobPipeCancelState.releaseCalls++;
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
__gopeed_blob_create_object_url(async () => pendingSource);
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value != "blob:test-1" {
|
||||
t.Fatalf("unexpected object URL: %#v", value)
|
||||
}
|
||||
|
||||
object := <-created
|
||||
reader, err := object.open(context.Background(), stream.ObjectURLOpenRequest{Offset: 0, End: -1})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
readDone := make(chan error, 1)
|
||||
go func() {
|
||||
_, readErr := reader.Read(make([]byte, 1))
|
||||
readDone <- readErr
|
||||
}()
|
||||
|
||||
waitForJSValue(t, engine, `String(__blobPipeCancelState.readCalls)`, "1")
|
||||
if err := reader.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := reader.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
select {
|
||||
case readErr := <-readDone:
|
||||
if !errors.Is(readErr, context.Canceled) {
|
||||
t.Fatalf("unexpected pending read result: %v", readErr)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("pending Go read was not released by Close")
|
||||
}
|
||||
waitForJSValue(t, engine, `JSON.stringify({
|
||||
cancelCalls: __blobPipeCancelState.cancelCalls,
|
||||
releaseCalls: __blobPipeCancelState.releaseCalls,
|
||||
cancelReason: __blobPipeCancelState.cancelReason,
|
||||
})`, `{"cancelCalls":1,"releaseCalls":1,"cancelReason":"blob request closed"}`)
|
||||
}
|
||||
|
||||
func waitForJSValue(t *testing.T, engine *engine.Engine, expression, want string) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
var got any
|
||||
for time.Now().Before(deadline) {
|
||||
var err error
|
||||
got, err = engine.RunString(expression)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got == want {
|
||||
return
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("timed out waiting for JavaScript value %q, got %#v", want, got)
|
||||
}
|
||||
|
||||
func newStreamTestEngine(t *testing.T, created chan<- createdObjectURL) (*engine.Engine, func()) {
|
||||
t.Helper()
|
||||
var next atomic.Int64
|
||||
e := engine.NewEngine(&engine.Config{
|
||||
StreamConfig: &stream.Config{
|
||||
CreateObjectURL: func(opts *stream.ObjectURLOptions, open stream.ObjectURLOpener) (string, error) {
|
||||
created <- createdObjectURL{opts: opts, open: open}
|
||||
return fmt.Sprintf("blob:test-%d", next.Add(1)), nil
|
||||
},
|
||||
RevokeObjectURL: func(string) error { return nil },
|
||||
},
|
||||
})
|
||||
return e, e.Close
|
||||
}
|
||||
@@ -0,0 +1,810 @@
|
||||
(function () {
|
||||
const createBlobObjectURL = globalThis.__gopeed_create_blob_object_url;
|
||||
const revokeObjectURL = globalThis.__gopeed_revoke_object_url;
|
||||
|
||||
if (typeof globalThis.ReadableStream === "undefined") {
|
||||
class ReadableStreamDefaultController {
|
||||
constructor(stream) {
|
||||
this._stream = stream;
|
||||
}
|
||||
|
||||
enqueue(chunk) {
|
||||
this._stream._enqueue(chunk);
|
||||
}
|
||||
|
||||
close() {
|
||||
this._stream._close();
|
||||
}
|
||||
|
||||
error(err) {
|
||||
this._stream._error(err);
|
||||
}
|
||||
}
|
||||
|
||||
class ReadableStreamDefaultReader {
|
||||
constructor(stream) {
|
||||
this._stream = stream;
|
||||
}
|
||||
|
||||
read(view) {
|
||||
if (view !== undefined) {
|
||||
return this._stream._readInto(view);
|
||||
}
|
||||
return this._stream._read();
|
||||
}
|
||||
|
||||
cancel(reason) {
|
||||
if (!this._stream) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return this._stream.cancel(reason);
|
||||
}
|
||||
|
||||
releaseLock() {
|
||||
if (this._stream) {
|
||||
this._stream._reader = null;
|
||||
this._stream.locked = false;
|
||||
this._stream = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ReadableStream {
|
||||
constructor(source = {}) {
|
||||
this._source = source;
|
||||
this._queue = [];
|
||||
this._waiters = [];
|
||||
this._closed = false;
|
||||
this._errored = null;
|
||||
this._reader = null;
|
||||
this._pulling = false;
|
||||
this._disturbed = false;
|
||||
this.locked = false;
|
||||
this._controller = new ReadableStreamDefaultController(this);
|
||||
if (typeof source.start === "function") {
|
||||
source.start(this._controller);
|
||||
}
|
||||
}
|
||||
|
||||
_enqueue(chunk) {
|
||||
if (this._closed || this._errored) {
|
||||
return;
|
||||
}
|
||||
if (this._waiters.length > 0) {
|
||||
const waiter = this._waiters.shift();
|
||||
waiter.resolve({ done: false, value: chunk });
|
||||
return;
|
||||
}
|
||||
this._queue.push(chunk);
|
||||
}
|
||||
|
||||
_close() {
|
||||
this._closed = true;
|
||||
while (this._waiters.length > 0) {
|
||||
this._waiters.shift().resolve({ done: true, value: undefined });
|
||||
}
|
||||
}
|
||||
|
||||
_error(err) {
|
||||
this._errored = err || new Error("ReadableStream error");
|
||||
while (this._waiters.length > 0) {
|
||||
this._waiters.shift().reject(this._errored);
|
||||
}
|
||||
}
|
||||
|
||||
_read() {
|
||||
if (this._errored) {
|
||||
return Promise.reject(this._errored);
|
||||
}
|
||||
this._disturbed = true;
|
||||
this._markBodyUsed();
|
||||
if (this._queue.length > 0) {
|
||||
return Promise.resolve({ done: false, value: this._queue.shift() });
|
||||
}
|
||||
if (this._closed) {
|
||||
return Promise.resolve({ done: true, value: undefined });
|
||||
}
|
||||
this._maybePull();
|
||||
if (this._queue.length > 0) {
|
||||
return Promise.resolve({ done: false, value: this._queue.shift() });
|
||||
}
|
||||
if (this._closed) {
|
||||
return Promise.resolve({ done: true, value: undefined });
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
this._waiters.push({ resolve, reject });
|
||||
this._maybePull();
|
||||
});
|
||||
}
|
||||
|
||||
async _readInto(view) {
|
||||
if (!ArrayBuffer.isView(view) || view.byteLength === 0) {
|
||||
throw new TypeError("BYOB reads require a non-empty ArrayBufferView");
|
||||
}
|
||||
const result = await this._read();
|
||||
if (result.done) {
|
||||
return { done: true, value: new Uint8Array(view.buffer, view.byteOffset, 0) };
|
||||
}
|
||||
if (!(result.value instanceof Uint8Array)) {
|
||||
throw new TypeError("Byte streams require Uint8Array chunks");
|
||||
}
|
||||
const length = Math.min(view.byteLength, result.value.byteLength);
|
||||
const target = new Uint8Array(view.buffer, view.byteOffset, length);
|
||||
target.set(result.value.subarray(0, length));
|
||||
if (length < result.value.byteLength) {
|
||||
this._queue.unshift(result.value.subarray(length));
|
||||
}
|
||||
return { done: false, value: target };
|
||||
}
|
||||
|
||||
_maybePull() {
|
||||
if (this._closed || this._errored || this._pulling) {
|
||||
return;
|
||||
}
|
||||
if (this._queue.length > 0) {
|
||||
return;
|
||||
}
|
||||
if (!this._source || typeof this._source.pull !== "function") {
|
||||
return;
|
||||
}
|
||||
this._pulling = true;
|
||||
Promise.resolve(this._source.pull(this._controller))
|
||||
.catch((err) => {
|
||||
this._error(err);
|
||||
})
|
||||
.finally(() => {
|
||||
this._pulling = false;
|
||||
if (!this._closed && !this._errored && this._queue.length === 0 && this._waiters.length > 0) {
|
||||
this._maybePull();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_markBodyUsed() {
|
||||
if (typeof this.__gopeedMarkBodyUsed === "function") {
|
||||
this.__gopeedMarkBodyUsed();
|
||||
}
|
||||
}
|
||||
|
||||
getReader() {
|
||||
if (this.locked) {
|
||||
throw new TypeError("ReadableStream is locked");
|
||||
}
|
||||
this.locked = true;
|
||||
this._reader = new ReadableStreamDefaultReader(this);
|
||||
return this._reader;
|
||||
}
|
||||
|
||||
cancel(reason) {
|
||||
this._disturbed = true;
|
||||
this._markBodyUsed();
|
||||
if (this._source && typeof this._source.cancel === "function") {
|
||||
return Promise.resolve(this._source.cancel(reason)).then(() => {
|
||||
this._close();
|
||||
});
|
||||
}
|
||||
this._close();
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
async pipeTo(dest) {
|
||||
const reader = this.getReader();
|
||||
const writer = dest.getWriter();
|
||||
try {
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
await writer.close();
|
||||
break;
|
||||
}
|
||||
await writer.write(value);
|
||||
}
|
||||
} catch (err) {
|
||||
await writer.abort(err);
|
||||
throw err;
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
writer.releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
tee() {
|
||||
const reader = this.getReader();
|
||||
let reading = false;
|
||||
let currentRead = null;
|
||||
let finished = false;
|
||||
const waiting = [[], []];
|
||||
const queues = [[], []];
|
||||
|
||||
const cloneChunk = (chunk) => {
|
||||
if (chunk instanceof ArrayBuffer) return chunk.slice(0);
|
||||
if (ArrayBuffer.isView(chunk)) {
|
||||
const buffer = chunk.buffer.slice(0);
|
||||
if (chunk instanceof DataView) return new DataView(buffer, chunk.byteOffset, chunk.byteLength);
|
||||
return new chunk.constructor(buffer, chunk.byteOffset, chunk.length);
|
||||
}
|
||||
return chunk;
|
||||
};
|
||||
const flush = (index) => {
|
||||
while (waiting[index].length && queues[index].length) {
|
||||
waiting[index].shift().enqueue(queues[index].shift());
|
||||
}
|
||||
if (finished && queues[index].length === 0) {
|
||||
while (waiting[index].length) waiting[index].shift().close();
|
||||
}
|
||||
};
|
||||
const pump = () => {
|
||||
if (reading) return currentRead;
|
||||
if (finished) return Promise.resolve();
|
||||
reading = true;
|
||||
currentRead = (async () => {
|
||||
try {
|
||||
const result = await reader.read();
|
||||
if (result.done) {
|
||||
finished = true;
|
||||
} else {
|
||||
queues[0].push(result.value);
|
||||
queues[1].push(cloneChunk(result.value));
|
||||
}
|
||||
} catch (error) {
|
||||
finished = true;
|
||||
for (const branches of waiting) {
|
||||
while (branches.length) branches.shift().error(error);
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
reading = false;
|
||||
currentRead = null;
|
||||
flush(0);
|
||||
flush(1);
|
||||
}
|
||||
})();
|
||||
return currentRead;
|
||||
};
|
||||
const branch = (index) => new ReadableStream({
|
||||
pull(controller) {
|
||||
waiting[index].push(controller);
|
||||
flush(index);
|
||||
return pump();
|
||||
},
|
||||
});
|
||||
return [branch(0), branch(1)];
|
||||
}
|
||||
}
|
||||
|
||||
globalThis.ReadableStream = ReadableStream;
|
||||
}
|
||||
|
||||
if (typeof globalThis.WritableStream === "undefined") {
|
||||
class WritableStreamDefaultWriter {
|
||||
constructor(stream) {
|
||||
if (stream.locked) {
|
||||
throw new TypeError("WritableStream is locked");
|
||||
}
|
||||
this._stream = stream;
|
||||
this._stream.locked = true;
|
||||
}
|
||||
|
||||
write(chunk) {
|
||||
return this._stream._write(chunk);
|
||||
}
|
||||
|
||||
close() {
|
||||
return this._stream._close();
|
||||
}
|
||||
|
||||
abort(reason) {
|
||||
return this._stream._abort(reason);
|
||||
}
|
||||
|
||||
releaseLock() {
|
||||
if (this._stream) {
|
||||
this._stream.locked = false;
|
||||
this._stream = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class WritableStream {
|
||||
constructor(sink = {}) {
|
||||
this._sink = sink;
|
||||
this._observers = [];
|
||||
this._state = "writable";
|
||||
this.locked = false;
|
||||
}
|
||||
|
||||
_targets() {
|
||||
return [this._sink, ...this._observers];
|
||||
}
|
||||
|
||||
_write(chunk) {
|
||||
if (this._state !== "writable") {
|
||||
return Promise.reject(new TypeError("WritableStream is not writable"));
|
||||
}
|
||||
let chain = Promise.resolve();
|
||||
for (const target of this._targets()) {
|
||||
if (target && typeof target.write === "function") {
|
||||
chain = chain.then(() => target.write(chunk));
|
||||
}
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
_close() {
|
||||
if (this._state !== "writable") {
|
||||
return Promise.reject(new TypeError("WritableStream is not writable"));
|
||||
}
|
||||
this._state = "closed";
|
||||
let chain = Promise.resolve();
|
||||
for (const target of this._targets()) {
|
||||
if (target && typeof target.close === "function") {
|
||||
chain = chain.then(() => target.close());
|
||||
}
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
_abort(reason) {
|
||||
if (this._state === "errored") {
|
||||
return Promise.resolve();
|
||||
}
|
||||
this._state = "errored";
|
||||
let chain = Promise.resolve();
|
||||
for (const target of this._targets()) {
|
||||
if (target && typeof target.abort === "function") {
|
||||
chain = chain.then(() => target.abort(reason));
|
||||
}
|
||||
}
|
||||
return chain;
|
||||
}
|
||||
|
||||
_addObserver(observer) {
|
||||
this._observers.push(observer);
|
||||
}
|
||||
|
||||
getWriter() {
|
||||
return new WritableStreamDefaultWriter(this);
|
||||
}
|
||||
}
|
||||
|
||||
globalThis.WritableStream = WritableStream;
|
||||
}
|
||||
|
||||
if (typeof globalThis.TransformStream === "undefined") {
|
||||
class TransformStream {
|
||||
constructor(transformer = {}) {
|
||||
let readableController;
|
||||
this.readable = new ReadableStream({
|
||||
start(controller) {
|
||||
readableController = controller;
|
||||
}
|
||||
});
|
||||
this.writable = new WritableStream({
|
||||
async write(chunk) {
|
||||
if (typeof transformer.transform === "function") {
|
||||
await transformer.transform(chunk, readableController);
|
||||
return;
|
||||
}
|
||||
readableController.enqueue(chunk);
|
||||
},
|
||||
async close() {
|
||||
if (typeof transformer.flush === "function") {
|
||||
await transformer.flush(readableController);
|
||||
}
|
||||
readableController.close();
|
||||
},
|
||||
async abort(reason) {
|
||||
if (typeof transformer.abort === "function") {
|
||||
await transformer.abort(reason);
|
||||
}
|
||||
readableController.error(reason || new Error("TransformStream aborted"));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
globalThis.TransformStream = TransformStream;
|
||||
}
|
||||
|
||||
if (typeof globalThis.ByteLengthQueuingStrategy === "undefined") {
|
||||
globalThis.ByteLengthQueuingStrategy = class ByteLengthQueuingStrategy {
|
||||
constructor({ highWaterMark }) {
|
||||
this.highWaterMark = highWaterMark;
|
||||
}
|
||||
|
||||
size(chunk) {
|
||||
if (typeof chunk === "string") {
|
||||
return chunk.length;
|
||||
}
|
||||
if (chunk && typeof chunk.byteLength === "number") {
|
||||
return chunk.byteLength;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof globalThis.CountQueuingStrategy === "undefined") {
|
||||
globalThis.CountQueuingStrategy = class CountQueuingStrategy {
|
||||
constructor({ highWaterMark }) {
|
||||
this.highWaterMark = highWaterMark;
|
||||
}
|
||||
|
||||
size() {
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const blobReaders = new Map();
|
||||
|
||||
function getValueTypeName(value) {
|
||||
if (value === null) {
|
||||
return "null";
|
||||
}
|
||||
if (value === undefined) {
|
||||
return "undefined";
|
||||
}
|
||||
if (value && value.constructor && typeof value.constructor.name === "string" && value.constructor.name) {
|
||||
return value.constructor.name;
|
||||
}
|
||||
return typeof value;
|
||||
}
|
||||
|
||||
function toReadableStreamReader(value, sourceLabel) {
|
||||
if (value && typeof value.getReader === "function") {
|
||||
return value.getReader();
|
||||
}
|
||||
throw new TypeError(sourceLabel + " must return a ReadableStream, got " + getValueTypeName(value));
|
||||
}
|
||||
|
||||
async function openBlobReadable(blob, request) {
|
||||
const { offset, end } = request;
|
||||
const sliced = blob.slice(offset, end >= offset ? end + 1 : undefined);
|
||||
if (sliced && typeof sliced.stream === "function") {
|
||||
try {
|
||||
const stream = sliced.stream();
|
||||
if (stream && typeof stream.getReader === "function") {
|
||||
return stream;
|
||||
}
|
||||
} catch (_) {
|
||||
}
|
||||
}
|
||||
if (sliced && typeof sliced.arrayBuffer === "function") {
|
||||
const buffer = await sliced.arrayBuffer();
|
||||
return new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(new Uint8Array(buffer));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
}
|
||||
throw new TypeError("Blob source cannot be converted to a ReadableStream");
|
||||
}
|
||||
|
||||
function describeObjectURLValue(value) {
|
||||
if (value instanceof Blob) {
|
||||
return {
|
||||
kind: "blob",
|
||||
value,
|
||||
};
|
||||
}
|
||||
if (typeof value === "function") {
|
||||
return {
|
||||
kind: "opener",
|
||||
openReadable: value,
|
||||
sourceLabel: "gopeed.runtime.blob.createObjectURL opener function",
|
||||
};
|
||||
}
|
||||
return {
|
||||
kind: "other",
|
||||
value,
|
||||
};
|
||||
}
|
||||
|
||||
function releaseReader(reader, reason) {
|
||||
if (!reader) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (reason !== undefined && typeof reader.cancel === "function") {
|
||||
const result = reader.cancel(reason);
|
||||
if (result && typeof result.catch === "function") {
|
||||
result.catch(function () {});
|
||||
}
|
||||
}
|
||||
} catch (_) {
|
||||
}
|
||||
if (typeof reader.releaseLock === "function") {
|
||||
try {
|
||||
reader.releaseLock();
|
||||
} catch (_) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeBlobObjectURLOptions(value, options, defaultRange) {
|
||||
const normalized = {
|
||||
contentType: "",
|
||||
size: 0,
|
||||
range: !!defaultRange,
|
||||
};
|
||||
if (options && typeof options === "object") {
|
||||
if (typeof options.contentType === "string") {
|
||||
normalized.contentType = options.contentType;
|
||||
}
|
||||
if (Number.isFinite(Number(options.size)) && Number(options.size) > 0) {
|
||||
normalized.size = Number(options.size);
|
||||
}
|
||||
if (typeof options.range === "boolean") {
|
||||
normalized.range = options.range;
|
||||
}
|
||||
}
|
||||
if (!normalized.contentType && value && typeof value.type === "string") {
|
||||
normalized.contentType = value.type;
|
||||
}
|
||||
if (!normalized.size && value) {
|
||||
if (Number.isFinite(Number(value.size)) && Number(value.size) > 0) {
|
||||
normalized.size = Number(value.size);
|
||||
} else if (value._buffer && Number.isFinite(Number(value._buffer.byteLength))) {
|
||||
normalized.size = Number(value._buffer.byteLength);
|
||||
}
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function validateBlobObjectURLOptions(options) {
|
||||
if (options.range && !(Number.isFinite(Number(options.size)) && Number(options.size) > 0)) {
|
||||
throw new TypeError("gopeed.runtime.blob.createObjectURL options.range requires a positive options.size");
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeOpenRequest(request) {
|
||||
const offset = Math.max(0, Number(request && request.offset) || 0);
|
||||
const endValue = Number(request && request.end);
|
||||
const end = Number.isFinite(endValue) ? endValue : -1;
|
||||
return { offset, end };
|
||||
}
|
||||
|
||||
function yieldBlobPipeTask() {
|
||||
return new Promise((resolve) => {
|
||||
if (typeof setTimeout === "function") {
|
||||
setTimeout(resolve, 0);
|
||||
} else {
|
||||
Promise.resolve().then(resolve);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createPendingBlobReader(openReadable, request) {
|
||||
let activeReader;
|
||||
let ready;
|
||||
function ensureReader() {
|
||||
if (ready) {
|
||||
return ready;
|
||||
}
|
||||
ready = (async function () {
|
||||
const source = await openReadable(normalizeOpenRequest(request));
|
||||
activeReader = toReadableStreamReader(source, "gopeed.runtime.blob.createObjectURL opener function");
|
||||
return activeReader;
|
||||
})();
|
||||
return ready;
|
||||
}
|
||||
return {
|
||||
async read() {
|
||||
const reader = await ensureReader();
|
||||
return reader.read();
|
||||
},
|
||||
cancel(reason) {
|
||||
return ensureReader().then(function (reader) {
|
||||
return reader.cancel(reason);
|
||||
}, function () {});
|
||||
},
|
||||
releaseLock() {
|
||||
if (!ready) {
|
||||
return;
|
||||
}
|
||||
return ready.then(function (reader) {
|
||||
return reader.releaseLock();
|
||||
}, function () {});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
globalThis.__gopeed_blob_open_source = function (openReadable, request) {
|
||||
if (typeof openReadable !== "function") {
|
||||
throw new TypeError("blob opener must be callable");
|
||||
}
|
||||
const id = crypto.randomUUID ? crypto.randomUUID() : String(Date.now()) + "-" + String(Math.random());
|
||||
blobReaders.set(id, createPendingBlobReader(openReadable, request));
|
||||
return id;
|
||||
};
|
||||
|
||||
globalThis.__gopeed_blob_read_source = async function (id, chunkSize) {
|
||||
const reader = blobReaders.get(id);
|
||||
if (!reader) {
|
||||
return null;
|
||||
}
|
||||
const { done, value } = await reader.read();
|
||||
if (done) {
|
||||
blobReaders.delete(id);
|
||||
releaseReader(reader);
|
||||
return null;
|
||||
}
|
||||
const chunk = value instanceof Uint8Array ? value : new Uint8Array(value);
|
||||
const size = Math.max(1, Number(chunkSize) || chunk.byteLength);
|
||||
if (chunk.byteLength <= size) {
|
||||
return chunk.buffer.slice(chunk.byteOffset, chunk.byteOffset + chunk.byteLength);
|
||||
}
|
||||
const head = chunk.slice(0, size);
|
||||
let offset = size;
|
||||
blobReaders.set(id, {
|
||||
read() {
|
||||
if (offset >= chunk.byteLength) {
|
||||
return reader.read();
|
||||
}
|
||||
const next = chunk.slice(offset, Math.min(offset + size, chunk.byteLength));
|
||||
offset += next.byteLength;
|
||||
return Promise.resolve({ done: false, value: next });
|
||||
},
|
||||
cancel(reason) {
|
||||
return reader.cancel(reason);
|
||||
},
|
||||
releaseLock() {
|
||||
return reader.releaseLock();
|
||||
}
|
||||
});
|
||||
return head.buffer.slice(head.byteOffset, head.byteOffset + head.byteLength);
|
||||
};
|
||||
|
||||
globalThis.__gopeed_blob_close_source = function (id) {
|
||||
const reader = blobReaders.get(id);
|
||||
if (!reader) {
|
||||
return;
|
||||
}
|
||||
blobReaders.delete(id);
|
||||
releaseReader(reader, "blob request closed");
|
||||
};
|
||||
|
||||
const blobPipeReaderStates = new Map();
|
||||
|
||||
function releaseBlobPipeReader(state) {
|
||||
if (!state || state.released) {
|
||||
return;
|
||||
}
|
||||
state.released = true;
|
||||
if (state.reader && typeof state.reader.releaseLock === "function") {
|
||||
try {
|
||||
state.reader.releaseLock();
|
||||
} catch (_) {
|
||||
}
|
||||
}
|
||||
if (blobPipeReaderStates.get(state.pipeId) === state) {
|
||||
blobPipeReaderStates.delete(state.pipeId);
|
||||
}
|
||||
}
|
||||
|
||||
function finishBlobPipeReader(state, cancel, reason) {
|
||||
if (!state) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
if (cancel) {
|
||||
state.cancelRequested = true;
|
||||
if (reason !== undefined) {
|
||||
state.cancelReason = reason;
|
||||
}
|
||||
}
|
||||
if (state.cleanupPromise) {
|
||||
return state.cleanupPromise;
|
||||
}
|
||||
if (!state.reader) {
|
||||
// A cancellation may arrive while the asynchronous opener is pending.
|
||||
// Keep the state until the reader exists so it can still be canceled.
|
||||
if (!state.cancelRequested || state.openSettled) {
|
||||
releaseBlobPipeReader(state);
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
state.cleanupPromise = (async function () {
|
||||
try {
|
||||
if (state.cancelRequested && typeof state.reader.cancel === "function") {
|
||||
await state.reader.cancel(state.cancelReason);
|
||||
}
|
||||
} catch (_) {
|
||||
} finally {
|
||||
releaseBlobPipeReader(state);
|
||||
}
|
||||
})();
|
||||
return state.cleanupPromise;
|
||||
}
|
||||
|
||||
globalThis.__gopeed_blob_cancel_pipe_source = function (pipeId, reason) {
|
||||
const state = blobPipeReaderStates.get(pipeId);
|
||||
if (!state) {
|
||||
return;
|
||||
}
|
||||
// finishBlobPipeReader invokes reader.cancel() synchronously up to its
|
||||
// first await. Do not return the cleanup Promise: Go only needs to ensure
|
||||
// cancellation has started before releasing the source session.
|
||||
finishBlobPipeReader(state, true, reason).catch(function () {});
|
||||
};
|
||||
|
||||
globalThis.__gopeed_blob_pipe_source = function (openReadable, request, pipeId) {
|
||||
if (typeof openReadable !== "function") {
|
||||
throw new TypeError("blob opener must be callable");
|
||||
}
|
||||
const state = {
|
||||
pipeId,
|
||||
reader: null,
|
||||
cancelRequested: false,
|
||||
cancelReason: "blob pipe closed",
|
||||
cleanupPromise: null,
|
||||
openSettled: false,
|
||||
released: false,
|
||||
};
|
||||
blobPipeReaderStates.set(pipeId, state);
|
||||
const startPipe = async function () {
|
||||
try {
|
||||
const source = await openReadable(normalizeOpenRequest(request));
|
||||
state.openSettled = true;
|
||||
state.reader = toReadableStreamReader(source, "gopeed.runtime.blob.createObjectURL opener function");
|
||||
if (state.cancelRequested) {
|
||||
await finishBlobPipeReader(state, true, state.cancelReason);
|
||||
return;
|
||||
}
|
||||
while (true) {
|
||||
const { done, value } = await state.reader.read();
|
||||
if (done) {
|
||||
globalThis.__gopeed_blob_pipe_close(pipeId);
|
||||
return;
|
||||
}
|
||||
const chunk = value instanceof Uint8Array ? value : new Uint8Array(value);
|
||||
const buffer = chunk.buffer.slice(chunk.byteOffset, chunk.byteOffset + chunk.byteLength);
|
||||
if (!globalThis.__gopeed_blob_pipe_chunk(pipeId, buffer)) {
|
||||
state.cancelRequested = true;
|
||||
state.cancelReason = "blob pipe closed";
|
||||
return;
|
||||
}
|
||||
await yieldBlobPipeTask();
|
||||
}
|
||||
} catch (error) {
|
||||
if (!state.cancelRequested) {
|
||||
globalThis.__gopeed_blob_pipe_error(pipeId, error && error.stack ? error.stack : String(error));
|
||||
}
|
||||
} finally {
|
||||
state.openSettled = true;
|
||||
await finishBlobPipeReader(state, state.cancelRequested, state.cancelReason);
|
||||
}
|
||||
};
|
||||
if (typeof setTimeout === "function") {
|
||||
setTimeout(startPipe, 0);
|
||||
} else {
|
||||
Promise.resolve().then(startPipe);
|
||||
}
|
||||
};
|
||||
|
||||
globalThis.__gopeed_blob_create_object_url = function (value, options) {
|
||||
const described = describeObjectURLValue(value);
|
||||
if (described.kind === "blob") {
|
||||
const blob = described.value;
|
||||
const normalized = normalizeBlobObjectURLOptions(blob, options, Number(blob.size) > 0);
|
||||
validateBlobObjectURLOptions(normalized);
|
||||
const opener = async (request) => openBlobReadable(blob, request);
|
||||
return createBlobObjectURL(opener, normalized);
|
||||
}
|
||||
if (described.kind === "opener") {
|
||||
const normalized = normalizeBlobObjectURLOptions(null, options, false);
|
||||
validateBlobObjectURLOptions(normalized);
|
||||
return createBlobObjectURL(described.openReadable, normalized);
|
||||
}
|
||||
throw new TypeError("Unsupported object type for gopeed.runtime.blob.createObjectURL: " + getValueTypeName(described.value) + ". Expected Blob or opener function");
|
||||
};
|
||||
|
||||
globalThis.__gopeed_blob_revoke_object_url = function (url) {
|
||||
if (typeof url === "string") {
|
||||
revokeObjectURL(url);
|
||||
}
|
||||
};
|
||||
|
||||
})();
|
||||
@@ -1,6 +1,8 @@
|
||||
package vm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/dop251/goja"
|
||||
"github.com/dop251/goja_nodejs/eventloop"
|
||||
)
|
||||
@@ -25,7 +27,12 @@ func (vm *Vm) Get(name string) (value any) {
|
||||
func (vm *Vm) RunString(script string) (value any, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = r.(error)
|
||||
switch v := r.(type) {
|
||||
case error:
|
||||
err = v
|
||||
default:
|
||||
err = fmt.Errorf("panic: %v", r)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
@@ -1,482 +1,15 @@
|
||||
package xhr
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
_ "embed"
|
||||
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/file"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/inject/formdata"
|
||||
"github.com/GopeedLab/gopeed/pkg/download/engine/util"
|
||||
"github.com/dop251/goja"
|
||||
"github.com/imroc/req/v3"
|
||||
)
|
||||
|
||||
const (
|
||||
eventLoad = "load"
|
||||
eventReadystatechange = "readystatechange"
|
||||
eventProgress = "progress"
|
||||
eventAbort = "abort"
|
||||
eventError = "error"
|
||||
eventTimeout = "timeout"
|
||||
)
|
||||
//go:embed xhr.js
|
||||
var script string
|
||||
|
||||
const (
|
||||
redirectError = "error"
|
||||
redirectFollow = "follow"
|
||||
redirectManual = "manual"
|
||||
)
|
||||
|
||||
type ProgressEvent struct {
|
||||
Type string `json:"type"`
|
||||
LengthComputable bool `json:"lengthComputable"`
|
||||
Loaded int64 `json:"loaded"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
type EventProp struct {
|
||||
eventListeners map[string]func(event *ProgressEvent)
|
||||
Onload func(event *ProgressEvent) `json:"onload"`
|
||||
Onprogress func(event *ProgressEvent) `json:"onprogress"`
|
||||
Onabort func(event *ProgressEvent) `json:"onabort"`
|
||||
Onerror func(event *ProgressEvent) `json:"onerror"`
|
||||
Ontimeout func(event *ProgressEvent) `json:"ontimeout"`
|
||||
}
|
||||
|
||||
func (ep *EventProp) AddEventListener(event string, cb func(event *ProgressEvent)) {
|
||||
ep.eventListeners[event] = cb
|
||||
}
|
||||
|
||||
func (ep *EventProp) RemoveEventListener(event string) {
|
||||
delete(ep.eventListeners, event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callOnload() {
|
||||
event := &ProgressEvent{
|
||||
Type: eventLoad,
|
||||
LengthComputable: false,
|
||||
}
|
||||
if ep.Onload != nil {
|
||||
ep.Onload(event)
|
||||
}
|
||||
ep.callEventListener(event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callOnprogress(loaded, total int64) {
|
||||
event := &ProgressEvent{
|
||||
Type: eventProgress,
|
||||
LengthComputable: true,
|
||||
Loaded: loaded,
|
||||
Total: total,
|
||||
}
|
||||
if ep.Onprogress != nil {
|
||||
ep.Onprogress(event)
|
||||
}
|
||||
ep.callEventListener(event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callOnabort() {
|
||||
event := &ProgressEvent{
|
||||
Type: eventAbort,
|
||||
LengthComputable: false,
|
||||
}
|
||||
if ep.Onabort != nil {
|
||||
ep.Onabort(event)
|
||||
}
|
||||
ep.callEventListener(event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callOnerror() {
|
||||
event := &ProgressEvent{
|
||||
Type: eventError,
|
||||
LengthComputable: false,
|
||||
}
|
||||
if ep.Onerror != nil {
|
||||
ep.Onerror(event)
|
||||
}
|
||||
ep.callEventListener(event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callOntimeout() {
|
||||
event := &ProgressEvent{
|
||||
Type: eventTimeout,
|
||||
LengthComputable: false,
|
||||
}
|
||||
if ep.Ontimeout != nil {
|
||||
ep.Ontimeout(event)
|
||||
}
|
||||
ep.callEventListener(event)
|
||||
}
|
||||
|
||||
func (ep *EventProp) callEventListener(event *ProgressEvent) {
|
||||
if cb, ok := ep.eventListeners[event.Type]; ok {
|
||||
cb(event)
|
||||
}
|
||||
}
|
||||
|
||||
type XMLHttpRequestUpload struct {
|
||||
*EventProp
|
||||
}
|
||||
|
||||
type XMLHttpRequest struct {
|
||||
method string
|
||||
url string
|
||||
requestHeaders http.Header
|
||||
responseHeaders http.Header
|
||||
aborted bool
|
||||
client *req.Client
|
||||
fingerprint string
|
||||
|
||||
WithCredentials bool `json:"withCredentials"`
|
||||
Upload *XMLHttpRequestUpload `json:"upload"`
|
||||
Timeout int `json:"timeout"`
|
||||
ReadyState int `json:"readyState"`
|
||||
Status int `json:"status"`
|
||||
StatusText string `json:"statusText"`
|
||||
Response string `json:"response"`
|
||||
ResponseText string `json:"responseText"`
|
||||
// https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/responseURL
|
||||
// https://xhr.spec.whatwg.org/#the-responseurl-attribute
|
||||
ResponseUrl string `json:"responseURL"`
|
||||
// extend fetch redirect
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#redirect
|
||||
// https://fetch.spec.whatwg.org/#concept-request-redirect-mode
|
||||
Redirect string `json:"redirect"`
|
||||
*EventProp
|
||||
Onreadystatechange func(event *ProgressEvent) `json:"onreadystatechange"`
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) Open(method, url string) {
|
||||
xhr.method = method
|
||||
xhr.url = url
|
||||
xhr.requestHeaders = make(http.Header)
|
||||
xhr.responseHeaders = make(http.Header)
|
||||
xhr.doReadystatechange(1)
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) SetRequestHeader(key, value string) {
|
||||
xhr.requestHeaders.Add(key, value)
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) Send(data goja.Value) {
|
||||
setFingerprint(xhr.client, xhr.fingerprint)
|
||||
|
||||
d := xhr.parseData(data)
|
||||
var (
|
||||
contentType string
|
||||
contentLength int64
|
||||
isStringBody bool
|
||||
)
|
||||
|
||||
// Create request using req library
|
||||
reqBuilder := xhr.client.R()
|
||||
|
||||
// Set headers first
|
||||
if xhr.requestHeaders != nil {
|
||||
for key, values := range xhr.requestHeaders {
|
||||
if len(values) > 0 {
|
||||
// Merge multiple values with comma separator (HTTP standard)
|
||||
mergedValue := strings.Join(values, ", ")
|
||||
reqBuilder.SetHeader(key, mergedValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle request body
|
||||
if d != nil && xhr.method != "GET" && xhr.method != "HEAD" {
|
||||
switch v := d.(type) {
|
||||
case string:
|
||||
reqBuilder.SetBody(v)
|
||||
contentType = "text/plain;charset=UTF-8"
|
||||
contentLength = int64(len(v))
|
||||
isStringBody = true
|
||||
case *file.File:
|
||||
reqBuilder.SetBody(v.Reader)
|
||||
contentType = "application/octet-stream"
|
||||
contentLength = v.Size
|
||||
case *formdata.FormData:
|
||||
pr, pw := io.Pipe()
|
||||
mw := NewMultipart(pw)
|
||||
for _, e := range v.Entries() {
|
||||
arr := e.([]any)
|
||||
k := arr[0].(string)
|
||||
v := arr[1]
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
mw.WriteField(k, v)
|
||||
case *file.File:
|
||||
mw.WriteFile(k, v)
|
||||
}
|
||||
}
|
||||
go func() {
|
||||
defer pw.Close()
|
||||
defer mw.Close()
|
||||
mw.Send()
|
||||
}()
|
||||
reqBuilder.SetBody(pr)
|
||||
contentType = mw.FormDataContentType()
|
||||
contentLength = mw.Size()
|
||||
}
|
||||
}
|
||||
|
||||
// Only string body can specify Content-Type header by user
|
||||
if contentType != "" && (!isStringBody || xhr.requestHeaders.Get("Content-Type") == "") {
|
||||
reqBuilder.SetHeader("Content-Type", contentType)
|
||||
}
|
||||
|
||||
// Set timeout
|
||||
if xhr.Timeout > 0 {
|
||||
xhr.client.SetTimeout(time.Duration(xhr.Timeout) * time.Millisecond)
|
||||
}
|
||||
|
||||
// Configure redirect behavior
|
||||
xhr.client.SetRedirectPolicy(func(req *http.Request, via []*http.Request) error {
|
||||
if xhr.Redirect == redirectManual {
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
if xhr.Redirect == redirectError {
|
||||
return errors.New("redirect failed")
|
||||
}
|
||||
if len(via) > 20 {
|
||||
return errors.New("too many redirects")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// Execute request
|
||||
resp, err := reqBuilder.Send(xhr.method, xhr.url)
|
||||
if err != nil {
|
||||
// handle timeout error
|
||||
var ne net.Error
|
||||
if errors.As(err, &ne) && ne.Timeout() {
|
||||
if xhr.Timeout > 0 {
|
||||
xhr.Upload.callOntimeout()
|
||||
xhr.callOntimeout()
|
||||
}
|
||||
return
|
||||
}
|
||||
xhr.Upload.callOnerror()
|
||||
xhr.callOnerror()
|
||||
return
|
||||
}
|
||||
|
||||
xhr.Upload.callOnprogress(contentLength, contentLength)
|
||||
if !xhr.aborted {
|
||||
xhr.Upload.callOnload()
|
||||
}
|
||||
|
||||
// Set response URL (final URL after redirects)
|
||||
if resp.Response != nil && resp.Response.Request != nil && resp.Response.Request.URL != nil {
|
||||
responseUrl := resp.Response.Request.URL
|
||||
responseUrl.Fragment = ""
|
||||
xhr.ResponseUrl = responseUrl.String()
|
||||
} else {
|
||||
xhr.ResponseUrl = xhr.url
|
||||
}
|
||||
|
||||
// Set response headers
|
||||
xhr.responseHeaders = resp.Header
|
||||
xhr.Status = resp.StatusCode
|
||||
xhr.StatusText = resp.Status
|
||||
xhr.doReadystatechange(2)
|
||||
|
||||
bodyBytes := resp.Bytes()
|
||||
xhr.doReadystatechange(3)
|
||||
xhr.Response = string(bodyBytes)
|
||||
xhr.ResponseText = xhr.Response
|
||||
xhr.doReadystatechange(4)
|
||||
respBodyLen := int64(len(bodyBytes))
|
||||
xhr.callOnprogress(respBodyLen, respBodyLen)
|
||||
if !xhr.aborted {
|
||||
xhr.callOnload()
|
||||
}
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) Abort() {
|
||||
xhr.doReadystatechange(0)
|
||||
xhr.aborted = true
|
||||
xhr.Upload.callOnabort()
|
||||
xhr.callOnabort()
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) GetResponseHeader(key string) string {
|
||||
if xhr.responseHeaders == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.Join(xhr.responseHeaders.Values(key), ", ")
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) GetAllResponseHeaders() string {
|
||||
var buf bytes.Buffer
|
||||
for k, v := range xhr.responseHeaders {
|
||||
buf.WriteString(k)
|
||||
buf.WriteString(": ")
|
||||
buf.WriteString(strings.Join(v, ", "))
|
||||
buf.WriteString("\r\n")
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) callOnreadystatechange() {
|
||||
event := &ProgressEvent{
|
||||
Type: eventReadystatechange,
|
||||
LengthComputable: false,
|
||||
}
|
||||
if xhr.Onreadystatechange != nil {
|
||||
xhr.Onreadystatechange(event)
|
||||
}
|
||||
xhr.callEventListener(event)
|
||||
}
|
||||
|
||||
func (xhr *XMLHttpRequest) doReadystatechange(state int) {
|
||||
if xhr.aborted {
|
||||
return
|
||||
}
|
||||
xhr.ReadyState = state
|
||||
xhr.callOnreadystatechange()
|
||||
}
|
||||
|
||||
// parse js data to go struct
|
||||
func (xhr *XMLHttpRequest) parseData(data goja.Value) any {
|
||||
// check if data is null or undefined
|
||||
if data == nil || goja.IsNull(data) || goja.IsUndefined(data) || goja.IsNaN(data) {
|
||||
return nil
|
||||
}
|
||||
// check if data is File
|
||||
f, ok := data.Export().(*file.File)
|
||||
if ok {
|
||||
return f
|
||||
}
|
||||
// check if data is FormData
|
||||
fd, ok := data.Export().(*formdata.FormData)
|
||||
if ok {
|
||||
return fd
|
||||
}
|
||||
// otherwise, return data as string
|
||||
return data.String()
|
||||
}
|
||||
|
||||
func Enable(runtime *goja.Runtime, proxyHandler func(r *http.Request) (*url.URL, error)) error {
|
||||
progressEvent := runtime.ToValue(func(call goja.ConstructorCall) *goja.Object {
|
||||
if len(call.Arguments) < 1 {
|
||||
util.ThrowTypeError(runtime, "Failed to construct 'ProgressEvent': 1 argument required, but only 0 present.")
|
||||
}
|
||||
instance := &ProgressEvent{
|
||||
Type: call.Argument(0).String(),
|
||||
}
|
||||
instanceValue := runtime.ToValue(instance).(*goja.Object)
|
||||
instanceValue.SetPrototype(call.This.Prototype())
|
||||
return instanceValue
|
||||
})
|
||||
xhr := runtime.ToValue(func(call goja.ConstructorCall) *goja.Object {
|
||||
// Create req client with proxy support
|
||||
client := req.C()
|
||||
if proxyHandler != nil {
|
||||
client.SetProxy(proxyHandler)
|
||||
}
|
||||
|
||||
instance := &XMLHttpRequest{
|
||||
client: client,
|
||||
fingerprint: util.SafeGet[string](runtime, FingerprintMagicKey),
|
||||
Upload: &XMLHttpRequestUpload{
|
||||
EventProp: &EventProp{
|
||||
eventListeners: make(map[string]func(event *ProgressEvent)),
|
||||
},
|
||||
},
|
||||
EventProp: &EventProp{
|
||||
eventListeners: make(map[string]func(event *ProgressEvent)),
|
||||
},
|
||||
}
|
||||
instanceValue := runtime.ToValue(instance).(*goja.Object)
|
||||
instanceValue.SetPrototype(call.This.Prototype())
|
||||
return instanceValue
|
||||
})
|
||||
runtime.Set("__gopeed_setFingerprint", func(fingerprint string) {
|
||||
runtime.Set(FingerprintMagicKey, fingerprint)
|
||||
})
|
||||
if err := runtime.Set("ProgressEvent", progressEvent); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := runtime.Set("XMLHttpRequest", xhr); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Wrap multipart.Writer and stat content length
|
||||
type multipartWrapper struct {
|
||||
statBuffer *bytes.Buffer
|
||||
statWriter *multipart.Writer
|
||||
writer *multipart.Writer
|
||||
fields map[string]any
|
||||
}
|
||||
|
||||
func NewMultipart(w io.Writer) *multipartWrapper {
|
||||
var buf bytes.Buffer
|
||||
return &multipartWrapper{
|
||||
statBuffer: &buf,
|
||||
statWriter: multipart.NewWriter(&buf),
|
||||
writer: multipart.NewWriter(w),
|
||||
fields: make(map[string]any),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) WriteField(fieldname string, value string) error {
|
||||
w.fields[fieldname] = value
|
||||
return w.statWriter.WriteField(fieldname, value)
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) WriteFile(fieldname string, file *file.File) error {
|
||||
w.fields[fieldname] = file
|
||||
_, err := w.statWriter.CreateFormFile(fieldname, file.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) Size() int64 {
|
||||
w.statWriter.Close()
|
||||
size := int64(w.statBuffer.Len())
|
||||
for _, v := range w.fields {
|
||||
switch v := v.(type) {
|
||||
case *file.File:
|
||||
size += v.Size
|
||||
}
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) Send() error {
|
||||
for k, v := range w.fields {
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
if err := w.writer.WriteField(k, v); err != nil {
|
||||
return err
|
||||
}
|
||||
case *file.File:
|
||||
fw, err := w.writer.CreateFormFile(k, v.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = io.Copy(fw, v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) FormDataContentType() string {
|
||||
return w.writer.FormDataContentType()
|
||||
}
|
||||
|
||||
func (w *multipartWrapper) Close() error {
|
||||
return w.writer.Close()
|
||||
func Enable(runtime *goja.Runtime) error {
|
||||
_, err := runtime.RunString(script)
|
||||
return err
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package xhr
|
||||
|
||||
import "github.com/imroc/req/v3"
|
||||
|
||||
type Fingerprint string
|
||||
|
||||
const (
|
||||
FingerprintMagicKey = "__gopeed_xhr_fingerprint"
|
||||
|
||||
fingerprintChrome = "chrome"
|
||||
fingerprintFirefox = "firefox"
|
||||
fingerprintSafari = "safari"
|
||||
)
|
||||
|
||||
func setFingerprint(client *req.Client, fingerprint string) {
|
||||
switch fingerprint {
|
||||
case fingerprintChrome:
|
||||
client.ImpersonateChrome()
|
||||
case fingerprintFirefox:
|
||||
client.ImpersonateFirefox()
|
||||
case fingerprintSafari:
|
||||
client.ImpersonateSafari()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
(function () {
|
||||
if (typeof globalThis.ProgressEvent === "undefined") {
|
||||
globalThis.ProgressEvent = class ProgressEvent extends Event {
|
||||
constructor(type, init) {
|
||||
super(type);
|
||||
init = init || {};
|
||||
this.lengthComputable = Boolean(init.lengthComputable);
|
||||
this.loaded = Number(init.loaded) || 0;
|
||||
this.total = Number(init.total) || 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function createProgressEvent(type, loaded, total) {
|
||||
const lengthComputable = Number.isFinite(total) && total >= 0;
|
||||
return new ProgressEvent(type, {
|
||||
lengthComputable,
|
||||
loaded: loaded || 0,
|
||||
total: lengthComputable ? total : 0,
|
||||
});
|
||||
}
|
||||
|
||||
function canonicalHeaderName(name) {
|
||||
return String(name).toLowerCase().split("-").map(function (part) {
|
||||
return part ? part.charAt(0).toUpperCase() + part.slice(1) : part;
|
||||
}).join("-");
|
||||
}
|
||||
|
||||
function bodySize(body) {
|
||||
if (body == null) return 0;
|
||||
if (typeof body === "string") return new TextEncoder().encode(body).byteLength;
|
||||
if (body instanceof ArrayBuffer) return body.byteLength;
|
||||
if (typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(body)) {
|
||||
return body.byteLength;
|
||||
}
|
||||
if (typeof Blob !== "undefined" && body instanceof Blob) return Number(body.size) || 0;
|
||||
if (body && Number.isFinite(Number(body.size))) return Number(body.size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
class XMLHttpRequestUpload extends EventTarget {
|
||||
constructor() {
|
||||
super();
|
||||
this.onloadstart = null;
|
||||
this.onprogress = null;
|
||||
this.onabort = null;
|
||||
this.onerror = null;
|
||||
this.onload = null;
|
||||
this.ontimeout = null;
|
||||
this.onloadend = null;
|
||||
}
|
||||
|
||||
_emit(type, loaded, total) {
|
||||
const event = createProgressEvent(type, loaded, total);
|
||||
const handler = this["on" + type];
|
||||
if (typeof handler === "function") handler.call(this, event);
|
||||
this.dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
class XMLHttpRequest extends EventTarget {
|
||||
constructor() {
|
||||
super();
|
||||
this.readyState = XMLHttpRequest.UNSENT;
|
||||
this.response = "";
|
||||
this.responseText = "";
|
||||
this.responseType = "";
|
||||
this.responseURL = "";
|
||||
this.responseXML = null;
|
||||
this.status = 0;
|
||||
this.statusText = "";
|
||||
this.timeout = 0;
|
||||
this.withCredentials = false;
|
||||
this.upload = new XMLHttpRequestUpload();
|
||||
this.redirect = "follow";
|
||||
this.onreadystatechange = null;
|
||||
this.onloadstart = null;
|
||||
this.onprogress = null;
|
||||
this.onabort = null;
|
||||
this.onerror = null;
|
||||
this.onload = null;
|
||||
this.ontimeout = null;
|
||||
this.onloadend = null;
|
||||
this._method = "GET";
|
||||
this._url = "";
|
||||
this._requestHeaders = new Headers();
|
||||
this._responseHeaders = null;
|
||||
this._controller = null;
|
||||
this._sendStarted = false;
|
||||
this._aborted = false;
|
||||
this._timedOut = false;
|
||||
this._timeoutID = null;
|
||||
}
|
||||
|
||||
open(method, url, async) {
|
||||
if (arguments.length < 2) {
|
||||
throw new TypeError("XMLHttpRequest.open requires at least 2 arguments");
|
||||
}
|
||||
if (async === false) {
|
||||
throw new DOMException("Synchronous XMLHttpRequest is not supported", "NotSupportedError");
|
||||
}
|
||||
this._method = String(method).toUpperCase();
|
||||
this._url = String(url);
|
||||
this._requestHeaders = new Headers();
|
||||
this._responseHeaders = null;
|
||||
this._sendStarted = false;
|
||||
this._aborted = false;
|
||||
this._timedOut = false;
|
||||
this.response = "";
|
||||
this.responseText = "";
|
||||
this.responseURL = "";
|
||||
this.status = 0;
|
||||
this.statusText = "";
|
||||
this._setReadyState(XMLHttpRequest.OPENED);
|
||||
}
|
||||
|
||||
setRequestHeader(name, value) {
|
||||
if (this.readyState !== XMLHttpRequest.OPENED || this._sendStarted) {
|
||||
throw new DOMException("XMLHttpRequest is not opened", "InvalidStateError");
|
||||
}
|
||||
this._requestHeaders.append(name, value);
|
||||
}
|
||||
|
||||
getResponseHeader(name) {
|
||||
if (!this._responseHeaders || this.readyState < XMLHttpRequest.HEADERS_RECEIVED) return null;
|
||||
return this._responseHeaders.get(name);
|
||||
}
|
||||
|
||||
getAllResponseHeaders() {
|
||||
if (!this._responseHeaders || this.readyState < XMLHttpRequest.HEADERS_RECEIVED) return "";
|
||||
let result = "";
|
||||
this._responseHeaders.forEach(function (value, name) {
|
||||
result += canonicalHeaderName(name) + ": " + value + "\r\n";
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
overrideMimeType() {
|
||||
// Kept as a compatibility no-op until MIME decoding is configurable.
|
||||
}
|
||||
|
||||
send(body) {
|
||||
if (this.readyState !== XMLHttpRequest.OPENED || this._sendStarted) {
|
||||
throw new DOMException("XMLHttpRequest is not opened", "InvalidStateError");
|
||||
}
|
||||
this._sendStarted = true;
|
||||
this._aborted = false;
|
||||
this._timedOut = false;
|
||||
this._controller = new AbortController();
|
||||
const uploadTotal = bodySize(body);
|
||||
this._emit("loadstart", 0, -1);
|
||||
this.upload._emit("loadstart", 0, uploadTotal);
|
||||
|
||||
if (this.timeout > 0) {
|
||||
this._timeoutID = setTimeout(() => {
|
||||
this._timedOut = true;
|
||||
this._controller.abort();
|
||||
}, this.timeout);
|
||||
}
|
||||
|
||||
const options = {
|
||||
method: this._method,
|
||||
headers: this._requestHeaders,
|
||||
redirect: this.redirect || "follow",
|
||||
credentials: this.withCredentials ? "include" : "same-origin",
|
||||
signal: this._controller.signal,
|
||||
};
|
||||
if (this._method !== "GET" && this._method !== "HEAD" && body != null) {
|
||||
options.body = body;
|
||||
}
|
||||
|
||||
Promise.resolve().then(() => globalThis.fetch(this._url, options)).then(async (response) => {
|
||||
if (this._aborted || this._timedOut) return;
|
||||
this.upload._emit("progress", uploadTotal, uploadTotal);
|
||||
this.upload._emit("load", uploadTotal, uploadTotal);
|
||||
this.upload._emit("loadend", uploadTotal, uploadTotal);
|
||||
|
||||
this.status = response.status;
|
||||
this.statusText = response.statusText;
|
||||
this.responseURL = response.url || this._url;
|
||||
this._responseHeaders = response.headers;
|
||||
this._setReadyState(XMLHttpRequest.HEADERS_RECEIVED);
|
||||
|
||||
const contentLengthValue = response.headers.get("content-length");
|
||||
const contentLength = contentLengthValue == null ? -1 : Number(contentLengthValue);
|
||||
const chunks = [];
|
||||
let loaded = 0;
|
||||
if (response.body) {
|
||||
const reader = response.body.getReader();
|
||||
try {
|
||||
while (true) {
|
||||
const item = await reader.read();
|
||||
if (item.done) break;
|
||||
const chunk = item.value instanceof Uint8Array ? item.value : new Uint8Array(item.value);
|
||||
chunks.push(chunk);
|
||||
loaded += chunk.byteLength;
|
||||
this._setReadyState(XMLHttpRequest.LOADING);
|
||||
this._emit("progress", loaded, contentLength);
|
||||
}
|
||||
} finally {
|
||||
if (typeof reader.releaseLock === "function") reader.releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
const bytes = new Uint8Array(loaded);
|
||||
let offset = 0;
|
||||
for (const chunk of chunks) {
|
||||
bytes.set(chunk, offset);
|
||||
offset += chunk.byteLength;
|
||||
}
|
||||
const text = new TextDecoder().decode(bytes);
|
||||
switch (this.responseType) {
|
||||
case "arraybuffer":
|
||||
this.response = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
||||
break;
|
||||
case "blob":
|
||||
this.response = new Blob([bytes], { type: response.headers.get("content-type") || "" });
|
||||
break;
|
||||
case "json":
|
||||
try {
|
||||
this.response = text === "" ? null : JSON.parse(text);
|
||||
} catch (_) {
|
||||
this.response = null;
|
||||
}
|
||||
break;
|
||||
case "":
|
||||
case "text":
|
||||
default:
|
||||
this.response = text;
|
||||
this.responseText = text;
|
||||
break;
|
||||
}
|
||||
this._clearTimeout();
|
||||
this._setReadyState(XMLHttpRequest.DONE);
|
||||
this._emit("load", loaded, contentLength);
|
||||
this._emit("loadend", loaded, contentLength);
|
||||
}).catch((error) => {
|
||||
this._clearTimeout();
|
||||
if (this._timedOut) {
|
||||
this.status = 0;
|
||||
this._setReadyState(XMLHttpRequest.DONE);
|
||||
this.upload._emit("timeout", 0, uploadTotal);
|
||||
this._emit("timeout", 0, -1);
|
||||
this._emit("loadend", 0, -1);
|
||||
return;
|
||||
}
|
||||
if (this._aborted || (error && error.name === "AbortError")) {
|
||||
this.status = 0;
|
||||
this._setReadyState(XMLHttpRequest.DONE);
|
||||
this.upload._emit("abort", 0, uploadTotal);
|
||||
this._emit("abort", 0, -1);
|
||||
this._emit("loadend", 0, -1);
|
||||
return;
|
||||
}
|
||||
this.status = 0;
|
||||
this._setReadyState(XMLHttpRequest.DONE);
|
||||
this.upload._emit("error", 0, uploadTotal);
|
||||
this._emit("error", 0, -1);
|
||||
this._emit("loadend", 0, -1);
|
||||
});
|
||||
}
|
||||
|
||||
abort() {
|
||||
if (!this._sendStarted || this.readyState === XMLHttpRequest.DONE) return;
|
||||
this._aborted = true;
|
||||
this._clearTimeout();
|
||||
if (this._controller) this._controller.abort();
|
||||
}
|
||||
|
||||
_clearTimeout() {
|
||||
if (this._timeoutID != null) {
|
||||
clearTimeout(this._timeoutID);
|
||||
this._timeoutID = null;
|
||||
}
|
||||
}
|
||||
|
||||
_setReadyState(state) {
|
||||
this.readyState = state;
|
||||
this._emit("readystatechange", 0, -1);
|
||||
}
|
||||
|
||||
_emit(type, loaded, total) {
|
||||
const event = createProgressEvent(type, loaded, total);
|
||||
const handler = this["on" + type];
|
||||
if (typeof handler === "function") handler.call(this, event);
|
||||
this.dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
XMLHttpRequest.UNSENT = 0;
|
||||
XMLHttpRequest.OPENED = 1;
|
||||
XMLHttpRequest.HEADERS_RECEIVED = 2;
|
||||
XMLHttpRequest.LOADING = 3;
|
||||
XMLHttpRequest.DONE = 4;
|
||||
XMLHttpRequest.prototype.UNSENT = 0;
|
||||
XMLHttpRequest.prototype.OPENED = 1;
|
||||
XMLHttpRequest.prototype.HEADERS_RECEIVED = 2;
|
||||
XMLHttpRequest.prototype.LOADING = 3;
|
||||
XMLHttpRequest.prototype.DONE = 4;
|
||||
|
||||
globalThis.XMLHttpRequest = XMLHttpRequest;
|
||||
})();
|
||||
@@ -1 +1 @@
|
||||
(()=>{var e={725:function(e,t,r){var o,n,i;i="undefined"!=typeof self&&self||"undefined"!=typeof window&&window||void 0!==r.g&&r.g||this,o=function(e){"use strict";var t=i.BlobBuilder||i.WebKitBlobBuilder||i.MSBlobBuilder||i.MozBlobBuilder,r=i.URL||i.webkitURL||function(e,t){return(t=document.createElement("a")).href=e,t},o=i.Blob,n=r.createObjectURL,a=r.revokeObjectURL,s=i.Symbol&&i.Symbol.toStringTag,u=!1,f=!1,l=t&&t.prototype.append&&t.prototype.getBlob;try{u=2===new Blob(["ä"]).size,f=2===new Blob([new Uint8Array([1,2])]).size}catch(e){}function c(e){return e.map((function(e){if(e.buffer instanceof ArrayBuffer){var t=e.buffer;if(e.byteLength!==t.byteLength){var r=new Uint8Array(e.byteLength);r.set(new Uint8Array(t,e.byteOffset,e.byteLength)),t=r.buffer}return t}return e}))}function h(e,r){r=r||{};var o=new t;return c(e).forEach((function(e){o.append(e)})),r.type?o.getBlob(r.type):o.getBlob()}function d(e,t){return new o(c(e),t||{})}i.Blob&&(h.prototype=Blob.prototype,d.prototype=Blob.prototype);var p="function"==typeof TextEncoder?TextEncoder.prototype.encode.bind(new TextEncoder):function(e){for(var t=0,r=e.length,o=i.Uint8Array||Array,n=0,a=Math.max(32,r+(r>>1)+7),s=new o(a>>3<<3);t<r;){var u=e.charCodeAt(t++);if(u>=55296&&u<=56319){if(t<r){var f=e.charCodeAt(t);56320==(64512&f)&&(++t,u=((1023&u)<<10)+(1023&f)+65536)}if(u>=55296&&u<=56319)continue}if(n+4>s.length){a+=8,a=(a*=1+t/e.length*2)>>3<<3;var l=new Uint8Array(a);l.set(s),s=l}if(0!=(4294967168&u)){if(0==(4294965248&u))s[n++]=u>>6&31|192;else if(0==(4294901760&u))s[n++]=u>>12&15|224,s[n++]=u>>6&63|128;else{if(0!=(4292870144&u))continue;s[n++]=u>>18&7|240,s[n++]=u>>12&63|128,s[n++]=u>>6&63|128}s[n++]=63&u|128}else s[n++]=u}return s.slice(0,n)},y="function"==typeof TextDecoder?TextDecoder.prototype.decode.bind(new TextDecoder):function(e){for(var t=e.length,r=[],o=0;o<t;){var n,i,a,s,u=e[o],f=null,l=u>239?4:u>223?3:u>191?2:1;if(o+l<=t)switch(l){case 1:u<128&&(f=u);break;case 2:128==(192&(n=e[o+1]))&&(s=(31&u)<<6|63&n)>127&&(f=s);break;case 3:n=e[o+1],i=e[o+2],128==(192&n)&&128==(192&i)&&(s=(15&u)<<12|(63&n)<<6|63&i)>2047&&(s<55296||s>57343)&&(f=s);break;case 4:n=e[o+1],i=e[o+2],a=e[o+3],128==(192&n)&&128==(192&i)&&128==(192&a)&&(s=(15&u)<<18|(63&n)<<12|(63&i)<<6|63&a)>65535&&s<1114112&&(f=s)}null===f?(f=65533,l=1):f>65535&&(f-=65536,r.push(f>>>10&1023|55296),f=56320|1023&f),r.push(f),o+=l}for(var c=r.length,h="",d=0;d<c;)h+=String.fromCharCode.apply(String,r.slice(d,d+=4096));return h};function b(){var t=!!i.ActiveXObject||"-ms-scroll-limit"in document.documentElement.style&&"-ms-ime-align"in document.documentElement.style,r=i.XMLHttpRequest&&i.XMLHttpRequest.prototype.send;t&&r&&(XMLHttpRequest.prototype.send=function(e){e instanceof Blob?(this.setRequestHeader("Content-Type",e.type),r.call(this,e)):r.call(this,e)});try{new File([],""),e.File=i.File,e.FileReader=i.FileReader}catch(t){try{e.File=new Function('class File extends Blob {constructor(chunks, name, opts) {opts = opts || {};super(chunks, opts || {});this.name = name.replace(/\\//g, ":");this.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date();this.lastModified = +this.lastModifiedDate;}};return new File([], ""), File')()}catch(t){e.File=function(e,t,r){var o=new Blob(e,r),n=r&&void 0!==r.lastModified?new Date(r.lastModified):new Date;return o.name=t.replace(/\//g,":"),o.lastModifiedDate=n,o.lastModified=+n,o.toString=function(){return"[object File]"},s&&(o[s]="File"),o}}}}u?(b(),e.Blob=f?i.Blob:d):l?(b(),e.Blob=h):function(){function t(e){for(var t=new Array(e.byteLength),r=new Uint8Array(e),o=t.length;o--;)t[o]=r[o];return t}function o(e){for(var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",r=[],o=0;o<e.length;o+=3){var n=e[o],i=o+1<e.length,a=i?e[o+1]:0,s=o+2<e.length,u=s?e[o+2]:0,f=n>>2,l=(3&n)<<4|a>>4,c=(15&a)<<2|u>>6,h=63&u;s||(h=64,i||(c=64)),r.push(t[f],t[l],t[c],t[h])}return r.join("")}var s=Object.create||function(e){function t(){}return t.prototype=e,new t};function u(e){return Object.prototype.toString.call(e).slice(8,-1)}function f(e,t){return"object"==typeof e&&Object.prototype.isPrototypeOf.call(e.prototype,t)}var l=["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","ArrayBuffer"];function c(e){return t=l,r=u(e),-1!==t.indexOf(r)||f(i.ArrayBuffer,e);var t,r}function h(e,r){r=null==r?{}:r;for(var o=0,n=(e=e?e.slice():[]).length;o<n;o++){var a=e[o];a instanceof h?e[o]=a._buffer:"string"==typeof a?e[o]=p(a):"DataView"===u(s=a)||f(i.DataView,s)?e[o]=t(a.buffer):c(a)?e[o]=t(a):e[o]=p(String(a))}var s;this._buffer=i.Uint8Array?function(e){for(var t=0,r=e.length;r--;)t+=e[r].length;for(var o=new Uint8Array(t),n=0,i=0;i<e.length;i++){var a=e[i];o.set(a,n),n+=a.byteLength||a.length}return o}(e):[].concat.apply([],e),this.size=this._buffer.length,this.type=r.type||"",/[^\u0020-\u007E]/.test(this.type)?this.type="":this.type=this.type.toLowerCase()}funcLine truncated
|
||||
(()=>{var e={847(e,t,n){var r,o;r=function(){"use strict";function e(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function t(e,t,n){return t=s(t),function(e,t){if(t&&("object"==typeof t||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}(e,c()?Reflect.construct(t,n||[],s(e).constructor):t.apply(e,n))}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,f(r.key),r)}}function a(e,t,n){return t&&o(e.prototype,t),n&&o(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}function i(t,n){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=function(t,n){if(t){if("string"==typeof t)return e(t,n);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?e(t,n):void 0}}(t))||n&&t&&"number"==typeof t.length){r&&(t=r);var o=0,a=function(){};return{s:a,n:function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}},e:function(e){throw e},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,l=!0,s=!1;return{s:function(){r=r.call(t)},n:function(){var e=r.next();return l=e.done,e},e:function(e){s=!0,i=e},f:function(){try{l||null==r.return||r.return()}finally{if(s)throw i}}}}function l(){return l="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(e,t,n){var r=function(e,t){for(;!{}.hasOwnProperty.call(e,t)&&null!==(e=s(e)););return e}(e,t);if(r){var o=Object.getOwnPropertyDescriptor(r,t);return o.get?o.get.call(arguments.length<3?e:n):o.value}},l.apply(null,arguments)}function s(e){return s=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},s(e)}function c(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(c=function(){return!!e})()}function u(e,t){return u=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},u(e,t)}function f(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}!function(e){e.AbortSignal,e.AbortController}("undefined"!=typeof self?self:n.g);var p=function(){return a(function e(){r(this,e),Object.defineProperty(this,"listeners",{value:{},writable:!0,configurable:!0})},[{key:"addEventListener",value:function(e,t,n){e in this.listeners||(this.listeners[e]=[]),this.listeners[e].push({callback:t,options:n})}},{key:"removeEventListener",value:function(e,t){if(e in this.listeners)for(var n=this.listeners[e],r=0,o=n.length;r<o;r++)if(n[r].callback===t)return void n.splice(r,1)}},{key:"dispatchEvent",value:function(e){var t=this;if(e.type in this.listeners){for(var n=this.listeners[e.type].slice(),r=function(){var r=n[o];try{r.callback.call(t,e)}catch(e){Promise.resolve().then(function(){throw e})}r.options&&r.options.once&&t.removeEventListener(e.type,r.callback)},o=0,a=n.length;o<a;o++)r();return!e.defaultPrevented}}}])}(),b=function(e){function n(){var e;return r(this,n),(e=t(this,n)).listeners||p.call(e),Object.defineProperty(e,"aborted",{value:!1,writable:!0,configurable:!0}),Object.defineProperty(e,"onabort",{value:null,writable:!0,configurable:!0}),Object.defineProperty(e,"reason",{value:void 0,writable:!0,configurable:!0}),e}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),Object.defineProperty(e,"prototype",{writable:!1}),t&&u(e,t)}(n,e),a(n,[{key:"toString",value:function(){return"[object AbortSignal]"}},{key:"dispatchEvent",value:function(e){var t,r,o,a,i;"abort"===e.type&&(this.aborted=!0,"function"==typeof this.onabort&&this.onabort.call(this,e)),(t=n,r="dispatchEvent",o=this,i=l(s(1&(a=3)?t.prototype:t),r,o),2&a&&"function"==typeof i?function(e){return i.apply(o,e)}:i)([e])}},{key:"throwIfAborted",value:function(){var e=this.aborted,t=this.reason;if(e)throw void 0===t?"Aborted":t}}],[{key:"timeout",value:function(e){var t=new d;return setTimeout(function(){return t.abort(new DOMException("This signal is timeout in ".concat(e,"ms"),"TimeoutError"))},e),t.signal}},{keLine truncated
|
||||
+891
-2133
File diff suppressed because it is too large.
Load diff
@@ -18,8 +18,9 @@
|
||||
"webpack-cli": "^5.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"abortcontroller-polyfill": "^1.7.8",
|
||||
"blob-polyfill": "^7.0.20220408",
|
||||
"fastestsmallesttextencoderdecoder": "^1.0.22",
|
||||
"whatwg-fetch": "^3.6.20"
|
||||
"event-target-shim": "^6.0.2",
|
||||
"fastestsmallesttextencoderdecoder": "^1.0.22"
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
diff --git a/node_modules/whatwg-fetch/dist/fetch.umd.js b/node_modules/whatwg-fetch/dist/fetch.umd.js
|
||||
index 7a0d852..604691e 100644
|
||||
--- a/node_modules/whatwg-fetch/dist/fetch.umd.js
|
||||
+++ b/node_modules/whatwg-fetch/dist/fetch.umd.js
|
||||
@@ -394,6 +394,7 @@
|
||||
}
|
||||
}());
|
||||
this.referrer = null;
|
||||
+ this.redirect = options.redirect || 'follow'
|
||||
|
||||
if ((this.method === 'GET' || this.method === 'HEAD') && body) {
|
||||
throw new TypeError('Body not allowed for GET or HEAD requests')
|
||||
@@ -606,6 +607,10 @@
|
||||
}
|
||||
}
|
||||
|
||||
+ if ('redirect' in xhr) {
|
||||
+ xhr.redirect = request.redirect
|
||||
+ }
|
||||
+
|
||||
if (init && typeof init.headers === 'object' && !(init.headers instanceof Headers || (g.Headers && init.headers instanceof g.Headers))) {
|
||||
var names = [];
|
||||
Object.getOwnPropertyNames(init.headers).forEach(function(name) {
|
||||
diff --git a/node_modules/whatwg-fetch/fetch.js b/node_modules/whatwg-fetch/fetch.js
|
||||
index f39a983..d1fc903 100644
|
||||
--- a/node_modules/whatwg-fetch/fetch.js
|
||||
+++ b/node_modules/whatwg-fetch/fetch.js
|
||||
@@ -388,6 +388,7 @@ export function Request(input, options) {
|
||||
}
|
||||
}());
|
||||
this.referrer = null
|
||||
+ this.redirect = options.redirect || 'follow'
|
||||
|
||||
if ((this.method === 'GET' || this.method === 'HEAD') && body) {
|
||||
throw new TypeError('Body not allowed for GET or HEAD requests')
|
||||
@@ -600,6 +601,10 @@ export function fetch(input, init) {
|
||||
}
|
||||
}
|
||||
|
||||
+ if ('redirect' in xhr) {
|
||||
+ xhr.redirect = request.redirect
|
||||
+ }
|
||||
+
|
||||
if (init && typeof init.headers === 'object' && !(init.headers instanceof Headers || (g.Headers && init.headers instanceof g.Headers))) {
|
||||
var names = [];
|
||||
Object.getOwnPropertyNames(init.headers).forEach(function(name) {
|
||||
@@ -0,0 +1,2 @@
|
||||
import "../event/index.js";
|
||||
import "abortcontroller-polyfill/dist/abortcontroller-polyfill-only.js";
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Event, EventTarget } from "event-target-shim";
|
||||
|
||||
if (typeof globalThis.Event !== "function") {
|
||||
globalThis.Event = Event;
|
||||
}
|
||||
|
||||
if (typeof globalThis.EventTarget !== "function") {
|
||||
globalThis.EventTarget = EventTarget;
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
import 'whatwg-fetch'
|
||||
@@ -1,5 +1,6 @@
|
||||
import "./event/index.js"
|
||||
import "./abort/index.js"
|
||||
import "./blob/index.js"
|
||||
import "./crypto/index.js"
|
||||
// polyfill TextEncoder
|
||||
import 'fastestsmallesttextencoderdecoder';
|
||||
import "./fetch/index.js"
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
# The 3-Clause BSD License
|
||||
|
||||
Copyright © web-platform-tests contributors
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
Loaded 100 of 927 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user