Compare commits

...
140 Commits
Author SHA1 Message Date
Li Wei 21ec1e4539 fix: follow android 11 storage permissions (#66) 2022-12-16 21:43:06 +08:00
levi dda44cc770 fix: docker tag 2022-12-16 15:24:41 +08:00
levi 78911b2aa9 ci: fix docker build 2022-12-16 15:10:34 +08:00
Li Wei 4935dcd0e6 ci: fix 2022-12-16 15:02:33 +08:00
Li Wei e8fe6ad550 ci: docker supports more platforms (#64) 2022-12-16 14:56:56 +08:00
Li Wei db4003ad4f fix: speed calculate (#63) 2022-12-16 14:53:07 +08:00
Li Wei 0a4be165a5 fix: http download fail (#61) 2022-12-14 12:12:39 +08:00
levi c62867e3ef chore: fix docker-compose command 2022-12-13 15:00:32 +08:00
levi 6df4959845 ci: fix 2022-12-13 14:49:53 +08:00
levi e0b18013be chore: release v1.2.0 2022-12-13 14:48:01 +08:00
levi d205e3b372 feat: enable ru_RU 2022-12-13 14:47:08 +08:00
azatzin30 719daf054b feat: Translation to Russian language (#60) 2022-12-13 14:42:08 +08:00
levi a5f72a22c3 fix: i18n 2022-12-13 14:39:51 +08:00
levi bf5c7655f2 test: remove fail code 2022-12-12 16:06:00 +08:00
Li Wei 691c2f496e feat: support custom tracker url (#59) 2022-12-12 15:58:11 +08:00
gesneriana 9ef3405e43 fix: missing single quotes (#57) 2022-11-16 13:34:15 +08:00
azatzin30 215faa0bf0 feat: Added Russian language (#55) 2022-11-11 22:17:38 +08:00
cgcel 695da7ea87 feat: support docker compose (#54) 2022-11-10 14:13:34 +08:00
liwei 7c6c361d30 fix: docker dist copy 2022-11-08 22:55:56 +08:00
liwei 872ba0ba87 doc: fix docker run command 2022-11-08 22:36:47 +08:00
monkeyWie 93c14dcee5 fix: ip bind (#50) 2022-11-08 22:29:33 +08:00
liwei 1af0b2175c ci: path include Dockerfile 2022-11-08 21:50:45 +08:00
monkeyWie ee81ebffef fix: docker executable (#49) 2022-11-08 21:49:02 +08:00
levi dc00fd2543 doc: fix 2022-11-08 15:02:58 +08:00
levi 11015c7be1 doc: mac repair tip 2022-11-08 14:35:51 +08:00
monkeyWie 57befd2c03 fix: web download dir setting (#46) 2022-11-08 14:31:49 +08:00
monkeyWie 35156ed968 ci: remove windows zip root folder 2022-11-07 20:29:10 +08:00
levi ee6c36174f ci: fix version 2022-11-07 17:25:23 +08:00
levi fdfd95555d ci: fix docker build 2022-11-07 17:20:31 +08:00
levi 7ec824edda ci: fix 2022-11-07 17:13:00 +08:00
levi 1f8b1be669 ci: fix action version 2022-11-07 16:49:58 +08:00
levi b0bcfeae34 ci: fix action version 2022-11-07 16:48:02 +08:00
levi 10b346a326 ci: fix 2022-11-07 16:45:44 +08:00
levi a85af39b35 ci: fix build 2022-11-07 16:43:55 +08:00
levi 576f0d2e24 ci: fix docker tag 2022-11-07 16:41:52 +08:00
monkeyWie 95eb3a427c feat: docker support (#43) 2022-11-07 16:32:52 +08:00
monkeyWie e9442edd94 feat: support drag torrent file (#42) 2022-11-07 14:12:12 +08:00
monkeyWie 77e59f7376 feat: i18n support (#40) 2022-11-05 17:04:22 +08:00
monkeyWie 135fba1b54 doc: fix md 2022-11-05 13:16:57 +08:00
liwei ff813e3bc9 ci: build on published 2022-11-05 11:27:56 +08:00
Trim21 babfe79023 Update flags.go 2022-11-05 10:43:07 +08:00
liwei 89ac52bebd ci: path ignore 2022-11-04 21:48:22 +08:00
liwei 15e1c2f2d5 chore: git ignore generated_plugin 2022-11-04 21:43:31 +08:00
levi 203f0ccc82 chore: fix go version 2022-11-04 17:13:32 +08:00
levi 2422bd2c66 chore: update md 2022-11-04 16:38:07 +08:00
levi 0306a1ed6b chore: en link 2022-11-04 16:26:19 +08:00
levi 8a94650370 chore: en readme 2022-11-04 16:25:57 +08:00
levi 837a193e96 chore: add discord link 2022-11-04 15:40:31 +08:00
levi fcf0746f9a ci: fix version 2022-11-04 15:12:38 +08:00
levi 9eb466ccd3 ci: path ignore 2022-11-04 15:11:06 +08:00
levi c54f22613e chore: issue tpl 2022-11-04 15:06:54 +08:00
liwei 3e1cfa0a27 ci: release v1.0.0 again 2022-11-03 23:31:48 +08:00
liwei 9d22b49e41 ci: fix windows build 2022-11-03 23:23:23 +08:00
liwei 1c6df55975 ci: debug 2022-11-03 23:17:40 +08:00
liwei 237616ded2 ci: debug 2022-11-03 23:15:35 +08:00
liwei cebdda564e ci: debug windows 2022-11-03 22:18:15 +08:00
liwei 08ffb51946 ci: build windows 2022-11-03 21:21:39 +08:00
levi 8378f51f8e ci: fix windows 2022-11-03 20:11:28 +08:00
levi 978d786f99 ci: fix windows 2022-11-03 19:56:17 +08:00
levi 8b997fa7f2 ci: fix windows build 2022-11-03 19:38:00 +08:00
levi 745afb600c ci: debug windows 2022-11-03 19:21:59 +08:00
levi b45b840afb ci: fix windows build 2022-11-03 19:03:12 +08:00
levi 2a7a136203 ci: debug 2022-11-03 18:13:09 +08:00
levi dfb8958999 ci: debug 2022-11-03 18:05:24 +08:00
levi 1568f4d6ff ci: debug windows 2022-11-03 17:56:55 +08:00
levi 12ebceecb0 ci: debug windows 2022-11-03 17:34:23 +08:00
levi 8cfe46cb71 ci: debug windows mingw dir 2022-11-03 17:01:49 +08:00
liwei 2f1b929bcd ci: fix windows build 2022-11-02 22:55:11 +08:00
liwei 2144cd8c3a ci: release v1.0.0 2022-11-02 22:40:35 +08:00
liwei 080b34d070 ci: fix windows 2022-11-02 22:14:06 +08:00
liwei de73d2a29f ci: fix windows build 2022-11-02 22:04:45 +08:00
liwei 9de2b765fb chore: add windows build step 2022-11-02 21:58:49 +08:00
liwei be75b50521 ci: build windows 2022-11-02 21:58:34 +08:00
levi 39aec19471 chore: update md 2022-11-02 17:55:00 +08:00
monkeyWie 9884001d54 chore: update md 2022-11-02 17:35:08 +08:00
levi 29c834a070 chore: add linux build step 2022-11-02 17:26:03 +08:00
levi b16f348676 chore: update md 2022-11-02 17:23:10 +08:00
monkeyWie 7b46c2a2f6 chore: create LICENSE 2022-11-02 17:17:54 +08:00
levi 1716d8b33e chore: update md 2022-11-02 17:02:45 +08:00
levi 855f0b4002 chore: update md 2022-11-02 17:01:58 +08:00
monkeyWie ee0d081b83 ci: fix linux desktop icon 2022-11-02 16:41:31 +00:00
monkeyWie d2f91a49ad ci: fix android name 2022-11-02 16:05:51 +00:00
monkeyWie fb6f889ddb ci: fix linux name 2022-11-02 15:53:16 +00:00
monkeyWie 0e6f1013cb ci: fix linux 2022-11-02 15:48:54 +00:00
monkeyWie fd10b18707 ci: fix 2022-11-02 15:42:27 +00:00
monkeyWie f423cf5b6d ci: fix linux version 2022-11-02 15:37:39 +00:00
monkeyWie 37b54b0c0f ci: fix linux build 2022-11-02 15:27:04 +00:00
monkeyWie cd74c895d3 ci: fix config 2022-11-02 15:22:38 +00:00
monkeyWie 450027a8b0 ci: fix linux build env 2022-11-02 15:21:04 +00:00
monkeyWie 48821a519e ci: build linux 2022-11-02 15:16:24 +00:00
levi 423ee5c3d1 chore: remove test code 2022-11-02 12:12:31 +08:00
levi 96c8945ac8 chore: update md 2022-11-02 12:05:22 +08:00
levi b2b714ae88 chore: change banner 2022-11-02 12:04:01 +08:00
levi 2793ff696e chore: refactor md 2022-11-02 12:02:58 +08:00
levi 2f81c5ef04 ci: fix web build 2022-11-02 12:02:19 +08:00
levi 2eed470d5c feat: icon 2022-11-02 10:26:22 +08:00
levi 34f8df1999 ci: fix web zip 2022-11-01 16:39:57 +08:00
levi e901e08534 fix: android lib 2022-11-01 16:30:15 +08:00
levi 56561ba764 ci: fix web build name 2022-11-01 16:05:57 +08:00
levi cf60e5a60a ci: fix web embed 2022-11-01 15:30:45 +08:00
levi bf8e24a6af ci: build web 2022-11-01 15:13:59 +08:00
levi ff4e6f448c ci: improve name 2022-11-01 13:38:37 +08:00
levi dccf080fbb fix: mac build 2022-11-01 10:33:17 +08:00
levi 9d7094133d chore: update md 2022-11-01 10:17:27 +08:00
levi ea6277805c ci: move to main branch 2022-11-01 10:15:09 +08:00
levi 3ad3060d71 ci: fix macos build 2022-11-01 10:07:23 +08:00
levi c1545adeb2 fix: andriod unix socket 2022-11-01 10:05:04 +08:00
levi 9659176752 ci: fix gomobile init 2022-10-31 15:36:58 +08:00
levi 4371345c7d ci: apk build 2022-10-31 15:33:54 +08:00
levi 655f9d99c7 chore: git ignore 2022-10-31 15:25:21 +08:00
levi a9c6705bd3 ci: add xgo cache 2022-10-28 14:55:25 +08:00
levi 46e0c6258f ci: upgrade go version to 1.19 2022-10-28 14:46:16 +08:00
levi 7c596c6b91 ci: fix 2022-10-28 14:40:40 +08:00
levi 5e92379aa2 ci: fix macos arm 2022-10-28 14:39:41 +08:00
levi 8a47b6cfb5 ci: macos arm 2022-10-28 14:26:31 +08:00
levi 97eca4ec9c ci: fix overwrite 2022-10-28 11:11:28 +08:00
levi b0a7a5569a feat: add logger 2022-10-28 10:57:39 +08:00
levi 5073ae6166 ci: fix macos asset path 2022-10-27 16:26:49 +08:00
levi 81c6fcf15d fix: ci node install cache 2022-10-27 16:16:19 +08:00
levi 2c6f821c4b chore: update md 2022-10-27 16:11:30 +08:00
levi 236423fe3c ci: fix 2022-10-27 16:07:59 +08:00
levi a8136afc31 ci: flutter build 2022-10-27 15:57:56 +08:00
levi 4d4d3b446a feat: support flutter web 2022-10-27 13:58:43 +08:00
levi c4a30dc4c2 feat: flutter ui integration 2022-10-27 13:58:43 +08:00
levi 0fc133e46d feat: bt improve 2022-10-26 17:18:13 +08:00
monkeyWie c6aa83194f feat: support RESTful api (#33) 2022-09-09 18:31:57 +08:00
monkeyWie e292926697 fix: http download resume task (#31) 2022-08-12 13:06:58 +08:00
liwei 8100bafdf9 ci: multi platform build 2022-08-11 17:31:22 +08:00
liwei e0582b5442 test: add torrent file resolve case 2022-08-11 16:02:31 +08:00
monkeyWie 034137322f feat: integration torrent (#30) 2022-08-11 14:38:16 +08:00
liwei 43378c7dc8 ci: fix upload asset name 2021-03-29 11:10:33 +08:00
liwei 30b7189d59 chore: update test status icon 2021-03-29 11:02:20 +08:00
liwei bbd553d910 chore: add release icon 2021-03-29 10:54:00 +08:00
liwei 3996574b95 ci: path ignore 2021-03-29 10:53:37 +08:00
liwei bd585309bf ci: release on master branch 2021-03-26 17:09:36 +08:00
liwei 4eda5a3e76 ci: fix 2021-03-26 16:27:04 +08:00
liwei d3c5dafd3e ci: auto release 2021-03-26 16:23:24 +08:00
liwei 936e193c20 docs: add example 2021-03-26 16:22:53 +08:00
liwei 01501ca857 refactor: remove main 2021-03-25 20:02:28 +08:00
liwei 4763ff129f test: remove bt 2021-03-25 20:00:50 +08:00
277 changed files with 13998 additions and 3259 deletions

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
<!--
请根据以下模版提issue
-->
### 问题描述(必要)
### 版本号(必要)
### 操作系统(必要)
### 相关截图
### 相关日志
+35
View File
@@ -0,0 +1,35 @@
name-template: 'v$NEXT_PATCH_VERSION 🌈'
tag-template: 'v$NEXT_PATCH_VERSION'
categories:
- title: '🆕 Features'
labels:
- 'feat'
- 'feature'
- 'enhancement'
- title: '🐛 Bug Fixes'
labels:
- 'fix'
- 'bugfix'
- 'bug'
- title: '🧰 Maintenance'
label: 'chore'
- title: '📖 Document'
label: 'doc'
- title: '🚀 CI/CD'
label: 'ci'
change-template: '- $TITLE @$AUTHOR (#$NUMBER)'
version-resolver:
major:
labels:
- 'major'
minor:
labels:
- 'minor'
patch:
labels:
- 'patch'
default: patch
template: |
## Changes
$CHANGES
+340 -39
View File
@@ -1,48 +1,349 @@
name: build name: build
on: on:
pull_request: workflow_dispatch:
push: release:
branches: types: [published]
- dev
jobs: jobs:
# lint: get-release:
# name: Lint
# runs-on: ubuntu-latest
# steps:
# - uses: actions/setup-go@v2
# with:
# go-version: '^1.15.2'
# - uses: actions/checkout@v2
# - name: Lint Go Code
# run: |
# export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14
# go get -u golang.org/x/lint/golint
# golint -set_exit_status ./...
test:
name: Test
runs-on: ubuntu-latest runs-on: ubuntu-latest
outputs:
tag_name: ${{ steps.get-release.outputs.tag_name }}
upload_url: ${{ steps.get-release.outputs.upload_url }}
steps: steps:
- uses: actions/setup-go@v2 - uses: actions/checkout@v3
with: - uses: thebritican/[email protected]
go-version: '^1.15.2' id: get-release
- uses: actions/checkout@v2 build-windows:
- name: Run Unit tests. runs-on: windows-latest
run: go test ./... -v -covermode=count -coverprofile=coverage.txt needs: [get-release]
- uses: codecov/codecov-action@v1
with:
file: ./coverage.txt
fail_ci_if_error: true
build:
name: Build
runs-on: ubuntu-latest
# needs: [lint, test]
needs: [test]
steps: steps:
- uses: actions/setup-go@v2 - uses: actions/checkout@v3
- uses: actions/setup-go@v3
with: with:
go-version: '^1.15.2' go-version: "^1.19"
- uses: actions/checkout@v2 cache: true
- uses: subosito/flutter-action@v2
- name: Build - name: Build
run: go build -ldflags="-s -w" -o bin/gopeed cmd/gopeed/gopeed.go env:
VERSION: ${{ needs.get-release.outputs.tag_name }}
run: |
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/monkeyWie/gopeed/bind/desktop
cd ui/flutter
flutter build windows
$mingw = "C:\ProgramData\Chocolatey\lib\mingw\tools\install\mingw64\bin"
$system = "C:\Windows\System32"
$release = "build\windows\runner\Release\"
cp $mingw\libstdc++-6.dll $release
cp $mingw\libgcc_s_seh-1.dll $release
cp $mingw\libwinpthread-1.dll $release
cp $system\msvcp140.dll $release
cp $system\vcruntime140.dll $release
cp $system\vcruntime140_1.dll $release
New-Item -Path build\windows\Output -ItemType Directory
Compress-Archive -Path "$release*" -DestinationPath build\windows\Output\Gopeed-$env:VERSION-windows-amd64.zip
cd build/windows
echo @"
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
#define MyAppName "Gopeed"
#define MyAppVersion "$($env:VERSION.substring(1))"
#define MyAppPublisher "monkeyWie"
#define MyAppURL "https://gopeed.com"
#define MyAppExeName "gopeed.exe"
[Setup]
; NOTE: The value of AppId uniquely identifies this application. Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{5960F34D-1E42-402C-8C85-DE2FF24CBAE4}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\gopeed
DisableProgramGroupPage=yes
LicenseFile=..\..\..\..\LICENSE
; Remove the following line to run in administrative install mode (install for all users.)
PrivilegesRequired=lowest
OutputBaseFilename=gopeed
SetupIconFile=..\..\assets\icon\icon.ico
Compression=lzma
SolidCompression=yes
WizardStyle=modern
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
[Files]
Source: ".\runner\Release\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon
[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
"@ > setup.iss
iscc.exe setup.iss
mv "Output\gopeed.exe" "Output\Gopeed-$env:VERSION-windows-amd64-install.exe"
- name: Upload
uses: shogo82148/actions-upload-release-asset@v1
with:
upload_url: ${{ needs.get-release.outputs.upload_url }}
asset_path: ui/flutter/build/windows/Output/*
overwrite: true
build-macos-arm64-lib:
runs-on: ubuntu-latest
needs: [get-release]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "^1.19"
cache: true
- run: mkdir -p ~/image-cache
- id: image-cache
uses: actions/cache@v3
with:
path: ~/image-cache
key: image-cache-xgo-v1.19
- if: steps.image-cache.outputs.cache-hit != 'true'
run: |
docker pull techknowlogick/xgo:go-1.19.x
docker save -o ~/image-cache/xgo.tar techknowlogick/xgo:go-1.19.x
- if: steps.image-cache.outputs.cache-hit == 'true'
run: docker load -i ~/image-cache/xgo.tar
- name: Build
run: |
go install src.techknowlogick.com/xgo@latest
xgo --targets=darwin/arm64 -tags="nosqlite" -ldflags="-w -s" -buildmode=c-shared -pkg=bind/desktop -out=libgopeed .
mv libgopeed-*.dylib libgopeed.dylib
- uses: actions/upload-artifact@v3
with:
name: macos-arm64-lib
path: libgopeed.dylib
build-macos:
runs-on: macos-latest
needs: [get-release, build-macos-arm64-lib]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "^1.19"
cache: true
- uses: actions/setup-node@v3
with:
node-version: 16
- uses: subosito/flutter-action@v2
- name: Install appdmg
run: npm install -g appdmg
- uses: actions/download-artifact@v3
with:
name: macos-arm64-lib
path: ui/flutter/macos/Frameworks
- name: Build
env:
VERSION: ${{ needs.get-release.outputs.tag_name }}
run: |
PROJECT_DIR=$(pwd)
cd ui/flutter
flutter build macos
cd build/macos/Build/Products/Release
cat>appdmg.json<<EOF
{
"title": "Gopeed",
"icon": "Gopeed.app/Contents/Resources/AppIcon.icns",
"contents": [
{ "x": 448, "y": 344, "type": "link", "path": "/Applications" },
{ "x": 192, "y": 344, "type": "file", "path": "Gopeed.app" }
]
}
EOF
mkdir dist
appdmg appdmg.json dist/Gopeed-$VERSION-macos-arm64.dmg
# amd64
cd $PROJECT_DIR
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o bin/libgopeed.dylib github.com/monkeyWie/gopeed/bind/desktop
cp bin/libgopeed.dylib ui/flutter/build/macos/Build/Products/Release/Gopeed.app/Contents/Frameworks/
cd ui/flutter/build/macos/Build/Products/Release
appdmg appdmg.json dist/Gopeed-$VERSION-macos-amd64.dmg
- name: Upload
uses: shogo82148/actions-upload-release-asset@v1
with:
upload_url: ${{ needs.get-release.outputs.upload_url }}
asset_path: ui/flutter/build/macos/Build/Products/Release/dist/*
overwrite: true
build-linux:
runs-on: ubuntu-latest
needs: [get-release]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "^1.19"
cache: true
- uses: subosito/flutter-action@v2
- run: |
sudo apt-get update -y
sudo apt-get install -y ninja-build libgtk-3-dev
- name: Build
env:
VERSION: ${{ needs.get-release.outputs.tag_name }}
run: |
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/monkeyWie/gopeed/bind/desktop
cd ui/flutter
flutter build linux
mkdir -p debian/gui
cp assets/icon/icon_1024.png debian/gui/gopeed.png
cat>debian/debian.yaml<<EOF
flutter_app:
command: gopeed
arch: x64
parent: /usr/local/lib
control:
Package: gopeed
Version: ${VERSION:1}
Architecture: amd64
Essential: no
Priority: optional
Depends:
Maintainer: monkeyWie
Description: High speed downloader that supports all platforms.
EOF
cat>debian/gui/gopeed.desktop<<EOF
[Desktop Entry]
Version=${VERSION:1}
Name=Gopeed
GenericName=Gopeed
Comment=High speed downloader that supports all platforms.
Terminal=false
Type=Application
Categories=Utility
Keywords=Flutter;Downloader;
EOF
dart pub global activate flutter_to_debian
flutter_to_debian
mv debian/packages/gopeed*.deb debian/packages/Gopeed-$VERSION-linux-amd64.deb
- name: Upload
uses: shogo82148/actions-upload-release-asset@v1
with:
upload_url: ${{ needs.get-release.outputs.upload_url }}
asset_path: ui/flutter/debian/packages/*
overwrite: true
build-android:
runs-on: ubuntu-latest
needs: [get-release]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "^1.19"
cache: true
- uses: actions/setup-java@v3
with:
distribution: "zulu"
java-version: "11"
- uses: subosito/flutter-action@v2
- name: Build
env:
VERSION: ${{ needs.get-release.outputs.tag_name }}
run: |
mkdir ui/flutter/android/app/libs
go install golang.org/x/mobile/cmd/gomobile@latest
gomobile init
gomobile bind -tags nosqlite -ldflags="-w -s" -o ui/flutter/android/app/libs/libgopeed.aar -target=android -androidapi 19 -javapkg=com.gopeed github.com/monkeyWie/gopeed/bind/mobile
cd ui/flutter
echo $APK_KEYSTORE | base64 -d > android/app/upload-keystore.jks
flutter build apk
mkdir dist
cp build/app/outputs/flutter-apk/app-release.apk dist/Gopeed-$VERSION-android.apk
- name: Upload
uses: shogo82148/actions-upload-release-asset@v1
with:
upload_url: ${{ needs.get-release.outputs.upload_url }}
asset_path: ui/flutter/dist/*
overwrite: true
build-web:
runs-on: ubuntu-latest
needs: [get-release]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "^1.19"
cache: true
- uses: subosito/flutter-action@v2
- name: Build
env:
VERSION: ${{ needs.get-release.outputs.tag_name }}
run: |
cd ui/flutter
flutter build web
cd ../../
cp -r ui/flutter/build/web cmd/web/dist
mkdir -p dist/zip
goos_arr=(windows darwin linux)
goarch_arr=(386 amd64 arm64)
export CGO_ENABLED=0
for goos in "${goos_arr[@]}"; do
for goarch in "${goarch_arr[@]}"; do
name=gopeed-web-$VERSION-$goos-$goarch
dir="dist/$name/"
(GOOS=$goos GOARCH=$goarch go build -tags nosqlite,web -ldflags="-s -w" -o $dir github.com/monkeyWie/gopeed/cmd/web \
&& cd $dir \
&& file=$(ls -AU | head -1) \
&& mkdir $name \
&& mv $file $name/$(echo $file | sed -e "s/web/gopeed/g") \
&& zip -r ../zip/$name.zip * \
&& cd ../..) \
|| true
done
done
- name: Upload
uses: shogo82148/actions-upload-release-asset@v1
with:
upload_url: ${{ needs.get-release.outputs.upload_url }}
asset_path: dist/zip/*
overwrite: true
build-docker:
runs-on: ubuntu-latest
needs: [get-release]
steps:
- uses: actions/checkout@v3
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: liwei2633
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push
uses: docker/build-push-action@v2
env:
VERSION: ${{ needs.get-release.outputs.tag_name }}
with:
push: true
platforms: |
linux/386
linux/amd64
linux/arm64
tags: |
liwei2633/gopeed:latest
liwei2633/gopeed:${{ env.VERSION }}
+34
View File
@@ -0,0 +1,34 @@
name: release
on:
push:
branches:
- main
paths:
- "bind/**"
- "cmd/**"
- "internal/**"
- "pkg/**"
- "ui/**"
- ".github/workflows/release.yml"
- ".github/release-drafter.yml"
- "go.mod"
- "go.sum"
- "Dockerfile"
jobs:
release:
runs-on: ubuntu-latest
outputs:
tag_name: ${{ steps.create_release.outputs.tag_name }}
upload_url: ${{ steps.create_release.outputs.upload_url }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "^1.19"
- uses: release-drafter/release-drafter@v5
id: create_release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+70
View File
@@ -0,0 +1,70 @@
name: test
on:
pull_request:
branches:
- main
paths:
- "bind/**"
- "cmd/**"
- "internal/**"
- "pkg/**"
- "ui/**"
- ".github/workflows/test.yml"
- "go.mod"
- "go.sum"
push:
branches:
- main
paths:
- "bind/**"
- "cmd/**"
- "internal/**"
- "pkg/**"
- "ui/**"
- ".github/workflows/test.yml"
- "go.mod"
- "go.sum"
jobs:
# lint:
# name: Lint
# runs-on: ubuntu-latest
# steps:
# - uses: actions/setup-go@v2
# with:
# go-version: '^1.19'
# - uses: actions/checkout@v2
# - name: Lint Go Code
# run: |
# export PATH=$PATH:$(go env GOPATH)/bin # temporary fix. See https://github.com/actions/setup-go/issues/14
# go get -u golang.org/x/lint/golint
# golint -set_exit_status ./...
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "^1.19"
cache: true
- name: Run Unit tests.
run: go test $(go list ./... | grep -v /cmd/ | grep -v /bind/ ) -v -covermode=count -coverprofile=coverage.txt
- uses: codecov/codecov-action@v3
with:
files: ./coverage.txt
fail_ci_if_error: true
build:
name: Build
runs-on: ubuntu-latest
# needs: [lint, test]
needs: [test]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: "^1.19"
cache: true
- name: Build
run: go build github.com/monkeyWie/gopeed/cmd/gopeed
+13 -2
View File
@@ -11,6 +11,17 @@
# Output of the go coverage tool, specifically when used with LiteIDE # Output of the go coverage tool, specifically when used with LiteIDE
*.out *.out
debug
.idea/ .idea/
bin/
.torrent.db
.torrent.db-shm
.torrent.db-wal
*.data
*.db
.DS_Store
dist/
+21
View File
@@ -0,0 +1,21 @@
FROM cirrusci/flutter:3.3.7 AS flutter
WORKDIR /app
COPY ./ui/flutter/pubspec.yaml ./ui/flutter/pubspec.lock ./
RUN flutter pub get
COPY ./ui/flutter ./
RUN flutter build web
FROM golang:1.19.3 AS go
WORKDIR /app
COPY ./go.mod ./go.sum ./
RUN go mod download
COPY . .
COPY --from=flutter /app/build/web ./cmd/web/dist
RUN CGO_ENABLED=0 go build -tags nosqlite,web -ldflags="-s -w" -o dist/gopeed github.com/monkeyWie/gopeed/cmd/web
FROM alpine:3.14.2
LABEL maintainer="monkeyWie"
WORKDIR /app
COPY --from=go /app/dist/gopeed ./
EXPOSE 9999
ENTRYPOINT ["./gopeed"]
+674
View File
File diff suppressed because it is too large. Load diff
+149 -25
View File
@@ -1,38 +1,162 @@
[![Build Status](https://github.com/monkeyWie/gopeed-core/workflows/build/badge.svg)](https://github.com/monkeyWie/gopeed-core/actions?query=workflow%3Abuild) [![Test Status](https://github.com/monkeyWie/gopeed/workflows/test/badge.svg)](https://github.com/monkeyWie/gopeed/actions?query=workflow%3Atest)
[![Codecov](https://codecov.io/gh/monkeyWie/gopeed-core/branch/dev/graph/badge.svg)](https://codecov.io/gh/monkeyWie/gopeed-core) [![Codecov](https://codecov.io/gh/monkeyWie/gopeed/branch/main/graph/badge.svg)](https://codecov.io/gh/monkeyWie/gopeed)
[![Release](https://img.shields.io/github/release/monkeyWie/gopeed.svg?style=flat-square)](https://github.com/monkeyWie/gopeed/releases)
[![Discord](https://img.shields.io/discord/1037992631881449472?label=Discord&logo=discord&style=social)](https://discord.gg/ZUJqJrwCGB)
## gopeed ![](_docs/img/banner.png)
支持多协议(HTTP、BitTorrent)下载的客户端,提供命令行、RESTful API、WebSocket、Go 类库方式来使用。 [English](/README.md) | [中文](/README_zh-CN.md)
## 安装 ## Introduction
```sh Gopeed is a high-speed downloader developed by `Golang`+`Flutter`, which supports (HTTP, BitTorrent, Magnet) protocol downloads and supports all platforms.
go install github.com/monkeyWie/gopeed-core
## Installation
**Supported platforms**
- [x] windows
- [x] macos
- [x] linux
- [x] android
- [ ] ios
- [x] web
- [x] docker
[To Release](https://github.com/monkeyWie/gopeed/releases/latest)
> Tips: If the macos open failed, please execute the `xattr -d com.apple.quarantine /Applications/Gopeed.app` command in the terminal
### Command tool
use `go install`:
```bash
go install github.com/monkeyWie/gopeed/cmd/gopeed
``` ```
## 示例 ### Docker
```go #### Start directly
```bash
docker run -d -p 9999:9999 -v /path/to/download:/download liwei2633/gopeed
``` ```
## TODO #### Using Docker Compose
- [x] HTTP 下载实现 ```yaml
- [ ] BitTorrent 下载实现 version: '3'
- [x] .torrent 文件解析
- [x] tracker 协议实现
- [x] peer wire protocol 协议实现
- [ ] DHT 协议实现
- [ ] 磁力链接支持
- [ ] uTP 协议实现
- [x] 下载接口抽象(不关心具体协议)
- [ ] 支持自定义配置
- [ ] 限速功能实现
- [x] 命令行工具提供
- [ ] RESTful 服务提供
## 参与 services:
gopeed:
container_name: gopeed
ports:
- 9999:9999
image: liwei2633/gopeed
volumes:
- ~/Downloads:/download
restart: unless-stopped
```
由于项目目前还未定型,代码可能随时有大的调整,所以暂不接受 PR,当然如果有什么好的想法可以在 issue 区提出来。 Use the command below to start container:
```bash
docker-compose up -d
```
#### Access Gopeed
When the docker container is running, you can access the web page through `http://localhost:9999`.
> Tip: Modify the download path to `/download` on the setting page to access the downloaded files on the host.
## Showcase
![](_docs/img/ui-demo.png)
## 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.
> The front code is located in the `ui/flutter` directory.
### Environment
1. Golang 1.19+
2. Flutter 3.0+
### Clone
```bash
git clone [email protected]:monkeyWie/gopeed.git
```
### Build
#### Desktop
First, you need to configure the environment according to the [flutter desktop official website document](https://docs.flutter.dev/development/platform-integration/desktop), and then you need to prepare the `cgo` environment, which can be searched for yourself.
command:
- windows
```bash
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/monkeyWie/gopeed/bind/desktop
cd ui/flutter
flutter build windows
```
- macos
```bash
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/macos/Frameworks/libgopeed.dylib github.com/monkeyWie/gopeed/bind/desktop
cd ui/flutter
flutter build macos
```
- linux
```bash
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/monkeyWie/gopeed/bind/desktop
cd ui/flutter
flutter build linux
```
#### Mobile
Same as before, you also need to prepare the `cgo` environment, and then install `gomobile`:
```bash
go install golang.org/x/mobile/cmd/gomobile@latest
gomobile init
```
command:
- android
```bash
gomobile bind -tags nosqlite -ldflags="-w -s" -o ui/flutter/android/app/libs/libgopeed.aar -target=android -androidapi 19 -javapkg=com.gopeed github.com/monkeyWie/gopeed/bind/mobile
cd ui/flutter
flutter build apk
```
#### Web
Web platform communicates directly with the backend http server, no additional environment is required.
command:
```bash
cd ui/flutter
flutter build web
cd ../../
rm -rf cmd/web/dist
cp -r ui/flutter/build/web cmd/web/dist
go build -tags nosqlite,web -ldflags="-s -w" -o bin/ github.com/monkeyWie/gopeed/cmd/web
````
## License
[GPLv3](LICENSE)
+162
View File
@@ -0,0 +1,162 @@
[![Test Status](https://github.com/monkeyWie/gopeed/workflows/test/badge.svg)](https://github.com/monkeyWie/gopeed/actions?query=workflow%3Atest)
[![Codecov](https://codecov.io/gh/monkeyWie/gopeed/branch/main/graph/badge.svg)](https://codecov.io/gh/monkeyWie/gopeed)
[![Release](https://img.shields.io/github/release/monkeyWie/gopeed.svg?style=flat-square)](https://github.com/monkeyWie/gopeed/releases)
[![Discord](https://img.shields.io/discord/1037992631881449472?label=Discord&logo=discord&style=social)](https://discord.gg/ZUJqJrwCGB)
![](_docs/img/banner.png)
[English](/README.md) | [中文](/README_zh-CN.md)
## 介绍
Gopeed 是一款由`Golang`+`Flutter`开发的高速下载器,支持(HTTP、BitTorrent、Magnet)协议下载,并且支持全平台使用。
## 下载
**已支持平台**
- [x] windows
- [x] macos
- [x] linux
- [x] android
- [ ] ios
- [x] web
- [x] docker
[点击前往](https://github.com/monkeyWie/gopeed/releases/latest)
> 注:macos 版本运行如果提示损坏,请在终端执行 `xattr -d com.apple.quarantine /Applications/Gopeed.app` 命令
### 命令行工具
使用`go install`安装:
```bash
go install github.com/monkeyWie/gopeed/cmd/gopeed
```
### Docker
#### 直接运行
```bash
docker run -d -p 9999:9999 -v /path/to/download:/download liwei2633/gopeed
```
#### 使用 Docker Compose
```yaml
version: '3'
services:
gopeed:
container_name: gopeed
ports:
- 9999:9999
image: liwei2633/gopeed
volumes:
- ~/Downloads:/download
restart: unless-stopped
```
输入以下指令启动容器:
```bash
docker-compose up -d
```
#### 访问服务
当 docker 容器运行时,可以通过 `http://localhost:9999` 访问 web 页面。
> 提示:在设置页面把下载路径修改为 `/download` 以便在宿主机访问下载完的文件。
## 界面展示
![](_docs/img/ui-demo.png)
## 开发
本项目分为前端和后端两个部分,前端使用`flutter`,后端使用`Golang`,两边通过`http`协议进行通讯,在 unix 系统下,使用的是`unix socket`,在 windows 系统下,使用的是`tcp`协议。
> 前端代码位于`ui/flutter`目录下。
### 环境要求
1. Golang 1.19+
2. Flutter 3.0+
### 克隆项目
```bash
git clone [email protected]:monkeyWie/gopeed.git
```
### 编译
#### 桌面端
首先需要按照[flutter desktop 官网文档](https://docs.flutter.dev/development/platform-integration/desktop)进行环境配置,然后需要准备好`cgo`环境,具体可以自行搜索。
构建命令:
- windows
```bash
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/windows/libgopeed.dll github.com/monkeyWie/gopeed/bind/desktop
cd ui/flutter
flutter build windows
```
- macos
```bash
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/macos/Frameworks/libgopeed.dylib github.com/monkeyWie/gopeed/bind/desktop
cd ui/flutter
flutter build macos
```
- linux
```bash
go build -tags nosqlite -ldflags="-w -s" -buildmode=c-shared -o ui/flutter/linux/bundle/lib/libgopeed.so github.com/monkeyWie/gopeed/bind/desktop
cd ui/flutter
flutter build linux
```
#### 移动端
同样的也是需要准备好`cgo`环境,接着安装`gomobile`:
```bash
go install golang.org/x/mobile/cmd/gomobile@latest
gomobile init
```
构建命令:
- android
```bash
gomobile bind -tags nosqlite -ldflags="-w -s" -o ui/flutter/android/app/libs/libgopeed.aar -target=android -androidapi 19 -javapkg=com.gopeed github.com/monkeyWie/gopeed/bind/mobile
cd ui/flutter
flutter build apk
```
#### Web 端(推荐本地调试使用)
Web 端直接与后端 http 服务通讯,不需要额外准备环境。
构建命令:
```bash
cd ui/flutter
flutter build web
cd ../../
rm -rf cmd/web/dist
cp -r ui/flutter/build/web cmd/web/dist
go build -tags nosqlite,web -ldflags="-s -w" -o bin/ github.com/monkeyWie/gopeed/cmd/web
```
## 开源许可
基于 [GPLv3](LICENSE) 协议开源。
Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

+30
View File
@@ -0,0 +1,30 @@
package main
import (
"fmt"
"github.com/monkeyWie/gopeed/pkg/base"
"github.com/monkeyWie/gopeed/pkg/download"
)
func main() {
finallyCh := make(chan error)
_, err := download.Boot().
URL("https://www.baidu.com/index.html").
Listener(func(event *download.Event) {
if event.Key == download.EventKeyFinally {
finallyCh <- event.Err
}
}).
Create(&base.Options{
Connections: 8,
})
if err != nil {
panic(err)
}
err = <-finallyCh
if err != nil {
fmt.Printf("download fail:%v\n", err)
} else {
fmt.Println("download success")
}
}
+28
View File
@@ -0,0 +1,28 @@
package main
import "C"
import (
"encoding/json"
"github.com/monkeyWie/gopeed/pkg/rest"
"github.com/monkeyWie/gopeed/pkg/rest/model"
)
func main() {}
//export Start
func Start(cfg *C.char) (int, *C.char) {
var config model.StartConfig
if err := json.Unmarshal([]byte(C.GoString(cfg)), &config); err != nil {
return 0, C.CString(err.Error())
}
realPort, err := rest.Start(&config)
if err != nil {
return 0, C.CString(err.Error())
}
return realPort, nil
}
//export Stop
func Stop() {
rest.Stop()
}
+21
View File
@@ -0,0 +1,21 @@
package libgopeed
// #cgo LDFLAGS: -static-libstdc++
import "C"
import (
"encoding/json"
"github.com/monkeyWie/gopeed/pkg/rest"
"github.com/monkeyWie/gopeed/pkg/rest/model"
)
func Start(cfg string) (int, error) {
var config model.StartConfig
if err := json.Unmarshal([]byte(cfg), &config); err != nil {
return 0, err
}
return rest.Start(&config)
}
func Stop() {
rest.Stop()
}
+7
View File
@@ -0,0 +1,7 @@
_______ ______ .______ _______ _______ _______
/ _____| / __ \ | _ \ | ____|| ____|| \
| | __ | | | | | |_) | | |__ | |__ | .--. |
| | |_ | | | | | | ___/ | __| | __| | | | |
| |__| | | `--' | | | | |____ | |____ | '--' |
\______| \______/ | _| |_______||_______||_______/
+29
View File
@@ -0,0 +1,29 @@
package main
import (
_ "embed"
"fmt"
"github.com/monkeyWie/gopeed/pkg/rest"
"github.com/monkeyWie/gopeed/pkg/rest/model"
"net/http"
)
//go:embed banner.txt
var banner string
func main() {
cfg := &model.StartConfig{
Network: "tcp",
Address: "127.0.0.1:9999",
Storage: model.StorageBolt,
}
fmt.Println(banner)
srv, listener, err := rest.BuildServer(cfg)
if err != nil {
panic(err)
}
fmt.Printf("Server start success on http://%s\n", listener.Addr().String())
if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {
panic(err)
}
}
+3 -4
View File
@@ -4,7 +4,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"path/filepath"
) )
type args struct { type args struct {
@@ -14,19 +13,19 @@ type args struct {
} }
func parse() *args { func parse() *args {
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) dir, err := os.Getwd()
if err != nil { if err != nil {
panic(err) panic(err)
} }
var args args var args args
args.connections = flag.Int("C", 16, "Concurrent connections.") args.connections = flag.Int("C", 16, "Concurrent connections.")
args.dir = flag.String("D", dir, "Save directory.") args.dir = flag.String("D", dir, "Store directory.")
flag.Parse() flag.Parse()
t := flag.Args() t := flag.Args()
if len(t) > 0 { if len(t) > 0 {
args.url = t[0] args.url = t[0]
} else { } else {
gPrintln("missing url parameter") gPrintln("missing url parameter, for example: gopeed https://www.google.com or gopeed bt.torrent or gopeed magnet:?xt=urn:btih:...")
gPrintln("try 'gopeed -h' for more information") gPrintln("try 'gopeed -h' for more information")
os.Exit(1) os.Exit(1)
} }
-64
View File
@@ -1,64 +0,0 @@
package main
import (
"fmt"
"github.com/monkeyWie/gopeed-core/internal/protocol/http"
"github.com/monkeyWie/gopeed-core/pkg/base"
"github.com/monkeyWie/gopeed-core/pkg/download"
"github.com/monkeyWie/gopeed-core/pkg/util"
"path/filepath"
"sync"
)
const progressWidth = 20
func main() {
args := parse()
downloader := download.NewDownloader(http.FetcherBuilder)
res, err := downloader.Resolve(&base.Request{
URL: args.url,
})
if err != nil {
panic(err)
}
err = downloader.Create(res, &base.Options{
Path: *args.dir,
Connections: *args.connections,
})
if err != nil {
panic(err)
}
var wg sync.WaitGroup
wg.Add(1)
downloader.Listener(func(taskInfo *download.TaskInfo, eventKey download.EventKey) {
if eventKey == download.EventKeyProgress {
printProgress(taskInfo, "downloading...")
}
if eventKey == download.EventKeyDone {
printProgress(taskInfo, "complete")
wg.Done()
}
})
wg.Wait()
fmt.Println()
gPrint("saving file " + filepath.Join(*args.dir, res.Files[0].Name))
}
func printProgress(taskInfo *download.TaskInfo, title string) {
rate := float64(taskInfo.Progress.Downloaded) / float64(taskInfo.Res.TotalSize)
completeWidth := int(progressWidth * rate)
speed := util.ByteFmt(taskInfo.Progress.Speed)
totalSize := util.ByteFmt(taskInfo.Res.TotalSize)
fmt.Printf("\r%s [", title)
for i := 0; i < progressWidth; i++ {
if i < completeWidth {
fmt.Print("■")
} else {
fmt.Print("□")
}
}
fmt.Printf("] %.1f%% %s/s %s", rate*100, speed, totalSize)
}
+81
View File
@@ -0,0 +1,81 @@
package main
import (
"fmt"
"github.com/monkeyWie/gopeed/pkg/base"
"github.com/monkeyWie/gopeed/pkg/download"
"github.com/monkeyWie/gopeed/pkg/protocol/http"
"github.com/monkeyWie/gopeed/pkg/util"
"strings"
"sync"
)
const progressWidth = 20
func main() {
args := parse()
var wg sync.WaitGroup
_, err := download.Boot().
URL(args.url).
Listener(func(event *download.Event) {
if event.Key == download.EventKeyProgress {
printProgress(event.Task, "downloading...")
}
if event.Key == download.EventKeyFinally {
var title string
if event.Err != nil {
title = "fail"
} else {
title = "complete"
}
printProgress(event.Task, title)
fmt.Println()
if event.Err != nil {
fmt.Printf("reason: %s", event.Err.Error())
} else {
fmt.Printf("saving path: %s", *args.dir)
}
wg.Done()
}
}).
Create(&base.Options{
Path: *args.dir,
Extra: http.OptsExtra{Connections: *args.connections},
})
if err != nil {
panic(err)
}
wg.Add(1)
wg.Wait()
}
var (
lastLineLen = 0
sb = new(strings.Builder)
)
func printProgress(task *download.Task, title string) {
rate := float64(task.Progress.Downloaded) / float64(task.Res.Size)
completeWidth := int(progressWidth * rate)
speed := util.ByteFmt(task.Progress.Speed)
totalSize := util.ByteFmt(task.Res.Size)
sb.WriteString(fmt.Sprintf("\r%s [", title))
for i := 0; i < progressWidth; i++ {
if i < completeWidth {
sb.WriteString("■")
} else {
sb.WriteString("□")
}
}
sb.WriteString(fmt.Sprintf("] %.1f%% %s/s %s", rate*100, speed, totalSize))
if lastLineLen != 0 {
paddingLen := lastLineLen - sb.Len()
if paddingLen > 0 {
sb.WriteString(strings.Repeat(" ", paddingLen))
}
}
lastLineLen = sb.Len()
fmt.Print(sb.String())
sb.Reset()
}
+7
View File
@@ -0,0 +1,7 @@
_______ ______ .______ _______ _______ _______
/ _____| / __ \ | _ \ | ____|| ____|| \
| | __ | | | | | |_) | | |__ | |__ | .--. |
| | |_ | | | | | | ___/ | __| | __| | | | |
| |__| | | `--' | | | | |____ | |____ | '--' |
\______| \______/ | _| |_______||_______||_______/
+42
View File
@@ -0,0 +1,42 @@
//go:build web
// +build web
package main
import (
"embed"
"fmt"
"github.com/monkeyWie/gopeed/pkg/rest"
"github.com/monkeyWie/gopeed/pkg/rest/model"
"io/fs"
"net/http"
)
//go:embed banner.txt
var banner string
//go:embed dist/*
var dist embed.FS
func main() {
sub, err := fs.Sub(dist, "dist")
if err != nil {
panic(err)
}
cfg := &model.StartConfig{
Network: "tcp",
Address: "0.0.0.0:9999",
Storage: model.StorageBolt,
WebEnable: true,
WebFS: sub,
}
fmt.Println(banner)
srv, listener, err := rest.BuildServer(cfg)
if err != nil {
panic(err)
}
fmt.Printf("Server start success on http://%s\n", listener.Addr().String())
if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {
panic(err)
}
}
+11
View File
@@ -0,0 +1,11 @@
version: '3'
services:
gopeed:
container_name: gopeed
ports:
- 9999:9999 # HTTP port (host:container)
image: liwei2633/gopeed
volumes:
- ~/Downloads:/download # replace with your prefer path
restart: unless-stopped
+72 -13
View File
@@ -1,17 +1,76 @@
module github.com/monkeyWie/gopeed-core module github.com/monkeyWie/gopeed
go 1.13 go 1.19
require ( require (
github.com/RoaringBitmap/roaring v0.4.21 github.com/anacrolix/torrent v1.46.0
github.com/cenkalti/mse v0.0.0-20140930130441-6ef65f170972 github.com/gorilla/mux v1.8.0
github.com/gizak/termui/v3 v3.1.0 github.com/matoous/go-nanoid/v2 v2.0.0
github.com/google/uuid v1.1.1 go.etcd.io/bbolt v1.3.6
github.com/marksamman/bencode v0.0.0-20150821143521-dc84f26e086e golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde
github.com/mattn/go-tty v0.0.3 // indirect )
github.com/sirupsen/logrus v1.4.2
github.com/superhawk610/bar v0.0.2 require (
github.com/superhawk610/terminal v0.1.0 // indirect crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 // indirect
golang.org/x/net v0.0.0-20200707034311-ab3426394381 github.com/RoaringBitmap/roaring v1.0.1-0.20220510143707-3f418c4f42a4 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c github.com/ajwerner/btree v0.0.0-20211221152037-f427b3e689c0 // indirect
github.com/anacrolix/chansync v0.3.0 // indirect
github.com/anacrolix/dht/v2 v2.16.2-0.20220311024416-dd658f18fd51 // indirect
github.com/anacrolix/envpprof v1.2.1 // indirect
github.com/anacrolix/generics v0.0.0-20220618083756-f99e35403a60 // indirect
github.com/anacrolix/go-libutp v1.2.0 // indirect
github.com/anacrolix/log v0.13.1 // indirect
github.com/anacrolix/missinggo v1.3.0 // indirect
github.com/anacrolix/missinggo/perf v1.0.0 // indirect
github.com/anacrolix/missinggo/v2 v2.7.0 // indirect
github.com/anacrolix/mmsg v1.0.0 // indirect
github.com/anacrolix/multiless v0.2.1-0.20211218050420-533661eef5dc // indirect
github.com/anacrolix/stm v0.3.0 // indirect
github.com/anacrolix/sync v0.4.0 // indirect
github.com/anacrolix/upnp v0.1.3-0.20220123035249-922794e51c96 // indirect
github.com/anacrolix/utp v0.1.0 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/benbjohnson/immutable v0.3.0 // indirect
github.com/bits-and-blooms/bitset v1.2.0 // indirect
github.com/bradfitz/iter v0.0.0-20191230175014-e8f45d346db8 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/lispad/go-generics-tools v1.0.0 // indirect
github.com/mschoch/smat v0.2.0 // indirect
github.com/pion/datachannel v1.5.2 // indirect
github.com/pion/dtls/v2 v2.1.2 // indirect
github.com/pion/ice/v2 v2.1.20 // indirect
github.com/pion/interceptor v0.1.7 // indirect
github.com/pion/logging v0.2.2 // indirect
github.com/pion/mdns v0.0.5 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pion/rtcp v1.2.9 // indirect
github.com/pion/rtp v1.7.4 // indirect
github.com/pion/sctp v1.8.2 // indirect
github.com/pion/sdp/v3 v3.0.4 // indirect
github.com/pion/srtp/v2 v2.0.5 // indirect
github.com/pion/stun v0.3.5 // indirect
github.com/pion/transport v0.13.0 // indirect
github.com/pion/turn/v2 v2.0.6 // indirect
github.com/pion/udp v0.1.1 // indirect
github.com/pion/webrtc/v3 v3.1.24-0.20220208053747-94262c1b2b38 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rs/dnscache v0.0.0-20210201191234-295bba877686 // indirect
github.com/tidwall/btree v1.3.1 // indirect
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 // indirect
golang.org/x/exp v0.0.0-20220428152302-39d4317da171 // indirect
golang.org/x/mobile v0.0.0-20220928052126-fa6bcb076835 // indirect
golang.org/x/mod v0.6.0-dev.0.20211013180041-c96bc1413d57 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
golang.org/x/tools v0.1.8-0.20211029000441-d6a9af8af023 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
) )
+437 -50
View File
File diff suppressed because it is too large. Load diff
+27 -36
View File
@@ -1,61 +1,52 @@
package controller package controller
import ( import (
"golang.org/x/net/proxy" "errors"
"net"
"os" "os"
"time" "path/filepath"
) )
type Controller interface { type Controller struct {
GetConfig func(v any) (bool, error)
FileController
//ContextDialer() (proxy.Dialer, error)
}
type FileController interface {
Touch(name string, size int64) (file *os.File, err error) Touch(name string, size int64) (file *os.File, err error)
Open(name string) (file *os.File, err error)
Write(name string, offset int64, buf []byte) (int, error)
Close(name string) error
ContextDialer() (proxy.Dialer, error)
} }
type DefaultController struct { type DefaultFileController struct {
Files map[string]*os.File
} }
func NewController() *DefaultController { func NewController() *Controller {
return &DefaultController{Files: make(map[string]*os.File)} return &Controller{
FileController: &DefaultFileController{},
GetConfig: func(v any) (bool, error) {
return false, nil
},
}
} }
func (c *DefaultController) Touch(name string, size int64) (file *os.File, err error) { func (c *DefaultFileController) Touch(name string, size int64) (file *os.File, err error) {
dir := filepath.Dir(name)
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
return
}
file, err = os.Create(name) file, err = os.Create(name)
if err != nil && !errors.Is(err, os.ErrExist) {
return
}
if size > 0 { if size > 0 {
err = os.Truncate(name, size) err = os.Truncate(name, size)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
if err == nil {
c.Files[name] = file
}
return return
} }
func (c *DefaultController) Open(name string) (file *os.File, err error) { /*func (c *DefaultController) ContextDialer() (proxy.Dialer, error) {
file, err = os.OpenFile(name, os.O_RDWR, os.ModePerm)
if err == nil {
c.Files[name] = file
}
return
}
func (c *DefaultController) Write(name string, offset int64, buf []byte) (int, error) {
return c.Files[name].WriteAt(buf, offset)
}
func (c *DefaultController) Close(name string) error {
err := c.Files[name].Close()
delete(c.Files, name)
return err
}
func (c *DefaultController) ContextDialer() (proxy.Dialer, error) {
// return proxy.SOCKS5("tpc", "127.0.0.1:9999", nil, nil) // return proxy.SOCKS5("tpc", "127.0.0.1:9999", nil, nil)
var dialer proxy.Dialer var dialer proxy.Dialer
return &DialerWarp{dialer: dialer}, nil return &DialerWarp{dialer: dialer}, nil
@@ -107,4 +98,4 @@ func (d *DialerWarp) Dial(network, addr string) (c net.Conn, err error) {
return nil, err return nil, err
} }
return &ConnWarp{conn: conn}, nil return &ConnWarp{conn: conn}, nil
} }*/
+30 -12
View File
@@ -1,45 +1,63 @@
package fetcher package fetcher
import ( import (
"github.com/monkeyWie/gopeed-core/internal/controller" "github.com/monkeyWie/gopeed/internal/controller"
"github.com/monkeyWie/gopeed-core/pkg/base" "github.com/monkeyWie/gopeed/pkg/base"
) )
// 对应协议的下载支持 // Fetcher defines the interface for a download protocol.
type Fetcher interface { type Fetcher interface {
Setup(ctl controller.Controller) // Name return the name of the protocol.
// 解析请求 Name() string
Setup(ctl *controller.Controller) error
// Resolve resource info from request
Resolve(req *base.Request) (res *base.Resource, err error) Resolve(req *base.Request) (res *base.Resource, err error)
// 创建任务 // Create ready to download, but not started
Create(res *base.Resource, opts *base.Options) (err error) Create(res *base.Resource, opts *base.Options) (err error)
Start() (err error) Start() (err error)
Pause() (err error) Pause() (err error)
Continue() (err error) Continue() (err error)
Close() (err error)
// 获取任务各个文件下载进度 // Progress returns the progress of the download.
Progress() Progress Progress() Progress
// 该方法会一直阻塞,直到任务下载结束 // Wait for the download to complete, this method will block until the download is done.
Wait() (err error) Wait() (err error)
} }
// FetcherBuilder defines the interface for a fetcher builder.
type FetcherBuilder interface {
// Schemes returns the schemes supported by the fetcher.
Schemes() []string
// Build returns a new fetcher.
Build() Fetcher
// Store fetcher
Store(fetcher Fetcher) (any, error)
// Restore fetcher
Restore() (v any, f func(res *base.Resource, opts *base.Options, v any) Fetcher)
}
type DefaultFetcher struct { type DefaultFetcher struct {
Ctl controller.Controller Ctl *controller.Controller
DoneCh chan error DoneCh chan error
} }
func (f *DefaultFetcher) Setup(ctl controller.Controller) { func (f *DefaultFetcher) Setup(ctl *controller.Controller) (err error) {
f.Ctl = ctl f.Ctl = ctl
f.DoneCh = make(chan error, 1) f.DoneCh = make(chan error, 1)
return
} }
func (f *DefaultFetcher) Wait() (err error) { func (f *DefaultFetcher) Wait() (err error) {
return <-f.DoneCh return <-f.DoneCh
} }
// 获取任务中各个文件的已下载字节数 // Progress is a map of the progress of each file in the torrent.
type Progress []int64 type Progress []int64
// TotalDownloaded 获取任务总下载字节数 // TotalDownloaded returns the total downloaded bytes.
func (p Progress) TotalDownloaded() int64 { func (p Progress) TotalDownloaded() int64 {
total := int64(0) total := int64(0)
for _, d := range p { for _, d := range p {
+5
View File
@@ -0,0 +1,5 @@
package bt
type config struct {
Trackers []string `json:"trackers"`
}
+235
View File
@@ -0,0 +1,235 @@
package bt
import (
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/storage"
"github.com/monkeyWie/gopeed/internal/controller"
"github.com/monkeyWie/gopeed/internal/fetcher"
"github.com/monkeyWie/gopeed/pkg/base"
"github.com/monkeyWie/gopeed/pkg/util"
"path/filepath"
"sync"
"sync/atomic"
"time"
)
var client *torrent.Client
type Fetcher struct {
Ctl *controller.Controller
torrent *torrent.Torrent
res *base.Resource
opts *base.Options
ready atomic.Bool
progress fetcher.Progress
torrentPaths map[string]string
}
func (f *Fetcher) Name() string {
return "bt"
}
func (f *Fetcher) Setup(ctl *controller.Controller) (err error) {
f.Ctl = ctl
var once sync.Once
once.Do(func() {
cfg := torrent.NewDefaultClientConfig()
cfg.ListenPort = 0
// Support custom download path for each torrent
pieceCompletion, err := storage.NewDefaultPieceCompletionForDir(cfg.DataDir)
if err != nil {
pieceCompletion = storage.NewMapPieceCompletion()
}
clientImplCloser := storage.NewFileOpts(storage.NewFileClientOpts{
ClientBaseDir: cfg.DataDir,
TorrentDirMaker: func(baseDir string, info *metainfo.Info, infoHash metainfo.Hash) string {
if dir, ok := f.torrentPaths[infoHash.String()]; ok {
return dir
}
return baseDir
},
PieceCompletion: pieceCompletion,
})
cfg.DefaultStorage = clientImplCloser
client, err = torrent.NewClient(cfg)
if err != nil {
return
}
f.torrentPaths = make(map[string]string)
})
return
}
func (f *Fetcher) Wait() (err error) {
for {
if f.ready.Load() {
done := true
for _, selectIndex := range f.opts.SelectFiles {
file := f.torrent.Files()[selectIndex]
if file.BytesCompleted() < file.Length() {
done = false
break
}
}
if done {
// remove unselected files
for i, file := range f.torrent.Files() {
selected := false
for _, selectIndex := range f.opts.SelectFiles {
if i == selectIndex {
selected = true
break
}
}
if !selected {
util.SafeRemove(filepath.Join(f.opts.Path, file.Path()))
}
}
break
}
}
time.Sleep(time.Millisecond * 500)
}
return nil
}
func (f *Fetcher) Resolve(req *base.Request) (res *base.Resource, err error) {
if err = f.addTorrent(req.URL, true); err != nil {
return
}
defer f.torrent.Drop()
res = &base.Resource{
Req: req,
Name: f.torrent.Name(),
Range: true,
Files: make([]*base.FileInfo, len(f.torrent.Files())),
Hash: f.torrent.InfoHash().String(),
}
for i, file := range f.torrent.Files() {
res.Files[i] = &base.FileInfo{
Name: filepath.Base(file.DisplayPath()),
Path: util.Dir(file.Path()),
Size: file.Length(),
}
res.Size += file.Length()
}
return
}
func (f *Fetcher) Create(res *base.Resource, opts *base.Options) (err error) {
f.res = res
f.opts = opts
if len(f.opts.SelectFiles) == 0 {
f.opts.SelectFiles = make([]int, 0)
for i := range f.torrent.Files() {
f.opts.SelectFiles = append(f.opts.SelectFiles, i)
}
}
if f.opts.Path != "" {
f.torrentPaths[res.Hash] = f.opts.Path
}
f.progress = make(fetcher.Progress, len(f.opts.SelectFiles))
f.ready.Store(false)
return nil
}
func (f *Fetcher) Start() (err error) {
if err = f.addTorrent(f.res.Req.URL, false); err != nil {
return
}
files := f.torrent.Files()
if len(f.opts.SelectFiles) == len(files) {
f.torrent.DownloadAll()
} else {
for _, selectIndex := range f.opts.SelectFiles {
file := files[selectIndex]
file.Download()
}
}
return
}
func (f *Fetcher) Pause() (err error) {
f.ready.Store(false)
f.torrent.Drop()
return
}
func (f *Fetcher) Continue() (err error) {
return f.Start()
}
func (f *Fetcher) Close() (err error) {
f.torrent.Drop()
return nil
}
func (f *Fetcher) Progress() fetcher.Progress {
if !f.ready.Load() {
return f.progress
}
for i := range f.progress {
selectIndex := f.opts.SelectFiles[i]
file := f.torrent.Files()[selectIndex]
f.progress[i] = file.BytesCompleted()
}
return f.progress
}
func (f *Fetcher) addTorrent(url string, resolve bool) (err error) {
schema := util.ParseSchema(url)
if schema == "MAGNET" {
f.torrent, err = client.AddMagnet(url)
} else {
f.torrent, err = client.AddTorrentFromFile(url)
}
if err != nil {
return
}
var cfg config
exist, err := f.Ctl.GetConfig(&cfg)
if err != nil {
return
}
if exist && len(cfg.Trackers) > 0 {
announceList := make([][]string, 0)
for _, tracker := range cfg.Trackers {
announceList = append(announceList, []string{tracker})
}
f.torrent.AddTrackers(announceList)
}
<-f.torrent.GotInfo()
if resolve {
return
}
f.ready.Store(true)
return
}
type FetcherBuilder struct {
}
var schemes = []string{"FILE", "MAGNET"}
func (fb *FetcherBuilder) Schemes() []string {
return schemes
}
func (fb *FetcherBuilder) Build() fetcher.Fetcher {
return &Fetcher{}
}
func (fb *FetcherBuilder) Store(f fetcher.Fetcher) (data any, err error) {
return nil, nil
}
func (fb *FetcherBuilder) Restore() (v any, f func(res *base.Resource, opts *base.Options, v any) fetcher.Fetcher) {
return nil, nil
}
+69
View File
@@ -0,0 +1,69 @@
package bt
import (
"encoding/json"
"github.com/monkeyWie/gopeed/internal/controller"
"github.com/monkeyWie/gopeed/internal/fetcher"
"github.com/monkeyWie/gopeed/internal/test"
"github.com/monkeyWie/gopeed/pkg/base"
"reflect"
"testing"
)
func TestFetcher_Resolve_Torrent(t *testing.T) {
doResolve(t, buildFetcher())
}
func TestFetcher_Config(t *testing.T) {
doResolve(t, buildConfigFetcher())
}
func doResolve(t *testing.T, fetcher fetcher.Fetcher) {
res, err := fetcher.Resolve(&base.Request{
URL: "./testdata/ubuntu-22.04-live-server-amd64.iso.torrent",
})
if err != nil {
panic(err)
}
want := &base.Resource{
Req: res.Req,
Name: "ubuntu-22.04-live-server-amd64.iso",
Size: 1466714112,
Range: true,
Files: []*base.FileInfo{
{
Name: "ubuntu-22.04-live-server-amd64.iso",
Size: 1466714112,
},
},
Hash: "8a55cfbd5ca5d11507364765936c4f9e55b253ed",
}
if !reflect.DeepEqual(want, res) {
t.Errorf("Resolve() got = %v, want %v", res, want)
}
}
func buildFetcher() fetcher.Fetcher {
fetcher := new(FetcherBuilder).Build()
fetcher.Setup(controller.NewController())
return fetcher
}
func buildConfigFetcher() fetcher.Fetcher {
fetcher := new(FetcherBuilder).Build()
newController := controller.NewController()
mockCfg := config{
Trackers: []string{
"udp://tracker.birkenwald.de:6969/announce",
"udp://tracker.bitsearch.to:1337/announce",
}}
newController.GetConfig = func(v any) (bool, error) {
if err := json.Unmarshal([]byte(test.ToJson(mockCfg)), v); err != nil {
return false, err
}
return true, nil
}
fetcher.Setup(newController)
return fetcher
}
+5
View File
@@ -0,0 +1,5 @@
package http
type config struct {
Connections int `json:"connections"`
}
+165 -82
View File
@@ -4,15 +4,17 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"github.com/monkeyWie/gopeed-core/internal/fetcher" "github.com/monkeyWie/gopeed/internal/fetcher"
"github.com/monkeyWie/gopeed-core/pkg/base" "github.com/monkeyWie/gopeed/pkg/base"
fhttp "github.com/monkeyWie/gopeed/pkg/protocol/http"
"github.com/monkeyWie/gopeed/pkg/util"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"io" "io"
"io/ioutil"
"mime" "mime"
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"net/url" "net/url"
"os"
"path" "path"
"path/filepath" "path/filepath"
"strconv" "strconv"
@@ -33,36 +35,43 @@ func (re *RequestError) Error() string {
return fmt.Sprintf("http request fail,code:%d", re.Code) return fmt.Sprintf("http request fail,code:%d", re.Code)
} }
type chunk struct {
Status base.Status
Begin int64
End int64
Downloaded int64
}
func newChunk(begin int64, end int64) *chunk {
return &chunk{
Status: base.DownloadStatusReady,
Begin: begin,
End: end,
}
}
type Fetcher struct { type Fetcher struct {
*fetcher.DefaultFetcher *fetcher.DefaultFetcher
res *base.Resource res *base.Resource
opts *base.Options opts *base.Options
status base.Status status base.Status
clients []*http.Response chunks []*chunk
chunks []*Chunk
file *os.File
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
pauseCh chan interface{} pauseCh chan interface{}
} }
func NewFetcher() *Fetcher { func (f *Fetcher) Name() string {
return &Fetcher{ return "http"
DefaultFetcher: new(fetcher.DefaultFetcher),
pauseCh: make(chan interface{}),
}
}
var protocols = []string{"HTTP", "HTTPS"}
func FetcherBuilder() ([]string, func() fetcher.Fetcher) {
return protocols, func() fetcher.Fetcher {
return NewFetcher()
}
} }
func (f *Fetcher) Resolve(req *base.Request) (*base.Resource, error) { func (f *Fetcher) Resolve(req *base.Request) (*base.Resource, error) {
if err := base.ParseReqExtra[fhttp.ReqExtra](req); err != nil {
return nil, err
}
httpReq, err := buildRequest(nil, req) httpReq, err := buildRequest(nil, req)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -91,7 +100,7 @@ func (f *Fetcher) Resolve(req *base.Request) (*base.Resource, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
res.TotalSize = parse res.Size = parse
} }
} else if base.HttpCodeOK == httpResp.StatusCode { } else if base.HttpCodeOK == httpResp.StatusCode {
// 返回200响应码,不支持断点下载,通过Content-Length头获取文件大小,获取不到的话可能是chunked编码 // 返回200响应码,不支持断点下载,通过Content-Length头获取文件大小,获取不到的话可能是chunked编码
@@ -101,98 +110,97 @@ func (f *Fetcher) Resolve(req *base.Request) (*base.Resource, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
res.TotalSize = parse res.Size = parse
} }
} else { } else {
return nil, NewRequestError(httpResp.StatusCode, httpResp.Status) return nil, NewRequestError(httpResp.StatusCode, httpResp.Status)
} }
file := &base.FileInfo{ file := &base.FileInfo{
Size: res.TotalSize, Size: res.Size,
} }
contentDisposition := httpResp.Header.Get(base.HttpHeaderContentDisposition) contentDisposition := httpResp.Header.Get(base.HttpHeaderContentDisposition)
if contentDisposition != "" { if contentDisposition != "" {
_, params, _ := mime.ParseMediaType(contentDisposition) _, params, _ := mime.ParseMediaType(contentDisposition)
filename := params["filename"] filename := params["filePath"]
if filename != "" { if filename != "" {
file.Name = filename file.Name = filename
} }
} }
// Get file filename by URL // Get file filePath by URL
if file.Name == "" && strings.Count(req.URL, "/") > 2 { if file.Name == "" && strings.Count(req.URL, "/") > 2 {
file.Name = filepath.Base(req.URL) file.Name = filepath.Base(req.URL)
} }
// unknown file filename // unknown file filePath
if file.Name == "" { if file.Name == "" {
file.Name = "unknown" file.Name = "unknown"
} }
res.Files = append(res.Files, file) res.Files = append(res.Files, file)
res.Name = file.Name
return res, nil return res, nil
} }
func (f *Fetcher) Create(res *base.Resource, opts *base.Options) error { func (f *Fetcher) Create(res *base.Resource, opts *base.Options) error {
if opts.Connections != 1 && !res.Range {
opts.Connections = 1
}
f.res = res f.res = res
f.opts = opts f.opts = opts
f.status = base.DownloadStatusReady f.status = base.DownloadStatusReady
if err := base.ParseReqExtra[fhttp.ReqExtra](res.Req); err != nil {
return err
}
if err := base.ParseOptsExtra[fhttp.OptsExtra](opts); err != nil {
return err
}
if opts.Extra == nil {
opts.Extra = &fhttp.OptsExtra{}
}
extra := opts.Extra.(*fhttp.OptsExtra)
if extra.Connections == 0 {
var cfg config
exist, err := f.Ctl.GetConfig(&cfg)
if err != nil {
return err
}
if exist {
extra.Connections = cfg.Connections
} else {
extra.Connections = 1
}
}
return nil return nil
} }
func (f *Fetcher) Start() (err error) { func (f *Fetcher) Start() (err error) {
// 创建文件 // 创建文件
name := f.filename() name := f.filepath()
_, err = f.Ctl.Touch(name, f.res.TotalSize) f.file, err = f.Ctl.Touch(name, f.res.Size)
if err != nil { if err != nil {
return err return err
} }
f.status = base.DownloadStatusStart f.status = base.DownloadStatusRunning
if f.res.Range { f.chunks = f.splitChunk()
// 每个连接平均需要下载的分块大小
chunkSize := f.res.TotalSize / int64(f.opts.Connections)
f.chunks = make([]*Chunk, f.opts.Connections)
f.clients = make([]*http.Response, f.opts.Connections)
for i := 0; i < f.opts.Connections; i++ {
var (
begin = chunkSize * int64(i)
end int64
)
if i == f.opts.Connections-1 {
// 最后一个分块需要保证把文件下载完
end = f.res.TotalSize - 1
} else {
end = begin + chunkSize - 1
}
chunk := NewChunk(begin, end)
f.chunks[i] = chunk
}
} else {
// 只支持单连接下载
f.chunks = make([]*Chunk, 1)
f.clients = make([]*http.Response, 1)
f.chunks[0] = NewChunk(0, 0)
}
f.fetch() f.fetch()
return return
} }
func (f *Fetcher) Pause() (err error) { func (f *Fetcher) Pause() (err error) {
if base.DownloadStatusStart != f.status { if base.DownloadStatusRunning != f.status {
return return
} }
f.status = base.DownloadStatusPause f.status = base.DownloadStatusPause
if f.cancel != nil {
f.cancel() f.cancel()
<-f.pauseCh <-f.pauseCh
}
return return
} }
func (f *Fetcher) Continue() (err error) { func (f *Fetcher) Continue() (err error) {
if base.DownloadStatusStart == f.status || base.DownloadStatusDone == f.status { if base.DownloadStatusRunning == f.status || base.DownloadStatusDone == f.status {
return return
} }
f.status = base.DownloadStatusStart f.status = base.DownloadStatusRunning
var name = f.filename() f.file, err = os.OpenFile(f.filepath(), os.O_RDWR, os.ModeAppend)
_, err = f.Ctl.Open(name)
if err != nil { if err != nil {
return err return err
} }
@@ -200,6 +208,13 @@ func (f *Fetcher) Continue() (err error) {
return return
} }
func (f *Fetcher) Close() (err error) {
if err = f.Pause(); err != nil {
return
}
return
}
func (f *Fetcher) Progress() fetcher.Progress { func (f *Fetcher) Progress() fetcher.Progress {
p := make(fetcher.Progress, 0) p := make(fetcher.Progress, 0)
if len(f.chunks) > 0 { if len(f.chunks) > 0 {
@@ -212,19 +227,14 @@ func (f *Fetcher) Progress() fetcher.Progress {
return p return p
} }
func (f *Fetcher) filename() string { func (f *Fetcher) filepath() string {
// 创建文件 return util.Filepath(f.opts.Path, f.res.Files[0].Name, f.opts.Name)
var filename = f.opts.Name
if filename == "" {
filename = f.res.Files[0].Name
}
return filepath.Join(f.opts.Path, filename)
} }
func (f *Fetcher) fetch() { func (f *Fetcher) fetch() {
f.ctx, f.cancel = context.WithCancel(context.Background()) f.ctx, f.cancel = context.WithCancel(context.Background())
eg, _ := errgroup.WithContext(f.ctx) eg, _ := errgroup.WithContext(f.ctx)
for i := 0; i < f.opts.Connections; i++ { for i := 0; i < f.opts.Extra.(*fhttp.OptsExtra).Connections; i++ {
i := i i := i
eg.Go(func() error { eg.Go(func() error {
return f.fetchChunk(i) return f.fetchChunk(i)
@@ -234,7 +244,7 @@ func (f *Fetcher) fetch() {
go func() { go func() {
err := eg.Wait() err := eg.Wait()
// 下载停止,关闭文件句柄 // 下载停止,关闭文件句柄
f.Ctl.Close(f.filename()) f.file.Close()
if f.status == base.DownloadStatusPause { if f.status == base.DownloadStatusPause {
f.pauseCh <- nil f.pauseCh <- nil
} else { } else {
@@ -249,7 +259,6 @@ func (f *Fetcher) fetch() {
} }
func (f *Fetcher) fetchChunk(index int) (err error) { func (f *Fetcher) fetchChunk(index int) (err error) {
filename := f.filename()
chunk := f.chunks[index] chunk := f.chunks[index]
httpReq, err := buildRequest(f.ctx, f.res.Req) httpReq, err := buildRequest(f.ctx, f.res.Req)
@@ -285,7 +294,6 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
if err != nil { if err != nil {
return err return err
} }
f.clients[index] = resp
if resp.StatusCode != base.HttpCodeOK && resp.StatusCode != base.HttpCodePartialContent { if resp.StatusCode != base.HttpCodeOK && resp.StatusCode != base.HttpCodePartialContent {
err = NewRequestError(resp.StatusCode, resp.Status) err = NewRequestError(resp.StatusCode, resp.Status)
return err return err
@@ -303,7 +311,7 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
for { for {
n, err := resp.Body.Read(buf) n, err := resp.Body.Read(buf)
if n > 0 { if n > 0 {
_, err := f.Ctl.Write(filename, chunk.Begin+chunk.Downloaded, buf[:n]) _, err := f.file.WriteAt(buf[:n], chunk.Begin+chunk.Downloaded)
if err != nil { if err != nil {
return false, err return false, err
} }
@@ -324,16 +332,45 @@ func (f *Fetcher) fetchChunk(index int) (err error) {
} }
} }
if f.status == base.DownloadStatusPause {
chunk.Status = base.DownloadStatusPause
} else if chunk.Downloaded >= chunk.End-chunk.Begin+1 {
chunk.Status = base.DownloadStatusDone
} else {
if err != nil { if err != nil {
chunk.Status = base.DownloadStatusError chunk.Status = base.DownloadStatusError
} else { return
chunk.Status = base.DownloadStatusDone
} }
isComplete := f.res.Range && chunk.Downloaded >= chunk.End-chunk.Begin+1
if f.status == base.DownloadStatusPause && !isComplete {
chunk.Status = base.DownloadStatusPause
return
}
chunk.Status = base.DownloadStatusDone
return
}
func (f *Fetcher) splitChunk() (chunks []*chunk) {
if f.res.Range {
connections := f.opts.Extra.(*fhttp.OptsExtra).Connections
// 每个连接平均需要下载的分块大小
chunkSize := f.res.Size / int64(connections)
chunks = make([]*chunk, connections)
for i := 0; i < connections; i++ {
var (
begin = chunkSize * int64(i)
end int64
)
if i == connections-1 {
// 最后一个分块需要保证把文件下载完
end = f.res.Size - 1
} else {
end = begin + chunkSize - 1
}
chunk := newChunk(begin, end)
chunks[i] = chunk
}
} else {
// 只支持单连接下载
chunks = make([]*chunk, 1)
chunks[0] = newChunk(0, 0)
} }
return return
} }
@@ -343,7 +380,7 @@ func buildClient() *http.Client {
jar, _ := cookiejar.New(nil) jar, _ := cookiejar.New(nil)
return &http.Client{ return &http.Client{
Jar: jar, Jar: jar,
Timeout: time.Second * 10, Timeout: time.Second * 60,
} }
} }
@@ -361,7 +398,7 @@ func buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request
if req.Extra == nil { if req.Extra == nil {
method = http.MethodGet method = http.MethodGet
} else { } else {
extra := req.Extra.(Extra) extra := req.Extra.(*fhttp.ReqExtra)
if extra.Method != "" { if extra.Method != "" {
method = extra.Method method = extra.Method
} else { } else {
@@ -373,7 +410,7 @@ func buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request
} }
} }
if extra.Body != "" { if extra.Body != "" {
body = ioutil.NopCloser(bytes.NewBufferString(extra.Body)) body = bytes.NewBufferString(extra.Body)
} }
} }
@@ -388,3 +425,49 @@ func buildRequest(ctx context.Context, req *base.Request) (httpReq *http.Request
httpReq.Header = headers httpReq.Header = headers
return httpReq, nil return httpReq, nil
} }
type fetcherData struct {
Chunks []*chunk
}
type FetcherBuilder struct {
}
var schemes = []string{"HTTP", "HTTPS"}
func (fb *FetcherBuilder) Schemes() []string {
return schemes
}
func (fb *FetcherBuilder) Build() fetcher.Fetcher {
return &Fetcher{
DefaultFetcher: new(fetcher.DefaultFetcher),
pauseCh: make(chan interface{}),
}
}
func (fb *FetcherBuilder) Store(f fetcher.Fetcher) (data any, err error) {
_f := f.(*Fetcher)
return &fetcherData{
Chunks: _f.chunks,
}, nil
}
func (fb *FetcherBuilder) Restore() (v any, f func(res *base.Resource, opts *base.Options, v any) fetcher.Fetcher) {
return &fetcherData{}, func(res *base.Resource, opts *base.Options, v any) fetcher.Fetcher {
fd := v.(*fetcherData)
fb := &FetcherBuilder{}
fetcher := fb.Build().(*Fetcher)
fetcher.res = res
fetcher.opts = opts
fetcher.status = base.DownloadStatusPause
base.ParseReqExtra[fhttp.ReqExtra](res.Req)
base.ParseOptsExtra[fhttp.OptsExtra](opts)
if len(fd.Chunks) == 0 {
fetcher.chunks = fetcher.splitChunk()
} else {
fetcher.chunks = fd.Chunks
}
return fetcher
}
}
+165 -37
View File
@@ -1,10 +1,13 @@
package http package http
import ( import (
"github.com/monkeyWie/gopeed-core/internal/controller" "encoding/json"
"github.com/monkeyWie/gopeed-core/internal/fetcher" "fmt"
"github.com/monkeyWie/gopeed-core/pkg/base" "github.com/monkeyWie/gopeed/internal/controller"
"github.com/monkeyWie/gopeed-core/pkg/test" "github.com/monkeyWie/gopeed/internal/fetcher"
"github.com/monkeyWie/gopeed/internal/test"
"github.com/monkeyWie/gopeed/pkg/base"
"github.com/monkeyWie/gopeed/pkg/protocol/http"
"net" "net"
"reflect" "reflect"
"testing" "testing"
@@ -13,7 +16,8 @@ import (
func TestFetcher_Resolve(t *testing.T) { func TestFetcher_Resolve(t *testing.T) {
testResolve(test.StartTestFileServer, &base.Resource{ testResolve(test.StartTestFileServer, &base.Resource{
TotalSize: test.BuildSize, Name: test.BuildName,
Size: test.BuildSize,
Range: true, Range: true,
Files: []*base.FileInfo{ Files: []*base.FileInfo{
{ {
@@ -23,7 +27,8 @@ func TestFetcher_Resolve(t *testing.T) {
}, },
}, t) }, t)
testResolve(test.StartTestChunkedServer, &base.Resource{ testResolve(test.StartTestChunkedServer, &base.Resource{
TotalSize: 0, Name: test.BuildName,
Size: 0,
Range: false, Range: false,
Files: []*base.FileInfo{ Files: []*base.FileInfo{
{ {
@@ -37,7 +42,7 @@ func TestFetcher_Resolve(t *testing.T) {
func testResolve(startTestServer func() net.Listener, want *base.Resource, t *testing.T) { func testResolve(startTestServer func() net.Listener, want *base.Resource, t *testing.T) {
listener := startTestServer() listener := startTestServer()
defer listener.Close() defer listener.Close()
fetcher := NewFetcher() fetcher := buildFetcher()
res, err := fetcher.Resolve(&base.Request{ res, err := fetcher.Resolve(&base.Request{
URL: "http://" + listener.Addr().String() + "/" + test.BuildName, URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
}) })
@@ -46,7 +51,7 @@ func testResolve(startTestServer func() net.Listener, want *base.Resource, t *te
} }
res.Req = nil res.Req = nil
if !reflect.DeepEqual(want, res) { if !reflect.DeepEqual(want, res) {
t.Errorf("Resolve error = %v, want %v", res, want) t.Errorf("Resolve() got = %v, want %v", res, want)
} }
} }
@@ -75,7 +80,13 @@ func TestFetcher_DownloadChunked(t *testing.T) {
defer listener.Close() defer listener.Close()
// chunked编码下载 // chunked编码下载
downloadNormal(listener, 1, t) downloadNormal(listener, 1, t)
downloadContinue(listener, 1, t) }
func TestFetcher_DownloadPost(t *testing.T) {
listener := test.StartTestPostServer()
defer listener.Close()
// post下载
downloadPost(listener, 1, t)
} }
func TestFetcher_DownloadRetry(t *testing.T) { func TestFetcher_DownloadRetry(t *testing.T) {
@@ -92,29 +103,18 @@ func TestFetcher_DownloadError(t *testing.T) {
downloadError(listener, 1, t) downloadError(listener, 1, t)
} }
func downloadReady(listener net.Listener, connections int, t *testing.T) fetcher.Fetcher { func TestFetcher_DownloadResume(t *testing.T) {
fetcher := NewFetcher() listener := test.StartTestFileServer()
fetcher.Setup(controller.NewController()) defer listener.Close()
res, err := fetcher.Resolve(&base.Request{
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
})
if err != nil {
t.Fatal(err)
}
err = fetcher.Create(res, &base.Options{
Name: test.DownloadName,
Path: test.Dir,
Connections: connections,
})
if err != nil {
t.Fatal(err)
}
return fetcher
downloadResume(listener, 1, t)
downloadResume(listener, 5, t)
downloadResume(listener, 8, t)
downloadResume(listener, 16, t)
} }
func downloadNormal(listener net.Listener, connections int, t *testing.T) { func TestFetcher_Config(t *testing.T) {
fetcher := downloadReady(listener, connections, t) fetcher, _, _ := doDownloadReady(buildConfigFetcher(), test.StartTestFileServer(), 0, t)
err := fetcher.Start() err := fetcher.Start()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -126,21 +126,92 @@ func downloadNormal(listener net.Listener, connections int, t *testing.T) {
want := test.FileMd5(test.BuildFile) want := test.FileMd5(test.BuildFile)
got := test.FileMd5(test.DownloadFile) got := test.FileMd5(test.DownloadFile)
if want != got { if want != got {
t.Errorf("Download error = %v, want %v", got, want) t.Errorf("Download() got = %v, want %v", got, want)
} }
} }
func downloadContinue(listener net.Listener, connections int, t *testing.T) { func downloadReady(listener net.Listener, connections int, t *testing.T) (fetcher fetcher.Fetcher, res *base.Resource, opts *base.Options) {
fetcher := downloadReady(listener, connections, t) return doDownloadReady(buildFetcher(), listener, connections, t)
}
func doDownloadReady(f fetcher.Fetcher, listener net.Listener, connections int, t *testing.T) (fetcher fetcher.Fetcher, res *base.Resource, opts *base.Options) {
fetcher = f
res, err := fetcher.Resolve(&base.Request{
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
})
if err != nil {
t.Fatal(err)
}
var extra any = nil
if connections > 0 {
extra = http.OptsExtra{
Connections: connections,
}
}
opts = &base.Options{
Name: test.DownloadName,
Path: test.Dir,
Extra: extra,
}
err = fetcher.Create(res, opts)
if err != nil {
t.Fatal(err)
}
return
}
func downloadNormal(listener net.Listener, connections int, t *testing.T) {
fetcher, _, _ := downloadReady(listener, connections, t)
err := fetcher.Start() err := fetcher.Start()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
time.Sleep(time.Millisecond * 200) err = fetcher.Wait()
if err != nil {
t.Fatal(err)
}
want := test.FileMd5(test.BuildFile)
got := test.FileMd5(test.DownloadFile)
if want != got {
t.Errorf("Download() got = %v, want %v", got, want)
}
}
func downloadPost(listener net.Listener, connections int, t *testing.T) {
fetcher, _, _ := downloadReady(listener, connections, t)
fetcher.(*Fetcher).res.Req.Extra = &http.ReqExtra{
Method: "POST",
Header: map[string]string{
"Authorization": "Bearer 123456",
},
Body: fmt.Sprintf(`{"name":"%s"}`, test.BuildName),
}
err := fetcher.Start()
if err != nil {
t.Fatal(err)
}
err = fetcher.Wait()
if err != nil {
t.Fatal(err)
}
want := test.FileMd5(test.BuildFile)
got := test.FileMd5(test.DownloadFile)
if want != got {
t.Errorf("Download() got = %v, want %v", got, want)
}
}
func downloadContinue(listener net.Listener, connections int, t *testing.T) {
fetcher, _, _ := downloadReady(listener, connections, t)
err := fetcher.Start()
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
if err := fetcher.Pause(); err != nil { if err := fetcher.Pause(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
time.Sleep(time.Millisecond * 200) time.Sleep(time.Millisecond * 50)
if err := fetcher.Continue(); err != nil { if err := fetcher.Continue(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -151,18 +222,75 @@ func downloadContinue(listener net.Listener, connections int, t *testing.T) {
want := test.FileMd5(test.BuildFile) want := test.FileMd5(test.BuildFile)
got := test.FileMd5(test.DownloadFile) got := test.FileMd5(test.DownloadFile)
if want != got { if want != got {
t.Errorf("Download error = %v, want %v", got, want) t.Errorf("Download() got = %v, want %v", got, want)
} }
} }
func downloadError(listener net.Listener, connections int, t *testing.T) { func downloadError(listener net.Listener, connections int, t *testing.T) {
fetcher := downloadReady(listener, connections, t) fetcher, _, _ := downloadReady(listener, connections, t)
err := fetcher.Start() err := fetcher.Start()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = fetcher.Wait() err = fetcher.Wait()
if err == nil { if err == nil {
t.Errorf("Download error = %v, want %v", err, nil) t.Errorf("Download() got = %v, want %v", err, nil)
} }
} }
func downloadResume(listener net.Listener, connections int, t *testing.T) {
fetcher, res, opts := downloadReady(listener, connections, t)
err := fetcher.Start()
if err != nil {
t.Fatal(err)
}
fb := new(FetcherBuilder)
time.Sleep(time.Millisecond * 50)
data, err := fb.Store(fetcher)
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 50)
fetcher.Pause()
_, f := fb.Restore()
f(res, opts, data)
if err != nil {
t.Fatal(err)
}
fetcher.Setup(controller.NewController())
fetcher.Continue()
err = fetcher.Wait()
if err != nil {
t.Fatal(err)
}
want := test.FileMd5(test.BuildFile)
got := test.FileMd5(test.DownloadFile)
if want != got {
t.Errorf("Download() got = %v, want %v", got, want)
}
}
func buildFetcher() *Fetcher {
fetcher := new(FetcherBuilder).Build()
fetcher.Setup(controller.NewController())
return fetcher.(*Fetcher)
}
func buildConfigFetcher() fetcher.Fetcher {
fetcher := new(FetcherBuilder).Build()
newController := controller.NewController()
mockCfg := config{
Connections: 16,
}
newController.GetConfig = func(v any) (bool, error) {
if err := json.Unmarshal([]byte(test.ToJson(mockCfg)), v); err != nil {
return false, err
}
return true, nil
}
fetcher.Setup(newController)
return fetcher
}
-24
View File
@@ -1,24 +0,0 @@
package http
import "github.com/monkeyWie/gopeed-core/pkg/base"
type Chunk struct {
Status base.Status
Begin int64
End int64
Downloaded int64
}
func NewChunk(begin int64, end int64) *Chunk {
return &Chunk{
Status: base.DownloadStatusReady,
Begin: begin,
End: end,
}
}
type Extra struct {
Method string
Header map[string]string
Body string
}
@@ -1,12 +1,14 @@
package test package test
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"math/rand" "math/rand"
"net" "net"
"net/http" "net/http"
"os" "os"
"time"
) )
const ( const (
@@ -25,6 +27,25 @@ func StartTestFileServer() net.Listener {
}) })
} }
type SlowFileServer struct {
delay time.Duration
handler http.Handler
}
func (s *SlowFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
time.Sleep(s.delay)
s.handler.ServeHTTP(w, r)
}
func StartTestSlowFileServer(delay time.Duration) net.Listener {
return startTestServer(func() http.Handler {
return &SlowFileServer{
delay: delay,
handler: http.FileServer(http.Dir(Dir)),
}
})
}
func StartTestChunkedServer() net.Listener { func StartTestChunkedServer() net.Listener {
return startTestServer(func() http.Handler { return startTestServer(func() http.Handler {
mux := http.NewServeMux() mux := http.NewServeMux()
@@ -61,6 +82,29 @@ func StartTestRetryServer() net.Listener {
}) })
} }
func StartTestPostServer() net.Listener {
return startTestServer(func() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/"+BuildName, func(writer http.ResponseWriter, request *http.Request) {
if request.Method == "POST" && request.Header.Get("Authorization") != "" {
var data map[string]interface{}
if err := json.NewDecoder(request.Body).Decode(&data); err != nil {
panic(err)
}
if data["name"] == BuildName {
file, err := os.Open(BuildFile)
if err != nil {
panic(err)
}
defer file.Close()
io.Copy(writer, file)
}
}
})
return mux
})
}
func StartTestErrorServer() net.Listener { func StartTestErrorServer() net.Listener {
counter := 0 counter := 0
return startTestServer(func() http.Handler { return startTestServer(func() http.Handler {
@@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"os" "os"
"path/filepath"
) )
func FileMd5(filePath string) string { func FileMd5(filePath string) string {
@@ -26,7 +27,29 @@ func FileMd5(filePath string) string {
return hex.EncodeToString(hash.Sum(nil)) return hex.EncodeToString(hash.Sum(nil))
} }
func DirMd5(dirPath string) string {
hash := md5.New()
filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
if _, err := io.Copy(hash, file); err != nil {
return err
}
return nil
})
return hex.EncodeToString(hash.Sum(nil))
}
func ToJson(v interface{}) string { func ToJson(v interface{}) string {
buf, _ := json.Marshal(v) buf, _ := json.Marshal(v)
return string(buf) return string(buf)
} }
func JsonEqual(v1 any, v2 any) bool {
return ToJson(v1) == ToJson(v2)
}
-23
View File
@@ -1,23 +0,0 @@
package main
import (
"fmt"
"github.com/monkeyWie/gopeed-core/pkg/protocol/http"
)
func main() {
request := &http.Request{
Method: "get",
URL: "http://github.com/proxyee-down-org/proxyee-down/releases/download/3.4/proxyee-down-main.jar",
Header: map[string]string{
"Host": "github.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Referer": "http://github.com/proxyee-down-org/proxyee-down/releases",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
},
}
got, _ := http.Resolve(request)
fmt.Println(got)
}
+6 -12
View File
@@ -1,15 +1,13 @@
package base package base
import "errors" type Status string
type Status int
const ( const (
DownloadStatusReady Status = iota DownloadStatusReady Status = "ready"
DownloadStatusStart DownloadStatusRunning Status = "running"
DownloadStatusPause DownloadStatusPause Status = "pause"
DownloadStatusError DownloadStatusError Status = "error"
DownloadStatusDone DownloadStatusDone Status = "done"
) )
const ( const (
@@ -23,7 +21,3 @@ const (
HttpHeaderRangeFormat = "bytes=%d-%d" HttpHeaderRangeFormat = "bytes=%d-%d"
) )
var (
DeleteErr = errors.New("delete")
)
+8
View File
@@ -0,0 +1,8 @@
package base
import "errors"
var (
NotFound = errors.New("not found")
BadParams = errors.New("bad params")
)
+51 -13
View File
@@ -1,36 +1,74 @@
package base package base
import "github.com/monkeyWie/gopeed/pkg/util"
// 下载请求 // 下载请求
type Request struct { type Request struct {
// 下载链接 // 下载链接
URL string URL string `json:"url"`
// 附加信息 // 附加信息
Extra interface{} Extra any `json:"extra"`
} }
// 资源信息 // 资源信息
type Resource struct { type Resource struct {
Req *Request Req *Request `json:"req"`
// 资源名称
Name string `json:"name"`
// 资源总大小 // 资源总大小
TotalSize int64 Size int64 `json:"size"`
// 是否支持断点下载 // 是否支持断点下载
Range bool Range bool `json:"range"`
// 资源所包含的文件列表 // 资源所包含的文件列表
Files []*FileInfo Files []*FileInfo `json:"files"`
// 资源hash值
Hash string `json:"hash"`
} }
type FileInfo struct { type FileInfo struct {
Name string Name string `json:"name"`
Path string Path string `json:"path"`
Size int64 Size int64 `json:"size"`
} }
// 下载选项 // 下载选项
type Options struct { type Options struct {
// 保存文件名 // 保存文件名
Name string Name string `json:"name"`
// 保存目录 // 保存目录
Path string Path string `json:"path"`
// 并发连接数 // 选择下载的文件下标列表
Connections int SelectFiles []int `json:"selectFiles"`
// 附加信息
Extra any `json:"extra"`
}
func ParseReqExtra[E any](req *Request) error {
if req.Extra == nil {
return nil
}
if _, ok := req.Extra.(*E); ok {
return nil
}
var t E
if err := util.MapToStruct(req.Extra, &t); err != nil {
return err
}
req.Extra = &t
return nil
}
func ParseOptsExtra[E any](opts *Options) error {
if opts.Extra == nil {
return nil
}
if _, ok := opts.Extra.(*E); ok {
return nil
}
var t E
if err := util.MapToStruct(opts.Extra, &t); err != nil {
return err
}
opts.Extra = &t
return nil
} }
+437 -129
View File
File diff suppressed because it is too large. Load diff
+147 -12
View File
@@ -1,19 +1,24 @@
package download package download
import ( import (
"github.com/monkeyWie/gopeed-core/internal/protocol/http" "github.com/monkeyWie/gopeed/internal/test"
"github.com/monkeyWie/gopeed-core/pkg/base" "github.com/monkeyWie/gopeed/pkg/base"
"github.com/monkeyWie/gopeed-core/pkg/test" "github.com/monkeyWie/gopeed/pkg/protocol/http"
"reflect" "reflect"
"sync" "sync"
"testing" "testing"
"time"
) )
func TestDownloader_Resolve(t *testing.T) { func TestDownloader_Resolve(t *testing.T) {
listener := test.StartTestFileServer() listener := test.StartTestFileServer()
defer listener.Close() defer listener.Close()
downloader := NewDownloader(http.FetcherBuilder) downloader := NewDownloader(nil)
if err := downloader.Setup(); err != nil {
t.Fatal(err)
}
defer downloader.Clear()
req := &base.Request{ req := &base.Request{
URL: "http://" + listener.Addr().String() + "/" + test.BuildName, URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
} }
@@ -22,8 +27,9 @@ func TestDownloader_Resolve(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
want := &base.Resource{ want := &base.Resource{
Name: test.BuildName,
Req: req, Req: req,
TotalSize: test.BuildSize, Size: test.BuildSize,
Range: true, Range: true,
Files: []*base.FileInfo{ Files: []*base.FileInfo{
{ {
@@ -33,8 +39,8 @@ func TestDownloader_Resolve(t *testing.T) {
}, },
}, },
} }
if !reflect.DeepEqual(res, want) { if !reflect.DeepEqual(want, res) {
t.Errorf("Resolve error = %s, want %s", test.ToJson(res), test.ToJson(want)) t.Errorf("Resolve() got = %v, want %v", res, want)
} }
} }
@@ -42,7 +48,11 @@ func TestDownloader_Create(t *testing.T) {
listener := test.StartTestFileServer() listener := test.StartTestFileServer()
defer listener.Close() defer listener.Close()
downloader := NewDownloader(http.FetcherBuilder) downloader := NewDownloader(nil)
if err := downloader.Setup(); err != nil {
t.Fatal(err)
}
defer downloader.Clear()
req := &base.Request{ req := &base.Request{
URL: "http://" + listener.Addr().String() + "/" + test.BuildName, URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
} }
@@ -53,15 +63,17 @@ func TestDownloader_Create(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
downloader.Listener(func(taskInfo *TaskInfo, eventKey EventKey) { downloader.Listener(func(event *Event) {
if eventKey == EventKeyDone { if event.Key == EventKeyDone {
wg.Done() wg.Done()
} }
}) })
err = downloader.Create(res, &base.Options{ _, err = downloader.Create(res, &base.Options{
Path: test.Dir, Path: test.Dir,
Name: test.DownloadName, Name: test.DownloadName,
Extra: http.OptsExtra{
Connections: 4, Connections: 4,
},
}) })
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -70,6 +82,129 @@ func TestDownloader_Create(t *testing.T) {
want := test.FileMd5(test.BuildFile) want := test.FileMd5(test.BuildFile)
got := test.FileMd5(test.DownloadFile) got := test.FileMd5(test.DownloadFile)
if want != got { if want != got {
t.Errorf("Download error = %v, want %v", got, want) t.Errorf("Download() got = %v, want %v", got, want)
}
}
func TestDownloader_StoreAndRestore(t *testing.T) {
listener := test.StartTestSlowFileServer(time.Millisecond * 2000)
defer listener.Close()
downloader := NewDownloader(&DownloaderConfig{
Storage: NewBoltStorage("./"),
})
if err := downloader.Setup(); err != nil {
t.Fatal(err)
}
defer downloader.Clear()
req := &base.Request{
URL: "http://" + listener.Addr().String() + "/" + test.BuildName,
}
res, err := downloader.Resolve(req)
if err != nil {
t.Fatal(err)
}
id, err := downloader.Create(res, &base.Options{
Path: test.Dir,
Name: test.DownloadName,
Extra: http.OptsExtra{
Connections: 4,
},
})
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 1001)
err = downloader.Pause(id)
if err != nil {
t.Fatal(err)
}
downloader.Close()
downloader = NewDownloader(&DownloaderConfig{
Storage: NewBoltStorage("./"),
})
if err := downloader.Setup(); err != nil {
t.Fatal(err)
}
task := downloader.GetTask(id)
if task == nil {
t.Fatal("task is nil")
}
var wg sync.WaitGroup
wg.Add(1)
downloader.Listener(func(event *Event) {
if event.Key == EventKeyDone {
wg.Done()
}
})
err = downloader.Continue(id)
wg.Wait()
if err != nil {
t.Fatal(err)
}
want := test.FileMd5(test.BuildFile)
got := test.FileMd5(test.DownloadFile)
if want != got {
t.Errorf("StoreAndResume() got = %v, want %v", got, want)
}
downloader.Clear()
}
func TestDownloader_Protocol_Config(t *testing.T) {
downloader := NewDownloader(nil)
if err := downloader.Setup(); err != nil {
t.Fatal(err)
}
defer downloader.Clear()
var httpCfg map[string]any
exits, err := downloader.getProtocolConfig("http", &httpCfg)
if err != nil {
t.Fatal(err)
}
if exits {
t.Errorf("getProtocolConfig() got = %v, want %v", exits, false)
}
storeCfg := &DownloaderStoreConfig{
RefreshInterval: 500,
DownloadDir: "./downloads",
ProtocolConfig: map[string]any{
"http": map[string]any{
"connections": 4,
},
"bt": map[string]any{
"trackerSubscribeUrls": []string{
"https://raw.githubusercontent.com/XIU2/TrackersListCollection/master/best.txt",
},
"trackers": []string{
"udp://tracker.coppersurfer.tk:6969/announce",
"udp://tracker.leechers-paradise.org:6969/announce",
},
},
},
Extra: map[string]any{
"theme": "dark",
},
}
if err := downloader.PutConfig(storeCfg); err != nil {
t.Fatal(err)
}
exits, newStoreCfg, err := downloader.GetConfig()
if err != nil {
t.Fatal(err)
}
if !exits {
t.Errorf("GetConfig() got = %v, want %v", exits, true)
}
if !test.JsonEqual(storeCfg, newStoreCfg) {
t.Errorf("GetConfig() got = %v, want %v", test.ToJson(storeCfg), test.ToJson(newStoreCfg))
} }
} }
+5 -2
View File
@@ -8,10 +8,13 @@ const (
EventKeyContinue = "continue" EventKeyContinue = "continue"
EventKeyProgress = "progress" EventKeyProgress = "progress"
EventKeyError = "error" EventKeyError = "error"
EventKeyDelete = "delete"
EventKeyDone = "done" EventKeyDone = "done"
EventKeyFinally = "finally"
) )
type Event struct { type Event struct {
EventKey Key EventKey
TaskInfo Task *Task
Err error
} }
+48
View File
@@ -0,0 +1,48 @@
package download
import (
"github.com/monkeyWie/gopeed/internal/controller"
"github.com/monkeyWie/gopeed/internal/fetcher"
"github.com/monkeyWie/gopeed/internal/protocol/bt"
"github.com/monkeyWie/gopeed/internal/protocol/http"
)
type DownloaderConfig struct {
Controller *controller.Controller
FetchBuilders []fetcher.FetcherBuilder
Storage Storage
*DownloaderStoreConfig
}
func (cfg *DownloaderConfig) Init() *DownloaderConfig {
if cfg.Controller == nil {
cfg.Controller = controller.NewController()
}
if len(cfg.FetchBuilders) == 0 {
cfg.FetchBuilders = []fetcher.FetcherBuilder{
new(http.FetcherBuilder),
new(bt.FetcherBuilder),
}
}
if cfg.Storage == nil {
cfg.Storage = NewMemStorage()
}
if cfg.DownloaderStoreConfig == nil {
cfg.DownloaderStoreConfig = &DownloaderStoreConfig{}
}
if cfg.RefreshInterval == 0 {
cfg.RefreshInterval = 350
}
return cfg
}
// DownloaderStoreConfig is the config that can restore the downloader.
type DownloaderStoreConfig struct {
RefreshInterval int `json:"refreshInterval"` // RefreshInterval time duration to refresh task progress(ms)
DownloadDir string `json:"downloadDir"` // DownloadDir is the directory to save the downloaded files
ProtocolConfig map[string]any `json:"protocolConfig"` // ProtocolConfig is special config for each protocol
Extra map[string]any `json:"extra"` // Extra is the extra config
}
+228
View File
@@ -0,0 +1,228 @@
package download
import (
"encoding/json"
"go.etcd.io/bbolt"
"os"
"path/filepath"
"reflect"
)
type Storage interface {
Setup(buckets []string) error
Put(bucket string, key string, v any) error
Get(bucket string, key string, v any) (bool, error)
List(bucket string, v any) error
Pop(bucket string, key string, v any) error
Delete(bucket string, key string) error
Close() error
Clear() error
}
func changeValue(p any, v any) {
if v == nil {
return
}
rp := reflect.ValueOf(p)
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Slice {
if rv.Len() == 0 {
return
}
// get underlying type
tp := reflect.TypeOf(p).Elem().Elem()
for i := 0; i < rv.Len(); i++ {
// convert to underlying type
vv := rv.Index(i).Elem().Convert(tp)
rp.Elem().Set(reflect.Append(rp.Elem(), vv))
}
} else if rv.Kind() == reflect.Ptr {
rp.Elem().Set(rv.Elem())
} else {
rp.Elem().Set(rv)
}
}
var memData = make(map[string]map[string]any)
type MemStorage struct {
}
func NewMemStorage() *MemStorage {
return &MemStorage{}
}
func (n *MemStorage) Setup(buckets []string) error {
for _, bucket := range buckets {
if _, ok := memData[bucket]; !ok {
memData[bucket] = make(map[string]any)
}
}
return nil
}
func (n *MemStorage) Put(bucket string, key string, v any) error {
memData[bucket][key] = v
return nil
}
func (n *MemStorage) Get(bucket string, key string, v any) (bool, error) {
if dv, ok := memData[bucket][key]; ok {
changeValue(v, dv)
return true, nil
}
return false, nil
}
func (n *MemStorage) List(bucket string, v any) error {
data := memData[bucket]
list := make([]any, 0)
for _, v := range data {
list = append(list, v)
}
changeValue(v, list)
return nil
}
func (n *MemStorage) Pop(bucket string, key string, v any) error {
data := memData[bucket]
changeValue(v, data[key])
delete(data, key)
return nil
}
func (n *MemStorage) Delete(bucket string, key string) error {
delete(memData[bucket], key)
return nil
}
func (n *MemStorage) Close() error {
return nil
}
func (n *MemStorage) Clear() error {
memData = make(map[string]map[string]any)
return nil
}
const (
dbFile = "gopeed.db"
)
type BoltStorage struct {
db *bbolt.DB
path string
}
func NewBoltStorage(dir string) *BoltStorage {
path := filepath.Join(dir, dbFile)
db, err := bbolt.Open(path, 0600, nil)
if err != nil {
panic(err)
}
return &BoltStorage{
db: db,
path: path,
}
}
func (b *BoltStorage) Setup(buckets []string) error {
return b.db.Update(func(tx *bbolt.Tx) error {
for _, bucket := range buckets {
_, err := tx.CreateBucketIfNotExists([]byte(bucket))
if err != nil {
return err
}
}
return nil
})
}
func (b *BoltStorage) Put(bucket string, key string, v any) error {
buf, err := json.Marshal(v)
if err != nil {
return err
}
return b.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(bucket))
return b.Put([]byte(key), buf)
})
}
func (b *BoltStorage) Get(bucket string, key string, v any) (bool, error) {
var data []byte
err := b.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(bucket))
data = b.Get([]byte(key))
return nil
})
if err != nil {
return false, err
}
if data == nil {
return false, nil
}
if err := json.Unmarshal(data, v); err != nil {
return false, err
}
return true, nil
}
func (b *BoltStorage) List(bucket string, v any) error {
list := make([]any, 0)
tv := reflect.TypeOf(v).Elem().Elem()
if err := b.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(bucket))
if err := b.ForEach(func(k, v []byte) error {
data := reflect.New(tv.Elem()).Interface()
if err := json.Unmarshal(v, &data); err != nil {
return err
}
list = append(list, data)
return nil
}); err != nil {
return err
}
return nil
}); err != nil {
return err
}
changeValue(v, list)
return nil
}
func (b *BoltStorage) Pop(bucket string, key string, v any) error {
var data []byte
err := b.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(bucket))
kb := []byte(key)
data = b.Get(kb)
return b.Delete(kb)
})
if err != nil {
return err
}
return json.Unmarshal(data, v)
}
func (b *BoltStorage) Delete(bucket string, key string) error {
return b.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(bucket))
return b.Delete([]byte(key))
})
}
func (b *BoltStorage) Close() error {
return b.db.Close()
}
func (b *BoltStorage) Clear() error {
if err := b.Close(); err != nil {
return err
}
if err := os.Remove(b.path); err != nil {
return err
}
return nil
}
+49
View File
@@ -0,0 +1,49 @@
package download
import (
gonanoid "github.com/matoous/go-nanoid/v2"
"github.com/monkeyWie/gopeed/internal/fetcher"
"github.com/monkeyWie/gopeed/pkg/base"
"github.com/monkeyWie/gopeed/pkg/util"
"sync"
"time"
)
type Task struct {
ID string `json:"id"`
Res *base.Resource `json:"res"`
Opts *base.Options `json:"opts"`
Status base.Status `json:"status"`
Progress *Progress `json:"progress"`
Size int64 `json:"size"`
CreatedAt time.Time `json:"createdAt"`
fetcherBuilder fetcher.FetcherBuilder
fetcher fetcher.Fetcher
timer *util.Timer
lock *sync.Mutex
}
func NewTask() *Task {
id, err := gonanoid.New()
if err != nil {
panic(err)
}
return &Task{
ID: id,
Status: base.DownloadStatusReady,
CreatedAt: time.Now(),
}
}
func (t *Task) clone() *Task {
return &Task{
ID: t.ID,
Res: t.Res,
Opts: t.Opts,
Status: t.Status,
Progress: t.Progress,
Size: t.Size,
CreatedAt: t.CreatedAt,
}
}
-57
View File
@@ -1,57 +0,0 @@
package bt
import (
"crypto/rand"
"github.com/monkeyWie/gopeed-core/pkg/protocol/bt/metainfo"
"github.com/monkeyWie/gopeed-core/pkg/protocol/bt/torrent"
"net/url"
)
type Client struct {
PeerID [20]byte
// torrents []torrent.Torrent
}
func NewClient() *Client {
client := &Client{}
// 生成Peer ID,规则为前三位固定字母(-GP)+SemVer(xyz)+End(-),后面随机生成
// 参考:https://wiki.theory.org/index.php/BitTorrentSpecification#peer_id
peerID := [20]byte{'-', 'G', 'P', '0', '0', '1', '-'}
_, err := rand.Read(peerID[7:])
if err != nil {
panic(err)
}
client.PeerID = peerID
return client
}
func (client *Client) AddTorrent(rawurl string) error {
parse, err := url.Parse(rawurl)
if err != nil {
return err
}
var metaInfo *metainfo.MetaInfo
switch parse.Scheme {
case "magnet":
// TODO 磁力链接解析
default:
metaInfo, err = metainfo.ParseFromFile(rawurl)
break
}
if err != nil {
return err
}
torrent := torrent.Torrent{
// client: client,
MetaInfo: metaInfo,
}
// client.torrents = append(client.torrents, torrent)
torrent.Download("")
return nil
}
-21
View File
@@ -1,21 +0,0 @@
package bt
import (
"testing"
)
func TestClient_AddTorrent(t *testing.T) {
/*client := NewClient()
metaInfo, err := metainfo.ParseFromFile("testdata/Game.of.Thrones.S08E05.720p.WEB.H264-MEMENTO.torrent")
if err != nil {
panic(err)
}
torrent := &
torrent.MetaInfo.Announce = "udp://tracker.opentrackr.org:1337/announce"
torrent.MetaInfo.AnnounceList = [][]string{}
err = torrent.Tracker()
if err != nil {
panic(err)
return
}*/
}
Loaded 50 of 277 files, more files were not shown because too many files have changed in this diff. Show more