Spaces:
Paused
Paused
Upload 6 files
Browse files- Dockerfile +10 -7
- README.md +21 -26
- nginx.conf +40 -4
- oauth2-proxy.cfg +40 -0
- start.sh +11 -11
Dockerfile
CHANGED
|
@@ -7,9 +7,6 @@ ENV HOME=/home/user
|
|
| 7 |
ENV PATH=$HOME/.local/bin:$PATH
|
| 8 |
|
| 9 |
# Install essential system packages and build tools
|
| 10 |
-
# - nginx: Web server for static files (WASM) and reverse proxy
|
| 11 |
-
# - apache2-utils: For htpasswd (Basic Auth)
|
| 12 |
-
# - emscripten dependencies: python3, cmake, git, xz-utils
|
| 13 |
RUN apt-get update && apt-get install -y \
|
| 14 |
curl \
|
| 15 |
wget \
|
|
@@ -24,22 +21,27 @@ RUN apt-get update && apt-get install -y \
|
|
| 24 |
cmake \
|
| 25 |
pkg-config \
|
| 26 |
python3 \
|
| 27 |
-
python3-pip \
|
| 28 |
nginx \
|
| 29 |
-
apache2-utils \
|
| 30 |
xz-utils \
|
| 31 |
bzip2 \
|
|
|
|
| 32 |
&& apt-get clean && rm -rf /var/lib/apt/lists/*
|
| 33 |
|
| 34 |
# Install ttyd (Web Terminal)
|
| 35 |
RUN wget https://github.com/tsl0922/ttyd/releases/download/1.7.7/ttyd.x86_64 -O /usr/bin/ttyd \
|
| 36 |
&& chmod +x /usr/bin/ttyd
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# Install Emscripten (EMSDK)
|
| 39 |
-
# We install it to /opt/emsdk and make it available for all users
|
| 40 |
WORKDIR /opt
|
| 41 |
RUN git clone https://github.com/emscripten-core/emsdk.git
|
| 42 |
-
# 修正:将 cd 命令包含在 RUN 指令中
|
| 43 |
RUN cd emsdk && \
|
| 44 |
./emsdk install latest && \
|
| 45 |
./emsdk activate latest && \
|
|
@@ -65,6 +67,7 @@ USER user
|
|
| 65 |
|
| 66 |
# Copy configuration files
|
| 67 |
COPY --chown=user:user nginx.conf /etc/nginx/nginx.conf
|
|
|
|
| 68 |
COPY --chown=user:user start.sh .
|
| 69 |
COPY --chown=user:user build_wasm.sh .
|
| 70 |
|
|
|
|
| 7 |
ENV PATH=$HOME/.local/bin:$PATH
|
| 8 |
|
| 9 |
# Install essential system packages and build tools
|
|
|
|
|
|
|
|
|
|
| 10 |
RUN apt-get update && apt-get install -y \
|
| 11 |
curl \
|
| 12 |
wget \
|
|
|
|
| 21 |
cmake \
|
| 22 |
pkg-config \
|
| 23 |
python3 \
|
|
|
|
| 24 |
nginx \
|
|
|
|
| 25 |
xz-utils \
|
| 26 |
bzip2 \
|
| 27 |
+
ca-certificates \
|
| 28 |
&& apt-get clean && rm -rf /var/lib/apt/lists/*
|
| 29 |
|
| 30 |
# Install ttyd (Web Terminal)
|
| 31 |
RUN wget https://github.com/tsl0922/ttyd/releases/download/1.7.7/ttyd.x86_64 -O /usr/bin/ttyd \
|
| 32 |
&& chmod +x /usr/bin/ttyd
|
| 33 |
|
| 34 |
+
# Install oauth2-proxy
|
| 35 |
+
# Download v7.6.0 release
|
| 36 |
+
RUN wget https://github.com/oauth2-proxy/oauth2-proxy/releases/download/v7.6.0/oauth2-proxy-v7.6.0.linux-amd64.tar.gz \
|
| 37 |
+
&& tar -xzf oauth2-proxy-v7.6.0.linux-amd64.tar.gz \
|
| 38 |
+
&& mv oauth2-proxy-v7.6.0.linux-amd64/oauth2-proxy /usr/bin/oauth2-proxy \
|
| 39 |
+
&& chmod +x /usr/bin/oauth2-proxy \
|
| 40 |
+
&& rm -rf oauth2-proxy-v7.6.0.linux-amd64*
|
| 41 |
+
|
| 42 |
# Install Emscripten (EMSDK)
|
|
|
|
| 43 |
WORKDIR /opt
|
| 44 |
RUN git clone https://github.com/emscripten-core/emsdk.git
|
|
|
|
| 45 |
RUN cd emsdk && \
|
| 46 |
./emsdk install latest && \
|
| 47 |
./emsdk activate latest && \
|
|
|
|
| 67 |
|
| 68 |
# Copy configuration files
|
| 69 |
COPY --chown=user:user nginx.conf /etc/nginx/nginx.conf
|
| 70 |
+
COPY --chown=user:user oauth2-proxy.cfg .
|
| 71 |
COPY --chown=user:user start.sh .
|
| 72 |
COPY --chown=user:user build_wasm.sh .
|
| 73 |
|
README.md
CHANGED
|
@@ -14,7 +14,7 @@ license: mit
|
|
| 14 |
|
| 15 |
此环境配置了 `sudo` 权限,方便你在运行时安装所需的软件(如 OpenClaw)。
|
| 16 |
|
| 17 |
-
**新特性:支持
|
| 18 |
|
| 19 |
## 🚀 快速开始
|
| 20 |
|
|
@@ -27,43 +27,38 @@ license: mit
|
|
| 27 |
6. 点击 **Create Space**。
|
| 28 |
|
| 29 |
### 2. 上传文件
|
| 30 |
-
将本仓库中的所有文件(`Dockerfile`, `README.md`, `start.sh`, `nginx.conf`, `build_wasm.sh`)上传到你的 Space 仓库中。
|
| 31 |
|
| 32 |
### 3. 配置鉴权环境变量 (重要!)
|
| 33 |
-
在 Space 的 **Settings** -> **Variables and secrets** 中添加以下环境变量
|
| 34 |
|
| 35 |
-
| 变量名 | 描述 |
|
| 36 |
-
|--------|------|------
|
| 37 |
-
| `
|
| 38 |
-
| `
|
|
|
|
| 39 |
|
| 40 |
-
**
|
|
|
|
|
|
|
| 41 |
|
| 42 |
### 4. 访问 VPS
|
| 43 |
配置好环境变量后,Space 会自动重启。
|
| 44 |
-
点击 **App** 标签页,
|
| 45 |
|
| 46 |
## 🛠️ 功能特性
|
|
|
|
|
|
|
| 47 |
- **Web 终端**: 访问根路径 `/`,使用 `ttyd` 管理 VPS。
|
| 48 |
-
- **WASM 游戏预览**: 访问 `/game`,预览编译好的 WebAssembly 游戏
|
| 49 |
-
- **WASM 编译环境**: 预装 Emscripten (emsdk)
|
| 50 |
-
- **Nginx 反代**: 统一管理路由和 Basic Auth 鉴权,稳定可靠。
|
| 51 |
|
| 52 |
## 🎮 如何编译 OpenClaw 为 WASM
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
``
|
| 57 |
-
# 运行辅助构建脚本
|
| 58 |
-
./build_wasm.sh
|
| 59 |
-
```
|
| 60 |
-
|
| 61 |
-
如果编译成功,访问 `https://<你的Space地址>/game` 即可看到游戏界面。
|
| 62 |
-
|
| 63 |
-
### 注意事项
|
| 64 |
-
- **图形界面限制**: 直接运行 `./OpenClaw` 可能会报错 `Could not initialize SDL: No available video device`,因为 Space 没有连接显示器。
|
| 65 |
-
- **持久化存储**: Hugging Face Spaces 重启后,非 `/data` 目录下的文件会丢失。建议将重要数据保存在 `/data` 目录(如果启用了 Persistent Storage)或使用 Git 同步代码。
|
| 66 |
|
| 67 |
## ⚠️ 安全警告
|
| 68 |
-
- 此 VPS 拥有 root 权限
|
| 69 |
-
-
|
|
|
|
| 14 |
|
| 15 |
此环境配置了 `sudo` 权限,方便你在运行时安装所需的软件(如 OpenClaw)。
|
| 16 |
|
| 17 |
+
**新特性:采用 Nginx + oauth2-proxy 架构,支持稳定的 GitHub/Google OAuth 登录与 WebAssembly 游戏预览。**
|
| 18 |
|
| 19 |
## 🚀 快速开始
|
| 20 |
|
|
|
|
| 27 |
6. 点击 **Create Space**。
|
| 28 |
|
| 29 |
### 2. 上传文件
|
| 30 |
+
将本仓库中的所有文件(`Dockerfile`, `README.md`, `start.sh`, `nginx.conf`, `oauth2-proxy.cfg`, `build_wasm.sh`)上传到你的 Space 仓库中。
|
| 31 |
|
| 32 |
### 3. 配置鉴权环境变量 (重要!)
|
| 33 |
+
在 Space 的 **Settings** -> **Variables and secrets** 中添加以下环境变量:
|
| 34 |
|
| 35 |
+
| 变量名 | 描述 | 示例 |
|
| 36 |
+
|--------|------|------|
|
| 37 |
+
| `OAUTH2_PROXY_CLIENT_ID` | OAuth Client ID (GitHub 或 Google) | `Ov23li...` |
|
| 38 |
+
| `OAUTH2_PROXY_CLIENT_SECRET` | OAuth Client Secret | `a1b2c3...` |
|
| 39 |
+
| `OAUTH2_PROXY_COOKIE_SECRET` | Cookie 加密密钥 (16/24/32字节) | `python3 -c 'import os,base64; print(base64.b64encode(os.urandom(16)).decode())'` 生成一个 |
|
| 40 |
|
| 41 |
+
**回调地址配置**:
|
| 42 |
+
* GitHub App Callback URL: `https://<你的Space地址>/oauth2/callback`
|
| 43 |
+
* Google Client Redirect URI: `https://<你的Space地址>/oauth2/callback`
|
| 44 |
|
| 45 |
### 4. 访问 VPS
|
| 46 |
配置好环境变量后,Space 会自动重启。
|
| 47 |
+
点击 **App** 标签页,你会看到 GitHub/Google 登录页面。登录成功后即可进入终端。
|
| 48 |
|
| 49 |
## 🛠️ 功能特性
|
| 50 |
+
- **OAuth 鉴权**: 使用 oauth2-proxy,安全稳定,支持 Session 管理。
|
| 51 |
+
- **Nginx 反代**: 高性能反向代理,处理静态文件和 WebSocket。
|
| 52 |
- **Web 终端**: 访问根路径 `/`,使用 `ttyd` 管理 VPS。
|
| 53 |
+
- **WASM 游戏预览**: 访问 `/game`,预览编译好的 WebAssembly 游戏。
|
| 54 |
+
- **WASM 编译环境**: 预装 Emscripten (emsdk)。
|
|
|
|
| 55 |
|
| 56 |
## 🎮 如何编译 OpenClaw 为 WASM
|
| 57 |
|
| 58 |
+
1. 登录终端。
|
| 59 |
+
2. 运行 `./build_wasm.sh`。
|
| 60 |
+
3. 如果编译成功,访问 `https://<你的Space地址>/game` 即可玩游戏。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
## ⚠️ 安全警告
|
| 63 |
+
- 此 VPS 拥有 root 权限。
|
| 64 |
+
- oauth2-proxy 默认配置允许 GitHub 上的任何用户登录。**强烈建议**修改 `oauth2-proxy.cfg` 中的 `email_domains` 或设置 `authenticated_emails_file` 来限制特定用户。
|
nginx.conf
CHANGED
|
@@ -26,13 +26,40 @@ http {
|
|
| 26 |
|
| 27 |
server {
|
| 28 |
listen 7860;
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# 1. Terminal (ttyd) - Root Path
|
| 35 |
location / {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
proxy_pass http://127.0.0.1:7681;
|
| 37 |
proxy_http_version 1.1;
|
| 38 |
proxy_set_header Upgrade $http_upgrade;
|
|
@@ -41,10 +68,19 @@ http {
|
|
| 41 |
proxy_set_header X-Real-IP $remote_addr;
|
| 42 |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
| 43 |
proxy_set_header X-Forwarded-Proto $scheme;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
# 2. Game (WASM) - /game Path
|
| 47 |
location /game {
|
|
|
|
|
|
|
|
|
|
| 48 |
alias /var/www/html/game;
|
| 49 |
index index.html;
|
| 50 |
|
|
|
|
| 26 |
|
| 27 |
server {
|
| 28 |
listen 7860;
|
| 29 |
+
server_name localhost;
|
| 30 |
+
|
| 31 |
+
# =========================================================
|
| 32 |
+
# OAuth2 Proxy Authentication
|
| 33 |
+
# =========================================================
|
| 34 |
+
|
| 35 |
+
location /oauth2/ {
|
| 36 |
+
proxy_pass http://127.0.0.1:4180;
|
| 37 |
+
proxy_set_header Host $host;
|
| 38 |
+
proxy_set_header X-Real-IP $remote_addr;
|
| 39 |
+
proxy_set_header X-Scheme $scheme;
|
| 40 |
+
proxy_set_header X-Auth-Request-Redirect $request_uri;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
location = /oauth2/auth {
|
| 44 |
+
proxy_pass http://127.0.0.1:4180;
|
| 45 |
+
proxy_set_header Host $host;
|
| 46 |
+
proxy_set_header X-Real-IP $remote_addr;
|
| 47 |
+
proxy_set_header X-Scheme $scheme;
|
| 48 |
+
# nginx auth_request includes headers but not body
|
| 49 |
+
proxy_set_header Content-Length "";
|
| 50 |
+
proxy_pass_request_body off;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# =========================================================
|
| 54 |
+
# Protected Resources
|
| 55 |
+
# =========================================================
|
| 56 |
|
| 57 |
# 1. Terminal (ttyd) - Root Path
|
| 58 |
location / {
|
| 59 |
+
auth_request /oauth2/auth;
|
| 60 |
+
error_page 401 = /oauth2/sign_in;
|
| 61 |
+
|
| 62 |
+
# If authenticated, pass to ttyd
|
| 63 |
proxy_pass http://127.0.0.1:7681;
|
| 64 |
proxy_http_version 1.1;
|
| 65 |
proxy_set_header Upgrade $http_upgrade;
|
|
|
|
| 68 |
proxy_set_header X-Real-IP $remote_addr;
|
| 69 |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
| 70 |
proxy_set_header X-Forwarded-Proto $scheme;
|
| 71 |
+
|
| 72 |
+
# Pass user info from oauth2-proxy (optional)
|
| 73 |
+
auth_request_set $user $upstream_http_x_auth_request_user;
|
| 74 |
+
auth_request_set $email $upstream_http_x_auth_request_email;
|
| 75 |
+
proxy_set_header X-User $user;
|
| 76 |
+
proxy_set_header X-Email $email;
|
| 77 |
}
|
| 78 |
|
| 79 |
# 2. Game (WASM) - /game Path
|
| 80 |
location /game {
|
| 81 |
+
auth_request /oauth2/auth;
|
| 82 |
+
error_page 401 = /oauth2/sign_in;
|
| 83 |
+
|
| 84 |
alias /var/www/html/game;
|
| 85 |
index index.html;
|
| 86 |
|
oauth2-proxy.cfg
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# oauth2-proxy configuration
|
| 2 |
+
|
| 3 |
+
# Listen address
|
| 4 |
+
http_address = "127.0.0.1:4180"
|
| 5 |
+
|
| 6 |
+
# Email Domains to allow authentication for (this authorizes any email on this domain)
|
| 7 |
+
# For public access restricted by specific emails, use email_domains = ["*"] and set authenticated_emails_file
|
| 8 |
+
email_domains = ["*"]
|
| 9 |
+
|
| 10 |
+
# The OAuth Client ID, Secret
|
| 11 |
+
# These will be injected via environment variables: OAUTH2_PROXY_CLIENT_ID, OAUTH2_PROXY_CLIENT_SECRET
|
| 12 |
+
# client_id = "..."
|
| 13 |
+
# client_secret = "..."
|
| 14 |
+
|
| 15 |
+
# Cookie Settings
|
| 16 |
+
cookie_secret = "OAUTH2_PROXY_COOKIE_SECRET_RANDOM_123"
|
| 17 |
+
cookie_secure = true
|
| 18 |
+
cookie_httponly = true
|
| 19 |
+
cookie_refresh = "1h"
|
| 20 |
+
cookie_expire = "168h"
|
| 21 |
+
|
| 22 |
+
# Provider Settings
|
| 23 |
+
# We will use GitHub by default, but can be configured for Google
|
| 24 |
+
provider = "github"
|
| 25 |
+
|
| 26 |
+
# Upstreams
|
| 27 |
+
# We don't use oauth2-proxy to proxy traffic directly (Nginx does that via auth_request)
|
| 28 |
+
# So we point it to a dummy static response or itself
|
| 29 |
+
upstreams = [
|
| 30 |
+
"http://127.0.0.1:4180/static"
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
# Redirect URL
|
| 34 |
+
# This should match your HF Space URL + /oauth2/callback
|
| 35 |
+
# redirect_url = "https://<your-space>.hf.space/oauth2/callback"
|
| 36 |
+
|
| 37 |
+
# Logging
|
| 38 |
+
request_logging = true
|
| 39 |
+
auth_logging = true
|
| 40 |
+
standard_logging = true
|
start.sh
CHANGED
|
@@ -1,19 +1,19 @@
|
|
| 1 |
#!/bin/bash
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
echo "Generating .htpasswd for user: $USER"
|
| 9 |
-
htpasswd -bc /etc/nginx/.htpasswd "$USER" "$PASSWORD"
|
| 10 |
-
|
| 11 |
-
# 启动 ttyd (本地监听,无 Auth,由 Nginx 接管)
|
| 12 |
-
# -i 127.0.0.1: 仅监听本地
|
| 13 |
-
# -W: 允许写入
|
| 14 |
echo "Starting ttyd on 127.0.0.1:7681..."
|
| 15 |
ttyd -p 7681 -i 127.0.0.1 -W bash &
|
| 16 |
|
| 17 |
-
# 启动
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
echo "Starting Nginx on port 7860..."
|
| 19 |
nginx -g "daemon off;"
|
|
|
|
| 1 |
#!/bin/bash
|
| 2 |
|
| 3 |
+
# 确保必要的环境变量已设置,否则退出或使用默认值
|
| 4 |
+
export OAUTH2_PROXY_CLIENT_ID=${OAUTH2_PROXY_CLIENT_ID:-"your_client_id"}
|
| 5 |
+
export OAUTH2_PROXY_CLIENT_SECRET=${OAUTH2_PROXY_CLIENT_SECRET:-"your_client_secret"}
|
| 6 |
+
export OAUTH2_PROXY_COOKIE_SECRET=${OAUTH2_PROXY_COOKIE_SECRET:-$(python3 -c 'import os,base64; print(base64.b64encode(os.urandom(16)).decode())')}
|
| 7 |
|
| 8 |
+
# 1. 启动 ttyd (本地监听)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
echo "Starting ttyd on 127.0.0.1:7681..."
|
| 10 |
ttyd -p 7681 -i 127.0.0.1 -W bash &
|
| 11 |
|
| 12 |
+
# 2. 启动 oauth2-proxy (本地监听)
|
| 13 |
+
echo "Starting oauth2-proxy on 127.0.0.1:4180..."
|
| 14 |
+
# 使用配置文件启动,环境变量会自动覆盖配置中的值
|
| 15 |
+
oauth2-proxy --config=oauth2-proxy.cfg &
|
| 16 |
+
|
| 17 |
+
# 3. 启动 Nginx (对外监听 7860)
|
| 18 |
echo "Starting Nginx on port 7860..."
|
| 19 |
nginx -g "daemon off;"
|