| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 | #!/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 <bob@robotics.uk.to>
#
# 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 <http://www.gnu.org/licenses/>.
PROJECT_NAME='freedombone'
export TEXTDOMAIN=$PROJECT_NAME-zeronetavahi
export TEXTDOMAINDIR="/usr/share/locale"
MESH_INSTALL_DIR=/var/lib
WEB_PAGE_TITLE=$"${PROJECT_NAME} Mesh"
TRACKER_PORT=6969
ZERONET_INSTALL=$MESH_INSTALL_DIR/zeronet
MESH_USERNAME='fbone'
MY_USERNAME=$MESH_USERNAME
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
ID_FILE=$ZERONET_INSTALL/${PROJECT_NAME}-id
ZERONET_INDEX=$ZERONET_INSTALL/mesh.html
ZERONET_CONFIG=$ZERONET_INSTALL/bootstrap
IPFS_PORT=4001
IPFS_PATH=/usr/bin
IPFS_COMMAND=$IPFS_PATH/ipfs
IPFS_USERS_FILE=/home/$MY_USERNAME/.ipfs-users
IPFS_PUBLIC=/home/$MY_USERNAME/.ipfs-public
MY_BLOG_STR=$"My Blog"
MY_MAIL_STR=$"My Mail"
MY_FORUM_STR=$"My Forum"
MY_ID_STR=$"My ID Service"
YOUR_SITES_STR=$'Your Sites'
ON_THE_MESH_STR=$'On the Mesh'
BLOGS_STR=$"Blogs"
MAIL_STR=$"Mail"
FORA_STR=$"Fora"
TOX_STR=$"Users on the Mesh"
ID_STR=$"ID Services"
NO_USERS_STR=$"No users found"
# service names advertised by avahi
ENABLE_ZERONET=
ZERONET_BLOG_STR="ZeroNet Blog"
ZERONET_MAIL_STR="ZeroNet Mail"
ZERONET_FORUM_STR="ZeroNet Forum"
ZERONET_ID_STR="ZeroNet ID Service"
# contains the output of the avahi command
TEMPFILE_BASE=$(mktemp /tmp/zeronetavahibase.XXXXXX)
TEMPFILE=$(mktemp /tmp/zeronetavahi.XXXXXX)
# List of tox users previously seen
PREV_TOX_USERS_FILE=/root/.prev_tox_users
function ipfs_publish {
    # Publishes anything within the ~/Public directory
    DIR_TO_CHECK=/home/$MY_USERNAME/Public
    if [ ! -d $DIR_TO_CHECK ]; then
        return
    fi
    OLD_STAT_FILE=/home/$MY_USERNAME/.old_stat.txt
    if [ -e $OLD_STAT_FILE ]
    then
        OLD_STAT=$(cat $OLD_STAT_FILE)
    else
        OLD_STAT="nothing"
    fi
    NEW_STAT=$(stat -t $DIR_TO_CHECK)
    if [ "$OLD_STAT" != "$NEW_STAT" ]; then
        su -c "echo \$($IPFS_COMMAND add -rq /home/$MY_USERNAME/Public | tail -n 1) > $IPFS_PUBLIC" - $MY_USERNAME
        echo "$NEW_STAT" > $OLD_STAT_FILE
        chown $MY_USERNAME:$MY_USERNAME $OLD_STAT_FILE
    fi
    if [ -f $IPFS_PUBLIC ]; then
        IPFS_PUBLIC_ID=$(cat $IPFS_PUBLIC)
        su -c "$IPFS_COMMAND name publish /ipfs/$IPFS_PUBLIC_ID" - $MY_USERNAME
    fi
}
function ipfs_bootstrap {
    cat $TEMPFILE_BASE | grep "ipfs_id\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
    state=0
    address=""
    peer=""
    if [ -d /home/$MY_USERNAME/Desktop ]; then
        echo -n '' > ${IPFS_USERS_FILE}.new
    fi
    while IFS='' read -r line || [[ -n "$line" ]]; do
        if [ ${state} -eq "3" ]; then
            if [[ $line == *"txt ="* ]]; then
                ipfs_txt=$(echo "$line" | awk -F '[' '{print $2}' | awk -F ']' '{print $1}' | awk -F '"' '{print $2}')
                ipfs_peer_id=$(echo "$ipfs_txt" | awk -F ':' '{print $1}')
                ipfs_tox_id=$(echo "$ipfs_txt" | awk -F ':' '{print $2}')
                su -c "$IPFS_COMMAND bootstrap add /ip4/${address}/tcp/${IPFS_PORT}/ipfs/${ipfs_peer_id}" - $MY_USERNAME
                if [ -d /home/$MY_USERNAME/Desktop ]; then
                    if [[ $ipfs_tox_id != 'none' ]]; then
                        echo "$ipfs_tox_id:$ipfs_peer_id" >> ${IPFS_USERS_FILE}.new
                    fi
                fi
                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_id"* && $line == "= "* ]]; then
            state=1
        fi
    done < "$TEMPFILE"
    # Create a list of user sites, in alphabetical order by Tox nick
    if [ -d /home/$MY_USERNAME/Desktop ]; then
        if [ -f ${IPFS_USERS_FILE}.new ]; then
            sites_list=$(cat ${IPFS_USERS_FILE}.new | sort -d)
            echo "${sites_list}" > ${IPFS_USERS_FILE}
            chown $MY_USERNAME:$MY_USERNAME ${IPFS_USERS_FILE}
            rm ${IPFS_USERS_FILE}.new
        fi
    fi
}
function detect_new_tox_users {
    CURRENT_USERS_FILE=$1
    if [ ! -f $CURRENT_USERS_FILE ]; then
        return
    fi
    # Check that this is a GUI installation with a desktop
    if [ ! -d /home/$MESH_USERNAME/Desktop ]; then
        return
    fi
    # Produce notifications if new users appear
    if [ -f $PREV_TOX_USERS_FILE ]; then
        while IFS='' read -r line || [[ -n "$line" ]]; do
            if [[ $line != "Failed*" && $line != "data "* ]]; then
                if ! grep -q "$line" $PREV_TOX_USERS_FILE; then
                    # get the nick of the user
                    toxuser="$(echo "$line" | awk -F ' ' '{print $1}')"
                    if [ -r "/home/$MESH_USERNAME/.dbus/Xdbus" ]; then
                        . "/home/$MESH_USERNAME/.dbus/Xdbus"
                    fi
                    export DISPLAY=:0.0
                    export XAUTHORITY=/home/$MESH_USERNAME/.Xauthority
                    sudo -u $MESH_USERNAME /usr/bin/notify-send $"$toxuser" $"has joined the mesh" --icon=dialog-information
                    break
                fi
            fi
        done < "$CURRENT_USERS_FILE"
    fi
    # Store the previous tox users list
    cp -f $CURRENT_USERS_FILE $PREV_TOX_USERS_FILE
}
function detect_tox_users {
    # don't show the first peer field
    lstox | awk -F ' ' '{$1=""; print $0}' | sed -e 's/^[[:space:]]*//' | sort -d > $TEMPFILE
    detect_new_tox_users $TEMPFILE
    toxctr=0
    echo '<ol type="square">' >> $TOX_USERS_FILE.new
    while IFS='' read -r line || [[ -n "$line" ]]; do
        if [[ $line != "Failed*" && $line != "data "* ]]; then
            echo "  <li>$line</li>" >> $TOX_USERS_FILE.new
            toxctr=$((toxctr + 1))
        fi
    done < "$TEMPFILE"
    if [ $toxctr -eq "0" ]; then
        echo "  <li>${NO_USERS_STR}</li>" >> $TOX_USERS_FILE.new
    fi
    echo '<ol>' >> $TOX_USERS_FILE.new
}
function assemble_web_page {
    f="$1"
    fhome="$2"
    cat "${f}-header.html" "${f}.new" "${f}-footer.html" > "${f}.html"
    rm -f "${f}.new"
    if [ -d "${fhome}" ]; then
        sudo chown zeronet:zeronet "${f}.html"
    fi
}
function create_index {
    blogaddress=$(cat /home/$MY_USERNAME/.config/zeronet/myblog)
    mailaddress=$(cat /home/$MY_USERNAME/.config/zeronet/mymail)
    forumaddress=$(cat /home/$MY_USERNAME/.config/zeronet/myforum)
    idaddress=$(cat /home/$MY_USERNAME/.config/zeronet/myzeroid)
    if [ -f $ZERONET_INDEX ]; then
        sed -i "s|.*${MY_BLOG_STR}.*|  <li><a href=\"${blogaddress}\">${MY_BLOG_STR}</a></li>|g" $ZERONET_INDEX
        sed -i "s|.*${MY_MAIL_STR}.*|  <li><a href=\"${mailaddress}\">${MY_MAIL_STR}</a></li>|g" $ZERONET_INDEX
        sed -i "s|.*${MY_FORUM_STR}.*|  <li><a href=\"${forumaddress}\">${MY_FORUM_STR}</a></li>|g" $ZERONET_INDEX
        sed -i "s|.*${MY_ID_STR}.*|  <li><a href=\"${idaddress}\">${MY_ID_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
    echo "<H2>${YOUR_SITES_STR}</H2>" >> $ZERONET_INDEX
    echo '' >> $ZERONET_INDEX
    echo '<ol type="square">' >> $ZERONET_INDEX
    echo "  <li><a href=\"${blogaddress}\">${MY_BLOG_STR}</a></li>" >> $ZERONET_INDEX
    echo "  <li><a href=\"${mailaddress}\">${MY_MAIL_STR}</a></li>" >> $ZERONET_INDEX
    echo "  <li><a href=\"${forumaddress}\">${MY_FORUM_STR}</a></li>" >> $ZERONET_INDEX
    echo "  <li><a href=\"${idaddress}\">${MY_ID_STR}</a></li>" >> $ZERONET_INDEX
#    echo "  <li><a href=\"http://localhost:5001/webui\">File Share</a></li>" >> $ZERONET_INDEX
    echo '</ol>' >> $ZERONET_INDEX
    echo '' >> $ZERONET_INDEX
    echo "<H2>${ON_THE_MESH_STR}</H2>" >> $ZERONET_INDEX
    echo '' >> $ZERONET_INDEX
    echo '<ol type="square">' >> $ZERONET_INDEX
    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
    echo "  <li><a href=\"${ID_FILE}.html\">${ID_STR}</a></li>" >> $ZERONET_INDEX
    echo "  <li><a href=\"${TOX_USERS_FILE}.html\">${TOX_STR}</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 {
    footer_file=${1}-footer.html
    if [ -f $footer_file ]; then
        return
    fi
    echo '</body>' >> $footer_file
    echo '</html>' >> $footer_file
}
function avahi_extract_info {
    # Create a list of bootstrap nodes
    avahi-browse -atr > $TEMPFILE_BASE
    cat $TEMPFILE_BASE | grep "hostname =\|address =\|port =" > $TEMPFILE
    if [ ! -f $TEMPFILE ]; then
        exit 1
    fi
}
function zeronet_detect {
    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"
}
function zeronet_detect_blog {
    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 '<ol type="square">' >> $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 "  <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
        if [[ $line == *"${ZERONET_BLOG_STR}"* && $line == "= "* ]]; then
            state=1
        fi
    done < "$TEMPFILE"
    echo '</ol>' >> $BLOGS_FILE.new
}
function zeronet_detect_mail {
    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 '<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
        if [[ $line == *"${ZERONET_MAIL_STR}"* && $line == "= "* ]]; then
            state=1
        fi
    done < "$TEMPFILE"
    echo '</ol>' >> $MAIL_FILE.new
}
function zeronet_detect_fora {
    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 '<ol type="square">' >> $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 "  <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
        if [[ $line == *"${ZERONET_FORUM_STR}"* && $line == "= "* ]]; then
            state=1
        fi
    done < "$TEMPFILE"
    echo '<ol>' >> $FORUM_FILE.new
}
function zeronet_detect_id {
    if [ -f $ID_FILE.new ]; then
        rm -f $ID_FILE.new
    fi
    cat $TEMPFILE_BASE | grep "${ZERONET_ID_STR}\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
    state=0
    address=""
    peer=""
    echo '<ol type="square">' >> $ID_FILE.new
    while IFS='' read -r line || [[ -n "$line" ]]; do
        if [ ${state} -eq "3" ]; then
            if [[ $line == *"txt ="* ]]; then
                id_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
                echo "  <li><a href=${id_url}>${peer}</a></li>" >> $ID_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_ID_STR}"* && $line == "= "* ]]; then
            state=1
        fi
    done < "$TEMPFILE"
    echo '</ol>' >> $ID_FILE.new
}
function avahi_remove_info {
    rm -f $TEMPFILE_BASE
    rm -f $TEMPFILE
}
function zeronet_update_config {
    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_INSTALL ]; then
        sudo chown zeronet:zeronet $ZERONET_CONFIG
    fi
}
if [ ! -d /etc/avahi ]; then
    exit 0
