Ubuntu利用Cloudflare Tunnel实现免费内网穿透的教程! - 万事屋 - 技术宅银魂 - 科技改变生活 - 万事屋

Ubuntu利用Cloudflare Tunnel实现免费内网穿透的教程!

Cloudflare Tunnel是什么?

Cloudflare Tunnel是一个强大的内网穿透工具,即使你没有公网IP,也没有任何第三方服务器,它仍然可以完美的实现内网穿透。而且还是完全免费的!

Cloudflare Tunnel 优点:

不需要自建服务器;免费;带宽很高;可以跨运营商;不必在客户端上安装软件;无需备案;支持HTTP3/QUIC;不限服务数量。

Cloudflare Tunnel 缺点:

延迟高抖动高(免费版的Cloudflare隧道服务器都在国外);国内暂不支持HTTP3/QUIC;暂不支持UDP;需要自己有域名且需要将域名托管在Cloudflare。

之前转了两篇利用Cloudflare Tunnel实现内网穿透的教程:

https://www.rei3.com/gintama-post/8541.html

https://www.rei3.com/gintama-post/3611.html

 

有朋友发现有点复杂,而且随着Cloudflare更新,出现了不少变化。

这里转个教程,几乎全程代码命令,但实际操作更稳当些。

安装 cloudflared

有两种安装方法:

一、通过软件源安装

以 Ubuntu 20.04 LTS 为例。 国内通过这一方式安装速度较慢,故不再推荐该方式。

添加源,<RELEASE>内填写版本代号(见下表)
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/ <RELEASE> main' | 
sudo tee /etc/apt/sources.list.d/cloudflare-main.list
# 添加GPG Key
sudo curl https://pkg.cloudflare.com/cloudflare-main.gpg -o /usr/share/keyrings/cloudflare-main.gpg
# 安装
apt update&&apt install cloudflared

Ubuntu 版本代号和版本对应表:

代号版本
Focal20.04
Xenial16.04
Wily15.10
Vivid15.04
Utopic14.10
Trusty14.04
Precise12.04

如果实在不确定,直接去cloudlfare pkg页面找到对应的连接就行:Cloudflare packages

二、通过软件包/二进制文件安装

打开 Release 页面并选择对应架构和操作系统的deb、rpm包或二进制文件下载即可。 手动更新:cloudflared update。

现在通过deb包安装的cloudflared也能通过apt服务更新了(会自动添加Key和source到系统中)。

配置内网穿透

一、登录到 Cloudflare Tunnel

cloudflared tunnel login

执行后若安装正确则会出现一个登录URL,将该链接复制到任何设备的任何浏览器中登录并选择你想用于内网穿透的域名即可。 成功后会生成证书,放置于~/cloudflared/cert.pem中。

二、创建隧道

cloudflared tunnel create <Tunnel-NAME>

<Tunnel-NAME>即为隧道名称,随便起就可以。

三、创建配置文件

配置文件的保存位置一般是 ~/.cloudflared/config.yml,也可以自定义随便在哪都可以。 示例配置文件:

tunnel: 6ff42ae2-765d-4adf-8112-31c55c1551ef
credentials-file: /root/.cloudflared/6ff42ae2-765d-4adf-8112-31c55c1551ef.json
protocol: http2
originRequest:
  connectTimeout: 30s
  noTLSVerify: false

ingress:
  - hostname: gitlab.widgetcorp.tech
    service: http://localhost:80
  - hostname: gitlab-ssh.widgetcorp.tech
    service: ssh://localhost:22
  - service: http_status:404

其中ingress字段可以创建多个服务,支持的服务类型如下表,新建其他服务前需要到 Cloudflare 控制台对其他子域名进行解析。

服务名称描述service 示例值
HTTP/S常规网页服务https://localhost:8000
TCPTCP 连接tcp://localhost:25565
SSHSSH 连接ssh://localhost:22
RDP远程桌面(RDP)rdp://localhost

上表转自 Cloudflare 官方文档,仅节选常用协议,若需要完整版请至 Ingress rules 页面查看。

四、将隧道解析到域名上

cloudflared tunnel route dns <Tunnel-NAME> <SUBDOMAIN>

<Tunnel-NAME>填写刚才创建的隧道,<SUBDOMAIN>填写你想用于内网穿透的子域名(该子域名无需提前创建),执行后会自动将隧道解析到该子域名上。 举例:在刚才的配置文件中配置了两个服务,就需要运行两次:

cloudflared tunnel route dns <Tunnel-NAME> gitlab.widgetcorp.tech
cloudflared tunnel route dns <Tunnel-NAME> gitlab-ssh.widgetcorp.tech

五、手动解析其他子域名

打开 Cloudflare 控制台 ,点击左侧网站,点击对应的域名,左侧DNS,找到刚才设置的子域名,将之前自动设置的子域名的解析内容复制到新子域名上就可以。

试运行:

cloudflared --config <config-File> tunnel run <Tunnel-NAME>
# 例:cloudflared --config /root/.cloudflared/config.yml tunnel run mytun

随后稍等一分钟左右,在其他设备上访问刚才解析的子域名测试即可。

六、注册为系统服务(自动安装)

cloudflared service install

七、注册为系统服务(Systemd)

在/etc/systemd/system下创建cloudflared.service文件:

[Unit]
Description=Cloudflare Tunnel
After=network.target

[Service]
ExecStart=cloudflared --config <config-File> tunnel run <Tunnel-NAME>
Restart=always
RestartSec=5
StartLimitInterval=0

[Install]
WantedBy=multi-user.target

保存退出后systemctl enable cloudflared加入开机启动,systemctl start cloudflared启动服务。 查询日志:journalctl -a -u cloudflared。

部分服务(ingress)配置示例

VNC

这里以RealVNC Server为例。

- hostname: vnc.example.com
  service: tcp://localhost:5900

 

 

    请登录后查看回复内容

万事屋新帖