Skip to content

Commit

Permalink
add service switchport
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyGALLAND committed Sep 21, 2024
1 parent 90cea50 commit fc1332a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 77 deletions.
76 changes: 76 additions & 0 deletions switchport/all/usr/sbin/switchport.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env ash

SCRIPT_NAME=$(basename "$0")
TARGET_DIR="/usr/syno/share/nginx"
TARGET_FILE="${TARGET_DIR}/*.mustache"
CURRENT_HTTP_PORT_FILE="${TARGET_DIR}/current_http_port"
CURRENT_HTTPS_PORT_FILE="${TARGET_DIR}/current_https_port"

usage() {
echo "${script_name} usage:"
echo " ${script_name} HTTP_PORT HTTPS_PORT"
echo " requires two parameters or none"
echo " with no parameter default HTTP_PORT 80 and HTTPS_PORT 443"
echo " The port number must be between 1 and 65535"
exit 0
}

is_number() {
[ -n "$1" ] && [ "$1" -eq "$1" ] 2>/dev/null
if [ $? -ne 0 ]; then
return 0
fi
return 1
}

outrange() {
if is_number $1; then
echo "Port number \"$1\" not accepted. The port must be a number"
usage
fi

if [ $1 -ge 1 ] && [ $1 -le 65535 ]; then
return
else
echo "Port number \"$1\" not accepted. The port number must be between 1 and 65535"
usage
fi
}

CURRENT_HTTP_PORT=80
CURRENT_HTTPS_PORT=443

if [ -f "${CURRENT_HTTP_PORT_FILE}" ]; then
CURRENT_HTTP_PORT=$(cat "${CURRENT_HTTP_PORT_FILE}")
fi

if [ -f "${CURRENT_HTTPS_PORT_FILE}" ]; then
CURRENT_HTTPS_PORT=$(cat "${CURRENT_HTTPS_PORT_FILE}")
fi

if [ "$#" != "0" ] && [ "$#" != "2" ]; then usage; fi

if [ "$#" -eq "0" ]; then
NEW_HTTP_PORT=${CURRENT_HTTP_PORT}
NEW_HTTPS_PORT=${CURRENT_HTTPS_PORT}
fi

if [ "$#" -eq "2" ]; then
NEW_HTTP_PORT=$1
NEW_HTTPS_PORT=$2
fi

outrange "${NEW_HTTP_PORT}"
outrange "${NEW_HTTPS_PORT}"

if [ "${CURRENT_HTTP_PORT}" != "${NEW_HTTP_PORT}" ]; then
#sed -i "s/^\([ \t]\+listen[ \t]\+[]:[]*\)${CURRENT_HTTP_PORT}\([^0-9]\)/\1${NEW_HTTP_PORT}\2/" "${TARGET_FILE}"
echo ${NEW_HTTP_PORT} >"${CURRENT_HTTP_PORT_FILE}"
echo "Switch port http ${CURRENT_HTTP_PORT} to ${NEW_HTTP_PORT}"
fi

if [ "${CURRENT_HTTPS_PORT}" != "${NEW_HTTPS_PORT}" ]; then
sed -i "s/^\([ \t]\+listen[ \t]\+[]:[]*\)${CURRENT_HTTPS_PORT}\([^0-9]\)/\1${NEW_HTTPS_PORT}\2/" "${TARGET_FILE}"
echo ${NEW_HTTPS_PORT} >"${CURRENT_HTTPS_PORT_FILE}"
echo "Switch port https ${CURRENT_HTTPS_PORT} to ${NEW_HTTPS_PORT}"
fi
94 changes: 17 additions & 77 deletions switchport/install.sh
Original file line number Diff line number Diff line change
@@ -1,83 +1,23 @@
#!/usr/bin/env ash

SCRIPT_NAME=$(basename "$0")
#TARGET_DIR="/tmpRoot/usr/syno/share/nginx"
TARGET_DIR="/tmp"
TARGET_FILE="${TARGET_DIR}/*.mustache"
CURRENT_HTTP_PORT_FILE="${TARGET_DIR}/current_http_port"
CURRENT_HTTPS_PORT_FILE="${TARGET_DIR}/current_https_port"

