http.sh/http.sh

94 lines
3 KiB
Bash
Raw Normal View History

2020-05-23 22:13:11 +02:00
#!/bin/bash
trap ctrl_c INT
source config/master.sh
2020-05-23 22:13:11 +02:00
function ctrl_c() {
2020-10-11 20:25:05 +02:00
[[ $socket != '' ]] && rm $socket
2020-05-23 22:13:11 +02:00
pkill -P $$
2020-10-11 20:25:05 +02:00
echo -e "Cleaned up, exitting.\nHave an awesome day!!"
2020-05-23 22:13:11 +02:00
}
if [[ ! -f "$(pwd)/http.sh" ]]; then
echo -e "Please run HTTP.sh inside it's designated directory\nRunning the script from arbitrary locations isn't supported."
exit 1
fi
2020-10-11 20:25:05 +02:00
echo "-= HTTP.sh =-"
for i in $(cat src/dependencies.required); do
which $i > /dev/null 2>&1
if [[ $? != 0 ]]; then
echo "ERROR: can't find $i"
error=true
fi
done
for i in $(cat src/dependencies.optional); do
which $i > /dev/null 2>&1
[[ $? != 0 ]] && echo "WARNING: can't find $i"
done
if [[ $error == true ]]; then
echo "Fix above dependencies, and we might just let you pass."
exit 0
fi
if [[ $1 == "init" ]]; then # will get replaced with proper parameter parsing in 1.0
mkdir -p "${cfg[namespace]}/${cfg[root]}" "${cfg[namespace]}/workers/example"
touch "${cfg[namespace]}/config.sh" "${cfg[namespace]}/workers/example/control"
cat <<LauraIsCute > "${cfg[namespace]}/config.sh"
# app config - loaded on server bootup
# your application-specific config goes here!
# worker_add <worker> <interval>
# ---
# worker_add example 5
LauraIsCute
cat <<LauraIsCute > "${cfg[namespace]}/workers/example/worker.sh"
#!/bin/bash
date
LauraIsCute
cat <<LauraIsCute > "${cfg[namespace]}/${cfg[root]}/index.shs"
#!/bin/bash
source templates/head.sh
echo "<h1>Hello from HTTP.sh!</h1><br>To get started with your app, check out $(pwd)/${cfg[namespace]}/
<ul><li>$(pwd)/${cfg[namespace]}/${cfg[root]} - your files go here</li>
<li>$(pwd)/${cfg[namespace]}/workers/ - worker directory, with an example one ready to go</li>
<li>$(pwd)/${cfg[namespace]}/config.sh - config for everything specific to your app AND workers</li>
<li>$(pwd)/config/master.sh - master server config</li>
<li>$(pwd)/src/ - HTTP.sh src, feel free to poke around :P</li></ul>
&copy; sdomi, selfisekai, ptrcnull - 2020"
LauraIsCute
chmod +x "${cfg[namespace]}/workers/example/worker.sh"
echo -e "Success..?\nTry running ./http.sh now"
exit 0
fi
2020-07-27 10:59:15 +02:00
source src/worker.sh
if [[ -f "${cfg[namespace]}/config.sh" ]]; then
source "${cfg[namespace]}/config.sh"
2020-07-27 10:59:15 +02:00
fi
if [[ ${cfg[http]} == true ]]; then
2020-10-11 20:25:05 +02:00
# this is a workaround because ncat kept messing up large (<150KB) files over HTTP - but not over HTTPS!
socket=$(mktemp -u /tmp/XXXX.socket)
ncat -l -U "$socket" -c src/server.sh -k 2>> /dev/null &
socat TCP-LISTEN:${cfg[port]},fork,bind=${cfg[ip]} UNIX-CLIENT:$socket &
echo "[HTTP] listening on ${cfg[ip]}:${cfg[port]} through '$socket'"
#ncat -v -l ${cfg[ip]} ${cfg[port]} -c ./src/server.sh -k 2>> /dev/null &
fi
2020-05-23 22:13:11 +02:00
if [[ ${cfg[ssl]} == true ]]; then
echo "[SSL] listening on port ${cfg[ip]}:${cfg[ssl_port]}"
2020-05-23 22:13:11 +02:00
if [[ ${cfg[ssl_key]} != '' && ${cfg[ssl_cert]} != '' ]]; then
2020-10-11 20:25:05 +02:00
ncat -l ${cfg[ip]} ${cfg[ssl_port]} -c ./src/server.sh -k --ssl --ssl-cert ${cfg[ssl_cert]} --ssl-key ${cfg[ssl_key]} 2>> /dev/null &
2020-05-23 22:13:11 +02:00
else
2020-10-11 20:25:05 +02:00
ncat -l ${cfg[ip]} ${cfg[ssl_port]} -c ./src/server.sh -k --ssl 2>> /dev/null &
2020-05-23 22:13:11 +02:00
fi
fi
wait