freedombone-app-peertube 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # PeerTube server
  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 media"
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. PEERTUBE_DOMAIN_NAME=
  34. PEERTUBE_CODE=
  35. PEERTUBE_REPO="https://github.com/Chocobozzz/PeerTube"
  36. PEERTUBE_COMMIT='fef2c7164e025b12a64185dbab058ef4129733c6'
  37. PEERTUBE_ONION_PORT=8136
  38. PEERTUBE_PORT=9004
  39. MESH_PEERTUBE_PORT=8500
  40. PEERTUBE_DIR=/etc/peertube
  41. peertube_variables=(PEERTUBE_DOMAIN_NAME
  42. PEERTUBE_CODE
  43. PEERTUBE_ADMIN_PASSWORD
  44. ONION_ONLY
  45. DDNS_PROVIDER
  46. MY_USERNAME
  47. MY_EMAIL_ADDRESS)
  48. function peertube_create_database {
  49. if [ -f $IMAGE_PASSWORD_FILE ]; then
  50. PEERTUBE_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  51. else
  52. if [ ! $PEERTUBE_ADMIN_PASSWORD ]; then
  53. PEERTUBE_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  54. fi
  55. fi
  56. if [ ! $PEERTUBE_ADMIN_PASSWORD ]; then
  57. return
  58. fi
  59. systemctl restart postgresql
  60. run_system_query_postgresql "CREATE USER peertube WITH PASSWORD '$PEERTUBE_ADMIN_PASSWORD';"
  61. run_system_query_postgresql "CREATE DATABASE peertube OWNER peertube;"
  62. run_system_query_postgresql "GRANT ALL PRIVILEGES ON DATABASE peertube to peertube;"
  63. run_system_query_postgresql "set statement_timeout to 40000;"
  64. }
  65. function logging_on_peertube {
  66. echo -n ''
  67. }
  68. function logging_off_peertube {
  69. echo -n ''
  70. }
  71. function remove_user_peertube {
  72. remove_username="$1"
  73. }
  74. function add_user_peertube {
  75. if [[ $(app_is_installed peertube) == "0" ]]; then
  76. echo '0'
  77. return
  78. fi
  79. new_username="$1"
  80. new_user_password="$2"
  81. echo '0'
  82. }
  83. function install_interactive_peertube {
  84. if [ ! $ONION_ONLY ]; then
  85. ONION_ONLY='no'
  86. fi
  87. if [[ $ONION_ONLY != "no" ]]; then
  88. PEERTUBE_DOMAIN_NAME='peertube.local'
  89. write_config_param "PEERTUBE_DOMAIN_NAME" "$PEERTUBE_DOMAIN_NAME"
  90. else
  91. function_check interactive_site_details
  92. interactive_site_details "peertube" "PEERTUBE_DOMAIN_NAME" "PEERTUBE_CODE"
  93. fi
  94. APP_INSTALLED=1
  95. }
  96. function peertube_set_admin_email {
  97. read_config_param $MY_EMAIL_ADDRESS
  98. data=$(tempfile 2>/dev/null)
  99. trap "rm -f $data" 0 1 2 5 15
  100. dialog --title $"Set PeerTube administrator email address" \
  101. --backtitle $"Freedombone Control Panel" \
  102. --inputbox $"Admin email address" 8 75 "$MY_EMAIL_ADDRESS" 2>$data
  103. sel=$?
  104. case $sel in
  105. 0) peertube_email=$(<$data)
  106. if [[ "$peertube_email" != *' '* && "$peertube_email" != *','* && "$peertube_email" != *';'* && "$peertube_email" == *'@'* && "$peertube_email" == *'.'* ]]; then
  107. if [ ${#peertube_email} -gt 8 ]; then
  108. sed -i "s|email:.*|email: '${peertube_email}'|g" $PEERTUBE_DIR/config/production.yaml
  109. systemctl restart peertube
  110. dialog --title $"Set PeerTube administrator email address" \
  111. --msgbox $"Set to $peertube_email" 6 75
  112. fi
  113. fi
  114. ;;
  115. esac
  116. rm $data
  117. }
  118. function peertube_disable_signups {
  119. dialog --title $"Disable PeerTube signups" \
  120. --backtitle $"Freedombone Control Panel" \
  121. --yesno $"\nDo you wish to disable further PeerTube signups?" 8 75
  122. sel=$?
  123. case $sel in
  124. 0) sed "0,/RE/s/enabled:.*/enabled: false/" $PEERTUBE_DIR/config/production.yaml;;
  125. 1) sed "0,/RE/s/enabled:.*/enabled: true/" $PEERTUBE_DIR/config/production.yaml;;
  126. 255) return;;
  127. esac
  128. systemctl restart peertube
  129. }
  130. function configure_interactive_peertube {
  131. while true
  132. do
  133. data=$(tempfile 2>/dev/null)
  134. trap "rm -f $data" 0 1 2 5 15
  135. dialog --backtitle $"Freedombone Control Panel" \
  136. --title $"PeerTube" \
  137. --radiolist $"Choose an operation:" 10 70 4 \
  138. 1 $"Set administrator email address" off \
  139. 2 $"Disable or enable signups" off \
  140. 3 $"Exit" on 2> $data
  141. sel=$?
  142. case $sel in
  143. 1) break;;
  144. 255) break;;
  145. esac
  146. case $(cat $data) in
  147. 1) peertube_set_admin_email;;
  148. 2) peertube_disable_signups;;
  149. 3) break;;
  150. esac
  151. done
  152. }
  153. function change_password_peertube {
  154. PEERTUBE_USERNAME="$1"
  155. PEERTUBE_PASSWORD="$2"
  156. if [ ${#PEERTUBE_PASSWORD} -lt 8 ]; then
  157. echo $'Peertube password is too short'
  158. return
  159. fi
  160. #${PROJECT_NAME}-pass -u $PEERTUBE_USERNAME -a peertube -p "$PEERTUBE_PASSWORD"
  161. }
  162. function reconfigure_peertube {
  163. echo -n ''
  164. }
  165. function upgrade_peertube {
  166. CURR_PEERTUBE_COMMIT=$(get_completion_param "peertube commit")
  167. if [[ "$CURR_PEERTUBE_COMMIT" == "$PEERTUBE_COMMIT" ]]; then
  168. return
  169. fi
  170. read_config_param PEERTUBE_DOMAIN_NAME
  171. systemctl stop peertube
  172. cd $PEERTUBE_DIR
  173. function_check set_repo_commit
  174. set_repo_commit $PEERTUBE_DIR "peertube commit" "$PEERTUBE_COMMIT" $PEERTUBE_REPO
  175. npm run upgrade-peertube
  176. chown -R peertube:peertube $PEERTUBE_DIR
  177. systemctl start peertube
  178. }
  179. function backup_local_peertube {
  180. PEERTUBE_DOMAIN_NAME='peertube.local'
  181. if grep -q "peertube domain" $COMPLETION_FILE; then
  182. PEERTUBE_DOMAIN_NAME=$(get_completion_param "peertube domain")
  183. fi
  184. systemctl stop peertube
  185. USE_POSTGRESQL=1
  186. function_check backup_database_to_usb
  187. backup_database_to_usb peertube
  188. systemctl start peertube
  189. peertube_path=$PEERTUBE_DIR/videos
  190. if [ -d $peertube_path ]; then
  191. suspend_site ${PEERTUBE_DOMAIN_NAME}
  192. systemctl stop peertube
  193. backup_directory_to_usb $peertube_path peertubevideos
  194. systemctl start peertube
  195. restart_site
  196. fi
  197. }
  198. function restore_local_peertube {
  199. PEERTUBE_DOMAIN_NAME='peertube.local'
  200. if grep -q "peertube domain" $COMPLETION_FILE; then
  201. PEERTUBE_DOMAIN_NAME=$(get_completion_param "peertube domain")
  202. fi
  203. if [ $PEERTUBE_DOMAIN_NAME ]; then
  204. suspend_site ${PEERTUBE_DOMAIN_NAME}
  205. systemctl stop peertube
  206. USE_POSTGRESQL=1
  207. restore_database peertube
  208. temp_restore_dir=/root/temppeertubevideos
  209. function_check restore_directory_from_usb
  210. restore_directory_from_usb $temp_restore_dir peertubevideos
  211. if [ -d $temp_restore_dir ]; then
  212. if [ -d $temp_restore_dir$PEERTUBE_DIR/videos ]; then
  213. cp -r $temp_restore_dir$PEERTUBE_DIR/videos/* $PEERTUBE_DIR/videos/
  214. else
  215. cp -r $temp_restore_dir/* $PEERTUBE_DIR/videos/
  216. fi
  217. chown -R peertube:peertube $PEERTUBE_DIR
  218. rm -rf $temp_restore_dir
  219. fi
  220. systemctl start peertube
  221. restart_site
  222. fi
  223. }
  224. function backup_remote_peertube {
  225. PEERTUBE_DOMAIN_NAME='peertube.local'
  226. if grep -q "peertube domain" $COMPLETION_FILE; then
  227. PEERTUBE_DOMAIN_NAME=$(get_completion_param "peertube domain")
  228. fi
  229. systemctl stop peertube
  230. USE_POSTGRESQL=1
  231. function_check backup_database_to_friend
  232. backup_database_to_friend peertube
  233. systemctl start peertube
  234. temp_backup_dir=$PEERTUBE_DIR/videos
  235. if [ -d $temp_backup_dir ]; then
  236. systemctl stop peertube
  237. suspend_site ${PEERTUBE_DOMAIN_NAME}
  238. backup_directory_to_friend $temp_backup_dir peertubevideos
  239. restart_site
  240. systemctl start peertube
  241. else
  242. echo $"Peertube domain specified but not found in $temp_backup_dir"
  243. exit 6383523
  244. fi
  245. }
  246. function restore_remote_peertube {
  247. PEERTUBE_DOMAIN_NAME='peertube.local'
  248. if grep -q "peertube domain" $COMPLETION_FILE; then
  249. PEERTUBE_DOMAIN_NAME=$(get_completion_param "peertube domain")
  250. fi
  251. suspend_site ${PEERTUBE_DOMAIN_NAME}
  252. systemctl stop peertube
  253. USE_POSTGRESQL=1
  254. function_check restore_database_from_friend
  255. restore_database_from_friend peertube
  256. temp_restore_dir=/root/temppeertubevideos
  257. function_check restore_directory_from_friend
  258. restore_directory_from_friend $temp_restore_dir peertubevideos
  259. if [ -d $temp_restore_dir ]; then
  260. if [ -d $temp_restore_dir$PEERTUBE_DIR/videos ]; then
  261. cp -r $temp_restore_dir$PEERTUBE_DIR/videos/* $PEERTUBE_DIR/videos/
  262. else
  263. cp -r $temp_restore_dir/* $PEERTUBE_DIR/videos/
  264. fi
  265. chown -R peertube: $PEERTUBE_DIR
  266. rm -rf $temp_restore_dir
  267. fi
  268. systemctl start peertube
  269. restart_site
  270. }
  271. function remove_peertube {
  272. if [ ${#PEERTUBE_DOMAIN_NAME} -eq 0 ]; then
  273. return
  274. fi
  275. systemctl stop peertube
  276. systemctl disable peertube
  277. rm /etc/systemd/system/peertube.service
  278. systemctl daemon-reload
  279. function_check remove_nodejs
  280. remove_nodejs peertube
  281. read_config_param "PEERTUBE_DOMAIN_NAME"
  282. nginx_dissite $PEERTUBE_DOMAIN_NAME
  283. remove_certs ${PEERTUBE_DOMAIN_NAME}
  284. if [ -f /etc/nginx/sites-available/$PEERTUBE_DOMAIN_NAME ]; then
  285. rm -f /etc/nginx/sites-available/$PEERTUBE_DOMAIN_NAME
  286. fi
  287. if [ -d /var/www/$PEERTUBE_DOMAIN_NAME ]; then
  288. rm -rf /var/www/$PEERTUBE_DOMAIN_NAME
  289. fi
  290. remove_config_param PEERTUBE_DOMAIN_NAME
  291. remove_config_param PEERTUBE_CODE
  292. function_check remove_onion_service
  293. remove_onion_service peertube ${PEERTUBE_ONION_PORT}
  294. remove_completion_param "install_peertube"
  295. sed -i '/peertube/d' $COMPLETION_FILE
  296. function_check drop_database_postgresql
  297. drop_database_postgresql peertube
  298. remove_postgresql_user peertube
  299. groupdel -f peertube
  300. userdel -r peertube
  301. if [ -d $PEERTUBE_DIR ]; then
  302. rm -rf $PEERTUBE_DIR
  303. fi
  304. function_check remove_ddns_domain
  305. remove_ddns_domain $PEERTUBE_DOMAIN_NAME
  306. }
  307. function peertube_setup_web {
  308. peertube_nginx_file=/etc/nginx/sites-available/$PEERTUBE_DOMAIN_NAME
  309. if [[ $ONION_ONLY == "no" ]]; then
  310. echo 'server {' > $peertube_nginx_file
  311. echo ' listen 80;' >> $peertube_nginx_file
  312. echo ' listen [::]:80;' >> $peertube_nginx_file
  313. echo " server_name $PEERTUBE_DOMAIN_NAME;" >> $peertube_nginx_file
  314. echo ' rewrite ^ https://$server_name$request_uri? permanent;' >> $peertube_nginx_file
  315. echo '}' >> $peertube_nginx_file
  316. echo '' >> $peertube_nginx_file
  317. echo 'server {' >> $peertube_nginx_file
  318. echo ' listen 443 ssl http2;' >> $peertube_nginx_file
  319. echo ' listen [::]:443 ssl http2;' >> $peertube_nginx_file
  320. echo " server_name $PEERTUBE_DOMAIN_NAME;" >> $peertube_nginx_file
  321. echo '' >> $peertube_nginx_file
  322. function_check nginx_ssl
  323. nginx_ssl $PEERTUBE_DOMAIN_NAME mobile
  324. function_check nginx_disable_sniffing
  325. nginx_disable_sniffing $PEERTUBE_DOMAIN_NAME
  326. echo ' add_header Strict-Transport-Security max-age=15768000;' >> $peertube_nginx_file
  327. echo '' >> $peertube_nginx_file
  328. echo ' location / {' >> $peertube_nginx_file
  329. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  330. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $peertube_nginx_file
  331. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  332. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  333. echo '' >> $peertube_nginx_file
  334. echo ' # For the video upload' >> $peertube_nginx_file
  335. echo ' client_max_body_size 2G;' >> $peertube_nginx_file
  336. echo ' }' >> $peertube_nginx_file
  337. echo '' >> $peertube_nginx_file
  338. echo ' location /static/webseed {' >> $peertube_nginx_file
  339. echo " if (\$request_method = 'OPTIONS') {" >> $peertube_nginx_file
  340. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  341. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  342. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  343. echo " add_header 'Access-Control-Max-Age' 1728000;" >> $peertube_nginx_file
  344. echo " add_header 'Content-Type' 'text/plain charset=UTF-8';" >> $peertube_nginx_file
  345. echo " add_header 'Content-Length' 0;" >> $peertube_nginx_file
  346. echo ' return 204;' >> $peertube_nginx_file
  347. echo ' }' >> $peertube_nginx_file
  348. echo '' >> $peertube_nginx_file
  349. echo " if (\$request_method = 'GET') {" >> $peertube_nginx_file
  350. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  351. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  352. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  353. echo ' }' >> $peertube_nginx_file
  354. echo '' >> $peertube_nginx_file
  355. echo " alias $PEERTUBE_DIR/videos;" >> $peertube_nginx_file
  356. echo ' }' >> $peertube_nginx_file
  357. echo '' >> $peertube_nginx_file
  358. echo ' # Websocket tracker' >> $peertube_nginx_file
  359. echo ' location /tracker/socket {' >> $peertube_nginx_file
  360. echo ' # Peers send a message to the tracker every 15 minutes' >> $peertube_nginx_file
  361. echo ' # Dont close the websocket before this time' >> $peertube_nginx_file
  362. echo ' proxy_read_timeout 1200s;' >> $peertube_nginx_file
  363. echo ' proxy_set_header Upgrade $http_upgrade;' >> $peertube_nginx_file
  364. echo ' proxy_set_header Connection "upgrade";' >> $peertube_nginx_file
  365. echo ' proxy_http_version 1.1;' >> $peertube_nginx_file
  366. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  367. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  368. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  369. echo ' }' >> $peertube_nginx_file
  370. echo '}' >> $peertube_nginx_file
  371. else
  372. echo -n '' > $peertube_nginx_file
  373. fi
  374. echo 'server {' >> $peertube_nginx_file
  375. echo " listen 127.0.0.1:$PEERTUBE_ONION_PORT default_server;" >> $peertube_nginx_file
  376. echo " server_name $PEERTUBE_ONION_HOSTNAME;" >> $peertube_nginx_file
  377. echo '' >> $peertube_nginx_file
  378. echo ' location / {' >> $peertube_nginx_file
  379. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  380. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $peertube_nginx_file
  381. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  382. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  383. echo '' >> $peertube_nginx_file
  384. echo ' # For the video upload' >> $peertube_nginx_file
  385. echo ' client_max_body_size 2G;' >> $peertube_nginx_file
  386. echo ' }' >> $peertube_nginx_file
  387. echo '' >> $peertube_nginx_file
  388. echo ' location /static/webseed {' >> $peertube_nginx_file
  389. echo " if (\$request_method = 'OPTIONS') {" >> $peertube_nginx_file
  390. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  391. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  392. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  393. echo " add_header 'Access-Control-Max-Age' 1728000;" >> $peertube_nginx_file
  394. echo " add_header 'Content-Type' 'text/plain charset=UTF-8';" >> $peertube_nginx_file
  395. echo " add_header 'Content-Length' 0;" >> $peertube_nginx_file
  396. echo ' return 204;' >> $peertube_nginx_file
  397. echo ' }' >> $peertube_nginx_file
  398. echo '' >> $peertube_nginx_file
  399. echo " if (\$request_method = 'GET') {" >> $peertube_nginx_file
  400. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  401. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  402. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  403. echo ' }' >> $peertube_nginx_file
  404. echo '' >> $peertube_nginx_file
  405. echo " alias $PEERTUBE_DIR/videos;" >> $peertube_nginx_file
  406. echo ' }' >> $peertube_nginx_file
  407. echo '' >> $peertube_nginx_file
  408. echo ' # Websocket tracker' >> $peertube_nginx_file
  409. echo ' location /tracker/socket {' >> $peertube_nginx_file
  410. echo ' # Peers send a message to the tracker every 15 minutes' >> $peertube_nginx_file
  411. echo ' # Dont close the websocket before this time' >> $peertube_nginx_file
  412. echo ' proxy_read_timeout 1200s;' >> $peertube_nginx_file
  413. echo ' proxy_set_header Upgrade $http_upgrade;' >> $peertube_nginx_file
  414. echo ' proxy_set_header Connection "upgrade";' >> $peertube_nginx_file
  415. echo ' proxy_http_version 1.1;' >> $peertube_nginx_file
  416. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  417. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  418. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  419. echo ' }' >> $peertube_nginx_file
  420. echo '}' >> $peertube_nginx_file
  421. # CSP currently causes an error
  422. sed -i '/Content-Security-Policy/d' $peertube_nginx_file
  423. function_check create_site_certificate
  424. create_site_certificate $PEERTUBE_DOMAIN_NAME 'yes'
  425. function_check nginx_ensite
  426. nginx_ensite $PEERTUBE_DOMAIN_NAME
  427. }
  428. function mesh_peertube_setup_web {
  429. peertube_nginx_file=$rootdir/etc/nginx/sites-available/peertube
  430. echo 'server {' >> $peertube_nginx_file
  431. echo " listen $MESH_PEERTUBE_PORT http2;" >> $peertube_nginx_file
  432. echo ' listen [::]:$MESH_PEERTUBE_PORT http2;' >> $peertube_nginx_file
  433. echo ' server_name $HOSTNAME;' >> $peertube_nginx_file
  434. echo '' >> $peertube_nginx_file
  435. echo ' location / {' >> $peertube_nginx_file
  436. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  437. echo ' proxy_set_header X-Real-IP $remote_addr;' >> $peertube_nginx_file
  438. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  439. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  440. echo '' >> $peertube_nginx_file
  441. echo ' # For the video upload' >> $peertube_nginx_file
  442. echo ' client_max_body_size 2G;' >> $peertube_nginx_file
  443. echo ' }' >> $peertube_nginx_file
  444. echo '' >> $peertube_nginx_file
  445. echo ' location /static/webseed {' >> $peertube_nginx_file
  446. echo " if (\$request_method = 'OPTIONS') {" >> $peertube_nginx_file
  447. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  448. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  449. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  450. echo " add_header 'Access-Control-Max-Age' 1728000;" >> $peertube_nginx_file
  451. echo " add_header 'Content-Type' 'text/plain charset=UTF-8';" >> $peertube_nginx_file
  452. echo " add_header 'Content-Length' 0;" >> $peertube_nginx_file
  453. echo ' return 204;' >> $peertube_nginx_file
  454. echo ' }' >> $peertube_nginx_file
  455. echo '' >> $peertube_nginx_file
  456. echo " if (\$request_method = 'GET') {" >> $peertube_nginx_file
  457. echo " add_header 'Access-Control-Allow-Origin' '*';" >> $peertube_nginx_file
  458. echo " add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';" >> $peertube_nginx_file
  459. echo " add_header 'Access-Control-Allow-Headers' 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';" >> $peertube_nginx_file
  460. echo ' }' >> $peertube_nginx_file
  461. echo '' >> $peertube_nginx_file
  462. echo " alias $PEERTUBE_DIR/videos;" >> $peertube_nginx_file
  463. echo ' }' >> $peertube_nginx_file
  464. echo '' >> $peertube_nginx_file
  465. echo ' # Websocket tracker' >> $peertube_nginx_file
  466. echo ' location /tracker/socket {' >> $peertube_nginx_file
  467. echo ' # Peers send a message to the tracker every 15 minutes' >> $peertube_nginx_file
  468. echo ' # Dont close the websocket before this time' >> $peertube_nginx_file
  469. echo ' proxy_read_timeout 1200s;' >> $peertube_nginx_file
  470. echo ' proxy_set_header Upgrade $http_upgrade;' >> $peertube_nginx_file
  471. echo ' proxy_set_header Connection "upgrade";' >> $peertube_nginx_file
  472. echo ' proxy_http_version 1.1;' >> $peertube_nginx_file
  473. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> $peertube_nginx_file
  474. echo ' proxy_set_header Host $host;' >> $peertube_nginx_file
  475. echo " proxy_pass http://localhost:${PEERTUBE_PORT};" >> $peertube_nginx_file
  476. echo ' }' >> $peertube_nginx_file
  477. echo '}' >> $peertube_nginx_file
  478. }
  479. function peertube_create_config {
  480. peertube_prefix=$1
  481. peertube_config_file=$peertube_prefix$PEERTUBE_DIR/config/production.yaml
  482. echo 'listen:' > $peertube_config_file
  483. echo " port: $PEERTUBE_PORT" >> $peertube_config_file
  484. echo '' >> $peertube_config_file
  485. echo '# Correspond to your reverse proxy "listen" configuration' >> $peertube_config_file
  486. echo 'webserver:' >> $peertube_config_file
  487. if [ ! $peertube_prefix ]; then
  488. if [[ $ONION_ONLY == 'no' ]]; then
  489. echo ' https: true' >> $peertube_config_file
  490. echo " hostname: '$PEERTUBE_DOMAIN_NAME'" >> $peertube_config_file
  491. echo ' port: 443' >> $peertube_config_file
  492. else
  493. echo ' https: false' >> $peertube_config_file
  494. echo " hostname: '$PEERTUBE_ONION_HOSTNAME'" >> $peertube_config_file
  495. echo ' port: 80' >> $peertube_config_file
  496. fi
  497. else
  498. echo ' https: false' >> $peertube_config_file
  499. echo " hostname: ''" >> $peertube_config_file
  500. echo " port: $MESH_PEERTUBE_PORT" >> $peertube_config_file
  501. fi
  502. echo '' >> $peertube_config_file
  503. echo '# Your database name will be "peertube"+database.suffix' >> $peertube_config_file
  504. echo 'database:' >> $peertube_config_file
  505. echo " hostname: 'localhost'" >> $peertube_config_file
  506. echo ' port: 5432' >> $peertube_config_file
  507. echo " suffix: ''" >> $peertube_config_file
  508. echo " username: 'peertube'" >> $peertube_config_file
  509. if [ ! $peertube_prefix ]; then
  510. echo " password: '$PEERTUBE_ADMIN_PASSWORD'" >> $peertube_config_file
  511. else
  512. echo " password: ''" >> $peertube_config_file
  513. fi
  514. echo '' >> $peertube_config_file
  515. echo '# From the project root directory' >> $peertube_config_file
  516. echo 'storage:' >> $peertube_config_file
  517. echo " certs: 'certs/'" >> $peertube_config_file
  518. echo " videos: 'videos/'" >> $peertube_config_file
  519. echo " logs: 'logs/'" >> $peertube_config_file
  520. echo " previews: 'previews/'" >> $peertube_config_file
  521. echo " thumbnails: 'thumbnails/'" >> $peertube_config_file
  522. echo " torrents: 'torrents/'" >> $peertube_config_file
  523. echo " cache: 'cache/'" >> $peertube_config_file
  524. echo '' >> $peertube_config_file
  525. echo 'cache:' >> $peertube_config_file
  526. echo ' previews:' >> $peertube_config_file
  527. echo ' size: 10 # Max number of previews you want to cache' >> $peertube_config_file
  528. echo '' >> $peertube_config_file
  529. echo 'admin:' >> $peertube_config_file
  530. echo " email: 'root@local'" >> $peertube_config_file
  531. echo '' >> $peertube_config_file
  532. echo 'signup:' >> $peertube_config_file
  533. echo ' enabled: true' >> $peertube_config_file
  534. echo ' limit: 2 # When the limit is reached, registrations are disabled. -1 == unlimited' >> $peertube_config_file
  535. echo '' >> $peertube_config_file
  536. echo 'user:' >> $peertube_config_file
  537. echo ' # Default value of maximum video BYTES the user can upload (does not take into account transcoded files).' >> $peertube_config_file
  538. echo ' # -1 == unlimited' >> $peertube_config_file
  539. echo ' video_quota: -1' >> $peertube_config_file
  540. echo '' >> $peertube_config_file
  541. echo '# If enabled, the video will be transcoded to mp4 (x264) with "faststart" flag' >> $peertube_config_file
  542. echo '# Uses a lot of CPU!' >> $peertube_config_file
  543. echo 'transcoding:' >> $peertube_config_file
  544. echo ' enabled: true' >> $peertube_config_file
  545. echo ' threads: 2' >> $peertube_config_file
  546. echo ' resolutions: # Only created if the original video has a higher resolution' >> $peertube_config_file
  547. echo ' 240p: true' >> $peertube_config_file
  548. echo ' 360p: false' >> $peertube_config_file
  549. echo ' 480p: false' >> $peertube_config_file
  550. echo ' 720p: false' >> $peertube_config_file
  551. echo ' 1080p: false' >> $peertube_config_file
  552. }
  553. function mesh_install_peertube {
  554. if [[ $VARIANT != "meshclient" && $VARIANT != "meshusb" ]]; then
  555. return
  556. fi
  557. chroot "$rootdir" apt-get -yq install ffmpeg
  558. function_check install_postgresql
  559. install_postgresql
  560. if [ -d $rootdir$PEERTUBE_DIR ]; then
  561. rm -rf $rootdir$PEERTUBE_DIR
  562. fi
  563. git clone $PEERTUBE_REPO $rootdir$PEERTUBE_DIR
  564. chroot "$rootdir" groupadd peertube
  565. chroot "$rootdir" useradd -c "PeerTube system account" -d $PEERTUBE_DIR -m -r -g peertube peertube
  566. cd $rootdir$PEERTUBE_DIR
  567. git checkout $PEERTUBE_COMMIT -b $PEERTUBE_COMMIT
  568. get_npm_arch
  569. cat <<EOF > $rootdir/usr/bin/install_peertube
  570. #!/bin/bash
  571. cd $PEERTUBE_DIR
  572. npm install --arch=$NPM_ARCH -g yarn
  573. if [ ! "$?" = "0" ]; then
  574. echo $'PeerTube Failed to install yarn'
  575. exit 79353234
  576. fi
  577. yarn add -D webpack --network-concurrency 1
  578. yarn install
  579. if [ ! "$?" = "0" ]; then
  580. echo $'PeerTube Failed to run yarn install'
  581. exit 63754235
  582. fi
  583. npm install --arch=$NPM_ARCH
  584. if [ ! "$?" = "0" ]; then
  585. echo $'PeerTube Failed to install peertube'
  586. exit 7835243
  587. fi
  588. npm run build --arch=$NPM_ARCH
  589. if [ ! "$?" = "0" ]; then
  590. echo $'PeerTube Failed to build peertube'
  591. exit 5293593
  592. fi
  593. EOF
  594. chmod +x $rootdir/usr/bin/install_peertube
  595. chroot "$rootdir" /usr/bin/install_peertube
  596. echo '[Unit]' > $rootdir/etc/systemd/system/peertube.service
  597. echo 'Description=PeerTube Decentralized video streaming platform' >> $rootdir/etc/systemd/system/peertube.service
  598. echo 'After=syslog.target' >> $rootdir/etc/systemd/system/peertube.service
  599. echo 'After=network.target' >> $rootdir/etc/systemd/system/peertube.service
  600. echo '' >> $rootdir/etc/systemd/system/peertube.service
  601. echo '[Service]' >> $rootdir/etc/systemd/system/peertube.service
  602. echo 'User=peertube' >> $rootdir/etc/systemd/system/peertube.service
  603. echo 'Group=peertube' >> $rootdir/etc/systemd/system/peertube.service
  604. echo "WorkingDirectory=$PEERTUBE_DIR" >> $rootdir/etc/systemd/system/peertube.service
  605. echo "ExecStart=/usr/local/bin/npm start" >> $rootdir/etc/systemd/system/peertube.service
  606. echo "ExecStop=/usr/local/bin/npm stop" >> $rootdir/etc/systemd/system/peertube.service
  607. echo 'StandardOutput=syslog' >> $rootdir/etc/systemd/system/peertube.service
  608. echo 'StandardError=syslog' >> $rootdir/etc/systemd/system/peertube.service
  609. echo 'SyslogIdentifier=peertube' >> $rootdir/etc/systemd/system/peertube.service
  610. echo 'Restart=always' >> $rootdir/etc/systemd/system/peertube.service
  611. echo "Environment=NODE_ENV=production" >> $rootdir/etc/systemd/system/peertube.service
  612. echo '' >> $rootdir/etc/systemd/system/peertube.service
  613. echo '[Install]' >> $rootdir/etc/systemd/system/peertube.service
  614. echo 'WantedBy=multi-user.target' >> $rootdir/etc/systemd/system/peertube.service
  615. peertube_create_config $rootdir
  616. chroot "$rootdir" chown -R peertube:peertube $PEERTUBE_DIR
  617. mesh_peertube_setup_web
  618. }
  619. function install_peertube {
  620. if [[ $VARIANT == "mesh"* ]]; then
  621. mesh_install_peertube
  622. return
  623. fi
  624. if [ ! $ONION_ONLY ]; then
  625. ONION_ONLY='no'
  626. fi
  627. if [ ! $PEERTUBE_DOMAIN_NAME ]; then
  628. echo $'The peertube domain name was not specified'
  629. exit 783523
  630. fi
  631. apt-get -yq install ffmpeg
  632. function_check install_postgresql
  633. install_postgresql
  634. if [ ! -d /var/www/$PEERTUBE_DOMAIN_NAME/htdocs ]; then
  635. mkdir -p /var/www/$PEERTUBE_DOMAIN_NAME/htdocs
  636. fi
  637. if [ -d $PEERTUBE_DIR ]; then
  638. rm -rf $PEERTUBE_DIR
  639. fi
  640. groupadd peertube
  641. useradd -c "PeerTube system account" -d $PEERTUBE_DIR -m -r -g peertube peertube
  642. peertube_create_database
  643. function_check install_nodejs
  644. install_nodejs peertube
  645. if [ -d /repos/peertube ]; then
  646. mkdir -p $PEERTUBE_DIR
  647. cp -r -p /repos/peertube/. $PEERTUBE_DIR
  648. cd $PEERTUBE_DIR
  649. git pull
  650. else
  651. function_check git_clone
  652. git_clone $PEERTUBE_REPO $PEERTUBE_DIR
  653. fi
  654. cd $PEERTUBE_DIR
  655. git checkout $PEERTUBE_COMMIT -b $PEERTUBE_COMMIT
  656. set_completion_param "peertube commit" "$PEERTUBE_COMMIT"
  657. npm install -g yarn
  658. if [ ! "$?" = "0" ]; then
  659. echo $'Failed to install yarn'
  660. exit 79353234
  661. fi
  662. yarn add -D webpack --network-concurrency 1
  663. yarn install
  664. if [ ! "$?" = "0" ]; then
  665. echo $'Failed to run yarn install'
  666. exit 63754235
  667. fi
  668. npm install
  669. if [ ! "$?" = "0" ]; then
  670. echo $'Failed to install peertube'
  671. exit 7835243
  672. fi
  673. npm run build
  674. if [ ! "$?" = "0" ]; then
  675. echo $'Failed to build peertube'
  676. exit 5293593
  677. fi
  678. PEERTUBE_ONION_HOSTNAME=$(add_onion_service peertube 80 ${PEERTUBE_ONION_PORT})
  679. echo '[Unit]' > /etc/systemd/system/peertube.service
  680. echo 'Description=PeerTube Decentralized video streaming platform' >> /etc/systemd/system/peertube.service
  681. echo 'After=syslog.target' >> /etc/systemd/system/peertube.service
  682. echo 'After=network.target' >> /etc/systemd/system/peertube.service
  683. echo '' >> /etc/systemd/system/peertube.service
  684. echo '[Service]' >> /etc/systemd/system/peertube.service
  685. echo 'User=peertube' >> /etc/systemd/system/peertube.service
  686. echo 'Group=peertube' >> /etc/systemd/system/peertube.service
  687. echo "WorkingDirectory=$PEERTUBE_DIR" >> /etc/systemd/system/peertube.service
  688. echo "ExecStart=/usr/local/bin/npm start" >> /etc/systemd/system/peertube.service
  689. echo "ExecStop=/usr/local/bin/npm stop" >> /etc/systemd/system/peertube.service
  690. echo 'StandardOutput=syslog' >> /etc/systemd/system/peertube.service
  691. echo 'StandardError=syslog' >> /etc/systemd/system/peertube.service
  692. echo 'SyslogIdentifier=peertube' >> /etc/systemd/system/peertube.service
  693. echo 'Restart=always' >> /etc/systemd/system/peertube.service
  694. echo "Environment=NODE_ENV=production" >> /etc/systemd/system/peertube.service
  695. echo '' >> /etc/systemd/system/peertube.service
  696. echo '[Install]' >> /etc/systemd/system/peertube.service
  697. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/peertube.service
  698. peertube_create_config
  699. chown -R peertube:peertube $PEERTUBE_DIR
  700. peertube_setup_web
  701. ${PROJECT_NAME}-pass -u $MY_USERNAME -a peertube -p "$PEERTUBE_ADMIN_PASSWORD"
  702. function_check add_ddns_domain
  703. add_ddns_domain $PEERTUBE_DOMAIN_NAME
  704. systemctl enable peertube
  705. systemctl daemon-reload
  706. systemctl start peertube
  707. systemctl restart nginx
  708. set_completion_param "peertube domain" "$PEERTUBE_DOMAIN_NAME"
  709. APP_INSTALLED=1
  710. }
  711. # NOTE: deliberately no exit 0