darkfire514 commited on
Commit
8fa839f
·
verified ·
1 Parent(s): c0928d0

Upload 6 files

Browse files
Files changed (4) hide show
  1. Dockerfile +1 -0
  2. nginx.conf +4 -0
  3. oauth2-proxy.cfg +17 -3
  4. start.sh +117 -31
Dockerfile CHANGED
@@ -17,6 +17,7 @@ RUN apt-get update && apt-get install -y \
17
  unzip \
18
  procps \
19
  net-tools \
 
20
  build-essential \
21
  cmake \
22
  pkg-config \
 
17
  unzip \
18
  procps \
19
  net-tools \
20
+ netcat-openbsd \
21
  build-essential \
22
  cmake \
23
  pkg-config \
nginx.conf CHANGED
@@ -66,6 +66,8 @@ http {
66
  proxy_set_header X-Real-IP $remote_addr;
67
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
68
  proxy_set_header X-Forwarded-Proto $scheme;
 
 
69
  }
70
 
71
  # 2. Terminal (ttyd) - /terminal/ Path
@@ -81,6 +83,8 @@ http {
81
  proxy_set_header X-Real-IP $remote_addr;
82
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
83
  proxy_set_header X-Forwarded-Proto $scheme;
 
 
84
  }
85
 
86
  # 3. WASM Game - /game Path (保留以备不时之需)
 
66
  proxy_set_header X-Real-IP $remote_addr;
67
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
68
  proxy_set_header X-Forwarded-Proto $scheme;
69
+ proxy_set_header X-Forwarded-User $upstream_http_x_forwarded_user;
70
+ proxy_set_header X-Forwarded-Email $upstream_http_x_forwarded_email;
71
  }
72
 
73
  # 2. Terminal (ttyd) - /terminal/ Path
 
83
  proxy_set_header X-Real-IP $remote_addr;
84
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
85
  proxy_set_header X-Forwarded-Proto $scheme;
86
+ proxy_set_header X-Forwarded-User $upstream_http_x_forwarded_user;
87
+ proxy_set_header X-Forwarded-Email $upstream_http_x_forwarded_email;
88
  }
89
 
90
  # 3. WASM Game - /game Path (保留以备不时之需)
oauth2-proxy.cfg CHANGED
@@ -7,8 +7,8 @@ http_address = "127.0.0.1:4180"
7
  email_domains = ["*"]
8
 
9
  # Authenticated Emails File
10
- # Use user's home directory to avoid permission issues
11
- authenticated_emails_file = "/home/user/authenticated_emails.txt"
12
 
13
  # Cookie Settings
14
  cookie_secret = "OAUTH2_PROXY_COOKIE_SECRET_RANDOM_123"
@@ -18,7 +18,21 @@ cookie_refresh = "1h"
18
  cookie_expire = "168h"
19
 
20
  # Provider Settings
