meshavahi 6.9KB

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