Skip to content

Commit 67fc36c

Browse files
authored
Merge pull request #778 from linuxserver/swag-auto-proxy-custom
Add support for new `swag_server_custom_directive` label
2 parents 168a30b + 68fb0b0 commit 67fc36c

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This mod gives SWAG the ability to auto-detect running containers via labels and
1616
- `swag_url=containername.domain.com` - *optional* - overrides *server_name* (defaults to `containername.*`)
1717
- `swag_auth=authelia` - *optional* - enables auth methods (options are `authelia`, `authentik`, `ldap` and `http` for basic http auth)
1818
- `swag_auth_bypass=/api,/othersubfolder` - *optional* - bypasses auth for selected subfolders. Comma separated, no spaces.
19+
- `swag_server_custom_directive=custom_directive;` - *optional* - injects the label value as is into the server block of the generated conf. Must be a valid nginx directive, ending with a semi colon.
1920

2021

2122
In SWAG docker arguments, set an environment variable `DOCKER_MODS=linuxserver/mods:universal-docker|linuxserver/mods:swag-auto-proxy` and either add a volume mapping for `/var/run/docker.sock:/var/run/docker.sock:ro`, or set an environment var `DOCKER_HOST=remoteaddress`.

root/app/auto-proxy.sh

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ else
1313
AUTO_GEN="${CONTAINER} ${AUTO_GEN}"
1414
else
1515
INSPECTION=$(docker inspect ${CONTAINER})
16-
for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass; do
16+
for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass swag_server_custom_directive; do
1717
VAR_VALUE=$(echo ${INSPECTION} | jq -r ".[0].Config.Labels[\"${VAR}\"]")
1818
if [ "${VAR_VALUE}" == "null" ]; then
1919
VAR_VALUE=""
@@ -40,7 +40,7 @@ fi
4040
for CONTAINER in ${AUTO_GEN}; do
4141
INSPECTION=$(docker inspect ${CONTAINER})
4242
rm -rf "/auto-proxy/${CONTAINER}.conf"
43-
for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass; do
43+
for VAR in swag_address swag_port swag_proto swag_url swag_auth swag_auth_bypass swag_server_custom_directive; do
4444
VAR_VALUE=$(echo ${INSPECTION} | jq -r ".[0].Config.Labels[\"${VAR}\"]")
4545
if [ "${VAR_VALUE}" == "null" ]; then
4646
VAR_VALUE=""
@@ -67,9 +67,15 @@ for CONTAINER in ${AUTO_GEN}; do
6767
echo "**** Overriding proto as ${swag_proto} for ${CONTAINER} ****"
6868
fi
6969
if [ -n "${swag_url}" ]; then
70-
sed -i "s|server_name .*|server_name ${swag_url};|" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
70+
SED_swag_url=$(sed -e 's/[&\\|]/\\&/g; s|$|\\|; $s|\\$||' <<<"${swag_url}")
71+
sed -i "s|server_name .*|server_name ${SED_swag_url};|" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
7172
echo "**** Overriding url as ${swag_url} for ${CONTAINER} ****"
7273
fi
74+
if [ -n "${swag_server_custom_directive}" ]; then
75+
SED_swag_server_custom_directive=$(sed -e 's/[&\\|]/\\&/g; s|$|\\|; $s|\\$||' <<<"${swag_server_custom_directive}")
76+
sed -i -e '/include.*ssl.conf;/a\' -e " ${SED_swag_server_custom_directive}" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
77+
echo "**** Adding custom directive from the swag_server_custom_directive label for ${CONTAINER} ****"
78+
fi
7379
if [ "${swag_auth}" == "authelia" ]; then
7480
sed -i "s|#include /config/nginx/authelia|include /config/nginx/authelia|g" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
7581
echo "**** Enabling Authelia for ${CONTAINER} ****"
@@ -133,8 +139,14 @@ DUDE
133139
if [ -z "${swag_url}" ]; then
134140
swag_url="${CONTAINER}.*"
135141
fi
136-
sed -i "s|server_name .*|server_name ${swag_url};|" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
142+
SED_swag_url=$(sed -e 's/[&\\|]/\\&/g; s|$|\\|; $s|\\$||' <<<"${swag_url}")
143+
sed -i "s|server_name .*|server_name ${SED_swag_url};|" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
137144
echo "**** Setting url ${swag_url} for ${CONTAINER} ****"
145+
if [ -n "${swag_server_custom_directive}" ]; then
146+
SED_swag_server_custom_directive=$(sed -e 's/[&\\|]/\\&/g; s|$|\\|; $s|\\$||' <<<"${swag_server_custom_directive}")
147+
sed -i -e '/include.*ssl.conf;/a\' -e " ${SED_swag_server_custom_directive}" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
148+
echo "**** Adding custom directive from the swag_server_custom_directive label for ${CONTAINER} ****"
149+
fi
138150
if [ "${swag_auth}" == "authelia" ]; then
139151
sed -i "s|#include /config/nginx/authelia|include /config/nginx/authelia|g" "/etc/nginx/http.d/auto-proxy-${CONTAINER}.subdomain.conf"
140152
echo "**** Enabling Authelia for ${CONTAINER} ****"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/with-contenv bash
22

3-
sed -i '/\/app\/auto-proxy.sh/d' /config/crontabs/root
3+
sed -i '/\/app\/auto-proxy.sh/d' /etc/crontabs/root
44
rm -rf /etc/nginx/http.d/auto-proxy*.conf /config/nginx/proxy-confs/auto-proxy*.conf

root/etc/s6-overlay/s6-rc.d/init-mod-swag-auto-proxy-setup/run

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ cp /defaults/auto-proxy-readme /etc/nginx/http.d/auto-proxy-readme
1111
rm -rf /auto-proxy
1212
mkdir /auto-proxy
1313

14-
if ! grep -q "/app/auto-proxy.sh" /config/crontabs/root; then
15-
echo "* * * * * /app/auto-proxy.sh" >> /config/crontabs/root
16-
cp /config/crontabs/root /etc/crontabs/root
14+
if ! grep -q "/app/auto-proxy.sh" /etc/crontabs/root; then
15+
echo "* * * * * /app/auto-proxy.sh" >> /etc/crontabs/root
1716
fi
1817

1918
/app/auto-proxy.sh

0 commit comments

Comments
 (0)