{"slug":"warden-worker","title":"你可曾想过，直接将BitWarden部署到Cloudflare Worker？","description":"warden-worker就是这样一个项目，它将Rust编译为WASM，然后部署到Cloudflare Worker，无需VPS，无需家里云，只需点点鼠标就可免费用上自己的密码托管！","coverImage":"https://b.acofork.com/img/warden-worker-warden-worker.webp","coverWidth":871,"coverHeight":858,"date":"2026-01-26","tags":["服务器"],"pinned":false,"wordCount":465,"html":"<h2 id=\"原理\">原理</h2><p>项目参考开源的 <a href=\"https://github.com/dani-garcia/vaultwarden\" target=\"_blank\" rel=\"noopener noreferrer\">dani-garcia/vaultwarden: Unofficial Bitwarden compatible server written in Rust, formerly known as bitwarden_rs</a> 将Rust源码编译为WASM以支持在Cloudflare Worker上运行。其中Worker负责REST API，D1负责存储加密后的数据</p>\n<h2 id=\"部署\">部署</h2><p>首先确保你安装了Rust，若无可前往： <a href=\"https://rust-lang.org/zh-CN/tools/install/\" target=\"_blank\" rel=\"noopener noreferrer\">安装 Rust - Rust 程序设计语言</a></p>\n<p>克隆仓库： <a href=\"https://github.com/afoim/warden-worker\" target=\"_blank\" rel=\"noopener noreferrer\">afoim/warden-worker: A Bitwarden-compatible server for Cloudflare Workers</a></p>\n<p>创建D1数据库</p>\n<div class=\"markdown-code\" data-language=\"sql\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>sql</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-sql\">wrangler d1 <span class=\"hljs-keyword\">create</span> warden<span class=\"hljs-operator\">-</span><span class=\"hljs-keyword\">sql</span></code></pre></div><p><img src=\"https://b.acofork.com/img/warden-worker-warden-worker-25.webp\" alt=\"\" width=\"1242\" height=\"532\" loading=\"eager\" decoding=\"async\" /></p>\n<p>替换 <strong>wrangler.jsonc</strong> 的数据库ID</p>\n<p><img src=\"https://b.acofork.com/img/warden-worker-warden-worker-26.webp\" alt=\"\" width=\"969\" height=\"1018\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>初始化数据库</p>\n<div class=\"markdown-code\" data-language=\"sql\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>sql</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-sql\">wrangler d1 <span class=\"hljs-keyword\">execute</span> warden<span class=\"hljs-operator\">-</span><span class=\"hljs-keyword\">sql</span> <span class=\"hljs-comment\">--remote --file=sql/schema_full.sql</span></code></pre></div><p><img src=\"https://b.acofork.com/img/warden-worker-warden-worker-27.webp\" alt=\"\" width=\"1585\" height=\"720\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>编译Rust WASM</p>\n<div class=\"markdown-code\" data-language=\"bash\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>bash</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-bash\">cargo build --release</code></pre></div><p>部署 Worker</p>\n<div class=\"markdown-code\" data-language=\"bash\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>bash</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-bash\">wrangler deploy</code></pre></div><p>设置白名单邮箱</p>\n<div class=\"markdown-code\" data-language=\"bash\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>bash</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-bash\">wrangler secret put ALLOWED_EMAILS</code></pre></div><p><img src=\"https://b.acofork.com/img/warden-worker-warden-worker-29.webp\" alt=\"\" width=\"1184\" height=\"216\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>设置JWT（脸滚键盘即可）</p>\n<div class=\"markdown-code\" data-language=\"bash\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>bash</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-bash\">wrangler secret put JWT_SECRET\nwrangler secret put JWT_REFRESH_SECRET</code></pre></div><p><img src=\"https://b.acofork.com/img/warden-worker-warden-worker-30.webp\" alt=\"\" width=\"1237\" height=\"420\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>设置2FA加密密钥（32字节Base64编码文本）</p>\n<div class=\"markdown-code\" data-language=\"bash\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>bash</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-bash\">wrangler secret put TWO_FACTOR_ENC_KEY</code></pre></div><p>Poweshell可以这样生成</p>\n<div class=\"markdown-code\" data-language=\"powershell\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M12 19h8\"></path><path d=\"m4 17 6-6-6-6\"></path></svg><span>powershell</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-powershell\">[<span class=\"hljs-type\">Convert</span>]::ToBase64String((<span class=\"hljs-number\">1</span>..<span class=\"hljs-number\">32</span> | <span class=\"hljs-built_in\">ForEach-Object</span> {<span class=\"hljs-built_in\">Get-Random</span> <span class=\"hljs-literal\">-Minimum</span> <span class=\"hljs-number\">0</span> <span class=\"hljs-literal\">-Maximum</span> <span class=\"hljs-number\">256</span>}))</code></pre></div><p><img src=\"https://b.acofork.com/img/warden-worker-warden-worker-31.webp\" alt=\"\" width=\"1612\" height=\"318\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>前往控制台绑定域名（若路由需要手动写一条解析到Cloudflare）</p>\n<p><img src=\"https://b.acofork.com/img/warden-worker-warden-worker-28.webp\" alt=\"\" width=\"1575\" height=\"614\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>使用移动端Bitwarden创建账号（使用白名单邮箱）</p>\n<p>接下来，前往网页端（ <code>/demo.html</code> 默认使用的是 Vaultwarden的前端，可能会有些Bug），启用2FA： <a href=\"https://cfbw.2x.nz\" target=\"_blank\" rel=\"noopener noreferrer\">https://cfbw.2x.nz</a> （用另一个TOTP验证器存储）</p>\n<p><em>顺便一提，想要修改邮箱或主密码也可以在网页端进行了</em></p>\n<p><img src=\"https://b.acofork.com/img/warden-worker-warden-worker-32.webp\" alt=\"\" width=\"1212\" height=\"1368\" loading=\"lazy\" decoding=\"async\" /></p>\n<p>将所有已登录的设备登出后再登入则会被要求TOTP</p>\n<h2 id=\"导入密码库\">导入密码库</h2><p>如果您有旧的密码库，请先前往 <strong>设置 - 密码库选项 - 导出 - .json</strong> </p>\n<p>再登录当前密码库，前往 <strong>设置 - 密码库选项 - 导入 - .json</strong></p>\n<p><img src=\"https://b.acofork.com/img/warden-worker-warden-worker-33.webp\" alt=\"\" width=\"552\" height=\"936\" loading=\"lazy\" decoding=\"async\" /></p>\n","toc":[{"id":"原理","text":"原理","depth":2},{"id":"部署","text":"部署","depth":2},{"id":"导入密码库","text":"导入密码库","depth":2}]}