freedomboneeee/src/zeronetavahi

176 lines
4.9 KiB
Plaintext
Raw Normal View History

2015-08-29 17:26:51 +02:00
#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# A script for using avahi to discover peers and update zeronet trackers
# License
# =======
#
# Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-08-29 23:44:13 +02:00
TRACKER_PORT=6969
2015-09-03 10:18:26 +02:00
ZERONET_INSTALL=/opt/zeronet
2015-08-29 17:26:51 +02:00
2015-09-03 10:18:26 +02:00
if [ ! -d $ZERONET_INSTALL ]; then
if [ -d /home/$USER/zeronet ]; then
ZERONET_INSTALL=/home/$USER/zeronet
2015-08-29 18:48:43 +02:00
else
exit 0
fi
2015-08-29 17:26:51 +02:00
fi
2015-09-03 10:18:26 +02:00
BOOTSTRAP_FILE=$ZERONET_INSTALL/bootstrap
BLOGS_FILE=$ZERONET_INSTALL/blogs
FORUM_FILE=$ZERONET_INSTALL/forum
2015-08-29 17:26:51 +02:00
if [ ! -d /etc/avahi ]; then
exit 0
fi
TEMPFILE_BASE=/tmp/tmpzeronetavahibase.txt
2015-08-29 17:26:51 +02:00
TEMPFILE=/tmp/tmpzeronetavahi.txt
avahi-browse -atr > $TEMPFILE_BASE
cat $TEMPFILE_BASE | grep "Workstation\|hostname =\|address =\|port =" > $TEMPFILE
2015-08-29 17:26:51 +02:00
if [ ! -f $TEMPFILE ]; then
exit 1
fi
if [ -f $BOOTSTRAP_FILE.new ]; then
rm -f $BOOTSTRAP_FILE.new
fi
2015-08-29 17:26:51 +02:00
state=0
address=""
peer=""
while IFS='' read -r line || [[ -n "$line" ]]; do
if [ ${state} -eq "2" ]; then
if [[ $line == *"address ="* ]]; then
address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
2015-08-31 13:20:57 +02:00
echo "http $peer:$TRACKER_PORT/announce None" >> $BOOTSTRAP_FILE.new
2015-08-29 17:38:24 +02:00
state=0
2015-08-29 17:26:51 +02:00
fi
fi
if [ ${state} -eq "1" ]; then
if [[ $line == *"hostname ="* ]]; then
peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
state=2
fi
fi
if [[ $line == *"Workstation"* && $line == "= "* ]]; then
state=1
fi
done < "$TEMPFILE"
# detect blogs in the mesh
if [ -f $BLOGS_FILE.new ]; then
rm -f $BLOGS_FILE.new
fi
cat $TEMPFILE_BASE | grep "ZeroNet Blog\|hostname =\|address =\|port =\|txt-record =" > $TEMPFILE
state=0
address=""
peer=""
while IFS='' read -r line || [[ -n "$line" ]]; do
if [ ${state} -eq "3" ]; then
if [[ $line == *"txt-record ="* ]]; then
blog_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
echo "$peer $blog_url" >> $BLOGS_FILE.new
state=0
fi
fi
if [ ${state} -eq "2" ]; then
if [[ $line == *"address ="* ]]; then
address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
state=3
fi
fi
if [ ${state} -eq "1" ]; then
if [[ $line == *"hostname ="* ]]; then
peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
state=2
fi
fi
if [[ $line == *"ZeroNet Blog"* && $line == "= "* ]]; then
state=1
fi
done < "$TEMPFILE"
# detect fora in the mesh
if [ -f $FORUM_FILE.new ]; then
rm -f $FORUM_FILE.new
fi
cat $TEMPFILE_BASE | grep "ZeroNet Forum\|hostname =\|address =\|port =\|txt-record =" > $TEMPFILE
state=0
address=""
peer=""
while IFS='' read -r line || [[ -n "$line" ]]; do
if [ ${state} -eq "3" ]; then
if [[ $line == *"txt-record ="* ]]; then
forum_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
echo "$peer $forum_url" >> $FORUM_FILE.new
state=0
fi
fi
if [ ${state} -eq "2" ]; then
if [[ $line == *"address ="* ]]; then
address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
state=3
fi
fi
if [ ${state} -eq "1" ]; then
if [[ $line == *"hostname ="* ]]; then
peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
state=2
fi
fi
if [[ $line == *"ZeroNet Forum"* && $line == "= "* ]]; then
state=1
fi
done < "$TEMPFILE"
rm -f $TEMPFILE_BASE
2015-08-29 17:26:51 +02:00
rm -f $TEMPFILE
2015-08-29 17:26:51 +02:00
cp -f $BOOTSTRAP_FILE.new $BOOTSTRAP_FILE
rm -f $BOOTSTRAP_FILE.new
2015-08-31 13:20:57 +02:00
if [ -d /home/zeronet ]; then
sudo chown zeronet:zeronet $BOOTSTRAP_FILE
fi
2015-08-29 17:26:51 +02:00
cp -f $BLOGS_FILE.new $BLOGS_FILE
rm -f $BLOGS_FILE.new
if [ -d /home/zeronet ]; then
sudo chown zeronet:zeronet $BLOGS_FILE
fi
cp -f $FORUM_FILE.new $FORUM_FILE
rm -f $FORUM_FILE.new
if [ -d /home/zeronet ]; then
sudo chown zeronet:zeronet $FORUM_FILE
fi
2015-08-29 17:26:51 +02:00
exit 0