freedombone-app-icecast 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Icecast application
  12. #
  13. # Notes: An attempt was made to get ices2 running with systemd, but that
  14. # was very unsuccessful. Instead there's a hacky cron entry which
  15. # starts icecast2 and ices2 if necessary
  16. #
  17. # License
  18. # =======
  19. #
  20. # Copyright (C) 2017-2018 Bob Mottram <bob@freedombone.net>
  21. #
  22. # This program is free software: you can redistribute it and/or modify
  23. # it under the terms of the GNU Affero General Public License as published by
  24. # the Free Software Foundation, either version 3 of the License, or
  25. # (at your option) any later version.
  26. #
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU Affero General Public License for more details.
  31. #
  32. # You should have received a copy of the GNU Affero General Public License
  33. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  34. VARIANTS='full'
  35. IN_DEFAULT_INSTALL=0
  36. SHOW_ON_ABOUT=1
  37. SHOW_ICANN_ADDRESS_ON_ABOUT=0
  38. ICECAST_DOMAIN_NAME=
  39. ICECAST_CODE=
  40. ICECAST_PORT=8005
  41. ICECAST_ONION_PORT=8146
  42. ICECAST_DIR=/icestream
  43. ICECAST_PLAYLIST_FILE=/etc/ices2/playlist.txt
  44. ICECAST_LOGIN_TEXT=$"Icecast login"
  45. icecast_variables=(MY_USERNAME
  46. MY_EMAIL_ADDRESS
  47. ONION_ONLY
  48. ICECAST_DOMAIN_NAME
  49. ICECAST_CODE
  50. DEFAULT_LANGUAGE)
  51. function icecast_rescan {
  52. if [ -d $ICECAST_DIR ]; then
  53. if [ -f $ICECAST_PLAYLIST_FILE ]; then
  54. rm $ICECAST_PLAYLIST_FILE
  55. fi
  56. icecast_add_file_to_playlist $ICECAST_DIR
  57. fi
  58. }
  59. function icecast_update_daemon {
  60. systemctl stop icecast2
  61. if [ -f /etc/init.d/icecast2 ]; then
  62. rm /etc/init.d/icecast2
  63. fi
  64. { echo '#!/bin/sh';
  65. echo "kill \$(pidof ices2)";
  66. echo 'systemctl stop icecast2'; } > /usr/bin/stop_icecast
  67. chmod +x /usr/bin/stop_icecast
  68. # Note that the sleep here actually is important
  69. { echo '#!/bin/bash';
  70. echo "isrunning=\$(ps aux | grep ices2)";
  71. echo "if [[ \"\$isrunning\" != *\"ices-playlist\"* ]]; then";
  72. echo ' systemctl start icecast2';
  73. echo ' sleep 3';
  74. echo ' cd /etc/ices2';
  75. echo ' ices2 ices-playlist.xml';
  76. echo 'fi'; } > /usr/bin/start_icecast
  77. chmod +x /usr/bin/start_icecast
  78. { echo '[Unit]';
  79. echo 'Description=Icecast';
  80. echo 'After=network.target';
  81. echo 'After=tor.service';
  82. echo '';
  83. echo '[Service]';
  84. echo 'User=icecast2';
  85. echo 'Group=icecast';
  86. echo 'ExecStart=/usr/bin/icecast2 -c /etc/icecast2/icecast.xml';
  87. echo 'Restart=on-failure';
  88. echo 'RestartSec=10';
  89. echo '';
  90. echo '[Install]';
  91. echo 'WantedBy=multi-user.target'; } > /etc/systemd/system/icecast2.service
  92. chown -R icecast2:icecast /etc/ices2
  93. chown -R icecast2:icecast /etc/icecast2
  94. systemctl daemon-reload
  95. systemctl enable icecast2
  96. if ! grep -q "start_icecast" /etc/crontab; then
  97. cron_add_mins 1 '/usr/bin/start_icecast 2> /dev/null'
  98. fi
  99. }
  100. function change_password_icecast {
  101. curr_username="$1"
  102. new_user_password="$2"
  103. stop_icecast
  104. sed -i -e "s|<source-password>[^<]*</source-password>|<source-password>$new_user_password</source-password>|" \
  105. -e "s|<relay-password>[^<]*</relay-password>|<relay-password>$new_user_password</relay-password>|" \
  106. -e "s|<admin-password>[^<]*</admin-password>|<admin-password>$new_user_password</admin-password>|" \
  107. /etc/icecast2/icecast.xml
  108. sed -i "s|<password>.*|<password>${new_user_password}</password>|g" /etc/ices2/ices-playlist.xml
  109. "${PROJECT_NAME}-pass" -u "$curr_username" -a icecast -p "$new_user_password"
  110. start_icecast
  111. }
  112. function logging_on_icecast {
  113. echo -n ''
  114. }
  115. function logging_off_icecast {
  116. echo -n ''
  117. }
  118. function reconfigure_icecast {
  119. echo -n ''
  120. }
  121. function icecast_convert_files {
  122. clear
  123. cd "${1}" || exit 2346824648
  124. echo $'Converting any mp3 files to ogg format'
  125. find . -type f -name '*.mp3' -exec bash -c 'ffmpeg -i "$0" -c:a libvorbis -q:a 4 "${0/%mp3/ogg}"' '{}' \;
  126. find . -name "*.mp3" -print0 | xargs -0 rm -f
  127. echo $'Converting any mp4 files to ogv format'
  128. find . -type f -name '*.mp4' -exec bash -c 'ffmpeg -i "$0" -c:a libvorbis -q:a 4 "${0/%mp3/ogv}"' '{}' \;
  129. find . -name "*.mp4" -print0 | xargs -0 rm -f
  130. chown -R icecast2:icecast $ICECAST_DIR
  131. }
  132. function icecast_add_file_to_playlist {
  133. files_dir="${1}"
  134. if [ ! -d "$files_dir" ]; then
  135. return
  136. fi
  137. echo $'Adding ogg files to playlist'
  138. find "$files_dir" -type f -name '*.ogg' -print0 | while read -r -d $'\0' file; do
  139. if ! grep -q "$file" $ICECAST_PLAYLIST_FILE; then
  140. echo "$file" >> $ICECAST_PLAYLIST_FILE
  141. fi
  142. done
  143. echo $'Adding ogv files to playlist'
  144. find "$files_dir" -type f -name '*.ogv' -print0 | while read -r -d $'\0' file; do
  145. if ! grep -q "$file" $ICECAST_PLAYLIST_FILE; then
  146. echo "$file" >> $ICECAST_PLAYLIST_FILE
  147. fi
  148. done
  149. chown icecast2:icecast $ICECAST_PLAYLIST_FILE
  150. stop_icecast
  151. start_icecast
  152. }
  153. function icecast_import_from_directory {
  154. data=$(mktemp 2>/dev/null)
  155. dialog --title "Choose a directory containing stream files" --dselect "/home/$MY_USERNAME/" 30 60 2> "$data"
  156. selected_dir=$(cat "$data")
  157. rm -f "$data"
  158. if [[ "$selected_dir" == "$ICECAST_DIR" ]]; then
  159. return
  160. fi
  161. if [ ! -d "$selected_dir" ]; then
  162. return
  163. fi
  164. if [[ "$selected_dir" == "/home/$MY_USERNAME/" ]]; then
  165. return
  166. fi
  167. if [[ "$selected_dir" == "/home/$MY_USERNAME/."* ]]; then
  168. return
  169. fi
  170. if [[ "$selected_dir" == *"/Maildir" || "$selected_dir" == *"/Sync" ]]; then
  171. return
  172. fi
  173. dialog --title $"Import stream files directory into Icecast" \
  174. --backtitle $"Freedombone Control Panel" \
  175. --defaultno \
  176. --yesno $"\\nImport the directory:\\n\\n $selected_dir" 12 75
  177. sel=$?
  178. case $sel in
  179. 1) return;;
  180. 255) return;;
  181. esac
  182. if [ ! -d $ICECAST_DIR ]; then
  183. mkdir -p $ICECAST_DIR
  184. fi
  185. dest_dir=$(basename "$selected_dir")
  186. # shellcheck disable=SC2086
  187. mv $selected_dir $ICECAST_DIR
  188. icecast_convert_files "$ICECAST_DIR/$dest_dir"
  189. icecast_add_file_to_playlist "$ICECAST_DIR/$dest_dir"
  190. dialog --title $"Import stream files directory into Icecast" \
  191. --msgbox $"Import success" 6 40
  192. }
  193. function icecast_import_from_usb {
  194. clear
  195. detect_usb_drive
  196. if [ ! -b "$USB_DRIVE" ]; then
  197. dialog --title $"Import stream files from USB drive" --msgbox $'No USB drive found' 6 50
  198. return
  199. fi
  200. backup_mount_drive "${USB_DRIVE}"
  201. if [ ! -d "$USB_MOUNT$ICECAST_DIR" ]; then
  202. dialog --title $"Import stream files from USB drive" --msgbox $'No stream files directory found on USB drive' 6 50
  203. backup_unmount_drive "${USB_DRIVE}"
  204. fi
  205. cp -ru "$USB_MOUNT$ICECAST_DIR/"* $ICECAST_DIR
  206. backup_unmount_drive "${USB_DRIVE}"
  207. icecast_convert_files $ICECAST_DIR
  208. dialog --title $"Import stream files from USB drive" --msgbox $'Import complete. You may now remove the USB drive' 6 50
  209. }
  210. function icecast_export_to_usb {
  211. clear
  212. detect_usb_drive
  213. if [ ! -b "$USB_DRIVE" ]; then
  214. dialog --title $"Export stream files to USB drive" --msgbox $'No USB drive found' 6 50
  215. return
  216. fi
  217. backup_mount_drive "${USB_DRIVE}"
  218. if [ ! -d "$USB_MOUNT$ICECAST_DIR" ]; then
  219. mkdir -p "$USB_MOUNT$ICECAST_DIR"
  220. fi
  221. cp -ru "$ICECAST_DIR/"* "$USB_MOUNT$ICECAST_DIR"
  222. backup_unmount_drive "${USB_DRIVE}"
  223. dialog --title $"Export stream files to USB drive" --msgbox $'Export complete. You may now remove the USB drive' 6 50
  224. }
  225. function icecast_format_drive {
  226. detect_usb_drive
  227. data=$(mktemp 2>/dev/null)
  228. dialog --title $"Format USB drive $USB_DRIVE for stream file storage" \
  229. --backtitle $"Freedombone Control Panel" \
  230. --defaultno \
  231. --yesno $"\\nPlease confirm that you wish to format drive\\n\\n ${USB_DRIVE}\\n\\nAll current data on the drive will be lost, and you will be prompted to give a password used to encrypt the drive.\\n\\nDANGER: If you screw up here and format the wrong drive it's your own fault!" 16 60
  232. sel=$?
  233. case $sel in
  234. 1) rm -f "$data"
  235. return;;
  236. 255) rm -f "$data"
  237. return;;
  238. esac
  239. rm -f "$data"
  240. clear
  241. echo ''
  242. echo $"Formatting drive $USB_DRIVE. ALL CONTENTS WILL BE LOST."
  243. echo ''
  244. "${PROJECT_NAME}-format" "$USB_DRIVE"
  245. dialog --title $"Format USB drive $USB_DRIVE for stream file storage" --msgbox $'Format complete. You may now export stream files or remove the USB drive' 6 50
  246. }
  247. function icecast_edit_playlist {
  248. editor $ICECAST_PLAYLIST_FILE
  249. stop_icecast
  250. start_icecast
  251. }
  252. function icecast_change_login {
  253. read_config_param "$MY_USERNAME"
  254. ICECAST_USER_PASSWORD=$("${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a icecastuser)
  255. data=$(mktemp 2>/dev/null)
  256. dialog --title $"Change Icecast stream visitor login" \
  257. --backtitle $"Freedombone Control Panel" \
  258. --inputbox $"Enter the new login password for stream visitors" 8 60 "$ICECAST_USER_PASSWORD" 2>"$data"
  259. sel=$?
  260. case $sel in
  261. 0) ICECAST_USER_PASSWORD=$(<"$data")
  262. if [[ "$ICECAST_USER_PASSWORD" != *' '* ]]; then
  263. if [ ${#ICECAST_USER_PASSWORD} -gt 8 ]; then
  264. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a icecastuser -p "$ICECAST_USER_PASSWORD"
  265. dialog --title $"Change Icecast stream visitor login" \
  266. --msgbox $"Password changed to $ICECAST_USER_PASSWORD" 6 75
  267. fi
  268. fi
  269. ;;
  270. esac
  271. rm -f "$data"
  272. }
  273. function icecast_enable_login {
  274. dialog --title $"Enable Icecast login" \
  275. --backtitle $"Freedombone Control Panel" \
  276. --defaultno \
  277. --yesno $"\\nDo you want to add a login so that random web users can't access your stream?" 10 60
  278. sel=$?
  279. case $sel in
  280. 0) if grep -q '#auth_basic' /etc/nginx/sites-available/icecast; then
  281. sed -i 's|#auth_basic|auth_basic|g' /etc/nginx/sites-available/icecast
  282. systemctl restart nginx
  283. fi
  284. read_config_param "$MY_USERNAME"
  285. ICECAST_USER_PASSWORD=$("${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a icecastuser)
  286. dialog --title $"Enable Icecast login" \
  287. --msgbox $"Icecast logins are now enabled with the password $ICECAST_USER_PASSWORD" 6 65
  288. ICECAST_USER_PASSWORD=
  289. ;;
  290. 1) if ! grep -q '#auth_basic' /etc/nginx/sites-available/icecast; then
  291. sed -i 's|auth_basic|#auth_basic|g' /etc/nginx/sites-available/icecast
  292. systemctl restart nginx
  293. fi
  294. dialog --title $"Disable Icecast login" \
  295. --msgbox $"Icecast logins are now disabled. Anyone can access your stream." 6 65
  296. ;;
  297. esac
  298. }
  299. function icecast_set_stream_name {
  300. data=$(mktemp 2>/dev/null)
  301. dialog --backtitle $"Freedombone Control Panel" \
  302. --title $"Change Icecast stream details" \
  303. --form "\\n" 8 60 4 \
  304. $"Stream name:" 1 1 "Example stream name" 1 18 40 1000 \
  305. $"Description:" 2 1 "A short description of your stream" 2 18 40 1000 \
  306. $"Genre:" 3 1 "Example genre" 3 18 40 1000 \
  307. 2> "$data"
  308. sel=$?
  309. case $sel in
  310. 1) rm -f "$data"
  311. return;;
  312. 255) rm -f "$data"
  313. return;;
  314. esac
  315. stream_name=$(sed -n 1p < "$data")
  316. stream_description=$(sed -n 2p < "$data")
  317. stream_genre=$(sed -n 3p < "$data")
  318. if [ ${#stream_name} -gt 2 ]; then
  319. sed -i "s|<name>.*|<name>${stream_name}</name>|g" /etc/ices2/ices-playlist.xml
  320. fi
  321. if [ ${#stream_description} -gt 2 ]; then
  322. sed -i "s|<description>.*|<description>${stream_description}</description>|g" /etc/ices2/ices-playlist.xml
  323. fi
  324. if [ ${#stream_genre} -gt 2 ]; then
  325. sed -i "s|<genre>.*|<genre>${stream_genre}</genre>|g" /etc/ices2/ices-playlist.xml
  326. fi
  327. rm -f "$data"
  328. stop_icecast
  329. start_icecast
  330. }
  331. function icecast_set_maximum_streams {
  332. data=$(mktemp 2>/dev/null)
  333. dialog --title $"Set the maximum clients" \
  334. --backtitle $"Freedombone Control Panel" \
  335. --inputbox $"Maximum number of clients" 8 40 "10" 2>"$data"
  336. sel=$?
  337. case $sel in
  338. 0)
  339. max_clients=$(<"$data")
  340. if [ ${#max_clients} -gt 0 ]; then
  341. if [[ "$max_clients" != *' '* ]]; then
  342. # shellcheck disable=SC2076
  343. if [[ "$max_clients" =~ '^[0-9]+$' ]] ; then
  344. sed -i "s|<clients>.*|<clients>${max_clients}</clients>|g" /etc/icecast2/icecast.xml
  345. stop_icecast
  346. start_icecast
  347. dialog --title $"Set the maximum clients" \
  348. --msgbox $"\\nMaximum Icecast clients was set to ${max_clients}" 8 50
  349. fi
  350. fi
  351. fi
  352. ;;
  353. esac
  354. rm -f "$data"
  355. }
  356. function configure_interactive_icecast {
  357. while true
  358. do
  359. data=$(mktemp 2>/dev/null)
  360. dialog --backtitle $"Freedombone Control Panel" \
  361. --title $"Icecast" \
  362. --radiolist $"Choose an operation:" 19 70 12 \
  363. 1 $"Import stream files from directory" off \
  364. 2 $"Import stream files from USB drive" off \
  365. 3 $"Format a USB drive for stream file storage" off \
  366. 4 $"Export stream files to USB drive" off \
  367. 5 $"Manually edit playlist" off \
  368. 6 $"Enable login for stream visitors" off \
  369. 7 $"Change password for stream visitors" off \
  370. 8 $"Re-scan playlist" off \
  371. 9 $"Restart stream" off \
  372. 10 $"Set Stream Name/Description/Genre" off \
  373. 11 $"Set maximum number of clients/streams" off \
  374. 12 $"Exit" on 2> "$data"
  375. sel=$?
  376. case $sel in
  377. 1) rm -f "$data"
  378. break;;
  379. 255) rm -f "$data"
  380. break;;
  381. esac
  382. case $(cat "$data") in
  383. 1) icecast_import_from_directory;;
  384. 2) icecast_import_from_usb;;
  385. 3) icecast_format_drive;;
  386. 4) icecast_export_to_usb;;
  387. 5) icecast_edit_playlist;;
  388. 6) icecast_enable_login;;
  389. 7) icecast_change_login;;
  390. 8) clear
  391. echo $'Rescanning Icecast playlist'
  392. icecast_rescan;;
  393. 9) clear
  394. echo $'Restarting Icecast stream'
  395. stop_icecast
  396. start_icecast;;
  397. 10) icecast_set_stream_name;;
  398. 11) icecast_set_maximum_streams;;
  399. 12) rm -f "$data"
  400. break;;
  401. esac
  402. rm -f "$data"
  403. done
  404. }
  405. function upgrade_icecast {
  406. icecast_update_daemon
  407. }
  408. function backup_local_icecast {
  409. if [ ! -d $ICECAST_DIR ]; then
  410. return
  411. fi
  412. stop_icecast
  413. cp /etc/nginx/.icepasswd $ICECAST_DIR
  414. cp /etc/ices2/ices-playlist.xml $ICECAST_DIR
  415. function_check backup_directory_to_usb
  416. backup_directory_to_usb $ICECAST_DIR icecast
  417. rm $ICECAST_DIR/.icepasswd
  418. start_icecast
  419. }
  420. function restore_local_icecast {
  421. if [ ! -d $ICECAST_DIR ]; then
  422. return
  423. fi
  424. stop_icecast
  425. temp_restore_dir=/root/tempicecast
  426. function_check restore_directory_from_usb
  427. restore_directory_from_usb $temp_restore_dir icecast
  428. if [ -d $temp_restore_dir$ICECAST_DIR ]; then
  429. cp -r $temp_restore_dir$ICECAST_DIR $ICECAST_DIR/
  430. else
  431. cp -r $temp_restore_dir/* $ICECAST_DIR/*
  432. fi
  433. cp $ICECAST_DIR/.icepasswd /etc/nginx/.icepasswd
  434. rm $ICECAST_DIR/.icepasswd
  435. cp $ICECAST_DIR/ices-playlist.xml /etc/ices2
  436. rm $ICECAST_DIR/ices-playlist.xml
  437. chown -R icecast2:icecast $ICECAST_DIR
  438. chown -R icecast2:icecast /etc/ices2
  439. start_icecast
  440. rm -rf $temp_restore_dir
  441. }
  442. function backup_remote_icecast {
  443. if [ ! -d $ICECAST_DIR ]; then
  444. return
  445. fi
  446. stop_icecast
  447. cp /etc/nginx/.icepasswd $ICECAST_DIR
  448. cp /etc/ices2/ices-playlist.xml $ICECAST_DIR
  449. function_check backup_directory_to_friend
  450. backup_directory_to_friend $ICECAST_DIR icecast
  451. rm $ICECAST_DIR/.icepasswd
  452. start_icecast
  453. }
  454. function restore_remote_icecast {
  455. if [ ! -d $ICECAST_DIR ]; then
  456. return
  457. fi
  458. stop_icecast
  459. temp_restore_dir=/root/tempicecast
  460. function_check restore_directory_from_friend
  461. restore_directory_from_friend $temp_restore_dir icecast
  462. if [ -d $temp_restore_dir$ICECAST_DIR ]; then
  463. cp -r $temp_restore_dir$ICECAST_DIR $ICECAST_DIR/
  464. else
  465. cp -r $temp_restore_dir/* $ICECAST_DIR/*
  466. fi
  467. cp $ICECAST_DIR/.icepasswd /etc/nginx/.icepasswd
  468. rm $ICECAST_DIR/.icepasswd
  469. cp $ICECAST_DIR/ices-playlist.xml /etc/ices2
  470. rm $ICECAST_DIR/ices-playlist.xml
  471. chown -R icecast2:icecast $ICECAST_DIR
  472. start_icecast
  473. rm -rf $temp_restore_dir
  474. }
  475. function remove_icecast {
  476. nginx_dissite icecast
  477. sed -i '/start_icecast/d' /etc/crontab
  478. stop_icecast
  479. systemctl disable icecast2
  480. rm /etc/systemd/system/icecast2.service
  481. rm /usr/bin/start_icecast
  482. rm /usr/bin/stop_icecast
  483. if [ -f /etc/nginx/sites-available/icecast ]; then
  484. rm /etc/nginx/sites-available/icecast
  485. fi
  486. if [ -d /var/www/icecast ]; then
  487. rm -rf /var/www/icecast
  488. fi
  489. apt-get -yq remove --purge icecast2
  490. if [ -d /etc/icecast2 ]; then
  491. rm -rf /etc/icecast2
  492. fi
  493. if [ -d /etc/ices2 ]; then
  494. rm -rf /etc/ices2
  495. fi
  496. function_check remove_onion_service
  497. remove_onion_service icecast ${ICECAST_ONION_PORT}
  498. sed -i '/icecast/d' "$COMPLETION_FILE"
  499. }
  500. function install_icecast {
  501. apt-get -yq install software-properties-common debconf-utils
  502. apt-get -yq update
  503. debconf-set-selections <<< "icecast2 icecast2/icecast-setup boolean false"
  504. apt-get -yq install icecast2
  505. apt-get -yq install ices2 ffmpeg apache2-utils
  506. if [ ! -f /etc/icecast2/icecast.xml ]; then
  507. echo $'Icecast not installed'
  508. exit 7923528
  509. fi
  510. if [ ! "${ICECAST_PASSWORD}" ]; then
  511. if [ -f "${IMAGE_PASSWORD_FILE}" ]; then
  512. ICECAST_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  513. else
  514. ICECAST_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  515. fi
  516. fi
  517. ICECAST_ONION_HOSTNAME=$(add_onion_service icecast 80 ${ICECAST_ONION_PORT})
  518. sed -i -e "s|<source-password>[^<]*</source-password>|<source-password>$ICECAST_PASSWORD</source-password>|" \
  519. -e "s|<relay-password>[^<]*</relay-password>|<relay-password>$ICECAST_PASSWORD</relay-password>|" \
  520. -e "s|<admin-password>[^<]*</admin-password>|<admin-password>$ICECAST_PASSWORD</admin-password>|" \
  521. -e "s|<hostname>[^<]*</hostname>|<hostname>$ICECAST_ONION_HOSTNAME</hostname>|" \
  522. /etc/icecast2/icecast.xml
  523. sed -i "s|<clients>.*|<clients>10</clients>|g" /etc/icecast2/icecast.xml
  524. sed -i "s|<port>.*|<port>$ICECAST_PORT</port>|g" /etc/icecast2/icecast.xml
  525. sed -i "s|<admin-user>.*|<admin-user>$MY_USERNAME</admin-user>|g" /etc/icecast2/icecast.xml
  526. sed -i "s|<admin>.*|<admin>$MY_EMAIL_ADDRESS</admin>|g" /etc/icecast2/icecast.xml
  527. sed -i "s|<location>.*|<location>The Interwebs</location>|g" /etc/icecast2/icecast.xml
  528. #sed -i 's|<!-- <bind-address>.*|<bind-address>127.0.0.1</bind-address>|g' /etc/icecast2/icecast.xml
  529. if [ ! -d /var/www/icecast/htdocs ]; then
  530. mkdir -p /var/www/icecast/htdocs
  531. fi
  532. icecast_nginx_site=/etc/nginx/sites-available/icecast
  533. { echo 'server {';
  534. echo " listen 127.0.0.1:$ICECAST_ONION_PORT default_server;";
  535. echo " server_name $ICECAST_ONION_HOSTNAME;";
  536. echo '';
  537. echo ' # Logs';
  538. echo ' access_log /dev/null;';
  539. echo ' error_log /dev/null;';
  540. echo '';
  541. echo ' location / {'; } > $icecast_nginx_site
  542. function_check nginx_limits
  543. nginx_limits "$ICECAST_ONION_HOSTNAME" '15m'
  544. { echo " proxy_pass http://localhost:$ICECAST_PORT;";
  545. echo " #auth_basic \"${ICECAST_LOGIN_TEXT}\";";
  546. echo ' #auth_basic_user_file /etc/nginx/.icepasswd;';
  547. echo ' }';
  548. echo '}'; } >> $icecast_nginx_site
  549. if [ ! -d /var/log/ices ]; then
  550. mkdir -p /var/log/ices
  551. fi
  552. if [ ! -d /etc/ices2 ]; then
  553. mkdir -p /etc/ices2
  554. fi
  555. { echo '<?xml version="1.0"?>';
  556. echo '<ices>';
  557. echo ' <!-- run in background -->';
  558. echo ' <background>1</background>';
  559. echo ' <!-- where logs, etc go. -->';
  560. echo ' <logpath>/var/log/ices</logpath>';
  561. echo ' <logfile>ices.log</logfile>';
  562. echo ' <!-- 1=error,2=warn,3=info,4=debug -->';
  563. echo ' <loglevel>1</loglevel>';
  564. echo ' <!-- set this to 1 to log to the console instead of to the file above -->';
  565. echo ' <consolelog>0</consolelog>';
  566. echo '';
  567. echo ' <!-- optional filename to write process id to -->';
  568. echo ' <!-- <pidfile>/home/ices/ices.pid</pidfile> -->';
  569. echo '';
  570. echo ' <stream>';
  571. echo ' <!-- metadata used for stream listing (not currently used) -->';
  572. echo ' <metadata>';
  573. echo ' <name>Example stream name</name>';
  574. echo ' <genre>Example genre</genre>';
  575. echo ' <description>A short description of your stream</description>';
  576. echo ' </metadata>';
  577. echo '';
  578. echo ' <!-- input module';
  579. echo '';
  580. echo ' The module used here is the playlist module - it has ';
  581. echo ' "submodules" for different types of playlist. There are';
  582. echo ' two currently implemented, "basic", which is a simple';
  583. echo ' file-based playlist, and "script" which invokes a command';
  584. echo ' to returns a filename to start playing. -->';
  585. echo '';
  586. echo ' <input>';
  587. echo ' <module>playlist</module>';
  588. echo ' <param name="type">basic</param>';
  589. echo " <param name=\"file\">$ICECAST_PLAYLIST_FILE</param>";
  590. echo ' <!-- random play -->';
  591. echo ' <param name="random">0</param>';
  592. echo ' <!-- if the playlist get updated that start at the beginning -->';
  593. echo ' <param name="restart-after-reread">0</param>';
  594. echo ' <!-- if set to 1 , plays once through, then exits. -->';
  595. echo ' <param name="once">0</param>';
  596. echo ' </input>';
  597. echo '';
  598. echo ' <!-- Stream instance';
  599. echo ' You may have one or more instances here. This allows you to ';
  600. echo ' send the same input data to one or more servers (or to different';
  601. echo ' mountpoints on the same server). Each of them can have different';
  602. echo ' parameters. This is primarily useful for a) relaying to multiple';
  603. echo ' independent servers, and b) encoding/reencoding to multiple';
  604. echo ' bitrates.';
  605. echo ' If one instance fails (for example, the associated server goes';
  606. echo ' down, etc), the others will continue to function correctly.';
  607. echo ' This example defines two instances as two mountpoints on the';
  608. echo ' same server. -->';
  609. echo ' <instance>';
  610. echo ' <!-- Server details:';
  611. echo ' You define hostname and port for the server here, along with';
  612. echo ' the source password and mountpoint. -->';
  613. echo ' <hostname>localhost</hostname>';
  614. echo " <port>$ICECAST_PORT</port>";
  615. echo " <password>$ICECAST_PASSWORD</password>";
  616. echo ' <mount>/example1.ogg</mount>';
  617. echo ' <!-- Reconnect parameters:';
  618. echo ' When something goes wrong (e.g. the server crashes, or the';
  619. echo ' network drops) and ices disconnects from the server, these';
  620. echo ' control how often it tries to reconnect, and how many times';
  621. echo ' it tries to reconnect. Delay is in seconds.';
  622. echo ' If you set reconnectattempts to -1, it will continue ';
  623. echo ' indefinately. Suggest setting reconnectdelay to a large value';
  624. echo ' if you do this.';
  625. echo ' -->';
  626. echo ' <reconnectdelay>2</reconnectdelay>';
  627. echo ' <reconnectattempts>5</reconnectattempts> ';
  628. echo '';
  629. echo ' <!-- maxqueuelength:';
  630. echo ' This describes how long the internal data queues may be. This';
  631. echo ' basically lets you control how much data gets buffered before';
  632. echo ' ices decides it cant send to the server fast enough, and ';
  633. echo ' either shuts down or flushes the queue (dropping the data)';
  634. echo ' and continues. ';
  635. echo ' For advanced users only.';
  636. echo ' -->';
  637. echo ' <maxqueuelength>80</maxqueuelength>';
  638. echo '';
  639. echo ' <!-- Live encoding/reencoding:';
  640. echo ' Currrently, the parameters given here for encoding MUST';
  641. echo ' match the input data for channels and sample rate. That ';
  642. echo ' restriction will be relaxed in the future.';
  643. echo ' Remove this section if you dont want your files getting reencoded.';
  644. echo ' -->';
  645. echo ' <encode> ';
  646. echo ' <nominal-bitrate>64000</nominal-bitrate>';
  647. echo ' <samplerate>22050</samplerate>';
  648. echo ' <channels>1</channels>';
  649. echo ' </encode>';
  650. echo ' </instance>';
  651. echo '';
  652. echo ' </stream>';
  653. echo '</ices>'; } > /etc/ices2/ices-playlist.xml
  654. sed -i 's|ENABLE=.*|ENABLE=true|g' /etc/default/icecast2
  655. if [ ! -d $ICECAST_DIR ]; then
  656. mkdir $ICECAST_DIR
  657. fi
  658. chown -R icecast2:icecast $ICECAST_DIR
  659. # create a password for users
  660. ICECAST_USER_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  661. if grep -q "$MY_USERNAME:" /etc/nginx/.icepasswd; then
  662. sed -i "/$MY_USERNAME:/d" /etc/nginx/.icepasswd
  663. fi
  664. echo "$ICECAST_USER_PASSWORD" | htpasswd -i -s -c /etc/nginx/.icepasswd "$MY_USERNAME"
  665. if [ ! -f /etc/nginx/.icepasswd ]; then
  666. echo $'/etc/nginx/.icepasswd not found'
  667. exit 73528235
  668. fi
  669. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a icecast -p "$ICECAST_PASSWORD"
  670. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a icecastuser -p "$ICECAST_USER_PASSWORD"
  671. groupadd icecast
  672. useradd -c "Icecast system account" -d /etc/icecast2 -m -r -g icecast icecast2
  673. icecast_update_daemon
  674. nginx_ensite icecast
  675. systemctl restart nginx
  676. icecast_rescan
  677. start_icecast
  678. APP_INSTALLED=1
  679. }
  680. function install_interactive_icecast {
  681. install_icecast
  682. }
  683. # NOTE: deliberately no exit 0