freedombone-mesh 12KB

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