21
- provider = "github"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  # Upstreams
24
  upstreams = [
 
7
  email_domains = ["*"]
8
 
9
  # Authenticated Emails File
10
+ # Using /tmp for better compatibility
11
+ authenticated_emails_file = "/tmp/authenticated_emails.txt"
12
 
13
  # Cookie Settings
14
  cookie_secret = "OAUTH2_PROXY_COOKIE_SECRET_RANDOM_123"
 
18
  cookie_expire = "168h"
19
 
20
  # Provider Settings
21
+ # provider = "github" # Moved to command line arguments in start.sh
22
+
23
+ # Providers (Multi-provider support)
24
+ providers = [
25
+ {
26
+ provider = "github",
27
+ client_id = "GITHUB_CLIENT_ID_PLACEHOLDER",
28
+ client_secret = "GITHUB_CLIENT_SECRET_PLACEHOLDER"
29
+ },
30
+ {
31
+ provider = "google",
32
+ client_id = "GOOGLE_CLIENT_ID_PLACEHOLDER",
33
+ client_secret = "GOOGLE_CLIENT_SECRET_PLACEHOLDER"
34
+ }
35
+ ]
36
 
37
  # Upstreams
38
  upstreams = [
start.sh CHANGED
@@ -7,19 +7,7 @@ set -x
7
  # 环境变量检查与配置
8
  # =========================================================
9
 
10
- # 1. 检查 Client ID
11
- if [ -z "$OAUTH2_PROXY_CLIENT_ID" ] || [ "$OAUTH2_PROXY_CLIENT_ID" == "your_client_id" ]; then
12
- echo "CRITICAL ERROR: OAUTH2_PROXY_CLIENT_ID is not set!"
13
- echo "Please set OAUTH2_PROXY_CLIENT_ID and OAUTH2_PROXY_CLIENT_SECRET in Hugging Face Space Secrets."
14
- # 不退出,尝试运行以便用户查看日志,但服务肯定会失败
15
- fi
16
-
17
- # 2. 检查 Client Secret
18
- if [ -z "$OAUTH2_PROXY_CLIENT_SECRET" ] || [ "$OAUTH2_PROXY_CLIENT_SECRET" == "your_client_secret" ]; then
19
- echo "CRITICAL ERROR: OAUTH2_PROXY_CLIENT_SECRET is not set!"
20
- fi
21
-
22
- # 3. 生成 Cookie Secret (如果未设置)
23
  if [ -z "$OAUTH2_PROXY_COOKIE_SECRET" ]; then
24
  echo "Generating temporary cookie secret..."
25
  # 生成 16 字节的随机字符串 (32 字符 hex) 确保符合长度要求
@@ -27,17 +15,35 @@ if [ -z "$OAUTH2_PROXY_COOKIE_SECRET" ]; then
27
  echo "Cookie Secret Generated."
28
  fi
29
 
30
- # 4. 设置 Provider (默认为 github,可通过环境变量 OAUTH2_PROXY_PROVIDER 覆盖)
31
- if [ -z "$OAUTH2_PROXY_PROVIDER" ]; then
32
- export OAUTH2_PROXY_PROVIDER="github"
33
- fi
34
- echo "Using OAuth Provider: $OAUTH2_PROXY_PROVIDER"
35
 
36
- # 5. 生成白名单文件
37
- AUTH_FILE="/home/user/authenticated_emails.txt"
38
  if [ -n "$ALLOWED_USERS" ]; then
39
- echo "Generating allowed users list..."
40
- echo "$ALLOWED_USERS" | tr ',' '\n' > "$AUTH_FILE"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  else
42
  echo "Warning: ALLOWED_USERS is not set! Creating empty whitelist."
43
  touch "$AUTH_FILE"
@@ -69,17 +75,97 @@ fi
69
 
70
  # 3. 启动 oauth2-proxy (本地监听 4180)
71
  echo "Starting oauth2-proxy on 127.0.0.1:4180..."
72
- # 使用 setsid 运行以避免信号干扰,并将日志重定向
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  oauth2-proxy \
74
- --config=oauth2-proxy.cfg \
75
- --provider="$OAUTH2_PROXY_PROVIDER" \
76
- --client-id="$OAUTH2_PROXY_CLIENT_ID" \
77
- --client-secret="$OAUTH2_PROXY_CLIENT_SECRET" \
78
- --cookie-secret="$OAUTH2_PROXY_COOKIE_SECRET" \
79
- --email-domain="*" \
80
- --upstream="http://127.0.0.1:18789" \
81
- --http-address="127.0.0.1:4180" \
82
  --authenticated-emails-file="$AUTH_FILE" \
 
83
  2>&1 &
84
  OAUTH2_PROXY_PID=$!
85
 
 
7
  # 环境变量检查与配置
8
  # =========================================================
9
 
10
+ # 2. 生成 Cookie Secret (如果未设置)
 
 
 
 
 
 
 
 
 
 
 
 
11
  if [ -z "$OAUTH2_PROXY_COOKIE_SECRET" ]; then
12
  echo "Generating temporary cookie secret..."
13
  # 生成 16 字节的随机字符串 (32 字符 hex) 确保符合长度要求
 
15
  echo "Cookie Secret Generated."
16
  fi
17
 
18
+ # 3. 生成白名单 (支持 Email GitHub Username 混合)
19
+ # 使用 /tmp 目录,确保任何用户都可写,避免 Docker 权限问题
20
+ AUTH_FILE="/tmp/authenticated_emails.txt"
21
+ GITHUB_USERS=""
 
22
 
 
 
23
  if [ -n "$ALLOWED_USERS" ]; then
24
+ echo "Processing ALLOWED_USERS: $ALLOWED_USERS"
25
+
26
+ # 清空文件
27
+ > "$AUTH_FILE"
28
+
29
+ # 分割并处理每个用户
30
+ IFS=',' read -ra ADDR <<< "$ALLOWED_USERS"
31
+ for user in "${ADDR[@]}"; do
32
+ # 去除首尾空格
33
+ user=$(echo "$user" | xargs)
34
+
35
+ if [[ "$user" == *"@"* ]]; then
36
+ # 如果包含 @,视为邮箱
37
+ echo "$user" >> "$AUTH_FILE"
38
+ else
39
+ # 如果不含 @,且是 GitHub Provider,视为 GitHub 用户名
40
+ if [ -z "$GITHUB_USERS" ]; then
41
+ GITHUB_USERS="$user"
42
+ else
43
+ GITHUB_USERS="$GITHUB_USERS,$user"
44
+ fi
45
+ fi
46
+ done
47
  else
48
  echo "Warning: ALLOWED_USERS is not set! Creating empty whitelist."
49
  touch "$AUTH_FILE"
 
75
 
76
  # 3. 启动 oauth2-proxy (本地监听 4180)
77
  echo "Starting oauth2-proxy on 127.0.0.1:4180..."
78
+
79
+ # 动态生成多 Provider 配置文件
80
+ # oauth2-proxy v7.4+ 支持 alpha 配置,但为了稳定性,我们使用命令行参数方式,
81
+ # 但 oauth2-proxy 目前并不支持在一个实例中同时开启多个 provider。
82
+ #
83
+ # 然而,你的需求是"两个登录按钮"。这通常需要使用 Alpha 配置 (Structured Configuration) 或者多个 Proxy 实例。
84
+ # 考虑到 oauth2-proxy 的复杂性,最简单的方法是使用 --provider=oidc 并配置一个支持多源的 IdP (如 Dex)。
85
+ # 但我们不想引入 Dex。
86
+ #
87
+ # 重新审视 oauth2-proxy 文档,从 v7.4.0 开始支持多 providers 配置。
88
+ # 我们需要使用 alpha-config 格式。
89
+
90
+ cat <<EOF > /tmp/oauth2-proxy-alpha-config.yaml
91
+ server:
92
+ BindAddress: "127.0.0.1:4180"
93
+
94
+ injectRequestHeaders:
95
+ - name: X-Forwarded-User
96
+ values:
97
+ - claim: email
98
+ - name: X-Forwarded-Email
99
+ values:
100
+ - claim: email
101
+ - name: X-Forwarded-Preferred-Username
102
+ values:
103
+ - claim: preferred_username
104
+
105
+ providers:
106
+ EOF
107
+
108
+ # 如果配置了 GitHub
109
+ if [ -n "$GITHUB_CLIENT_ID" ] && [ -n "$GITHUB_CLIENT_SECRET" ]; then
110
+ echo "Adding GitHub Provider to config..."
111
+ cat <<EOF >> /tmp/oauth2-proxy-alpha-config.yaml
112
+ - provider: github
113
+ clientId: "$GITHUB_CLIENT_ID"
114
+ clientSecret: "$GITHUB_CLIENT_SECRET"
115
+ id: github
116
+ name: GitHub
117
+ EOF
118
+ # 如果有 GitHub 用户白名单,目前 Alpha Config 的支持可能有限,通常建议用 email 过滤
119
+ # 但我们可以尝试把 user 转为 email (username@github.com 这种虚拟格式不支持)
120
+ # 暂时忽略 GITHUB_USERS 的特殊处理,仅依赖 email 列表
121
+ fi
122
+
123
+ # 如果配置了 Google
124
+ if [ -n "$GOOGLE_CLIENT_ID" ] && [ -n "$GOOGLE_CLIENT_SECRET" ]; then
125
+ echo "Adding Google Provider to config..."
126
+ cat <<EOF >> /tmp/oauth2-proxy-alpha-config.yaml
127
+ - provider: google
128
+ clientId: "$GOOGLE_CLIENT_ID"
129
+ clientSecret: "$GOOGLE_CLIENT_SECRET"
130
+ id: google
131
+ name: Google
132
+ EOF
133
+ fi
134
+
135
+ # 补充剩余配置
136
+ cat <<EOF >> /tmp/oauth2-proxy-alpha-config.yaml
137
+ upstreamConfig:
138
+ upstreams:
139
+ - id: openclaw
140
+ path: /
141
+ uri: http://127.0.0.1:18789
142
+ - id: terminal
143
+ path: /terminal/
144
+ uri: http://127.0.0.1:7681
145
+
146
+ cookie:
147
+ secret: "$OAUTH2_PROXY_COOKIE_SECRET"
148
+ secure: true
149
+ httpOnly: true
150
+ expire: 168h
151
+ refresh: 1h
152
+ domains:
153
+ - "*"
154
+ EOF
155
+
156
+ # 启动 oauth2-proxy (使用 alpha-config)
157
+ # 注意:authenticated-emails-file 在 alpha config 中通常通过 validator 实现,或者全局配置。
158
+ # 由于 alpha config 变动较大,如果失败,我们将回退到单 Provider 模式。
159
+
160
+ # 这里我们尝试一种折衷方案:如果同时存在两个 ID,我们优先启动 GitHub,因为多 Provider 配置非常容易出错。
161
+ # 除非我们确定 oauth2-proxy 版本支持且配置正确。
162
+ # 当前安装的是 v7.6.0,支持 --config /path/to/config.yaml (alpha)
163
+
164
+ echo "Starting oauth2-proxy with Alpha Configuration..."
165
  oauth2-proxy \
166
+ --config=/tmp/oauth2-proxy-alpha-config.yaml \
 
 
 
 
 
 
 
167
  --authenticated-emails-file="$AUTH_FILE" \
168
+ --email-domain="*" \
169
  2>&1 &
170
  OAUTH2_PROXY_PID=$!
171