Skip to content

Instantly share code, notes, and snippets.

@nosada
Last active September 26, 2018 12:04
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 nosada/580cd40747f10059166799dd9d82aaa9 to your computer and use it in GitHub Desktop.
Save nosada/580cd40747f10059166799dd9d82aaa9 to your computer and use it in GitHub Desktop.
Expose port for privoxy running on nspawn container
#!/bin/sh
CONTAINER_NAME="torspawn"
CONFIG_DIR="/etc/systemd/nspawn"
CONFIG_PREFIX="nspawn"
CONFIG_LOCATION="$CONFIG_DIR/$CONTAINER_NAME.$CONFIG_PREFIX"
INTERNAL="8118"
EXPOSING="58118"
# Wrong port already exposed: remove port configuration
grep $EXPOSING $CONFIG_LOCATION 1> /dev/null 2> /dev/null
EXPOSED_CORRECTLY=$?
if [ $EXPOSED_CORRECTLY -ne 0 ]; then
sed -i $CONFIG_LOCATION -e '/[Pp]ort/d'
fi
egrep '[Pp]ort' $CONFIG_LOCATION 1> /dev/null 2> /dev/null
PORT_CONFIGURED=$?
if [ $PORT_CONFIGURED -ne 0 ]; then
sed -i $CONFIG_LOCATION \
-e 's/\(\[Network\]\)/\1\nPort='$EXPOSING':'$INTERNAL'/g'
fi
if [ $EXPOSED_CORRECTLY -ne 0 ] || [ $PORT_CONFIGURED -ne 0 ]; then
machinectl stop $CONTAINER_NAME
sleep 5
machinectl start $CONTAINER_NAME
fi
NAME=expose-privoxy-port-on-nspawn
all: install
install: ${NAME} ${NAME}.service ${NAME}.timer
install -D -m 755 ${NAME} /usr/local/bin/
install -D -m 644 ${NAME}.service /etc/systemd/system/
install -D -m 644 ${NAME}.timer /etc/systemd/system/
systemctl daemon-reload
systemctl enable ${NAME}.timer
uninstall:
systemctl disable ${NAME}.timer
rm -f /usr/local/bin/${NAME}
rm -f /etc/systemd/system/${NAME}.service
rm -f /etc/systemd/system/${NAME}.timer
systemctl daemon-reload
[Unit]
Description=Expose port for privoxy running inside nspawn container
[Service]
Type=oneshot
ExecStart=/usr/local/bin/expose-privoxy-port-on-nspawn
[Unit]
Description=Expose port for privoxy running inside nspawn container
[Timer]
OnBootSec=1min
OnUnitActiveSec=5min
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment