9

我的硬盘不可能这么空~aria2篇

 2 years ago
source link: https://blog.chih.me/download-use-aria2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

我的硬盘不可能这么空~aria2篇

0 Comments

对于单纯的Aria2程序来说,它是一个轻量级、支持多种协议的命令行下载工具。

  • 支持的协议包括HTTP(S), FTP, BitTorrent, Metalink
  • aria2可以并发的进行下载,并尝试将下载带宽利用率最大化
  • 分片与续传,自动中止并替换慢的线程

看到命令行可能有人会想:“恩,又是那种强大到用起来要查手册写命令的玩意”。其实不然,由于aria2 支持 JSON-RPC和XML-RPC远程调用,可以通过web前端或客户端方便地进行管理。

在Linux下,可以很方便地通过包管理进行安装,比如Arch Linux

$ sudo pacman -S aria2

如果是Windows,可以下载编译好的二进制文件,放在C:\WINDOWS\system32目录下,就可以通过aria2c命令执行程序了

可以直接运行命令进行下载:

  • HTTP下载

    $ aria2c http://example.org/test.bin

  • 从两个源同时下载

    $ aria2c http://example.org/test.bin ftp://example.org/test.bin

  • 用种子下载

    $ aria2c http://example.org/test.torrent

  • 用磁力链下载

    $ aria2c 'magnet:?xt=urn:btih:248D0A1CD08284299DE78D5C1ED359BB46717D8C'

每次下载都要打命令太麻烦,可以通过配置文件开启rpc server模式并启用图形界面,让它自动等待下载任务、自动下载、暂停或续传。 以下的配置文件参考YAAW作者的配置示例

参数 --dir --input-file --save-session 根据实际情况修改路径

#允许rpc
enable-rpc=true
#允许所有来源, web界面跨域权限需要
rpc-allow-origin-all=true
#允许非外部访问
rpc-listen-all=true
#RPC端口, 仅当默认端口被占用时修改
#rpc-listen-port=6800
#最大同时下载数(任务数), 路由建议值: 3
max-concurrent-downloads=5
#断点续传
continue=true
#同服务器连接数
max-connection-per-server=5
#最小文件分片大小, 下载线程数上限取决于能分出多少片, 对于小文件重要
min-split-size=10M
#单文件最大线程数, 路由建议值: 5
split=10
#下载速度限制
max-overall-download-limit=0
#单文件速度限制
max-download-limit=0
#上传速度限制
max-overall-upload-limit=0
#单文件速度限制
max-upload-limit=0
#断开速度过慢的连接
#lowest-speed-limit=0
#验证用,需要1.16.1之后的release版本
#referer=*
input-file=/home/USER/.config/aria2_web/session.lock
save-session=/home/USER/.config/aria2_web/session.lock
#文件保存路径, 默认为当前启动位置
dir=/home/chih/Downloads
#文件缓存, 使用内置的文件缓存, 如果你不相信Linux内核文件缓存和磁盘内置缓存时使用, 需要1.16及以上版本
#disk-cache=0
#另一种Linux文件缓存方式, 使用前确保您使用的内核支持此选项, 需要1.15及以上版本(?)
#enable-mmap=true
#文件预分配, 能有效降低文件碎片, 提高磁盘性能. 缺点是预分配时间较长
#所需时间 none < falloc ? trunc << prealloc, falloc和trunc需要文件系统和内核支持
file-allocation=prealloc
#启用本地节点查找
bt-enable-lpd=true
#添加额外的tracker
#bt-tracker=<URI>,…
#单种子最大连接数
#bt-max-peers=55
#强制加密, 防迅雷必备
#bt-require-crypto=true
#当下载的文件是一个种子(以.torrent结尾)时, 自动下载BT
follow-torrent=true
#BT监听端口, 当端口屏蔽时使用
#listen-port=6881-6999

我把配置文件放在了~/.config/aria2_web目录下

$ mkdir -p ~/.config/aria2_web
$ vim ~/.config/aria2_web/aria2.conf

同时在目录下创建会话文件,未完成任务会保存在这里

$ touch ~/.config/aria2_web/session.lock

好了,可以通过aria2c --conf-path=~ /.config/aria2_web/aria2.conf进行配置文件的测试。

现在我希望可以让aria2自动开启,并可以方便地进行进程管理,我使用了systemd,当然,supervisor也是可以达到类似效果。

$ vim ~/.config/systemd/user/aria2.service

内容可以是这样:(感谢jahiyw指出错误: systemctl --user 的启动脚本应该把 WantedBy 设置成 default.target,而不是 multi-user.target,因为以 --user 方式启动不存在 multi-user.target,所以,systemctl --user enable 了之后,永远都不会自动启动)

[Unit]
Description=Aria2 Service
After=network.target

[Service]
ExecStart=/usr/bin/aria2c  --conf-path=%h/.config/aria2_web/aria2.conf

[Install]
WantedBy=default.target

通过以下命令管理:

$ systemctl --user start aria2
$ systemctl --user stop aria2
$ systemctl --user restart aria2
$ systemctl --user status aria2
$ systemctl --user enable aria2

注意: --user这个选项不要忘了,也不需要sudo,在这里用的是systemd给用户生成的systemd实例,具体可以见这里

YAAW是Binux写的一个Aria2 Web管理前端,直接用web服务器(Nginx, Apache)搭就是了,如果用的是上面的aria2配置,直接访问YAAW就可以管理aria2了。

附上我的Nginx配置:

server {
    listen       80;
    server_name  yaaw.site;

    location / {
        root   /opt/yaaw;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

一些支持导出到aria2 RPC的chrome扩展

迅雷离线 百度网盘 115网盘


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK