Astroport.ONE/_12345.sh

340 lines
14 KiB
Bash
Raw Permalink Normal View History

2022-11-02 13:31:39 +01:00
#!/bin/bash
################################################################################
# Author: Fred (support@qo-op.com)
# Version: 0.2
2022-11-02 13:31:39 +01:00
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
################################################################################
# PUBLISH AND SYNC ASTROPORT STATIONS SWARM MAPS
2022-11-02 13:31:39 +01:00
# This script scan Swarm API layer from official bootstraps
#
2022-12-27 19:35:43 +01:00
MY_PATH="`dirname \"$0\"`" # relative
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
. "${MY_PATH}/tools/my.sh"
2022-11-02 14:06:26 +01:00
## SEND LOG TO ~/.zen/tmp/_12345.log
2022-12-30 22:55:10 +01:00
exec 2>&1 >> ~/.zen/tmp/_12345.log
2022-11-02 14:06:26 +01:00
PORT=12345
2022-12-27 19:35:43 +01:00
YOU=$(myIpfsApi); ## API of $USER running ipfs
2022-11-02 14:06:26 +01:00
LIBRA=$(head -n 2 ~/.zen/Astroport.ONE/A_boostrap_nodes.txt | tail -n 1 | cut -d ' ' -f 2) ## SWARM#0 ENTRANCE URL
## KILLING OLD DAEMON OF MYSELF
ncrunning=$(ps axf --sort=+utime | grep -w 'nc -l -p 12345' | grep -v -E 'color=auto|grep' | tail -n 1 | cut -d " " -f 2)
2023-04-17 10:36:35 +02:00
[[ $ncrunning != "" ]] && echo "(≖‿‿≖) - KILLING Already Running MAP Server - (≖‿‿≖) " && kill -9 $ncrunning
2022-11-02 13:31:39 +01:00
## WHAT IS NODEG1PUB
NODEG1PUB=$($MY_PATH/tools/ipfs_to_g1.py ${IPFSNODEID})
##############################################
2023-09-06 14:08:20 +02:00
[[ ${IPFSNODEID} == "" || ${IPFSNODEID} == "null" ]] && echo "IPFSNODEID is empty" && exit 1
mkdir -p ~/.zen/tmp/swarm
mkdir -p ~/.zen/tmp/${IPFSNODEID}
## AVOID A swarm IN swarm LOOP !!!
2023-04-17 09:29:20 +02:00
rm -Rf ~/.zen/tmp/${IPFSNODEID}/swarm
2023-04-28 16:34:59 +02:00
## TIMESTAMPING
2023-01-08 16:33:07 +01:00
MOATS=$(date -u +"%Y%m%d%H%M%S%4N")
2023-09-12 21:43:25 +02:00
echo "${MOATS}" > ~/.zen/tmp/${IPFSNODEID}/_MySwarm.moats
echo "$(date -u)" > ~/.zen/tmp/${IPFSNODEID}/_MySwarm.staom
2023-04-28 16:34:59 +02:00
############################################################
## MySwarm KEY INIT & SET
############################################################
## CREATE CHAN = MySwarm_${IPFSNODEID}
CHAN=$(ipfs key list -l | grep -w "MySwarm_${IPFSNODEID}" | cut -d ' ' -f 1)
2023-04-28 16:34:59 +02:00
#######################################################
## CREATE MySwarm KEYS ?
if [[ ${CHAN} == "" || ${CHAN} == "null" ]]; then
echo "## MAKE /proc/cpuinfo IPFSNODEID DERIVATED KEY ##"
SECRET1=$(cat /proc/cpuinfo | grep -Ev MHz | sha512sum | cut -d ' ' -f 1)
SECRET2=${IPFSNODEID}
echo "SALT=$SECRET1 && PEPPER=$SECRET2" > ~/.zen/game/myswarm_secret.june
2023-04-28 16:34:59 +02:00
${MY_PATH}/tools/keygen -t ipfs -o ~/.zen/game/myswarm_secret.ipfskey "$SECRET1" "$SECRET2"
${MY_PATH}/tools/keygen -t duniter -o ~/.zen/game/myswarm_secret.dunikey "$SECRET1" "$SECRET2"
ipfs key import "MySwarm_${IPFSNODEID}" -f pem-pkcs8-cleartext ~/.zen/game/myswarm_secret.ipfskey
CHAN=$(ipfs key list -l | grep -w "MySwarm_${IPFSNODEID}" | cut -d ' ' -f 1 )
fi
######################################################## MAKE IPFS NODE CHAN ID CPU RELATED
2023-04-28 16:34:59 +02:00
## PUBLISH CHANNEL IPNS
2023-10-08 14:42:48 +02:00
echo "<meta http-equiv=\"refresh\" content=\"0; url='/ipns/${CHAN}'\" />" > ~/.zen/tmp/${IPFSNODEID}/_MySwarm.$(myHostName).html
2023-09-12 22:19:25 +02:00
2023-04-28 16:34:59 +02:00
############################################################
############################################################
2023-04-29 03:20:43 +02:00
echo 0 > ~/.zen/tmp/random.sleep
###################
# NEVER ENDING LOOP
###################################################################
## WILL SCAN ALL BOOSTRAP - REFRESH "SELF IPNS BALISE" - RECEIVE UPLINK ORDERS
###################################################################
while true; do
2022-11-05 17:51:10 +01:00
start=`date +%s`
MOATS=$(date -u +"%Y%m%d%H%M%S%4N")
2022-11-03 20:16:18 +01:00
2023-09-12 21:43:25 +02:00
lastrun=$(cat ~/.zen/tmp/${IPFSNODEID}/_MySwarm.moats)
2022-11-03 22:56:19 +01:00
duree=$(expr ${MOATS} - $lastrun)
2023-04-28 16:34:59 +02:00
## FIXING TIC TAC FOR NODE & SWARM REFRESH ( 1H )
2023-10-14 17:09:07 +02:00
if [[ ${duree} -gt 3600000 ]]; then
2022-11-05 17:51:10 +01:00
2024-02-21 13:59:28 +01:00
${MY_PATH}/ping_bootstrap.sh
2024-02-21 13:54:34 +01:00
#### UPLANET FLASHMEM UPDATES
2024-02-25 19:38:48 +01:00
${MY_PATH}/RUNTIME/UPlanetKEYS_refresh.sh &
2024-02-21 13:54:34 +01:00
PLAYERONE=($(ls -t ~/.zen/game/players/ | grep "@" 2>/dev/null))
[[ ${PLAYERONE[@]} == "" ]] && echo "EMPTY ASTROPORT - NO PLAYER - NO PUBLISHING" && continue
2023-04-28 16:34:59 +02:00
#####################################
2024-02-21 13:54:34 +01:00
( ##### SUB-PROCESS £
start=`date +%s`
2023-11-14 19:16:00 +01:00
# MONITOR pending
for player in ${PLAYERONE[@]}; do
g1pub=$(cat ~/.zen/game/players/${player}/.g1pub 2>/dev/null)
# Check Station PLAYER payments
PENDINGS=($(ls "$HOME/.zen/game/pending/${g1pub}/*.TX"))
for pending in "${PENDINGS[@]}"; do
echo ${pending}
# TODO TREAT PENDINGS
#
done
done
############# GET BOOSTRAP SWARM DATA
for bootnode in $(cat ~/.zen/Astroport.ONE/A_boostrap_nodes.txt | grep -Ev "#" | grep -v '^[[:space:]]*$') # remove comments and empty lines
do
2022-11-05 17:51:10 +01:00
2024-02-05 12:41:26 +01:00
## ex: /ip4/149.102.158.67/tcp/4001/p2p/12D3KooWL2FcDJ41U9SyLuvDmA5qGzyoaj2RoEHiJPpCvY8jvx9u)
echo "############# RUN LOOP ######### $(date)"
ipfsnodeid=${bootnode##*/}
2023-04-17 10:36:35 +02:00
[[ ${ipfsnodeid} == ${IPFSNODEID} ]] && echo "MYSELF : ${IPFSNODEID} - CONTINUE" && continue
2023-09-10 16:45:11 +02:00
[[ ${ipfsnodeid} == "null" || ${ipfsnodeid} == "" ]] && echo "BAD ${IPFSNODEID} - CONTINUE" && continue
2023-11-13 00:10:23 +01:00
## SWARM CONNECT
ipfs swarm connect ${bootnode}
2023-09-12 19:01:42 +02:00
## PREPARE TO REFRESH SWARM LOCAL CACHE
mkdir -p ~/.zen/tmp/swarm/${ipfsnodeid}
2024-01-11 00:18:46 +01:00
mkdir -p ~/.zen/tmp/-${ipfsnodeid}
2022-11-02 14:06:26 +01:00
2023-04-28 16:34:59 +02:00
## GET bootnode IP
iptype=$(echo ${bootnode} | cut -d '/' -f 2)
nodeip=$(echo ${bootnode} | cut -d '/' -f 3)
2022-11-05 17:51:10 +01:00
2023-04-28 16:34:59 +02:00
## IPFS GET TO /swarm/${ipfsnodeid}
echo "GETTING ${nodeip} : /ipns/${ipfsnodeid}"
2024-01-11 00:18:46 +01:00
ipfs --timeout 720s get -o ~/.zen/tmp/-${ipfsnodeid}/ /ipns/${ipfsnodeid}/
2023-04-28 16:34:59 +02:00
## SHOW WHAT WE GET
echo "__________________________________________________"
2024-01-11 00:18:46 +01:00
ls ~/.zen/tmp/-${ipfsnodeid}/
2023-04-28 16:34:59 +02:00
echo "__________________________________________________"
2023-09-12 19:01:42 +02:00
## LOCAL CACHE SWITCH WITH LATEST
2024-01-11 00:18:46 +01:00
if [[ -s ~/.zen/tmp/-${ipfsnodeid}/_MySwarm.moats ]]; then
if [[ $(diff ~/.zen/tmp/-${ipfsnodeid}/_MySwarm.moats ~/.zen/tmp/swarm/${ipfsnodeid}/_MySwarm.moats) || $(cat ~/.zen/tmp/swarm/${ipfsnodeid}/_MySwarm.moats 2>/dev/null) == "" ]]; then
2023-09-12 19:01:42 +02:00
rm -Rf ~/.zen/tmp/swarm/${ipfsnodeid}
2024-01-11 00:18:46 +01:00
mv ~/.zen/tmp/-${ipfsnodeid} ~/.zen/tmp/swarm/${ipfsnodeid}
2023-12-07 01:06:17 +01:00
echo "UPDATED : ~/.zen/tmp/swarm/${ipfsnodeid}"
2023-09-12 19:01:42 +02:00
else
echo "TimeStamp unchanged : $(cat ~/.zen/tmp/swarm/${ipfsnodeid}/_MySwarm.moats)"
2024-01-11 00:18:46 +01:00
rm -Rf ~/.zen/tmp/-${ipfsnodeid}/
2023-09-12 19:01:42 +02:00
continue
fi
else
echo "UNREACHABLE /ipns/${ipfsnodeid}/"
2023-09-12 19:01:42 +02:00
continue
fi
## ASK BOOSTRAP NODE TO GET MY MAP UPSYNC
## - MAKES MY BALISE PRESENT IN BOOSTRAP SWARM KEY -
if [[ $iptype == "ip4" || $iptype == "ip6" ]]; then
2023-10-08 14:42:48 +02:00
############ UPSYNC CALL
2023-04-28 16:34:59 +02:00
echo "STATION MAP UPSYNC : curl -s http://${nodeip}:12345/?${NODEG1PUB}=${IPFSNODEID}"
curl -s -m 10 http://${nodeip}:12345/?${NODEG1PUB}=${IPFSNODEID} -o ~/.zen/tmp/swarm/${ipfsnodeid}/map.${nodeip}.json
## LOOKING IF ITS SWARM MAP COULD COMPLETE MINE
echo "ANALYSING BOOSTRAP SWARM MAP"
2023-10-09 22:39:10 +02:00
itipnswarmap=$(cat ~/.zen/tmp/swarm/${ipfsnodeid}/map.${nodeip}.json | jq -r '.g1swarm' | rev | cut -d '/' -f 1 | rev )
2023-04-30 15:24:29 +02:00
ipfs ls /ipns/${itipnswarmap} | rev | cut -d ' ' -f 1 | rev | cut -d '/' -f 1 > ~/.zen/tmp/_swarm.${ipfsnodeid}
2024-01-11 00:18:46 +01:00
echo "================ ${nodeip}:12345 ZNODS LIST"
cat ~/.zen/tmp/_swarm.${ipfsnodeid}
echo "============================================"
for znod in $(cat ~/.zen/tmp/_swarm.${ipfsnodeid}); do
2023-04-29 20:03:54 +02:00
# CHECK znod validity
cznod=$(${MY_PATH}/tools/ipfs_to_g1.py ${znod} 2>/dev/null)
[[ ${cznod} == "" || ${cznod} == "null" ]] && echo "xxxxxxxxxxxx BAD ${znod} xxxx ON xxxxxx ${ipfsnodeid} - ERROR - CONTINUE" && continue
2023-04-29 20:03:54 +02:00
2023-09-10 17:43:53 +02:00
if [[ ! -d ~/.zen/tmp/swarm/${znod} ]]; then
echo "COMPLETING MY SWARM DATA WITH ZNOD=${znod}"
2023-05-01 03:50:19 +02:00
mkdir -p ~/.zen/tmp/swarm/${znod}
2023-04-28 22:27:13 +02:00
ipfs --timeout 180s get -o ~/.zen/tmp/swarm/${znod} /ipns/${znod}
else
echo "____________ KNOW ${znod}"
2024-02-25 14:39:37 +01:00
# TODO : SPEEDUP REFRESH COMPARE _MySwarm.moats AND KEEP LATEST
fi
2023-12-07 01:06:17 +01:00
2023-12-07 15:09:54 +01:00
ZMOATS=$(cat ~/.zen/tmp/swarm/${znod}/_MySwarm.moats 2>/dev/null)
2023-12-07 01:06:17 +01:00
MOATS_SECONDS=$(${MY_PATH}/tools/MOATS2seconds.sh ${MOATS})
ZMOATS_SECONDS=$(${MY_PATH}/tools/MOATS2seconds.sh ${ZMOATS})
DIFF_SECONDS=$((MOATS_SECONDS - ZMOATS_SECONDS))
if [ ${DIFF_SECONDS} -gt $(( 3 * 24 * 60 * 60 )) ]; then
2024-02-25 14:39:37 +01:00
echo "STATION IS STUCK... FOR TOO LONG... REMOVING ${znod} FROM SWARM"
2023-12-07 01:06:17 +01:00
rm -Rf ~/.zen/tmp/swarm/${znod}/
2023-12-07 15:09:54 +01:00
else
echo "${DIFF_SECONDS} seconds old"
2023-12-07 01:06:17 +01:00
fi
done
echo "============================================"
fi ## IP4 WAN BOOTSRAP UPSYNC FINISHED
done
2022-11-02 14:06:26 +01:00
#############################################
2023-04-28 22:27:13 +02:00
# ERASE EMPTY DIRECTORIES
du -b ~/.zen/tmp/swarm > /tmp/du
while read branch; do [[ $branch =~ "4096" ]] && echo "empty $branch" && rm -Rf $(echo $branch | cut -f 2 -d ' '); done < /tmp/du
############### UPDATE MySwarm CHAN
ls ~/.zen/tmp/swarm
SWARMSIZE=$(du -b ~/.zen/tmp/swarm | tail -n 1 | cut -f 1)
2022-11-02 14:06:26 +01:00
## SIZE MODIFIED => PUBLISH MySwarm_${IPFSNODEID}
2023-09-12 21:43:25 +02:00
[[ ${SWARMSIZE} != $(cat ~/.zen/tmp/swarm/.bsize 2>/dev/null) ]] \
&& echo ${SWARMSIZE} > ~/.zen/tmp/swarm/.bsize \
&& SWARMH=$(ipfs add -rwq ~/.zen/tmp/swarm/* | tail -n 1 ) \
2023-04-28 16:34:59 +02:00
&& echo "=== ~/.zen/tmp/swarm EVOLVED : PUBLISHING NEW STATE ===" \
2023-09-12 20:16:21 +02:00
&& ipfs name publish --key "MySwarm_${IPFSNODEID}" /ipfs/${SWARMH}
#############################################
2022-11-03 20:16:18 +01:00
2023-04-28 16:34:59 +02:00
######################################
############# RE PUBLISH SELF BALISE
2024-02-25 14:39:37 +01:00
# Clean Empty Directory
2023-04-17 02:01:40 +02:00
du -b ~/.zen/tmp/${IPFSNODEID} > /tmp/du
2023-04-28 22:27:13 +02:00
while read branch; do [[ $branch =~ "4096" ]] && echo "empty $branch" && rm -Rf $(echo $branch | cut -f 2 -d ' '); done < /tmp/du
2022-11-19 19:07:27 +01:00
2024-02-25 14:39:37 +01:00
# Scan IPFSNODEID cache
ls ~/.zen/tmp/${IPFSNODEID}/
BSIZE=$(du -b ~/.zen/tmp/${IPFSNODEID} | tail -n 1 | cut -f 1)
2024-02-25 14:39:37 +01:00
## IPFS GET LAST ONLINE IPFSNODEID MAP
rm -Rf ~/.zen/tmp/_${IPFSNODEID} 2>/dev/null
2023-04-28 16:34:59 +02:00
mkdir -p ~/.zen/tmp/_${IPFSNODEID}
2023-09-12 22:03:28 +02:00
ipfs get -o ~/.zen/tmp/_${IPFSNODEID}/ /ipns/${IPFSNODEID}/
2023-04-28 16:34:59 +02:00
NSIZE=$(du -b ~/.zen/tmp/_${IPFSNODEID} | tail -n 1 | cut -f 1)
2022-11-19 19:07:27 +01:00
2023-04-28 16:34:59 +02:00
### CHECK IF SIZE DIFFERENCE ?
## Local / IPNS size differ => FUSION LOCAL OVER ONLINE & PUBLISH
[[ ${BSIZE} != ${NSIZE} ]] \
2023-04-28 22:27:13 +02:00
&& echo "${MOATS}" > ~/.zen/tmp/${IPFSNODEID}/_MySwarm.moats \
2023-04-28 16:34:59 +02:00
&& MYCACHE=$(ipfs add -rwq ~/.zen/tmp/${IPFSNODEID}/* | tail -n 1 ) \
&& echo "PUBLISHING NEW BALISE STATE FOR STATION /ipns/${IPFSNODEID} INDEXES = $BSIZE octets" \
2023-09-12 20:16:21 +02:00
&& ipfs name publish /ipfs/${MYCACHE}
end=`date +%s`
2023-04-28 22:27:13 +02:00
echo "(*__*) MySwam Update ($BSIZE B) duration was "`expr $end - $start`' seconds. '$(date)
2022-11-03 22:56:19 +01:00
2022-12-23 15:36:57 +01:00
) & ##### SUB-PROCESS
2022-11-03 20:16:18 +01:00
# last run recording
2023-09-12 21:43:25 +02:00
echo "${MOATS}" > ~/.zen/tmp/${IPFSNODEID}/_MySwarm.moats
echo "$(date -u)" > ~/.zen/tmp/${IPFSNODEID}/_MySwarm.staom
2022-11-03 22:56:19 +01:00
else
2023-04-28 16:34:59 +02:00
echo "#######################"
echo "NOT SO QUICK"
2022-11-03 22:56:19 +01:00
echo "$duree only cache life"
echo "#######################"
2022-11-03 22:56:19 +01:00
fi
2023-04-28 16:34:59 +02:00
############ PREPARE HTTP 12345 JSON DOCUMENT
2022-11-05 17:51:10 +01:00
HTTPSEND="HTTP/1.1 200 OK
2022-11-02 15:12:13 +01:00
Access-Control-Allow-Origin: \*
2022-11-02 14:06:26 +01:00
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET
Server: Astroport.ONE
Content-Type: application/json; charset=UTF-8
{
2022-11-02 15:30:43 +01:00
\"created\" : \"${MOATS}\",
2022-12-27 19:35:43 +01:00
\"hostname\" : \"$(myHostName)\",
2022-11-02 15:34:44 +01:00
\"myIP\" : \"${myIP}\",
\"ipfsnodeid\" : \"${IPFSNODEID}\",
2023-04-05 19:17:26 +02:00
\"astroport\" : \"http://${myIP}:1234\",
\"g1station\" : \"${myIPFS}/ipns/${IPFSNODEID}\",
\"g1swarm\" : \"${myIPFS}/ipns/${CHAN}\"
2022-11-02 14:06:26 +01:00
}
"
2022-11-05 17:51:10 +01:00
######################################################################################
2023-04-28 16:34:59 +02:00
# WAIT FOR REQUEST ON PORT12345 (netcat is waiting)
[[ ! -s ~/.zen/tmp/random.sleep ]] \
&& T2WAIT=$((3600-${RANDOM:0:3})) \
|| T2WAIT=$(cat ~/.zen/tmp/random.sleep)
2023-04-30 15:24:29 +02:00
if [[ $T2WAIT == 0 || $T2WAIT != $(cat ~/.zen/tmp/random.sleep 2>/dev/null) ]]; then
(
echo "# AUTO RELAUNCH IN $T2WAIT SECONDS"
echo $T2WAIT > ~/.zen/tmp/random.sleep
sleep $T2WAIT && rm ~/.zen/tmp/random.sleep
curl -s "http://127.0.0.1:12345"
) & ## AUTO RELAUNCH IN ABOUT AN HOUR : DESYNC SWARM REFRESHINGS
fi
######################################################################################
echo '(◕‿‿◕) http://'$myIP:'12345 READY (◕‿‿◕)'
REQ=$(echo "$HTTPSEND" | nc -l -p 12345 -q 1) ## # WAIT FOR 12345 PORT CONTACT
2023-04-28 16:34:59 +02:00
######################################################################################
######################################################################################
######################################################################################
## VISIT RECEIVED
URL=$(echo "$REQ" | grep '^GET' | cut -d ' ' -f2 | cut -d '?' -f2)
HOSTP=$(echo "$REQ" | grep '^Host:' | cut -d ' ' -f2 | cut -d '?' -f2)
HOST=$(echo "$HOSTP" | cut -d ':' -f 1)
COOKIE=$(echo "$REQ" | grep '^Cookie:' | cut -d ' ' -f2)
echo "RECEPTION : $URL"
arr=(${URL//[=&]/ })
#####################################################################
### UPSYNC STATION REQUEST :12345/?G1PUB=g1_to_ipfs(G1PUB)&...
2023-04-28 16:34:59 +02:00
### TODO : include CODE HASH & TOKEN ....
#####################################################################
if [[ ${arr[0]} != "" ]]; then
2023-04-28 16:34:59 +02:00
## CHECK URL CONSISTENCY ( do we get G1PUB=IPNSPUB right ? )
GPUB=${arr[0]}
ASTROTOIPFS=$(${MY_PATH}/tools/g1_to_ipfs.py ${arr[0]} 2>/dev/null)
2023-04-28 16:34:59 +02:00
2023-04-30 15:24:29 +02:00
if [[ "${ASTROTOIPFS}" == "${arr[1]}" && ${ASTROTOIPFS} != "" && ${arr[1]} != "" ]]; then
2023-04-28 16:34:59 +02:00
## WE SPEAK THE SAME PROTOCOL
echo "WE HAVE A STATION ${GPUB} CONTACT"
(
2023-04-18 15:44:02 +02:00
mkdir -p ~/.zen/tmp/swarm/${ASTROTOIPFS}
echo "<<< MAJOR TOM TO GROUND CONTROL >>>"
2023-04-18 15:44:02 +02:00
echo "UPSYNC TO ~/.zen/tmp/swarm/${ASTROTOIPFS}"
ipfs --timeout 180s get -o ~/.zen/tmp/swarm/${ASTROTOIPFS} /ipns/${ASTROTOIPFS}
) &
fi
fi
2022-11-05 17:51:10 +01:00
#### 12345 NETWORK MAP TOKEN
end=`date +%s`
echo '(#__#) WAITING TIME was '`expr $end - $start`' seconds.'
2022-11-03 22:56:19 +01:00
echo '(^‿‿^) 12345 TOKEN '${MOATS}' CONSUMED (^‿‿^)'
done
2022-11-02 14:06:26 +01:00
exit 0