feat: support WEBUI_PASSWORD env var with default password

Set default WebUI password to "proxygo" and allow customization via
WEBUI_PASSWORD environment variable, eliminating the need to manually
compute SHA256 hashes and modify source code.
This commit is contained in:
jonasen1988
2026-03-27 19:48:36 +08:00
parent f678e2e016
commit f2c3fbac24
4 changed files with 36 additions and 10 deletions

View File

@@ -176,18 +176,22 @@ docker compose up -d --build
### 登录密码说明
当前版本在代码里只保存了 WebUI 密码的 SHA256 哈希值,默认明文密码没有在仓库中说明,也不能通过 `config.json` 或 WebUI 修改
默认密码为 `proxygo`,程序启动时会在日志中提示
如果你要自定义密码,当前可行方式是
1. 生成密码的 SHA256
2. 修改 `config/config.go` 中的 `WebUIPasswordHash`
3. 重新构建并启动程序
例如生成 SHA256
可通过环境变量 `WEBUI_PASSWORD` 自定义密码
```bash
printf 'your-password' | shasum -a 256
# 本地运行
WEBUI_PASSWORD=your-password go run .
# Docker
docker run -e WEBUI_PASSWORD=your-password ...
```
也可以在 `docker-compose.yml``environment` 中添加:
```yaml
- WEBUI_PASSWORD=your-password
```
## API 概览

View File

@@ -1,11 +1,15 @@
package config
import (
"crypto/sha256"
"encoding/json"
"fmt"
"os"
"sync"
)
const DefaultPassword = "proxygo"
func dataDir() string {
if d := os.Getenv("DATA_DIR"); d != "" {
os.MkdirAll(d, 0755)
@@ -63,10 +67,19 @@ var (
cfgMu sync.RWMutex
)
func passwordHash(plain string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(plain)))
}
func DefaultConfig() *Config {
// 优先从环境变量 WEBUI_PASSWORD 读取密码,未设置时使用默认密码
password := os.Getenv("WEBUI_PASSWORD")
if password == "" {
password = DefaultPassword
}
return &Config{
WebUIPort: ":7778",
WebUIPasswordHash: "64c2de42ff93286f5c7108867ffe3167a24f4c1abee648dea7bc7fa1d11e2b21",
WebUIPasswordHash: passwordHash(password),
ProxyPort: ":7777",
DBPath: dataDir() + "proxy.db",
ValidateConcurrency: 300,

View File

@@ -11,6 +11,7 @@ services:
environment:
- TZ=Asia/Shanghai
- DATA_DIR=/app/data
# - WEBUI_PASSWORD=your-password # 自定义 WebUI 登录密码,默认: proxygo
healthcheck:
test: ["CMD", "wget", "-qO-", "http://localhost:7778/"]
interval: 30s

View File

@@ -2,6 +2,7 @@ package main
import (
"log"
"os"
"sync"
"sync/atomic"
"time"
@@ -26,6 +27,13 @@ func main() {
// 加载配置(优先读取 config.json
cfg := config.Load()
// 提示密码信息
if os.Getenv("WEBUI_PASSWORD") == "" {
log.Printf("[main] WebUI 使用默认密码: %s可通过环境变量 WEBUI_PASSWORD 自定义)", config.DefaultPassword)
} else {
log.Println("[main] WebUI 密码已通过环境变量 WEBUI_PASSWORD 设置")
}
// 初始化存储
store, err := storage.New(cfg.DBPath)
if err != nil {