usage() {
echo "${script_name} usage:"
echo " ${script_name} HTTP_PORT HTTPS_PORT"
echo " requires two parameters or none"
echo " with no parameter default HTTP_PORT 80 and HTTPS_PORT 443"
echo " The port number must be between 1 and 65535"
exit 0
}

is_number() {
[ -n "$1" ] && [ "$1" -eq "$1" ] 2>/dev/null
if [ $? -ne 0 ]; then
return 0
fi
return 1
}

outrange() {
if is_number $1; then
echo "Port number \"$1\" not accepted. The port must be a number"
usage
fi

if [ $1 -ge 1 ] && [ $1 -le 65535 ]; then
return
else
echo "Port number \"$1\" not accepted. The port number must be between 1 and 65535"
usage
fi
}

if [ "${1}" = "late" ]; then

echo "Installing daemon for switchport"
cp -vf /usr/sbin/switchport.sh /tmpRoot/usr/sbin/switchport.sh
shift

CURRENT_HTTP_PORT=80
CURRENT_HTTPS_PORT=443

if [ -f "${CURRENT_HTTP_PORT_FILE}" ]; then
CURRENT_HTTP_PORT=$(cat "${CURRENT_HTTP_PORT_FILE}")
fi

if [ -f "${CURRENT_HTTPS_PORT_FILE}" ]; then
CURRENT_HTTPS_PORT=$(cat "${CURRENT_HTTPS_PORT_FILE}")
fi

if [ "$#" != "0" ] && [ "$#" != "2" ]; then usage; fi

if [ "$#" -eq "0" ]; then
NEW_HTTP_PORT=${CURRENT_HTTP_PORT}
NEW_HTTPS_PORT=${CURRENT_HTTPS_PORT}
fi

if [ "$#" -eq "2" ]; then
NEW_HTTP_PORT=$1
NEW_HTTPS_PORT=$2
fi

outrange "${NEW_HTTP_PORT}"
outrange "${NEW_HTTPS_PORT}"

if [ "${CURRENT_HTTP_PORT}" != "${NEW_HTTP_PORT}" ]; then
#sed -i "s/^\([ \t]\+listen[ \t]\+[]:[]*\)${CURRENT_HTTP_PORT}\([^0-9]\)/\1${NEW_HTTP_PORT}\2/" "${TARGET_FILE}"
echo ${NEW_HTTP_PORT} >"${CURRENT_HTTP_PORT_FILE}"
echo "Switch port http ${CURRENT_HTTP_PORT} to ${NEW_HTTP_PORT}"
fi

if [ "${CURRENT_HTTPS_PORT}" != "${NEW_HTTPS_PORT}" ]; then
sed -i "s/^\([ \t]\+listen[ \t]\+[]:[]*\)${CURRENT_HTTPS_PORT}\([^0-9]\)/\1${NEW_HTTPS_PORT}\2/" "${TARGET_FILE}"
echo ${NEW_HTTPS_PORT} >"${CURRENT_HTTPS_PORT_FILE}"
echo "Switch port https ${CURRENT_HTTPS_PORT} to ${NEW_HTTPS_PORT}"
fi

DEST="/tmpRoot/lib/systemd/system/switchport.service"
echo "[Unit]" >${DEST}
echo "Desallocates ports http(s)" >>${DEST}
echo "After=multi-user.target" >>${DEST}
echo >>${DEST}
echo "[Service]" >>${DEST}
echo "Type=oneshot" >>${DEST}
echo "RemainAfterExit=true" >>${DEST}
echo "ExecStart=/usr/sbin/switchport.sh $1 $2" >>${DEST}
echo >>${DEST}
echo "[Install]" >>${DEST}
echo "WantedBy=multi-user.target" >>${DEST}

mkdir -vp /tmpRoot/lib/systemd/system/multi-user.target.wants
ln -vsf /lib/systemd/system/switchport.service /tmpRoot/lib/systemd/system/multi-user.target.wants/switchport.service
fi

0 comments on commit fc1332a

Please sign in to comment.