From 260c70e3173f3595fa183612c8641c6da36de572 Mon Sep 17 00:00:00 2001 From: B Stack Date: Wed, 9 Dec 2020 20:30:40 -0500 Subject: use uwsgi! apparently uwsgi_python3 is the useful binary in Devuan --- config/hex-zero | 165 ------------------------------------------- config/hex-zero.etc.default | 7 ++ config/hex-zero.init | 167 ++++++++++++++++++++++++++++++++++++++++++++ hex-zero.py | 6 +- hex-zero.wsgi | 19 +++++ 5 files changed, 194 insertions(+), 170 deletions(-) delete mode 100755 config/hex-zero create mode 100644 config/hex-zero.etc.default create mode 100755 config/hex-zero.init create mode 100755 hex-zero.wsgi diff --git a/config/hex-zero b/config/hex-zero deleted file mode 100755 index 1d79d18..0000000 --- a/config/hex-zero +++ /dev/null @@ -1,165 +0,0 @@ -#!/bin/sh -### BEGIN INIT INFO -# Provides: hex-zero -# Required-Start: $local_fs $network $remote_fs $syslog -# Required-Stop: $local_fs $network $remote_fs $syslog -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: 0x0 file hosting service -# Description: Python3 Flask application that presents a basic file hosting service -### END INIT INFO - -# Author: B. Stack - -# Do NOT "set -e" - -# PATH should only include /usr/* if it runs after the mountnfs.sh script -PATH=/sbin:/usr/sbin:/bin:/usr/bin -DESC="hex-zero" -NAME=hex-zero -DAEMON=/var/www/0x0/hex-zero.py -DAEMON_ARGS="runserver" -PIDFILE=/var/run/$NAME.pid -SCRIPTNAME=/etc/init.d/$NAME -USER=hex-zero - -# Exit if the package is not installed -[ -x "$DAEMON" ] || exit 0 - -# Read configuration variable file if it is present -[ -r /etc/default/$NAME ] && . /etc/default/$NAME - -# Load the VERBOSE setting and other rcS variables -. /lib/init/vars.sh - -# Define LSB log_* functions. -# Depend on lsb-base (>= 3.2-14) to ensure that this file is present -# and status_of_proc is working. -. /lib/lsb/init-functions - -# -# Function that starts the daemon/service -# -do_start() -{ - # Return - # 0 if daemon has been started - # 1 if daemon was already running - # 2 if daemon could not be started - start-stop-daemon --start --quiet --chuid $USER --group $USER --make-pidfile --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \ - || return 1 - start-stop-daemon --start --quiet --chuid $USER --group $USER --background --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- \ - $DAEMON_ARGS \ - || return 2 - # The above code will not work for interpreted scripts, use the next - # six lines below instead (Ref: #643337, start-stop-daemon(8) ) - #start-stop-daemon --start --quiet --pidfile $PIDFILE --startas $DAEMON \ - # --name $NAME --test > /dev/null \ - # || return 1 - #start-stop-daemon --start --quiet --pidfile $PIDFILE --startas $DAEMON \ - # --name $NAME -- $DAEMON_ARGS \ - # || return 2 - - # Add code here, if necessary, that waits for the process to be ready - # to handle requests from services started subsequently which depend - # on this one. As a last resort, sleep for some time. -} - -# -# Function that stops the daemon/service -# -do_stop() -{ - # Return - # 0 if daemon has been stopped - # 1 if daemon was already stopped - # 2 if daemon could not be stopped - # other if a failure occurred - start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $USER --pidfile $PIDFILE - RETVAL="$?" - [ "$RETVAL" = 2 ] && return 2 - # Wait for children to finish too if this is a daemon that forks - # and if the daemon is only ever run from this initscript. - # If the above conditions are not satisfied then add some other code - # that waits for the process to drop all resources that could be - # needed by services started subsequently. A last resort is to - # sleep for some time. - start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --user $USER --pidfile $PIDFILE - [ "$?" = 2 ] && return 2 - # Many daemons don't delete their pidfiles when they exit. - rm -f $PIDFILE - return "$RETVAL" -} - -# -# Function that sends a SIGHUP to the daemon/service -# -do_reload() { - # - # If the daemon can reload its configuration without - # restarting (for example, when it is sent a SIGHUP), - # then implement that here. - # - start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME - return 0 -} - -case "$1" in - start) - [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" - do_start - case "$?" in - 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; - 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; - esac - ;; - stop) - [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" - do_stop - case "$?" in - 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; - 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; - esac - ;; - status) - status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $? - ;; - #reload|force-reload) - # - # If do_reload() is not implemented then leave this commented out - # and leave 'force-reload' as an alias for 'restart'. - # - #log_daemon_msg "Reloading $DESC" "$NAME" - #do_reload - #log_end_msg $? - #;; - restart|force-reload) - # - # If the "reload" option is implemented then remove the - # 'force-reload' alias - # - log_daemon_msg "Restarting $DESC" "$NAME" - do_stop - case "$?" in - 0|1) - do_start - case "$?" in - 0) log_end_msg 0 ;; - 1) log_end_msg 1 ;; # Old process is still running - *) log_end_msg 1 ;; # Failed to start - esac - ;; - *) - # Failed to stop - log_end_msg 1 - ;; - esac - ;; - *) - #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2 - echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 - exit 3 - ;; -esac - -: diff --git a/config/hex-zero.etc.default b/config/hex-zero.etc.default new file mode 100644 index 0000000..c8d8da7 --- /dev/null +++ b/config/hex-zero.etc.default @@ -0,0 +1,7 @@ +export UWSGI_USER=hex-zero +export UWSGI_BIN=/usr/bin/uwsgi_python3 +export LISTEN_HOST=localhost +export LISTEN_PORT=3031 +export PIDFILE=/run/hex-zero.pid +export LOGFILE=/var/log/hex-zero/hex-zero.log +export UWSGI_OPTS="--http-socket ${LISTEN_HOST}:${LISTEN_PORT} --wsgi-file /var/www/0x0/hex-zero.py --callable app --log-x-forwarded-for --logto ${LOGFILE} --touch-reload /var/www/0x0/hex-zero.py --touch-reload /var/www/0x0/hex-zero.conf --pidfile ${PIDFILE}" diff --git a/config/hex-zero.init b/config/hex-zero.init new file mode 100755 index 0000000..17bac95 --- /dev/null +++ b/config/hex-zero.init @@ -0,0 +1,167 @@ +#!/bin/sh +### BEGIN INIT INFO +# Provides: hex-zero +# Required-Start: $local_fs $network $remote_fs $syslog +# Required-Stop: $local_fs $network $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: 0x0 file hosting service +# Description: Python3 Flask application that presents a basic file hosting service +### END INIT INFO + +# Author: B. Stack + +# Do NOT "set -e" + +# PATH should only include /usr/* if it runs after the mountnfs.sh script +PATH=/sbin:/usr/sbin:/bin:/usr/bin +DESC="hex-zero" +NAME=hex-zero +DAEMON=/var/www/0x0/hex-zero.wsgi +DAEMON_ARGS="" +PIDFILE=/tmp/$NAME.pid +SCRIPTNAME=/etc/init.d/$NAME +USER=hex-zero + +# Exit if the package is not installed +[ -x "$DAEMON" ] || exit 0 + +# Read configuration variable file if it is present +[ -r /etc/default/$NAME ] && . /etc/default/$NAME + +# Load the VERBOSE setting and other rcS variables +. /lib/init/vars.sh + +# Define LSB log_* functions. +# Depend on lsb-base (>= 3.2-14) to ensure that this file is present +# and status_of_proc is working. +. /lib/lsb/init-functions + +# +# Function that starts the daemon/service +# +do_start() +{ + # Return + # 0 if daemon has been started + # 1 if daemon was already running + # 2 if daemon could not be started + #start-stop-daemon --start --quiet --chuid $USER --group $USER --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \ + # || return 1 + #start-stop-daemon --start --quiet --chuid $USER --group $USER --background --pidfile $PIDFILE --exec $DAEMON -- \ + # $DAEMON_ARGS \ + # || return 2 + #su $USER -c "$DAEMON" & + # The above code will not work for interpreted scripts, use the next + # six lines below instead (Ref: #643337, start-stop-daemon(8) ) + start-stop-daemon --start --chuid $USER --pidfile $PIDFILE --startas $DAEMON \ + --name uwsgi_python3 --test > /dev/null \ + || return 1 + start-stop-daemon --start --chuid $USER --background --pidfile $PIDFILE --startas $DAEMON \ + --name uwsgi_python3 -- $DAEMON_ARGS \ + || return 2 + + # Add code here, if necessary, that waits for the process to be ready + # to handle requests from services started subsequently which depend + # on this one. As a last resort, sleep for some time. +} + +# +# Function that stops the daemon/service +# +do_stop() +{ + # Return + # 0 if daemon has been stopped + # 1 if daemon was already stopped + # 2 if daemon could not be stopped + # other if a failure occurred + #start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --user $USER --pidfile $PIDFILE + /usr/bin/uwsgi_python3 --stop $PIDFILE 2>/dev/null + RETVAL="$?" + [ "$RETVAL" = 2 ] && return 2 + # Wait for children to finish too if this is a daemon that forks + # and if the daemon is only ever run from this initscript. + # If the above conditions are not satisfied then add some other code + # that waits for the process to drop all resources that could be + # needed by services started subsequently. A last resort is to + # sleep for some time. + start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --user $USER --pidfile $PIDFILE + [ "$?" = 2 ] && return 2 + # Many daemons don't delete their pidfiles when they exit. + rm -f $PIDFILE + return "$RETVAL" +} + +# +# Function that sends a SIGHUP to the daemon/service +# +do_reload() { + # + # If the daemon can reload its configuration without + # restarting (for example, when it is sent a SIGHUP), + # then implement that here. + # + start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name uwsgi_python3 + return 0 +} + +case "$1" in + start) + [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME" + do_start + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + stop) + [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME" + do_stop + case "$?" in + 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;; + 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;; + esac + ;; + status) + status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $? + ;; + #reload|force-reload) + # + # If do_reload() is not implemented then leave this commented out + # and leave 'force-reload' as an alias for 'restart'. + # + #log_daemon_msg "Reloading $DESC" "$NAME" + #do_reload + #log_end_msg $? + #;; + restart|force-reload) + # + # If the "reload" option is implemented then remove the + # 'force-reload' alias + # + log_daemon_msg "Restarting $DESC" "$NAME" + do_stop + case "$?" in + 0|1) + do_start + case "$?" in + 0) log_end_msg 0 ;; + 1) log_end_msg 1 ;; # Old process is still running + *) log_end_msg 1 ;; # Failed to start + esac + ;; + *) + # Failed to stop + log_end_msg 1 + ;; + esac + ;; + *) + #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2 + echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 + exit 3 + ;; +esac + +: diff --git a/hex-zero.py b/hex-zero.py index a93ebe0..81c8169 100755 --- a/hex-zero.py +++ b/hex-zero.py @@ -312,7 +312,6 @@ def store_url(url, addr): @app.route("/") def get(path): - app.logger.info("next source: {0}".format(str(request.environ["HTTP_X_FORWARDED_FOR"]))) p = os.path.splitext(path) id = su.debase(p[0]) @@ -349,7 +348,6 @@ def get(path): @app.route("/dump_files/") @app.route("/dump_files/") def dump_files(start=0): - app.logger.info("next source: {0}".format(str(request.environ["HTTP_X_FORWARDED_FOR"]))) meta = "#FORMAT: BEACON\n#PREFIX: {}/\n\n".format(fhost_url("https")) def gen(): @@ -363,7 +361,6 @@ def dump_files(start=0): @app.route("/dump_urls/") @app.route("/dump_urls/") def dump_urls(start=0): - app.logger.info("next source: {0}".format(str(request.environ["HTTP_X_FORWARDED_FOR"]))) meta = "#FORMAT: BEACON\n#PREFIX: {}/\n\n".format(fhost_url("https")) def gen(): @@ -381,7 +378,6 @@ def dump_urls(start=0): @app.route("/", methods=["GET", "POST"]) def fhost(): - app.logger.info("next source: {0}".format(str(request.environ["HTTP_X_FORWARDED_FOR"]))) if request.method == "POST": out = None @@ -397,6 +393,7 @@ def fhost(): out = shorten(request.form["shorten"]) if not out == None: + app.logger.info("Stored file: "+str(out.strip())) return Response(out, mimetype="text/plain") abort(400) @@ -422,7 +419,6 @@ def fhost(): @app.route("/robots.txt") def robots(): - app.logger.info("next source: {0}".format(str(request.environ["HTTP_X_FORWARDED_FOR"]))) return """User-agent: * Disallow: / """ diff --git a/hex-zero.wsgi b/hex-zero.wsgi new file mode 100755 index 0000000..7f10e20 --- /dev/null +++ b/hex-zero.wsgi @@ -0,0 +1,19 @@ +#!/bin/sh +test -f /etc/default/hex-zero && . /etc/default/hex-zero +test -z "${UWSGI_USER}" && export UWSGI_USER=hex-zero +test -z "${UWSGI_BIN}" && export UWSGI_BIN=/usr/bin/uwsgi_python3 +test -z "${LISTEN_HOST}" && export LISTEN_HOST=localhost +test -z "${LISTEN_PORT}" && export LISTEN_PORT=3031 +test -z "${PIDFILE}" && export PIDFILE=/tmp/hex-zero.pid +test -z "${LOGFILE}" && export LOGFILE=/var/log/hex-zero/hex-zero.log +test -z "${UWSGI_OPTS}" && export UWSGI_OPTS="--http-socket ${LISTEN_HOST}:${LISTEN_PORT} --wsgi-file /var/www/0x0/hex-zero.py --callable app --log-x-forwarded-for --logto ${LOGFILE} --touch-reload /var/www/0x0/hex-zero.py --touch-reload /var/www/0x0/hex-zero.conf --pidfile ${PIDFILE}" + +#test -n "${FORCE_KILL_OLD_UWSGI}" && { +# ps -o user=,pid=,command:80= -u "${UWSGI_USER}" | awk '/hex-zero\.py/{print $2}' | xargs --no-run-if-empty kill +#} + +#test "${UWSGI_USER}" != "${USER}" && { +# printf "Please run $0 as user UWSGI_USER (${UWSGI_USER}) and not $USER. Aborted.\n" 1>&2 ; exit 1 ; +#} + +${UWSGI_BIN} ${UWSGI_OPTS} -- cgit