zeronetavahi 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. PROJECT_NAME='freedombone'
  32. export TEXTDOMAIN=$PROJECT_NAME
  33. export TEXTDOMAINDIR="/usr/share/locale"
  34. WEB_PAGE_TITLE="Freedombone Mesh"
  35. TRACKER_PORT=6969
  36. ZERONET_INSTALL=/opt/zeronet
  37. ZERONET_USER_HOME=/home/zeronet
  38. MY_USERNAME=$USER
  39. if [ ! -d $ZERONET_INSTALL ]; then
  40. if [ -d /home/$MY_USERNAME/zeronet ]; then
  41. ZERONET_INSTALL=/home/$MY_USERNAME/zeronet
  42. else
  43. exit 0
  44. fi
  45. fi
  46. BLOGS_FILE=$ZERONET_INSTALL/freedombone-blogs
  47. FORUM_FILE=$ZERONET_INSTALL/freedombone-fora
  48. TOX_USERS_FILE=$ZERONET_INSTALL/freedombone-tox-users
  49. ZERONET_INDEX=/home/$MY_USERNAME/mesh.html
  50. ZERONET_CONFIG=$ZERONET_INSTALL/bootstrap
  51. IPFS_PORT=4001
  52. function create_index {
  53. if [ -f $ZERONET_INDEX ]; then
  54. sed -i "s|.*My Blog.*| <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/myblog)\">My Blog</a></li>|g" $ZERONET_INDEX
  55. sed -i "s|.*My Forum.*| <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/myforum)\">My Forum</a></li>|g" $ZERONET_INDEX
  56. return
  57. fi
  58. echo '<!DOCTYPE html>' > $ZERONET_INDEX
  59. echo '<html>' >> $ZERONET_INDEX
  60. echo '<head>' >> $ZERONET_INDEX
  61. echo "<title>$WEB_PAGE_TITLE</title>" >> $ZERONET_INDEX
  62. echo '</head>' >> $ZERONET_INDEX
  63. echo '<body>' >> $ZERONET_INDEX
  64. echo "<H1>$WEB_PAGE_TITLE</H1>" >> $ZERONET_INDEX
  65. echo '' >> $ZERONET_INDEX
  66. echo "<H2>Your Sites</H2>" >> $ZERONET_INDEX
  67. echo '' >> $ZERONET_INDEX
  68. echo '<ol type="square">' >> $ZERONET_INDEX
  69. echo " <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/myblog)\">My Blog</a></li>" >> $ZERONET_INDEX
  70. echo " <li><a href=\"$(cat /home/$MY_USERNAME/.config/zeronet/myforum)\">My Forum</a></li>" >> $ZERONET_INDEX
  71. echo " <li><a href=\"http://localhost:5001/webui\">File Share</a></li>" >> $ZERONET_INDEX
  72. echo '</ol>' >> $ZERONET_INDEX
  73. echo '' >> $ZERONET_INDEX
  74. echo "<H2>On the Mesh</H2>" >> $ZERONET_INDEX
  75. echo '' >> $ZERONET_INDEX
  76. echo '<ol type="square">' >> $ZERONET_INDEX
  77. echo " <li><a href=\"${BLOGS_FILE}.html\">Blogs</a></li>" >> $ZERONET_INDEX
  78. echo " <li><a href=\"${FORUM_FILE}.html\">Fora</a></li>" >> $ZERONET_INDEX
  79. echo " <li><a href=\"${TOX_USERS_FILE}.html\">Tox Users</a></li>" >> $ZERONET_INDEX
  80. echo '</ol>' >> $ZERONET_INDEX
  81. echo '' >> $ZERONET_INDEX
  82. echo '</body>' >> $ZERONET_INDEX
  83. echo '</html>' >> $ZERONET_INDEX
  84. }
  85. function create_header {
  86. header_file=${1}-header.html
  87. if [ -f $header_file ]; then
  88. return
  89. fi
  90. echo '<!DOCTYPE html>' > $header_file
  91. echo '<html>' >> $header_file
  92. echo '<head>' >> $header_file
  93. echo "<title>$WEB_PAGE_TITLE - $2</title>" >> $header_file
  94. echo '<meta http-equiv="refresh" content="60">' >> $header_file
  95. echo '</head>' >> $header_file
  96. echo '<body>' >> $header_file
  97. echo "<H1>$2</H1>" >> $header_file
  98. }
  99. function create_footer {
  100. footer_file=${1}-footer.html
  101. if [ -f $footer_file ]; then
  102. return
  103. fi
  104. echo '</body>' >> $footer_file
  105. echo '</html>' >> $footer_file
  106. }
  107. if [ ! -d /etc/avahi ]; then
  108. exit 0
  109. fi
  110. # Create a list of bootstrap nodes
  111. TEMPFILE_BASE=/tmp/tmpzeronetavahibase.txt
  112. TEMPFILE=/tmp/tmpzeronetavahi.txt
  113. avahi-browse -atr > $TEMPFILE_BASE
  114. cat $TEMPFILE_BASE | grep "hostname =\|address =\|port =" > $TEMPFILE
  115. if [ ! -f $TEMPFILE ]; then
  116. exit 1
  117. fi
  118. if [ -f $ZERONET_CONFIG.new ]; then
  119. rm -f $ZERONET_CONFIG.new
  120. fi
  121. touch $ZERONET_CONFIG.new
  122. state=0
  123. address=""
  124. peer=""
  125. tracker_ctr=0
  126. while IFS='' read -r line || [[ -n "$line" ]]; do
  127. if [ ${state} -eq "1" ]; then
  128. if [[ $line == *"address ="* ]]; then
  129. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  130. tracker_url="http://$peer:$TRACKER_PORT/announce"
  131. if ! grep -q "$tracker_url" $ZERONET_CONFIG.new; then
  132. echo "$tracker_url" >> $ZERONET_CONFIG.new
  133. tracker_ctr=$((tracker_ctr + 1))
  134. fi
  135. state=0
  136. fi
  137. fi
  138. if [[ $line == *"hostname ="* ]]; then
  139. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  140. state=1
  141. fi
  142. done < "$TEMPFILE"
  143. # detect blogs in the mesh
  144. if [ -f $BLOGS_FILE.new ]; then
  145. rm -f $BLOGS_FILE.new
  146. fi
  147. cat $TEMPFILE_BASE | grep "ZeroNet Blog\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
  148. state=0
  149. address=""
  150. peer=""
  151. echo '<ol type="square">' >> $BLOGS_FILE.new
  152. while IFS='' read -r line || [[ -n "$line" ]]; do
  153. if [ ${state} -eq "3" ]; then
  154. if [[ $line == *"txt ="* ]]; then
  155. blog_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  156. echo " <li><a href=${blog_url}>${peer}</a></li>" >> $BLOGS_FILE.new
  157. state=0
  158. fi
  159. fi
  160. if [ ${state} -eq "2" ]; then
  161. if [[ $line == *"address ="* ]]; then
  162. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  163. state=3
  164. fi
  165. fi
  166. if [ ${state} -eq "1" ]; then
  167. if [[ $line == *"hostname ="* ]]; then
  168. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  169. state=2
  170. fi
  171. fi
  172. if [[ $line == *"ZeroNet Blog"* && $line == "= "* ]]; then
  173. state=1
  174. fi
  175. done < "$TEMPFILE"
  176. echo '</ol>' >> $BLOGS_FILE.new
  177. # detect fora in the mesh
  178. if [ -f $FORUM_FILE.new ]; then
  179. rm -f $FORUM_FILE.new
  180. fi
  181. cat $TEMPFILE_BASE | grep "ZeroNet Forum\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
  182. state=0
  183. address=""
  184. peer=""
  185. echo '<ol type="square">' >> $FORUM_FILE.new
  186. while IFS='' read -r line || [[ -n "$line" ]]; do
  187. if [ ${state} -eq "3" ]; then
  188. if [[ $line == *"txt ="* ]]; then
  189. forum_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  190. echo " <li><a href=${forum_url}>${peer}</a></li>" >> $FORUM_FILE.new
  191. state=0
  192. fi
  193. fi
  194. if [ ${state} -eq "2" ]; then
  195. if [[ $line == *"address ="* ]]; then
  196. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  197. state=3
  198. fi
  199. fi
  200. if [ ${state} -eq "1" ]; then
  201. if [[ $line == *"hostname ="* ]]; then
  202. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  203. state=2
  204. fi
  205. fi
  206. if [[ $line == *"ZeroNet Forum"* && $line == "= "* ]]; then
  207. state=1
  208. fi
  209. done < "$TEMPFILE"
  210. echo '<ol>' >> $FORUM_FILE.new
  211. # IPFS bootstrap
  212. cat $TEMPFILE_BASE | grep "IPFS\|hostname =\|address =\|port =\|txt =" > $TEMPFILE
  213. state=0
  214. address=""
  215. peer=""
  216. while IFS='' read -r line || [[ -n "$line" ]]; do
  217. if [ ${state} -eq "3" ]; then
  218. if [[ $line == *"txt ="* ]]; then
  219. ipfs_peer_id=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}' | awk -F '"' '{print $2}')
  220. ipfs bootstrap add /ip4/${address}/tcp/${IPFS_PORT}/ipfs/${ipfs_peer_id}
  221. state=0
  222. fi
  223. fi
  224. if [ ${state} -eq "2" ]; then
  225. if [[ $line == *"address ="* ]]; then
  226. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  227. state=3
  228. fi
  229. fi
  230. if [ ${state} -eq "1" ]; then
  231. if [[ $line == *"hostname ="* ]]; then
  232. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  233. state=2
  234. fi
  235. fi
  236. if [[ $line == *"IPFS"* && $line == "= "* ]]; then
  237. state=1
  238. fi
  239. done < "$TEMPFILE"
  240. # detect Tox users
  241. lstox > $TEMPFILE
  242. toxctr=0
  243. echo '<ol type="square">' >> $TOX_USERS_FILE.new
  244. while IFS='' read -r line || [[ -n "$line" ]]; do
  245. if [[ $line != "Failed*" ]]; then
  246. echo " <li>$line</li>" >> $TOX_USERS_FILE.new
  247. toxctr=$((toxctr + 1))
  248. fi
  249. done < "$TEMPFILE"
  250. if [ $toxctr -eq "0" ]; then
  251. echo " <li>No users found</li>" >> $TOX_USERS_FILE.new
  252. fi
  253. echo '<ol>' >> $TOX_USERS_FILE.new
  254. rm -f $TEMPFILE_BASE
  255. rm -f $TEMPFILE
  256. if [ ${tracker_ctr} -gt "0" ]; then
  257. cp -f $ZERONET_CONFIG.new $ZERONET_CONFIG
  258. else
  259. if [ -f $ZERONET_CONFIG ]; then
  260. rm -f $ZERONET_CONFIG
  261. fi
  262. fi
  263. rm -f $ZERONET_CONFIG.new
  264. if [ -d $ZERONET_USER_HOME ]; then
  265. sudo chown zeronet:zeronet $ZERONET_CONFIG
  266. fi
  267. # make some html headers and footers
  268. create_header $BLOGS_FILE "Blogs"
  269. create_header $FORUM_FILE "Fora"
  270. create_header $TOX_USERS_FILE "Tox Users"
  271. create_footer $BLOGS_FILE
  272. create_footer $FORUM_FILE
  273. create_footer $TOX_USERS_FILE
  274. # make the index page
  275. create_index
  276. # create a web page showing the available blogs
  277. cat ${BLOGS_FILE}-header.html ${BLOGS_FILE}.new ${BLOGS_FILE}-footer.html > ${BLOGS_FILE}.html
  278. rm -f ${BLOGS_FILE}.new
  279. if [ -d $ZERONET_USER_HOME ]; then
  280. sudo chown zeronet:zeronet $BLOGS_FILE
  281. fi
  282. # create a web page showing the available fora
  283. cat ${FORUM_FILE}-header.html ${FORUM_FILE}.new ${FORUM_FILE}-footer.html > ${FORUM_FILE}.html
  284. rm -f ${FORUM_FILE}.new
  285. if [ -d $ZERONET_USER_HOME ]; then
  286. sudo chown zeronet:zeronet $FORUM_FILE
  287. fi
  288. # create a web page showing Tox users
  289. cat ${TOX_USERS_FILE}-header.html ${TOX_USERS_FILE}.new ${TOX_USERS_FILE}-footer.html > ${TOX_USERS_FILE}.html
  290. rm -f ${TOX_USERS_FILE}.new
  291. if [ -d $ZERONET_USER_HOME ]; then
  292. sudo chown zeronet:zeronet $TOX_USERS_FILE
  293. fi
  294. exit 0