freedombone-app-cryptpad 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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='fe202a92b490404e3d8b7b8a2712e2e0a33a9851'
  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. rm -rf $CRYPTPAD_DIR/.cache/bower
  91. su -c './node_modules/bower/bin/bower install --config.interactive=false' - cryptpad
  92. su -c './node_modules/bower/bin/bower update --config.interactive=false' - cryptpad
  93. cryptpad_generate_api_config
  94. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  95. systemctl start cryptpad
  96. }
  97. function backup_local_cryptpad {
  98. source_directory=$CRYPTPAD_DIR/datastore
  99. if [ -d $source_directory ]; then
  100. systemctl stop cryptpad
  101. dest_directory=cryptpad
  102. function_check suspend_site
  103. suspend_site cryptpad
  104. function_check backup_directory_to_usb
  105. backup_directory_to_usb $source_directory $dest_directory
  106. function_check restart_site
  107. restart_site
  108. systemctl start cryptpad
  109. fi
  110. }
  111. function restore_local_cryptpad {
  112. if [ -d $CRYPTPAD_DIR ]; then
  113. systemctl stop cryptpad
  114. temp_restore_dir=/root/tempcryptpad
  115. function_check restore_directory_from_usb
  116. restore_directory_from_usb $temp_restore_dir cryptpad
  117. if [ ! -d $temp_restore_dir$CRYPTPAD_DIR/datastore ]; then
  118. if [ -d $temp_restore_dir ]; then
  119. cp -r $temp_restore_dir/* $CRYPTPAD_DIR/datastore/
  120. else
  121. systemctl start cryptpad
  122. echo 'Failed to restore cryptpad'
  123. rm -rf $temp_restore_dir
  124. exit 8736529
  125. fi
  126. else
  127. cp -r $temp_restore_dir$CRYPTPAD_DIR/datastore/* $CRYPTPAD_DIR/datastore/
  128. fi
  129. rm -rf $temp_restore_dir
  130. systemctl start cryptpad
  131. fi
  132. }
  133. function backup_remote_cryptpad {
  134. if grep -q "cryptpad domain" "$COMPLETION_FILE"; then
  135. temp_backup_dir=$CRYPTPAD_DIR/datastore
  136. if [ -d $temp_backup_dir ]; then
  137. systemctl stop cryptpad
  138. function_check suspend_site
  139. suspend_site cryptpad
  140. echo $"Backing up Cryptpad installation"
  141. function_check backup_directory_to_friend
  142. backup_directory_to_friend $temp_backup_dir cryptpad
  143. function_check restart_site
  144. restart_site
  145. systemctl start cryptpad
  146. else
  147. echo $"cryptpad domain specified but not found in ${temp_backup_dir}"
  148. fi
  149. fi
  150. }
  151. function restore_remote_cryptpad {
  152. if [ -d $CRYPTPAD_DIR ]; then
  153. systemctl stop cryptpad
  154. temp_restore_dir=/root/tempcryptpad
  155. function_check restore_directory_from_usb
  156. restore_directory_from_friend $temp_restore_dir cryptpad
  157. if [ ! -d $temp_restore_dir$CRYPTPAD_DIR/datastore ]; then
  158. if [ -d $temp_restore_dir ]; then
  159. cp -r $temp_restore_dir/* $CRYPTPAD_DIR/datastore/
  160. else
  161. systemctl start cryptpad
  162. echo 'Failed to restore cryptpad'
  163. rm -rf $temp_restore_dir
  164. return
  165. fi
  166. else
  167. cp -r $temp_restore_dir$CRYPTPAD_DIR/datastore/* $CRYPTPAD_DIR/datastore/
  168. fi
  169. rm -rf $temp_restore_dir
  170. systemctl start cryptpad
  171. fi
  172. }
  173. function remove_cryptpad {
  174. systemctl stop cryptpad
  175. systemctl disable cryptpad
  176. if [ -f /etc/systemd/system/cryptpad.service ]; then
  177. rm /etc/systemd/system/cryptpad.service
  178. fi
  179. systemctl daemon-reload
  180. function_check remove_nodejs
  181. remove_nodejs cryptpad
  182. nginx_dissite cryptpad
  183. if [ -d $CRYPTPAD_DIR ]; then
  184. rm -rf $CRYPTPAD_DIR
  185. fi
  186. if [ -f /etc/nginx/sites-available/cryptpad ]; then
  187. rm /etc/nginx/sites-available/cryptpad
  188. fi
  189. function_check remove_onion_service
  190. remove_onion_service cryptpad ${CRYPTPAD_ONION_PORT}
  191. remove_app cryptpad
  192. remove_completion_param install_cryptpad
  193. sed -i '/cryptpad/d' "$COMPLETION_FILE"
  194. userdel -r cryptpad
  195. }
  196. function cryptpad_create_config {
  197. cryptpad_install_type=$1
  198. cryptpad_prefix=
  199. if [[ "$cryptpad_install_type" == "mesh" ]]; then
  200. # shellcheck disable=SC2154
  201. cryptpad_prefix="$rootdir"
  202. fi
  203. { echo '/*@flow*/';
  204. echo '/*';
  205. echo ' globals module';
  206. echo '*/';
  207. echo "var domain = ' http://localhost:${CRYPTPAD_PORT}/';";
  208. echo 'module.exports = {';
  209. echo " httpAddress: '::',";
  210. echo ' httpHeaders: {';
  211. echo ' "X-XSS-Protection": "1; mode=block",';
  212. echo ' "X-Content-Type-Options": "nosniff",';
  213. echo ' "Access-Control-Allow-Origin": "*"';
  214. echo ' },';
  215. echo '';
  216. echo ' contentSecurity: [';
  217. echo " \"default-src 'none'\",";
  218. echo " \"style-src 'unsafe-inline' 'self' \" + domain,";
  219. echo " \"script-src 'self'\" + domain,";
  220. echo " \"font-src 'self' data:\" + domain,";
  221. echo '';
  222. echo ' "child-src blob: *",';
  223. echo ' "frame-src blob: *",';
  224. echo ' "media-src * blob:",';
  225. echo '';
  226. echo " \"connect-src 'self' ws: wss: blob:\" + domain,";
  227. echo '';
  228. echo " \"img-src 'self' data: blob:\" + domain,";
  229. echo '';
  230. echo ' "frame-ancestors *",';
  231. echo " ].join('; '),";
  232. echo '';
  233. echo ' padContentSecurity: [';
  234. echo " \"default-src 'none'\",";
  235. echo " \"style-src 'unsafe-inline' 'self'\" + domain,";
  236. echo " \"script-src 'self' 'unsafe-eval' 'unsafe-inline'\" + domain,";
  237. echo " \"font-src 'self'\" + domain,";
  238. echo '';
  239. echo ' "child-src *",';
  240. echo ' "frame-src *",';
  241. echo '';
  242. echo " \"connect-src 'self' ws: wss:\" + domain,";
  243. echo '';
  244. echo ' "img-src * blob:",';
  245. echo " ].join('; '),";
  246. echo '';
  247. echo " httpPort: ${CRYPTPAD_PORT},";
  248. echo '';
  249. echo ' // This is for allowing the cross-domain iframe to function when developing';
  250. echo " httpSafePort: ${CRYPTPAD_PORT2},";
  251. echo '';
  252. echo " websocketPath: '/cryptpad_websocket',";
  253. echo '';
  254. echo ' logToStdout: false,';
  255. echo '';
  256. echo ' verbose: false,';
  257. echo '';
  258. echo ' mainPages: [';
  259. echo " 'index'";
  260. echo ' ],';
  261. echo '';
  262. echo ' removeDonateButton: true,';
  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,';
  271. echo '';
  272. echo ' customLimits: {';
  273. echo ' },';
  274. echo '';
  275. echo ' adminEmail: false,';
  276. echo '';
  277. echo " storage: './storage/file',";
  278. echo '';
  279. echo " filePath: './datastore/',";
  280. echo " pinPath: './pins',";
  281. echo " blobPath: './blob',";
  282. echo " blobStagingPath: './blobstage',";
  283. echo ' channelExpirationMs: 30000,';
  284. echo ' openFileLimit: 1024,';
  285. echo " rpc: './rpc.js',";
  286. echo ' suppressRPCErrors: false,';
  287. echo ' enableUploads: true,';
  288. echo ' //restrictUploads: false,';
  289. echo ' maxUploadSize: 20 * 1024 * 1024,';
  290. echo ' //logFeedback: true,';
  291. echo ' //logRPC: true,';
  292. echo '};'; } >> "$cryptpad_prefix$CRYPTPAD_DIR/config.js"
  293. if [[ "$cryptpad_install_type" != "mesh" ]]; then
  294. chown cryptpad:cryptpad "$cryptpad_prefix$CRYPTPAD_DIR/config.js"
  295. else
  296. chroot "$rootdir" chown cryptpad:cryptpad $CRYPTPAD_DIR/config.js
  297. fi
  298. }
  299. function mesh_install_cryptpad {
  300. # shellcheck disable=SC2153
  301. if [[ "$VARIANT" != "meshclient" && "$VARIANT" != "meshusb" ]]; then
  302. return
  303. fi
  304. if [ ! -d "$rootdir/var/www/cryptpad" ]; then
  305. mkdir "$rootdir/var/www/cryptpad"
  306. fi
  307. if [ -d "$rootdir$CRYPTPAD_DIR" ]; then
  308. rm -rf "$rootdir$CRYPTPAD_DIR"
  309. fi
  310. git_clone "$CRYPTPAD_REPO" "$rootdir$CRYPTPAD_DIR"
  311. if [ ! -d "$rootdir$CRYPTPAD_DIR" ]; then
  312. echo $'Unable to clone cryptpad repo'
  313. exit 783251
  314. fi
  315. # an unprivileged user to run as
  316. chroot "$rootdir" useradd -d $CRYPTPAD_DIR/ cryptpad
  317. cd "$rootdir$CRYPTPAD_DIR" || exit 34683568
  318. git checkout "$CRYPTPAD_COMMIT" -b "$CRYPTPAD_COMMIT"
  319. chroot "$rootdir" chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  320. cryptpad_nginx_site=$rootdir/etc/nginx/sites-available/cryptpad
  321. { echo 'server {';
  322. echo ' listen [::]:80 default_server;';
  323. echo " server_name P${PEER_ID}.local;";
  324. echo '';
  325. echo ' # Logs';
  326. echo ' access_log /dev/null;';
  327. echo ' error_log /dev/null;';
  328. echo '';
  329. echo ' # Root';
  330. echo " root $CRYPTPAD_DIR;";
  331. echo '';
  332. echo ' index index.html;';
  333. echo '';
  334. echo ' add_header X-XSS-Protection "1; mode=block";';
  335. echo ' add_header X-Content-Type-Options nosniff;';
  336. echo ' add_header X-Frame-Options SAMEORIGIN;';
  337. echo '';
  338. echo " if (\$uri = /pad/inner.html) {";
  339. echo " set \$scriptSrc \"'self' 'unsafe-eval' 'unsafe-inline'\";";
  340. echo ' }';
  341. echo '';
  342. echo ' location = /cryptpad_websocket {';
  343. echo " proxy_pass http://[::]:$CRYPTPAD_PORT;";
  344. echo " proxy_set_header X-Real-IP \$remote_addr;";
  345. echo " proxy_set_header Host \$host;";
  346. echo " proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;";
  347. echo '';
  348. echo ' # WebSocket support (nginx 1.4)';
  349. echo ' proxy_http_version 1.1;';
  350. echo " proxy_set_header Upgrade \$http_upgrade;";
  351. echo ' proxy_set_header Connection upgrade;';
  352. echo ' }';
  353. echo '';
  354. echo ' location ^~ /customize.dist/ {';
  355. echo ' # This is needed in order to prevent infinite recursion between /customize/ and the root';
  356. echo ' }';
  357. echo ' location ^~ /customize/ {';
  358. echo " rewrite ^/customize/(.*)\$ \$1 break;";
  359. echo " try_files /customize/\$uri /customize.dist/\$uri;";
  360. echo ' }';
  361. echo ' location = /api/config {';
  362. echo " proxy_pass http://localhost:$CRYPTPAD_PORT;";
  363. echo " proxy_set_header X-Real-IP \$remote_addr;";
  364. echo " proxy_set_header Host \$host;";
  365. echo " proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;";
  366. echo ' }';
  367. echo '';
  368. echo ' location ^~ /blob/ {';
  369. echo ' add_header Cache-Control max-age=31536000;';
  370. echo " try_files \$uri =404;";
  371. echo ' }';
  372. echo '';
  373. echo ' location ^~ /datastore/ {';
  374. echo ' add_header Cache-Control max-age=0;';
  375. echo " try_files \$uri =404;";
  376. echo ' }';
  377. echo '';
  378. echo ' location ^~ /register/ {';
  379. echo " try_files \$uri =404;";
  380. echo ' }';
  381. echo '';
  382. echo ' location ^~ /login/ {';
  383. echo " try_files \$uri =404;";
  384. echo ' }';
  385. echo '';
  386. echo ' location ^~ /about.html {';
  387. echo " try_files \$uri =404;";
  388. echo ' }';
  389. echo '';
  390. echo ' location ^~ /contact.html {';
  391. echo " try_files \$uri =404;";
  392. echo ' }';
  393. echo '';
  394. echo ' location ^~ /what-is-cryptpad.html {';
  395. echo " try_files \$uri =404;";
  396. echo ' }';
  397. echo '';
  398. echo ' location ~ ^/(register|login|settings|user|pad|drive|poll|slide|code|whiteboard|file|media)$ {';
  399. echo " rewrite ^(.*)\$ \$1/ redirect;";
  400. echo ' }';
  401. echo '';
  402. echo " try_files /www/\$uri /www/\$uri/index.html /customize/\$uri;";
  403. echo '}'; } > "$cryptpad_nginx_site"
  404. cd "$rootdir$CRYPTPAD_DIR" || exit 6246824624527
  405. get_npm_arch
  406. cat <<EOF > "$rootdir/usr/bin/install_cryptpad"
  407. #!/bin/bash
  408. cd $CRYPTPAD_DIR || exit 358735
  409. npm install --arch=$NPM_ARCH --build-from-source
  410. npm install --arch=$NPM_ARCH bower@1.8.4
  411. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  412. su -c './node_modules/bower/bin/bower install --config.interactive=false' - cryptpad
  413. cp config.example.js config.js
  414. EOF
  415. chmod +x "$rootdir/usr/bin/install_cryptpad"
  416. chroot "$rootdir" /usr/bin/install_cryptpad
  417. if [ ! -f "$rootdir$CRYPTPAD_DIR/config.js" ]; then
  418. echo $'Cryptpad config file not found'
  419. exit 628252
  420. fi
  421. rm "$rootdir/usr/bin/install_cryptpad"
  422. cryptpad_create_config mesh
  423. chroot "$rootdir" chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  424. # daemon
  425. { echo '[Unit]';
  426. echo 'Description=Cryptpad';
  427. echo 'After=syslog.target';
  428. echo 'After=network.target';
  429. echo '';
  430. echo '[Service]';
  431. echo 'User=cryptpad';
  432. echo 'Group=cryptpad';
  433. echo "WorkingDirectory=$CRYPTPAD_DIR";
  434. echo "ExecStart=/usr/local/bin/node $CRYPTPAD_DIR/server.js";
  435. echo 'Environment=PATH=/usr/bin:/usr/local/bin';
  436. echo 'Environment=NODE_ENV=production';
  437. echo 'Restart=on-failure';
  438. echo '';
  439. echo '[Install]';
  440. echo 'WantedBy=multi-user.target'; } > "$rootdir/etc/systemd/system/cryptpad.service"
  441. chroot "$rootdir" systemctl enable cryptpad.service
  442. }
  443. function install_cryptpad_main {
  444. if [[ $(app_is_installed cryptpad_main) == "1" ]]; then
  445. return
  446. fi
  447. if [ ! -d /var/www/cryptpad ]; then
  448. mkdir /var/www/cryptpad
  449. fi
  450. if [ -d $CRYPTPAD_DIR ]; then
  451. rm -rf $CRYPTPAD_DIR
  452. fi
  453. if [ -d /repos/cryptpad ]; then
  454. mkdir $CRYPTPAD_DIR
  455. cp -r -p /repos/cryptpad/. $CRYPTPAD_DIR
  456. cd $CRYPTPAD_DIR || exit 3468356385
  457. git pull
  458. else
  459. function_check git_clone
  460. git_clone $CRYPTPAD_REPO $CRYPTPAD_DIR
  461. fi
  462. if [ ! -d $CRYPTPAD_DIR ]; then
  463. echo $'Unable to clone cryptpad repo'
  464. exit 783251
  465. fi
  466. # an unprivileged user to run as
  467. useradd -d $CRYPTPAD_DIR/ cryptpad
  468. cd $CRYPTPAD_DIR || exit 34683655
  469. git checkout $CRYPTPAD_COMMIT -b $CRYPTPAD_COMMIT
  470. set_completion_param "cryptpad commit" "$CRYPTPAD_COMMIT"
  471. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  472. CRYPTPAD_ONION_HOSTNAME=$(add_onion_service cryptpad 80 ${CRYPTPAD_ONION_PORT})
  473. cryptpad_nginx_site=/etc/nginx/sites-available/cryptpad
  474. { echo 'server {';
  475. echo " listen 127.0.0.1:$CRYPTPAD_ONION_PORT default_server;";
  476. echo " server_name $CRYPTPAD_ONION_HOSTNAME;";
  477. echo '';
  478. echo ' # Logs';
  479. echo ' access_log /dev/null;';
  480. echo ' error_log /dev/null;';
  481. echo '';
  482. echo ' # Root';
  483. echo " root $CRYPTPAD_DIR;";
  484. echo '';
  485. echo ' index index.html;';
  486. echo '';
  487. echo " if (\$args ~ ver=) {";
  488. echo " set \$cacheControl max-age=31536000;";
  489. echo ' }';
  490. echo " add_header Cache-Control \$cacheControl;";
  491. echo '';
  492. echo ' add_header X-XSS-Protection "1; mode=block";';
  493. echo ' add_header X-Content-Type-Options nosniff;';
  494. echo ' add_header X-Frame-Options SAMEORIGIN;';
  495. echo '';
  496. echo " if (\$uri = /pad/inner.html) {";
  497. echo " set \$scriptSrc \"'self' 'unsafe-eval' 'unsafe-inline'\";";
  498. echo ' }';
  499. echo '';
  500. echo ' location = /cryptpad_websocket {';
  501. echo " proxy_pass http://localhost:$CRYPTPAD_PORT;";
  502. echo " proxy_set_header X-Real-IP \$remote_addr;";
  503. echo " proxy_set_header Host \$host;";
  504. echo " proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;";
  505. echo '';
  506. echo ' # WebSocket support (nginx 1.4)';
  507. echo ' proxy_http_version 1.1;';
  508. echo " proxy_set_header Upgrade \$http_upgrade;";
  509. echo ' proxy_set_header Connection upgrade;';
  510. echo ' }';
  511. echo '';
  512. echo ' location ^~ /customize.dist/ {';
  513. echo ' # This is needed in order to prevent infinite recursion between /customize/ and the root';
  514. echo ' }';
  515. echo ' location ^~ /customize/ {';
  516. echo " rewrite ^/customize/(.*)\$ \$1 break;";
  517. echo " try_files /customize/\$uri /customize.dist/\$uri;";
  518. echo ' }';
  519. echo ' location = /api/config {';
  520. echo " proxy_pass http://localhost:$CRYPTPAD_PORT;";
  521. echo " proxy_set_header X-Real-IP \$remote_addr;";
  522. echo " proxy_set_header Host \$host;";
  523. echo " proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;";
  524. echo ' }';
  525. echo '';
  526. echo ' location ^~ /blob/ {';
  527. echo ' add_header Cache-Control max-age=31536000;';
  528. echo " try_files \$uri =404;";
  529. echo ' }';
  530. echo '';
  531. echo ' location ^~ /datastore/ {';
  532. echo ' add_header Cache-Control max-age=0;';
  533. echo " try_files \$uri =404;";
  534. echo ' }';
  535. echo '';
  536. echo ' location ^~ /register/ {';
  537. echo " try_files \$uri =404;";
  538. echo ' }';
  539. echo '';
  540. echo ' location ^~ /login/ {';
  541. echo " try_files \$uri =404;";
  542. echo ' }';
  543. echo '';
  544. echo ' location ^~ /about.html {';
  545. echo " try_files \$uri =404;";
  546. echo ' }';
  547. echo '';
  548. echo ' location ^~ /contact.html {';
  549. echo " try_files \$uri =404;";
  550. echo ' }';
  551. echo '';
  552. echo ' location ^~ /what-is-cryptpad.html {';
  553. echo " try_files \$uri =404;";
  554. echo ' }';
  555. echo '';
  556. echo ' location ~ ^/(register|login|settings|user|pad|drive|poll|slide|code|whiteboard|file|media)$ {';
  557. echo " rewrite ^(.*)\$ \$1/ redirect;";
  558. echo ' }';
  559. echo '';
  560. echo " try_files /www/\$uri /www/\$uri/index.html /customize/\$uri;";
  561. echo '}'; } > $cryptpad_nginx_site
  562. function_check nginx_ensite
  563. nginx_ensite cryptpad
  564. install_completed cryptpad_main
  565. }
  566. function install_cryptpad {
  567. function_check install_nodejs
  568. install_nodejs cryptpad
  569. install_cryptpad_main
  570. cd $CRYPTPAD_DIR || exit 35483548
  571. npm install
  572. npm install bower@1.8.4
  573. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  574. su -c './node_modules/bower/bin/bower install --config.interactive=false' - cryptpad
  575. su -c './node_modules/bower/bin/bower update --config.interactive=false' - cryptpad
  576. cp config.example.js config.js
  577. if [ ! -f config.js ]; then
  578. echo $'Cryptpad config file not found'
  579. exit 628252
  580. fi
  581. cryptpad_create_config
  582. chown -R cryptpad:cryptpad $CRYPTPAD_DIR
  583. # daemon
  584. { echo '[Unit]';
  585. echo 'Description=Cryptpad';
  586. echo 'After=syslog.target';
  587. echo 'After=network.target';
  588. echo '';
  589. echo '[Service]';
  590. echo 'User=cryptpad';
  591. echo 'Group=cryptpad';
  592. echo "WorkingDirectory=$CRYPTPAD_DIR";
  593. echo "ExecStart=/usr/local/bin/node $CRYPTPAD_DIR/server.js";
  594. echo 'Environment=PATH=/usr/bin:/usr/local/bin';
  595. echo 'Environment=NODE_ENV=production';
  596. echo 'Restart=on-failure';
  597. echo '';
  598. echo '[Install]';
  599. echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/cryptpad.service
  600. systemctl enable cryptpad.service
  601. systemctl daemon-reload
  602. systemctl start cryptpad.service
  603. sleep 8
  604. cryptpad_generate_api_config
  605. # install again
  606. cd $CRYPTPAD_DIR || exit 73537453
  607. su -c './node_modules/bower/bin/bower install --config.interactive=false' - cryptpad
  608. systemctl restart nginx
  609. APP_INSTALLED=1
  610. }
  611. # NOTE: deliberately there is no "exit 0"