參考鏈接:使用Cloudflare Worker代理Telegram Bot Api – 廢墟 (anerg.com)
(68條消息) cf反代tg代理并使用自己的域名_拖拉斯基~的博客-CSDN博客
Cloud Flare 添加谷歌鏡像站(反向代理)_cloudflare 反向代理_StarDream-Online的博客-CSDN博客
在白嫖到了免費(fèi)的二級域名(白嫖二級域名 2023.05親測可用 - 簡書 (jianshu.com))
之后,用它來代理tg的bot api
添加站點(diǎn)
首先需要添加自己的域名到Cloudflare奢赂,沒有域名是無法繼續(xù)下一步的
這里的服務(wù)器名稱應(yīng)該是有個(gè)地方會提供給你的其徙,就是以 cloudflare.com
結(jié)尾的兩個(gè)地址
NS anderson.ns.cloudflare.com
NS nancy.ns.cloudflare.com
這個(gè)是我目前網(wǎng)站的服務(wù)器名稱
此外還需要添加一個(gè) DNS 記錄
名稱隨便填补箍,這里填 tg注祖,IPv4地址隨便填
填了tg之后 ,tg.你的域名
就是你實(shí)際使用到的代理地址了
添加Workers
在Cloudflare中新建一個(gè)workers
選擇創(chuàng)建Workers
隨便填個(gè)名字品嚣,比如tg
磁浇,然后直接"Deploy"
這個(gè)部分和參考鏈接里面的界面好像不太一樣送滞,沒有什么“http路由器”這些侠草,但是不影響后面的過程
保存完之后,返回Workers的創(chuàng)建頁面犁嗅,點(diǎn)擊標(biāo)題進(jìn)行編輯
直接選擇“快速編輯”边涕,進(jìn)入到代碼編輯頁面
將以前的代碼全部刪除,然后粘貼下面的代碼
/**
* Helper functions to check if the request uses
* corresponding method.
*
*/
const Method = (method) => (req) => req.method.toLowerCase() === method.toLowerCase();
const Get = Method('get');
const Post = Method('post');
const Path = (regExp) => (req) => {
const url = new URL(req.url);
const path = url.pathname;
return path.match(regExp) && path.match(regExp)[0] === path;
};
/*
* The regex to get the bot_token and api_method from request URL
* as the first and second backreference respectively.
*/
const URL_PATH_REGEX = /^\/bot(?<bot_token>[^/]+)\/(?<api_method>[a-z]+)/i;
/**
* Router handles the logic of what handler is matched given conditions
* for each request
*/
class Router {
constructor() {
this.routes = [];
}
handle(conditions, handler) {
this.routes.push({
conditions,
handler,
});
return this;
}
get(url, handler) {
return this.handle([Get, Path(url)], handler);
}
post(url, handler) {
return this.handle([Post, Path(url)], handler);
}
all(handler) {
return this.handler([], handler);
}
route(req) {
const route = this.resolve(req);
if (route) {
return route.handler(req);
}
const description = 'No matching route found';
const error_code = 404;
return new Response(
JSON.stringify({
ok: false,
error_code,
description,
}),
{
status: error_code,
statusText: description,
headers: {
'content-type': 'application/json',
},
}
);
}
/**
* It returns the matching route that returns true
* for all the conditions if any.
*/
resolve(req) {
return this.routes.find((r) => {
if (!r.conditions || (Array.isArray(r) && !r.conditions.length)) {
return true;
}
if (typeof r.conditions === 'function') {
return r.conditions(req);
}
return r.conditions.every((c) => c(req));
});
}
}
/**
* Sends a POST request with JSON data to Telegram Bot API
* and reads in the response body.
* @param {Request} request the incoming request
*/
async function handler(request) {
// Extract the URl method from the request.
const { url, ..._request } = request;
const { pathname: path, search } = new URL(url);
// Leave the first match as we are interested only in backreferences.
const { bot_token, api_method } = path.match(URL_PATH_REGEX).groups;
// Build the URL
const api_url = 'https://api.telegram.org/bot' + bot_token + '/' + api_method + search;
// Get the response from API.
const response = await fetch(api_url, _request);
const result = await response.text();
const res = new Response(result, _request);
res.headers.set('Content-Type', 'application/json');
return res;
}
/**
* Handles the incoming request.
* @param {Request} request the incoming request.
*/
async function handleRequest(request) {
const r = new Router();
r.get(URL_PATH_REGEX, (req) => handler(req));
r.post(URL_PATH_REGEX, (req) => handler(req));
const resp = await r.route(request);
return resp;
}
/**
* Hook into the fetch event.
*/
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request));
});
記得保存愧哟;
添加Workers路由
點(diǎn)擊網(wǎng)站奥吩,再點(diǎn)擊“workers路由”進(jìn)行操作
路由填tg.你的域名/*
,服務(wù)選擇你剛剛創(chuàng)建的workers蕊梧,然后保存
測試
Tg bot的api是
### 這個(gè)地址需要外網(wǎng)訪問
https://api.telegram.org/bot<bot的token>/sendMessage?chat_id=<tg用戶的id>&text=<發(fā)送內(nèi)容>
將上面的參數(shù)替換成你自己的參數(shù)霞赫,如果你的賬號可以收到新的消息表示你的機(jī)器人bot創(chuàng)建是正常的
上面的地址如果可以正常收到消息,再訪問下面這個(gè)地址
### 這個(gè)地址在國內(nèi)就可以訪問
https://tg.你的域名/bot<bot的token>/sendMessage?chat_id=<tg用戶的id>&text=<發(fā)送內(nèi)容>
以上就完成了Cloudflare反向代理Tg bot肥矢,通過Cloudflare還可以反向代理谷歌等網(wǎng)址端衰,參考鏈接里面有例子,具體需要自行了解