astrXbian/zen/ipfs_SWARM_refresh.sh

118 lines
5.0 KiB
Bash
Raw Normal View History

2020-12-12 01:26:39 +01:00
#!/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"
2021-01-31 17:21:20 +01:00
timestamp=$(date -u +%s%N | cut -b1-13)
2020-12-12 01:26:39 +01:00
########################################################################
# Delete "ipfs_swarm" if file structure is evolving... or online only mode
[[ ! -f /tmp/swarm_cleaned ]] && rm -Rf /home/$YOU/.zen/ipfs_swarm/ && mkdir -p /home/$YOU/.zen/ipfs_swarm && echo "SWARM CLEAN OK" > /tmp/swarm_cleaned
########################################################################
2021-02-16 23:53:09 +01:00
rm /tmp/treated.ipfs.swarm
touch /tmp/treated.ipfs.swarm
2020-12-12 01:26:39 +01:00
count=1
for peerline in $(ipfs swarm peers && cat ~/.zen/A_swarm_map.txt | sort | uniq);
2020-12-12 01:26:39 +01:00
do
2020-12-25 18:27:47 +01:00
ipfsnodeid=$(echo "$peerline" | awk -F '/' '{print $8}')
2020-12-25 20:27:04 +01:00
[[ "$ipfsnodeid" == "" ]] && continue
2021-02-21 20:33:38 +01:00
# Convert ipfsnodeid into g1pub
2020-12-25 18:27:47 +01:00
whoisg1=$(~/.zen/astrXbian/zen/tools/ipfs_to_g1.py $ipfsnodeid)
2021-02-21 20:33:38 +01:00
# control ip isLAN?
2020-12-25 18:27:47 +01:00
ip=$(echo "$peerline" | awk -F '/' '{print $3}')
2020-12-12 01:26:39 +01:00
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])/")
2021-02-21 20:33:38 +01:00
## $ipfsnodeid already Treated ?
2021-02-16 23:53:09 +01:00
[[ $(cat /tmp/treated.ipfs.swarm | grep $ipfsnodeid ) ]] && continue
2020-12-25 18:27:47 +01:00
nowdate=$(date)
2020-12-12 01:26:39 +01:00
2021-01-31 17:21:20 +01:00
echo "### ANALYSING $whoisg1 = $ipfsnodeid ($ip) ###"
2020-12-25 18:27:47 +01:00
echo "${peerline}"
myfriendpeer=$(cat ~/.zen/A_my_swarm.txt | grep $ipfsnodeid )
if [[ "$myfriendpeer" != "" ]]; then
2020-12-25 20:30:11 +01:00
[[ ! $(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 12 ipfs get --output=/home/$YOU/.zen/ipfs_swarm/ /ipns/$ipfsnodeid
2021-02-01 04:00:38 +01:00
if [ $? != 0 ]; then
echo "TIMEOUT REACHED ___ REMOVE $ipfsnodeid FROM ~/.zen/A_my_swarm.txt";
grep -vEi $ip ~/.zen/A_my_swarm.txt
fi
2020-12-25 18:27:47 +01:00
else
2021-02-21 20:33:38 +01:00
[[ -f ~/.zen/DEFCON ]] && export DEFCON=$(cat ~/.zen/DEFCON) || export DEFCON=$(cat ~/.zen/astrXbian/DEFCON) # like in crom_MINUTE.sh
2021-01-31 17:21:20 +01:00
echo "!!! NOT MY FRIEND !!! ___________________ # DEFCON : $DEFCON "
2021-01-31 17:34:46 +01:00
if [[ ( $DEFCON < 5 ) && ! $isLAN ]]
2021-01-31 17:21:20 +01:00
then
### SECURITY CHOICE TO MAKE IN /etc/sudoers ###
# $USER ALL=(ALL:ALL) NOPASSWD:ALL
# $USER ALL=(ALL:ALL) NOPASSWD:/usr/bin/fail2ban-client
sudo fail2ban-client add recidive 2>/dev/null 1>&2
sudo fail2ban-client start recidive
sudo fail2ban-client set recidive banip $ip
2021-01-31 17:21:20 +01:00
# Show ALL banned IP
sudo fail2ban-client status recidive
2021-01-31 17:21:20 +01:00
fi
if [[ ! $isLAN ]]; then
ipfs swarm disconnect "$peerline"
ipfs bootstrap rm "$peerline"
fi
2020-12-25 18:27:47 +01:00
fi
echo "$ipfsnodeid" >> /tmp/treated.ipfs.swarm
2020-12-12 01:26:39 +01:00
done
2021-02-01 02:43:19 +01:00
########################################################################
# REFRESH xbian Film Serie Anime ASTRXBIAN index
2021-02-01 02:43:19 +01:00
########################################################################
2021-02-13 11:30:52 +01:00
mkdir -p ~/.zen/ipfs/.${IPFSNODEID}/astroport/kodi/vstream/
2021-02-01 02:43:19 +01:00
PREFIX="F"
cat ~/.zen/ipfs_swarm/.*/astroport/kodi/vstream/${PREFIX}* | sort | uniq > ~/.zen/ipfs/.${IPFSNODEID}/${PREFIX}ASTRXBIAN
PREFIX="S"
cat ~/.zen/ipfs_swarm/.*/astroport/kodi/vstream/${PREFIX}* | sort | uniq > ~/.zen/ipfs/.${IPFSNODEID}/${PREFIX}ASTRXBIAN
PREFIX="A"
cat ~/.zen/ipfs_swarm/.*/astroport/kodi/vstream/${PREFIX}* | sort | uniq > ~/.zen/ipfs/.${IPFSNODEID}/${PREFIX}ASTRXBIAN
2021-02-20 17:34:01 +01:00
########################################################################
########################################################################
echo "RETRIEVING MY OWN DATA FROM IPNS TO ~/.zen/ipfs_swarm"
ipfs get --output=/home/$YOU/.zen/ipfs_swarm/ /ipns/$IPFSNODEID
# REFRESH IPNS SELF PUBLISH
2021-02-01 02:43:19 +01:00
########################################################################
~/.zen/astrXbian/zen/ipns_self_publish.sh
########################################################################
2021-02-01 02:43:19 +01:00
2020-12-12 01:26:39 +01:00
exit 0