Skip to content

Instantly share code, notes, and snippets.

@ngxson
Last active February 15, 2023 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngxson/f50e11ee0f2549410b6992becc9bfebe to your computer and use it in GitHub Desktop.
Save ngxson/f50e11ee0f2549410b6992becc9bfebe to your computer and use it in GitHub Desktop.
Portainer docker + JS injection
js_path "/etc/nginx/conf.d/";
js_import main from default.js;
server {
listen 9443 default_server ssl http2;
listen [::]:9443 default_server ssl http2;
ssl_certificate /certs/cert.pem;
ssl_certificate_key /certs/key.pem;
set $target "http://172.17.0.1:9000";
location / {
proxy_pass $target;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
location ~* \.(?:js)$ {
proxy_pass $target;
# tweak
proxy_set_header Accept-Encoding "";
js_body_filter main.inject_js;
js_header_filter main.content_length_filter;
}
location /api/websocket {
proxy_pass $target;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
}
function inject_js(r, data, flags) {
const search = "(self.webpackChunkportainer";
if (data && data.startsWith && data.startsWith(search)) {
r.sendBuffer(data.replace(search, `
document.head.insertAdjacentHTML("beforeend", "<style>div.sidebar > button {display: none;}</style>");
${search}`), flags);
} else {
r.sendBuffer(data, flags);
}
}
function content_length_filter(r) {
r.headersOut['Content-Length'] = '';
}
export default {inject_js, content_length_filter};
version: "2.4"
services:
portainer:
image: portainer/portainer-ce:latest
container_name: portainer-ce
hostname: portainer-ce
restart: always
ports:
- 9000:9000
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ${HOME}/data/portainer:/data
nginx:
image: nginx:latest
container_name: portainer-nginx
hostname: portainer-nginx
restart: always
stop_signal: SIGKILL
ports:
- 9443:9443
volumes:
- ./certs:/certs
- ./default.conf:/etc/nginx/conf.d/default.conf
- ./default.js:/etc/nginx/conf.d/default.js
command: /bin/sh -c "sed -i '1iload_module /usr/lib/nginx/modules/ngx_http_js_module.so;\\n' /etc/nginx/nginx.conf && nginx -g 'daemon off;'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment