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
|
|
|
########################################################################
|
|
|
|
# This script is controling IPFS swarm
|
|
|
|
|
|
|
|
# Removes from peers not friends on gchange+
|
|
|
|
# remember Nodes ID and IPs
|
|
|
|
# add IPs to fail2ban
|
|
|
|
|
|
|
|
# Sync friends peers ~/.zen/ipfs/ into ~/.zen/ipfs_swarm/.IPFSNODEID's/ directories
|
|
|
|
#
|
2020-04-28 00:39:00 +02:00
|
|
|
|
2020-12-06 20:10:35 +01:00
|
|
|
## NOT WORKING !!! YET
|
2020-12-05 02:10:53 +01:00
|
|
|
exit 1
|
2020-04-13 19:12:20 +02:00
|
|
|
########################################################################
|
|
|
|
# \\///
|
|
|
|
# qo-op
|
|
|
|
############# '$MY_PATH/$ME'
|
|
|
|
########################################################################
|
|
|
|
# ex: ./'$ME'
|
2020-12-06 20:10:35 +01:00
|
|
|
########################################################################
|
|
|
|
# TODO remove because 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 20:10:35 +01:00
|
|
|
for peer in $(ipfs swarm peers);
|
|
|
|
do
|
|
|
|
id=$(echo "$peer" | awk -F '/' '{print $7}')
|
|
|
|
ip=$(echo "$peer" | awk -F '/' '{print $3}')
|
|
|
|
nowdate=$(date)
|
|
|
|
timestamp=$(date -u +%s%N | cut -b1-13)
|
|
|
|
|
|
|
|
echo "$nowdate - $id - $ip"
|
|
|
|
foundIp=$(cat ~/.zen/allow_ip.txt | grep "$ip" | wc -l)
|
|
|
|
if [ "$foundIp" -eq 0 ] ; then
|
|
|
|
echo "${ip} of peer ${id} is not in the authorized ip list. ${peer} will be removed from the swarm"
|
|
|
|
ipfs swarm disconnect $peer
|
|
|
|
ipfs bootstrap rm $peer
|
|
|
|
else
|
|
|
|
${path_script}/timeout.sh -t 30 ipfs get --output ${path_shared_files}/ /ipns/$id
|
|
|
|
fi
|
|
|
|
done
|
2020-05-08 03:46:37 +02:00
|
|
|
|
2020-05-14 11:51:25 +02:00
|
|
|
echo "PUBLISHING MY OWN /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)
|
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-05-14 11:51:25 +02:00
|
|
|
echo "REFRESHING /home/$YOU/.zen/ipfs_swarm/ from my SWARM peers"
|
2020-12-05 00:10:19 +01:00
|
|
|
rm -Rf /home/$YOU/.zen/ipfs_swarm/.12D3KooW*
|
2020-06-15 04:06:27 +02:00
|
|
|
rm -Rf /home/$YOU/.zen/ipfs_swarm/*
|
|
|
|
|
2020-04-13 19:12:20 +02:00
|
|
|
count=1
|
2020-12-05 00:50:22 +01:00
|
|
|
|
|
|
|
########################################################################
|
|
|
|
# Let's look for Friends and "IPFS swarm connect" with them
|
|
|
|
########################################################################
|
|
|
|
cd ~/.zen/astroport/zen/jaklis
|
|
|
|
# 1. Get Gchange "liking_me" people list
|
|
|
|
for liking_me in $(./jaklis.py like | jq -r '.likes[].issuer');
|
|
|
|
do
|
|
|
|
friend_of_mine=$(./jaklis.py like -p $liking_me | jq -r '.yours');
|
|
|
|
# 2. Searching "ipfstryme" message from friend_of_mine
|
|
|
|
[[ $friend_of_mine != null ]] && echo "Connect to my friend $liking_me" && friend_of_mine_count=$((friend_of_mine_count+1)) && ipfsadd=0 \
|
|
|
|
&& for peer in $(./jaklis.py read -n50 -j | jq -r --arg friendKEY "$liking_me" '.[] | select(.pubkey == $friendKEY)' | jq 'select(.title == "ipfstryme")' | jq -r '.content' | head -n1);
|
|
|
|
do
|
|
|
|
id=$(echo "$peer" | awk -F '/' '{print $7}')
|
|
|
|
[[ "$id" == "" ]] && echo "WARNING! ID empty for $peer " && continue
|
|
|
|
#### REFRESH ~/.zen/ipfs_swarm/ with /ipns/$id
|
|
|
|
$MY_PATH/tools/timeout.sh -t 30 ipfs get --output=/home/$YOU/.zen/ipfs_swarm/ /ipns/$id
|
|
|
|
|
|
|
|
|
|
|
|
ipfsadd=$((ipfsadd+1))
|
|
|
|
done;
|
|
|
|
[[ $friend_of_mine != null && $ipfsadd == 0 ]] && echo "No ipfstryme message..." && echo "$liking_me" >> /tmp/A_liking_me_NO_ipfstryme.txt
|
|
|
|
# 4. liking_me IS NOT a friend_of_mine!!
|
2020-12-05 02:10:53 +01:00
|
|
|
[[ $friend_of_mine == null ]] && \
|
|
|
|
echo "I am NOT LIKING $liking_me" && \
|
|
|
|
rm -Rf /home/$YOU/.zen/ipfs_swarm/.$id && \
|
|
|
|
|
2020-12-05 00:50:22 +01:00
|
|
|
liking_me_count=$((liking_me_count+1))
|
|
|
|
done
|
|
|
|
|
2020-05-08 03:46:37 +02:00
|
|
|
# Search for All peers Nodes. ## TODO: To be changed when Swarm is too big or not expanding
|
2020-12-05 00:10:19 +01:00
|
|
|
#for id in $(ls -d ~/.zen/ipfs_swarm/.12D3KooW* | cut -d '.' -f 3); # Alternative search...
|
2020-05-17 21:15:35 +02:00
|
|
|
for peer in $(ipfs swarm peers);
|
2020-04-13 19:12:20 +02:00
|
|
|
do
|
2020-12-05 00:50:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-05-26 21:33:12 +02:00
|
|
|
|
2020-05-23 02:14:39 +02:00
|
|
|
echo " ($count) Refreshing /home/$YOU/.zen/ipfs_swarm/.$id"
|
2020-05-26 21:37:45 +02:00
|
|
|
|
|
|
|
### Is "ipfs_swarm/.$id/G1SSB/_ssb.whoami" my SSB friend??
|
|
|
|
ssbID=$(cat /home/$YOU/.zen/ipfs_swarm/.$id/G1SSB/_ssb.whoami)
|
|
|
|
isMyFriend=$(grep -Rwl "$ssbID" /tmp/ssb-friends.txt)
|
2020-05-26 22:05:11 +02:00
|
|
|
if [[ -f /tmp/ssb-friends.txt && ! $isMyFriend ]]; then
|
2020-05-26 21:37:45 +02:00
|
|
|
echo "WARNING! $count IS NOT my SSB Friend !!! Check $ssbID"
|
2020-12-05 02:10:53 +01:00
|
|
|
|
2020-05-26 21:37:45 +02:00
|
|
|
ipfs swarm disconnect $peer
|
|
|
|
ipfs bootstrap rm $peer
|
2020-06-06 05:22:18 +02:00
|
|
|
## Send sbotc alert message
|
2020-05-26 22:05:11 +02:00
|
|
|
if [[ -f /tmp/ssb-friends_ALERT.txt && ! $(grep -Rwl "$ssbID" /tmp/ssb-friends_ALERT.txt) ]]; then
|
2020-06-06 05:22:18 +02:00
|
|
|
sbotc publish '{"type":"post","text":"Salut '"$ssbID"' essaye de se connecter à ton swarm ipfs mais PAS ami!? (http://127.0.0.1/ipfs/'"$id"')"}'
|
2020-05-26 22:05:11 +02:00
|
|
|
echo "$ssbID" >> /tmp/ssb-friends_ALERT.txt
|
|
|
|
fi
|
2020-05-26 21:37:45 +02:00
|
|
|
## TODO: Use fail2ban if same node continue entering our swarm...
|
2020-05-26 22:06:25 +02:00
|
|
|
count=$((count+1))
|
2020-05-26 21:37:45 +02:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2020-05-22 23:38:57 +02:00
|
|
|
|
2020-05-26 21:33:12 +02:00
|
|
|
#### IPFS SWARM SHAPING CONTROL
|
2020-05-17 21:21:42 +02:00
|
|
|
### CHECK Astroport Station Database Coherence?
|
2020-05-17 21:15:35 +02:00
|
|
|
if [[ ! -d /home/$YOU/.zen/ipfs_swarm/.$id ]]; then
|
2020-05-17 21:21:42 +02:00
|
|
|
echo "WARNING! $peer is NOT an Astroport Station !!"
|
2020-05-17 21:15:35 +02:00
|
|
|
ipfs swarm disconnect $peer
|
|
|
|
ipfs bootstrap rm $peer
|
2020-05-26 21:33:12 +02:00
|
|
|
# TODO bannish with firewall ?
|
2020-05-17 21:15:35 +02:00
|
|
|
fi
|
2020-05-17 21:21:42 +02:00
|
|
|
|
2020-05-23 00:31:17 +02:00
|
|
|
echo "============================================================="
|
2020-05-22 23:38:57 +02:00
|
|
|
count=$((count+1))
|
2020-05-17 21:15:35 +02:00
|
|
|
|
2020-04-13 19:12:20 +02:00
|
|
|
done
|
|
|
|
|
2020-05-13 20:37:04 +02:00
|
|
|
# TODO: TASK treatment.
|