serafin

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

  •  
  •   serafin · May 9, 2023 · 5588 views
    This topic created in 1124 days ago, the information mentioned may be changed or developed.

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

    有的地方根本不管的,或者根本不管 TLS 流量的
    titanium98118
        9
    titanium98118  
       May 9, 2023
    ddns 有什么问题?
    别人把 a/aaaa 记录写到你的 ip 上就会被抄水表?
    y1y1
        10
    y1y1  
       May 9, 2023 via iPhone
    前两年还有讨论的,最近没见有说因为 ddns 被封宽带的
    hanguofu
        11
    hanguofu  
       May 9, 2023 via Android
    不好意思歪个楼: 怎样才能访问位于光猫后面的设备(光猫没有打开相应的端口)?
    serafin
        12
    serafin  
    OP
       May 9, 2023
    @hanguofu 如果有公网,VPN 最方便
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3048 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 08:56 · PVG 16:56 · LAX 01:56 · JFK 04:56
    ♥ Do have faith in what you're doing.