Wetty = Web + TTY,基於 Node.js + xterm.js + WebSocket,提供瀏覽器原生終端體驗,支援 SSH 連線、公鑰免密、TLS 加密、多用戶隔離。
# CentOS/RHEL (EPEL)
yum install -y epel-release
yum install -y nodejs npm git
# Ubuntu/Debian
apt-get update
apt-get install -y nodejs npm git
git clone https://github.com/krishnasrinivas/wetty.git
cd wetty
npm install # 生產環境建議 npm ci --production
# 編譯 TypeScript (若來源為 TS 版本)
npm run build 2>/dev/null || true
openssl req -x509 -newkey rsa:2048 -nodes \
-keyout key.pem -out cert.pem -days 365 \
-subj "/CN=$(hostname)"
cat > /etc/systemd/system/wetty.service <<'EOF'
[Unit]
Description=Wetty Web Terminal
After=network.target
[Service]
Type=simple
User=wetty
WorkingDirectory=/opt/wetty
ExecStart=/usr/bin/node app.js --sslkey key.pem --sslcert cert.pem -p 3000 --sshauth=publickey --base /wetty/
Restart=on-failure
RestartSec=5
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
EOF
# 建立專用用戶
useradd -r -s /bin/false -d /opt/wetty wetty
chown -R wetty:wetty /opt/wetty
systemctl daemon-reload
systemctl enable --now wetty
ssh-keygen -t ed25519 -f ~/.ssh/wetty_key~/.ssh/authorized_keys:cat ~/.ssh/wetty_key.pub >> ~/.ssh/authorized_keys--sshauth=publickey 並確保私鑰可被 Wetty 進程讀取 (放在 /opt/wetty/.ssh/,權限 600)。https://host:3000/wetty/ssh/ 直接進入 Shell,無需輸入密碼。# 編輯 app.js (或 src/App.ts 重新建構)
# 找到 sshauth 默認值
const DEFAULT_SSH_AUTH = 'publickey'; // 原為 'password'
# 重新建構並重啟服務
server {
listen 443 ssl http2;
server_name terminal.example.com;
ssl_certificate /etc/letsencrypt/live/terminal.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/terminal.example.com/privkey.pem;
location /wetty/ {
proxy_pass https://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 86400;
}
}
fail2ban 監聽 Wetty 登入失敗日誌。journalctl -u wetty -f 監控連線行為。