跳到主内容

在 VPS 上配置 Aria2 RPC 服务并将文件同步至坚果云 webdav

Aria2 RPC 配置

aria2-rpc.conf (源文件)

################################ Aria2 配置文件 ################################
#       需要保存为 ~/.aria2/aria2.conf                                         #
#       或者在运行时通过 --conf-path 选项指定                                  #
################################################################################

############################## RPC 以及安全性设置 ##############################
# 启用 RPC 服务
enable-rpc=true
# 允许接受所有来源的 RPC 请求
rpc-allow-origin-all=true
# 接受非本地请求
rpc-listen-all=true
# RPC 服务所绑定的端口
rpc-listen-port=6800
# 不保存通过 RPC 上传的种子/元数据文件
rpc-save-upload-metadata=false
# rpc 密钥(旧的 rpc-user,passwd 已被废弃)
# 在客户端需要附带此密钥才接受请求
#rpc-secret=

##################################### 速度 #####################################
# 最大并发下载量
max-concurrent-downloads=5
# 一次下载任务最多向服务器同时建立的连接数
max-connection-per-server=5
# 最小文件分片大小, 下载线程数上限取决于能分出多少片, 对于小文件重要
min-split-size=2M
# 单文件最大线程数
split=4
# 全局下载速度限制,0 表示不限制
max-overall-download-limit=0
# 单文件下载速度限制
max-download-limit=0
# 上传速度限制
max-overall-upload-limit=0
# 单文件上传速度限制
max-upload-limit=0
# 断开速度过慢的连接
lowest-speed-limit=0

################################# 进度保存相关 #################################
input-file=/data/cache/session.txt
save-session=/data/cache/session.txt
# 定时保存会话的时间间隔(秒),需要1.16.1之后的release版
save-session-interval=60
# 自动保存 .aria2 控制文件的时间间隔(秒)
# 设为 0 则不会保存
#auto-save-interval=
# 当启动 aria2 时,是否从上次保存的会话继续下载
continue=true

################################### 本地文件 ###################################
# 下载文件保存路径, 默认为当前启动位置
dir=/data/download
# 磁盘缓存,需要 1.16 以上版本
# 其作用是减少磁盘 IO 频率
disk-cache=64M
# 启用 mmap ,需要 1.15 以上版本
enable-mmap=true
# 文件预分配, 能有效降低文件碎片, 提高磁盘性能. 缺点是预分配时间较长
# none      不会进行预分配
# prealloc  在下载前预分配
# falloc    在下载前预分配,但不会阻塞;需要文件系统支持 posix_fallocate(3) 函数
#           目前 ext4(extended) btrfs xfs ntfs(mingw built) 支持
# trunc     让操作系统将对应文件截断至指定大小,但这是仅仅是修改文件系统元数据,
#           而无法避免磁盘碎片
# 所需时间 none < trunc < falloc << prealloc
file-allocation=falloc

################################### HTTP设置 ###################################
# 通过 HTTP(S) 下载时的 referer 头字段的设置
# *     将下载地址作为 referer 头
referer=*
# 加载 cookies,支持以下三种格式:
# - Firefox3 format (SQLite3)
# - Chromium/Google Chrome (SQLite3)
# - Mozilla/Firefox(1.x/2.x)/Netscape
# 通常将此字段设置为浏览器所使用的 cookie 数据库文件
# 需要 aria2 在编译时链接了 sqlite3 库
#load-cookies=~/.aria2/cookies.sqlite
# 用户身份,通常设置为浏览器使用的 User-Agent,以下载某些只允许浏览下载的网站
# 可以在 https://github.com/fengzhizi715/user-agent-list 获取各浏览器使用的 UA
user-agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'

