// 定义一个名为 mytxt 的变量,并将 'passwd' 作为默认的读写权限 let mytxt= 'passwd'; export default { async fetch (request, env) { // 如果环境变量中有 txt,则将其赋值给 mytxt,否则保持默认值 mytxt = env.txt || mytxt; let KV; // 检查 KV (键值存储) 是否已经被设置 if (env.KV) { // 将 env.KV 赋值一个名为 KV 的常量 KV = env.KV; } else { //throw new Error('KV 命名空间未绑定'); return new Response('KV 命名空间未绑定', { status: 400, headers: { 'content-type': 'text/plain; charset=utf-8' }, }); } // 从请求的 URL 中获取需要的参数 const url = new URL(request.url); let txt; if (url.pathname === `/${mytxt}`){ txt = mytxt; } else { // 获取 URL 查询参数中的 'txt',如果不存在则赋值为 "null" txt = url.searchParams.get('txt') || "null"; } // 检查提供的 txt 是否与 mytxt 相符 if (txt === mytxt) { const 文件名 = url.pathname.startsWith('/') ? url.pathname.substring(1) : url.pathname; if (文件名 == "config" || 文件名 == mytxt) { const html = configHTML(url.hostname, txt); return new Response(html, { headers: { 'Content-Type': 'text/html; charset=UTF-8', }, }); } else if (文件名 == "config/update.bat") { return new Response(下载bat(url.hostname, txt), { headers: { "Content-Disposition": `attachment; filename=update.bat`, "content-type": "text/plain; charset=utf-8", }, }); } else if (文件名 == "config/update.sh") { return new Response(下载sh(url.hostname, txt), { headers: { "Content-Disposition": `attachment; filename=update.sh`, "content-type": "text/plain; charset=utf-8", }, }); } else { // 获取 URL 查询参数中的 'text' 和 'b64',如果不存在则赋值为 "null" const text = url.searchParams.get('text') || "null"; const b64 = url.searchParams.get('b64') || "null"; // 如果 'text' 和 'b64' 都为 "null",则从 KV 中读取并返回文件内容 if (text === "null" && b64 === "null"){ const value = await KV.get(文件名); return new Response(value , { status: 200, headers: { 'content-type': 'text/plain; charset=utf-8' }, }); } else { // 检查文件是否存在 await fileExists(KV, 文件名); // 如果 'b64' 为 "null" ,则以明文方式写入文件,如果 'text' 为 "null" ,则以 base64 方式写入文件 if (b64 === "null" ){ await KV.put(文件名, text); return new Response(text, { status: 200, headers: { 'content-type': 'text/plain; charset=utf-8' }, }); } else if (text === "null" ){ await KV.put(文件名, base64Decode(空格替换加号(b64))); return new Response(base64Decode(空格替换加号(b64)), { status: 200, headers: { 'content-type': 'text/plain; charset=utf-8' }, }); } } } } else if (url.pathname == "/"){//首页改成一个nginx伪装页 return new Response(` Welcome to nginx!

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

`, { headers: { 'Content-Type': 'text/html; charset=UTF-8', }, }); } else {// 如果 txt 不符,返回 'txt 有误'// return new Response('txt 有误', { status: 400, headers: { 'content-type': 'text/plain; charset=utf-8' }, }); } } }; // 定义一个名为 fileExists 的异步函数,通过查询 KV 中是否有 filename 对应的值来判断文件是否存在 async function fileExists(KV, filename) { const value = await KV.get(filename); return value !== null; } // 定义一个名为 base64Decode 的函数,用于将 base64 编码的字符串转换为 utf-8 编码的字符 function base64Decode(str) { const bytes = new Uint8Array(atob(str).split('').map(c => c.charCodeAt(0))); const decoder = new TextDecoder('utf-8'); return decoder.decode(bytes); } function 空格替换加号(str) { str = str.replace(/ /g, '+'); return str; } function 下载bat(域名,txt) { return [ `@echo off`, `chcp 65001`, `setlocal`, ``, `set "DOMAIN=${域名}"`, `set "txt=${txt}"`, ``, `rem %~nx1表示第一个参数的文件名和扩展名`, `set "FILENAME=%~nx1"`, ``, `rem PowerShell命令读取文件的前65行内容,将内容转换为UTF8并进行base64编码`, `for /f "delims=" %%i in ('powershell -command "$content = ((Get-Content -Path '%cd%/%FILENAME%' -Encoding UTF8) | Select-Object -First 65) -join [Environment]::NewLine; [convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($content))"') do set "BASE64_TEXT=%%i"`, ``, `rem 将内容保存到response.txt`, `rem echo %BASE64_TEXT% > response.txt`, ``, `rem 构造带有文件名和内容作为参数的URL`, `set "URL=https://%DOMAIN%/%FILENAME%?txt=%txt%^&b64=%BASE64_TEXT%"`, ``, `rem 显示请求的响应 `, `rem powershell -Command "(Invoke-WebRequest -Uri '%URL%').Content"`, `start %URL%`, `endlocal`, ``, `echo 更新数据完成,倒数5秒后自动关闭窗口...`, `timeout /t 5 >nul`, `exit` ].join('\r\n'); } function 下载sh(域名,txt) { return `#!/bin/bash export LANG=zh_CN.UTF-8 DOMAIN="${域名}" txt="${txt}" if [ -n "$1" ]; then FILENAME="$1" else echo "无文件名" exit 1 fi BASE64_TEXT=$(head -n 65 $FILENAME | base64 -i 0) curl -k "https://$DOMAIN/$FILENAME?txt=$txt&b64=$BASE64_TEXT" echo "更新数据完成" ` } function configHTML(域名, txt) { return ` CF-Workers-TEXT2KV

CF-Workers-TEXT2KV 配置信息

服务域名: ${域名}
txt: ${txt}

注意! 因URL长度内容所限,脚本更新方式一次最多更新65行内容

Windows脚本:
使用方法: <update.bat ip.txt>

Linux脚本: <curl https://${域名}/config/update.sh?txt=${txt} -o update.sh && chmod +x update.sh>
使用方法: <./update.sh ip.txt>


在线文档查询:
https://${域名}/?txt=${txt}


` }