darkfire514 commited on
Commit
cf4f9f7
·
verified ·
1 Parent(s): c57e208

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +3 -1
  2. oauth2-proxy.cfg +5 -13
  3. start.sh +33 -6
README.md CHANGED
@@ -34,6 +34,7 @@ license: mit
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())'` 生成一个 |
@@ -48,6 +49,7 @@ license: mit
48
 
49
  ## 🛠️ 功能特性
50
  - **OAuth 鉴权**: 使用 oauth2-proxy,安全稳定,支持 Session 管理。
 
51
  - **Nginx 反代**: 高性能反向代理,处理静态文件和 WebSocket。
52
  - **Web 终端**: 访问根路径 `/`,使用 `ttyd` 管理 VPS。
53
  - **WASM 游戏预览**: 访问 `/game`,预览编译好的 WebAssembly 游戏。
@@ -61,4 +63,4 @@ license: mit
61
 
62
  ## ⚠️ 安全警告
63
  - 此 VPS 拥有 root 权限。
64
- - oauth2-proxy 默认配允许 GitHub 上的任何用户登录。**强烈建议**修改 `oauth2-proxy.cfg` 中的 `email_domains` 或设置 `authenticated_emails_file` 来限制特定用户
 
34
 
35
  | 变量名 | 描述 | 示例 |
36
  |--------|------|------|
37
+ | `ALLOWED_USERS` | **必填**。允许登录的 GitHub 用户名或 Email,逗号分隔。 | `yourgithubuser,your@email.com` |
38
  | `OAUTH2_PROXY_CLIENT_ID` | OAuth Client ID (GitHub 或 Google) | `Ov23li...` |
39
  | `OAUTH2_PROXY_CLIENT_SECRET` | OAuth Client Secret | `a1b2c3...` |
40
  | `OAUTH2_PROXY_COOKIE_SECRET` | Cookie 加密密钥 (16/24/32字节) | `python3 -c 'import os,base64; print(base64.b64encode(os.urandom(16)).decode())'` 生成一个 |
 
49
 
50
  ## 🛠️ 功能特性
51
  - **OAuth 鉴权**: 使用 oauth2-proxy,安全稳定,支持 Session 管理。
52
+ - **白名单机制**: 仅允许 `ALLOWED_USERS` 中的用户登录,拒绝陌生人访问。
53
  - **Nginx 反代**: 高性能反向代理,处理静态文件和 WebSocket。
54
  - **Web 终端**: 访问根路径 `/`,使用 `ttyd` 管理 VPS。
55
  - **WASM 游戏预览**: 访问 `/game`,预览编译好的 WebAssembly 游戏。
 
63
 
64
  ## ⚠️ 安全警告
65
  - 此 VPS 拥有 root 权限。
66
+ - 请务必正确设`ALLOWED_USERS`,否则虽然能登录,但可能被 oauth2-proxy 拒绝(如果未设置则可能开放给所有人,具体取决于配置)
oauth2-proxy.cfg CHANGED
@@ -3,14 +3,13 @@
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"
@@ -20,20 +19,13 @@ 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
 
3
  # Listen address
4
  http_address = "127.0.0.1:4180"
5
 
6
+ # Email Domains
7
+ # We restrict access using authenticated_emails_file, so email_domains can be "*"
8
  email_domains = ["*"]
9
 
10
+ # Authenticated Emails File
11
+ # Only emails listed in this file will be allowed to login
12
+ authenticated_emails_file = "/etc/oauth2-proxy/authenticated_emails.txt"
 
13
 
14
  # Cookie Settings
15
  cookie_secret = "OAUTH2_PROXY_COOKIE_SECRET_RANDOM_123"
 
19
  cookie_expire = "168h"
20
 
21
  # Provider Settings
 
22
  provider = "github"
23
 
24
  # Upstreams
 
 
25
  upstreams = [
26
  "http://127.0.0.1:4180/static"
27
  ]
28
 
 
 
 
 
29
  # Logging
30
  request_logging = true
31
  auth_logging = true
start.sh CHANGED
@@ -1,18 +1,45 @@
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..."
 
1
  #!/bin/bash
2
 
3
+ # 确保必要的环境变量已设置
4
+ if [ -z "$OAUTH2_PROXY_CLIENT_ID" ] || [ "$OAUTH2_PROXY_CLIENT_ID" == "your_client_id" ]; then
5
+ echo "Error: OAUTH2_PROXY_CLIENT_ID is not set in Hugging Face Secrets!"
6
+ fi
7
+
8
+ if [ -z "$OAUTH2_PROXY_CLIENT_SECRET" ] || [ "$OAUTH2_PROXY_CLIENT_SECRET" == "your_client_secret" ]; then
9
+ echo "Error: OAUTH2_PROXY_CLIENT_SECRET is not set in Hugging Face Secrets!"
10
+ fi
11
+
12
+ # 生成默认 Cookie Secret (如果未设置)
13
+ if [ -z "$OAUTH2_PROXY_COOKIE_SECRET" ]; then
14
+ echo "Generating temporary cookie secret..."
15
+ export OAUTH2_PROXY_COOKIE_SECRET=$(python3 -c 'import os,base64; print(base64.b64encode(os.urandom(16)).decode())')
16
+ fi
17
+
18
+ # 生成白名单文件
19
+ # 我们从环境变量 ALLOWED_USERS 中读取邮箱列表(逗号分隔)
20
+ # 并将其写入 oauth2-proxy 期望的文件格式(每行一个邮箱)
21
+ mkdir -p /etc/oauth2-proxy
22
+ if [ -n "$ALLOWED_USERS" ]; then
23
+ echo "Generating allowed users list..."
24
+ echo "$ALLOWED_USERS" | tr ',' '\n' > /etc/oauth2-proxy/authenticated_emails.txt
25
+ else
26
+ echo "Warning: ALLOWED_USERS is not set! Anyone with a GitHub account can login."
27
+ # 创建一个空文件,或者允许所有(取决于配置,但为了安全建议留空或报错)
28
+ touch /etc/oauth2-proxy/authenticated_emails.txt
29
+ fi
30
 
31
  # 1. 启动 ttyd (本地监听)
32
  echo "Starting ttyd on 127.0.0.1:7681..."
33
  ttyd -p 7681 -i 127.0.0.1 -W bash &
34
 
35
  # 2. 启动 oauth2-proxy (本地监听)
36
+ # 我们直接在命令行传递 Client ID 和 Secret,确保它们被正确读取
37
  echo "Starting oauth2-proxy on 127.0.0.1:4180..."
38
+ oauth2-proxy \
39
+ --config=oauth2-proxy.cfg \
40
+ --client-id="$OAUTH2_PROXY_CLIENT_ID" \
41
+ --client-secret="$OAUTH2_PROXY_CLIENT_SECRET" \
42
+ --cookie-secret="$OAUTH2_PROXY_COOKIE_SECRET" &
43
 
44
  # 3. 启动 Nginx (对外监听 7860)
45
  echo "Starting Nginx on port 7860..."