10

Cloudflare 隧道内网穿透搭建记录

 2 years ago
source link: https://johnrosen1.com/2022/04/19/cloudflare/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Cloudflare 隧道内网穿透搭建记录

由于国内运营商不让用80端口,备案又麻烦,因此我想出了这招。

  1. 免费且不需要服务器
  2. 暂时不支持UDP协议
  1. 一个托管于Cloudflare的域名,相关教程看这里创建 Cloudflare 帐户并添加网站
  2. 一台本地Linux Web机器,即内网穿透的对象
  3. 正常网络连接

安装 Cloudflared

curl -LO https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
dpkg -i cloudflared-linux-amd64.deb
rm cloudflared-linux-amd64.deb

登录 Cloudflared

cloudflared tunnel login

这时会弹出来一个URL,用浏览器打开,登录认证,然后选择你想用来做内网穿透的域名即可。

成功后会生成证书,放置于~/cloudflared/cert.pem中。

名字可以随意起

cloudflared tunnel create <Tunnel-NAME>

成功后会提示,相关凭证已放置于~/.cloudflared/<Tunnel-UUID>.json中。

新建 Tunnel 对应的 DNS 记录

<SUBDOMAIN>填你想用来做内网穿透的域名,可以为二级域名(example.com)或三级域名(www.example.com)等。

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

成功后会创建CNAME记录将域名指向隧道。

新建配置文件

nano ~/.cloudflared/config.yml

写入以下配置

tunnel: <Tunnel-UUID>
credentials-file: /root/.cloudflared/<Tunnel-UUID>.json
protocol: h2mux
originRequest:
connectTimeout: 30s
noTLSVerify: false
ingress:
- hostname: <SUBDOMAIN>
service: http://localhost:80
- service: http_status:404

2022.4.20 更新:经测试,http2h2mux均可正常建立连接,只有quic无法建立连接。

由于国内环境关系,无法使用默认的quic建立隧道,因此需指定http2,http://localhost:80为本地服务的地址。

启动 Cloudflared

修改systemd文件

nano /etc/systemd/system/cloudflared.service

写入以下内容

[Unit]
Description=cloudflared
After=network.target

[Service]
TimeoutStartSec=0
Type=notify
ExecStart=/usr/bin/cloudflared --loglevel debug --transport-loglevel warn --config /root/.cloudflared/config.yml tunnel run <Tunnel-NAME>
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

启动 Cloudflared

systemctl enable cloudflared --now

测试穿透是否成功

等待一两分钟,然后尝试访问https://<SUBDOMAIN>,如可正常显示则成功。

Debug指令:systemctl status cloudflared journalctl -a -u cloudflared (-r / -f)

由于Cloudflare走V2ray的sniffing或者fakedns都会出错,因此需专门写一条iptables规则开机启动。

crontab -e
@reboot sleep 30s && iptables -t nat -I OUTPUT -p tcp --dport 7844 -j RETURN
@reboot sleep 30s && iptables -t nat -I OUTPUT -p udp --dport 7844 -j RETURN

这玩意不比什么花生壳,frp香多了,免费又不需要服务器,哈哈哈哈哈哈哈哈哈。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK