在外网访问家里的树莓派下载机

最近用上了Aria2作为下载器,一开始是在笔记本上装的Aria2,但是需要较长时间的下载任务就得保持笔记本一直开着,于是就在树莓派上装了Aria2。在Mac端的管理用 Maria / YAAW都挺不错。

然而不在家里的时候,要想添加下载任务或是查看进度似乎比较麻烦,曾经想要写个Aria2下载完成后发邮件的东西,但是觉得太麻烦;又想用node.js在公网的服务器上给家里的树莓派做个代理,但是自己写这样的,好像……更复杂了啊……

后来想起了以前做iOS越狱开发时的SSH over USB,用SSH做类似的forwarding应该能达到要求。当然,这需要有一台可以在公网访问的服务器。

首先需要在ssh中打开

GatewayPorts clientspecified

关于GatewayPorts,man手册中的介绍如下,

     GatewayPorts
             Specifies whether remote hosts are allowed to connect to ports
             forwarded for the client.  By default, sshd(8) binds remote port
             forwardings to the loopback address.  This prevents other remote
             hosts from connecting to forwarded ports.  GatewayPorts can be
             used to specify that sshd should allow remote port forwardings to
             bind to non-loopback addresses, thus allowing other hosts to con-
             nect.  The argument may be ``no'' to force remote port forward-
             ings to be available to the local host only, ``yes'' to force
             remote port forwardings to bind to the wildcard address, or
             ``clientspecified'' to allow the client to select the address to
             which the forwarding is bound.  The default is ``no''.

接下来,我们梳理一下需要在哪些端口进行什么通信。

公网服务器端口 树莓派端口 作用
2222 22 将树莓派的22端口映射到公网服务器的2222端口,以便在公网ssh到树莓派上
8080 80 将树莓派的80端口映射到公网服务器的8080端口,这样就可以使用树莓派上的YAAW(当然你在外网的电脑上设置好相应的jsonrpc也行,但可能会有不方便下载yaaw的时候)
6801 6800 将树莓派的6800端口映射到公网服务器的6801端口,这个是真正与树莓派上的Aria2通信的端口

后文为了方便放代码,公网服务器的IP地址用$server_ip,公网服务器的用户名使用$server_user,对应的,树莓派上的用户名使用$pi_user

那么首先确保这三个端口(2222, 8080, 6801)在服务器上是未被使用的(如果已经使用了,可以根据自身情况替换),并且可以通过防火墙等。

在服务器上执行

iptables -A INPUT -i eth0 -p tcp --dport 2222 -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --dport 2222 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8080 -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --dport 8080 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 6801 -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --dport 6801 -j ACCEPT

然后为了完成树莓派上22端口到服务器上2222端口的映射,我们需要在树莓派上执行

ssh -4 -p 22 -fN -o "PubkeyAuthentication=yes" -o "PasswordAuthentication=yes" -o "StrictHostKeyChecking=false" -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" -R $server_ip:2222:localhost:22 $server_user@$server_ip

对以上命令的说明如下,

arg 说明
-v 4 使用IPv4
-p 服务器上sshd运行的端口
-f 认证之后将 ssh 转到后台运行
-N 不执行脚本或命令
-o ssh的选项
"PubkeyAuthentication=yes" 免密码登录SSH
"PasswordAuthentication=yes" 允许密码登录SSH
"ServerAliveInterval=60" 避免自动断开,每60秒发送一个heartbeat包
"ServerAliveCountMax 3" 发送heartbeat包失败的次数限制
-R 远程端口转发

如果没有意外的话,你会发现在其他任意机器上执行,

ssh $pi_user@$server_ip

并输入正确的树莓派用户的密码之后,是ssh到树莓派上的。

接下来我们在服务器上执行,

ssh -N -p 2222 -f $pi_user@$server_ip -L*:8088:localhost:80 -n /bin/bash
ssh -N -p 2222 -f $pi_user@$server_ip -L*:6801:localhost:6800 -n /bin/bash

输入正确的树莓派用户的密码后,我们的ssh转发就完成了。

假设YAAW在本地网络上是http://raspberrypi.local/yaaw,那么现在用任意设备访问http://$server_ip:8080/yaaw就可以看到了,然后在settings里设置jsonrpc地址为http://$server_ip:6801/jsonrpc即可。

Maria的设置类似,

maria

最后,为了阻止其他用户访问,可以在树莓派上用.htaccess来限制yaaw及其子目录只能在登录后访问(如果你选择使用YAAW,并且树莓派上的服务器是Apache的话),然后在Aria2里也需要设置rpc-secret。

设置了rpc-secret之后,jsonrpc的地址类似http://token:$secret@$server_ip:6801/jsonrpc

One thought on “在外网访问家里的树莓派下载机”

Leave a Reply

Your email address will not be published. Required fields are marked *

sixteen − eight =