freedombone-app-scuttlebot 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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_DOMAIN_NAME=
  36. SCUTTLEBOT_CODE=
  37. SCUTTLEBOT_VERSION='10.4.6'
  38. SCUTTLEBOT_PORT=8010
  39. SCUTTLEBOT_ONION_PORT=8623
  40. GIT_SSB_PORT=7718
  41. NGINX_GIT_SSB_PORT=7719
  42. scuttlebot_variables=(MY_USERNAME
  43. SCUTTLEBOT_DOMAIN_NAME
  44. SCUTTLEBOT_CODE
  45. DEFAULT_DOMAIN_NAME
  46. SYSTEM_TYPE)
  47. function logging_on_scuttlebot {
  48. echo -n ''
  49. }
  50. function logging_off_scuttlebot {
  51. echo -n ''
  52. }
  53. function scuttlebot_create_invite {
  54. invite_string=$(su -c "sbot invite.create 1" - scuttlebot | sed 's/"//g')
  55. clear
  56. echo -e '\n\nYour Scuttlebot invite code is:\n\n'${invite_string}'\n\n'
  57. read -n1 -r -p $"Press any key to continue..." key
  58. }
  59. function configure_interactive_scuttlebot {
  60. while true
  61. do
  62. data=$(tempfile 2>/dev/null)
  63. trap "rm -f $data" 0 1 2 5 15
  64. dialog --backtitle $"Freedombone Control Panel" \
  65. --title $"Scuttlebot" \
  66. --radiolist $"Choose an operation:" 10 50 2 \
  67. 1 $"Create an invite" off \
  68. 2 $"Exit" on 2> $data
  69. sel=$?
  70. case $sel in
  71. 1) return;;
  72. 255) return;;
  73. esac
  74. case $(cat $data) in
  75. 1) scuttlebot_create_invite;;
  76. 2) break;;
  77. esac
  78. done
  79. }
  80. function remove_user_scuttlebot {
  81. remove_username="$1"
  82. }
  83. function add_user_scuttlebot {
  84. new_username="$1"
  85. new_user_password="$2"
  86. echo '0'
  87. }
  88. function install_interactive_scuttlebot {
  89. if [[ $ONION_ONLY != "no" ]]; then
  90. SCUTTLEBOT_DOMAIN_NAME='scuttlebot.local'
  91. write_config_param "SCUTTLEBOT_DOMAIN_NAME" "$SCUTTLEBOT_DOMAIN_NAME"
  92. else
  93. function_check interactive_site_details
  94. interactive_site_details scuttlebot
  95. fi
  96. APP_INSTALLED=1
  97. }
  98. function change_password_scuttlebot {
  99. new_username="$1"
  100. new_user_password="$2"
  101. echo '0'
  102. }
  103. function reconfigure_scuttlebot {
  104. if [ -d /etc/scuttlebot/.ssb ]; then
  105. systemctl stop scuttlebot
  106. rm -rf /etc/scuttlebot/.ssb
  107. systemctl start scuttlebot
  108. fi
  109. }
  110. function upgrade_scuttlebot {
  111. if ! grep -q 'scuttlebot version:' $COMPLETION_FILE; then
  112. return
  113. fi
  114. CURR_SCUTTLEBOT_VERSION=$(get_completion_param "scuttlebot version")
  115. echo "scuttlebot current version: ${CURR_SCUTTLEBOT_VERSION}"
  116. echo "scuttlebot app version: ${SCUTTLEBOT_VERSION}"
  117. if [[ "${CURR_SCUTTLEBOT_VERSION}" == "${SCUTTLEBOT_VERSION}" ]]; then
  118. return
  119. fi
  120. npm upgrade -g scuttlebot@${SCUTTLEBOT_VERSION} --save
  121. if [ ! "$?" = "0" ]; then
  122. return
  123. fi
  124. sed -i "s|scuttlebot version.*|scuttlebot version:${SCUTTLEBOT_VERSION}|g" ${COMPLETION_FILE}
  125. }
  126. function backup_local_scuttlebot {
  127. if [ -d /etc/scuttlebot/.ssb ]; then
  128. systemctl stop scuttlebot
  129. function_check backup_directory_to_usb
  130. backup_directory_to_usb /etc/scuttlebot/.ssb scuttlebot
  131. systemctl start scuttlebot
  132. fi
  133. }
  134. function restore_local_scuttlebot {
  135. if [ -d /etc/scuttlebot ]; then
  136. systemctl stop scuttlebot
  137. temp_restore_dir=/root/tempscuttlebot
  138. function_check restore_directory_from_usb
  139. restore_directory_from_usb $temp_restore_dir scuttlebot
  140. if [ -d $temp_restore_dir/etc/scuttlebot/.ssb ]; then
  141. cp -r $temp_restore_dir/etc/scuttlebot/.ssb /etc/scuttlebot/
  142. else
  143. cp -r $temp_restore_dir/* /etc/scuttlebot/.ssb/*
  144. fi
  145. systemctl start scuttlebot
  146. rm -rf $temp_restore_dir
  147. fi
  148. }
  149. function backup_remote_scuttlebot {
  150. if [ -d /etc/scuttlebot/.ssb ]; then
  151. systemctl stop scuttlebot
  152. function_check backup_directory_to_friend
  153. backup_directory_to_friend /etc/scuttlebot/.ssb scuttlebot
  154. systemctl start scuttlebot
  155. fi
  156. }
  157. function restore_remote_scuttlebot {
  158. if [ -d /etc/scuttlebot ]; then
  159. systemctl stop scuttlebot
  160. temp_restore_dir=/root/tempscuttlebot
  161. function_check restore_directory_from_friend
  162. restore_directory_from_friend $temp_restore_dir scuttlebot
  163. if [ -d $temp_restore_dir/etc/scuttlebot/.ssb ]; then
  164. cp -r $temp_restore_dir/etc/scuttlebot/.ssb /etc/scuttlebot/
  165. else
  166. cp -r $temp_restore_dir/* /etc/scuttlebot/.ssb/*
  167. fi
  168. systemctl start scuttlebot
  169. rm -rf $temp_restore_dir
  170. fi
  171. }
  172. function remove_scuttlebot {
  173. firewall_remove ${SCUTTLEBOT_PORT}
  174. firewall_remove ${GIT_SSB_PORT}
  175. if [ $SCUTTLEBOT_DOMAIN_NAME ]; then
  176. nginx_dissite ${SCUTTLEBOT_DOMAIN_NAME}
  177. rm /etc/nginx/sites-available/${SCUTTLEBOT_DOMAIN_NAME}
  178. fi
  179. systemctl stop scuttlebot
  180. systemctl disable scuttlebot
  181. rm /etc/systemd/system/scuttlebot.service
  182. systemctl daemon-reload
  183. userdel -r scuttlebot
  184. if [ -d /etc/scuttlebot ]; then
  185. rm -rf /etc/scuttlebot
  186. fi
  187. remove_completion_param install_scuttlebot
  188. sed -i '/scuttlebot /d' $COMPLETION_FILE
  189. }
  190. function scuttlebot_git_setup {
  191. if [[ "$1" == "mesh" ]]; then
  192. git_ssb_nginx_site=$rootdir/etc/nginx/sites-available/git_ssb
  193. echo 'server {' > $git_ssb_nginx_site
  194. echo " listen $NGINX_GIT_SSB_PORT default_server;" >> $git_ssb_nginx_site
  195. echo " server_name P${PEER_ID}.local;" >> $git_ssb_nginx_site
  196. echo '' >> $git_ssb_nginx_site
  197. echo ' access_log /dev/null;' >> $git_ssb_nginx_site
  198. echo ' error_log /dev/null;' >> $git_ssb_nginx_site
  199. echo '' >> $git_ssb_nginx_site
  200. echo ' add_header X-XSS-Protection "1; mode=block";' >> $git_ssb_nginx_site
  201. echo ' add_header X-Content-Type-Options nosniff;' >> $git_ssb_nginx_site
  202. echo ' add_header X-Frame-Options SAMEORIGIN;' >> $git_ssb_nginx_site
  203. else
  204. if [ ! $SCUTTLEBOT_DOMAIN_NAME ]; then
  205. exit 7357225
  206. fi
  207. git_ssb_nginx_site=/etc/nginx/sites-available/${SCUTTLEBOT_DOMAIN_NAME}
  208. function_check nginx_http_redirect
  209. nginx_http_redirect $SCUTTLEBOT_DOMAIN_NAME "index index.html"
  210. echo 'server {' >> $git_ssb_nginx_site
  211. echo ' listen 443 ssl;' >> $git_ssb_nginx_site
  212. echo ' listen [::]:443 ssl;' >> $git_ssb_nginx_site
  213. echo " server_name $SCUTTLEBOT_DOMAIN_NAME;" >> $git_ssb_nginx_site
  214. echo '' >> $git_ssb_nginx_site
  215. function_check nginx_compress
  216. nginx_compress $SCUTTLEBOT_DOMAIN_NAME
  217. echo '' >> $git_ssb_nginx_site
  218. echo ' # Security' >> $git_ssb_nginx_site
  219. function_check nginx_ssl
  220. nginx_ssl $SCUTTLEBOT_DOMAIN_NAME
  221. function_check nginx_disable_sniffing
  222. nginx_disable_sniffing $SCUTTLEBOT_DOMAIN_NAME
  223. fi
  224. echo '' >> $git_ssb_nginx_site
  225. echo ' location = / {' >> $git_ssb_nginx_site
  226. echo " proxy_pass http://localhost:${GIT_SSB_PORT};" >> $git_ssb_nginx_site
  227. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $git_ssb_nginx_site
  228. echo ' proxy_set_header Host $host;' >> $git_ssb_nginx_site
  229. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $git_ssb_nginx_site
  230. echo ' proxy_http_version 1.1;' >> $git_ssb_nginx_site
  231. echo ' proxy_set_header Upgrade $http_upgrade;' >> $git_ssb_nginx_site
  232. echo ' proxy_set_header Connection upgrade;' >> $git_ssb_nginx_site
  233. echo ' }' >> $git_ssb_nginx_site
  234. echo '}' >> $git_ssb_nginx_site
  235. if [ $SCUTTLEBOT_ONION_HOSTNAME ]; then
  236. echo '' >> $git_ssb_nginx_site
  237. echo 'server {' >> $git_ssb_nginx_site
  238. echo " listen 127.0.0.1:${SCUTTLEBOT_ONION_PORT} default_server;" >> $git_ssb_nginx_site
  239. echo " server_name ${SCUTTLEBOT_ONION_HOSTNAME};" >> $git_ssb_nginx_site
  240. echo '' >> $git_ssb_nginx_site
  241. echo ' access_log /dev/null;' >> $git_ssb_nginx_site
  242. echo ' error_log /dev/null;' >> $git_ssb_nginx_site
  243. echo '' >> $git_ssb_nginx_site
  244. echo ' add_header X-XSS-Protection "1; mode=block";' >> $git_ssb_nginx_site
  245. echo ' add_header X-Content-Type-Options nosniff;' >> $git_ssb_nginx_site
  246. echo ' add_header X-Frame-Options SAMEORIGIN;' >> $git_ssb_nginx_site
  247. echo '' >> $git_ssb_nginx_site
  248. echo ' location = / {' >> $git_ssb_nginx_site
  249. echo " proxy_pass http://localhost:${GIT_SSB_PORT};" >> $git_ssb_nginx_site
  250. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $git_ssb_nginx_site
  251. echo ' proxy_set_header Host $host;' >> $git_ssb_nginx_site
  252. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $git_ssb_nginx_site
  253. echo ' proxy_http_version 1.1;' >> $git_ssb_nginx_site
  254. echo ' proxy_set_header Upgrade $http_upgrade;' >> $git_ssb_nginx_site
  255. echo ' proxy_set_header Connection upgrade;' >> $git_ssb_nginx_site
  256. echo ' }' >> $git_ssb_nginx_site
  257. echo '}' >> $git_ssb_nginx_site
  258. fi
  259. nginx_ensite git_ssb
  260. }
  261. function mesh_install_scuttlebot {
  262. SCUTTLEBOT_ONION_HOSTNAME=
  263. get_npm_arch
  264. cat <<EOF > $rootdir/usr/bin/install_scuttlebot
  265. #!/bin/bash
  266. npm install --arch=$NPM_ARCH -g scuttlebot@${SCUTTLEBOT_VERSION}
  267. npm install --arch=$NPM_ARCH -g git-ssb
  268. npm install --arch=$NPM_ARCH -g git-remote-ssb
  269. EOF
  270. chroot "$rootdir" /bin/chmod +x /usr/bin/install_scuttlebot
  271. chroot "$rootdir" /usr/bin/install_scuttlebot
  272. rm $rootdir/usr/bin/install_scuttlebot
  273. if [ ! -f $rootdir/usr/local/bin/sbot ]; then
  274. echo $'Scuttlebot was not installed'
  275. exit 528253
  276. fi
  277. if [ ! -d $rootdir/etc/scuttlebot ]; then
  278. mkdir -p $rootdir/etc/scuttlebot
  279. fi
  280. # an unprivileged user to run as
  281. chroot "$rootdir" useradd -d /etc/scuttlebot/ scuttlebot
  282. # daemon
  283. echo '[Unit]' > $rootdir/etc/systemd/system/scuttlebot.service
  284. echo 'Description=Scuttlebot (messaging system)' >> $rootdir/etc/systemd/system/scuttlebot.service
  285. echo 'After=syslog.target' >> $rootdir/etc/systemd/system/scuttlebot.service
  286. echo 'After=network.target' >> $rootdir/etc/systemd/system/scuttlebot.service
  287. echo '' >> $rootdir/etc/systemd/system/scuttlebot.service
  288. echo '[Service]' >> $rootdir/etc/systemd/system/scuttlebot.service
  289. echo 'Type=simple' >> $rootdir/etc/systemd/system/scuttlebot.service
  290. echo 'User=scuttlebot' >> $rootdir/etc/systemd/system/scuttlebot.service
  291. echo 'Group=scuttlebot' >> $rootdir/etc/systemd/system/scuttlebot.service
  292. echo "WorkingDirectory=/etc/scuttlebot" >> $rootdir/etc/systemd/system/scuttlebot.service
  293. echo 'ExecStart=/usr/local/bin/sbot server' >> $rootdir/etc/systemd/system/scuttlebot.service
  294. echo 'Restart=always' >> $rootdir/etc/systemd/system/scuttlebot.service
  295. echo 'Environment="USER=scuttlebot"' >> $rootdir/etc/systemd/system/scuttlebot.service
  296. echo '' >> $rootdir/etc/systemd/system/scuttlebot.service
  297. echo '[Install]' >> $rootdir/etc/systemd/system/scuttlebot.service
  298. echo 'WantedBy=multi-user.target' >> $rootdir/etc/systemd/system/scuttlebot.service
  299. scuttlebot_git_setup mesh
  300. }
  301. function install_scuttlebot {
  302. function_check install_nodejs
  303. install_nodejs scuttlebot
  304. npm install -g scuttlebot@${SCUTTLEBOT_VERSION}
  305. if [ ! -f /usr/local/bin/sbot ]; then
  306. exit 528253
  307. fi
  308. npm install -g git-ssb
  309. npm install -g git-remote-ssb
  310. if [ ! -d /etc/scuttlebot ]; then
  311. mkdir -p /etc/scuttlebot
  312. fi
  313. # an unprivileged user to run as
  314. useradd -d /etc/scuttlebot/ scuttlebot
  315. # daemon
  316. echo '[Unit]' > /etc/systemd/system/scuttlebot.service
  317. echo 'Description=Scuttlebot (messaging system)' >> /etc/systemd/system/scuttlebot.service
  318. echo 'After=syslog.target' >> /etc/systemd/system/scuttlebot.service
  319. echo 'After=network.target' >> /etc/systemd/system/scuttlebot.service
  320. echo '' >> /etc/systemd/system/scuttlebot.service
  321. echo '[Service]' >> /etc/systemd/system/scuttlebot.service
  322. echo 'Type=simple' >> /etc/systemd/system/scuttlebot.service
  323. echo 'User=scuttlebot' >> /etc/systemd/system/scuttlebot.service
  324. echo 'Group=scuttlebot' >> /etc/systemd/system/scuttlebot.service
  325. echo "WorkingDirectory=/etc/scuttlebot" >> /etc/systemd/system/scuttlebot.service
  326. echo 'ExecStart=/usr/local/bin/sbot server' >> /etc/systemd/system/scuttlebot.service
  327. echo 'Restart=always' >> /etc/systemd/system/scuttlebot.service
  328. echo 'Environment="USER=scuttlebot"' >> /etc/systemd/system/scuttlebot.service
  329. echo '' >> /etc/systemd/system/scuttlebot.service
  330. echo '[Install]' >> /etc/systemd/system/scuttlebot.service
  331. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/scuttlebot.service
  332. chown -R scuttlebot:scuttlebot /etc/scuttlebot
  333. # files gw_name myhostname mdns4_minimal [NOTFOUND=return] dns
  334. sed -i "s|hosts:.*|hosts: files mdns4_minimal dns mdns4 mdns|g" /etc/nsswitch.conf
  335. # start the daemon
  336. systemctl enable scuttlebot.service
  337. systemctl daemon-reload
  338. systemctl start scuttlebot.service
  339. sleep 3
  340. if [ ! -d /etc/scuttlebot/.ssb ]; then
  341. echo $'Scuttlebot config not generated'
  342. exit 73528
  343. fi
  344. echo '{' > /etc/scuttlebot/.ssb/config
  345. echo " \"host\": \"${DEFAULT_DOMAIN_NAME}\"," >> /etc/scuttlebot/.ssb/config
  346. echo " \"port\": ${SCUTTLEBOT_PORT}," >> /etc/scuttlebot/.ssb/config
  347. echo ' "timeout": 30000,' >> /etc/scuttlebot/.ssb/config
  348. echo ' "pub": true,' >> /etc/scuttlebot/.ssb/config
  349. echo ' "local": true,' >> /etc/scuttlebot/.ssb/config
  350. echo ' "friends": {' >> /etc/scuttlebot/.ssb/config
  351. echo ' "dunbar": 150,' >> /etc/scuttlebot/.ssb/config
  352. echo ' "hops": 3' >> /etc/scuttlebot/.ssb/config
  353. echo ' },' >> /etc/scuttlebot/.ssb/config
  354. echo ' "gossip": {' >> /etc/scuttlebot/.ssb/config
  355. echo ' "connections": 2' >> /etc/scuttlebot/.ssb/config
  356. echo ' },' >> /etc/scuttlebot/.ssb/config
  357. echo ' "master": [],' >> /etc/scuttlebot/.ssb/config
  358. echo ' "logging": {' >> /etc/scuttlebot/.ssb/config
  359. echo ' "level": "error"' >> /etc/scuttlebot/.ssb/config
  360. echo ' }' >> /etc/scuttlebot/.ssb/config
  361. echo '}' >> /etc/scuttlebot/.ssb/config
  362. chown scuttlebot:scuttlebot /etc/scuttlebot/.ssb/config
  363. systemctl restart scuttlebot.service
  364. firewall_add scuttlebot ${SCUTTLEBOT_PORT}
  365. firewall_add git_ssb ${GIT_SSB_PORT}
  366. SCUTTLEBOT_ONION_HOSTNAME=$(add_onion_service scuttlebot 80 ${SCUTTLEBOT_ONION_PORT})
  367. scuttlebot_git_setup
  368. function_check create_site_certificate
  369. create_site_certificate ${SCUTTLEBOT_DOMAIN_NAME} 'yes'
  370. systemctl restart nginx
  371. if ! grep -q "scuttlebot version:" ${COMPLETION_FILE}; then
  372. echo "scuttlebot version:${SCUTTLEBOT_VERSION}" >> ${COMPLETION_FILE}
  373. else
  374. sed -i "s|scuttlebot version.*|scuttlebot version:${SCUTTLEBOT_VERSION}|g" ${COMPLETION_FILE}
  375. fi
  376. APP_INSTALLED=1
  377. }
  378. # NOTE: deliberately no exit 0