如何在树莓派上使用STUN/ICE实现NAT穿透

6
我正在尝试在我的树莓派上设置一个实现,能够穿透NAT路由器,以便我可以远程连接它,无论它位于哪个网络背后。
我已经尝试了pagekite.me,这是一个很好的中继/反向隧道解决方案,但我觉得它仍然有点笨重(由于还需要连接另一台服务器,存在延迟问题)。
我已经了解了STUN和ICE,但我不知道有哪些解决方案可以在我的树莓派上实现。
目标是,我可以通过SSH进入我的树莓派,无论其网络配置(路由器/网络)和网络防火墙如何。
有人能指点我应该寻找什么或者去哪里寻找吗?

我也想在SSH中使用STUN/TURN/ICE……这方面有进展吗? - Zibri
1个回答

2
我不直接回答你的STUN/ICE问题,而是回答你的目标,即如何突破NAT并远程ssh到您的树莓派。
最简单的解决方法是使用反向ssh隧道,特别是使用autossh和基于密钥的身份验证。
这需要您拥有自己的服务器,并在某个地方具有一个用于pi呼叫的ssh端口(我使用一个只需坐在家庭网络上的服务器,找出您的公共IP地址,如果您想使用易记URL,请注册免费的ddns帐户)。
准备您的树莓派,最好在您自己的家庭网络中与您将要保留和使用的服务器一起(只需连接运行Linux或其他pi的旧台式机到路由器并保持开启即可。对于此示例,我假设您已将外部端口30022转发到您家庭路由器上的服务器的22端口)。您还将使用基于密钥的身份验证。
在您的树莓派上:
sudo apt-get install autossh
# Generate key
sudo -u pi ssh-keygen
# Copy key to your server (while you're on your home network with the server is easiest, but not necessary)
sudo -u pi ssh-copy-id -i /home/pi/.ssh/id_rsa.pub [serverUser]@[serverIP]

如果您愿意,您可以在树莓派的主目录中创建另一个名为myConf.sh的“配置文件”。

#!/bin/bash
rSSHPort=31001 # you'll use a different port for each pi you connect to your server.  Make sure all these ports are forwarded on your home router to the server (port forward the range e.g. 31000-31100 would let you do 100 pi's)
# Phone Home
USER=pi # or whatever your pi user is named
KEY=/home/pi/.ssh/id_rsa
HOST=myServer.ddns.net # or whatever your server URL or public IP address is 
REMOTE_USER=serverUser # or the user you want your pi to connect to the server as
REMOTE_PORT=30022 # or whatever port you have forwarded to your server for ssh (don't use 22 as its even more of a security vulnerability). 

您需要一个最终的脚本来实现与服务器的SSH连接。如果您愿意,可以将其命名为connectServer.sh,并将其放置在/home/pi/目录中。请注意,此脚本将执行实际的SSH调用。
#!/bin/bash
# Reverse Tunnel SSH in to server
# -f detach script from terminal
# -N no commands can be executed on server side
# -R reverse tunnel
# -p using server ssh port
# -i path to key file
# Source the Config File
source '/home/pi/myConf.sh'

connect()
{
        # TODO need to check that autoSSH isn't already in process list
        autoSSHProc=$( ps ax | grep autossh | wc -l )
        if [ "$autoSSHProc" -le "1" ]
        then
                log
                su -c "autossh -f -N -q -i ${KEY} -p ${REMOTE_PORT} -R ${rSSHPort}:localhost:22 ${REMOTE_USER}@${HOST} -oControlMaster=no -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no" $USER
        fi
}

# Log connection details
log()
{
        datStr=$(date)
        echo "Connected system using autossh at " "$datStr" >> connection.log
}

connect

现在将其运行在你的树莓派上。

sudo ./connectServer

现在,您希望从笔记本电脑或其他设备登录到服务器。请使用ssh命令进行登录。
ssh [serverUser]@[serverIP] -p 30022

一旦进入您的服务器,您可以与反向隧道连接

ssh pi@localhost -p 31001

看这里!

以下是我用来完成此任务的参考资料:

反向ssh转发:https://www.howtoforge.com/reverse-ssh-tunneling

设置带有ssh密钥的服务器,传递给pi单元:http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/http://jmatthews.us/blog/2013/02/18/rpi-dorm/

设置自动拨号回家:https://www.raspberrypi.org/forums/viewtopic.php?f=36&t=32077


OP已经提到使用反向隧道技术,但是由于延迟的原因并不想在那里增加额外路线。也许SU上的这个问题可以帮助解答:https://superuser.com/questions/827623/is-it-possible-to-make-a-peer-to-peer-ssh-connection-via-bittorrent-like-techniq - renyuneyun
当然可以,但是Pagekite本身显然是延迟问题的一部分。我现在仍在使用上述解决方案的变体,已经五年了,在100多个设备上使用。您提供的链接答案可能有所帮助,但没有提供清晰的工作示例。也许你可以写一个@renyuneyun分享一下好处? - Kelton Temby
实际上我也在寻找解决方案。目前我的搜索结果告诉我尝试使用UDP隧道或类似的东西,可能是IETF ICE(作为标准)。但是,我没有找到任何可以直接尝试的应用程序。我提到的另一个问题似乎只是针对ssh的专用解决方案(我还没有尝试过),但我想要更通用的解决方案(否则我将回退到当前的反向隧道,实际上与您的方式相同)。 - renyuneyun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接