Created
September 17, 2024 22:11
-
-
Save samj/886ac15b655e40b504e97a07eace1d3c to your computer and use it in GitHub Desktop.
Tailscale on Qnap with Exit Node and Advertise Routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
CONF=/etc/config/qpkg.conf | |
SOCKET=/tmp/tailscale/tailscaled.sock | |
QPKG_NAME="Tailscale" | |
QPKG_ROOT=`/sbin/getcfg ${QPKG_NAME} Install_Path -f ${CONF}` | |
QPKG_PORT=`/sbin/getcfg ${QPKG_NAME} Service_Port -f ${CONF}` | |
export QNAP_QPKG=${QPKG_NAME} | |
set -e | |
case "$1" in | |
start) | |
ENABLED=$(/sbin/getcfg ${QPKG_NAME} Enable -u -d FALSE -f ${CONF}) | |
if [ "${ENABLED}" != "TRUE" ]; then | |
echo "${QPKG_NAME} is disabled." | |
exit 1 | |
fi | |
mkdir -p /home/httpd/cgi-bin/qpkg | |
ln -sf ${QPKG_ROOT}/ui /home/httpd/cgi-bin/qpkg/${QPKG_NAME} | |
mkdir -p -m 0755 /tmp/tailscale | |
if [ -e /tmp/tailscale/tailscaled.pid ]; then | |
PID=$(cat /tmp/tailscale/tailscaled.pid) | |
if [ -d /proc/${PID}/ ]; then | |
echo "${QPKG_NAME} is already running." | |
exit 0 | |
fi | |
fi | |
${QPKG_ROOT}/tailscaled --port ${QPKG_PORT} --statedir=${QPKG_ROOT}/state --socket=${SOCKET} 2> /dev/null & | |
echo $! > /tmp/tailscale/tailscaled.pid | |
GW_IFACE=$(ip route | grep default | awk '{print $5}') | |
IP_CIDR=$(ip route show dev $GW_IFACE | awk '/proto kernel/ {print $1}') | |
# Wait for tailscaled to be ready, with a timeout of 10 seconds | |
for i in {1..10}; do | |
if [ -S /tmp/tailscale/tailscaled.sock ]; then | |
# Bring Tailscale up with the correct routes | |
${QPKG_ROOT}/tailscale up --reset --advertise-exit-node --advertise-routes=${IP_CIDR} | |
break | |
fi | |
sleep 1 | |
done | |
# If we reach here and the socket still doesn't exist, exit with an error | |
if [ ! -S /tmp/tailscale/tailscaled.sock ]; then | |
echo "tailscaled did not start within the expected time. Exiting." | |
exit 1 | |
fi | |
;; | |
stop) | |
if [ -e /tmp/tailscale/tailscaled.pid ]; then | |
PID=$(cat /tmp/tailscale/tailscaled.pid) | |
kill -9 ${PID} || true | |
rm -f /tmp/tailscale/tailscaled.pid | |
fi | |
;; | |
restart) | |
$0 stop | |
$0 start | |
;; | |
remove) | |
;; | |
*) | |
echo "Usage: $0 {start|stop|restart|remove}" | |
exit 1 | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment