#!/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 # License # ======= # # Copyright (C) 2015-2016 Bob Mottram # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero 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 Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . PROJECT_NAME='freedombone' export TEXTDOMAIN=$PROJECT_NAME-zeronetavahi export TEXTDOMAINDIR="/usr/share/locale" WEB_PAGE_TITLE=$"${PROJECT_NAME} Mesh" TRACKER_PORT=6969 ZERONET_INSTALL=/opt/zeronet ZERONET_USER_HOME=/home/zeronet MY_USERNAME=$USER if [ ! -d $ZERONET_INSTALL ]; then if [ -d /home/$MY_USERNAME/zeronet ]; then ZERONET_INSTALL=/home/$MY_USERNAME/zeronet else exit 0 fi fi BLOGS_FILE=$ZERONET_INSTALL/${PROJECT_NAME}-blogs MAIL_FILE=$ZERONET_INSTALL/${PROJECT_NAME}-mail FORUM_FILE=$ZERONET_INSTALL/${PROJECT_NAME}-fora TOX_USERS_FILE=$ZERONET_INSTALL/${PROJECT_NAME}-tox-users ZERONET_INDEX=/home/$MY_USERNAME/mesh.html ZERONET_CONFIG=$ZERONET_INSTALL/bootstrap IPFS_PORT=4001 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 sed -i "s|.*${MY_BLOG_STR}.*|
  • ${MY_BLOG_STR}
  • |g" $ZERONET_INDEX sed -i "s|.*${MY_MAIL_STR}.*|
  • ${MY_MAIL_STR}
  • |g" $ZERONET_INDEX sed -i "s|.*${MY_FORUM_STR}.*|
  • ${MY_FORUM_STR}
  • |g" $ZERONET_INDEX return fi echo '' > $ZERONET_INDEX echo '' >> $ZERONET_INDEX echo '' >> $ZERONET_INDEX echo "$WEB_PAGE_TITLE" >> $ZERONET_INDEX echo '' >> $ZERONET_INDEX echo '' >> $ZERONET_INDEX echo "

    $WEB_PAGE_TITLE

    " >> $ZERONET_INDEX echo '' >> $ZERONET_INDEX echo "

    Your Sites

    " >> $ZERONET_INDEX echo '' >> $ZERONET_INDEX echo '
      ' >> $ZERONET_INDEX echo "
    1. ${MY_BLOG_STR}
    2. " >> $ZERONET_INDEX echo "
    3. ${MY_MAIL_STR}
    4. " >> $ZERONET_INDEX echo "
    5. ${MY_FORUM_STR}
    6. " >> $ZERONET_INDEX # echo "
    7. File Share
    8. " >> $ZERONET_INDEX echo '
    ' >> $ZERONET_INDEX echo '' >> $ZERONET_INDEX echo "

    On the Mesh

    " >> $ZERONET_INDEX echo '' >> $ZERONET_INDEX echo '
      ' >> $ZERONET_INDEX echo "
    1. ${BLOGS_STR}
    2. " >> $ZERONET_INDEX echo "
    3. ${MAIL_STR}
    4. " >> $ZERONET_INDEX echo "
    5. ${FORA_STR}
    6. " >> $ZERONET_INDEX echo "
    7. Tox Users
    8. " >> $ZERONET_INDEX echo '
    ' >> $ZERONET_INDEX echo '' >> $ZERONET_INDEX echo '' >> $ZERONET_INDEX echo '' >> $ZERONET_INDEX } function create_header { header_file=${1}-header.html if [ -f $header_file ]; then return fi echo '' > $header_file echo '' >> $header_file echo '' >> $header_file echo "$WEB_PAGE_TITLE - $2" >> $header_file echo '' >> $header_file echo '' >> $header_file echo '' >> $header_file echo "

    $2

    " >> $header_file } function create_footer { footer_file=${1}-footer.html if [ -f $footer_file ]; then return fi echo '' >> $footer_file echo '' >> $footer_file } if [ ! -d /etc/avahi ]; then exit 0 fi # Create a list of bootstrap nodes TEMPFILE_BASE=/tmp/tmpzeronetavahibase.txt TEMPFILE=/tmp/tmpzeronetavahi.txt avahi-browse -atr > $TEMPFILE_BASE cat $TEMPFILE_BASE | grep "hostname =\|address =\|port =" > $TEMPFILE if [ ! -f $TEMPFILE ]; then exit 1 fi if [ -f $ZERONET_CONFIG.new ]; then rm -f $ZERONET_CONFIG.new fi touch $ZERONET_CONFIG.new state=0 address="" peer="" tracker_ctr=0 while IFS='' read -r line || [[ -n "$line" ]]; do if [ ${state} -eq "1" ]; then if [[ $line == *"address ="* ]]; then address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}') tracker_url="http://$peer:$TRACKER_PORT/announce" if ! grep -q "$tracker_url" $ZERONET_CONFIG.new; then echo "$tracker_url" >> $ZERONET_CONFIG.new tracker_ctr=$((tracker_ctr + 1)) fi state=0 fi fi if [[ $line == *"hostname ="* ]]; then peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}') 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_STR}\|hostname =\|address =\|port =\|txt =" > $TEMPFILE state=0 address="" peer="" echo '
      ' >> $BLOGS_FILE.new while IFS='' read -r line || [[ -n "$line" ]]; do if [ ${state} -eq "3" ]; then if [[ $line == *"txt ="* ]]; then blog_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}') echo "
    1. ${peer}
    2. " >> $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_STR}"* && $line == "= "* ]]; then state=1 fi done < "$TEMPFILE" echo '
    ' >> $BLOGS_FILE.new # detect mail in the mesh if [ -f $MAIL_FILE.new ]; then rm -f $MAIL_FILE.new fi cat $TEMPFILE_BASE | grep "${ZERONET_MAIL_STR}\|hostname =\|address =\|port =\|txt =" > $TEMPFILE state=0 address="" peer="" echo '
      ' >> $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 "
    1. ${peer}
    2. " >> $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 if [[ $line == *"${ZERONET_MAIL_STR}"* && $line == "= "* ]]; then state=1 fi done < "$TEMPFILE" echo '
    ' >> $MAIL_FILE.new # detect fora in the mesh if [ -f $FORUM_FILE.new ]; then rm -f $FORUM_FILE.new fi cat $TEMPFILE_BASE | grep "${ZERONET_FORUM_STR}\|hostname =\|address =\|port =\|txt =" > $TEMPFILE state=0 address="" peer="" echo '
      ' >> $FORUM_FILE.new while IFS='' read -r line || [[ -n "$line" ]]; do if [ ${state} -eq "3" ]; then if [[ $line == *"txt ="* ]]; then forum_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}') echo "
    1. ${peer}
    2. " >> $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_STR}"* && $line == "= "* ]]; then state=1 fi done < "$TEMPFILE" echo '
        ' >> $FORUM_FILE.new # IPFS bootstrap 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 ipfs_peer_id=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}' | awk -F '"' '{print $2}') ipfs bootstrap add /ip4/${address}/tcp/${IPFS_PORT}/ipfs/${ipfs_peer_id} 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" # detect Tox users lstox > $TEMPFILE toxctr=0 echo '
          ' >> $TOX_USERS_FILE.new while IFS='' read -r line || [[ -n "$line" ]]; do if [[ $line != "Failed*" ]]; then echo "
        1. $line
        2. " >> $TOX_USERS_FILE.new toxctr=$((toxctr + 1)) fi done < "$TEMPFILE" if [ $toxctr -eq "0" ]; then echo "
        3. ${NO_USERS_STR}
        4. " >> $TOX_USERS_FILE.new fi echo '
            ' >> $TOX_USERS_FILE.new rm -f $TEMPFILE_BASE 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 if [ -d $ZERONET_USER_HOME ]; then sudo chown zeronet:zeronet $ZERONET_CONFIG fi # make some html headers and footers 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 create_footer $MAIL_FILE create_footer $FORUM_FILE 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 if [ -d $ZERONET_USER_HOME ]; then sudo chown zeronet:zeronet $BLOGS_FILE fi # 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 if [ -d $ZERONET_USER_HOME ]; then sudo chown zeronet:zeronet $FORUM_FILE fi # 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 exit 0