Parcourir la source

fix(caddy): use basic_auth with import for htpasswd file

- basicauth (deprecated) -> basic_auth (new syntax)
- Use 'import /etc/caddy/htpasswd' to load credentials from file
- htpasswd format: 'username hash' (space separated, not colon)
- Use caddy hash-password to generate bcrypt hashes
lushdog@outlook.com il y a 1 mois
Parent
commit
2f39a52d44
3 fichiers modifiés avec 27 ajouts et 19 suppressions
  1. 3 1
      .env.example
  2. 18 15
      API_AUTH.md
  3. 6 3
      Caddyfile

+ 3 - 1
.env.example

@@ -9,5 +9,7 @@ SOL_ENDPOINT=https://lb.drpc.live/solana/YOUR_API_KEY
 SOL_SECRET_KEY=your_base58_encoded_secret_key
 
 # Basic Auth 配置
-# 生成 htpasswd 文件命令:docker run --rm httpd:alpine htpasswd -nbB admin your_password > htpasswd
+# 生成 htpasswd 文件:
+# HASH=$(docker run --rm caddy:2-alpine caddy hash-password --plaintext 'your_password')
+# echo "admin $HASH" > htpasswd
 # htpasswd 文件已添加到 .gitignore,不会被提交到 Git

+ 18 - 15
API_AUTH.md

@@ -18,25 +18,26 @@
 
 ### 1. 生成 htpasswd 文件
 
-Caddy 使用 htpasswd 文件存储用户名和密码。使用以下命令生成:
+Caddy 使用 bcrypt 哈希格式,htpasswd 文件格式为 `username hash`(空格分隔)。
 
 ```bash
-# 使用 Docker 生成 htpasswd 文件(推荐)
-docker run --rm httpd:alpine htpasswd -nbB admin your_password > htpasswd
+# 步骤 1:生成密码哈希
+docker run --rm caddy:2-alpine caddy hash-password --plaintext 'your_password'
+# 输出示例:$2a$14$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
-# 示例输出(内容会写入 htpasswd 文件):
-# admin:$2y$05$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-```
+# 步骤 2:将用户名和哈希写入 htpasswd 文件(注意是空格分隔,不是冒号)
+echo 'admin $2a$14$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' > htpasswd
 
-**参数说明:**
+# 或者一步完成:
+HASH=$(docker run --rm caddy:2-alpine caddy hash-password --plaintext 'your_password')
+echo "admin $HASH" > htpasswd
+```
 
-- `-n`:输出到标准输出而不是文件
-- `-b`:使用批处理模式(密码在命令行提供)
-- `-B`:使用 bcrypt 加密(Caddy 要求)
+**注意:** Caddy 的格式是 `username hash`(空格分隔),不是 httpd 的 `username:hash`(冒号分隔)。
 
 ### 2. 配置 htpasswd 文件
 
-生成的 `htpasswd` 文件会自动被 Caddy 读取(已通过 docker-compose 挂载到容器中)。
+生成的 `htpasswd` 文件会通过 Caddyfile 的 `import` 导入(已通过 docker-compose 挂载到容器中)。
 
 **注意**:`htpasswd` 文件已添加到 `.gitignore`,不会被提交到 Git。
 
@@ -260,8 +261,9 @@ services:
 在服务器上执行:
 
 ```bash
-# 生成 htpasswd 文件
-docker run --rm httpd:alpine htpasswd -nbB admin your_password > htpasswd
+# 生成密码哈希并写入 htpasswd 文件
+HASH=$(docker run --rm caddy:2-alpine caddy hash-password --plaintext 'your_password')
+echo "admin $HASH" > htpasswd
 
 # 查看生成的文件
 cat htpasswd
@@ -330,8 +332,9 @@ A: 联系项目管理员获取用户名和密码。密码存储在服务器的 `
 A: 在服务器上重新生成 htpasswd 文件:
 
 ```bash
-# 生成新密码
-docker run --rm httpd:alpine htpasswd -nbB admin new_password > htpasswd
+# 生成新密码哈希并写入 htpasswd 文件
+HASH=$(docker run --rm caddy:2-alpine caddy hash-password --plaintext 'new_password')
+echo "admin $HASH" > htpasswd
 
 # 重启服务
 docker compose restart caddy

+ 6 - 3
Caddyfile

@@ -1,8 +1,11 @@
 love.hdlife.me {
-	# Basic Auth - 使用 htpasswd 文件
+	# Basic Auth - 用户名和 bcrypt 哈希从外部文件导入
 	# 生成命令(在服务器上执行):
-	# docker run --rm httpd:alpine htpasswd -nbB admin your_password > htpasswd
-	basicauth * htpasswd
+	# docker run --rm caddy:2-alpine caddy hash-password --plaintext 'your_password'
+	# 然后将用户名和哈希写入 htpasswd 文件,格式为:username $2a$14$xxxxx
+	basic_auth * {
+		import /etc/caddy/htpasswd
+	}
 
 	# 反向代理到 byreal-table 服务
 	reverse_proxy byreal-table:3000