|
16 | 16 |
|
17 | 17 | from apps.backend.components.collections.agent_new import components |
18 | 18 | from apps.node_man.constants import NodeType |
| 19 | +from apps.node_man.models import GlobalSettings |
19 | 20 | from pipeline.builder import ServiceActivity, Var |
20 | 21 |
|
21 | 22 |
|
@@ -181,7 +182,79 @@ def start_nginx(cls): |
181 | 182 | script = fh.read() |
182 | 183 | # 脚本模板中存在 {print $2} 等和 format 关键字冲突的片段 |
183 | 184 | # 此处的字符串渲染采用 % 的方式 |
| 185 | + nginx_http_whitelist = GlobalSettings.get_config( |
| 186 | + key=GlobalSettings.KeyEnum.NGINX_HTTP_WHITELIST.value, default=[] |
| 187 | + ) |
| 188 | + if not nginx_http_whitelist: |
| 189 | + server_tpl = """ |
| 190 | + server { |
| 191 | + listen %(bk_nodeman_nginx_download_port)s; |
| 192 | + listen [::]:%(bk_nodeman_nginx_download_port)s; |
| 193 | + server_name localhost; |
| 194 | + root %(nginx_path)s; |
| 195 | +
|
| 196 | + location / { |
| 197 | + index index.html; |
| 198 | + } |
| 199 | + error_page 500 502 503 504 /50x.html; |
| 200 | + location = /50x.html { |
| 201 | + root html; |
| 202 | + } |
| 203 | + } |
| 204 | + server { |
| 205 | + listen %(bk_nodeman_nginx_proxy_pass_port)s; |
| 206 | + listen [::]:%(bk_nodeman_nginx_proxy_pass_port)s; |
| 207 | + server_name localhost; |
| 208 | + resolver ${nginx_dns_list[@]}; |
| 209 | + proxy_connect; |
| 210 | + proxy_connect_allow 443 563; |
| 211 | + location / { |
| 212 | + proxy_pass http://\$http_host\$request_uri; |
| 213 | + } |
| 214 | + } |
| 215 | + """ |
| 216 | + else: |
| 217 | + server_tpl = """ |
| 218 | + map \$host \$http_whitelist { |
| 219 | + default 0; |
| 220 | + %(nginx_http_whitelist)s |
| 221 | + } |
| 222 | + server { |
| 223 | + listen %(bk_nodeman_nginx_download_port)s; |
| 224 | + listen [::]:%(bk_nodeman_nginx_download_port)s; |
| 225 | + server_name localhost; |
| 226 | + root %(nginx_path)s; |
| 227 | +
|
| 228 | + location / { |
| 229 | + index index.html; |
| 230 | + } |
| 231 | + error_page 500 502 503 504 /50x.html; |
| 232 | + location = /50x.html { |
| 233 | + root html; |
| 234 | + } |
| 235 | + } |
| 236 | + server { |
| 237 | + listen %(bk_nodeman_nginx_proxy_pass_port)s; |
| 238 | + listen [::]:%(bk_nodeman_nginx_proxy_pass_port)s; |
| 239 | + server_name localhost; |
| 240 | + resolver ${nginx_dns_list[@]}; |
| 241 | + location / { |
| 242 | + if (\$http_whitelist = 0) { |
| 243 | + return 403; |
| 244 | + } |
| 245 | + proxy_pass http://\$http_host\$request_uri; |
| 246 | + } |
| 247 | + } |
| 248 | + """ |
| 249 | + |
| 250 | + server_tpl = server_tpl % { |
| 251 | + "nginx_path": settings.DOWNLOAD_PATH, |
| 252 | + "bk_nodeman_nginx_download_port": settings.BK_NODEMAN_NGINX_DOWNLOAD_PORT, |
| 253 | + "bk_nodeman_nginx_proxy_pass_port": settings.BK_NODEMAN_NGINX_PROXY_PASS_PORT, |
| 254 | + "nginx_http_whitelist": "\n ".join(f"{host} 1;" for host in nginx_http_whitelist), |
| 255 | + } |
184 | 256 | script_content = script % { |
| 257 | + "nginx_server": server_tpl, |
185 | 258 | "nginx_path": settings.DOWNLOAD_PATH, |
186 | 259 | "bk_nodeman_nginx_download_port": settings.BK_NODEMAN_NGINX_DOWNLOAD_PORT, |
187 | 260 | "bk_nodeman_nginx_proxy_pass_port": settings.BK_NODEMAN_NGINX_PROXY_PASS_PORT, |
|
0 commit comments