#################################### BT相关 ####################################
# 不将仅做种的任务计入总任务数限制
bt-detach-seed-only=true
# 启用本地节点查找
bt-enable-lpd=true
# 强制加密, 拒绝旧 BT 协议握手,防迅雷必备
bt-require-crypto=true
# 在下载磁力链接时,先检查本地元数据缓存,如果不存在再向 DHT 请求
bt-load-saved-metadata=true
# 单种子最大连接数
bt-max-peers=0
# 通过磁力链接下载时,将元数据保存为 torrent 文件
bt-save-metadata=true
# 添加额外的tracker
# https://trackerslist.com/all_aria2.txt 提供了大量可用 tracker 地址
#bt-tracker=<uris>
# 启用 DHT 网络
enable-dht=true
enable-dht6=true
# dht 数据文件保存地址
dht-file-path=/data/cache/dht.dat
dht-file-path6=/data/cache/dht6.dat
# 当下载的文件是一个种子(以.torrent结尾)时, 自动下载BT
follow-torrent=true
# BT监听端口, 当端口屏蔽时修改
#listen-port=6881-6999
# 设置全局最大 BT 上传速度
max-overall-upload-limit=4M
# BT 下载中,该端点名称前缀
peer-id-prefix=A2-1-35-0-
peer-agent=aria2/1.35.0
# 分享率,当上传达到此比率则停止做种
seed-ratio=2.0
# 即便任务已完成或被移除,也保存 .aria2 控制文件,这对识别下载完成的种子很有帮助
# 默认情况下,控制文件将在下载完成后删除
force-save=false
# 对于继续种子下载,通过检验和而非控制文件方式获取下载状态
check-integrity=true

################################### 事件钩子 ###################################
#       事件钩子的参数都是一个指向 Bash 或批处理脚本文件的路径                 #
#       当事件发生时,该脚本被调用并按顺序传入三个参数                         #
#                                                                              #
#       -   GID                                                                #
#       -   文件数目                                                           #
#       -   文件路径,如果有多个文件,则只会传递第一个,                       #
#           要获取更多文件路径,需要 RPC                                       #
################################################################################
# 在 BT 下载完整(Hash 检查通过)后运行
#on-bt-download-complete=/data/script/none.sh
# 在 HTTP(S) 和 FTP 下载完成后运行
#on-download-complete=/data/script/none.sh
# 下载任务因错误而终止
#on-download-error=/data/script/none.sh
# 下载任务被主动暂停
#on-download-pause=/data/script/none.sh
# 下载任务开始前
#on-download-start=/data/script/none.sh
# 下载任务结束后,可以作为 complete 和 error 两个事件的 fallback
#on-download-stop=/data/script/none.sh

WebDav hook

Rclone 1 是个好软件,除了 WebDav 以外,还支持一大票网盘和对象存储服务。如果不出意外的话,之后会介绍的挂载 OneDrive 也会用它。

例如,配置坚果云的 WebDav,我们先在坚果云的网盘上创建一个单独的文件夹,例如命名为 vps-download 好了,然后创建一个新的 WebDav 角色,也命名为 vps-download, 将会得到一个应用密码,之后用 passwd 表示它。这一配置位于网页端的 『账户信息』『安全选项』里面。

那么,连接坚果云的三个要素就确定了:

URL 地址:

https://dav.jianguoyun.com/dav/vps-download

用户账户

登录坚果云的用户账户,不是应用账户。

应用密码

上述步骤所创建的应用密码 passwd。

可以通过 rclone config 命令来进行交互式的配置,也可以直接编辑位于 ~/.config/rclone/rclone.conf 的配置文件。 每一个表对应一个 remote,推荐在创建新 remote 时不要在名字中加空格,因为在后期还需要在命令行中使用:

[JianGuoDav]
type = webdav
url = https://dav.jianguoyun.com/dav/vps-download
vendor = other
user = 用户账户
pass = 散列后的应用密码

推荐在本地配置好后将 rclone 配置文件上传到 VPS。

在配置好后,就可以使用命令:

rclone copy example.txt JianGuoDav:/

将本地的 example.txt 上传到远程了,远程的根目录是从 /vps-download 开始计算的,如果没有指定文件名,则使用与源文件相同的文件名。 由于 BT 的多文件下载比较复杂,因此后续再处理,这里只处理普通下载文件和 Bt 单文件的上传:

# on download complete
gid=$1
fnum=$2
fpath=$3
now=$(date '+%Y-%m-%d %H:%M:%S')
log=/data/log/aria2-webdav.log
# 通过 rclone 上传
rclone copy "$fpath" "JianGuoDav:/"
if [[ $? -ne 0 ]]; then
    echo "[$now] WARNING rclone copy $fpath failed" >> $log
else
    echo "[$now] INFO save $fpath, numbers: $fnum" >> $log
fi

OneDrive Hook

