freedombone-mesh 11KB

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