freedombone-mesh 11KB

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