forked from qo-op/G1BILLET
281 lines
11 KiB
Bash
Executable File
281 lines
11 KiB
Bash
Executable File
#!/bin/bash
|
|
################################################################################
|
|
# Author: Fred (support@qo-op.com)
|
|
# Version: 1.0
|
|
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
|
|
################################################################################
|
|
MY_PATH="`dirname \"$0\"`" # relative
|
|
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
|
|
ME="${0##*/}"
|
|
|
|
################################################################################
|
|
# Create different king of G1BILLET(s) with $MONTANT DU or TW IPNS
|
|
# ${MY_PATH}/G1BILLETS.sh 5 986397643 ticket # MONTANT # STYLE #
|
|
################################################################################
|
|
MONTANT="$1"
|
|
[[ $MONTANT == "0" ]] && MONTANT=""
|
|
|
|
### COMMAND MODE ###
|
|
if [[ $MONTANT != "daemon" ]]; then
|
|
|
|
[[ $MONTANT == "" ]] && MONTANT="___"
|
|
|
|
STYLE="$2"
|
|
|
|
DICE=$(cat $MY_PATH/DICE 2>/dev/null) ## HOW MANY WORDS SECRETS
|
|
[[ $DICE != ?(-)+([0-9]) ]] && DICE=2
|
|
echo "$DICE WORDS SECRETS (Define length in $MY_PATH/DICE)"
|
|
|
|
## CHECK IF STYLE IS EMAIL => astro IPFS G1BILLET
|
|
if [[ "${STYLE}" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$ ]]; then
|
|
echo "ASTROPORT NEW PLAYER"
|
|
# echo "PLAYER : $STYLE"
|
|
EMAIL=${STYLE}
|
|
STYLE="astro"
|
|
fi
|
|
|
|
## STYLE SELECTED: PDF DE 6 BILLETS OR SINGLE
|
|
NBbillets=6
|
|
[[ "${STYLE}" != "" && "${STYLE}" != "solian" && "${STYLE}" != "jeu" ]] && NBbillets=1
|
|
|
|
echo "CREATION $NBbillets x G1BILLET - $STYLE -"
|
|
|
|
# CHECK IF $STYLE IMAGES EXIST
|
|
[[ ! -f ${MY_PATH}/images/g1${STYLE}.png ]] && echo "AJOUTER : ./images/g1${STYLE}.jpg" && exit 1
|
|
[[ ! -f ${MY_PATH}/images/fond${STYLE}.jpg ]] && echo "STYLE INCONNU ? ./images/fond${STYLE}.jpg" && exit 1
|
|
[[ ! -f ${MY_PATH}/images/logo${STYLE}.png ]] && echo "AJOUTER : ./images/logo${STYLE}.jpg" && exit 1
|
|
|
|
UNIQID=$(${MY_PATH}/diceware.sh 1 | xargs)${RANDOM:0:2}$(${MY_PATH}/diceware.sh 1 | xargs)
|
|
|
|
# CREATION DE $NBbillets BILLETS DE $MONTANT DU
|
|
boucle=0;
|
|
while [ $boucle -lt $NBbillets ]
|
|
do
|
|
boucle=$((boucle+1))
|
|
mkdir -p "${MY_PATH}/tmp/g1billet/${UNIQID}"
|
|
|
|
## ADAPT SECURITY LEVEL
|
|
SALT=$(${MY_PATH}/diceware.sh $DICE | xargs)
|
|
SECRET=$(${MY_PATH}/diceware.sh $DICE | xargs)
|
|
|
|
# CREATION CLEF BILLET
|
|
BILLETPUBKEY=$(python3 ${MY_PATH}/key_create_dunikey.py "$SALT" "$SECRET")
|
|
rm -f /tmp/secret.dunikey
|
|
echo "$boucle : $BILLETPUBKEY"
|
|
|
|
${MY_PATH}/keygen -t ipfs -o ${MY_PATH}/tmp/g1billet.${UNIQID}.ipns.key "$SALT" "$SECRET"
|
|
ASTRONAUTENS=$(ipfs key import ${UNIQID} -f pem-pkcs8-cleartext ${MY_PATH}/tmp/g1billet.${UNIQID}.ipns.key)
|
|
rm -f ${MY_PATH}/tmp/g1billet.${UNIQID}.ipns.key
|
|
ipfs key rm ${UNIQID} && echo "/ipns/$ASTRONAUTENS"
|
|
|
|
### GRAPHISM #####
|
|
# CREATION FICHIER IMAGE BILLET dans ${MY_PATH}/tmp/g1billet/${UNIQID}
|
|
$(${MY_PATH}/MAKE_G1BILLET.sh "${SALT}" "${SECRET}" "${MONTANT}" "${BILLETPUBKEY}" "${UNIQID}" "${STYLE}" "${ASTRONAUTENS}" "${EMAIL}")
|
|
|
|
done
|
|
|
|
if [[ ${NBbillets} == 1 ]]
|
|
then
|
|
|
|
# ONE FILE ONLY
|
|
cp ${MY_PATH}/tmp/g1billet/${UNIQID}/*.jpg ${MY_PATH}/tmp/g1billet/${UNIQID}.jpg
|
|
|
|
# CLEANING TEMP FILES
|
|
rm -Rf ${MY_PATH}/tmp/g1billet/${UNIQID}
|
|
# ALLOWS ANY USER TO DELETE
|
|
chmod 777 ${MY_PATH}/tmp/g1billet/${UNIQID}.jpg
|
|
export ZFILE="${MY_PATH}/tmp/g1billet/${UNIQID}.jpg"
|
|
|
|
else
|
|
|
|
# MONTAGE DES IMAGES DES BILLETS VERS ${MY_PATH}/tmp/g1billet/${UNIQID}.pdf
|
|
montage ${MY_PATH}/tmp/g1billet/${UNIQID}/*.jpg -tile 2x3 -geometry 964x459 ${MY_PATH}/tmp/g1billet/${UNIQID}.pdf
|
|
# NB!! if "not autorized" then edit /etc/ImageMagick-6/policy.xml and comment
|
|
# <!-- <policy domain="coder" rights="none" pattern="PDF" /> -->
|
|
# CLEANING TEMP FILES
|
|
rm -Rf ${MY_PATH}/tmp/g1billet/${UNIQID}
|
|
# ALLOWS ANY USER TO DELETE
|
|
chmod 777 ${MY_PATH}/tmp/g1billet/${UNIQID}.pdf
|
|
export ZFILE="${MY_PATH}/tmp/g1billet/${UNIQID}.pdf"
|
|
|
|
fi
|
|
|
|
|
|
###########################################################################
|
|
## ASTRO : FIND TW ADDRESS + EMAIL = CREATE PLAYER on 1234 API
|
|
|
|
if [[ ${EMAIL} ]]; then
|
|
|
|
UN=$(echo ${SALT} | jq -Rr @uri) ## URL ENCODED
|
|
DE=$(echo ${SECRET} | jq -Rr @uri) ## URL ENCODED
|
|
echo curl -s 'https://astroport.copylaradio.com/?salt='$UN'&pepper='$DE'&g1pub=on&email='$EMAIL
|
|
# && curl -s 'https://astroport.copylaradio.com/?salt='$UN'&pepper='$DE'&g1pub=on&email='$EMAIL
|
|
|
|
fi
|
|
###########################################################################
|
|
|
|
[[ $XDG_SESSION_TYPE == 'x11' ]] && xdg-open "$ZFILE"
|
|
|
|
echo "$ZFILE" # IMPORTANT ## LAST LINE : INFORM DAEMON
|
|
|
|
else
|
|
################################################################################
|
|
################################################################################
|
|
## MAKE IT A NETWORK MICRO SERVICE -- PORTS : INPUT=33101 OUTPUT=33102
|
|
############## CLEAN START DAEMON MODE ###
|
|
pidportinuse=$(ps axf --sort=+utime | grep -w "nc -l -p 33101" | grep -v -E 'color=auto|grep' | awk '{gsub(/^ +| +$/,"")} {print $0}' | tail -n 1 | cut -d " " -f 1)
|
|
[[ $pidportinuse ]] && echo "KILLING OLD DEAMON 33101 $pidportinuse" && kill -9 $pidportinuse && killall G1BILLETS.sh && exit 1
|
|
|
|
pidportinuse=$(ps axf --sort=+utime | grep -w "nc -l -p 33102" | grep -v -E 'color=auto|grep' | awk '{gsub(/^ +| +$/,"")} {print $0}' | tail -n 1 | cut -d " " -f 1)
|
|
[[ $pidportinuse ]] && kill -9 $pidportinuse && echo "KILLING NOT COLLECTED THREAD $pidportinuse"
|
|
#####################################################################
|
|
myIP=$(hostname -I | awk '{print $1}' | head -n 1)
|
|
isLAN=$(route -n |awk '$1 == "0.0.0.0" {print $2}' | grep -E "/(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1$)|(^[fF][cCdD])/")
|
|
|
|
if [[ $isLAN ]]; then
|
|
HNAME="http://g1billet.localhost"
|
|
else
|
|
HNAME="http://vmi891906.contaboserver.net"
|
|
fi
|
|
RNAME="$HNAME:33102"
|
|
|
|
HTTPWELLCOME='HTTP/1.1 200 OK
|
|
Access-Control-Allow-Origin: *
|
|
Access-Control-Allow-Credentials: true
|
|
Access-Control-Allow-Methods: GET
|
|
Server: Astroport
|
|
Content-Type: text/html; charset=UTF-8
|
|
|
|
<!DOCTYPE html><html>
|
|
<head>
|
|
<title>[G1BILLET] HTTP MICRO SERVICE - 33101 - 33102 -</title>
|
|
<meta http-equiv="refresh" content="20; url='$RNAME'" />
|
|
<style>
|
|
#countdown { display: flex; justify-content: center; align-items: center; color: #0e2c4c; font-size: 20px; width: 60px; height: 60px; background-color: #e7d9fc; border-radius: 50%;}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<center><h1><a href="'$RNAME'">(♥‿‿♥)</a>.</h1></center>
|
|
<center><div id="countdown"></div></center>
|
|
<script>
|
|
var timeLeft = 20;
|
|
var elem = document.getElementById("countdown");
|
|
var timerId = setInterval(countdown, 1000);
|
|
|
|
function countdown() {
|
|
if (timeLeft == -1) {
|
|
clearTimeout(timerId);
|
|
doSomething();
|
|
} else {
|
|
elem.innerHTML = timeLeft + " s";
|
|
timeLeft--;
|
|
}
|
|
}
|
|
</script>
|
|
</body></html>'
|
|
|
|
function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
|
|
|
|
#### LOG REDIRECTION
|
|
echo "=================================================="
|
|
echo "PLANCHE : $HNAME:33101"
|
|
echo "TICKET : $HNAME:33101/?montant=10&style=ticket"
|
|
echo "ASTRO TW : $HNAME:33101/?montant=0&style=astro${RANDOM}@yopmail.com"
|
|
echo "=================================================="
|
|
echo "LOG : tail -f ${MY_PATH}/tmp/G1BILLETS.log"
|
|
echo "=================================================="
|
|
mkdir -p ${MY_PATH}/tmp
|
|
exec 2>&1 >> ${MY_PATH}/tmp/G1BILLETS.log
|
|
|
|
#####################################################################
|
|
########### daemon loop
|
|
#####################################################################
|
|
while true; do
|
|
echo "=================================================="
|
|
echo "$ME DAEMON READY"
|
|
echo "=================================================="
|
|
echo "LOG : ON"
|
|
echo "=================================================="
|
|
REQ=$(echo "$HTTPWELLCOME" | nc -l -p 33101 -q 1) ## # WAIT FOR 33101 PORT CONTACT
|
|
|
|
MOATS=$(date -u +"%Y%m%d%H%M%S%4N")
|
|
|
|
URL=$(echo "$REQ" | grep '^GET' | cut -d ' ' -f2 | cut -d '?' -f2) ### CONTACT URL ####
|
|
HOSTP=$(echo "$REQ" | grep '^Host:' | cut -d ' ' -f2 | cut -d '?' -f2)
|
|
HOST=$(echo "$HOSTP" | cut -d ':' -f 1)
|
|
|
|
echo "=================================================="
|
|
echo "$MOATS" $(date)
|
|
echo "=================================================="
|
|
echo "$REQ"
|
|
echo "=================================================="
|
|
echo "$URL"
|
|
arr=(${URL//[=&]/ })
|
|
# PARAM (x 3) EXTRACT "¶m=value"
|
|
ONE=$(urldecode ${arr[0]} | xargs); TWO=$(urldecode ${arr[2]} | xargs);
|
|
MONTANT=$(urldecode ${arr[1]} | xargs); STYLE=$(urldecode ${arr[3]} | xargs);
|
|
# echo "DECODED : $ONE=$MONTANT & $TWO=$STYLE"
|
|
|
|
#####################################################################
|
|
echo ${MY_PATH}/${ME} '"'$MONTANT'"' '"'$STYLE'"'
|
|
#####################################################################
|
|
# EXECUTE COMMAND
|
|
LOG=$(${MY_PATH}/${ME} "$MONTANT" "$STYLE")
|
|
|
|
echo $LOG
|
|
# EXTRACT VALUES FROM SELF LOG
|
|
IPNS=$(echo "$LOG" | grep '/ipns/')
|
|
[[ $IPNS ]] && echo "TW IPNS : $IPNS"
|
|
CURL=$(echo "$LOG" | grep -w curl)
|
|
[[ $IPNS ]] && echo "LIEN ACTIVATION : $CURL"
|
|
|
|
ZFILE=$(echo "$LOG" | tail -n 1) ### LAST LINE : INFORM DAEMON
|
|
|
|
# PREPARE FILE SENDING
|
|
FILE_NAME="$(basename "${ZFILE}")"
|
|
EXT="${FILE_NAME##*.}"
|
|
BSIZE=$(du -b "${ZFILE}" | awk '{print $1}' | tail -n 1)
|
|
|
|
# KILL OLD 33102 - USE IT IF YOU ( publishing )&
|
|
pidportinuse=$(ps axf --sort=+utime | grep -w "nc -l -p 33102" | grep -v -E 'color=auto|grep' | awk '{gsub(/^ +| +$/,"")} {print $0}' | tail -n 1 | cut -d " " -f 1)
|
|
[[ $pidportinuse ]] && kill -9 $pidportinuse && echo "KILLING NOT COLLECTED THREAD $pidportinuse"
|
|
|
|
# HTTP/1.1 200 OK
|
|
echo 'HTTP/1.1 200 OK
|
|
Access-Control-Allow-Origin: *
|
|
Access-Control-Allow-Credentials: true
|
|
Access-Control-Allow-Methods: GET
|
|
Server: Astroport.G1BILLET
|
|
Cache-Control: public
|
|
Content-Transfer-Encoding: Binary
|
|
Content-Length:'${BSIZE}'
|
|
Content-Disposition: attachment; filename='${FILE_NAME}'
|
|
' > ${MY_PATH}/tmp/http.${MOATS}
|
|
|
|
cat ${ZFILE} >> ${MY_PATH}/tmp/http.${MOATS}
|
|
|
|
# NETCAT PUBLISH port=33102
|
|
echo "PUBLISHING ${MOATS} : $RNAME"
|
|
|
|
if [[ $XDG_SESSION_TYPE != 'x11' ]]; then
|
|
(
|
|
cat ${MY_PATH}/tmp/http.${MOATS} | nc -l -p 33102 -q 1 > /dev/null 2>&1 \
|
|
&& rm -f "${MY_PATH}/tmp/http.${MOATS}" \
|
|
&& rm -f "${ZFILE}" \
|
|
&& echo "G1BILLETS FILE CONSUMED"
|
|
) &
|
|
else
|
|
rm -f "${MY_PATH}/tmp/http.${MOATS}" \
|
|
&& rm -f "${ZFILE}" \
|
|
&& echo "G1BILLETS FILE CONSUMED"
|
|
fi
|
|
|
|
done
|
|
#####################################################################
|
|
## loop ###############################################################
|
|
#####################################################################
|
|
|
|
fi
|
|
|
|
exit 0
|