侧边栏壁纸
  • 累计撰写 29 篇文章
  • 累计创建 31 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

VolaryDDNS地址解析工具源码

Administrator
2026-01-12 / 0 评论 / 0 点赞 / 47 阅读 / 0 字
// index.js (这是 Cloudflare Worker 的主要文件)

export default {
  async fetch(request, env) {
    const url = new URL(request.url);

    // 1. 如果是 POST 请求,说明用户提交了表单
    if (request.method === "POST" && url.pathname === "/update") {
      try {
        const formData = await request.formData();
        const userToken = formData.get('token');
        const targetIp = formData.get('ip');

        if (!userToken || !targetIp) {
          return new Response("错误:Token 或 IP 不能为空。", { status: 400 });
        }

        const apiUrl = "https://volaryddns.net/api/update";

        const apiRequestInit = {
          method: "POST",
          headers: {
            "Content-Type": "application/json;charset=UTF-8",
          },
          body: JSON.stringify({
            token: userToken,
            ip: targetIp
          }),
        };

        const apiResponse = await fetch(apiUrl, apiRequestInit);
        const apiResult = await apiResponse.json(); // 假设 API 返回 JSON

        // 返回 API 的原始结果给用户
        return new Response(JSON.stringify(apiResult, null, 2), {
          headers: { "Content-Type": "application/json" },
          status: apiResponse.status // 保持原始 API 的状态码
        });

      } catch (error) {
        return new Response(`处理请求失败: ${error.message}`, { status: 500 });
      }
    }

    // 2. 如果是 GET 请求或访问根路径,返回 HTML 表单页面
    return new Response(htmlPage, {
      headers: {
        "Content-Type": "text/html;charset=UTF-8",
      },
    });
  },
};

// HTML 页面内容
const htmlPage = `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DDNS 地址更新器</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; background-color: #f4f4f4; color: #333; }
        .container { max-width: 500px; margin: 50px auto; padding: 20px; background-color: #fff; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
        h1 { text-align: center; color: #0056b3; }
        label { display: block; margin-bottom: 8px; font-weight: bold; }
        input[type="text"] { width: calc(100% - 22px); padding: 10px; margin-bottom: 20px; border: 1px solid #ccc; border-radius: 4px; }
        button { width: 100%; padding: 12px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; transition: background-color 0.3s ease; }
        button:hover { background-color: #0056b3; }
        #responseArea { margin-top: 20px; padding: 15px; background-color: #e9ecef; border-radius: 4px; white-space: pre-wrap; word-wrap: break-word; font-family: monospace; }
        .success { color: green; }
        .error { color: red; }
    </style>
</head>
<body>
    <div class="container">
        <h1>DDNS 地址更新</h1>
        <form id="updateForm">
            <label for="token">您的 Token:</label>
            <input type="text" id="token" name="token" placeholder="请输入您的 DDNS Token" required>

            <label for="ip">目标 IP 地址:</label>
            <input type="text" id="ip" name="ip" placeholder="请输入要解析到的 IP 地址 (例如: 1.2.3.4)" pattern="^([0-9]{1,3}\.){3}[0-9]{1,3}$" required>

            <button type="submit">更新解析</button>
        </form>
        <div id="responseArea"></div>
    </div>

    <script>
        document.getElementById('updateForm').addEventListener('submit', async function(event) {
            event.preventDefault(); // 阻止表单默认提交行为

            const token = document.getElementById('token').value;
            const ip = document.getElementById('ip').value;
            const responseArea = document.getElementById('responseArea');

            responseArea.innerHTML = '正在更新...';
            responseArea.className = ''; // 清除之前的状态样式

            try {
                const response = await fetch('/update', { // 注意这里是相对路径 /update
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded' // 表单提交的MIME类型
                    },
                    body: new URLSearchParams({ token: token, ip: ip })
                });

                const result = await response.json(); // 假设返回 JSON
                
                let displayMessage = '';
                if (result.success) {
                    displayMessage = \`<span class="success">更新成功!</span><br>\`;
                } else {
                    displayMessage = \`<span class="error">更新失败!</span><br>\`;
                }
                displayMessage += JSON.stringify(result, null, 2);
                
                responseArea.innerHTML = displayMessage;
                responseArea.className = result.success ? 'success' : 'error';

            } catch (error) {
                responseArea.innerHTML = \`<span class="error">请求出错:\${error.message}</span>\`;
                responseArea.className = 'error';
                console.error('Fetch error:', error);
            }
        });
    </script>
</body>
</html>
`;
0

评论区