astrXbian/zen/ipfs_SWARM_refresh.sh

95 lines
4.3 KiB
Bash
Executable File
Raw Permalink 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.

#!/bin/bash
########################################################################
# Author: Fred (support@qo-op.com)
# Version: 2020.03.24
# 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##*/}"
########################################################################
# \\///
# qo-op
############# '$MY_PATH/$ME'
########################################################################
# ex: ./'$ME'
########################################################################
# This script is controling IPFS swarm
# Sync friends peers ~/.zen/ipfs/ into ~/.zen/ipfs_swarm/.IPFSNODEID's/ directories
# add not friend_of_mine IPs to fail2ban
########################################################################
# TODO remove because now it is in~/.bashrc
export YOU=$(ps auxf --sort=+utime | grep -w ipfs | grep -v -E 'color=auto|grep' | tail -n 1 | cut -d " " -f 1)
export IPFSNODEID=$(ipfs id -f='<id>\n')
########################################################################
mkdir -p ~/.zen/ipfs_swarm
echo '
___ _ _ __ __ _ _ _ _ _ _ __
| |_)|_(_ (_\ //\ |_)|\/| |_)|_|_|_)|_(_ |_|
_|_| | __) __)\/\//--\| \| | | \|_| | \|___)| |
'
echo "I am $IPFSNODEID"
echo "REFRESHING /home/$YOU/.zen/ipfs_swarm/ from my SWARM peers"
rm -Rf /home/$YOU/.zen/ipfs_swarm/.12D3KooW*
rm -Rf /home/$YOU/.zen/ipfs_swarm/.Qm*
rm -Rf /home/$YOU/.zen/ipfs_swarm/*
count=1
for peerline in $(ipfs swarm peers && cat ~/.zen/A_swarm_map.txt);
do
ipfsnodeid=$(echo "$peerline" | awk -F '/' '{print $8}')
[[ "$ipfsnodeid" == "" ]] && continue
whoisg1=$(~/.zen/astrXbian/zen/tools/ipfs_to_g1.py $ipfsnodeid)
ip=$(echo "$peerline" | awk -F '/' '{print $3}')
isLAN=$(echo $ip | cut -f3 -d '/' | grep -E "(^127\.)|(^192\.168\.)|(^fd42\:)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1$)|(^[fF][cCdD])/")
nowdate=$(date)
timestamp=$(date -u +%s%N | cut -b1-13)
echo "### $nowdate ### whoisg1 : $ipfsnodeid - $ip"
echo "${peerline}"
myfriendpeer=$(cat ~/.zen/A_my_swarm.txt | grep $ipfsnodeid )
if [[ "$myfriendpeer" != "" ]]; then
[[ ! $(ipfs swarm peers | grep $ipfsnodeid) ]] && ipfs swarm connect "$peerline" && ipfs bootstrap add "$peerline"
echo "OK Friend REFRESH /ipns/$ipfsnodeid INTO ~/.zen/ipfs_swarm/"
$MY_PATH/tools/timeout.sh -t 20 ipfs get --output=/home/$YOU/.zen/ipfs_swarm/ /ipns/$ipfsnodeid
else
echo "KO ___________________ # YOU CAN ACTIVATE FAIL2BAN"
# PLEASE ADD $USER NOPASSWD:ALL for fail2ban-client in /etc/sudoers
# sudo fail2ban-client add recidive 2>/dev/null 1>&2
# sudo fail2ban-client start recidive
# sudo fail2ban-client set recidive banip $ip
# Show banned IP
# sudo fail2ban-client status recidive
echo "?? BAN $ip ($ipfsnodeid)"
fi
done
########################################################################
########################################################################
########################################################################
echo "$IPFSNODEID PUBLISHING /home/$YOU/.zen/ipfs/ to SWARM"
[[ ! -d /home/$YOU/.zen/ipfs ]] && echo "ERROR : /home/$YOU/.zen/ipfs/ NOT FOUND" && exit 1
IWALLETS=$(ipfs add -rHq /home/$YOU/.zen/ipfs | tail -n 1)
NODEIPNS=$(ipfs name publish --quieter /ipfs/$IWALLETS)
# Retrieve my own data in /home/$YOU/.zen/ipfs_swarm/
ipfs get --output=/home/$YOU/.zen/ipfs_swarm/ /ipns/$IPFSNODEID
# GET XZUID
[[ -f ~/.zen/ipfs/.${IPFSNODEID}/_xbian.zuid ]] && XZUID="$(cat ~/.zen/ipfs/.${IPFSNODEID}/_xbian.zuid)" || XZUID="$(hostname)"
echo "CREATE local FASTRXBIAN from ipfs_swarm found indexes"
echo "-----------------------------------------------------------------"
mkdir -p /tmp/xbian/
echo "CAT;TMDB;YEAR;TITLE;SAISON;GENRES;GROUPES;RES;URLS=http://127.0.0.1:10021" > /tmp/xbian/FASTRXBIAN # CREATE INDEX HEADER
cat ~/.zen/ipfs_swarm/xbian/F* | grep -v -E 'CAT;TMDB' >> /tmp/xbian/FASTRXBIAN # GET All lines
cat /tmp/xbian/FASTRXBIAN | uniq > /tmp/xbian/FASTRXBIAN.uniq # REMOVE DUPLICATE
mv /tmp/xbian/FASTRXBIAN.uniq /var/www/astrxbian/FASTRXBIAN # Publish to localhost ASTROPORT relay
cp -f /var/www/astrxbian/FASTRXBIAN ~/.zen/ipfs/xbian/F${XZUID}
exit 0