MiraiForum

    • Register
    • Login
    • Search
    • Popular
    • Recent
    • Unsolved
    • Tags
    • Groups
    • 友情链接

    [教程] 使用 cloud flare worker 实现免费接口代理

    开发交流
    8
    31
    4045
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Y
      Yan502 last edited by

      b9da2f9c-1640-4737-b457-7a10aa112d90-image.png 我们网站需要代理这个接口,要怎么弄呢

      1 Reply Last reply Reply Quote 0
      • Dituon
        Dituon last edited by

        cloud flare worker 的域名 workers.dev 可能在中国某些地区被污染,挂梯子后重试,之后挂载到自己的域名上

        1 Reply Last reply Reply Quote 0
        • MrXiaoM
          MrXiaoM 童心未泯 last edited by MrXiaoM

          之前写的一个脚本,反向代理一个网站,还可以替换内容中的原链接为新链接。

          // API地址
          const api = 'https://lolibooru.moe/';
          // 替换地址 (改为空值不替换)
          const referTo = '';
          
          async function handleRequest(request, origin, pathname, searchParams) {
            const apiUrl = new URL(api);
            apiUrl.pathname = pathname;
            
            for (const [key, value] of searchParams) {
              apiUrl.searchParams.append(key, value);
            }
            request = new Request(apiUrl, request);
            request.headers.set('Origin', apiUrl.origin);
            
            let response = await fetch(request, { method: 'GET' });
            if(response.status == 200 || response.status == 301 || response.status == 302) {
              let changed = false;
              if (referTo.length > 0) {
                const type = response.headers.get("Content-Type");
                if (type != null && (
                  type.includes("text") ||
                  type.includes("javascript") ||
                  type.includes("json")
                )) {
                  let body = await response.text();
                  body = body.replace(api, referTo);
                  response = new Response(body, response);
                  changed = true;
                }
              }
              if (!changed) response = new Response(response.body, response);
          
              response.headers.set('Access-Control-Allow-Origin', origin);
              response.headers.append('Vary', 'Origin');
            }
            return response;
          }
          
          addEventListener('fetch', (event) => {
            const request = event.request;
            const url = new URL(request.url);
            const origin = request.headers.get('Origin');
            const pathname = url.pathname;
            const searchParams = url.searchParams;
            return event.respondWith(handleRequest(request, origin, pathname, searchParams));
          });
          
          
          1 Reply Last reply Reply Quote 0
          • Y
            Yan502 last edited by Yan502

            这里一直显示无效的域名 是因为什么

            MrXiaoM 1 Reply Last reply Reply Quote 0
            • MrXiaoM
              MrXiaoM 童心未泯 @Yan502 last edited by

              @Yan502 不用 https://

              Y 1 Reply Last reply Reply Quote 0
              • Y
                Yan502 @MrXiaoM last edited by Yan502

                @MrXiaoM 还是会报错的

                MrXiaoM 1 Reply Last reply Reply Quote 0
                • MrXiaoM
                  MrXiaoM 童心未泯 @Yan502 last edited by

                  @Yan502 你是不是看不懂英文
                  用根域名,不要用子域名

                  Dituon Y 2 Replies Last reply Reply Quote 0
                  • Dituon
                    Dituon @MrXiaoM last edited by

                    翻译: 使用 izhtai.net 而不是 www.admin.izhtai.net

                    1 Reply Last reply Reply Quote 0
                    • Y
                      Yan502 @MrXiaoM last edited by

                      @MrXiaoM 之前没有注意到, 我们目前的项目就是这个域名,我也搞不懂,这怎么代理

                      Dituon 1 Reply Last reply Reply Quote 0
                      • Dituon
                        Dituon @Yan502 last edited by

                        @Yan502 更新了 「常见问题」 小节,可以参考一下

                        Y 1 Reply Last reply Reply Quote 0
                        • Y
                          Yan502 @Dituon last edited by

                          @Dituon ok 谢谢

                          1 Reply Last reply Reply Quote 0
                          • 1
                          • 2
                          • 2 / 2
                          • First post
                            Last post
                          Powered by Mamoe Technologies & NodeBB | 友情链接 | 服务监控 | Contact