zeronetavahi 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 zeronet trackers
  12. # Blogs and fora are discovered on the network and then published
  13. # to an index file which can be opened in a browser
  14. # License
  15. # =======
  16. #
  17. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  18. #
  19. # This program is free software: you can redistribute it and/or modify
  20. # it under the terms of the GNU General Public License as published by
  21. # the Free Software Foundation, either version 3 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU General Public License
  30. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. WEB_PAGE_TITLE="Freedombone Mesh"
  32. TRACKER_PORT=6969
  33. ZERONET_INSTALL=/opt/zeronet
  34. ZERONET_USER_HOME=/home/zeronet
  35. MY_USERNAME=$USER
  36. if [ ! -d $ZERONET_INSTALL ]; then
  37. if [ -d /home/$MY_USERNAME/zeronet ]; then
  38. ZERONET_INSTALL=/home/$MY_USERNAME/zeronet
  39. else
  40. exit 0
  41. fi
  42. fi
  43. BOOTSTRAP_FILE=$ZERONET_INSTALL/bootstrap
  44. BLOGS_FILE=$ZERONET_INSTALL/freedombone-blogs
  45. FORUM_FILE=$ZERONET_INSTALL/freedombone-fora
  46. TOX_USERS_FILE=$ZERONET_INSTALL/freedombone-tox-users
  47. ZERONET_INDEX=/home/$MY_USERNAME/mesh.html
  48. function create_index {
  49. if [ -f $ZERONET_INDEX ]; then
  50. return
  51. fi
  52. echo '<!DOCTYPE html>' > $ZERONET_INDEX
  53. echo '<html>' >> $ZERONET_INDEX
  54. echo '<head>' >> $ZERONET_INDEX
  55. echo "<title>$WEB_PAGE_TITLE</title>" >> $ZERONET_INDEX
  56. echo '</head>' >> $ZERONET_INDEX
  57. echo '<body>' >> $ZERONET_INDEX
  58. echo "<H1>$WEB_PAGE_TITLE</H1>" >> $ZERONET_INDEX
  59. echo '' >> $ZERONET_INDEX
  60. echo "<H2>Your Sites</H2>" >> $ZERONET_INDEX
  61. echo '' >> $ZERONET_INDEX
  62. echo '<ol type="square">' >> $ZERONET_INDEX
  63. echo " <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/myblog)\">My Blog</a></li>" >> $ZERONET_INDEX
  64. echo " <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/myforum)\">My Forum</a></li>" >> $ZERONET_INDEX
  65. echo '</ol>' >> $ZERONET_INDEX
  66. echo '' >> $ZERONET_INDEX
  67. echo "<H2>On the Mesh</H2>" >> $ZERONET_INDEX
  68. echo '' >> $ZERONET_INDEX
  69. echo '<ol type="square">' >> $ZERONET_INDEX
  70. echo " <li><a href=\"${BLOGS_FILE}.html\">Blogs</a></li>" >> $ZERONET_INDEX
  71. echo " <li><a href=\"${FORUM_FILE}.html\">Fora</a></li>" >> $ZERONET_INDEX
  72. echo " <li><a href=\"${TOX_USERS_FILE}.html\">Tox Users</a></li>" >> $ZERONET_INDEX
  73. echo '</ol>' >> $ZERONET_INDEX
  74. echo '' >> $ZERONET_INDEX
  75. echo '</body>' >> $ZERONET_INDEX
  76. echo '</html>' >> $ZERONET_INDEX
  77. }
  78. function create_header {
  79. header_file=${1}-header.html
  80. if [ -f $header_file ]; then
  81. return
  82. fi
  83. echo '<!DOCTYPE html>' > $header_file
  84. echo '<html>' >> $header_file
  85. echo '<head>' >> $header_file
  86. echo "<title>$WEB_PAGE_TITLE - $2</title>" >> $header_file
  87. echo '<meta http-equiv="refresh" content="60">' >> $header_file
  88. echo '</head>' >> $header_file
  89. echo '<body>' >> $header_file
  90. echo "<H1>$2</H1>" >> $header_file
  91. }
  92. function create_footer {
  93. footer_file=${1}-footer.html
  94. if [ -f $footer_file ]; then
  95. return
  96. fi
  97. echo '</body>' >> $footer_file
  98. echo '</html>' >> $footer_file
  99. }
  100. if [ ! -d /etc/avahi ]; then
  101. exit 0
  102. fi
  103. # Create a list of bootstrap nodes
  104. TEMPFILE_BASE=/tmp/tmpzeronetavahibase.txt
  105. TEMPFILE=/tmp/tmpzeronetavahi.txt
  106. avahi-browse -atr > $TEMPFILE_BASE
  107. cat $TEMPFILE_BASE | grep "Workstation\|hostname =\|address =\|port =" > $TEMPFILE
  108. if [ ! -f $TEMPFILE ]; then
  109. exit 1
  110. fi
  111. if [ -f $BOOTSTRAP_FILE.new ]; then
  112. rm -f $BOOTSTRAP_FILE.new
  113. fi
  114. state=0
  115. address=""
  116. peer=""
  117. while IFS='' read -r line || [[ -n "$line" ]]; do
  118. if [ ${state} -eq "2" ]; then
  119. if [[ $line == *"address ="* ]]; then
  120. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  121. echo "http $peer:$TRACKER_PORT/announce None" >> $BOOTSTRAP_FILE.new
  122. state=0
  123. fi
  124. fi
  125. if [ ${state} -eq "1" ]; then
  126. if [[ $line == *"hostname ="* ]]; then
  127. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  128. state=2
  129. fi
  130. fi
  131. if [[ $line == *"Workstation"* && $line == "= "* ]]; then
  132. state=1
  133. fi
  134. done < "$TEMPFILE"
  135. # detect blogs in the mesh
  136. if [ -f $BLOGS_FILE.new ]; then
  137. rm -f $BLOGS_FILE.new
  138. fi
  139. cat $TEMPFILE_BASE | grep "ZeroNet Blog\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
  140. state=0
  141. address=""
  142. peer=""
  143. echo '<ol type="square">' >> $BLOGS_FILE.new
  144. while IFS='' read -r line || [[ -n "$line" ]]; do
  145. if [ ${state} -eq "3" ]; then
  146. if [[ $line == *"txt ="* ]]; then
  147. blog_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  148. echo " <li><a href=${blog_url}>${peer}</a></li>" >> $BLOGS_FILE.new
  149. state=0
  150. fi
  151. fi
  152. if [ ${state} -eq "2" ]; then
  153. if [[ $line == *"address ="* ]]; then
  154. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  155. state=3
  156. fi
  157. fi
  158. if [ ${state} -eq "1" ]; then
  159. if [[ $line == *"hostname ="* ]]; then
  160. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  161. state=2
  162. fi
  163. fi
  164. if [[ $line == *"ZeroNet Blog"* && $line == "= "* ]]; then
  165. state=1
  166. fi
  167. done < "$TEMPFILE"
  168. echo '</ol>' >> $BLOGS_FILE.new
  169. # detect fora in the mesh
  170. if [ -f $FORUM_FILE.new ]; then
  171. rm -f $FORUM_FILE.new
  172. fi
  173. cat $TEMPFILE_BASE | grep "ZeroNet Forum\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
  174. state=0
  175. address=""
  176. peer=""
  177. echo '<ol type="square">' >> $FORUM_FILE.new
  178. while IFS='' read -r line || [[ -n "$line" ]]; do
  179. if [ ${state} -eq "3" ]; then
  180. if [[ $line == *"txt ="* ]]; then
  181. forum_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  182. echo " <li><a href=${forum_url}>${peer}</a></li>" >> $FORUM_FILE.new
  183. state=0
  184. fi
  185. fi
  186. if [ ${state} -eq "2" ]; then
  187. if [[ $line == *"address ="* ]]; then
  188. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  189. state=3
  190. fi
  191. fi
  192. if [ ${state} -eq "1" ]; then
  193. if [[ $line == *"hostname ="* ]]; then
  194. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  195. state=2
  196. fi
  197. fi
  198. if [[ $line == *"ZeroNet Forum"* && $line == "= "* ]]; then
  199. state=1
  200. fi
  201. done < "$TEMPFILE"
  202. echo '<ol>' >> $FORUM_FILE.new
  203. # detect Tox users
  204. lstox > $TEMPFILE
  205. toxctr=0
  206. echo '<ol type="square">' >> $TOX_USERS_FILE.new
  207. while IFS='' read -r line || [[ -n "$line" ]]; do
  208. if [[ $line != "Failed*" ]]; then
  209. echo " <li>$line</li>" >> $TOX_USERS_FILE.new
  210. toxctr=$((toxctr + 1))
  211. fi
  212. done < "$TEMPFILE"
  213. if [ $toxctr -eq "0" ]; then
  214. echo " <li>No users found</li>" >> $TOX_USERS_FILE.new
  215. fi
  216. echo '<ol>' >> $TOX_USERS_FILE.new
  217. rm -f $TEMPFILE_BASE
  218. rm -f $TEMPFILE
  219. cp -f $BOOTSTRAP_FILE.new $BOOTSTRAP_FILE
  220. rm -f $BOOTSTRAP_FILE.new
  221. if [ -d $ZERONET_USER_HOME ]; then
  222. sudo chown zeronet:zeronet $BOOTSTRAP_FILE
  223. fi
  224. # make some html headers and footers
  225. create_header $BLOGS_FILE "Blogs"
  226. create_header $FORUM_FILE "Fora"
  227. create_header $TOX_USERS_FILE "Tox Users"
  228. create_footer $BLOGS_FILE
  229. create_footer $FORUM_FILE
  230. create_footer $TOX_USERS_FILE
  231. # make the index page
  232. create_index
  233. # create a web page showing the available blogs
  234. cat ${BLOGS_FILE}-header.html ${BLOGS_FILE}.new ${BLOGS_FILE}-footer.html > ${BLOGS_FILE}.html
  235. rm -f ${BLOGS_FILE}.new
  236. if [ -d $ZERONET_USER_HOME ]; then
  237. sudo chown zeronet:zeronet $BLOGS_FILE
  238. fi
  239. # create a web page showing the available fora
  240. cat ${FORUM_FILE}-header.html ${FORUM_FILE}.new ${FORUM_FILE}-footer.html > ${FORUM_FILE}.html
  241. rm -f ${FORUM_FILE}.new
  242. if [ -d $ZERONET_USER_HOME ]; then
  243. sudo chown zeronet:zeronet $FORUM_FILE
  244. fi
  245. # create a web page showing Tox users
  246. cat ${TOX_USERS_FILE}-header.html ${TOX_USERS_FILE}.new ${TOX_USERS_FILE}-footer.html > ${TOX_USERS_FILE}.html
  247. rm -f ${TOX_USERS_FILE}.new
  248. if [ -d $ZERONET_USER_HOME ]; then
  249. sudo chown zeronet:zeronet $TOX_USERS_FILE
  250. fi
  251. exit 0