Simplify systemd files

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2018-01-24 11:03:56 +00:00
parent ecdaeaf368
commit 0c3c2ca28b
3 changed files with 29 additions and 39 deletions

View File

@@ -9,8 +9,9 @@ _ask() {
_ask "Config filename (tun0)" NAME
NAME=${NAME:-tun0}
DIR="/etc/glorytun/$NAME"
if [ -f /etc/glorytun/"$NAME" ]; then
if [ -d "$DIR" ]; then
echo "This config already exit!"
exit 1
fi
@@ -31,16 +32,32 @@ if [ -z "$KEY" ]; then
fi
# install files
mkdir -p /etc/glorytun
mkdir -p "$DIR"
cat > /etc/glorytun/"$NAME" <<EOF
cat > "$DIR/env" <<EOF
DEV=gt${HOST:+c}-$NAME
HOST=$HOST
BIND_PORT=$BIND_PORT
PORT=$PORT
MTU_AUTO=yes
BIND_PORT=$BIND_PORT
OPTIONS=v4only mtu-auto
EOF
( umask 077; echo "$KEY" > /etc/glorytun/"$NAME".key )
( umask 077; echo "$KEY" > "$DIR/key" )
[ "$HOST" ] && cat > "$DIR/post.sh" <<'EOF'
#!/bin/sh
PREF=32765
TABLE=200
# keep the current route to HOST
src=$(ip route get "$HOST" | awk '/src/{getline;print $0}' RS=' ')
ip rule add from "$src" table main pref "$((PREF-1))" || true
# forward everything else to the tunnel
ip rule add from all table "$TABLE" pref "$PREF" || true
EOF
[ -f "$DIR/post.sh" ] && chmod u+x "$DIR/post.sh"
# start services
_ask "Start glorytun now ? (enter to skip)" START