Skip to content

Commit 6c5588a

Browse files
committed
fix: 安装Proxy增加白名单支持 (closed #2687)
1 parent 75eec0f commit 6c5588a

File tree

3 files changed

+75
-24
lines changed

3 files changed

+75
-24
lines changed

apps/backend/agent/manager.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from apps.backend.components.collections.agent_new import components
1818
from apps.node_man.constants import NodeType
19+
from apps.node_man.models import GlobalSettings
1920
from pipeline.builder import ServiceActivity, Var
2021

2122

@@ -181,7 +182,79 @@ def start_nginx(cls):
181182
script = fh.read()
182183
# 脚本模板中存在 {print $2} 等和 format 关键字冲突的片段
183184
# 此处的字符串渲染采用 % 的方式
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+
}
184256
script_content = script % {
257+
"nginx_server": server_tpl,
185258
"nginx_path": settings.DOWNLOAD_PATH,
186259
"bk_nodeman_nginx_download_port": settings.BK_NODEMAN_NGINX_DOWNLOAD_PORT,
187260
"bk_nodeman_nginx_proxy_pass_port": settings.BK_NODEMAN_NGINX_PROXY_PASS_PORT,

apps/node_man/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ class KeyEnum(Enum):
196196
# 无需排队执行的订阅白名单
197197
UNQUEUED_SUBSCRIPTION_WHITELIST = "UNQUEUED_SUBSCRIPTION_WHITELIST"
198198
AUTO_TRIGGER_SUBSCRIPTION_BIZ_KEY = "AUTO_TRIGGER_SUBSCRIPTION_BIZ_KEY"
199+
NGINX_HTTP_WHITELIST = "NGINX_HTTP_WHITELIST"
199200

200201
key = models.CharField(_("键"), max_length=255, db_index=True, primary_key=True)
201202
v_json = JSONField(_("值"))

script_tools/start_nginx.sh.tpl

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,8 @@ http {
6666
include mime.types;
6767
default_type application/octet-stream;
6868
sendfile on;
69-
server {
70-
listen %(bk_nodeman_nginx_download_port)s;
71-
listen [::]:%(bk_nodeman_nginx_download_port)s;
72-
server_name localhost;
73-
root %(nginx_path)s;
7469
75-
location / {
76-
index index.html;
77-
}
78-
error_page 500 502 503 504 /50x.html;
79-
location = /50x.html {
80-
root html;
81-
}
82-
}
83-
server {
84-
listen %(bk_nodeman_nginx_proxy_pass_port)s;
85-
listen [::]:%(bk_nodeman_nginx_proxy_pass_port)s;
86-
server_name localhost;
87-
resolver ${nginx_dns_list[@]};
88-
proxy_connect;
89-
proxy_connect_allow 443 563;
90-
location / {
91-
proxy_pass http://\$http_host\$request_uri;
92-
}
93-
}
70+
%(nginx_server)s
9471
}" > /opt/nginx-portable/conf/nginx.conf;
9572
/opt/nginx-portable/nginx-portable start;
9673
sleep 5

0 commit comments

Comments
 (0)