freedombone-app-cryptpad 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # cryptpad application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2017 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. VARIANTS='full full-vim writer'
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. SHOW_ICANN_ADDRESS_ON_ABOUT=0
  34. CRYPTPAD_ONION_PORT=8119
  35. CRYPTPAD_PORT=9003
  36. CRYPTPAD_PORT2=9005
  37. CRYPTPAD_REPO="https://github.com/xwiki-labs/cryptpad"
  38. CRYPTPAD_COMMIT='cfc9ce33d6c7034a13cb0e6d606de6fba38697e0'
  39. CRYPTPAD_DIR=/etc/cryptpad
  40. cryptpad_variables=(ONION_ONLY)
  41. function logging_on_cryptpad {
  42. echo -n ''
  43. }
  44. function logging_off_cryptpad {
  45. echo -n ''
  46. }
  47. function remove_user_cryptpad {
  48. remove_username="$1"
  49. }
  50. function add_user_cryptpad {
  51. new_username="$1"
  52. new_user_password="$2"
  53. echo '0'
  54. }
  55. function install_interactive_cryptpad {
  56. echo -n ''
  57. APP_INSTALLED=1
  58. }
  59. function change_password_cryptpad {
  60. curr_username="$1"
  61. new_user_password="$2"
  62. }
  63. function reconfigure_cryptpad {
  64. if [ -d $CRYPTPAD_DIR/datastore ]; then
  65. rm -rf $CRYPTPAD_DIR/datastore
  66. fi
  67. }
  68. function cryptpad_generate_api_config {
  69. if [ ! -d $CRYPTPAD_DIR/customize/api ]; then
  70. mkdir -p $CRYPTPAD_DIR/customize/api
  71. fi
  72. wget 127.0.0.1:$CRYPTPAD_PORT/api/config -O $CRYPTPAD_DIR/customize/api/config
  73. if [ ! -f $CRYPTPAD_DIR/customize/api/config ]; then
  74. echo $'Unable to wget api/config'
  75. exit 89252
  76. fi
  77. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  78. }
  79. function upgrade_cryptpad {
  80. CURR_CRYPTPAD_COMMIT=$(get_completion_param "cryptpad commit")
  81. if [[ "$CURR_CRYPTPAD_COMMIT" == "$CRYPTPAD_COMMIT" ]]; then
  82. return
  83. fi
  84. systemctl stop cryptpad
  85. # update to the next commit
  86. function_check set_repo_commit
  87. set_repo_commit $CRYPTPAD_DIR "cryptpad commit" "$CRYPTPAD_COMMIT" $CRYPTPAD_REPO
  88. cd $CRYPTPAD_DIR
  89. cryptpad_create_config
  90. npm upgrade
  91. npm install
  92. su -c 'echo "2" | bower update' - cryptpad
  93. su -c 'bower install --config.interactive=false' - cryptpad
  94. cryptpad_generate_api_config
  95. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  96. systemctl start cryptpad
  97. }
  98. function backup_local_cryptpad {
  99. source_directory=$CRYPTPAD_DIR/datastore
  100. if [ -d $source_directory ]; then
  101. systemctl stop cryptpad
  102. dest_directory=cryptpad
  103. function_check suspend_site
  104. suspend_site cryptpad
  105. function_check backup_directory_to_usb
  106. backup_directory_to_usb $source_directory $dest_directory
  107. function_check restart_site
  108. restart_site
  109. systemctl start cryptpad
  110. fi
  111. }
  112. function restore_local_cryptpad {
  113. if [ -d $CRYPTPAD_DIR ]; then
  114. systemctl stop cryptpad
  115. temp_restore_dir=/root/tempcryptpad
  116. function_check restore_directory_from_usb
  117. restore_directory_from_usb $temp_restore_dir cryptpad
  118. if [ ! -d $temp_restore_dir$CRYPTPAD_DIR/datastore ]; then
  119. if [ -d $temp_restore_dir ]; then
  120. cp -r $temp_restore_dir/* $CRYPTPAD_DIR/datastore/
  121. else
  122. systemctl start cryptpad
  123. echo 'Failed to restore cryptpad'
  124. rm -rf $temp_restore_dir
  125. exit 8736529
  126. fi
  127. else
  128. cp -r $temp_restore_dir$CRYPTPAD_DIR/datastore/* $CRYPTPAD_DIR/datastore/
  129. fi
  130. rm -rf $temp_restore_dir
  131. systemctl start cryptpad
  132. fi
  133. }
  134. function backup_remote_cryptpad {
  135. if grep -q "cryptpad domain" $COMPLETION_FILE; then
  136. temp_backup_dir=$CRYPTPAD_DIR/datastore
  137. if [ -d $temp_backup_dir ]; then
  138. systemctl stop cryptpad
  139. function_check suspend_site
  140. suspend_site cryptpad
  141. echo $"Backing up Cryptpad installation"
  142. function_check backup_directory_to_friend
  143. backup_directory_to_friend $temp_backup_dir cryptpad
  144. function_check restart_site
  145. restart_site
  146. systemctl start cryptpad
  147. else
  148. echo $"cryptpad domain specified but not found in ${temp_backup_dir}"
  149. fi
  150. fi
  151. }
  152. function restore_remote_cryptpad {
  153. if [ -d $CRYPTPAD_DIR ]; then
  154. systemctl stop cryptpad
  155. temp_restore_dir=/root/tempcryptpad
  156. function_check restore_directory_from_usb
  157. restore_directory_from_friend $temp_restore_dir cryptpad
  158. if [ ! -d $temp_restore_dir$CRYPTPAD_DIR/datastore ]; then
  159. if [ -d $temp_restore_dir ]; then
  160. cp -r $temp_restore_dir/* $CRYPTPAD_DIR/datastore/
  161. else
  162. systemctl start cryptpad
  163. echo 'Failed to restore cryptpad'
  164. rm -rf $temp_restore_dir
  165. return
  166. fi
  167. else
  168. cp -r $temp_restore_dir$CRYPTPAD_DIR/datastore/* $CRYPTPAD_DIR/datastore/
  169. fi
  170. rm -rf $temp_restore_dir
  171. systemctl start cryptpad
  172. fi
  173. }
  174. function remove_cryptpad {
  175. systemctl stop cryptpad
  176. systemctl disable cryptpad
  177. if [ -f /etc/systemd/system/cryptpad.service ]; then
  178. rm /etc/systemd/system/cryptpad.service
  179. fi
  180. systemctl daemon-reload
  181. function_check remove_nodejs
  182. remove_nodejs cryptpad
  183. nginx_dissite cryptpad
  184. if [ -d $CRYPTPAD_DIR ]; then
  185. rm -rf $CRYPTPAD_DIR
  186. fi
  187. if [ -f /etc/nginx/sites-available/cryptpad ]; then
  188. rm /etc/nginx/sites-available/cryptpad
  189. fi
  190. function_check remove_onion_service
  191. remove_onion_service cryptpad ${CRYPTPAD_ONION_PORT}
  192. remove_app cryptpad
  193. remove_completion_param install_cryptpad
  194. sed -i '/cryptpad/d' $COMPLETION_FILE
  195. userdel -r cryptpad
  196. }
  197. function cryptpad_create_config {
  198. cryptpad_install_type=$1
  199. cryptpad_prefix=
  200. if [[ "$cryptpad_install_type" == "mesh" ]]; then
  201. cryptpad_prefix=$rootdir
  202. fi
  203. echo '/*@flow*/' > $cryptpad_prefix$CRYPTPAD_DIR/config.js
  204. echo '/*' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  205. echo ' globals module' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  206. echo '*/' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  207. echo "var domain = ' http://localhost:${CRYPTPAD_PORT}/';" >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  208. echo 'module.exports = {' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  209. echo " httpAddress: '::'," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  210. echo ' httpHeaders: {' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  211. echo ' "X-XSS-Protection": "1; mode=block",' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  212. echo ' "X-Content-Type-Options": "nosniff",' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  213. echo ' "Access-Control-Allow-Origin": "*"' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  214. echo ' },' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  215. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  216. echo ' contentSecurity: [' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  217. echo " \"default-src 'none'\"," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  218. echo " \"style-src 'unsafe-inline' 'self' \" + domain," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  219. echo " \"script-src 'self'\" + domain," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  220. echo " \"font-src 'self' data:\" + domain," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  221. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  222. echo ' "child-src blob: *",' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  223. echo ' "frame-src blob: *",' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  224. echo ' "media-src * blob:",' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  225. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  226. echo " \"connect-src 'self' ws: wss: blob:\" + domain," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  227. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  228. echo " \"img-src 'self' data: blob:\" + domain," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  229. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  230. echo ' "frame-ancestors *",' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  231. echo " ].join('; ')," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  232. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  233. echo ' padContentSecurity: [' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  234. echo " \"default-src 'none'\"," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  235. echo " \"style-src 'unsafe-inline' 'self'\" + domain," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  236. echo " \"script-src 'self' 'unsafe-eval' 'unsafe-inline'\" + domain," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  237. echo " \"font-src 'self'\" + domain," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  238. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  239. echo ' "child-src *",' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  240. echo ' "frame-src *",' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  241. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  242. echo " \"connect-src 'self' ws: wss:\" + domain," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  243. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  244. echo ' "img-src * blob:",' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  245. echo " ].join('; ')," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  246. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  247. echo " httpPort: ${CRYPTPAD_PORT}," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  248. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  249. echo ' // This is for allowing the cross-domain iframe to function when developing' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  250. echo " httpSafePort: ${CRYPTPAD_PORT2}," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  251. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  252. echo " websocketPath: '/cryptpad_websocket'," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  253. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  254. echo ' logToStdout: false,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  255. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  256. echo ' verbose: false,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  257. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  258. echo ' mainPages: [' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  259. echo " 'index'" >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  260. echo ' ],' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  261. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  262. echo ' removeDonateButton: true,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  263. echo ' allowSubscriptions: false,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  264. if [[ "$cryptpad_install_type" == "mesh" ]]; then
  265. echo " myDomain: 'http://P${PEER_ID}.local'," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  266. else
  267. CRYPTPAD_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_cryptpad/hostname)
  268. echo " myDomain: 'http://${CRYPTPAD_ONION_HOSTNAME}'," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  269. fi
  270. echo ' defaultStorageLimit: 50 * 1024 * 1024,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  271. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  272. echo ' customLimits: {' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  273. echo ' },' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  274. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  275. echo ' adminEmail: false,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  276. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  277. echo " storage: './storage/file'," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  278. echo '' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  279. echo " filePath: './datastore/'," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  280. echo " pinPath: './pins'," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  281. echo " blobPath: './blob'," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  282. echo " blobStagingPath: './blobstage'," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  283. echo ' channelExpirationMs: 30000,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  284. echo ' openFileLimit: 1024,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  285. echo " rpc: './rpc.js'," >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  286. echo ' suppressRPCErrors: false,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  287. echo ' enableUploads: true,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  288. echo ' //restrictUploads: false,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  289. echo ' maxUploadSize: 20 * 1024 * 1024,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  290. echo ' //logFeedback: true,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  291. echo ' //logRPC: true,' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  292. echo '};' >> $cryptpad_prefix$CRYPTPAD_DIR/config.js
  293. chown cryptpad:cryptpad $cryptpad_prefix$CRYPTPAD_DIR/config.js
  294. }
  295. function mesh_install_cryptpad {
  296. if [[ $VARIANT != "meshclient" && $VARIANT != "meshusb" ]]; then
  297. return
  298. fi
  299. if [ ! -d $rootdir/var/www/cryptpad ]; then
  300. mkdir $rootdir/var/www/cryptpad
  301. fi
  302. if [ -d $rootdir$CRYPTPAD_DIR ]; then
  303. rm -rf $rootdir$CRYPTPAD_DIR
  304. fi
  305. git_clone $CRYPTPAD_REPO $rootdir$CRYPTPAD_DIR
  306. if [ ! -d $rootdir$CRYPTPAD_DIR ]; then
  307. echo $'Unable to clone cryptpad repo'
  308. exit 783251
  309. fi
  310. # an unprivileged user to run as
  311. chroot "$rootdir" useradd -d $CRYPTPAD_DIR/ cryptpad
  312. cd $rootdir$CRYPTPAD_DIR
  313. git checkout $CRYPTPAD_COMMIT -b $CRYPTPAD_COMMIT
  314. chroot "$rootdir" chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  315. cryptpad_nginx_site=$rootdir/etc/nginx/sites-available/cryptpad
  316. echo 'server {' > $cryptpad_nginx_site
  317. echo " listen 80 default_server;" >> $cryptpad_nginx_site
  318. echo " server_name P${PEER_ID}.local;" >> $cryptpad_nginx_site
  319. echo '' >> $cryptpad_nginx_site
  320. echo ' # Logs' >> $cryptpad_nginx_site
  321. echo ' access_log /dev/null;' >> $cryptpad_nginx_site
  322. echo ' error_log /dev/null;' >> $cryptpad_nginx_site
  323. echo '' >> $cryptpad_nginx_site
  324. echo ' # Root' >> $cryptpad_nginx_site
  325. echo " root $CRYPTPAD_DIR;" >> $cryptpad_nginx_site
  326. echo '' >> $cryptpad_nginx_site
  327. echo ' index index.html;' >> $cryptpad_nginx_site
  328. echo '' >> $cryptpad_nginx_site
  329. echo ' add_header X-XSS-Protection "1; mode=block";' >> $cryptpad_nginx_site
  330. echo ' add_header X-Content-Type-Options nosniff;' >> $cryptpad_nginx_site
  331. echo ' add_header X-Frame-Options SAMEORIGIN;' >> $cryptpad_nginx_site
  332. echo '' >> $cryptpad_nginx_site
  333. echo ' if ($uri = /pad/inner.html) {' >> $cryptpad_nginx_site
  334. echo " set \$scriptSrc \"'self' 'unsafe-eval' 'unsafe-inline'\";" >> $cryptpad_nginx_site
  335. echo ' }' >> $cryptpad_nginx_site
  336. echo '' >> $cryptpad_nginx_site
  337. echo ' location = /cryptpad_websocket {' >> $cryptpad_nginx_site
  338. echo " proxy_pass http://localhost:$CRYPTPAD_PORT;" >> $cryptpad_nginx_site
  339. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $cryptpad_nginx_site
  340. echo ' proxy_set_header Host $host;' >> $cryptpad_nginx_site
  341. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $cryptpad_nginx_site
  342. echo '' >> $cryptpad_nginx_site
  343. echo ' # WebSocket support (nginx 1.4)' >> $cryptpad_nginx_site
  344. echo ' proxy_http_version 1.1;' >> $cryptpad_nginx_site
  345. echo ' proxy_set_header Upgrade $http_upgrade;' >> $cryptpad_nginx_site
  346. echo ' proxy_set_header Connection upgrade;' >> $cryptpad_nginx_site
  347. echo ' }' >> $cryptpad_nginx_site
  348. echo '' >> $cryptpad_nginx_site
  349. echo ' location ^~ /customize.dist/ {' >> $cryptpad_nginx_site
  350. echo ' # This is needed in order to prevent infinite recursion between /customize/ and the root' >> $cryptpad_nginx_site
  351. echo ' }' >> $cryptpad_nginx_site
  352. echo ' location ^~ /customize/ {' >> $cryptpad_nginx_site
  353. echo ' rewrite ^/customize/(.*)$ $1 break;' >> $cryptpad_nginx_site
  354. echo ' try_files /customize/$uri /customize.dist/$uri;' >> $cryptpad_nginx_site
  355. echo ' }' >> $cryptpad_nginx_site
  356. echo ' location = /api/config {' >> $cryptpad_nginx_site
  357. echo ' default_type text/javascript;' >> $cryptpad_nginx_site
  358. echo ' rewrite ^.*$ /customize/api/config break;' >> $cryptpad_nginx_site
  359. echo ' }' >> $cryptpad_nginx_site
  360. echo '' >> $cryptpad_nginx_site
  361. echo ' location ^~ /blob/ {' >> $cryptpad_nginx_site
  362. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  363. echo ' }' >> $cryptpad_nginx_site
  364. echo '' >> $cryptpad_nginx_site
  365. echo ' location ^~ /register/ {' >> $cryptpad_nginx_site
  366. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  367. echo ' }' >> $cryptpad_nginx_site
  368. echo '' >> $cryptpad_nginx_site
  369. echo ' location ^~ /login/ {' >> $cryptpad_nginx_site
  370. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  371. echo ' }' >> $cryptpad_nginx_site
  372. echo '' >> $cryptpad_nginx_site
  373. echo ' location ^~ /about.html {' >> $cryptpad_nginx_site
  374. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  375. echo ' }' >> $cryptpad_nginx_site
  376. echo '' >> $cryptpad_nginx_site
  377. echo ' location ^~ /contact.html {' >> $cryptpad_nginx_site
  378. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  379. echo ' }' >> $cryptpad_nginx_site
  380. echo '' >> $cryptpad_nginx_site
  381. echo ' location ^~ /what-is-cryptpad.html {' >> $cryptpad_nginx_site
  382. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  383. echo ' }' >> $cryptpad_nginx_site
  384. echo '' >> $cryptpad_nginx_site
  385. echo ' location ~ ^/(register|login|settings|user|pad|drive|poll|slide|code|whiteboard|file|media)$ {' >> $cryptpad_nginx_site
  386. echo ' rewrite ^(.*)$ $1/ redirect;' >> $cryptpad_nginx_site
  387. echo ' }' >> $cryptpad_nginx_site
  388. echo '' >> $cryptpad_nginx_site
  389. echo ' try_files /www/$uri /www/$uri/index.html /customize/$uri;' >> $cryptpad_nginx_site
  390. echo '}' >> $cryptpad_nginx_site
  391. cd $rootdir$CRYPTPAD_DIR
  392. get_npm_arch
  393. cat <<EOF > $rootdir/usr/bin/install_cryptpad
  394. #!/bin/bash
  395. cd $CRYPTPAD_DIR
  396. npm install --arch=$NPM_ARCH --build-from-source
  397. npm install --arch=$NPM_ARCH -g bower@1.8.0
  398. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  399. su -c 'bower install' - cryptpad
  400. cp config.example.js config.js
  401. EOF
  402. chmod +x $rootdir/usr/bin/install_cryptpad
  403. chroot "$rootdir" /usr/bin/install_cryptpad
  404. if [ ! -f $rootdir$CRYPTPAD_DIR/config.js ]; then
  405. echo $'Cryptpad config file not found'
  406. exit 628252
  407. fi
  408. rm $rootdir/usr/bin/install_cryptpad
  409. cryptpad_create_config mesh
  410. chroot "$rootdir" chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  411. # daemon
  412. echo '[Unit]' > $rootdir/etc/systemd/system/cryptpad.service
  413. echo 'Description=Cryptpad' >> $rootdir/etc/systemd/system/cryptpad.service
  414. echo 'After=syslog.target' >> $rootdir/etc/systemd/system/cryptpad.service
  415. echo 'After=network.target' >> $rootdir/etc/systemd/system/cryptpad.service
  416. echo '' >> $rootdir/etc/systemd/system/cryptpad.service
  417. echo '[Service]' >> $rootdir/etc/systemd/system/cryptpad.service
  418. echo 'User=cryptpad' >> $rootdir/etc/systemd/system/cryptpad.service
  419. echo 'Group=cryptpad' >> $rootdir/etc/systemd/system/cryptpad.service
  420. echo "WorkingDirectory=$CRYPTPAD_DIR" >> $rootdir/etc/systemd/system/cryptpad.service
  421. echo "ExecStart=/usr/local/bin/node $CRYPTPAD_DIR/server.js" >> $rootdir/etc/systemd/system/cryptpad.service
  422. echo 'Environment=PATH=/usr/bin:/usr/local/bin' >> $rootdir/etc/systemd/system/cryptpad.service
  423. echo 'Environment=NODE_ENV=production' >> $rootdir/etc/systemd/system/cryptpad.service
  424. echo 'Restart=on-failure' >> $rootdir/etc/systemd/system/cryptpad.service
  425. echo '' >> $rootdir/etc/systemd/system/cryptpad.service
  426. echo '[Install]' >> $rootdir/etc/systemd/system/cryptpad.service
  427. echo 'WantedBy=multi-user.target' >> $rootdir/etc/systemd/system/cryptpad.service
  428. chroot "$rootdir" systemctl enable cryptpad.service
  429. }
  430. function install_cryptpad_main {
  431. if [[ $(app_is_installed cryptpad_main) == "1" ]]; then
  432. return
  433. fi
  434. if [ ! -d /var/www/cryptpad ]; then
  435. mkdir /var/www/cryptpad
  436. fi
  437. if [ -d $CRYPTPAD_DIR ]; then
  438. rm -rf $CRYPTPAD_DIR
  439. fi
  440. if [ -d /repos/cryptpad ]; then
  441. mkdir $CRYPTPAD_DIR
  442. cp -r -p /repos/cryptpad/. $CRYPTPAD_DIR
  443. cd $CRYPTPAD_DIR
  444. git pull
  445. else
  446. function_check git_clone
  447. git_clone $CRYPTPAD_REPO $CRYPTPAD_DIR
  448. fi
  449. if [ ! -d $CRYPTPAD_DIR ]; then
  450. echo $'Unable to clone cryptpad repo'
  451. exit 783251
  452. fi
  453. # an unprivileged user to run as
  454. useradd -d $CRYPTPAD_DIR/ cryptpad
  455. cd $CRYPTPAD_DIR
  456. git checkout $CRYPTPAD_COMMIT -b $CRYPTPAD_COMMIT
  457. set_completion_param "cryptpad commit" "$CRYPTPAD_COMMIT"
  458. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  459. CRYPTPAD_ONION_HOSTNAME=$(add_onion_service cryptpad 80 ${CRYPTPAD_ONION_PORT})
  460. cryptpad_nginx_site=/etc/nginx/sites-available/cryptpad
  461. echo 'server {' > $cryptpad_nginx_site
  462. echo " listen 127.0.0.1:$CRYPTPAD_ONION_PORT default_server;" >> $cryptpad_nginx_site
  463. echo " server_name $CRYPTPAD_ONION_HOSTNAME;" >> $cryptpad_nginx_site
  464. echo '' >> $cryptpad_nginx_site
  465. echo ' # Logs' >> $cryptpad_nginx_site
  466. echo ' access_log /dev/null;' >> $cryptpad_nginx_site
  467. echo ' error_log /dev/null;' >> $cryptpad_nginx_site
  468. echo '' >> $cryptpad_nginx_site
  469. echo ' # Root' >> $cryptpad_nginx_site
  470. echo " root $CRYPTPAD_DIR;" >> $cryptpad_nginx_site
  471. echo '' >> $cryptpad_nginx_site
  472. echo ' index index.html;' >> $cryptpad_nginx_site
  473. echo '' >> $cryptpad_nginx_site
  474. echo ' if ($args ~ ver=) {' >> $cryptpad_nginx_site
  475. echo ' set $cacheControl max-age=31536000;' >> $cryptpad_nginx_site
  476. echo ' }' >> $cryptpad_nginx_site
  477. echo ' add_header Cache-Control $cacheControl;' >> $cryptpad_nginx_site
  478. echo '' >> $cryptpad_nginx_site
  479. echo ' add_header X-XSS-Protection "1; mode=block";' >> $cryptpad_nginx_site
  480. echo ' add_header X-Content-Type-Options nosniff;' >> $cryptpad_nginx_site
  481. echo ' add_header X-Frame-Options SAMEORIGIN;' >> $cryptpad_nginx_site
  482. echo '' >> $cryptpad_nginx_site
  483. echo ' if ($uri = /pad/inner.html) {' >> $cryptpad_nginx_site
  484. echo " set \$scriptSrc \"'self' 'unsafe-eval' 'unsafe-inline'\";" >> $cryptpad_nginx_site
  485. echo ' }' >> $cryptpad_nginx_site
  486. echo '' >> $cryptpad_nginx_site
  487. echo ' location = /cryptpad_websocket {' >> $cryptpad_nginx_site
  488. echo " proxy_pass http://localhost:$CRYPTPAD_PORT;" >> $cryptpad_nginx_site
  489. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $cryptpad_nginx_site
  490. echo ' proxy_set_header Host $host;' >> $cryptpad_nginx_site
  491. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $cryptpad_nginx_site
  492. echo '' >> $cryptpad_nginx_site
  493. echo ' # WebSocket support (nginx 1.4)' >> $cryptpad_nginx_site
  494. echo ' proxy_http_version 1.1;' >> $cryptpad_nginx_site
  495. echo ' proxy_set_header Upgrade $http_upgrade;' >> $cryptpad_nginx_site
  496. echo ' proxy_set_header Connection upgrade;' >> $cryptpad_nginx_site
  497. echo ' }' >> $cryptpad_nginx_site
  498. echo '' >> $cryptpad_nginx_site
  499. echo ' location ^~ /customize.dist/ {' >> $cryptpad_nginx_site
  500. echo ' # This is needed in order to prevent infinite recursion between /customize/ and the root' >> $cryptpad_nginx_site
  501. echo ' }' >> $cryptpad_nginx_site
  502. echo ' location ^~ /customize/ {' >> $cryptpad_nginx_site
  503. echo ' rewrite ^/customize/(.*)$ $1 break;' >> $cryptpad_nginx_site
  504. echo ' try_files /customize/$uri /customize.dist/$uri;' >> $cryptpad_nginx_site
  505. echo ' }' >> $cryptpad_nginx_site
  506. echo ' location = /api/config {' >> $cryptpad_nginx_site
  507. echo ' default_type text/javascript;' >> $cryptpad_nginx_site
  508. echo ' rewrite ^.*$ /customize/api/config break;' >> $cryptpad_nginx_site
  509. echo ' }' >> $cryptpad_nginx_site
  510. echo '' >> $cryptpad_nginx_site
  511. echo ' location ^~ /blob/ {' >> $cryptpad_nginx_site
  512. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  513. echo ' }' >> $cryptpad_nginx_site
  514. echo '' >> $cryptpad_nginx_site
  515. echo ' location ^~ /register/ {' >> $cryptpad_nginx_site
  516. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  517. echo ' }' >> $cryptpad_nginx_site
  518. echo '' >> $cryptpad_nginx_site
  519. echo ' location ^~ /login/ {' >> $cryptpad_nginx_site
  520. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  521. echo ' }' >> $cryptpad_nginx_site
  522. echo '' >> $cryptpad_nginx_site
  523. echo ' location ^~ /about.html {' >> $cryptpad_nginx_site
  524. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  525. echo ' }' >> $cryptpad_nginx_site
  526. echo '' >> $cryptpad_nginx_site
  527. echo ' location ^~ /contact.html {' >> $cryptpad_nginx_site
  528. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  529. echo ' }' >> $cryptpad_nginx_site
  530. echo '' >> $cryptpad_nginx_site
  531. echo ' location ^~ /what-is-cryptpad.html {' >> $cryptpad_nginx_site
  532. echo ' try_files $uri =404;' >> $cryptpad_nginx_site
  533. echo ' }' >> $cryptpad_nginx_site
  534. echo '' >> $cryptpad_nginx_site
  535. echo ' location ~ ^/(register|login|settings|user|pad|drive|poll|slide|code|whiteboard|file|media)$ {' >> $cryptpad_nginx_site
  536. echo ' rewrite ^(.*)$ $1/ redirect;' >> $cryptpad_nginx_site
  537. echo ' }' >> $cryptpad_nginx_site
  538. echo '' >> $cryptpad_nginx_site
  539. echo ' try_files /www/$uri /www/$uri/index.html /customize/$uri;' >> $cryptpad_nginx_site
  540. echo '}' >> $cryptpad_nginx_site
  541. function_check nginx_ensite
  542. nginx_ensite cryptpad
  543. install_completed cryptpad_main
  544. }
  545. function install_cryptpad {
  546. function_check install_nodejs
  547. install_nodejs cryptpad
  548. install_cryptpad_main
  549. cd $CRYPTPAD_DIR
  550. npm install
  551. npm install -g bower@1.8.0
  552. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  553. su -c 'bower install' - cryptpad
  554. cp config.example.js config.js
  555. if [ ! -f config.js ]; then
  556. echo $'Cryptpad config file not found'
  557. exit 628252
  558. fi
  559. cryptpad_create_config
  560. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  561. # daemon
  562. echo '[Unit]' > /etc/systemd/system/cryptpad.service
  563. echo 'Description=Cryptpad' >> /etc/systemd/system/cryptpad.service
  564. echo 'After=syslog.target' >> /etc/systemd/system/cryptpad.service
  565. echo 'After=network.target' >> /etc/systemd/system/cryptpad.service
  566. echo '' >> /etc/systemd/system/cryptpad.service
  567. echo '[Service]' >> /etc/systemd/system/cryptpad.service
  568. echo 'User=cryptpad' >> /etc/systemd/system/cryptpad.service
  569. echo 'Group=cryptpad' >> /etc/systemd/system/cryptpad.service
  570. echo "WorkingDirectory=$CRYPTPAD_DIR" >> /etc/systemd/system/cryptpad.service
  571. echo "ExecStart=/usr/local/bin/node $CRYPTPAD_DIR/server.js" >> /etc/systemd/system/cryptpad.service
  572. echo 'Environment=PATH=/usr/bin:/usr/local/bin' >> /etc/systemd/system/cryptpad.service
  573. echo 'Environment=NODE_ENV=production' >> /etc/systemd/system/cryptpad.service
  574. echo 'Restart=on-failure' >> /etc/systemd/system/cryptpad.service
  575. echo '' >> /etc/systemd/system/cryptpad.service
  576. echo '[Install]' >> /etc/systemd/system/cryptpad.service
  577. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/cryptpad.service
  578. systemctl enable cryptpad.service
  579. systemctl daemon-reload
  580. systemctl start cryptpad.service
  581. sleep 6
  582. cryptpad_generate_api_config
  583. # install again
  584. cd $CRYPTPAD_DIR
  585. su -c 'bower install' - cryptpad
  586. systemctl restart nginx
  587. APP_INSTALLED=1
  588. }
  589. # NOTE: deliberately there is no "exit 0"