meshavahi 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # A script for using avahi to discover peers and update tox/ipfs
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=$PROJECT_NAME-meshavahi
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. MESH_USERNAME='fbone'
  33. MY_USERNAME=$MESH_USERNAME
  34. IPFS_PORT=4001
  35. IPFS_PATH=/usr/bin
  36. IPFS_COMMAND=$IPFS_PATH/ipfs
  37. IPFS_USERS_FILE=/tmp/.ipfs-users
  38. IPFS_PUBLIC=/home/$MY_USERNAME/.ipfs-public
  39. # contains the output of the avahi command
  40. TEMPFILE_BASE=$(mktemp /tmp/meshavahibase.XXXXXX)
  41. TEMPFILE=$(mktemp /tmp/meshavahi.XXXXXX)
  42. # List of tox users previously seen
  43. PREV_TOX_USERS_FILE=/root/.prev_tox_users
  44. function ipfs_publish {
  45. # Publishes anything within the ~/Public directory
  46. DIR_TO_CHECK=/home/$MY_USERNAME/Public
  47. if [ ! -d $DIR_TO_CHECK ]; then
  48. return
  49. fi
  50. OLD_STAT_FILE=/home/$MY_USERNAME/.old_stat.txt
  51. if [ -e $OLD_STAT_FILE ]
  52. then
  53. OLD_STAT=$(cat $OLD_STAT_FILE)
  54. else
  55. OLD_STAT="nothing"
  56. fi
  57. NEW_STAT=$(stat -t $DIR_TO_CHECK)
  58. # include some subdirectories
  59. for dir in $DIR_TO_CHECK/*/
  60. do
  61. REALLY_NEW_STAT="$NEW_STAT$(stat -t $dir)"
  62. NEW_STAT="$REALLY_NEW_STAT"
  63. for dir2 in "$dir"/*/
  64. do
  65. REALLY_NEW_STAT="$NEW_STAT$(stat -t $dir2)"
  66. NEW_STAT="$REALLY_NEW_STAT"
  67. for dir3 in "$dir2"/*/
  68. do
  69. REALLY_NEW_STAT="$NEW_STAT$(stat -t $dir3)"
  70. NEW_STAT="$REALLY_NEW_STAT"
  71. done
  72. done
  73. done
  74. if [ "$OLD_STAT" != "$NEW_STAT" ]; then
  75. su -c "echo \$($IPFS_COMMAND add -rq /home/$MY_USERNAME/Public | tail -n 1) > $IPFS_PUBLIC" - $MY_USERNAME
  76. echo "$NEW_STAT" > $OLD_STAT_FILE
  77. chown $MY_USERNAME:$MY_USERNAME $OLD_STAT_FILE
  78. fi
  79. if [ -f $IPFS_PUBLIC ]; then
  80. IPFS_PUBLIC_ID=$(cat $IPFS_PUBLIC)
  81. su -c "$IPFS_COMMAND name publish /ipfs/$IPFS_PUBLIC_ID" - $MY_USERNAME
  82. fi
  83. }
  84. function ipfs_bootstrap {
  85. # TODO switch to ipv6
  86. cat $TEMPFILE_BASE | grep "ipfs_id\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
  87. state=0
  88. address=""
  89. peer=""
  90. if [ -d /home/$MY_USERNAME/Desktop ]; then
  91. echo -n '' > ${IPFS_USERS_FILE}.new
  92. fi
  93. while IFS='' read -r line || [[ -n "$line" ]]; do
  94. if [ ${state} -eq "3" ]; then
  95. if [[ $line == *"txt ="* ]]; then
  96. ipfs_txt=$(echo "$line" | awk -F '[' '{print $2}' | awk -F ']' '{print $1}' | awk -F '"' '{print $2}')
  97. ipfs_peer_id=$(echo "$ipfs_txt" | awk -F ':' '{print $1}')
  98. ipfs_tox_id=$(echo "$ipfs_txt" | awk -F ':' '{print $2}')
  99. su -c "$IPFS_COMMAND bootstrap add /ip6/${address}/tcp/${IPFS_PORT}/ipfs/${ipfs_peer_id}" - $MY_USERNAME
  100. if [ -d /home/$MY_USERNAME/Desktop ]; then
  101. if [[ $ipfs_tox_id != 'none' ]]; then
  102. echo "$ipfs_tox_id:$ipfs_peer_id" >> ${IPFS_USERS_FILE}.new
  103. fi
  104. fi
  105. state=0
  106. fi
  107. fi
  108. if [ ${state} -eq "2" ]; then
  109. if [[ $line == *"address ="* ]]; then
  110. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  111. state=3
  112. fi
  113. fi
  114. if [ ${state} -eq "1" ]; then
  115. if [[ $line == *"hostname ="* ]]; then
  116. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  117. state=2
  118. fi
  119. fi
  120. if [[ $line == *"ipfs_id"* && $line == "= "* ]]; then
  121. state=1
  122. fi
  123. done < "$TEMPFILE"
  124. # Create a list of user sites, in alphabetical order by Tox nick
  125. if [ -d /home/$MY_USERNAME/Desktop ]; then
  126. if [ -f ${IPFS_USERS_FILE}.new ]; then
  127. sites_list=$(cat ${IPFS_USERS_FILE}.new | sort -d)
  128. echo "${sites_list}" > ${IPFS_USERS_FILE}
  129. chown $MY_USERNAME:$MY_USERNAME ${IPFS_USERS_FILE}
  130. rm ${IPFS_USERS_FILE}.new
  131. fi
  132. fi
  133. }
  134. function detect_new_tox_users {
  135. CURRENT_USERS_FILE=$1
  136. if [ ! -f $CURRENT_USERS_FILE ]; then
  137. return
  138. fi
  139. # Check that this is a GUI installation with a desktop
  140. if [ ! -d /home/$MESH_USERNAME/Desktop ]; then
  141. return
  142. fi
  143. # Produce notifications if new users appear
  144. if [ -f $PREV_TOX_USERS_FILE ]; then
  145. while IFS='' read -r line || [[ -n "$line" ]]; do
  146. if [[ $line != "Failed*" && $line != "data "* && $line != "Anon "* && $line != "anon "* && $line != "anonymous "* && $line != "Anonymous "* ]]; then
  147. if ! grep -q "$line" $PREV_TOX_USERS_FILE; then
  148. # get the nick of the user
  149. toxidstr=$(echo "$line" | awk -F ' ' '{print $(NF)}')
  150. toxuser=$(echo "$line" | sed "s| $toxidstr||g")
  151. if [ -r "/home/$MESH_USERNAME/.dbus/Xdbus" ]; then
  152. . "/home/$MESH_USERNAME/.dbus/Xdbus"
  153. fi
  154. export DISPLAY=:0.0
  155. export XAUTHORITY=/home/$MESH_USERNAME/.Xauthority
  156. sudo -u $MESH_USERNAME /usr/bin/notify-send $"$toxuser" $"has joined the mesh" --icon=dialog-information
  157. break
  158. fi
  159. fi
  160. done < "$CURRENT_USERS_FILE"
  161. fi
  162. # Store the previous tox users list
  163. cp -f $CURRENT_USERS_FILE $PREV_TOX_USERS_FILE
  164. }
  165. function detect_tox_users {
  166. # don't show the first peer field
  167. lstox | awk -F ' ' '{$1=""; print $0}' | sed -e 's/^[[:space:]]*//' | sort -d > $TEMPFILE
  168. detect_new_tox_users $TEMPFILE
  169. }
  170. function avahi_extract_info {
  171. # Create a list of bootstrap nodes
  172. avahi-browse -atr > $TEMPFILE_BASE
  173. cat $TEMPFILE_BASE | grep "hostname =\|address =\|port =" > $TEMPFILE
  174. if [ ! -f $TEMPFILE ]; then
  175. exit 1
  176. fi
  177. }
  178. function avahi_remove_info {
  179. rm -f $TEMPFILE_BASE
  180. rm -f $TEMPFILE
  181. }
  182. if [ ! -d /etc/avahi ]; then
  183. exit 0
  184. fi
  185. avahi_extract_info
  186. ipfs_bootstrap
  187. ipfs_publish
  188. detect_tox_users
  189. avahi_remove_info
  190. exit 0