1 aria2 安装与配置
aria2 是一个功能完善的下载软件,最大的缺点是软件本身没有图形界面,大大增加了配置难度(这对玩树莓派的人应该没啥影响 LOL)。这个我们后面解决,第一步要做的肯定是安装 aria2。
sudo apt-get install aria2
建立配置文件
$ mkdir ~/.config/aria2/
$ touch ~/.config/aria2/aria2.session
$ vim ~/.config/aria2/aria2.config
如果不熟悉 vim,可以用 nano 替代最后一行的 vim。
配置文件参考https://github.com/P3TERX/aria2.conf
在新建的 aria2.config 中输入以下内容。
daemon=true
rpc-secret=beekc
enable-rpc=true
rpc-allow-origin-all=true
rpc-listen-all=true
max-concurrent-downloads=5
continue=true
max-connection-per-server=5
min-split-size=10M
split=10
max-overall-download-limit=0
max-download-limit=0
dir=/home/pi/Downloads
file-allocation=prealloc
check-certificate=false
save-session=/home/pi/.config/aria2/aria2.session
input-file=/home/pi/.config/aria2/aria2.session
save-session-interval=60
enable-dht=true
bt-enable-lpd=true
enable-peer-exchange=true
bt-tracker=udp://tracker.coppersurfer.tk:6969/announce,udp://9.rarbg.to:2710/announce,udp://9.rarbg.me:2710/announce,udp://tracker.openbittorrent.com:80/announce,udp://tracker.leechers-paradise.org:6969/announce,udp://tracker.opentrackr.org:1337/announce,udp://tracker.internetwarriors.net:1337/announce,udp://exodus.desync.com:6969/announce,udp://tracker.tiny-vps.com:6969/announce,udp://retracker.lanta-net.ru:2710/announce,udp://open.demonii.si:1337/announce,udp://open.stealth.si:80/announce,udp://bt.xxx-tracker.com:2710/announce,udp://tracker.torrent.eu.org:451/announce,udp://tracker.cyberia.is:6969/announce,udp://denis.stalker.upeer.me:6969/announce,udp://torrentclub.tech:6969/announce,udp://tracker.moeking.me:6969/announce,udp://tracker3.itzmx.com:6961/announce,udp://explodie.org:6969/announce
第二行是 rpc 密码,后面在使用 AriaNg 时需要填写。
最后一部分是 bt-trcker 是 tracker 服务器,用来帮助寻找做种的用户。可以使用网站 http://www.tkser.tk/ ( 备用 http://edam.top/tk/ ) 进行更新。
如果使用 HTTPS,在配置文件中加入
rpc-secure=true
rpc-certificate=/path/to/cer
rpc-private-key=/path/to/key
至此已经可以使用命令行控制 aria2 进行下载操作了,但是这样不是很方便。下一节中将介绍使用 AriaNg 搭建图形化的控制界面。
2 安装 apache 与 AriaNg
Aria 自身没有图形界面,先需要搭建一个 web 服务器。这里使用的是 apache ,如果熟悉使用其他软件也可以。首先安装 apache 软件。
sudo apt-get install apache2
这时打开浏览器,在地址栏输入树莓派的地址就可以看到 apache 的默认网页。这说明 web 服务器已经可以正常工作了。默认网页存储在 /var/www/html ,只要把它替换成 AriaNg 就可以了。
在 https://github.com/mayswind/AriaNg/releases/ 上下载最新版本的 AriaNg,然后解压到 /var/www/html 中,并删除原来的默认网页。再在浏览器中访问时就会出现 AriaNg 的界面了。
现在访问 AriaNg 会报错,因为还没有输入 rpc 密码。点击 AriaNg 设置 — RPC — Aria2 RPC 密钥 中填入上节中设定的密码。然后就可以全部的功能了。
3 samba
使用 samba 可以像管理本地磁盘一样管理远程设备,帮助我们将下载的文件拷到本机中。先安装 samba 软件。
sudo apt-get install samba samba-common-bin
配置 samba 用户密码
sudo touch /etc/samba/smbpasswd
smbpasswd -a pi
建立 samba配置文件
sudo vim /etc/samba/smb.conf
在其中输入下面内容
[share]
path = /home/pi
available = yes
browseable = yes
#public = yes
writable = yes
最后重启 samba 服务,就可以通过 samba 访问树莓派了。
3.2 开机自启动
创建systemctl service文件
sudo vim /lib/systemd/system/aria2.service
User,conf-path下换成自己的username
[Unit]
Description=Aria2 Service
After=network.target
[Service]
User=ubuntu
ExecStart=/usr/bin/aria2c --enable-rpc --rpc-listen-all --rpc-allow-origin-all --save-session /home/pi/.config/aria2/aria2.session --input-file /home/pi/.config/aria2/aria2.session --conf-path=/home/pi/.config/aria2/aria2.conf
[Install]
WantedBy=default.target
重载服务并设置开机启动
sudo systemctl daemon-reload
sudo systemctl enable aria2
sudo systemctl start aria2
sudo systemctl status aria2
看到如下文字证明启动成功(记住TCP port,AiraNg配置以及公网端口映射需要)
● aria2.service - Aria2 Service
Loaded: loaded (/lib/systemd/system/aria2.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2018-12-29 08:24:20 CST; 4 days ago
Main PID: 21855 (aria2c)
CGroup: /system.slice/aria2.service
└─21855 /usr/bin/aria2c --conf-path=/home/pi/.config/aria2/aria2.config
Dec 29 08:24:20 ubuntu systemd[1]: Started Aria2 Service.
Dec 29 08:24:20 ubuntu aria2c[21855]: 12/29 08:24:20 [NOTICE] IPv4 RPC: listening on TCP port 6800
Dec 29 08:24:20 ubuntu aria2c[21855]: 12/29 08:24:20 [NOTICE] IPv6 RPC: listening on TCP port 6800
Dec 29 08:25:21 ubuntu aria2c[21855]: 12/29 08:25:21 [NOTICE] Serialized session to '/home/ubuntu/.config/aria2/aria
4.1 将AriaNg部署在Raspberry Pi上
配置Tomcat服务器
首先需要一个web服务器,这里使用了Tomcat 7。(这里纯属个人喜好,用Apache或Nginx看个人需要)
wget https://www-eu.apache.org/dist/tomcat/tomcat-7/v7.0.92/bin/apache-tomcat-7.0.92.tar.gz
tar -xzvf apache-tomcat-7.0.92.tar.gz
mv apache-tomcat-7.0.92/ tomcat7
vim ./tomcat7/bin/catalina.sh
修改此启动配置文件,JAVA路径添加默认java路径,此配置中的java路径为sudo apt-get install default-jre安装的默认路径。
# JAVA_HOME Must point at your Java Development Kit installation.
# Required to run the with the "debug" argument.
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-arm64
# JAVA_OPTS (Optional) Java runtime options used when any command
# is executed.
# Include here and not in CATALINA_OPTS all options, that
# should be used by Tomcat and also by the stop process,
# the version command etc.
# Most options should go into CATALINA_OPTS.
JAVA_OPTS="-server -Xms512m -Xmx1024m -XX:PermSize=600M -XX:MaxPermSize=600m -Dcom.sun.management.jmxremote"
修改服务器文件配置文件:
vim ./tomcat7/conf/server.xml
将
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改为
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443" />
即可使用默认http 80 端口
vim ./tomcat7/bin/start.sh
在浏览器中访问http://your-ip/即可打开Apache Tomcat的默认webpage了。(如下图)
配置Tomcat自启动
vim /etc/rc.local
在文件中添加
sudo /home/ubuntu/tomcat7/bin/startup.sh
配置AriaNg
wget https://github.com/mayswind/AriaNg/releases/download/1.0.0/AriaNg-1.0.0.zip
unzip AriaNg-1.0.0.zip -d aira-ng
sudo mv aira-ng ~/tomcat7/webapps/ROOT/
在浏览器中访问http://your-ip/aira-ng即可打开AriaNg了。
4.2 本地使用AriaNg
网页版
直接下载AriaNg,打开index.html
在AiraNg Settings中,选择RPC选项卡,将Aira2 RPC Address中IP地址和端口号修改为Raspberry Pi的IP地址以及前面status中的端口号(默认6800)。
桌面版
直接下载AriaNg-Native,打开,设置同上。
5.1 DMZ主机
DMZ主机是目前绝大多数路由器都会带的功能,其功能就是实现将内网设备直接暴露在公网之上。
移动光猫配置页面(需要超级帐号):
TP-Link配置页面:
5.2 端口映射
如果不想要把所有的端口都暴露在公网上(这样做风险较大),可以只将其中的几个端口映射到公网上。
这里使用移动光猫的设置举例:
这样配置好访问路由器的公网IP地址以及绑定的端口号就可以直接访问到Raspberry Pi的相应端口。
5.3 获取公网IP地址
访问Raspberry Pi还需要获取公网IP地址,而路由的公网IP地址有可能会变化。
可以访问 https://www.ipchicken.com/ https://www.iplocation.net/find-ip-address 等网站获取本机的IP地址 这里提供一个(粗鄙)的Python获取IP地址发送邮件的脚本(Python 3.6, 2.7上没尝试过)
import requests, re
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
def _format_addr(s):
name, addr = parseaddr(s)
return formataddr((name, addr))
if __name__ == '__main__':
# get webpage
url = "https://www.ipchicken.com/"
r = requests.get(url)
# get IP address from webpage
pattern = re.compile(r'((25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)')
matchObj = pattern.search(r.text)
IpAddress = matchObj.group(0)
# send email
# email (modify by yourself)
from_addr = Your_email_address
password = Your_password
to_addr = ['Your_another_email_address']
smtp_server = 'your.smtp.server'
smtp_port = 25
# message
msg = MIMEText(''+IpAddress, 'plain', 'utf-8')
msg['Subject'] = Header('Raspberry Pi Ip Address', 'utf-8').encode()
msg['From'] = _format_addr('Raspberry Pi <%s>' % from_addr)
msg['To'] = _format_addr('Raspberry Pi Manager <%s>' % to_addr[0])
# send
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.login(from_addr, password)
server.sendmail(from_addr, to_addr, msg.as_string())
print("Send Email Success")
except smtplib.SMTPException:
print("Error: Unable to send Email")
server.quit()
5.4 花生壳内网穿透
如果家中用的路由是二级路由,那么恭喜,无论是DMZ主机还是端口映射都没有用了。
二级路由就是说你是在内网的内网中,如果路由器的公网地址和从https://www.ipchicken.com/上获取的IP地址不一样就说明是二级路由。
这种也可以有个(不需要VPS的)方法解决,就是利用花生壳的内网穿透功能。目前这个功能需要6元钱。
花生壳官网上有详细的说明,这里就不赘述了。测试功能支持映射两个端口,这里需要将80和Aria2 RPC的端口(默认6800)映射出去即可。
花生壳会提供一个服务域名,这个域名就相当于公网IP地址。
5.5 利用Frp内网穿透
如果有条件(比如有一个VPS),可以通过Frp作反向代理实现内网穿透。
安装参见Frp 的 GitHub page,有很详细的说明,这里不赘述了。
服务器端使用frps 和配置文件frps.ini如下:
[common]
bind_addr = 0.0.0.0
bind_port = 5443
log_file = ./frps.log
log_level = info
log_max_days = 3
# auth token
token = [your token]
max_pool_count = 50
tcp_mux = true
Raspberry Pi端使用frpc 和配置文件frpc.ini如下(ip填写服务器的ip,token和服务器端保持一致):
除了aria2的6800端口,还可以将其他端口如ssh,http映射到服务器ip上。
[common]
server_addr = [your ip]
server_port = 5443
token = [your token]
tcp_mux = true
[aria2]
type = tcp
local_ip = 127.0.0.1
local_port = 6800
remote_port = 6800
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 4022
[http]
type = tcp
local_ip = 127.0.0.1
local_port = 80
remote_port = 8080
手动挂载的方法如下:
sudo fdisk -l
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000d2ae0
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 976769023 976766976 465.8G 7 HPFS/NTFS/exFAT
或
sudo blkid
/dev/mmcblk0p1: SEC_TYPE="msdos" UUID="9FF3-2ADB" TYPE="vfat" PARTUUID="c2909e2d-01"
/dev/mmcblk0p2: UUID="a407e17a-e170-45d8-8fb7-dd34fe646ab9" TYPE="ext4" PARTUUID="c2909e2d-02"
/dev/mmcblk0: PTUUID="c2909e2d" PTTYPE="dos"
/dev/sda1: LABEL="New Volume" UUID="848CC9268CC91398" TYPE="ntfs" PARTUUID="000d2ae0-01"
从中可以获取到硬盘的盘符/dev/sda1
sudo mkdir ~/share/disk1
sudo mount /dev/sda1 ~/share/disk1
如果需要重启机器,则需要采用自动挂载的方式:
sudo vim /etc/fstab
加入如下一条
UUID=848CC9268CC91398 /home/ubuntu/share/disk1 ntfs defaults 0 3
执行如下命令验证
sudo mount -a
需要创建一个samba帐号
sudo touch /etc/samba/smbpasswd
smbpasswd -a pi
再输入两次密码后即可创建账户
sudo vim /etc/samba/smb.conf
在文件末尾加入如下信息
[share]
path = /home/pi
available = yes
browseable = yes
#public = yes
writable = yes
修改读写权限
sudo chown -R pi:pi /home/pi
sudo chmod -R 777 /home/pi
重启samba服务
sudo service smbd restart
即可进行连接测试(在运行窗口中输入\ip即可访问)
First, you need to login to your Raspberry pi via ssh. Then change your working directory to home.
cd /home/pi/
Install some dependencies. We need to use git, tmux, python3, and aria2.
```key
sudo apt update
sudo apt install git tmux python3 aria2
Download webui-aria2 to the current working directory. We can simply clone it with git.
git clone https://github.com/ziahamza/webui-aria2.git
Start aria2 as tmux session. This will run aria2 in background and listen to RPC protocol. By default the RPC will listen 6800 port. Let’s leave it to that.
tmux new -d \
-c /home/pi/ \
-s aria2rpc \
'aria2c --enable-rpc --rpc-listen-all' \;
Start webui-aria2 as tmux session, too. We simply use python3 built-in HTTP server. This also will run the http server in background. We use port 8600 for HTTP server.
tmux new -d \
-c /home/pi/webui-aria2/docs/ \
-s aria2webui \
'python3 -m http.server 8600' \;
That’s it, now open your Raspberry pi IP address with port 8600 from your browser. You can find out your Raspberry Pi ip address with ifconfig.
Run at startup
The solution above is enough assuming you never turn off your Raspberry pi. But it’s mostly not the case. You’re going to need to restart it once in a while. Or maybe there will be a sudden power failure. You never know.
To run the services in background, we need to create a shell script. Let’s call the script run-aria2.sh. You can edit it with nano, vim, or any text editor.
nano /home/pi/run-aria2.sh
The content is similar with previous section. Here is the command I write on it.
#!/bin/sh
# Start aria2 RPC server
tmux new -d -c /home/pi/ -s aria2rpc 'aria2c --enable-rpc --rpc-listen-all' \;
# Start webui-aria2 HTTP server
tmux new -d -c /home/pi/webui-aria2/docs/ -s aria2webui 'python3 -m http.server 8600' \;
Next thing is to make it executable with chmod.
chmod +x run-aria2.sh
In Raspbian OS, you can start a script immediately after finished booting by writing it in /etc/rc.local. So we need to execute previous shell script there. Open the file with your favorite text editor.
sudo nano /etc/rc.local
Make sure put the following line before exit 0.
su pi -c ‘/home/pi/run-aria2.sh &’
If you’re worry that you write it wrong, here is my rc.local content looks like.
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
su pi -c '/home/pi/run-aria2.sh &'
exit 0
Now restart your Raspberry pi to check if it works.
sudo reboot
After your Raspberry pi done restarting, it should be running in background. That’s it, enjoy your download manager!