[网络技巧] CF Worker搭建vless协议代理
作者:CC下载站 日期:2023-08-02 11:25:24 浏览:64 分类:web
1、复制如下代码到worker中
注意自己可修改uuid 具体搜索
3e036e57-4d74-469a-b312-b97b59680ec3
// ../node_modules/uuid/dist/esm-browser/regex.js var regex_default = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i; // ../node_modules/uuid/dist/esm-browser/validate.js function validate(uuid) { return typeof uuid === "string" && regex_default.test(uuid); } var validate_default = validate; // ../node_modules/uuid/dist/esm-browser/stringify.js var byteToHex = []; for (let i = 0; i < 256; ++i) { byteToHex.push((i + 256).toString(16).slice(1)); } function unsafeStringify(arr, offset = 0) { return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase(); } function stringify(arr, offset = 0) { const uuid = unsafeStringify(arr, offset); if (!validate_default(uuid)) { throw TypeError("Stringified UUID is invalid"); } return uuid; } var stringify_default = stringify; // vless-js/lib/vless-js.ts var WS_READY_STATE_OPEN = 1; function makeReadableWebSocketStream(ws, earlyDataHeader, log) { let readableStreamCancel = false; return new ReadableStream({ start(controller) { ws.addEventListener("message", async (e) => { if (readableStreamCancel) { return; } const vlessBuffer = e.data; controller.enqueue(vlessBuffer); }); ws.addEventListener("error", (e) => { log("socket has error"); readableStreamCancel = true; controller.error(e); }); ws.addEventListener("close", () => { try { log("webSocket is close"); if (readableStreamCancel) { return; } controller.close(); } catch (error2) { log(`websocketStream can't close DUE to `, error2); } }); const { earlyData, error } = base64ToArrayBuffer(earlyDataHeader); if (error) { log(`earlyDataHeader has invaild base64`); safeCloseWebSocket(ws); return; } if (earlyData) { controller.enqueue(earlyData); } }, pull(controller) { }, cancel(reason) { log(`websocketStream is cancel DUE to `, reason); if (readableStreamCancel) { return; } readableStreamCancel = true; safeCloseWebSocket(ws); } }); } function base64ToArrayBuffer(base64Str) { if (!base64Str) { return { error: null }; } try { base64Str = base64Str.replace(/-/g, "+").replace(/_/g, "/"); const decode = atob(base64Str); const arryBuffer = Uint8Array.from(decode, (c) => c.charCodeAt(0)); return { earlyData: arryBuffer.buffer, error: null }; } catch (error) { return { error }; } } function safeCloseWebSocket(socket) { try { if (socket.readyState === WS_READY_STATE_OPEN) { socket.close(); } } catch (error) { console.error("safeCloseWebSocket error", error); } } function processVlessHeader(vlessBuffer, userID) { if (vlessBuffer.byteLength < 24) { return { hasError: true, message: "invalid data" }; } const version = new Uint8Array(vlessBuffer.slice(0, 1)); let isValidUser = false; let isUDP = false; if (stringify_default(new Uint8Array(vlessBuffer.slice(1, 17))) === userID) { isValidUser = true; } if (!isValidUser) { return { hasError: true, message: "invalid user" }; } const optLength = new Uint8Array(vlessBuffer.slice(17, 18))[0]; const command = new Uint8Array( vlessBuffer.slice(18 + optLength, 18 + optLength + 1) )[0]; if (command === 1) { } else if (command === 2) { isUDP = true; } else { return { hasError: true, message: `command ${command} is not support, command 01-tcp,02-udp,03-mux` }; } const portIndex = 18 + optLength + 1; const portBuffer = vlessBuffer.slice(portIndex, portIndex + 2); const portRemote = new DataView(portBuffer).getInt16(0); let addressIndex = portIndex + 2; const addressBuffer = new Uint8Array( vlessBuffer.slice(addressIndex, addressIndex + 1) ); const addressType = addressBuffer[0]; let addressLength = 0; let addressValueIndex = addressIndex + 1; let addressValue = ""; switch (addressType) { case 1: addressLength = 4; addressValue = new Uint8Array( vlessBuffer.slice(addressValueIndex, addressValueIndex + addressLength) ).join("."); break; case 2: addressLength = new Uint8Array( vlessBuffer.slice(addressValueIndex, addressValueIndex + 1) )[0]; addressValueIndex += 1; addressValue = new TextDecoder().decode( vlessBuffer.slice(addressValueIndex, addressValueIndex + addressLength) ); break; case 3: addressLength = 16; const dataView = new DataView( vlessBuffer.slice(addressValueIndex, addressValueIndex + addressLength) ); const ipv6 = []; for (let i = 0; i < 8; i++) { ipv6.push(dataView.getUint16(i * 2).toString(16)); } addressValue = ipv6.join(":"); break; default: console.log(`invild addressType is ${addressType}`); } if (!addressValue) { return { hasError: true, message: `addressValue is empty, addressType is ${addressType}` }; } return { hasError: false, addressType, addressRemote: addressValue, portRemote, rawDataIndex: addressValueIndex + addressLength, vlessVersion: version, isUDP }; } // index.ts import { connect } from "cloudflare:sockets"; // dns.ts var doh = "https://cloudflare-dns.com/dns-query"; var dns = async (domain) => { const response = await fetch(`${doh}?name=${domain}`, { method: "GET", headers: { "Accept": "application/dns-json" } }); const data = await response.json(); const ans = data?.Answer; return ans?.find((record) => record.type === 1)?.data; }; var isCloudFlareIP = (ip) => { const CFIP = [ [2918526976, -4096], [1729491968, -1024], [1729546240, -1024], [1730085888, -1024], [2372222976, -16384], [1822605312, -16384], [3193827328, -4096], [3161612288, -4096], [3320508416, -1024], [3324608512, -32768], [2728263680, -131072], [1745879040, -524288], [1746403328, -262144], [2889875456, -524288], [2197833728, -1024] ]; const isIp4InCidr = (ip2, cidr) => { const [a, b, c, d] = ip2.split(".").map(Number); ip2 = a << 24 | b << 16 | c << 8 | d; const [range, mask] = cidr; return (ip2 & mask) === range; }; return CFIP.some((cidr) => isIp4InCidr(ip, cidr)); }; // index.ts var HTML404 = "Script by zizifn, modified by MisakaNo"; function delay2(ms) { return new Promise((resolve, rej) => { setTimeout(resolve, ms); }); } var workers_default = { async fetch(request, env, ctx) { let address = ""; let portWithRandomLog = ""; const userID = env.UUID || "3e036e57-4d74-469a-b312-b97b59680ec3"; const log = (info, event) => { console.log(`[${address}:${portWithRandomLog}] ${info}`, event || ""); }; const upgradeHeader = request.headers.get("Upgrade"); if (!upgradeHeader || upgradeHeader !== "websocket") { return new Response(HTML404, { status: 404, headers: new Headers({ "Content-Type": "text/html" }) }); } const webSocketPair = new WebSocketPair(); const [client, webSocket] = Object.values(webSocketPair); const earlyDataHeader = request.headers.get("sec-websocket-protocol") || ""; let remoteSocket = null; webSocket.accept(); const readableWebSocketStream = makeReadableWebSocketStream( webSocket, earlyDataHeader, log ); let vlessResponseHeader = new Uint8Array([0, 0]); let remoteConnectionReadyResolve; readableWebSocketStream.pipeTo( new WritableStream({ async write(chunk, controller) { if (remoteSocket) { const writer2 = remoteSocket.writable.getWriter(); await writer2.write(chunk); writer2.releaseLock(); return; } const { hasError, message, portRemote, addressType, addressRemote, rawDataIndex, vlessVersion, isUDP } = processVlessHeader(chunk, userID); address = addressRemote || ""; portWithRandomLog = `${portRemote} -- ${isUDP ? "udp " : "tcp "} `; if (isUDP && portRemote != 53) { controller.error("UDP proxy only enable for DNS which is port 53"); webSocket.close(); return; } if (hasError) { controller.error(message); webSocket.close(); return; } vlessResponseHeader = new Uint8Array([vlessVersion[0], 0]); const rawClientData = chunk.slice(rawDataIndex); let queryip = ""; if (addressType === 2) { queryip = await dns(addressRemote); if (queryip && isCloudFlareIP(queryip)) { queryip = "64.68.192." + Math.floor(Math.random() * 255); } } remoteSocket = connect({ hostname: queryip ? queryip : addressRemote, port: portRemote }); log(`connected`); const writer = remoteSocket.writable.getWriter(); await writer.write(rawClientData); writer.releaseLock(); remoteConnectionReadyResolve(remoteSocket); }, close() { console.log( `[${address}:${portWithRandomLog}] readableWebSocketStream is close` ); }, abort(reason) { console.log( `[${address}:${portWithRandomLog}] readableWebSocketStream is abort`, JSON.stringify(reason) ); } }) ); (async () => { await new Promise((resolve) => remoteConnectionReadyResolve = resolve); let count = 0; remoteSocket.readable.pipeTo( new WritableStream({ start() { if (webSocket.readyState === WebSocket.READY_STATE_OPEN) { webSocket.send(vlessResponseHeader); } }, async write(chunk, controller) { if (webSocket.readyState === WebSocket.READY_STATE_OPEN) { if (count++ > 2e4) { await delay2(1); } webSocket.send(chunk); } else { controller.error( "webSocket.readyState is not open, maybe close" ); } }, close() { console.log( `[${address}:${portWithRandomLog}] remoteConnection!.readable is close` ); }, abort(reason) { console.error( `[${address}:${portWithRandomLog}] remoteConnection!.readable abort`, reason ); } }) ).catch((error) => { console.error( `[${address}:${portWithRandomLog}] processWebSocket has exception `, error.stack || error ); safeCloseWebSocket2(webSocket); }); })(); return new Response(null, { status: 101, webSocket: client }); } }; function safeCloseWebSocket2(ws) { try { if (ws.readyState !== WebSocket.READY_STATE_CLOSED) { ws.close(); } } catch (error) { console.error("safeCloseWebSocket error", error); } } export { workers_default as default }; //# sourceMappingURL=index.js.map
2、节点配置如下
协议:Vless 地址:CF优选IP / 域名 端口:80 或 CF 支持的 HTTPS 端口 UUID/密码:设置的UUID(如未设置则为 `3e036e57-4d74-469a-b312-b97b59680ec3`) 传输协议:ws 伪装域名:设置的 Workers 自定义域名 路径:/
由于目前worker自带域名已被X,建议使用自己的域名
协议:Vless 地址:CF优选IP / 自己的域名 端口:443 或 CF 支持的 HTTPS 端口 UUID/密码:设置的UUID(如未设置则为 `3e036e57-4d74-469a-b312-b97b59680ec3`) 传输协议:ws 伪装域名:自己的域名 路径:/ 传输安全:TLS 跳过证书验证:true 或 false 都可以
参考文章
https://web.archive.org/web/20230526070655/https://blog.misaka.rest/2023/05/26/cf-wkrs-vless/
参考githun相关项目
https://github.com/zizifn/edgetunnel
The text was updated successfully, but these errors were encountered: |
猜你还喜欢
- 10-30 [网络线报] 城通网盘福利线报解析器 - 获取直连下载地址
- 08-30 [web] 目前国内可用Docker镜像加速器(2024-08)
- 06-04 [网络技术] 获取免费 .edu 邮箱&教育邮箱优惠汇总
- 06-04 [web] 5分钟快速申请一个EDU教育邮箱
- 04-01 [科学上网] 直连、中转、专线机场有什么区别|良心高效专线机场推荐|hy2机场推荐
- 03-29 [web] 免费公共DNS大全(IPv6+IPv4)
- 03-29 [玩技术] CTFHUB刷题笔记
- 03-22 [AI配音] 有哪些好用的AI智能配音工具值得推荐?
- 03-15 [web] 轻松申请免费域名并绑定到个人网站 | link顶级域名
- 03-15 [运营] 亚马逊如何选品?产品市场分析怎么做?包括哪些方面?
- 03-15 [web] 3分钟,免费拥有你的专属域名——免费一级域名注册申请及域名解析教程
- 03-14 [学习] 必看!手把手教你自动续期微软E5账号,长期畅享5T OneDrive云盘!
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[电影] 《环太平洋两部合集》 4K REMUX原盘 [杜比视界] 国英双语音轨 [内封特效字幕] [133.8G]
[电影] 异人之下 The Traveller 2024✨【影版】【4K正式版/HQ超高码/DDP5.1】✚【1080高码】无水印/无压缩
[动漫] 头文字D 动漫 (1998) S01-S06季 1080P 国粤日音轨 续作 剧场版 电影
[小说] 知轩藏书全站7667册txt小说合集精心校对版
[杂志] 电脑爱好者杂志14年 超全 [PDF]
[电影] 西游记全部版本-4K高清修复-总计384G-1986+1996+1998+2002+2010浙版+西游记后传
[纪录片] 【国家地理百年纪念典藏】超经典100集全 MP4格式 (绝佳学习资料)27GB
[纪录片] B站食贫道收费纪录片 *迷失东京* [1080P] 揭露日本大家感兴趣却不为人知的秘密
[网络线报] 城通网盘福利线报解析器 - 获取直连下载地址
[福利线报] 一个「脚本」搞定六大网盘(百度/阿里/天翼/迅雷/夸克/移动)
[游戏] 《黑神话悟空》免安装学习版【全dlc整合完整版】+Steam游戏解锁+游戏修改工具!
[动画] 《名侦探柯南》名侦探柯南百万美元的五菱星 [TC] [MP4]
[电视剧集] [BT下载][黑暗城市- 清扫魔 Dark City: The Cleaner 第一季][全06集][英语无字][MKV][720P/1080P][WEB-RAW]
[涨点姿势] 男性性技宝典:14招实战驭女术——爱抚、按摩、催情、姿势、高潮全攻略
[动画] 2002《火影忍者》720集全【4K典藏版】+11部剧场版+OVA+漫画 内嵌简日字幕
[剧集] 《斯巴达克斯》1-4季合集 无删减版 1080P 内嵌简英特效字幕
[CG剧情] 《黑神话:悟空》158分钟CG完整剧情合集 4K120帧最高画质
[短剧] 被下架·禁播的羞羞短剧·午夜短剧合集
[游戏] 黑神话悟空离线完整版+修改器
[图像处理] 光影魔术手v4.6.0.578绿色版
[影视] 美国内战 4K蓝光原盘下载+高清MKV版/内战/帝国浩劫:美国内战(台)/美帝崩裂(港) 2024 Civil War 63.86G
[影视] 一命 3D 蓝光高清MKV版/切腹 / 切腹:武士之死 / Hara-Kiri: Death of a Samurai / Ichimei 2011 一命 13.6G
[影视] 爱情我你他 蓝光原盘下载+高清MKV版/你、我、他她他 2005 Me and You and Everyone We Know 23.2G
[影视] 穿越美国 蓝光原盘下载+高清MKV版/窈窕老爸 / 寻找他妈…的故事 2005 Transamerica 20.8G
[电影] 《黄飞鸿》全系列合集
[Android] 开罗游戏 ▎像素风格的模拟经营的游戏厂商安卓游戏大合集
[游戏合集] 要战便战 v0.9.107 免安装绿色中文版
[资源] 精整2023年知识星球付费文合集136篇【PDF格式】
[系统]【黑果小兵】macOS Big Sur 11.0.1 20B50 正式版 with Clover 5126 黑苹果系统镜像下载
[美图] 【经典收藏美图集合】1500多张韩国美女高清图片让你的收藏夹更加丰富多彩
- 最新评论
-
有靳东!嘻嘻奥古斯都.凯撒 评论于:10-28 流星花园是F4处女作也是4人集体搭配的唯一一部!奥古斯都.凯撒 评论于:10-28 找了好久的资源,终于在这里找到了。感谢本站的资源和分享。谢谢AAAAA 评论于:10-26 找了好久的资源,终于在这里找到了。感谢本站的资源和分享。谢谢password63 评论于:10-26 找了好久的资源,终于在这里找齐了!!!!blog001 评论于:10-21 找了好久的资源,终于在这里找齐了!!!!blog001 评论于:10-21 找了好久的资源,终于在这里找到了。感谢本站的资源和分享。谢谢WillKwok 评论于:10-09 感谢分享1234123 评论于:10-07 太好了终于找到了谢谢Tom 评论于:10-07 谢谢分享loonghd 评论于:09-30
- 热门tag