freedombone-app-cryptpad 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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-2018 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 || exit 254724
  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. # shellcheck disable=SC2154
  202. cryptpad_prefix="$rootdir"
  203. fi
  204. { echo '/*@flow*/';
  205. echo '/*';
  206. echo ' globals module';
  207. echo '*/';
  208. echo "var domain = ' http://localhost:${CRYPTPAD_PORT}/';";
  209. echo 'module.exports = {';
  210. echo " httpAddress: '::',";
  211. echo ' httpHeaders: {';
  212. echo ' "X-XSS-Protection": "1; mode=block",';
  213. echo ' "X-Content-Type-Options": "nosniff",';
  214. echo ' "Access-Control-Allow-Origin": "*"';
  215. echo ' },';
  216. echo '';
  217. echo ' contentSecurity: [';
  218. echo " \"default-src 'none'\",";
  219. echo " \"style-src 'unsafe-inline' 'self' \" + domain,";
  220. echo " \"script-src 'self'\" + domain,";
  221. echo " \"font-src 'self' data:\" + domain,";
  222. echo '';
  223. echo ' "child-src blob: *",';
  224. echo ' "frame-src blob: *",';
  225. echo ' "media-src * blob:",';
  226. echo '';
  227. echo " \"connect-src 'self' ws: wss: blob:\" + domain,";
  228. echo '';
  229. echo " \"img-src 'self' data: blob:\" + domain,";
  230. echo '';
  231. echo ' "frame-ancestors *",';
  232. echo " ].join('; '),";
  233. echo '';
  234. echo ' padContentSecurity: [';
  235. echo " \"default-src 'none'\",";
  236. echo " \"style-src 'unsafe-inline' 'self'\" + domain,";
  237. echo " \"script-src 'self' 'unsafe-eval' 'unsafe-inline'\" + domain,";
  238. echo " \"font-src 'self'\" + domain,";
  239. echo '';
  240. echo ' "child-src *",';
  241. echo ' "frame-src *",';
  242. echo '';
  243. echo " \"connect-src 'self' ws: wss:\" + domain,";
  244. echo '';
  245. echo ' "img-src * blob:",';
  246. echo " ].join('; '),";
  247. echo '';
  248. echo " httpPort: ${CRYPTPAD_PORT},";
  249. echo '';
  250. echo ' // This is for allowing the cross-domain iframe to function when developing';
  251. echo " httpSafePort: ${CRYPTPAD_PORT2},";
  252. echo '';
  253. echo " websocketPath: '/cryptpad_websocket',";
  254. echo '';
  255. echo ' logToStdout: false,';
  256. echo '';
  257. echo ' verbose: false,';
  258. echo '';
  259. echo ' mainPages: [';
  260. echo " 'index'";
  261. echo ' ],';
  262. echo '';
  263. echo ' removeDonateButton: true,';
  264. echo ' allowSubscriptions: false,'; } > "$cryptpad_prefix$CRYPTPAD_DIR/config.js"
  265. if [[ "$cryptpad_install_type" == "mesh" ]]; then
  266. echo " myDomain: 'http://P${PEER_ID}.local'," >> "$cryptpad_prefix$CRYPTPAD_DIR/config.js"
  267. else
  268. CRYPTPAD_ONION_HOSTNAME=$(cat /var/lib/tor/hidden_service_cryptpad/hostname)
  269. echo " myDomain: 'http://${CRYPTPAD_ONION_HOSTNAME}'," >> "$cryptpad_prefix$CRYPTPAD_DIR/config.js"
  270. fi
  271. { echo ' defaultStorageLimit: 50 * 1024 * 1024,';
  272. echo '';
  273. echo ' customLimits: {';
  274. echo ' },';
  275. echo '';
  276. echo ' adminEmail: false,';
  277. echo '';
  278. echo " storage: './storage/file',";
  279. echo '';
  280. echo " filePath: './datastore/',";
  281. echo " pinPath: './pins',";
  282. echo " blobPath: './blob',";
  283. echo " blobStagingPath: './blobstage',";
  284. echo ' channelExpirationMs: 30000,';
  285. echo ' openFileLimit: 1024,';
  286. echo " rpc: './rpc.js',";
  287. echo ' suppressRPCErrors: false,';
  288. echo ' enableUploads: true,';
  289. echo ' //restrictUploads: false,';
  290. echo ' maxUploadSize: 20 * 1024 * 1024,';
  291. echo ' //logFeedback: true,';
  292. echo ' //logRPC: true,';
  293. echo '};'; } >> "$cryptpad_prefix$CRYPTPAD_DIR/config.js"
  294. if [[ "$cryptpad_install_type" != "mesh" ]]; then
  295. chown cryptpad:cryptpad "$cryptpad_prefix$CRYPTPAD_DIR/config.js"
  296. else
  297. chroot "$rootdir" chown cryptpad:cryptpad $CRYPTPAD_DIR/config.js
  298. fi
  299. }
  300. function mesh_install_cryptpad {
  301. # shellcheck disable=SC2153
  302. if [[ "$VARIANT" != "meshclient" && "$VARIANT" != "meshusb" ]]; then
  303. return
  304. fi
  305. if [ ! -d "$rootdir/var/www/cryptpad" ]; then
  306. mkdir "$rootdir/var/www/cryptpad"
  307. fi
  308. if [ -d "$rootdir$CRYPTPAD_DIR" ]; then
  309. rm -rf "$rootdir$CRYPTPAD_DIR"
  310. fi
  311. git_clone "$CRYPTPAD_REPO" "$rootdir$CRYPTPAD_DIR"
  312. if [ ! -d "$rootdir$CRYPTPAD_DIR" ]; then
  313. echo $'Unable to clone cryptpad repo'
  314. exit 783251
  315. fi
  316. # an unprivileged user to run as
  317. chroot "$rootdir" useradd -d $CRYPTPAD_DIR/ cryptpad
  318. cd "$rootdir$CRYPTPAD_DIR" || exit 34683568
  319. git checkout "$CRYPTPAD_COMMIT" -b "$CRYPTPAD_COMMIT"
  320. chroot "$rootdir" chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  321. cryptpad_nginx_site=$rootdir/etc/nginx/sites-available/cryptpad
  322. { echo 'server {';
  323. echo " listen 80 default_server;";
  324. echo " server_name P${PEER_ID}.local;";
  325. echo '';
  326. echo ' # Logs';
  327. echo ' access_log /dev/null;';
  328. echo ' error_log /dev/null;';
  329. echo '';
  330. echo ' # Root';
  331. echo " root $CRYPTPAD_DIR;";
  332. echo '';
  333. echo ' index index.html;';
  334. echo '';
  335. echo ' add_header X-XSS-Protection "1; mode=block";';
  336. echo ' add_header X-Content-Type-Options nosniff;';
  337. echo ' add_header X-Frame-Options SAMEORIGIN;';
  338. echo '';
  339. echo " if (\$uri = /pad/inner.html) {";
  340. echo " set \$scriptSrc \"'self' 'unsafe-eval' 'unsafe-inline'\";";
  341. echo ' }';
  342. echo '';
  343. echo ' location = /cryptpad_websocket {';
  344. echo " proxy_pass http://localhost:$CRYPTPAD_PORT;";
  345. echo " proxy_set_header X-Real-IP \$remote_addr;";
  346. echo " proxy_set_header Host \$host;";
  347. echo " proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;";
  348. echo '';
  349. echo ' # WebSocket support (nginx 1.4)';
  350. echo ' proxy_http_version 1.1;';
  351. echo " proxy_set_header Upgrade \$http_upgrade;";
  352. echo ' proxy_set_header Connection upgrade;';
  353. echo ' }';
  354. echo '';
  355. echo ' location ^~ /customize.dist/ {';
  356. echo ' # This is needed in order to prevent infinite recursion between /customize/ and the root';
  357. echo ' }';
  358. echo ' location ^~ /customize/ {';
  359. echo " rewrite ^/customize/(.*)\$ \$1 break;";
  360. echo " try_files /customize/\$uri /customize.dist/\$uri;";
  361. echo ' }';
  362. echo ' location = /api/config {';
  363. echo ' default_type text/javascript;';
  364. echo ' rewrite ^.*$ /customize/api/config break;';
  365. echo ' }';
  366. echo '';
  367. echo ' location ^~ /blob/ {';
  368. echo " try_files \$uri =404;";
  369. echo ' }';
  370. echo '';
  371. echo ' location ^~ /register/ {';
  372. echo " try_files \$uri =404;";
  373. echo ' }';
  374. echo '';
  375. echo ' location ^~ /login/ {';
  376. echo " try_files \$uri =404;";
  377. echo ' }';
  378. echo '';
  379. echo ' location ^~ /about.html {';
  380. echo " try_files \$uri =404;";
  381. echo ' }';
  382. echo '';
  383. echo ' location ^~ /contact.html {';
  384. echo " try_files \$uri =404;";
  385. echo ' }';
  386. echo '';
  387. echo ' location ^~ /what-is-cryptpad.html {';
  388. echo " try_files \$uri =404;";
  389. echo ' }';
  390. echo '';
  391. echo ' location ~ ^/(register|login|settings|user|pad|drive|poll|slide|code|whiteboard|file|media)$ {';
  392. echo " rewrite ^(.*)\$ \$1/ redirect;";
  393. echo ' }';
  394. echo '';
  395. echo " try_files /www/\$uri /www/\$uri/index.html /customize/\$uri;";
  396. echo '}'; } > "$cryptpad_nginx_site"
  397. cd "$rootdir$CRYPTPAD_DIR" || exit 6246824624527
  398. get_npm_arch
  399. cat <<EOF > "$rootdir/usr/bin/install_cryptpad"
  400. #!/bin/bash
  401. cd $CRYPTPAD_DIR
  402. npm install --arch=$NPM_ARCH --build-from-source
  403. npm install --arch=$NPM_ARCH -g bower@1.8.0
  404. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  405. su -c 'bower install' - cryptpad
  406. cp config.example.js config.js
  407. EOF
  408. chmod +x "$rootdir/usr/bin/install_cryptpad"
  409. chroot "$rootdir" /usr/bin/install_cryptpad
  410. if [ ! -f "$rootdir$CRYPTPAD_DIR/config.js" ]; then
  411. echo $'Cryptpad config file not found'
  412. exit 628252
  413. fi
  414. rm "$rootdir/usr/bin/install_cryptpad"
  415. cryptpad_create_config mesh
  416. chroot "$rootdir" chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  417. # daemon
  418. { echo '[Unit]';
  419. echo 'Description=Cryptpad';
  420. echo 'After=syslog.target';
  421. echo 'After=network.target';
  422. echo '';
  423. echo '[Service]';
  424. echo 'User=cryptpad';
  425. echo 'Group=cryptpad';
  426. echo "WorkingDirectory=$CRYPTPAD_DIR";
  427. echo "ExecStart=/usr/local/bin/node $CRYPTPAD_DIR/server.js";
  428. echo 'Environment=PATH=/usr/bin:/usr/local/bin';
  429. echo 'Environment=NODE_ENV=production';
  430. echo 'Restart=on-failure';
  431. echo '';
  432. echo '[Install]';
  433. echo 'WantedBy=multi-user.target'; } > "$rootdir/etc/systemd/system/cryptpad.service"
  434. chroot "$rootdir" systemctl enable cryptpad.service
  435. }
  436. function install_cryptpad_main {
  437. if [[ $(app_is_installed cryptpad_main) == "1" ]]; then
  438. return
  439. fi
  440. if [ ! -d /var/www/cryptpad ]; then
  441. mkdir /var/www/cryptpad
  442. fi
  443. if [ -d $CRYPTPAD_DIR ]; then
  444. rm -rf $CRYPTPAD_DIR
  445. fi
  446. if [ -d /repos/cryptpad ]; then
  447. mkdir $CRYPTPAD_DIR
  448. cp -r -p /repos/cryptpad/. $CRYPTPAD_DIR
  449. cd $CRYPTPAD_DIR || exit 3468356385
  450. git pull
  451. else
  452. function_check git_clone
  453. git_clone $CRYPTPAD_REPO $CRYPTPAD_DIR
  454. fi
  455. if [ ! -d $CRYPTPAD_DIR ]; then
  456. echo $'Unable to clone cryptpad repo'
  457. exit 783251
  458. fi
  459. # an unprivileged user to run as
  460. useradd -d $CRYPTPAD_DIR/ cryptpad
  461. cd $CRYPTPAD_DIR || exit 34683655
  462. git checkout $CRYPTPAD_COMMIT -b $CRYPTPAD_COMMIT
  463. set_completion_param "cryptpad commit" "$CRYPTPAD_COMMIT"
  464. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  465. CRYPTPAD_ONION_HOSTNAME=$(add_onion_service cryptpad 80 ${CRYPTPAD_ONION_PORT})
  466. cryptpad_nginx_site=/etc/nginx/sites-available/cryptpad
  467. { echo 'server {';
  468. echo " listen 127.0.0.1:$CRYPTPAD_ONION_PORT default_server;";
  469. echo " server_name $CRYPTPAD_ONION_HOSTNAME;";
  470. echo '';
  471. echo ' # Logs';
  472. echo ' access_log /dev/null;';
  473. echo ' error_log /dev/null;';
  474. echo '';
  475. echo ' # Root';
  476. echo " root $CRYPTPAD_DIR;";
  477. echo '';
  478. echo ' index index.html;';
  479. echo '';
  480. echo " if (\$args ~ ver=) {";
  481. echo " set \$cacheControl max-age=31536000;";
  482. echo ' }';
  483. echo " add_header Cache-Control \$cacheControl;";
  484. echo '';
  485. echo ' add_header X-XSS-Protection "1; mode=block";';
  486. echo ' add_header X-Content-Type-Options nosniff;';
  487. echo ' add_header X-Frame-Options SAMEORIGIN;';
  488. echo '';
  489. echo " if (\$uri = /pad/inner.html) {";
  490. echo " set \$scriptSrc \"'self' 'unsafe-eval' 'unsafe-inline'\";";
  491. echo ' }';
  492. echo '';
  493. echo ' location = /cryptpad_websocket {';
  494. echo " proxy_pass http://localhost:$CRYPTPAD_PORT;";
  495. echo " proxy_set_header X-Real-IP \$remote_addr;";
  496. echo " proxy_set_header Host \$host;";
  497. echo " proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;";
  498. echo '';
  499. echo ' # WebSocket support (nginx 1.4)';
  500. echo ' proxy_http_version 1.1;';
  501. echo " proxy_set_header Upgrade \$http_upgrade;";
  502. echo ' proxy_set_header Connection upgrade;';
  503. echo ' }';
  504. echo '';
  505. echo ' location ^~ /customize.dist/ {';
  506. echo ' # This is needed in order to prevent infinite recursion between /customize/ and the root';
  507. echo ' }';
  508. echo ' location ^~ /customize/ {';
  509. echo " rewrite ^/customize/(.*)\$ \$1 break;";
  510. echo " try_files /customize/\$uri /customize.dist/\$uri;";
  511. echo ' }';
  512. echo ' location = /api/config {';
  513. echo ' default_type text/javascript;';
  514. echo ' rewrite ^.*$ /customize/api/config break;';
  515. echo ' }';
  516. echo '';
  517. echo ' location ^~ /blob/ {';
  518. echo " try_files \$uri =404;";
  519. echo ' }';
  520. echo '';
  521. echo ' location ^~ /register/ {';
  522. echo " try_files \$uri =404;";
  523. echo ' }';
  524. echo '';
  525. echo ' location ^~ /login/ {';
  526. echo " try_files \$uri =404;";
  527. echo ' }';
  528. echo '';
  529. echo ' location ^~ /about.html {';
  530. echo " try_files \$uri =404;";
  531. echo ' }';
  532. echo '';
  533. echo ' location ^~ /contact.html {';
  534. echo " try_files \$uri =404;";
  535. echo ' }';
  536. echo '';
  537. echo ' location ^~ /what-is-cryptpad.html {';
  538. echo " try_files \$uri =404;";
  539. echo ' }';
  540. echo '';
  541. echo ' location ~ ^/(register|login|settings|user|pad|drive|poll|slide|code|whiteboard|file|media)$ {';
  542. echo " rewrite ^(.*)\$ \$1/ redirect;";
  543. echo ' }';
  544. echo '';
  545. echo " try_files /www/\$uri /www/\$uri/index.html /customize/\$uri;";
  546. echo '}'; } > $cryptpad_nginx_site
  547. function_check nginx_ensite
  548. nginx_ensite cryptpad
  549. install_completed cryptpad_main
  550. }
  551. function install_cryptpad {
  552. function_check install_nodejs
  553. install_nodejs cryptpad
  554. install_cryptpad_main
  555. cd $CRYPTPAD_DIR || exit 35483548
  556. npm install
  557. npm install -g bower@1.8.0
  558. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  559. su -c 'bower install' - cryptpad
  560. cp config.example.js config.js
  561. if [ ! -f config.js ]; then
  562. echo $'Cryptpad config file not found'
  563. exit 628252
  564. fi
  565. cryptpad_create_config
  566. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  567. # daemon
  568. { echo '[Unit]';
  569. echo 'Description=Cryptpad';
  570. echo 'After=syslog.target';
  571. echo 'After=network.target';
  572. echo '';
  573. echo '[Service]';
  574. echo 'User=cryptpad';
  575. echo 'Group=cryptpad';
  576. echo "WorkingDirectory=$CRYPTPAD_DIR";
  577. echo "ExecStart=/usr/local/bin/node $CRYPTPAD_DIR/server.js";
  578. echo 'Environment=PATH=/usr/bin:/usr/local/bin';
  579. echo 'Environment=NODE_ENV=production';
  580. echo 'Restart=on-failure';
  581. echo '';
  582. echo '[Install]';
  583. echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/cryptpad.service
  584. systemctl enable cryptpad.service
  585. systemctl daemon-reload
  586. systemctl start cryptpad.service
  587. sleep 6
  588. cryptpad_generate_api_config
  589. # install again
  590. cd $CRYPTPAD_DIR || exit 73537453
  591. su -c 'bower install' - cryptpad
  592. systemctl restart nginx
  593. APP_INSTALLED=1
  594. }
  595. # NOTE: deliberately there is no "exit 0"