freedombone-app-cryptpad 21KB

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