astrXbian/zen/gchange_INIT.sh

281 lines
15 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
########################################################################
# Author: Fred (support@qo-op.com)
# Version: 0.2
# 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`
# Retrieve Gchange profil data
# Sends "ipfstryme" messages to liked linking_me NODES
# If NODE is from "A_boostrap_nodes.txt" then auto-linking
########################################################################
# INITIALISATION & CONTROL
########################################################################
YOU=$(ps auxf --sort=+utime | grep -w ipfs | grep -v -E 'color=auto|grep' | tail -n 1 | cut -d " " -f 1);
IPFSNODEID=$(ipfs --timeout=20s id -f='<id>\n')
[[ ! $IPFSNODEID ]] && echo 'ERROR missing IPFS Node id !! IPFS is not installed !?' && exit 1
########################################################################
[[ ! -f ~/.zen/secret.dunikey ]] && echo "Missing ~/.zen/secret.dunikey. EXIT" && exit 1
G1PUB=$(cat ~/.zen/secret.dunikey | grep 'pub:' | cut -d ' ' -f 2)
[[ $G1PUB == "" ]] && echo "ERROR G1PUB empty !! EXIT" && exit 1
[[ -f ~/.zen/ipfs/.$IPFSNODEID/_xbian.zuid ]] && XZUID=$(cat ~/.zen/ipfs/.$IPFSNODEID/_xbian.zuid) || hostname -s > ~/.zen/ipfs/.$IPFSNODEID/_xbian.zuid
########################################################################
# CREATNG IPFS LOCAL REPOSITORY for Node G1 Identity : G1SSB
mkdir -p ~/.zen/ipfs/.$IPFSNODEID/G1SSB
########################################################################
# CREATING MY tryme.addr
~/.zen/astrXbian/zen/tools/add_externIP_to_ipfstryme.sh
cat ~/.zen/ipfs/.${IPFSNODEID}/tryme.addr
########################################################################
# ADD GCHANGE+ informations
########################################################################
GCHANGE="https://data.gchange.fr"
echo"## GET LATEST GCHANGE PROFILE"
curl -s ${GCHANGE}/user/profile/${G1PUB} > ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.gchange.json
Gtitle=$(cat ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.gchange.json | jq -r '._source.title')
[[ $Gtitle ]] && echo "$Gtitle" > ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.gchange_title
echo"# ACTUAL _g1.gchange_title=$Gtitle"
# UPDATE _g1.astroport_title with city
Atitle="Station $YOU@$XZUID"
city=$(cat ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.gchange.json | jq -r '._source.city')
[[ "$city" != "null" ]] && Atitle="$Atitle in $city"
echo "$Atitle" > ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.astroport_title
# UPDATE _g1.gchange_avatar.png
cat ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.gchange.json | jq -r '._source.avatar._content' | base64 -d > "/home/$YOU/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.gchange_avatar.png"
# CREATE _nodename
[[ -f /home/$YOU/.zen/ipfs/.$IPFSNODEID/G1SSB/_nodename ]] && nodename=$(cat /home/$YOU/.zen/ipfs/.$IPFSNODEID/G1SSB/_nodename)
if [[ "$nodename" == "" ]]; then
nodename=$(hostname)
extension=$(echo $nodename | cut -d '.' -f 2)
if [[ "$extension" == "$nodename" ]]; then
nodename="$nodename.home"
fi
echo "$nodename" > ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_nodename
fi
########################################################################
# DUNITER G1 Wallet balance
export LC_ALL=C.UTF-8 #attipix
export LANG=C.UTF-8 #attipix
# _g1.pubkey
[[ ! ~/.zen/ipfs/.${IPFSNODEID}/G1SSB/_g1.pubkey ]] && echo "$G1PUB" > ~/.zen/ipfs/.${IPFSNODEID}/G1SSB/_g1.pubkey
# ipfsnodeid
[[ ! -f ~/.zen/ipfs/.${IPFSNODEID}/.ipfsnodeid ]] && echo ${IPFSNODEID} > ~/.zen/ipfs/.${IPFSNODEID}/.ipfsnodeid
########################################################################
# NODE DISK PERFORMANCE, manual remove ~/.ipfs/test.disk (every week in cron_MINUTE) then run again
if [[ ! -f ~/.ipfs/test.disk ]]; then
diskperf=$(dd if=/dev/zero of=~/.ipfs/test.disk bs=10M count=1 oflag=dsync 2>&1 | tail -n 1 | sed s/\,\ /\ -/g | cut -d '-' -f 4)
echo "DISK PERFORMANCE TESTING : $diskperf"
# NODE disk.perf
echo "$diskperf" > ~/.zen/ipfs/.${IPFSNODEID}/disk.perf
# NODE disk.bytes
echo $(df ~/.ipfs/ | tail -n 1 | awk '{print $4}') > ~/.zen/ipfs/.${IPFSNODEID}/disk.bytes
# IPFS repo.stat
ipfs repo stat > ~/.zen/ipfs/.${IPFSNODEID}/repo.stat
###########################################
# GEOLOC MUST BE SECURELY EXCHANGED (TODO)
###########################################
# CREATE "_g1.gchange_geoPoint.lat" AND "_g1.gchange_geoPoint.lon"
# rm -f ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.gchange_geoPoint.lat
# echo $(curl -s ${GCHANGE}/user/profile/${G1PUB} | jq '._source.geoPoint.lat') > ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.gchange_geoPoint.lat
# rm -f ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.gchange_geoPoint.lon
# echo $(curl -s ${GCHANGE}/user/profile/${G1PUB} | jq '._source.geoPoint.lon') > ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.gchange_geoPoint.lon
fi
########################################################################
## CREATE & CUSTOMIZE WIKI
########################################################################
echo "####################################################
Customisation TiddlyWiki"
[[ ! $(cat ~/.zen/ipfs/.${IPFSNODEID}/index.html | grep ${IPFSNODEID}) ]] && mkdir -p ~/.zen/ipfs/.${IPFSNODEID} && cp ~/.zen/astrXbian/.install/templates/tiddlyone/index.html ~/.zen/ipfs/.${IPFSNODEID}/index.html
# cp -f ~/.zen/astrXbian/.install/templates/tiddlyone/index.html ~/.zen/ipfs/.${IPFSNODEID}/index.html
zuid="$(cat ~/.zen/ipfs/.$IPFSNODEID/_xbian.zuid)"
wiki="/home/$YOU/.zen/ipfs/.$IPFSNODEID/index.html"
updated="$(date +"%m-%d-%Y_%H:%M")"
sed -i "s/GettingStarted/$zuid/g" $wiki ## (star) pseudo (score)
sed -i "s/Moa/$zuid/g" $wiki ## (star) pseudo (score)
GTITLE="$(cat ~/.zen/ipfs/.$IPFSNODEID/G1SSB/_g1.gchange_title | sed 's/\ /-/g' | sed 's/\./-/g' )"
sed -i "s/$zuid/$GTITLE/g" $wiki ## (star) pseudo (score)
sed -i "s/_IPFSNODEID_/$IPFSNODEID/g" $wiki
sed -i "s/_G1PUB_/$G1PUB/g" $wiki
sed -i "s/_UPDATED_/$updated/g" $wiki
## CREATE IPFSNODEID G1PUB QR CODE
[[ ! -f /home/$YOU/.zen/ipfs/.$IPFSNODEID/QR.png ]] && qrencode -s 6 -o "/home/$YOU/.zen/ipfs/.$IPFSNODEID/QR.png" "$G1PUB"
# sed -i "s/Personnalisez/<a href=\"http:\/\/127.0.0.1:8181\/ipns\/$IPFSNODEID\/.$IPFSNODEID\/FRIENDS\">Amis de/g" $wiki
# https:\/\/www.gchange.fr\/#\/app\/records\/wallet?q=$G1PUB
########################################################################
## CREATING www EXCHANGE ZONE .$IPFSNODEID/astroport/www
if [[ ! -d /home/$YOU/astroport/www ]]; then
wwwuser=$(ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1) || wwuser="www-data"
mkdir -p /home/$YOU/astroport/www
chown -R $YOU:$wwwuser /home/$YOU/astroport/www
chmod -R g+rw /home/$YOU/astroport/www
else
echo ".$IPFSNODEID/astroport/www exchange zone OK"
fi
########################################################################
###
#### FRIENDS && BOOTSTRAP ipfstryme MESSAGE sending procedure
########################################################################
# GET LIST of issuer(s) who likes me & A_boostrap_nodes.txt
IamINBOOTSTRAP=$(cat ~/.zen/astrXbian/A_boostrap_nodes.txt | grep "$IPFSNODEID") # AM I A BOOTSTRAP ?
ipfsnodeid=${IamINBOOTSTRAP##*/}
## FILTER Qm !
[[ "${ipfsnodeid:0:2}" != "Qm" && "${ipfsnodeid:0:2}" != "" ]] && IamBOOTSTRAP=$(~/.zen/astrXbian/zen/tools/ipfs_to_g1.py $ipfsnodeid) || IamBOOTSTRAP="" # Contains G1PUB
########################################################################
################## BOOTSTRAP AUTO PINNING NEW MEDIAKEYs
[[ $IamBOOTSTRAP ]] && ~/.zen/astrXbian/zen/tools/autoPINfriends.sh 2>&1 > /home/${YOU}/.zen/ipfs/.${IPFSNODEID}/.log.autoPINfriends.txt
## SYNC SWARM EVERY 20 mn runtime
################## AUTO PINNING
########################################################################
########################################################################
################## CHECKING WHO GAVE ME STARS
################## BOOTSTRAP LIKES THEM BACK
################## SEND ipfstryme MESSAGES to FRIENDS
rm -f /tmp/friend_of_mine
## Getting Gchange liking_me list
~/.zen/astrXbian/zen/tools/timeout.sh -t 20 ~/.zen/astrXbian/zen/jaklis/jaklis.py -k ~/.zen/secret.dunikey -n "https://data.gchange.fr" stars | jq -r '.likes[].issuer' > ~/.zen/ipfs/.$IPFSNODEID/FRIENDS/liking_me
## Adding random bootstrap to liking_me list
~/.zen/astrXbian/zen/tools/ipfs_to_g1.py $(cat ~/.zen/astrXbian/A_boostrap_nodes.txt | grep -Ev "#" | rev | cut -d '/' -f 1 | rev | shuf | head -n 1) >> ~/.zen/ipfs/.$IPFSNODEID/FRIENDS/liking_me
for liking_me in $(cat ~/.zen/ipfs/.$IPFSNODEID/FRIENDS/liking_me | sort | uniq);
do
[[ "$liking_me" == "" ]] && continue ## Protect from empty line !!
##### BOOTSTRAP AUTO LIKE BACK (NOT liking Myself)
if [[ "$IamBOOTSTRAP" == "$G1PUB" ]]; then
echo "I AM A BOOTSTRAP AUTO LIKING $liking_me FRIEND"
if [[ ! -f ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me/tryme.addr && "$liking_me" != "$G1PUB" ]]; then
echo "SENDING 1 STAR AUTO LIKE to $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" unstars -p $liking_me
sleep $((1 + RANDOM % 2)) # SLOW DOWN
~/.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 $liking_me -n 1
sleep $((1 + RANDOM % 2)) # SLOW DOWN
## IF DISCONNECTED FROM GCHANGE - create FRIEND Space (Need Web UI session)
## touch ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me/tryme.addr
## echo 1 > ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me/stars.level
fi
fi
ipfsnodeid=$(~/.zen/astrXbian/zen/tools/g1_to_ipfs.py $liking_me)
echo "Reading stars.level from Stations IPNS Capsules."
starsin=$(ipfs --timeout=10s cat /ipns/$ipfsnodeid/.$ipfsnodeid/FRIENDS/$G1PUB/stars.level)
starsout=$(ipfs --timeout=10s cat /ipns/$IPFSNODEID/.$IPFSNODEID/FRIENDS/$liking_me/stars.level)
echo "I received $starsin stars while I sent $starsout stars."
##### CHECKING IF WE LIKE EACH OTHER (AVOID LIKING MYSELF)
~/.zen/astrXbian/zen/tools/timeout.sh -t 20 ~/.zen/astrXbian/zen/jaklis/jaklis.py -k ~/.zen/secret.dunikey -n "https://data.gchange.fr" stars -p $liking_me > /tmp/Gstars.json
## ZOMBIE PROTECTION
[[ "$?" == "0" && ! -f /tmp/Gstars.json ]] && rm -Rf ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me && echo "$liking_me is a ZOMBIE..." && continue
friend_of_mine=$(cat /tmp/Gstars.json | jq -r '.yours');
if [[ "$friend_of_mine" != "null" && "$liking_me" != "$G1PUB" ]]
then
# ADD $liking_me TO MY ipfs FRIENDS list
echo "$liking_me is my FRIEND"
mkdir -p ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me
# REFRESH & PUBLISH stars friends map
stars="$(cat /tmp/Gstars.json | jq -r '.yours.level')"
if [[ "$stars" == "null" || "$stars" == "" ]]; then
rm -Rf ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me
echo "$friend_of_mine NO STAR !! Removing $liking_me"
## TODO : remove "ipfs pin" in "~/.zen/PIN/"
continue ## REMOVE NO GOOD FRIENDS (no star)
fi
cp /tmp/Gstars.json ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me/ && rm -f /tmp/Gstars.json
echo "$stars" > ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me/stars.level && echo "***** $stars STARS *****"
echo "***** Convert $liking_me to ipfsnodeid *****"
ipfsnodeid=$(~/.zen/astrXbian/zen/tools/g1_to_ipfs.py $liking_me)
echo ${ipfsnodeid} > ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me/ipfsnodeid
## TIMEOUT... uncomment in DEFCON3 only
# zuid=$(ipfs --timeout=10s cat /ipns/${ipfsnodeid}/.${ipfsnodeid}/_xbian.zuid)
# [[ ${zuid} ]] && touch ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me/${zuid}
## NOT USEFULL TRYING
# SEND jaklis message if tryme.addr is different from last one
# if [[ $(diff ~/.zen/ipfs/.${IPFSNODEID}/tryme.addr ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me/tryme.addr 2>&1) ]]
# then
# echo "I AM $G1PUB"
# echo "Sending IPFSTRYME message to $liking_me"
#
# if [[ -f ~/.zen/ipfs/.${IPFSNODEID}/tryme.addr ]]; then
# ~/.zen/astrXbian/zen/tools/timeout.sh -t 20 ~/.zen/astrXbian/zen/jaklis/jaklis.py -k ~/.zen/secret.dunikey -n "https://data.gchange.fr" send -d $liking_me -t "ipfstryme" -f ~/.zen/ipfs/.${IPFSNODEID}/tryme.addr
# else
# echo 'MISSING ~/.zen/ipfs/.${IPFSNODEID}/tryme.addr'
# fi
# echo "$friend_of_mine" >> /tmp/friend_of_mine
# else
# echo "tryme.addr MESSAGE ALREADY SENT TO $liking_me"
# fi
# MEMORIZE actual tryme.addr (gchange_IPFS_swarm.sh is getting it from IPNS capsule)
cp -f ~/.zen/ipfs/.${IPFSNODEID}/tryme.addr ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me/
fi
sleep $((1 + RANDOM % 2)) # SLOW DOWN
done
# REMOVE NOT ANYMORE FRIENDS ~/.zen/ipfs/.${IPFSNODEID}/FRIENDS/$liking_me/
## TODO compare with /tmp/friend_of_mine
# REFRESH IPNS SELF PUBLISH
########################################################################
~/.zen/astrXbian/zen/ipns_self_publish.sh
########################################################################
########################################################################
########################################################################
## LOVE ###
# ACTIVATION MONNAIES LIBRES départ somme nulle ou pas.
# S en relatif M/N fixé selon le nb de GUILDES MadeInZion souscrites
# Echelle de confiance décroissante remonte à chaque POST
# Echelle de confiance LOVE/G1 (quantité de monnaie certifiée échangeable ~ ratio mis à jour lors des bisous)
# DU(t+1)=(DU+c²)/r
# S(t+1)=(S+DU)/r
# M(t+1)=N (et oui, si vous regardez les formules, on na pas besoin de M :slight_smile: On a donc pas besoin de stocker M !)
# r=(1+DU)*N/N(t+1) (diviser par r, cest multiplier par [N(t+1)/N]/(1+DU), qui prend en compte la variation de N)
## N est le nombre de relations 5* !!
# Chaque joueur démarre à 100 LOVE au début du JEu. DU = 100 LOVE.
# Le compteur de DU de chacun converge chaque jour vers 100 LOVE (prélevant au fort pour redistribuer aux faibles)
# amortis temporellement par C² (adapté à la durée de vie moyenne des joueurs de chaque essaim !!).
# Cela ressemble à circuit résonant (RLC) amortis par la durée de vie?!
# https://forum.monnaie-libre.fr/t/etude-commencer-a-la-moyenne/15231/34?u=qoop
# https://forum.monnaie-libre.fr/t/formules-en-referentiel-du-et-m-n/10417
echo "#### EXCECUTION TIME"
end=`date +%s`
echo Execution time was `expr $end - $start` seconds.
echo "########################################################################"
exit 0