Mystery0の小站

Mystery0の小站

Cloudflare Worker 反代 TG Bot API

2023-11-26

分享个以前找到的一个CF_worker反代TG_BOT_API的脚本

实际上我也不知道是哪里拷贝的了,和上面文章里面的有点像,估计是在哪个gist复制的吧

修改了一点东西,适配下载文件的场景,最终脚本如下:

const whitelist = ["/bot1234567890:"];
const tg_host = "api.telegram.org";

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

function validate(path) {
    for (var i = 0; i < whitelist.length; i++) {
        if (path.startsWith(whitelist[i]) || path.startsWith("/file" + whitelist[i]))
            return true; 
    }
    return false;
}

async function handleRequest(request) {
    var u = new URL(request.url);
    u.host = tg_host;
    if (!validate(u.pathname))
        return new Response('Unauthorized', {
            status: 403
        });
    var req = new Request(u, {
        method: request.method,
        headers: request.headers,
        body: request.body
    });
    const result = await fetch(req);
    return result;
}