V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
serafin
V2EX  ›  宽带症候群

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

  •  
  •   serafin · 325 天前 · 3862 次点击
    这是一个创建于 325 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个 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

    12 条回复    2023-05-09 22:53:04 +08:00
    Archeb
        1
    Archeb  
       325 天前
    前几天测试过类似的方案,但是群晖客户端连接是不认 302 跳转的,所以放弃了。
    serafin
        2
    serafin  
    OP
       325 天前
    @Archeb 是的,客户端连接没办法,只能复制 ip 。这是为不想用 ddns 的人准备的,如果能 ddns 还是用 ddns 吧。
    lopssh
        3
    lopssh  
       325 天前 via Android
    不错很秀。。。🤩
    eudemonwind
        4
    eudemonwind  
       325 天前 via Android
    围观下哈
    scemsjyd
        5
    scemsjyd  
       325 天前 via iPhone
    不懂就问:
    你的定时任务调用接口 和 ddns 的 Job 调用修改域名厂商 ip 解析 原理是一样的 为什么这样可以避免查水表呢 好像唯一区别就是没有域名解析
    EasonSummer
        6
    EasonSummer  
       325 天前
    国内 IP 不能有域名解析吧 大概这个意思
    @scemsjyd
    tony1016
        7
    tony1016  
       325 天前
    都用 cloudflare 了,就用它的 tunnel 不是很方便嘛
    Masoud2023
        8
    Masoud2023  
       325 天前
    所以为什么不 ddns

    有的地方根本不管的,或者根本不管 TLS 流量的
    titanium98118
        9
    titanium98118  
       325 天前
    ddns 有什么问题?
    别人把 a/aaaa 记录写到你的 ip 上就会被抄水表?
    y1y1
        10
    y1y1  
       325 天前 via iPhone
    前两年还有讨论的,最近没见有说因为 ddns 被封宽带的
    hanguofu
        11
    hanguofu  
       324 天前 via Android
    不好意思歪个楼: 怎样才能访问位于光猫后面的设备(光猫没有打开相应的端口)?
    serafin
        12
    serafin  
    OP
       324 天前
    @hanguofu 如果有公网,VPN 最方便
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2870 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:28 · PVG 19:28 · LAX 04:28 · JFK 07:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.