DDNS 的替代方案 --- Cloudflare worker 302 重定向

355 天前
 serafin

一个 DDNS 的替代方案 --- Cloudflare worker 302 重定向。好像部分地区 DDNS + web 服务会被查水表,所以想到了 302 重定向。免费的 Cloudflare Worker 能实现,完美!

Worker 代码如下,要在设置里绑定 KV (代码中 KV 名是“test”)。记得把 token 改一下。

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const clientIP = request.headers.get('CF-Connecting-IP')

  const url = new URL(request.url)
  const token = url.searchParams.get('token')

  if (token === "123") {
    await test.put('ip', clientIP)
    return new Response(`Client IP: ${clientIP} Token: ${token}`)
  } else {
    let ip = await test.get('ip')
    return new Response(`${ip}`)
    //return Response.redirect(`http://${ip}:5000`, 302)
  }
}

设置 crontab 定时任务 或者

群晖 NAS 打开“控制面板”-->“任务计划”--> 新增 --> “用户定义的脚本” 每分钟执行。

curl https://test123.speed.ga/?token=123

测试链接:

打开 https://test123.speed.ga/ 查看 ip 。

现在是查看 ip ,为了方便测试。推荐改为 302 重定向到 ip+端口。

https://test123.speed.ga/?token=123 更新访问者 ip

3964 次点击
所在节点    宽带症候群
12 条回复
Archeb
355 天前
前几天测试过类似的方案,但是群晖客户端连接是不认 302 跳转的,所以放弃了。
serafin
355 天前
@Archeb 是的,客户端连接没办法,只能复制 ip 。这是为不想用 ddns 的人准备的,如果能 ddns 还是用 ddns 吧。
lopssh
355 天前
不错很秀。。。🤩
eudemonwind
355 天前
围观下哈
scemsjyd
355 天前
不懂就问:
你的定时任务调用接口 和 ddns 的 Job 调用修改域名厂商 ip 解析 原理是一样的 为什么这样可以避免查水表呢 好像唯一区别就是没有域名解析
EasonSummer
355 天前
国内 IP 不能有域名解析吧 大概这个意思
@scemsjyd
tony1016
355 天前
都用 cloudflare 了,就用它的 tunnel 不是很方便嘛
Masoud2023
355 天前
所以为什么不 ddns

有的地方根本不管的,或者根本不管 TLS 流量的
titanium98118
355 天前
ddns 有什么问题?
别人把 a/aaaa 记录写到你的 ip 上就会被抄水表?
y1y1
355 天前
前两年还有讨论的,最近没见有说因为 ddns 被封宽带的
hanguofu
355 天前
不好意思歪个楼: 怎样才能访问位于光猫后面的设备(光猫没有打开相应的端口)?
serafin
355 天前
@hanguofu 如果有公网,VPN 最方便

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/938444

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX