Astroport.ONE/12345.sh

259 lines
12 KiB
Bash
Raw Normal View History

#!/bin/bash
################################################################################
# Author: Fred (support@qo-op.com)
# Version: 0.1
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
################################################################################
################################################################################
2022-10-22 13:01:46 +02:00
## ASTROPORT API SERVER http://$myIP:1234
## ATOMIC GET REDIRECT TO ONE SHOT WEB SERVICE THROUGH 12345-12445 PORTS
################################################################################
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
ME="${0##*/}"
MOATS=$(date -u +"%Y%m%d%H%M%S%4N")
IPFSNODEID=$(cat ~/.ipfs/config | jq -r .Identity.PeerID)
2022-10-22 12:20:29 +02:00
myIP=$(hostname -I | awk '{print $1}' | head -n 1)
[[ ! $myIP ]] && myIP="127.0.1.1"
PORT=12345
## CHECK FOR ANY ALREADY RUNNING nc
2022-10-07 02:16:53 +02:00
ncrunning=$(ps auxf --sort=+utime | grep -w 'nc -l -p 1234' | grep -v -E 'color=auto|grep' | tail -n 1 | cut -d " " -f 1)
2022-10-22 12:20:29 +02:00
[[ $ncrunning ]] && echo "ERROR - API Server Already Running - http://$myIP:1234/?salt=toto&pepper=toto " && exit 1
2022-10-22 12:20:29 +02:00
echo "LAUNCHING Astroport API Server - http://$myIP:1234/?salt=toto&pepper=toto&messaging"
2022-10-06 21:18:54 +02:00
[[ $DISPLAY ]] && xdg-open "file://$HOME/.zen/Astroport.ONE/templates/instascan.html" 2>/dev/null
function urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }
while true; do
2022-10-21 20:11:47 +02:00
MOATS=$(date -u +"%Y%m%d%H%M%S%4N")
2022-10-21 22:09:13 +02:00
## CHANGE NEXT PORT (HERE YOU CREATE A SOCKET QUEUE)
2022-10-22 12:20:29 +02:00
[ $PORT -lt 12445 ] && PORT=$((PORT+${RANDOM:0:2})) || PORT=12345
## RANDOM PORT SWAPPINESS
2022-10-21 22:09:13 +02:00
2022-10-21 20:22:28 +02:00
echo "************************************************************************* "
2022-10-22 13:01:46 +02:00
echo "ASTROPORT API SERVER UP.......................... http://$myIP:1234 PORT"
echo "$MOATS LANDING PAGE http://$myIP:$PORT"
2022-10-21 18:18:16 +02:00
# REPLACE myIP in http response template
2022-10-22 15:47:54 +02:00
sed "s~127.0.0.1:12345~$myIP:$PORT~g" $HOME/.zen/Astroport.ONE/templates/index.http > ~/.zen/tmp/${MOATS}.myIP.http
sed -i "s~127.0.0.1~$myIP~g" ~/.zen/tmp/${MOATS}.myIP.http
2022-10-21 18:18:16 +02:00
2022-10-22 13:01:46 +02:00
## WAITING TO SERVE LANDING REDIRECT PAGE
2022-10-22 15:47:54 +02:00
URL=$(cat $HOME/.zen/tmp/${MOATS}.myIP.http | nc -l -p 1234 -q 1 | grep '^GET' | cut -d ' ' -f2 | cut -d '?' -f2)
2022-10-22 13:01:46 +02:00
############################################################################
2022-10-21 20:11:47 +02:00
start=`date +%s`
2022-10-22 13:01:46 +02:00
## NO API CONTACT - PUBLISH HOMEPAGE
2022-10-21 21:50:40 +02:00
if [[ $URL == "/" ]]; then
2022-10-22 13:01:46 +02:00
echo "API NULL CALL : http://$myIP:1234"
echo "___________________________ Launching homepage.html"
2022-10-21 22:05:26 +02:00
echo "HTTP/1.1 200 OK
2022-10-21 22:00:29 +02:00
Server: Astroport
Content-Type: text/html; charset=UTF-8
2022-10-22 15:47:54 +02:00
" > ~/.zen/tmp/${MOATS}.index.redirect
sed "s~127.0.0.1~$myIP~g" $HOME/.zen/Astroport.ONE/templates/homepage.html >> ~/.zen/tmp/${MOATS}.index.redirect
2022-10-21 22:00:29 +02:00
2022-10-22 15:47:54 +02:00
cat ~/.zen/tmp/${MOATS}.index.redirect | nc -l -p ${PORT} -q 1 &
2022-10-22 13:01:46 +02:00
end=`date +%s`
echo Execution time was `expr $end - $start` seconds.
2022-10-21 22:00:29 +02:00
continue
2022-10-21 21:50:40 +02:00
fi
2022-10-21 22:00:29 +02:00
echo "=================================================="
echo "GET RECEPTION : $URL"
arr=(${URL//[=&]/ })
echo "PARAM : ${arr[0]} = ${arr[1]} & ${arr[2]} = ${arr[3]} & ${arr[4]} = ${arr[5]} & ${arr[6]} = ${arr[7]}"
[[ ${arr[0]} == "" && ${arr[1]} == "" ]] && echo "GET NO DATA" && continue
2022-10-07 17:08:35 +02:00
[[ ${arr[1]} == "ph1" ]] && echo "GET NO DATA" && continue
########## CHECK GET PARAM NAMES
###################################################################################################
###################################################################################################
2022-10-21 20:11:47 +02:00
# API ZERO : ?salt=Phrase%20Une&pepper=Phrase%20Deux
if [[ ${arr[0]} == "salt" ]]; then
2022-10-22 13:01:46 +02:00
echo "Application G1Radar !!"
SALT=$(urldecode ${arr[1]})
[[ ! $SALT ]] && echo "BAD SALT API CALL" && continue
PEPPER=$(urldecode ${arr[3]})
[[ ! $PEPPER ]] && echo "BAD PEPPER API CALL" && continue
2022-10-21 20:11:47 +02:00
TYPE=$(urldecode ${arr[4]})
PLAYER=$(urldecode ${arr[5]})
2022-10-22 13:01:46 +02:00
echo "API ZERO CALL : http://$myIP:1234/?salt=$SALT&pepper=$PEPPER&$TYPE=$PLAYER"
2022-10-22 15:47:54 +02:00
echo "\"$SALT\" \"$PEPPER\"" > ~/.zen/tmp/${MOATS}.secret.june
# CALCULATING G1PUB
${MY_PATH}/tools/keygen -t duniter -o ~/.zen/tmp/${MOATS}.secret.key "$SALT" "$PEPPER"
G1PUB=$(cat ~/.zen/tmp/${MOATS}.secret.key | grep 'pub:' | cut -d ' ' -f 2)
[[ ! $G1PUB ]] && echo "ERROR - G1PUB COMPUTATION EMPTY" && continue
## CALCULATING IPNS ADDRESS
ipfs key rm gchange 2>/dev/null
2022-10-22 15:47:54 +02:00
rm -f ~/.zen/tmp/${MOATS}.${G1PUB}.ipns.key
${MY_PATH}/tools/keygen -t ipfs -o ~/.zen/tmp/${MOATS}.${G1PUB}.ipns.key "$SALT" "$PEPPER"
GNS=$(ipfs key import gchange -f pem-pkcs8-cleartext ~/.zen/tmp/${MOATS}.${G1PUB}.ipns.key )
echo "Astronaute TW ? http://$myIP:8080/ipns/$GNS"
2022-10-21 13:54:53 +02:00
2022-10-21 20:20:09 +02:00
if [[ $TYPE == "messaging" ]]; then
2022-10-21 20:11:47 +02:00
2022-10-22 13:01:46 +02:00
echo "Extracting $G1PUB messages..."
2022-10-22 15:47:54 +02:00
${MY_PATH}/tools/jaklis/jaklis.py -k ~/.zen/tmp/${MOATS}.secret.key read -n 10 -j > ~/.zen/tmp/messin.${G1PUB}.json
[[ $(grep -v -E 'Aucun message à afficher' ~/.zen/tmp/messin.${G1PUB}.json) == "True" ]] && echo "[]" > ~/.zen/tmp/messin.${G1PUB}.json
${MY_PATH}/tools/jaklis/jaklis.py -k ~/.zen/tmp/${MOATS}.secret.key read -n 10 -j -o > ~/.zen/tmp/messout.${G1PUB}.json
[[ $(grep -v -E 'Aucun message à afficher' ~/.zen/tmp/messout.${G1PUB}.json) == "True" ]] && echo "[]" > ~/.zen/tmp/messout.${G1PUB}.json
echo "Creating messages In/Out JSON ~/.zen/tmp/${MOATS}.messaging.json"
echo '[' > ~/.zen/tmp/${MOATS}.messaging.json
cat ~/.zen/tmp/messin.${G1PUB}.json >> ~/.zen/tmp/${MOATS}.messaging.json
echo "," >> ~/.zen/tmp/${MOATS}.messaging.json
cat ~/.zen/tmp/messout.${G1PUB}.json >> ~/.zen/tmp/${MOATS}.messaging.json
echo ']' >> ~/.zen/tmp/${MOATS}.messaging.json
2022-10-21 20:11:47 +02:00
2022-10-21 22:05:26 +02:00
echo "HTTP/1.1 200 OK
2022-10-21 20:11:47 +02:00
Server: Astroport
Content-Type: text/html; charset=UTF-8
2022-10-22 15:47:54 +02:00
" > ~/.zen/tmp/${MOATS}.index.redirect
cat ~/.zen/tmp/${MOATS}.messaging.json >> ~/.zen/tmp/${MOATS}.index.redirect
2022-10-21 20:11:47 +02:00
fi
2022-10-22 13:01:46 +02:00
## IF NO MESSAGING => IPNS TW REDIRECT PAGE
2022-10-22 15:47:54 +02:00
[[ "$TYPE" == "g1pub" ]] && sed "s~_TWLINK_~https://www.gchange.fr/#/app/user/$G1PUB/~g" ~/.zen/Astroport.ONE/templates/index.redirect > ~/.zen/tmp/${MOATS}.index.redirect
[[ ! -f ~/.zen/tmp/${MOATS}.index.redirect ]] && sed "s~_TWLINK_~http://$myIP:8080/ipns/$GNS~g" ~/.zen/Astroport.ONE/templates/index.redirect > ~/.zen/tmp/${MOATS}.index.redirect
2022-10-21 20:11:47 +02:00
## RESPONDING
2022-10-22 15:47:54 +02:00
cat ~/.zen/tmp/${MOATS}.index.redirect | nc -l -p ${PORT} -q 1 &
echo "HTTP 1.1 PROTOCOL DOCUMENT READY ~/.zen/tmp/${MOATS}.index.redirect"
2022-10-22 13:01:46 +02:00
echo "$MOATS -----> PAGE AVAILABLE -----> http://$myIP:${PORT}"
2022-10-21 19:03:05 +02:00
#echo "$GNS" | nc -l -p ${PORT} -q 1 &
## CHECK IF ALREADY EXISTING PLAYER
2022-10-21 13:54:53 +02:00
# IF NOT = BATCH CREATE TW
2022-10-21 20:11:47 +02:00
end=`date +%s`
echo Execution time was `expr $end - $start` seconds.
continue
fi
###################################################################################################
###################################################################################################
# API ONE : ?email/elastic=ELASTICID&salt=PHRASE%20UNE&pepper=PHRASE%20DEUX&pseudo=PROFILENAME
if [[ ${arr[0]} == "email" || ${arr[0]} == "elastic" ]]; then
2022-10-07 02:50:28 +02:00
2022-10-07 02:16:53 +02:00
#######################################
2022-10-22 15:47:54 +02:00
### RELAUCH myIP.http.${MOATS} SELF REDIRECT $PORT PAGE UNTIL ~/.zen/tmp/${MOATS}.index.redirect IS CREATED &
2022-10-06 15:57:22 +02:00
###################################################################################################
2022-10-22 15:47:54 +02:00
while [[ ! -f ~/.zen/tmp/${MOATS}.index.redirect && ! $(ps auxf --sort=+utime | grep -w 'nc -l -p '${PORT} | grep -v -E 'color=auto|grep') ]]; do cat $HOME/.zen/tmp/${MOATS}.myIP.http | nc -l -p ${PORT} -q 1; done &
2022-10-06 15:57:22 +02:00
###################################################################################################
2022-10-22 13:01:46 +02:00
PLAYER=$(urldecode ${arr[1]})
SALT=$(urldecode ${arr[3]})
PEPPER=$(urldecode ${arr[5]})
PSEUDO=$(urldecode ${arr[7]})
if [[ ! $PSEUDO ]]; then
PSEUDO=$(echo $PLAYER | cut -d '@' -f 1)
PSEUDO=${PSEUDO,,}; PSEUDO=${PSEUDO%%[0-9]*}
fi
2022-10-06 15:57:22 +02:00
# PASS CRYPTING KEY
PASS=$(echo "${RANDOM}${RANDOM}${RANDOM}${RANDOM}" | tail -c-7)
echo "$SALT"
echo "$PEPPER"
if [[ ! -d ~/.zen/game/players/$PLAYER ]]; then
2022-10-22 13:01:46 +02:00
# ASTRONAUT NEW VISA
$MY_PATH/tools/VISA.new.sh "$SALT" "$PEPPER" "$PLAYER" "$PSEUDO"
2022-10-22 13:01:46 +02:00
else
2022-10-22 13:01:46 +02:00
# ASTRONAUT EXISTING PLAYER
CHECK=$(cat ~/.zen/game/players/$PLAYER/secret.june | grep -w "$SALT")
[[ $CHECK ]] && CHECK=$(cat ~/.zen/game/players/$PLAYER/secret.june | grep -w "$PEPPER")
[[ ! $CHECK ]] && echo "ERROR - CREDENTIALS NOT CORRESPONDING WITH PLAYER" && continue
2022-10-22 13:01:46 +02:00
mkdir -p ~/.zen/tmp/TW/
cp ~/.zen/game/players/$PLAYER/ipfs/moa/index.html ~/.zen/tmp/TW/index.html
fi
2022-10-07 02:50:28 +02:00
###################################################################################################
2022-10-22 13:01:46 +02:00
# VERIFICATION PAR EXTRACTION MOA
rm -f ~/.zen/tmp/tiddlers.json
tiddlywiki --load ~/.zen/tmp/TW/index.html --output ~/.zen/tmp --render '.' 'tiddlers.json' 'text/plain' '$:/core/templates/exporters/JsonFile' 'exportFilter' '[tag[moa]]'
TITLE=$(cat ~/.zen/tmp/tiddlers.json | jq -r '.[].title') # Dessin de PLAYER
PLAYER=$(echo $TITLE | rev | cut -f 1 -d ' ' | rev)
2022-10-10 18:06:37 +02:00
[[ ! $PLAYER ]] && echo "ERROR WRONG TW" && continue
echo "Bienvenue Astronaute $PLAYER. Nous avons capté votre TW"
echo "Redirection"
[[ $YOU ]] && TWLINK="http://$myIP:8080/ipns/$GNS" \
|| TWLINK="$LIBRA/ipns/$GNS"
echo "$TWLINK"
# Injection TWLINK dans template de redirection.
2022-10-22 15:47:54 +02:00
sed "s~_TWLINK_~$TWLINK~g" ~/.zen/Astroport.ONE/templates/index.redirect > ~/.zen/tmp/${MOATS}.index.redirect
2022-10-07 02:16:53 +02:00
2022-10-22 15:47:54 +02:00
## NOW ~/.zen/tmp/${MOATS}.index.redirect APPEARS. WAITING $PORT AVAILABLE THEN INJECT $TWLINK REDIRECT
2022-10-21 22:00:29 +02:00
while [[ $(ps auxf --sort=+utime | grep -w 'nc -l -p '${PORT} | grep -v -E 'color=auto|grep') ]]; do echo "sleeping...."; sleep 0.5; done
2022-10-22 13:01:46 +02:00
echo "ASTRONAUT REDIRECTION $TWLINK AVAILABLE on http://$myIP:${PORT}"
2022-10-21 18:22:16 +02:00
2022-10-22 15:47:54 +02:00
cat ~/.zen/tmp/${MOATS}.index.redirect | nc -l -p ${PORT} -q 1 &
###################################################################################################
2022-10-06 15:57:22 +02:00
end=`date +%s`
echo Execution time was `expr $end - $start` seconds.
fi
2022-10-06 15:57:22 +02:00
###################################################################################################
###################################################################################################
# API TWO : ?qrcode=G1PUB
if [[ ${arr[0]} == "qrcode" ]]; then
## Astroport.ONE local use QRCODE Contains PLAYER G1PUB
QRCODE=$(echo $URL | cut -d ' ' -f2 | cut -d '=' -f 2 | cut -d '&' -f 1) && echo "Instascan.html QR : $QRCODE"
g1pubpath=$(grep $QRCODE ~/.zen/game/players/*/.g1pub | cut -d ':' -f 1 2>/dev/null)
PLAYER=$(echo "$g1pubpath" | rev | cut -d '/' -f 2 | rev 2>/dev/null)
## FORCE LOCAL USE ONLY. Remove to open 1234 API
[[ ! -d ~/.zen/game/players/$PLAYER || $PLAYER == "" ]] && echo "AUCUN PLAYER !!" && continue
2022-10-06 15:57:22 +02:00
## UNE SECOND HTTP SERVER TO RECEIVE PASS
[[ ${arr[2]} == "" ]] && continue
2022-10-07 02:16:53 +02:00
fi
2022-10-06 15:57:22 +02:00
###################################################################################################
###################################################################################################
# API THREE : ?qrcode=G1PUB&url=HTTPLINK
## Demande de copie d'une URL reçue.
if [[ ${arr[0]} == "qrcode" && ${arr[2]} == "url" ]]; then
wsource="${arr[3]}"
[[ ${arr[4]} == "type" ]] && wtype="${arr[5]}" || wtype="Youtube"
## LANCEMENT COPIE
~/.zen/Astropor.ONE/ajouter_video.sh "$(urldecode $wsource)" "$wtype" "$QRCODE" &
echo "$QRCODE $wsource"
fi
## ENVOYER MESSAGE GCHANGE POUR QRCODE
## Une seule boucle !!!
[[ "$1" == "ONE" ]] && exit 0
2022-10-21 18:05:44 +02:00
2022-10-22 15:47:54 +02:00
rm ~/.zen/tmp/${MOATS}.myIP.http
2022-10-21 18:05:44 +02:00
HOMEPAGE=""
done
exit 0