首先,你得在 安装了浏览器 的日常使用主机上安装 rclone,这样才能弹出一个页面进入 OneDrive 网站上获取授权。

在 Windows 系统上用 scoop install rclone 安装了 rclone 后,运行 rclone authorize onedrive ,然后会弹出浏览器窗口,进入微软登录页面用 OAuth2 验证。

输入你的微软帐号密码登录后,rclone 就取得了微软授权,在终端中显示授权码。这个授权码可以保存下来给其他 rclone 程序使用(例如我们要作为下载服务器的主机)。

# rclone authorize onedrive
2020/02/02 00:22:07 NOTICE: Config file "C:\\Users\\zom\\.config\\rclone\\rclone.conf" not found - using defaults
If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=_yOQ6xvaun9P7FSOIj-2aw
Log in and authorize rclone for access
Waiting for code...
Got code
Paste the following into your remote machine --->
{"access_token":"EwCAA8l6BAAUO9**************************************************************防止泄漏************************************************************************DAs$","expiry":"2020-02-02T01:27:49.2021085+08:00"}
<---End paste

可以看到, rclone 用 JSON 配置它自己,并且从 expiry 字段读取到,这个授权码存在一个有效期,过期后不可用。上面的 paste 部分保存下来,命名为 onedrive.json。

执行 rclone config 进入配置流程,下面会先介绍输入,然后展示当时的终端情况。

  1. rclone 询问你的意图

我们选择 n,新建一个 remote。

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> n
  1. 设置新 remote 的名字

随便起一个就好,例如,因为 OneDrive 是微软家的,就取名叫 ms 了。

name> ms
  1. 选择服务提供方

选择 22 ,微软 OneDrive。

注意,如果 rclone 版本不同,编号可能不一样,记得看准了选, 其他用不到的部分已被略去。

Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
21 / Microsoft Azure Blob Storage
   \ "azureblob"
22 / Microsoft OneDrive
   \ "onedrive"
23 / OpenDrive
   \ "opendrive"
storage> 22

特别说明一下, drive_id 是微软给你的 OneDrive 帐号分配的 ID,可以在网页登录 OneDrive 时从 URL 中获取.

  1. 询问 Microsoft App Clinet ID,由于没有,所以留空。

** See help for onedrive backend at: https://rclone.org/onedrive/ **

Microsoft App Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>
  1. Microsoft App Client Secret,同样留空。

Microsoft App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret>
  1. 询问是否进阶编辑,选择是。

Edit advanced config? (y/n)
y) Yes
n) No
y/n> y
  1. Chunk Size,保持默认即可。

Chunk size to upload files with - must be multiple of 320k (327,680 bytes).

Above this size files will be chunked - must be multiple of 320k (327,680 bytes). Note
that the chunks will be buffered into memory.
Enter a size with suffix k,M,G,T. Press Enter for the default ("10M").
chunk_size>
  1. 是否自动配置 remote,选择 n,因为远程服务器没有浏览器。

Remote config
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine
y) Yes
n) No
y/n> n
  1. 输入我们之前获取的 onedrive.json 文件内容。

For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine (same rclone version recommended) :
       rclone authorize "onedrive"
Then paste the result below:
result> {******* 敏感信息,已隐藏 ********}
  1. 选择 OneDrive 服务类型,个人版就选 1,persional 或 bussiness 这一类。

Choose a number from below, or type in an existing value
 1 / OneDrive Personal or Business
   \ "onedrive"
 2 / Root Sharepoint site
   \ "sharepoint"
 3 / Type in driveID
   \ "driveid"
 4 / Type in SiteID
   \ "siteid"
 5 / Search a Sharepoint site
   \ "search"
Your choice> 1
  1. rclone 通过之前的配置,查询到微软服务器上你的 OneDrive 帐号对应的 ID,让你选择

如果买了多个 OneDrive 计划的话,可能有多种选择,选其中一个方便的就好。

Found 1 drives, please select the one you want to use:
0:  (personal) id=c***********0
Chose drive to use:> 0
  1. 配置基本完成,接下来一路 y 过去就好了,问的问题都是「你的配置是不是这样?」、「还要继续配置其他 remote 吗?」这样的问题。

然后将生成的 ~/.config/rclone/rclone.conf[ms] 表下的内容上传到 VPS 即可使用。

事件钩子和 WebDav hook 一样,只不过将远程修改一下即可。