如何在我的 Raspberry Pi 上使用 STUN/ICE 实现 NAT 穿越

Dav*_*d C 7 networking nat nat-traversal stun

我正在尝试在我的树莓派上设置一个能够突破 NAT 路由器后面的实现,这样我就可以远程连接到它,无论它位于哪个网络后面。

我已经尝试过 pagekite.me,它作为中继/反向隧道解决方案非常棒,但我觉得它仍然有点笨重(存在延迟问题,因为中间需要连接一台额外的服务器)。

我已经阅读了有关 STUN 和 ICE 的信息,但我不知道有什么解决方案可以在我的树莓派上实现。

目标是我可以通过 SSH 连接到我的 PI,无论其网络配置(路由器/网络)和网络防火墙如何。

有人可以指出我应该在哪里或寻找什么的正确方向吗?

Kel*_*mby 2

我不是直接回答你的 STUN/ICE 问题,而是回答你的目标,即如何远程突破 NAT 和 ssh 到你的 pi。

解决此问题的最简单方法是使用反向 ssh 隧道,尤其是使用 autossh 和基于密钥的身份验证的隧道。

这需要你有自己的服务器,在某个地方有一个 ssh 端口,供 pi 调用(我使用的服务器只是位于我的家庭网络上,找出你的公共 IP 地址,如果你想使用 dns 帐户,请注册一个免费的 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]
Run Code Online (Sandbox Code Playgroud)

然后,如果您愿意,您还需要另一个“配置文件”,位于您的 pi 的主目录中。如果您愿意,可以将其命名为 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). 
Run Code Online (Sandbox Code Playgroud)

然后,您将获得一个最终脚本,它将实现对服务器的实际 ssh 回调。如果您愿意,可以将其命名为 connectServer.sh,并将其放在 /home/pi/ 中

#!/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
Run Code Online (Sandbox Code Playgroud)

现在在你的 pi 上运行它

sudo ./connectServer
Run Code Online (Sandbox Code Playgroud)

现在您想从笔记本电脑或任何连接设备通过 ssh 登录到服务器。

ssh [serverUser]@[serverIP] -p 30022
Run Code Online (Sandbox Code Playgroud)

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

ssh pi@localhost -p 31001
Run Code Online (Sandbox Code Playgroud)

瞧!

以下是我到目前为止所使用的参考资料:

反向 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