freedombone-mesh 11KB

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