V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
jeesk
V2EX  ›  分享发现

分享一下防止国内 oss 防止被白嫖,从而白嫖 cloudflare 的思路

  •  1
     
  •   jeesk · 1 月 11 日 · 783 次点击
    这是一个创建于 87 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1. 使用 r2 自己做一个 dl 资源分发下载站,worker 一个 js 文件即可处理. 简单高效
    // src/templates/populated-worker/src/index.js
    
    var src_default = {
      async fetch(request, env) {
        const { DATABASE, r2_dl} = env;
        const url = new URL(request.url);
    
        switch (url.pathname) {
          case "/ping": 
            return new Response('pong', { status: 200 });
    
          case "/upload": {
            // --- 新增 Basic Auth 校验 ---
            const auth = request.headers.get("Authorization");
            if (!auth) {
              return new Response("Unauthorized", {
                status: 401,
                headers: { "WWW-Authenticate": 'Basic realm="Upload Access"' }
              });
            }
    
            // 解析 Basic Auth 字符串 (Base64 解码)
            const base64 = auth.split(' ')[1];
            const decoded = atob(base64);
            const [user, pass] = decoded.split(':');
    
            // 验证用户名和密码 (对应后台设置的环境变量)
            if (user !== "v2ex" || pass !== "xe2v") {
              return new Response("Forbidden", { status: 403 });
            }
            // --- 校验结束 ---
    
            if (request.method !== "PUT") {
              return new Response("Method Not Allowed", { status: 405 });
            }
    
            const fileName = url.searchParams.get("filename");
            if (!fileName) {
              return new Response("Missing 'filename' parameter", { status: 400 });
            }
    
            try {
              await r2_dl.put(fileName, request.body, {
                httpMetadata: {
                  contentType: fileName.endsWith(".apk") 
                    ? "application/vnd.android.package-archive" 
                    : "application/octet-stream",
                }
              });
              return new Response(`Successfully uploaded: ${fileName}`, { status: 200 });
            } catch (e) {
              return new Response(`Upload failed: ${e.message}`, { status: 500 });
            }
          }
    
          case "/download": {
            const fileName = url.searchParams.get("file");
            if (!fileName) return new Response("Missing 'file'", { status: 400 });
    
            const object = await r2_dl.get(fileName);
            if (object === null) return new Response("Not Found", { status: 404 });
    
            const headers = new Headers();
            object.writeHttpMetadata(headers);
            headers.set("etag", object.httpEtag);
            headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(fileName.split('/').pop())}"`);
    
            return new Response(object.body, { headers });
          }
    
          default:
            return new Response('Not Found', { status: 404 });
        }
      }
    };
    
    export { src_default as default };
    
    
    1. 比如 dl.xx.com/1.apk 使用 doh 查询这个 dns. 然后再请求
    2. 如果上面的 ip 很有可能 sockstimeout, 说明这个 ip 已经被封。 目前看有国内 5%的请求失败。
    3. anycast 的 ip 被封是有可能的, 目前从我获取到的数据大约是 5%的请求会被 ban. 这个时候建议使用大站 ip 。
          "www.visa.cn", // Visa 中国官网(带 www ) - CloudFlare CDN
             "www.visa.com", // Visa 国际官网
             "visa.com", // Visa 国际
             "www.shopify.com", // Shopify 官方
             "store.ubi.com", // Ubisoft 商店
             "mfa.gov.ua", // 乌克兰外交部
    
    

    使用上面的 ip 随机来请求,上面的 ip 比较稳定.

    1. 如上面的 ip 都不行,那么就需要针对网络运营商优选 ip. 两种情况, 伪代码如下
    val 运营商  = if(手机是 wifi 或者 vpn) {
      //那么直接使用 ipinfo 之类来获取运营商
    }else{
      // 安卓直接 simOperator 来获取运营商信息
    }
    
    // 使用
    
       val ipStr =  if(result == "中国联通"){
          httpRequest("https://cf.090227.xyz/cu")
        }else if(result == "中国移动"){
          httpRequest("https://cf.090227.xyz/cmccd")
        }else if(result == "中国电信"){
          httpRequest("https://cf.090227.xyz/ct")
        }else{
          // 不知道是杀,听天由命了。 选择最快的的 ip
          httpRequest("https://cf.090227.xyz/ip.164746.xyz")
          }
        
    拿到 ip 后再访问下载站。 
    
    
    

    当然你也可以使用 github release 的方式找几个 proxy 来下载请求,

    我推荐的是最开始优选 github 代理加速, 代理炸了,再使用 cf 优选 ip.

    然后上面的一些地址最好使用 dns txt 保持动态更新。 美滋滋。 上面优选 ip 最好先并发请求一下,能够 ping 测速一下在做真实请求。 这样一来每个月 50g 的流量,一年能省不少的钱。谁叫国内没法上架呢。

    目前尚无回复
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   1052 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 18:29 · PVG 02:29 · LAX 11:29 · JFK 14:29
    ♥ Do have faith in what you're doing.