You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

220 lines
11 KiB

#!/bin/bash
########################################################################
# Author: Fred (support@qo-op.com)
# Version: 2020.12.03
# 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##*/}"
countMErunning=$(ps auxf --sort=+utime | grep -w $ME | grep -v -E 'color=auto|grep' | wc -l)
[[ $countMErunning -gt 2 ]] && echo "$ME already running $countMErunning time" && exit 0
start=`date +%s`
YOU=$(ps auxf --sort=+utime | grep -w ipfs | grep -v -E 'color=auto|grep' | tail -n 1 | cut -d " " -f 1) && [[ ! $YOU ]] && echo "ipfs NOT RUNNING. EXIT" && exit 1
G1PUB=$(cat ~/.zen/secret.dunikey | grep 'pub:' | cut -d ' ' -f 2) && [[ ! $G1PUB ]] && echo "ERREUR G1PUB. EXIT" && exit 1
IPFSNODEID=$(cat ~/.ipfs/config | jq -r .Identity.PeerID)
[[ ! $IPFSNODEID ]] && echo 'ERROR missing IPFS Node id !! IPFS is not installed !?' && exit 1
# CLEAN "A_my_swarm.txt" and "A_swarm_map.txt"
if [[ $(cat ~/.zen/A_swarm_map.txt | wc -l) -gt 300 ]]; then
rm -f ~/.zen/A_my_swarm.txt && touch ~/.zen/A_my_swarm.txt
rm -f ~/.zen/A_swarm_map.txt && touch ~/.zen/A_swarm_map.txt
fi
########################################################################
# Force Strict #swarm0 !!?
rm -f /tmp/ipfstryme.*
rm -f ~/.zen/A_Qm_swarm.txt
liking_me_count=0
friend_of_mine_count=0
echo $(date)
########################################################################
# Let's look for Friends and "IPFS swarm connect" with them
########################################################################
rm -f /tmp/liking_me ## FILE WILL BE REFRESHED
## REFRESH IPFS SWARM FROM AVAILABLE FRIENDS
for g1friend in $(find ~/.zen/ipfs/.$IPFSNODEID/FRIENDS/ -type d | rev | cut -d '/' -f 1 | rev);
do
ipfsnodeid=$(~/.zen/astrXbian/zen/tools/g1_to_ipfs.py $g1friend)
echo "### $g1friend ipfs reading XID"
XID=$(ipfs --timeout=20s cat /ipns/$ipfsnodeid/.$ipfsnodeid/_xbian.zuid)
if [[ $XID ]]; then
echo "### REFRESH ipfs_swarm $ipfsnodeid ($XID)"
ipfs --timeout=360s get --output=/home/$YOU/.zen/ipfs_swarm/ /ipns/$ipfsnodeid
fi
done
# Check liking_me station in my swarm FRIENDS
for swarm_frof in $(ls ~/.zen/ipfs_swarm/.*/FRIENDS/$G1PUB/stars.level | cut -d '/' -f 6 | cut -d '.' -f 2); ## FROM IPFS
do
liking_me=$(~/.zen/astrXbian/zen/tools/ipfs_to_g1.py $swarm_frof)
## PUBLISH /tmp/liking_me for ./tools/add_externIP_to_ipfstryme.sh
echo "$liking_me" >> /tmp/liking_me
do_I_like=$(ls ~/.zen/ipfs/.$IPFSNODEID/FRIENDS/$liking_me 2>/dev/null)
# Get Gchange "liking_me" people list (made by gchange_INIT.sh)
# [[ ! -f /tmp/liking_me ]] && ~/.zen/astrXbian/zen/tools/timeout.sh -t 12 ~/.zen/astrXbian/zen/jaklis/jaklis.py -k ~/.zen/secret.dunikey -n "https://data.gchange.fr" stars | jq -r '.likes[].issuer' > /tmp/liking_me.try && [ ! -s /tmp/liking_me.try ] && cp /tmp/liking_me.try /tmp/liking_me && rm /tmp/liking_me.try
#for liking_me in $(cat /tmp/liking_me | sort | uniq); ## FROM GCHANGE
#do
[[ "$liking_me" == "" || "$liking_me" == "$G1PUB" ]] && continue
# DE TOUS CEUX QUI M'AIMENT JE VAIS LIRE LES MESSAGES pour collecter leurs ipfstryme vers ~/.zen/A_swarm_map.txt
# SI JE SUIS LEUR AMI EN RETOUR, je conserve leur ID dans ~/.zen/A_my_swarm.txt
# Check if my like is reverse
liking_me_count=$((liking_me_count+1))
echo ">>>>>> CHECKING liking_me n° $liking_me_count "
## IPFS WAY TO DO IT (LESS STRESS ON GCHANGE)
rm /tmp/ipfstryme.$liking_me 2>/dev/null
ipfsnodeid=$(~/.zen/astrXbian/zen/tools/g1_to_ipfs.py $liking_me)
echo "Getting /ipns/$ipfsnodeid/.$ipfsnodeid/tryme.addr ..."
ipfs --timeout=30s cat /ipns/$ipfsnodeid/.$ipfsnodeid/tryme.addr > /tmp/ipfstryme.$liking_me
if [[ ! -f /tmp/ipfstryme.$liking_me ]]; then
echo "TIMEOUT : READING 'ipfstryme' message from $liking_me" ## GCHANGE ACCESS
## Get "/tmp/ipfstryme.$liking_me" from Gchange "ipfstryme" message
~/.zen/astrXbian/zen/tools/timeout.sh -t 12 ~/.zen/astrXbian/zen/jaklis/jaklis.py -k ~/.zen/secret.dunikey -n "https://data.gchange.fr" read -n300 -j | jq -r --arg friendKEY "$liking_me" '.[] | select(.pubkey == $friendKEY)' | jq 'select(.title == "ipfstryme")' | jq -r '.content' > /tmp/ipfstryme.$liking_me
fi
## ZOMBIE NODE sends more that 300 ipfstryme ?
## [[ $(cat /tmp/ipfstryme.$liking_me | sort | uniq | wc -l) -gt 300 ]] && echo "THIS NODE IS A ZOMBIE..." && rm -f /tmp/ipfstryme.$liking_me && continue
# CLEAN /tmp/ipfstryme.$liking_me
> /tmp/ipfstryme.clean
for line in $(cat /tmp/ipfstryme.$liking_me | shuf);
do
ipfsnodeid=$(echo $line | rev | cut -d '/' -f 1 | rev)
stationip=$(echo $line | cut -d '/' -f 3)
ping -c 1 -W 1 $stationip 2>&1>/dev/null && sping="OK" || sping="NOK"
ipfs swarm connect $line 2>&1>/dev/null && swarmping="OK" || swarmping="NOK"
echo "## ping $stationip : $sping / STATION $ipfsnodeid ($swarmping)"
[[ $ipfsnodeid == "" ]] && break
[[ ! $(cat /tmp/ipfstryme.clean | grep $ipfsnodeid) ]] && [[ "$swarmping" == "OK" ]] && echo $line >> /tmp/ipfstryme.clean
done
cat /tmp/ipfstryme.clean | sort | uniq > /tmp/ipfstryme.$liking_me
cat /tmp/ipfstryme.$liking_me
echo "__________________________________________"
done
for peerline in $(cat /tmp/ipfstryme.* | sort | uniq);
do
echo "======================================================"
# GET $ipfsnodeid FROM $peerline
echo "PEERLINE: $peerline"
ipfsnodeid=${peerline##*/} # http://mywiki.wooledge.org/BashFAQ/100
[[ "$ipfsnodeid" == "" ]] && continue
# GET $whoisg1 FROM $peerline
## Remove Qm ipfs nodes
[[ "${ipfsnodeid:0:2}" != "Qm" ]] && whoisg1=$(~/.zen/astrXbian/zen/tools/ipfs_to_g1.py $ipfsnodeid)
[[ ! $whoisg1 ]] && continue
# KEEPING LAST ipfstryme MESSAGES only (CAN BE REMOVED SOON WHEN FULL IPFS)
nbmessage=0
for messageid in $(~/.zen/astrXbian/zen/tools/timeout.sh -t 12 ~/.zen/astrXbian/zen/jaklis/jaklis.py -k ~/.zen/secret.dunikey -n "https://data.gchange.fr" read -n300 -j | jq -r --arg friendKEY "$whoisg1" '.[] | select(.pubkey == $friendKEY)' | jq 'select(.title == "ipfstryme")' | jq -r '.id')
do
nbmessage=$((nbmessage+1))
[ $nbmessage -gt 1 ] && echo "Delete $nbmessage OLD 'ipfstryme' messages from $whoisg1" && ~/.zen/astrXbian/zen/tools/timeout.sh -t 12 ~/.zen/astrXbian/zen/jaklis/jaklis.py -k ~/.zen/secret.dunikey -n "https://data.gchange.fr" delete -i $messageid
sleep 0.5
done
[[ $nbmessage == 0 ]] && echo "NO ipfstryme MESSAGE from $whoisg1" && echo "$ipfsnodeid" >> ~/.zen/A_dead_swarm.txt && continue
# SEND MESSAGE ~/.zen/astrXbian/zen/jaklis/jaklis.py -k ~/.zen/secret.dunikey -n "https://data.gchange.fr" send -d $whoisg1 -t 'Astroport' -m 'Aucun message de ta part https://copylaradio.com'"
## CHECKING STARS FROM IPNS
starsin=$(ipfs --timeout=10s cat /ipns/$ipfsnodeid/.$ipfsnodeid/FRIENDS/$G1PUB/stars.level || echo 0)
starsout=$(ipfs --timeout=10s cat /ipns/$IPFSNODEID/.$IPFSNODEID/FRIENDS/$whoisg1/stars.level || echo 0)
# REFRESH A_swarm_map.txt
echo $peerline >> ~/.zen/A_swarm_map.txt
cat ~/.zen/A_swarm_map.txt | sort | uniq > /tmp/A_swarm_map.txt
cp -f /tmp/A_swarm_map.txt ~/.zen/A_swarm_map.txt
# FRIEND ADDRESS DISCOVERY
echo "Am I friend with my FRIEND friend $whoisg1 ?"
echo "starsin=$starsin * starsout=$starsout *"
# ipfs cat /ipns/$ipfsnodeid/.$ipfsnodeid/FRIENDS/$G1PUB/tryme.addr # IS my own /ipns/$IPFSNODEID/.$IPFSNODEID/tryme.addr
# GCHANGE : friend_of_mine=$(~/.zen/astrXbian/zen/tools/timeout.sh -t 12 ~/.zen/astrXbian/zen/jaklis/jaklis.py -k ~/.zen/secret.dunikey -n "https://data.gchange.fr" stars -p $whoisg1 | jq -r '.yours.pseudo'); # must contains my XZUID
## LOOKING IN SWARM CACHE
friend_of_mine=$(ls ~/.zen/ipfs/.$IPFSNODEID/FRIENDS/$whoisg1 2>/dev/null)
if [[ $friend_of_mine ]]; then
# COUNTING MY FRIENDS
friend_of_mine_count=$((friend_of_mine_count+1))
if [[ ! $(grep "$ipfsnodeid" ~/.zen/A_my_swarm.txt) ]]
then
echo "OK FRIEND"
echo "$peerline" >> ~/.zen/A_my_swarm.txt
# ipfs bootstrap add "$peerline"
## NOW WILL BE USING Peering.Peers config
else
echo "ALREADY RECORDED FRIEND"
fi
## Sync ipfs_swarm
ipfs --timeout=120s get --output=/home/$YOU/.zen/ipfs_swarm/ /ipns/$ipfsnodeid
else
## REMOVE FRIENDS DIRECTORY (should be removed from Astroport App)
echo "NOT FULL FRIEND $ipfsnodeid / $whoisg1"
[[ "$liking_me" != "" ]] && rm -Rf ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me && rm -Rf ~/.zen/ipfs_swarm/.$ipfsnodeid && echo "REMOVING SWARM SYNC"
# SEND ME A MESSAGE TO DECIDE WHAT TO DO ?
# SEND MESSAGE ~/.zen/astrXbian/zen/jaklis/jaklis.py -k ~/.zen/secret.dunikey -n "https://data.gchange.fr" send -d $whoisg1 -t 'Astroport' -m 'Rejoins mon #Swarm0 https://copylaradio.com'"
fi
done;
# UNIQ file
cat ~/.zen/A_my_swarm.txt | rev | sort | uniq | rev > /tmp/A_my_swarm.txt
cp -f /tmp/A_my_swarm.txt ~/.zen/A_my_swarm.txt
cat ~/.zen/A_Qm_swarm.txt | rev | sort | uniq | rev > /tmp/A_Qm_swarm.txt
cp -f /tmp/A_Qm_swarm.txt ~/.zen/A_Qm_swarm.txt
echo "####################################################
I have $friend_of_mine_count friends amoung $liking_me_count liking me people
__________________________________________
My actual #SWARM0"
# ipfs swarm peers
echo $(date)
echo "####################################################"
# ipfs_SWARM_refresh
[[ $(cat ~/.zen/A_my_swarm.txt | wc -l) -gt 0 ]] && ~/.zen/astrXbian/zen/ipfs_SWARM_refresh.sh && exit 0 || echo "SWARM LOST !! "
########################################################################
########################################################################
## ERROR !! SHOULD HAVE exit 0 before !!
########################################################################
## SWARM LOST !! EMERGENCY RECOVERY CODE !!!
########################################################################
ipfs bootstrap > /tmp/ipfs_bootstrap
## REBOOTSTRAP
[[ $(cat /tmp/ipfs_bootstrap | wc -l) -lt 2 ]] && ipfs bootstrap rm --all
for bootnode in $(cat ~/.zen/astrXbian/A_boostrap_nodes.txt | grep -Ev "#") # remove comments
do
ipfsnodeid=${bootnode##*/}
[[ "$ipfsnodeid" != "$IPFSNODEID" ]] && sleep 20 ## RALENTIR DEMARRAGE STATIONS NON BOOTSTRAP
## ADD $bootnode TO BOOTSTRAP
ipfs bootstrap add $bootnode
echo "$bootnode" >> ~/.zen/A_my_swarm.txt
done
## RECONNECT WITH FRIENDS
for g1id in $(ls ~/.zen/ipfs/.$IPFSNODEID/FRIENDS);
do
if [[ -d ~/.zen/ipfs/.$IPFSNODEID/FRIENDS/$g1id ]]; then
ipfsnodeid=$(~/.zen/astrXbian/zen/tools/g1_to_ipfs.py $g1id)
ipfs --timeout=10s swarm connect /ipfs/$ipfsnodeid
[[ $? == 0 ]] && ipfs swarm peers | grep $ipfsnodeid | tail -n 1 >> ~/.zen/A_my_swarm.txt
fi
done
# UNIQ file
cat ~/.zen/A_my_swarm.txt | sort | uniq > /tmp/A_my_swarm.txt
cp -f /tmp/A_my_swarm.txt ~/.zen/A_my_swarm.txt
cp -f ~/.zen/A_my_swarm.txt ~/.zen/A_swarm_map.txt
echo "#### EXCECUTION TIME"
end=`date +%s`
echo Execution time was `expr $end - $start` seconds.
echo "########################################################################"