zeronetavahi 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU 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 General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. TRACKER_PORT=6969
  30. ZERONET_INSTALL=/opt/zeronet
  31. if [ ! -d $ZERONET_INSTALL ]; then
  32. if [ -d /home/$USER/zeronet ]; then
  33. ZERONET_INSTALL=/home/$USER/zeronet
  34. else
  35. exit 0
  36. fi
  37. fi
  38. BOOTSTRAP_FILE=$ZERONET_INSTALL/bootstrap
  39. BLOGS_FILE=$ZERONET_INSTALL/blogs
  40. FORUM_FILE=$ZERONET_INSTALL/forum
  41. if [ ! -d /etc/avahi ]; then
  42. exit 0
  43. fi
  44. TEMPFILE_BASE=/tmp/tmpzeronetavahibase.txt
  45. TEMPFILE=/tmp/tmpzeronetavahi.txt
  46. avahi-browse -atr > $TEMPFILE_BASE
  47. cat $TEMPFILE_BASE | grep "Workstation\|hostname =\|address =\|port =" > $TEMPFILE
  48. if [ ! -f $TEMPFILE ]; then
  49. exit 1
  50. fi
  51. if [ -f $BOOTSTRAP_FILE.new ]; then
  52. rm -f $BOOTSTRAP_FILE.new
  53. fi
  54. state=0
  55. address=""
  56. peer=""
  57. while IFS='' read -r line || [[ -n "$line" ]]; do
  58. if [ ${state} -eq "2" ]; then
  59. if [[ $line == *"address ="* ]]; then
  60. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  61. echo "http $peer:$TRACKER_PORT/announce None" >> $BOOTSTRAP_FILE.new
  62. state=0
  63. fi
  64. fi
  65. if [ ${state} -eq "1" ]; then
  66. if [[ $line == *"hostname ="* ]]; then
  67. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  68. state=2
  69. fi
  70. fi
  71. if [[ $line == *"Workstation"* && $line == "= "* ]]; then
  72. state=1
  73. fi
  74. done < "$TEMPFILE"
  75. # detect blogs in the mesh
  76. if [ -f $BLOGS_FILE.new ]; then
  77. rm -f $BLOGS_FILE.new
  78. fi
  79. cat $TEMPFILE_BASE | grep "ZeroNet Blog\|hostname =\|address =\|port =\|txt-record =" > $TEMPFILE
  80. state=0
  81. address=""
  82. peer=""
  83. while IFS='' read -r line || [[ -n "$line" ]]; do
  84. if [ ${state} -eq "3" ]; then
  85. if [[ $line == *"txt-record ="* ]]; then
  86. blog_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  87. echo "$peer $blog_url" >> $BLOGS_FILE.new
  88. state=0
  89. fi
  90. fi
  91. if [ ${state} -eq "2" ]; then
  92. if [[ $line == *"address ="* ]]; then
  93. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  94. state=3
  95. fi
  96. fi
  97. if [ ${state} -eq "1" ]; then
  98. if [[ $line == *"hostname ="* ]]; then
  99. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  100. state=2
  101. fi
  102. fi
  103. if [[ $line == *"ZeroNet Blog"* && $line == "= "* ]]; then
  104. state=1
  105. fi
  106. done < "$TEMPFILE"
  107. # detect fora in the mesh
  108. if [ -f $FORUM_FILE.new ]; then
  109. rm -f $FORUM_FILE.new
  110. fi
  111. cat $TEMPFILE_BASE | grep "ZeroNet Forum\|hostname =\|address =\|port =\|txt-record =" > $TEMPFILE
  112. state=0
  113. address=""
  114. peer=""
  115. while IFS='' read -r line || [[ -n "$line" ]]; do
  116. if [ ${state} -eq "3" ]; then
  117. if [[ $line == *"txt-record ="* ]]; then
  118. forum_url=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  119. echo "$peer $forum_url" >> $FORUM_FILE.new
  120. state=0
  121. fi
  122. fi
  123. if [ ${state} -eq "2" ]; then
  124. if [[ $line == *"address ="* ]]; then
  125. address=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  126. state=3
  127. fi
  128. fi
  129. if [ ${state} -eq "1" ]; then
  130. if [[ $line == *"hostname ="* ]]; then
  131. peer=$(echo $line | awk -F '[' '{print $2}' | awk -F ']' '{print $1}')
  132. state=2
  133. fi
  134. fi
  135. if [[ $line == *"ZeroNet Forum"* && $line == "= "* ]]; then
  136. state=1
  137. fi
  138. done < "$TEMPFILE"
  139. rm -f $TEMPFILE_BASE
  140. rm -f $TEMPFILE
  141. cp -f $BOOTSTRAP_FILE.new $BOOTSTRAP_FILE
  142. rm -f $BOOTSTRAP_FILE.new
  143. if [ -d /home/zeronet ]; then
  144. sudo chown zeronet:zeronet $BOOTSTRAP_FILE
  145. fi
  146. cp -f $BLOGS_FILE.new $BLOGS_FILE
  147. rm -f $BLOGS_FILE.new
  148. if [ -d /home/zeronet ]; then
  149. sudo chown zeronet:zeronet $BLOGS_FILE
  150. fi
  151. cp -f $FORUM_FILE.new $FORUM_FILE
  152. rm -f $FORUM_FILE.new
  153. if [ -d /home/zeronet ]; then
  154. sudo chown zeronet:zeronet $FORUM_FILE
  155. fi
  156. exit 0