freedombone-mesh 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # A script to easily locate mesh peers and start communicating
  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. MUMBLE_PATH=/usr/bin/mumble
  30. TOXIC_PATH=/usr/bin/toxic
  31. DHTNODES=/usr/share/toxic/DHTnodes
  32. PEERS_FILE=/tmp/meshpeers.txt
  33. TOX_PORT=33445
  34. TOXCORE_REPO='git://github.com/irungentoo/toxcore.git'
  35. TOX_BOOTSTRAP_ID_FILE=/var/lib/tox-bootstrapd/pubkey.txt
  36. QTOX_INI="/home/$USER/.config/tox/qtox.ini"
  37. # client or server installations sounds odd for a mesh, but this
  38. # indicates whether this is a dedicated mesh peer ("yes") or
  39. # a 'client' such as a laptop or desktop machine with
  40. # the freedombone-client script installed
  41. SERVER_INSTALLATION="no"
  42. function install_toxcore {
  43. if [ -f /etc/tox-bootstrapd.conf ]; then
  44. return
  45. fi
  46. sudo apt-get -y install build-essential libtool autotools-dev
  47. sudo apt-get -y install automake checkinstall check git yasm
  48. sudo apt-get -y install libsodium13 libsodium-dev libcap2-bin
  49. sudo apt-get -y install libconfig9 libconfig-dev
  50. if [ ! -d ~/develop ]; then
  51. mkdir ~/develop
  52. fi
  53. cd ~/develop
  54. git clone $TOXCORE_REPO
  55. cd ~/develop/toxcore
  56. autoreconf -i
  57. ./configure --enable-daemon
  58. if [ ! "$?" = "0" ]; then
  59. exit 78467
  60. fi
  61. make
  62. if [ ! "$?" = "0" ]; then
  63. exit 84562
  64. fi
  65. sudo make install
  66. sudo cp /usr/local/lib/libtoxcore* /usr/lib/
  67. if [ ! -f /usr/local/bin/tox-bootstrapd ]; then
  68. echo "File not found /usr/local/bin/tox-bootstrapd"
  69. exit 73862
  70. fi
  71. sudo useradd --home-dir /var/lib/tox-bootstrapd --create-home --system --shell /sbin/nologin --comment "Account to run Tox's DHT bootstrap daemon" --user-group tox-bootstrapd
  72. sudo chmod 700 /var/lib/tox-bootstrapd
  73. if [ ! -f ~/develop/toxcore/other/bootstrap_daemon/tox-bootstrapd.conf ]; then
  74. echo "File not found $INSTALL_DIR/toxcore/other/bootstrap_daemon/tox-bootstrapd.conf"
  75. exit 476835
  76. fi
  77. # create configuration file
  78. echo "port = $TOX_PORT" > /tmp/tox-bootstrapd.conf
  79. echo 'keys_file_path = "/var/lib/tox-bootstrapd/keys"' >> /tmp/tox-bootstrapd.conf
  80. echo 'pid_file_path = "/var/run/tox-bootstrapd/tox-bootstrapd.pid"' >> /tmp/tox-bootstrapd.conf
  81. echo 'enable_ipv6 = true' >> /tmp/tox-bootstrapd.conf
  82. echo 'enable_ipv4_fallback = true' >> /tmp/tox-bootstrapd.conf
  83. echo 'enable_lan_discovery = true' >> /tmp/tox-bootstrapd.conf
  84. echo 'enable_tcp_relay = true' >> /tmp/tox-bootstrapd.conf
  85. echo "tcp_relay_ports = [443, 3389, $TOX_PORT]" >> /tmp/tox-bootstrapd.conf
  86. echo 'enable_motd = true' >> /tmp/tox-bootstrapd.conf
  87. echo 'motd = "tox-bootstrapd"' >> /tmp/tox-bootstrapd.conf
  88. sudo cp /tmp/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
  89. rm /tmp/tox-bootstrapd.conf
  90. if [ -f /bin/systemctl ]; then
  91. if [ ! -f ~/develop/toxcore/other/bootstrap_daemon/tox-bootstrapd.service ]; then
  92. echo "File not found ~/develop/toxcore/other/bootstrap_daemon/tox-bootstrapd.service"
  93. exit 7359
  94. fi
  95. sudo cp ~/develop/toxcore/other/bootstrap_daemon/tox-bootstrapd.service /etc/systemd/system/
  96. sudo systemctl daemon-reload
  97. sudo systemctl enable tox-bootstrapd.service
  98. sudo systemctl start tox-bootstrapd.service
  99. if [ ! "$?" = "0" ]; then
  100. sudo systemctl status tox-bootstrapd.service
  101. exit 5846
  102. fi
  103. sudo systemctl restart tox-bootstrapd.service
  104. else
  105. sudo cp ~/develop/toxcore/other/bootstrap_daemon/tox-bootstrapd.sh /etc/init.d/tox-bootstrapd
  106. sudo chmod 755 /etc/init.d/tox-bootstrapd
  107. sudo update-rc.d tox-bootstrapd defaults
  108. sudo service tox-bootstrapd start
  109. fi
  110. TOX_PUBLIC_KEY=$(cat /var/log/syslog | grep tox | grep "Public Key" | awk -F ' ' '{print $8}' | tail -1)
  111. if [ ${#TOX_PUBLIC_KEY} -lt 30 ]; then
  112. echo 'Could not obtain the tox node public key'
  113. exit 6529
  114. fi
  115. # save the public key for later reference
  116. echo "$TOX_PUBLIC_KEY" > /tmp/boostrapid
  117. sudo mv /tmp/boostrapid $TOX_BOOTSTRAP_ID_FILE
  118. }
  119. function install_toxid {
  120. if [ -f /usr/local/bin/toxid ]; then
  121. return
  122. fi
  123. cd ~/develop
  124. git clone https://github.com/bashrc/toxid
  125. cd ~/develop/toxid
  126. make
  127. sudo make install
  128. if [ ! -f /usr/local/bin/toxid ]; then
  129. echo "Couldn't install toxid"
  130. exit 6389
  131. fi
  132. if ! grep -Fxq "toxavahi" /etc/crontab; then
  133. cp /etc/crontab /tmp/crontab
  134. echo "* * * * * root toxavahi > /dev/null" >> /tmp/crontab
  135. sudo cp /tmp/crontab /etc/crontab
  136. rm /tmp/crontab
  137. fi
  138. }
  139. function run_tox {
  140. echo $QTOX_INI
  141. if [[ -f $TOXIC_PATH || -f $QTOX_INI ]]; then
  142. if [[ $SERVER_INSTALLATION == "no" ]]; then
  143. # update bootstrap nodes
  144. if [ -f $DHTNODES ]; then
  145. if [ ! -f $DHTNODES.internet ]; then
  146. sudo cp $DHTNODES $DHTNODES.internet
  147. fi
  148. fi
  149. lstox -f dht > /tmp/dht
  150. sudo cp /tmp/dht $DHTNODES
  151. fi
  152. # get a list of peers
  153. PEER_TOX_ID_LIST=$(lstox | grep $AVAHI_DOMAIN | sort -u)
  154. if [ ! "$PEER_TOX_ID_LIST" ]; then
  155. echo 'No peers found'
  156. exit 0
  157. fi
  158. PEER_TOX_ID_LIST_COUNT=$(echo "$PEER_TOX_ID_LIST" | wc -l)
  159. PEER_TOX_ID=''
  160. if [ $PEER_TOX_ID_LIST_COUNT -lt "2" ]; then
  161. # single peer
  162. PEER_TOX_ID=$(echo "$PEER_TOX_ID_LIST" | awk -F ' ' '{print $3}')
  163. else
  164. # choose a user from a list
  165. echo ''
  166. echo "Select a user on $AVAHI_DOMAIN:"
  167. ctr=0
  168. while IFS='' read -r line || [[ -n "$line" ]]; do
  169. toxusername=$(echo $line | awk -F ' ' '{print $2}')
  170. echo " $ctr. $toxusername"
  171. ctr=$((ctr + 1))
  172. done < "$PEER_TOX_ID_LIST"
  173. read user_index
  174. PEER_TOX_ID=$(echo "$PEER_TOX_ID_LIST" | tail -n+${user_index} | head -n1 | awk -F ' ' '{print $3}')
  175. fi
  176. # if this is a valid ID
  177. if [ ${#PEER_TOX_ID} -gt 30 ]; then
  178. # start client and make a friend request
  179. if [ -f $QTOX_INI ]; then
  180. echo 'Launch qTox'
  181. qtox &
  182. else
  183. echo "n
  184. /nick $USER
  185. /add $PEER_TOX_ID
  186. /exit
  187. " | $TOXIC_PATH -d
  188. # Running twice is a hack to get around buggyness in the client
  189. $TOXIC_PATH -d
  190. fi
  191. exit 0
  192. else
  193. # ID was invalid
  194. echo $PEER_TOX_ID
  195. echo "Tox ID for $AVAHI_DOMAIN was not found"
  196. exit 6
  197. fi
  198. fi
  199. }
  200. if [ -f /var/lib/batman ]; then
  201. SERVER_INSTALLATION="yes"
  202. fi
  203. if [[ $SERVER_INSTALLATION == "no" ]]; then
  204. if [ ! -f /usr/bin/batman ]; then
  205. freedombone-client
  206. fi
  207. fi
  208. # alternative toxic paths
  209. if [ -f /usr/local/bin/toxic ]; then
  210. TOXIC_PATH=/usr/local/bin/toxic
  211. fi
  212. if [ -f /usr/local/share/toxic/DHTnodes ]; then
  213. DHTNODES=/usr/local/share/toxic/DHTnodes
  214. fi
  215. if [[ $SERVER_INSTALLATION == "no" ]]; then
  216. if [ ! -f /tmp/meshtype ]; then
  217. install_toxcore
  218. install_toxid
  219. sudo batman start
  220. if [ ! "$?" = "0" ]; then
  221. exit 2
  222. fi
  223. fi
  224. fi
  225. avahi-browse -atl | awk -F ' ' '{print $4}' | sort -u > $PEERS_FILE
  226. if [ ! -f $PEERS_FILE ]; then
  227. echo 'No peers were found'
  228. exit 0
  229. fi
  230. ctr=0
  231. while IFS='' read -r line || [[ -n "$line" ]]; do
  232. ctr=$((ctr + 1))
  233. done < "$PEERS_FILE"
  234. if [ ${ctr} -lt "1" ]; then
  235. echo 'No peers were found'
  236. exit 0
  237. fi
  238. clear
  239. echo ''
  240. echo ".---. . . "
  241. echo "| | | "
  242. echo "|--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-. "
  243. echo "| | (.-' (.-' ( | ( )| | | | )( )| | (.-' "
  244. echo "' ' --' --' -' - -' ' ' -' -' -' ' - --'"
  245. echo ' Freedom in the Mesh'
  246. echo ''
  247. echo 'Please choose a peer to connect to:'
  248. idx=1
  249. while IFS='' read -r line || [[ -n "$line" ]]; do
  250. echo " $idx. $line"
  251. idx=$((idx + 1))
  252. done < "$PEERS_FILE"
  253. peer_index=0
  254. read peer_index
  255. # if no selection made
  256. if [ ! $peer_index ]; then
  257. echo 'Nothing was selected'
  258. echo ''
  259. exit 0
  260. fi
  261. # get the avahi domain name
  262. AVAHI_DOMAIN=
  263. idx=1
  264. while IFS='' read -r line || [[ -n "$line" ]]; do
  265. if [ ${idx} -eq "$peer_index" ]; then
  266. AVAHI_DOMAIN=${line}.local
  267. fi
  268. idx=$((idx + 1))
  269. done < "$PEERS_FILE"
  270. if [ ! $AVAHI_DOMAIN ]; then
  271. echo 'No domain name'
  272. exit 3
  273. fi
  274. # if only mumble is installed
  275. if [ ! -f $TOXIC_PATH ]; then
  276. if [ -f $MUMBLE_PATH ]; then
  277. $MUMBLE_PATH &
  278. exit 0
  279. fi
  280. echo 'You need mumble/toxic/qTox installed on your system'
  281. if [[ $SERVER_INSTALLATION == "no" ]]; then
  282. sudo batman stop
  283. fi
  284. exit 4
  285. fi
  286. # if only tox is installed
  287. if [ ! -f $MUMBLE_PATH ]; then
  288. run_tox
  289. fi
  290. echo ''
  291. echo 'Choose communication service:'
  292. echo ' 1. VoIP'
  293. echo ' 2. Tox Chat'
  294. echo ''
  295. read peer_index
  296. # if no selection made
  297. if [ ! $peer_index ]; then
  298. echo 'Nothing was selected'
  299. echo ''
  300. exit 0
  301. fi
  302. if [[ $peer_index == 1 ]]; then
  303. if [ -f $MUMBLE_PATH ]; then
  304. echo ''
  305. echo 'To setup for the first time click "Add New", then set:'
  306. echo " Label: $AVAHI_DOMAIN"
  307. echo " Address: $AVAHI_DOMAIN"
  308. echo ' Port: 64738'
  309. echo " Username: $USER"
  310. echo ''
  311. echo 'Press Enter to continue.'
  312. echo ''
  313. read peer_index
  314. $MUMBLE_PATH &
  315. else
  316. echo 'Mumble may not be installed on this system'
  317. exit 5
  318. fi
  319. fi
  320. if [[ $peer_index == 2 ]]; then
  321. run_tox
  322. fi
  323. exit 0