fi
avahi_extract_info
if [ $ENABLE_ZERONET ]; then
    zeronet_detect
    zeronet_detect_id
    zeronet_detect_blog
    zeronet_detect_mail
    zeronet_detect_fora
    zeronet_update_config
fi
ipfs_bootstrap
ipfs_publish
#detect_tox_users
avahi_remove_info
# make some html headers and footers
if [ $ENABLE_ZERONET ]; then
    create_header $ID_FILE $ID_STR
    create_header $BLOGS_FILE $BLOGS_STR
    create_header $MAIL_FILE $MAIL_STR
    create_header $FORUM_FILE $FORA_STR
    create_footer $ID_FILE
    create_footer $BLOGS_FILE
    create_footer $MAIL_FILE
    create_footer $FORUM_FILE
    # make the index page
    create_index
    assemble_web_page ${ID_FILE} $ZERONET_INSTALL
    assemble_web_page ${BLOGS_FILE} $ZERONET_INSTALL
    assemble_web_page ${MAIL_FILE} $ZERONET_INSTALL
    assemble_web_page ${FORUM_FILE} $ZERONET_INSTALL
    chown -R zeronet:zeronet $ZERONET_INSTALL
fi
#create_header $TOX_USERS_FILE $"Tox Users"
#create_footer $TOX_USERS_FILE
#assemble_web_page ${TOX_USERS_FILE} $ZERONET_INSTALL
exit 0
 |