forked from axiom-team/astrXbian
289 lines
14 KiB
Bash
Executable File
289 lines
14 KiB
Bash
Executable File
#!/bin/bash
|
|
########################################################################
|
|
# Author: Fred (support@qo-op.com)
|
|
# Armust Blegde release
|
|
# Version: 0.3
|
|
# License: AGPL-3.0 (https://choosealicense.com/licenses/agpl-3.0/)
|
|
########################################################################
|
|
# REGULATEUR DES PULSATIONS DE L'AUTOMATE # CADENCE # 1/60 Hz
|
|
# INJECTEZ VOS ACTIONS DANS LES CYCLES...
|
|
########################################################################
|
|
MY_PATH="`dirname \"$0\"`" # relative
|
|
MY_PATH="`( cd \"$MY_PATH\" && pwd )`" # absolutized and normalized
|
|
ME="${0##*/}"
|
|
|
|
source $HOME/.profile
|
|
# CORRECT PATH
|
|
[[ ! $(echo $PATH | grep "/usr/local/bin") ]] && export PATH="$PATH:/usr/local/bin"
|
|
[[ ! $(echo $PATH | grep "$HOME/.local/bin") ]] && export PATH="$PATH:$HOME/.local/bin"
|
|
[[ ! $(echo $PATH | grep "/usr/games") ]] && export PATH="$PATH:/usr/games"
|
|
|
|
#### INIT STATION PARAMETERS
|
|
[[ $IPFSNODEID ]] && export IPFSNODEID=$IPFSNODEID || export IPFSNODEID=$(ipfs id -f='<id>\n')
|
|
## CONTROL IPFS IS RUNNING OR RESTART
|
|
isIPFSRunning=$(ps auxf --sort=+utime | grep -w ipfs | grep -v -E 'color=auto|grep' | tail -n 1 | cut -d ' ' -f 1)
|
|
if [[ ! $isIPFSRunning ]]; then
|
|
[[ -f /etc/sudoers.d/systemctl ]] && sudo systemctl restart ipfs
|
|
echo "ERROR! ipfs daemon was not running. RELAUNCHING."
|
|
exit 1
|
|
fi
|
|
########################################################################
|
|
# CORRECT VSTREAM ASTROPORT SITE/ART REMOVED AFTER PLUGIN UPDATE
|
|
[[ ! -f ~/.kodi/addons/plugin.video.vstream/resources/sites/astroport.py ]] && [[ -f ~/.zen/astroport.py ]] && cp -f ~/.zen/astroport.py ~/.kodi/addons/plugin.video.vstream/resources/sites/
|
|
[[ ! -f ~/.kodi/addons/plugin.video.vstream/resources/art/astroport.png ]] && cp ~/.zen/astrXbian/.install/.kodi/addons/plugin.video.vstream/resources/art/astroport.png ~/.kodi/addons/plugin.video.vstream/resources/art/
|
|
########################################################################
|
|
|
|
### NO IP GATEWAY : EXIT
|
|
ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` 2>&1>/dev/null && echo "LAN OK" || exit 1
|
|
|
|
########################################################################
|
|
### TEMP CODE (add missing packages to xbian) - TODO REMOVE after 30 mn for git pull & apply
|
|
# [[ $(which ffmpeg) == "" && "$USER" == "xbian" ]] && sudo apt-get update && sudo apt-get install git fail2ban inotify-tools curl net-tools libsodium* python3-dev python3-pip python3-setuptools python3-wheel mpack libssl-dev libffi-dev build-essential qrencode jq bc gawk ffmpeg sqlite dnsutils vlc mp3info x11-utils -y
|
|
|
|
########################################################################
|
|
|
|
########################################################################
|
|
# HELP DEVLT: Search "TODO" in code
|
|
# WARNING: This script must be processing less than 1 minute, so do not charge too much this script
|
|
# _ _____ ____ _ _______ ________ ____
|
|
# | | / / | / __ \/ | / / _/ | / / ____/ / / /
|
|
# | | /| / / /| | / /_/ / |/ // // |/ / / __ / / /
|
|
# | |/ |/ / ___ |/ _, _/ /| // // /| / /_/ / /_/_/
|
|
# |__/|__/_/ |_/_/ |_/_/ |_/___/_/ |_/\____/ (_|_)
|
|
|
|
########################################################################
|
|
# 'figlet' was used for labeling code trunks
|
|
########################################################################
|
|
### MODIFY WITH GREAT CARE!!! MUST BE BUG FREE !!!!!
|
|
### EVERY STATION RUN THIS SCRIPT EVERY MINUTE.
|
|
export isKodiRunning=$(ps auxf --sort=+utime | grep -w kodi | grep -v -E 'color=auto|grep' | tail -n 1 | cut -d ' ' -f 1)
|
|
########################################################################
|
|
cd $MY_PATH
|
|
|
|
########################################################################
|
|
# WHAT TIME(s) is it ?
|
|
timestamp="$(date -u +%s%N | cut -b1-13)"
|
|
minute=$(date -u +%M)
|
|
heure=$(date -u +%H)
|
|
timebar="$heure:$minute"
|
|
dayoftheweek=$(date +%w)
|
|
dayofthemonth=$(date +%d)
|
|
weeknumber=$(date +%V)
|
|
|
|
# WAKEUP TIME
|
|
if [[ ! -f /tmp/A_Station_ON ]]; then
|
|
echo "onheure=$heure; onminute=$minute; ontimebar=$timebar" > /tmp/A_Station_ON
|
|
else
|
|
source /tmp/A_Station_ON
|
|
old=$ontimebar
|
|
new=$timebar
|
|
# feeding variables by using read and splitting with IFS
|
|
IFS=: read old_hour old_min <<< "$old"
|
|
IFS=: read hour min <<< "$new"
|
|
# convert hours to minutes
|
|
# the 10# is there to avoid errors with leading zeros
|
|
# by telling bash that we use base 10
|
|
total_old_minutes=$((10#$old_hour*60 + 10#$old_min))
|
|
total_minutes=$((10#$hour*60 + 10#$min))
|
|
runtime=$((total_minutes - total_old_minutes))
|
|
fi
|
|
# RUN TIME $runtime
|
|
|
|
echo "RUNTIME $runtime mn ($timebar)"
|
|
########################################################################
|
|
## CHECK & (RE)ACTIVATE ipfs p2p tunnels
|
|
########################################################################
|
|
# NextCloud FROM taurus TO oasis (https://taurus.copylaradio.com)
|
|
~/.zen/astrXbian/zen/ipfs_P2P_forward.sh
|
|
########## SUPPORT : OPEN OR CLOSE SSH ACCESS #################
|
|
# [[ "$(ipfs p2p ls | grep $G1PUB-ssh)" == "" ]] && sleep $((1 + RANDOM % 10)) && ~/.zen/astrXbian/zen/ssh_forward2onelove.sh # OPEN-SSH
|
|
# [[ "$(ipfs p2p ls | grep $G1PUB-ssh)" != "" ]] && sleep $((1 + RANDOM % 10)) && ~/.zen/astrXbian/zen/ssh_forward2onelove.sh # CLOSE-SSH
|
|
|
|
########################################################################
|
|
# DEFCON
|
|
########################################################################
|
|
# STATION CAN MODIFY DEFCON (cool 5 to 1 war) - only 5 and 4 (fail2ban) are ready to use.
|
|
[[ -f ~/.zen/DEFCON ]] && export DEFCON=$(cat ~/.zen/DEFCON) || export DEFCON=$(cat ~/.zen/astrXbian/DEFCON) # ADAPT $DEFCON scenario
|
|
## DEFCON SYSTEM ACTIVATED
|
|
|
|
########################################################################
|
|
# SMARM MAINTENANCE / 15 mn
|
|
########################################################################
|
|
# astrXbian TestNET (git pull) -> desactivate in release ipfs get codeID
|
|
########################################################################
|
|
# ACTIVATE 15-17 MN "git pull" update
|
|
if [[ $(($minute % 15)) == 0 ]]; then
|
|
########## DESYNCH SWARM NODE REQUESTS #################
|
|
sleep $((1 + RANDOM % 120))
|
|
# git reset --hard
|
|
git pull
|
|
[[ ! $? == 0 ]] && echo "GIT.P2P.LEGAL IS UNREACHABLE"
|
|
# if [[ $(diff ~/.zen/astrXbian/.install/.kodi/userdata/addon_data/plugin.video.vstream/settings.xml ~/.kodi/userdata/addon_data/plugin.video.vstream/settings.xml) ]]; then
|
|
# cp ~/.zen/astrXbian/.install/.kodi/userdata/addon_data/plugin.video.vstream/settings.xml ~/.kodi/userdata/addon_data/plugin.video.vstream/settings.xml
|
|
# echo "MAJ Vstream settings"
|
|
# fi
|
|
|
|
## IN CASE OF jaklis UPGRADE ACTIVATE AND REMOVE
|
|
## cd ~/.zen/astrXbian/zen/jaklis; ./setup.sh; cd -
|
|
|
|
# CLEAN AND REMOVE BAD DIRECTORIES
|
|
## BYPASS index.html DELETION
|
|
[[ -f ~/.zen/ipfs/index.html ]] && cp ~/.zen/ipfs/index.html /tmp/ipfsindex.html
|
|
|
|
[[ -d ~/.zen/ipfs/astroport ]] && rm -Rf ~/.zen/ipfs/astroport
|
|
[[ -d ~/.zen/ipfs_swarm/astroport ]] && rm -Rf ~/.zen/ipfs_swarm/astroport
|
|
[[ -d ~/.zen/ipfs/xbian ]] && rm -Rf ~/.zen/ipfs/xbian
|
|
[[ -d ~/.zen/ipfs_swarm/xbian ]] && rm -Rf ~/.zen/ipfs_swarm/xbian
|
|
[[ -d ~/.zen/ipfs/PIN ]] && rm -Rf ~/.zen/ipfs/PIN
|
|
[[ -d ~/.zen/ipfs_swarm/PIN ]] && rm -Rf ~/.zen/ipfs_swarm/PIN
|
|
## CLEANING PARASITE BAD FILES
|
|
rm ~/.zen/ipfs/*
|
|
rm ~/.zen/ipfs/.*
|
|
rm ~/.zen/ipfs_swarm/*
|
|
rm ~/.zen/ipfs_swarm/.*
|
|
|
|
## RESTORE index.html from /tmp/ipfsindex.html
|
|
[[ -f /tmp/ipfsindex.html ]] && mv /tmp/ipfsindex.html ~/.zen/ipfs/index.html
|
|
|
|
## PING ALL MY FRIENDS
|
|
echo "## FRIENDS IPFS PINGing"
|
|
for g1pub in $(ls -t ~/.zen/ipfs/.$IPFSNODEID/FRIENDS/); do [[ $g1pub ]] && ipfs ping --timeout=7s -n 5 /ipfs/$(~/.zen/astrXbian/zen/tools/g1_to_ipfs.py $g1pub 2>/dev/null) 2>/dev/null; done
|
|
|
|
fi
|
|
|
|
# __ __ _____ ____
|
|
# __/ // /_/ ___/ ______ __________ ___ / __ \
|
|
# /_ _ __/\__ \ | /| / / __ `/ ___/ __ `__ \/ / / /
|
|
# /_ _ __/___/ / |/ |/ / /_/ / / / / / / / / /_/ /
|
|
# /_//_/ /____/|__/|__/\__,_/_/ /_/ /_/ /_/\____/ gchange sync
|
|
#
|
|
##################################################################
|
|
# TODO: IDEA make gchange/ipfs/ipns refresh speed depending on ipfs swarm size and DEFCON level !
|
|
## SYNC GCHANGE EVERY 1 hour runtime
|
|
[[ $(($runtime % 60)) == 0 ]] && $MY_PATH/zen/gchange_INIT.sh && $MY_PATH/zen/gchange_IPFS_swarm.sh &
|
|
|
|
# QUICK SYNC SWARM EVERY 20 minutes +/- 30 sec
|
|
[[ $(($runtime % 20)) == 0 ]] && sleep $((1 + RANDOM % 30)) && $MY_PATH/zen/ipfs_FRIENDS_refresh.sh &
|
|
|
|
########################################################################
|
|
# ACTIVATE ~/astroport directory monitoring (xbian only)
|
|
########################################################################
|
|
###
|
|
# _ _
|
|
# __ _| |__ (_) __ _ _ __
|
|
# \ \/ / '_ \| |/ _` | '_ \
|
|
# > <| |_) | | (_| | | | |
|
|
# /_/\_\_.__/|_|\__,_|_| |_| inotifywait
|
|
#
|
|
# ON Rpi/xbian files added in ~/astroport are monitored to activate "new_file_in_astroport.sh"
|
|
# Transfert ~/astroport/*/files to IPFS and make Astroport/KODI indexes in IPNS
|
|
if [[ "$USER" == "xbian" ]]; then
|
|
[[ ! -e $HOME/astroport ]] && mkdir -p $HOME/astroport
|
|
# VERIFY LAUNCH inotifywait ?!
|
|
inotifywaitRunning=$(ps auxf --sort=+utime | grep -w inotifywait | grep -w astroport | grep -v -E 'color=auto|grep' | tail -n 1 | cut -d ' ' -f 1)
|
|
if [[ "$inotifywaitRunning" != "xbian" ]]
|
|
then
|
|
/usr/bin/inotifywait -r -e close_write -m /home/$USER/astroport | while read dir flags file; do $MY_PATH/zen/new_file_in_astroport.sh "$dir" "$file"; done >> /tmp/astroport.inotify.log 2>&1 &
|
|
fi
|
|
fi
|
|
|
|
##########################################################
|
|
|
|
##################################################################
|
|
#
|
|
# _________ ______
|
|
# ____ ___ ____ /_ __/ | / ____/
|
|
# /_ / / _ \/ __ \ / / / /| |/ / __
|
|
# / /_/ __/ / / / / / / ___ / /_/ /
|
|
# /___/\___/_/ /_/ /_/ /_/ |_\____/
|
|
#
|
|
# REFRESHing MEDIAKEY / ZenTAG nano DHTs => Zen File ECONOMY wallet cycle
|
|
if [[ "$timebar" == "12:12" || "$timebar" == "00:12" || "$timebar" == "03:03" || $(($runtime % 720)) == 0 || $runtime == 12 ]]; then
|
|
# Every 12h runtime restarting IPFS daemon
|
|
[[ "$timebar" == "03:03" ]] && [[ -f /etc/sudoers.d/systemctl || "$USER" == "xbian" ]] && sudo systemctl restart ipfs && exit 0
|
|
|
|
#### Refresh & publish MEDIAKEYs
|
|
$MY_PATH/zen/ipns_TAG_refresh.sh
|
|
|
|
## REINIT dead swarm STATIONS
|
|
rm -f ~/.zen/A_dead_swarm.txt
|
|
fi
|
|
|
|
##################################################################
|
|
##################################################################
|
|
# 5 mn / 24h REBOOT & MIDNIGHT:01 : CLEANING & UPDATE
|
|
##################################################################
|
|
##################################################################
|
|
if [[ "$timebar" == "00:01" || "$runtime" == "5" || $(($runtime % 1380)) == 0 ]]; then
|
|
echo '
|
|
# ____ ____ ____ ___
|
|
# / __ \/ __ \ _ / __ < /
|
|
# / / / / / / / (_) / / / / /
|
|
# / /_/ / /_/ / _ / /_/ / /
|
|
# \____/\____/ (_) \____/_/
|
|
#
|
|
# MAINTENANCE
|
|
'
|
|
sleep $((1 + RANDOM % 15)) ### Be careful if random sleep is not activated...
|
|
############################## That swarm could become an aggressive blob !!
|
|
### PEACE & LOVE software is processing... We are HyperWeb people here.
|
|
# DEFCON 5 quiet behaviour so Please keep cool, take care and be zen.
|
|
# GOOD BYE GOOGLE, AMAZON, FACEBOOK, APPLE, MICROSOFT. LIBRE WE ARE.
|
|
####################################################################
|
|
# REFRESH & UPGRADE youtube-dl
|
|
youtube-dl --rm-cache-dir
|
|
youtube-dl -U
|
|
|
|
|
|
##################################################################
|
|
if [[ $dayoftheweek == 1 ]]; then
|
|
# EVERY MONDAY
|
|
# _____ __ __ ___
|
|
# / ___// /_____ _________ _____ ____ / |/ /___ __ __
|
|
# \__ \/ __/ __ \/ ___/ __ `/ __ `/ _ \ / /|_/ / __ `/ |/_/
|
|
# ___/ / /_/ /_/ / / / /_/ / /_/ / __/ / / / / /_/ /> <
|
|
# /____/\__/\____/_/ \__,_/\__, /\___/ /_/ /_/\__,_/_/|_|
|
|
# /____/
|
|
# 1/2 HDD for IPFS size - dicotomic adaptation 7th EVERY MONTH
|
|
availableDiskSize=$(df -P ~/ | awk 'NR>1{sum+=$4}END{print sum}')
|
|
availableDiskSize="$(($availableDiskSize * 1024))"
|
|
|
|
diskSize="$((availableDiskSize / 2))"
|
|
ipfs config Datastore.StorageMax $diskSize
|
|
echo "StorageMax = $diskSize"
|
|
|
|
# New full test review in gchange_INIT.sh
|
|
rm -f ~/.ipfs/test.disk
|
|
|
|
# LOG ROTATE WEEKLY
|
|
[[ -f /tmp/astroport.log ]] && [[ ! -f ~/.zen/astroport.$weeknumber.log.tgz ]] && tar cvzf ~/.zen/astroport.$weeknumber.log.tgz /tmp/astroport.log && echo "" > /tmp/astroport.log
|
|
|
|
fi
|
|
|
|
##################################################################
|
|
## CHECK STARTGATE LEVEL KEYS
|
|
# _ open _
|
|
# ___| |_ __ _ _ __ __ _ __ _| |_ ___
|
|
#/ __| __/ _` | '__/ _` |/ _` | __/ _ \
|
|
#\__ \ || (_| | | | (_| | (_| | || __/
|
|
#|___/\__\__,_|_| \__, |\__,_|\__\___|
|
|
# |___/
|
|
# IPNS KEYS used to publish with star friends (creates 5 levels of streaming / file sharing)
|
|
# RESET
|
|
ipfs key rm star_1; ipfs key rm star_2; ipfs key rm star_3; ipfs key rm star_4; ipfs key rm star_5
|
|
rm -Rf ~/.zen/key/
|
|
# RENEW & SEND STARS TO FRIENDS ~/.zen/astrXbian/zen/manage_stargates.sh
|
|
$MY_PATH/zen/manage_stargates.sh
|
|
|
|
fi
|
|
|
|
### DISAPEAR ONE HOUR FROM IPFS SWARM ZOMBIES > 300 peers in swarm
|
|
#if [[ $(cat /tmp/ipfs_swarm_peers | wc -l) -gt 300 ]]; then
|
|
#if [[ "$timebar" == "04:05" ]]; then
|
|
# ~/.zen/astrXbian/zen/ipfs_SWARM_renew.sh 3600 ## CUT IPFS FOR ONE HOUR
|
|
## SWITCHING ON
|
|
# echo "1" > ~/.zen/ipfs/.$IPFSNODEID/.switch
|
|
#fi
|
|
#fi
|
|
## BETTER ACTIVATING DEFCON 3 / swarm.key MODE
|