freedombone-mesh 11KB

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