freedombone-app-scuttlebot 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # scuttlebot pub application. Enables nat traversal for SSB.
  12. # https://scuttlebot.io
  13. #
  14. # License
  15. # =======
  16. #
  17. # Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
  18. #
  19. # This program is free software: you can redistribute it and/or modify
  20. # it under the terms of the GNU Affero General Public License as published by
  21. # the Free Software Foundation, either version 3 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU Affero General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU Affero General Public License
  30. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. VARIANTS='full full-vim social'
  32. IN_DEFAULT_INSTALL=0
  33. SHOW_ON_ABOUT=0
  34. SHOW_ICANN_ADDRESS_ON_ABOUT=0
  35. SCUTTLEBOT_VERSION='10.4.6'
  36. SCUTTLEBOT_PORT=8010
  37. scuttlebot_variables=(MY_USERNAME
  38. DEFAULT_DOMAIN_NAME
  39. SYSTEM_TYPE)
  40. function logging_on_scuttlebot {
  41. echo -n ''
  42. }
  43. function logging_off_scuttlebot {
  44. echo -n ''
  45. }
  46. function scuttlebot_create_invite {
  47. invite_string=$(su -c "sbot invite.create 1" - scuttlebot | sed 's/"//g')
  48. clear
  49. echo -e '\n\nYour Scuttlebot invite code is:\n\n'${invite_string}'\n\n'
  50. read -n1 -r -p $"Press any key to continue..." key
  51. }
  52. function configure_interactive_scuttlebot {
  53. while true
  54. do
  55. data=$(tempfile 2>/dev/null)
  56. trap "rm -f $data" 0 1 2 5 15
  57. dialog --backtitle $"Freedombone Control Panel" \
  58. --title $"Scuttlebot" \
  59. --radiolist $"Choose an operation:" 10 50 2 \
  60. 1 $"Create an invite" off \
  61. 2 $"Exit" on 2> $data
  62. sel=$?
  63. case $sel in
  64. 1) return;;
  65. 255) return;;
  66. esac
  67. case $(cat $data) in
  68. 1) scuttlebot_create_invite;;
  69. 2) break;;
  70. esac
  71. done
  72. }
  73. function remove_user_scuttlebot {
  74. remove_username="$1"
  75. }
  76. function add_user_scuttlebot {
  77. new_username="$1"
  78. new_user_password="$2"
  79. echo '0'
  80. }
  81. function install_interactive_scuttlebot {
  82. echo -n ''
  83. APP_INSTALLED=1
  84. }
  85. function change_password_scuttlebot {
  86. new_username="$1"
  87. new_user_password="$2"
  88. echo '0'
  89. }
  90. function reconfigure_scuttlebot {
  91. if [ -d /etc/scuttlebot/.ssb ]; then
  92. systemctl stop scuttlebot
  93. rm -rf /etc/scuttlebot/.ssb
  94. systemctl start scuttlebot
  95. fi
  96. }
  97. function upgrade_scuttlebot {
  98. if ! grep -q 'scuttlebot version:' $COMPLETION_FILE; then
  99. return
  100. fi
  101. CURR_SCUTTLEBOT_VERSION=$(get_completion_param "scuttlebot version")
  102. echo "scuttlebot current version: ${CURR_SCUTTLEBOT_VERSION}"
  103. echo "scuttlebot app version: ${SCUTTLEBOT_VERSION}"
  104. if [[ "${CURR_SCUTTLEBOT_VERSION}" == "${SCUTTLEBOT_VERSION}" ]]; then
  105. return
  106. fi
  107. npm upgrade -g scuttlebot@${SCUTTLEBOT_VERSION} --save
  108. if [ ! "$?" = "0" ]; then
  109. return
  110. fi
  111. sed -i "s|scuttlebot version.*|scuttlebot version:${SCUTTLEBOT_VERSION}|g" ${COMPLETION_FILE}
  112. }
  113. function backup_local_scuttlebot {
  114. if [ -d /etc/scuttlebot/.ssb ]; then
  115. systemctl stop scuttlebot
  116. function_check backup_directory_to_usb
  117. backup_directory_to_usb /etc/scuttlebot/.ssb scuttlebot
  118. systemctl start scuttlebot
  119. fi
  120. }
  121. function restore_local_scuttlebot {
  122. if [ -d /etc/scuttlebot ]; then
  123. systemctl stop scuttlebot
  124. temp_restore_dir=/root/tempscuttlebot
  125. function_check restore_directory_from_usb
  126. restore_directory_from_usb $temp_restore_dir scuttlebot
  127. if [ -d $temp_restore_dir/etc/scuttlebot/.ssb ]; then
  128. cp -r $temp_restore_dir/etc/scuttlebot/.ssb /etc/scuttlebot/
  129. else
  130. cp -r $temp_restore_dir/* /etc/scuttlebot/.ssb/*
  131. fi
  132. systemctl start scuttlebot
  133. rm -rf $temp_restore_dir
  134. fi
  135. }
  136. function backup_remote_scuttlebot {
  137. if [ -d /etc/scuttlebot/.ssb ]; then
  138. systemctl stop scuttlebot
  139. function_check backup_directory_to_friend
  140. backup_directory_to_friend /etc/scuttlebot/.ssb scuttlebot
  141. systemctl start scuttlebot
  142. fi
  143. }
  144. function restore_remote_scuttlebot {
  145. if [ -d /etc/scuttlebot ]; then
  146. systemctl stop scuttlebot
  147. temp_restore_dir=/root/tempscuttlebot
  148. function_check restore_directory_from_friend
  149. restore_directory_from_friend $temp_restore_dir scuttlebot
  150. if [ -d $temp_restore_dir/etc/scuttlebot/.ssb ]; then
  151. cp -r $temp_restore_dir/etc/scuttlebot/.ssb /etc/scuttlebot/
  152. else
  153. cp -r $temp_restore_dir/* /etc/scuttlebot/.ssb/*
  154. fi
  155. systemctl start scuttlebot
  156. rm -rf $temp_restore_dir
  157. fi
  158. }
  159. function remove_scuttlebot {
  160. firewall_remove ${SCUTTLEBOT_PORT}
  161. systemctl stop scuttlebot
  162. systemctl disable scuttlebot
  163. rm /etc/systemd/system/scuttlebot.service
  164. systemctl daemon-reload
  165. userdel -r scuttlebot
  166. if [ -d /etc/scuttlebot ]; then
  167. rm -rf /etc/scuttlebot
  168. fi
  169. remove_completion_param install_scuttlebot
  170. sed -i '/scuttlebot /d' $COMPLETION_FILE
  171. }
  172. function mesh_install_scuttlebot {
  173. cat <<EOF > $rootdir/usr/bin/install_scuttlebot
  174. #!/bin/bash
  175. npm install -g scuttlebot@${SCUTTLEBOT_VERSION}
  176. EOF
  177. chroot "$rootdir" /bin/chmod +x /usr/bin/install_scuttlebot
  178. chroot "$rootdir" /usr/bin/install_scuttlebot
  179. rm $rootdir/usr/bin/install_scuttlebot
  180. if [ ! -f $rootdir/usr/local/bin/sbot ]; then
  181. echo $'Scuttlebot was not installed'
  182. exit 528253
  183. fi
  184. if [ ! -d $rootdir/etc/scuttlebot ]; then
  185. mkdir -p $rootdir/etc/scuttlebot
  186. fi
  187. # an unprivileged user to run as
  188. chroot "$rootdir" useradd -d /etc/scuttlebot/ scuttlebot
  189. # daemon
  190. echo '[Unit]' > $rootdir/etc/systemd/system/scuttlebot.service
  191. echo 'Description=Scuttlebot (messaging system)' >> $rootdir/etc/systemd/system/scuttlebot.service
  192. echo 'After=syslog.target' >> $rootdir/etc/systemd/system/scuttlebot.service
  193. echo 'After=network.target' >> $rootdir/etc/systemd/system/scuttlebot.service
  194. echo '' >> $rootdir/etc/systemd/system/scuttlebot.service
  195. echo '[Service]' >> $rootdir/etc/systemd/system/scuttlebot.service
  196. echo 'Type=simple' >> $rootdir/etc/systemd/system/scuttlebot.service
  197. echo 'User=scuttlebot' >> $rootdir/etc/systemd/system/scuttlebot.service
  198. echo 'Group=scuttlebot' >> $rootdir/etc/systemd/system/scuttlebot.service
  199. echo "WorkingDirectory=/etc/scuttlebot" >> $rootdir/etc/systemd/system/scuttlebot.service
  200. echo 'ExecStart=/usr/local/bin/sbot server' >> $rootdir/etc/systemd/system/scuttlebot.service
  201. echo 'Restart=always' >> $rootdir/etc/systemd/system/scuttlebot.service
  202. echo 'Environment="USER=scuttlebot"' >> $rootdir/etc/systemd/system/scuttlebot.service
  203. echo '' >> $rootdir/etc/systemd/system/scuttlebot.service
  204. echo '[Install]' >> $rootdir/etc/systemd/system/scuttlebot.service
  205. echo 'WantedBy=multi-user.target' >> $rootdir/etc/systemd/system/scuttlebot.service
  206. }
  207. function install_scuttlebot {
  208. function_check install_nodejs
  209. install_nodejs scuttlebot
  210. npm install -g scuttlebot@${SCUTTLEBOT_VERSION}
  211. if [ ! -f /usr/local/bin/sbot ]; then
  212. exit 528253
  213. fi
  214. if [ ! -d /etc/scuttlebot ]; then
  215. mkdir -p /etc/scuttlebot
  216. fi
  217. # an unprivileged user to run as
  218. useradd -d /etc/scuttlebot/ scuttlebot
  219. # daemon
  220. echo '[Unit]' > /etc/systemd/system/scuttlebot.service
  221. echo 'Description=Scuttlebot (messaging system)' >> /etc/systemd/system/scuttlebot.service
  222. echo 'After=syslog.target' >> /etc/systemd/system/scuttlebot.service
  223. echo 'After=network.target' >> /etc/systemd/system/scuttlebot.service
  224. echo '' >> /etc/systemd/system/scuttlebot.service
  225. echo '[Service]' >> /etc/systemd/system/scuttlebot.service
  226. echo 'Type=simple' >> /etc/systemd/system/scuttlebot.service
  227. echo 'User=scuttlebot' >> /etc/systemd/system/scuttlebot.service
  228. echo 'Group=scuttlebot' >> /etc/systemd/system/scuttlebot.service
  229. echo "WorkingDirectory=/etc/scuttlebot" >> /etc/systemd/system/scuttlebot.service
  230. echo 'ExecStart=/usr/local/bin/sbot server' >> /etc/systemd/system/scuttlebot.service
  231. echo 'Restart=always' >> /etc/systemd/system/scuttlebot.service
  232. echo 'Environment="USER=scuttlebot"' >> /etc/systemd/system/scuttlebot.service
  233. echo '' >> /etc/systemd/system/scuttlebot.service
  234. echo '[Install]' >> /etc/systemd/system/scuttlebot.service
  235. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/scuttlebot.service
  236. chown -R scuttlebot:scuttlebot /etc/scuttlebot
  237. # files gw_name myhostname mdns4_minimal [NOTFOUND=return] dns
  238. sed -i "s|hosts:.*|hosts: files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf
  239. # start the daemon
  240. systemctl enable scuttlebot.service
  241. systemctl daemon-reload
  242. systemctl start scuttlebot.service
  243. sleep 3
  244. if [ ! -d /etc/scuttlebot/.ssb ]; then
  245. echo $'Scuttlebot config not generated'
  246. exit 73528
  247. fi
  248. echo '{' > /etc/scuttlebot/.ssb/config
  249. echo " \"host\": \"${DEFAULT_DOMAIN_NAME}\"," >> /etc/scuttlebot/.ssb/config
  250. echo " \"port\": ${SCUTTLEBOT_PORT}," >> /etc/scuttlebot/.ssb/config
  251. echo ' "timeout": 30000,' >> /etc/scuttlebot/.ssb/config
  252. echo ' "pub": true,' >> /etc/scuttlebot/.ssb/config
  253. echo ' "local": true,' >> /etc/scuttlebot/.ssb/config
  254. echo ' "friends": {' >> /etc/scuttlebot/.ssb/config
  255. echo ' "dunbar": 150,' >> /etc/scuttlebot/.ssb/config
  256. echo ' "hops": 3' >> /etc/scuttlebot/.ssb/config
  257. echo ' },' >> /etc/scuttlebot/.ssb/config
  258. echo ' "gossip": {' >> /etc/scuttlebot/.ssb/config
  259. echo ' "connections": 2' >> /etc/scuttlebot/.ssb/config
  260. echo ' },' >> /etc/scuttlebot/.ssb/config
  261. echo ' "master": [],' >> /etc/scuttlebot/.ssb/config
  262. echo ' "logging": {' >> /etc/scuttlebot/.ssb/config
  263. echo ' "level": "error"' >> /etc/scuttlebot/.ssb/config
  264. echo ' }' >> /etc/scuttlebot/.ssb/config
  265. echo '}' >> /etc/scuttlebot/.ssb/config
  266. chown scuttlebot:scuttlebot /etc/scuttlebot/.ssb/config
  267. systemctl restart scuttlebot.service
  268. firewall_add scuttlebot ${SCUTTLEBOT_PORT}
  269. if ! grep -q "scuttlebot version:" ${COMPLETION_FILE}; then
  270. echo "scuttlebot version:${SCUTTLEBOT_VERSION}" >> ${COMPLETION_FILE}
  271. else
  272. sed -i "s|scuttlebot version.*|scuttlebot version:${SCUTTLEBOT_VERSION}|g" ${COMPLETION_FILE}
  273. fi
  274. APP_INSTALLED=1
  275. }
  276. # NOTE: deliberately no exit 0