astroport/zen/ipfs_SWARM_refresh.sh

86 lines
3.4 KiB
Bash
Raw Permalink Normal View History

2020-04-13 19:12:20 +02: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##*/}"
2020-12-06 20:10:35 +01:00
########################################################################
2020-04-13 19:12:20 +02:00
# \\///
# qo-op
############# '$MY_PATH/$ME'
########################################################################
# ex: ./'$ME'
2020-12-06 20:10:35 +01:00
########################################################################
2020-12-06 21:29:37 +01:00
# 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
2020-05-26 21:33:12 +02:00
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')
2020-04-13 19:12:20 +02:00
########################################################################
2020-05-08 03:46:37 +02:00
mkdir -p ~/.zen/ipfs_swarm
2020-04-13 19:12:20 +02:00
echo '
___ _ _ __ __ _ _ _ _ _ _ __
| |_)|_(_ (_\ //\ |_)|\/| |_)|_|_|_)|_(_ |_|
_|_| | __) __)\/\//--\| \| | | \|_| | \|___)| |
'
2020-05-21 11:32:43 +02:00
echo "I am $IPFSNODEID"
2020-05-04 03:19:27 +02:00
2020-12-06 21:29:37 +01:00
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
2020-12-06 20:10:35 +01:00
for peer in $(ipfs swarm peers);
do
2020-12-06 21:29:37 +01:00
ipfsnodeid=$(echo "$peer" | awk -F '/' '{print $7}')
2020-12-06 20:10:35 +01:00
ip=$(echo "$peer" | awk -F '/' '{print $3}')
nowdate=$(date)
timestamp=$(date -u +%s%N | cut -b1-13)
echo "$nowdate - $id - $ip"
2020-12-08 03:36:10 +01:00
foundIp=$(cat ~/.zen/A_allow_ip.txt | grep "$ip")
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])/")
2020-12-06 21:29:37 +01:00
2020-12-08 03:36:10 +01:00
if [[ ! $foundIp && ! $isLAN ]] ; then
2020-12-06 21:29:37 +01:00
echo "${ip} of peer ${id} is not in the authorized ip list."
echo "${peer} will be removed from the swarm"
ipfs swarm disconnect $peer
ipfs bootstrap rm $peer
2020-12-07 14:45:13 +01:00
echo "# FAIL2BAN # $USER must activate no password sudo (Rpi & Xbian OK)"
2020-12-06 21:29:37 +01:00
[[ $USER == "pi" || $USER == "xbian" ]] && echo "BAN $ip" \
&& sudo fail2ban-client add recidive \
&& sudo fail2ban-client set recidive banip $ip
2020-12-06 20:10:35 +01:00
else
2020-12-08 02:33:58 +01:00
echo "${peer}"
2020-12-07 14:45:13 +01:00
echo "REFRESH /ipns/$ipfsnodeid INTO ~/.zen/ipfs_swarm/"
$MY_PATH/tools/timeout.sh -t 20 ipfs get --output=/home/$YOU/.zen/ipfs_swarm/ /ipns/$ipfsnodeid
2020-12-06 21:29:37 +01:00
2020-12-06 20:10:35 +01:00
fi
2020-12-06 21:29:37 +01:00
2020-12-06 20:10:35 +01:00
done
2020-05-08 03:46:37 +02:00
2020-12-08 02:39:04 +01:00
echo "$IPFSNODEID PUBLISHING /home/$YOU/.zen/ipfs/ to SWARM"
2020-05-14 11:51:25 +02:00
[[ ! -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)
2020-04-28 00:12:24 +02:00
NODEIPNS=$(ipfs name publish --quieter /ipfs/$IWALLETS)
2020-12-06 20:10:35 +01:00
# Put my own data in /home/$YOU/.zen/ipfs_swarm/
2020-05-21 11:32:43 +02:00
ipfs get --output=/home/$YOU/.zen/ipfs_swarm/ /ipns/$IPFSNODEID
2020-04-28 00:09:16 +02:00
2020-12-07 14:45:13 +01:00
# NB: could start sync IPNS from "swarm peers" and check ~/.zen/ipfs_swarm/.$ipfsnodeid/G1SSB/_g1.pubkey
# if it is a gchange friend of mine... Then decide to bannish or keep.
# But IPNS sync with bad node could lead to a filesystem concistency corruption...
# Should be sync in a temp separate folder... So keeping IP control for now.
2020-12-06 21:29:37 +01:00
exit 0