freedomboneeee/src/zeronetavahi

394 lines
12 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
# Blogs and fora are discovered on the network and then published
# to an index file which can be opened in a browser
2015-08-29 17:26:51 +02:00
# License
# =======
#
2016-01-02 22:58:27 +01:00
# Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
2015-08-29 17:26:51 +02:00
#
# 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-11-27 12:42:16 +01:00
PROJECT_NAME='freedombone'
export TEXTDOMAIN=$PROJECT_NAME
export TEXTDOMAINDIR="/usr/share/locale"
2015-12-11 18:48:15 +01:00
WEB_PAGE_TITLE=$"${PROJECT_NAME} Mesh"
2015-08-29 23:44:13 +02:00
TRACKER_PORT=6969
2015-09-03 10:18:26 +02:00
ZERONET_INSTALL=/opt/zeronet
2015-09-03 20:06:44 +02:00
ZERONET_USER_HOME=/home/zeronet
2015-09-03 13:23:57 +02:00
MY_USERNAME=$USER
2015-08-29 17:26:51 +02:00
2015-09-03 10:18:26 +02:00
if [ ! -d $ZERONET_INSTALL ]; then
2015-09-03 13:23:57 +02:00
if [ -d /home/$MY_USERNAME/zeronet ]; then
ZERONET_INSTALL=/home/$MY_USERNAME/zeronet
2015-08-29 18:48:43 +02:00
else
exit 0
fi
2015-08-29 17:26:51 +02:00
fi
2015-12-08 17:22:48 +01:00
BLOGS_FILE=$ZERONET_INSTALL/${PROJECT_NAME}-blogs
2015-12-11 18:15:52 +01:00
MAIL_FILE=$ZERONET_INSTALL/${PROJECT_NAME}-mail
2015-12-08 17:22:48 +01:00
FORUM_FILE=$ZERONET_INSTALL/${PROJECT_NAME}-fora
TOX_USERS_FILE=$ZERONET_INSTALL/${PROJECT_NAME}-tox-users
2015-09-03 13:23:57 +02:00
ZERONET_INDEX=/home/$MY_USERNAME/mesh.html
2015-09-12 19:48:34 +02:00
ZERONET_CONFIG=$ZERONET_INSTALL/bootstrap
2015-09-30 21:46:18 +02:00
IPFS_PORT=4001
2015-12-11 18:48:15 +01:00
MY_BLOG_STR=$"My Blog"
MY_MAIL_STR=$"My Mail"
MY_FORUM_STR=$"My Forum"
BLOGS_STR=$"Blogs"
MAIL_STR=$"Mail"
FORA_STR=$"Fora"
ZERONET_BLOG_STR=$"ZeroNet Blog"
ZERONET_MAIL_STR=$"ZeroNet Mail"
ZERONET_FORUM_STR=$"ZeroNet Forum"
NO_USERS_STR=$"No users found"
function create_index {
if [ -f $ZERONET_INDEX ]; then
2015-12-11 18:48:15 +01:00
sed -i "s|.*${MY_BLOG_STR}.*| <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/myblog)\">${MY_BLOG_STR}</a></li>|g" $ZERONET_INDEX
sed -i "s|.*${MY_MAIL_STR}.*| <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/mymail)\">${MY_MAIL_STR}</a></li>|g" $ZERONET_INDEX
sed -i "s|.*${MY_FORUM_STR}.*| <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/myforum)\">${MY_FORUM_STR}</a></li>|g" $ZERONET_INDEX
return
fi
echo '<!DOCTYPE html>' > $ZERONET_INDEX
echo '<html>' >> $ZERONET_INDEX
echo '<head>' >> $ZERONET_INDEX
echo "<title>$WEB_PAGE_TITLE</title>" >> $ZERONET_INDEX
echo '</head>' >> $ZERONET_INDEX
echo '<body>' >> $ZERONET_INDEX
echo "<H1>$WEB_PAGE_TITLE</H1>" >> $ZERONET_INDEX
echo '' >> $ZERONET_INDEX
2015-09-03 13:09:09 +02:00
echo "<H2>Your Sites</H2>" >> $ZERONET_INDEX
echo '' >> $ZERONET_INDEX
echo '<ol type="square">' >> $ZERONET_INDEX
2015-12-11 18:48:15 +01:00
echo " <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/myblog)\">${MY_BLOG_STR}</a></li>" >> $ZERONET_INDEX
echo " <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/mymail)\">${MY_MAIL_STR}</a></li>" >> $ZERONET_INDEX
echo " <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/myforum)\">${MY_FORUM_STR}</a></li>" >> $ZERONET_INDEX
# echo " <li><a href=\"http://localhost:5001/webui\">File Share</a></li>" >> $ZERONET_INDEX
2015-09-03 13:09:09 +02:00
echo '</ol>' >> $ZERONET_INDEX
echo '' >> $ZERONET_INDEX
echo "<H2>On the Mesh</H2>" >> $ZERONET_INDEX
echo '' >> $ZERONET_INDEX
echo '<ol type="square">' >> $ZERONET_INDEX
2015-12-11 18:48:15 +01:00
echo " <li><a href=\"${BLOGS_FILE}.html\">${BLOGS_STR}</a></li>" >> $ZERONET_INDEX
echo " <li><a href=\"${MAIL_FILE}.html\">${MAIL_STR}</a></li>" >> $ZERONET_INDEX
echo " <li><a href=\"${FORUM_FILE}.html\">${FORA_STR}</a></li>" >> $ZERONET_INDEX
2015-09-04 22:28:11 +02:00
echo " <li><a href=\"${TOX_USERS_FILE}.html\">Tox Users</a></li>" >> $ZERONET_INDEX
echo '</ol>' >> $ZERONET_INDEX
echo '' >> $ZERONET_INDEX
echo '</body>' >> $ZERONET_INDEX
echo '</html>' >> $ZERONET_INDEX
}
function create_header {
header_file=${1}-header.html
if [ -f $header_file ]; then
return
fi
echo '<!DOCTYPE html>' > $header_file
echo '<html>' >> $header_file
echo '<head>' >> $header_file
echo "<title>$WEB_PAGE_TITLE - $2</title>" >> $header_file
echo '<meta http-equiv="refresh" content="60">' >> $header_file
echo '</head>' >> $header_file
echo '<body>' >> $header_file
echo "<H1>$2</H1>" >> $header_file
}
function create_footer {
2015-09-03 13:20:32 +02:00
footer_file=${1}-footer.html
if [ -f $footer_file ]; then
return
fi
echo '</body>' >> $footer_file
echo '</html>' >> $footer_file
}
2015-09-03 10:18:26 +02:00
2015-08-29 17:26:51 +02:00
if [ ! -d /etc/avahi ]; then
exit 0
fi
# Create a list of bootstrap nodes
TEMPFILE_BASE=/tmp/tmpzeronetavahibase.txt
2015-08-29 17:26:51 +02:00
TEMPFILE=/tmp/tmpzeronetavahi.txt
avahi-browse -atr > $TEMPFILE_BASE
2015-09-11 20:36:04 +02:00
cat $TEMPFILE_BASE | grep "hostname =\|address =\|port =" > $TEMPFILE
2015-08-29 17:26:51 +02:00
if [ ! -f $TEMPFILE ]; then
exit 1
fi
if [ -f $ZERONET_CONFIG.new ]; then
rm -f $ZERONET_CONFIG.new
fi
2015-09-12 20:21:48 +02:00
touch $ZERONET_CONFIG.new
2015-08-29 17:26:51 +02:00
state=0
address=""
peer=""
tracker_ctr=0
2015-08-29 17:26:51 +02:00
while IFS='' read -r line || [[ -n "$line" ]]; do
2015-09-11 20:36:04 +02:00
if [ ${state} -eq "1" ]; then
2015-08-29 17:26:51 +02:00
if [[ $line == *"address ="* ]]; then
address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
2015-09-11 23:39:18 +02:00
tracker_url="http://$peer:$TRACKER_PORT/announce"
if ! grep -q "$tracker_url" $ZERONET_CONFIG.new; then
2015-09-12 19:48:34 +02:00
echo "$tracker_url" >> $ZERONET_CONFIG.new
2015-09-11 23:39:18 +02:00
tracker_ctr=$((tracker_ctr + 1))
fi
2015-08-29 17:38:24 +02:00
state=0
2015-08-29 17:26:51 +02:00
fi
fi
2015-09-11 20:36:04 +02:00
if [[ $line == *"hostname ="* ]]; then
peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
2015-08-29 17:26:51 +02:00
state=1
fi
done < "$TEMPFILE"
# detect blogs in the mesh
if [ -f $BLOGS_FILE.new ]; then
rm -f $BLOGS_FILE.new
fi
2015-12-11 18:48:15 +01:00
cat $TEMPFILE_BASE | grep "${ZERONET_BLOG_STR}\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
state=0
address=""
peer=""
echo '<ol type="square">' >> $BLOGS_FILE.new
while IFS='' read -r line || [[ -n "$line" ]]; do
if [ ${state} -eq "3" ]; then
2015-09-03 16:36:48 +02:00
if [[ $line == *"txt ="* ]]; then
blog_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
2015-09-03 16:36:48 +02:00
echo " <li><a href=${blog_url}>${peer}</a></li>" >> $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
2015-12-11 18:48:15 +01:00
if [[ $line == *"${ZERONET_BLOG_STR}"* && $line == "= "* ]]; then
state=1
fi
done < "$TEMPFILE"
echo '</ol>' >> $BLOGS_FILE.new
2015-12-11 18:15:52 +01:00
# detect mail in the mesh
if [ -f $MAIL_FILE.new ]; then
rm -f $MAIL_FILE.new
fi
2015-12-11 18:48:15 +01:00
cat $TEMPFILE_BASE | grep "${ZERONET_MAIL_STR}\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
2015-12-11 18:15:52 +01:00
state=0
address=""
peer=""
echo '<ol type="square">' >> $MAIL_FILE.new
while IFS='' read -r line || [[ -n "$line" ]]; do
if [ ${state} -eq "3" ]; then
if [[ $line == *"txt ="* ]]; then
mail_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
echo " <li><a href=${mail_url}>${peer}</a></li>" >> $MAIL_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
2015-12-11 18:48:15 +01:00
if [[ $line == *"${ZERONET_MAIL_STR}"* && $line == "= "* ]]; then
2015-12-11 18:15:52 +01:00
state=1
fi
done < "$TEMPFILE"
echo '</ol>' >> $MAIL_FILE.new
# detect fora in the mesh
if [ -f $FORUM_FILE.new ]; then
rm -f $FORUM_FILE.new
fi
2015-12-11 18:48:15 +01:00
cat $TEMPFILE_BASE | grep "${ZERONET_FORUM_STR}\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
state=0
address=""
peer=""
echo '<ol type="square">' >> $FORUM_FILE.new
while IFS='' read -r line || [[ -n "$line" ]]; do
if [ ${state} -eq "3" ]; then
2015-09-03 16:36:48 +02:00
if [[ $line == *"txt ="* ]]; then
forum_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
2015-09-03 16:36:48 +02:00
echo " <li><a href=${forum_url}>${peer}</a></li>" >> $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
2015-12-11 18:48:15 +01:00
if [[ $line == *"${ZERONET_FORUM_STR}"* && $line == "= "* ]]; then
state=1
fi
done < "$TEMPFILE"
echo '<ol>' >> $FORUM_FILE.new
2015-09-30 20:29:09 +02:00
2015-09-30 23:25:01 +02:00
# IPFS bootstrap
2015-09-30 20:29:09 +02:00
cat $TEMPFILE_BASE | grep "IPFS\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
state=0
address=""
peer=""
while IFS='' read -r line || [[ -n "$line" ]]; do
if [ ${state} -eq "3" ]; then
if [[ $line == *"txt ="* ]]; then
2015-09-30 21:54:28 +02:00
ipfs_peer_id=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}' | awk -F '"' '{print $2}')
2015-09-30 22:01:16 +02:00
ipfs bootstrap add /ip4/${address}/tcp/${IPFS_PORT}/ipfs/${ipfs_peer_id}
2015-09-30 20:29:09 +02:00
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 == *"IPFS"* && $line == "= "* ]]; then
state=1
fi
done < "$TEMPFILE"
2015-09-04 22:28:11 +02:00
# detect Tox users
lstox > $TEMPFILE
toxctr=0
echo '<ol type="square">' >> $TOX_USERS_FILE.new
while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ $line != "Failed*" ]]; then
echo " <li>$line</li>" >> $TOX_USERS_FILE.new
toxctr=$((toxctr + 1))
fi
done < "$TEMPFILE"
if [ $toxctr -eq "0" ]; then
2015-12-11 18:48:15 +01:00
echo " <li>${NO_USERS_STR}</li>" >> $TOX_USERS_FILE.new
2015-09-04 22:28:11 +02:00
fi
echo '<ol>' >> $TOX_USERS_FILE.new
rm -f $TEMPFILE_BASE
2015-08-29 17:26:51 +02:00
rm -f $TEMPFILE
if [ ${tracker_ctr} -gt "0" ]; then
cp -f $ZERONET_CONFIG.new $ZERONET_CONFIG
else
if [ -f $ZERONET_CONFIG ]; then
rm -f $ZERONET_CONFIG
fi
fi
rm -f $ZERONET_CONFIG.new
2015-09-03 20:06:44 +02:00
if [ -d $ZERONET_USER_HOME ]; then
sudo chown zeronet:zeronet $ZERONET_CONFIG
2015-08-31 13:20:57 +02:00
fi
2015-08-29 17:26:51 +02:00
# make some html headers and footers
2015-12-11 18:48:15 +01:00
create_header $BLOGS_FILE $BLOGS_STR
create_header $MAIL_FILE $MAIL_STR
create_header $FORUM_FILE $FORA_STR
create_header $TOX_USERS_FILE $"Tox Users"
create_footer $BLOGS_FILE
2015-12-11 18:15:52 +01:00
create_footer $MAIL_FILE
create_footer $FORUM_FILE
2015-09-04 22:28:11 +02:00
create_footer $TOX_USERS_FILE
# make the index page
create_index
# create a web page showing the available blogs
cat ${BLOGS_FILE}-header.html ${BLOGS_FILE}.new ${BLOGS_FILE}-footer.html > ${BLOGS_FILE}.html
rm -f ${BLOGS_FILE}.new
2015-09-03 20:06:44 +02:00
if [ -d $ZERONET_USER_HOME ]; then
sudo chown zeronet:zeronet $BLOGS_FILE
fi
2015-12-11 18:15:52 +01:00
# create a web page showing the available mail contacts
cat ${MAIL_FILE}-header.html ${MAIL_FILE}.new ${MAIL_FILE}-footer.html > ${MAIL_FILE}.html
rm -f ${MAIL_FILE}.new
if [ -d $ZERONET_USER_HOME ]; then
sudo chown zeronet:zeronet $MAIL_FILE
fi
# create a web page showing the available fora
cat ${FORUM_FILE}-header.html ${FORUM_FILE}.new ${FORUM_FILE}-footer.html > ${FORUM_FILE}.html
rm -f ${FORUM_FILE}.new
2015-09-03 20:06:44 +02:00
if [ -d $ZERONET_USER_HOME ]; then
sudo chown zeronet:zeronet $FORUM_FILE
fi
2015-09-04 22:28:11 +02:00
# create a web page showing Tox users
cat ${TOX_USERS_FILE}-header.html ${TOX_USERS_FILE}.new ${TOX_USERS_FILE}-footer.html > ${TOX_USERS_FILE}.html
rm -f ${TOX_USERS_FILE}.new
if [ -d $ZERONET_USER_HOME ]; then
sudo chown zeronet:zeronet $TOX_USERS_FILE
fi
2015-08-29 17:26:51 +02:00
exit 0