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