freedombone-app-icecast 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. mv "$selected_dir" $ICECAST_DIR
  187. icecast_convert_files "$ICECAST_DIR/$dest_dir"
  188. icecast_add_file_to_playlist "$ICECAST_DIR/$dest_dir"
  189. dialog --title $"Import stream files directory into Icecast" \
  190. --msgbox $"Import success" 6 40
  191. }
  192. function icecast_import_from_usb {
  193. clear
  194. detect_usb_drive
  195. if [ ! -b "$USB_DRIVE" ]; then
  196. dialog --title $"Import stream files from USB drive" --msgbox $'No USB drive found' 6 50
  197. return
  198. fi
  199. backup_mount_drive "${USB_DRIVE}"
  200. if [ ! -d "$USB_MOUNT$ICECAST_DIR" ]; then
  201. dialog --title $"Import stream files from USB drive" --msgbox $'No stream files directory found on USB drive' 6 50
  202. backup_unmount_drive "${USB_DRIVE}"
  203. fi
  204. cp -ru "$USB_MOUNT$ICECAST_DIR/*" $ICECAST_DIR
  205. backup_unmount_drive "${USB_DRIVE}"
  206. icecast_convert_files $ICECAST_DIR
  207. dialog --title $"Import stream files from USB drive" --msgbox $'Import complete. You may now remove the USB drive' 6 50
  208. }
  209. function icecast_export_to_usb {
  210. clear
  211. detect_usb_drive
  212. if [ ! -b "$USB_DRIVE" ]; then
  213. dialog --title $"Export stream files to USB drive" --msgbox $'No USB drive found' 6 50
  214. return
  215. fi
  216. backup_mount_drive "${USB_DRIVE}"
  217. if [ ! -d "$USB_MOUNT$ICECAST_DIR" ]; then
  218. mkdir -p "$USB_MOUNT$ICECAST_DIR"
  219. fi
  220. cp -ru "$ICECAST_DIR/*" "$USB_MOUNT$ICECAST_DIR"
  221. backup_unmount_drive "${USB_DRIVE}"
  222. dialog --title $"Export stream files to USB drive" --msgbox $'Export complete. You may now remove the USB drive' 6 50
  223. }
  224. function icecast_format_drive {
  225. detect_usb_drive
  226. data=$(mktemp 2>/dev/null)
  227. dialog --title $"Format USB drive $USB_DRIVE for stream file storage" \
  228. --backtitle $"Freedombone Control Panel" \
  229. --defaultno \
  230. --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
  231. sel=$?
  232. case $sel in
  233. 1) rm -f "$data"
  234. return;;
  235. 255) rm -f "$data"
  236. return;;
  237. esac
  238. rm -f "$data"
  239. clear
  240. echo ''
  241. echo $"Formatting drive $USB_DRIVE. ALL CONTENTS WILL BE LOST."
  242. echo ''
  243. "${PROJECT_NAME}-format" "$USB_DRIVE"
  244. 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
  245. }
  246. function icecast_edit_playlist {
  247. editor $ICECAST_PLAYLIST_FILE
  248. stop_icecast
  249. start_icecast
  250. }
  251. function icecast_change_login {
  252. read_config_param "$MY_USERNAME"
  253. ICECAST_USER_PASSWORD=$("${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a icecastuser)
  254. data=$(mktemp 2>/dev/null)
  255. dialog --title $"Change Icecast stream visitor login" \
  256. --backtitle $"Freedombone Control Panel" \
  257. --inputbox $"Enter the new login password for stream visitors" 8 60 "$ICECAST_USER_PASSWORD" 2>"$data"
  258. sel=$?
  259. case $sel in
  260. 0) ICECAST_USER_PASSWORD=$(<"$data")
  261. if [[ "$ICECAST_USER_PASSWORD" != *' '* ]]; then
  262. if [ ${#ICECAST_USER_PASSWORD} -gt 8 ]; then
  263. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a icecastuser -p "$ICECAST_USER_PASSWORD"
  264. dialog --title $"Change Icecast stream visitor login" \
  265. --msgbox $"Password changed to $ICECAST_USER_PASSWORD" 6 75
  266. fi
  267. fi
  268. ;;
  269. esac
  270. rm -f "$data"
  271. }
  272. function icecast_enable_login {
  273. dialog --title $"Enable Icecast login" \
  274. --backtitle $"Freedombone Control Panel" \
  275. --defaultno \
  276. --yesno $"\\nDo you want to add a login so that random web users can't access your stream?" 10 60
  277. sel=$?
  278. case $sel in
  279. 0) if grep -q '#auth_basic' /etc/nginx/sites-available/icecast; then
  280. sed -i 's|#auth_basic|auth_basic|g' /etc/nginx/sites-available/icecast
  281. systemctl restart nginx
  282. fi
  283. read_config_param "$MY_USERNAME"
  284. ICECAST_USER_PASSWORD=$("${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a icecastuser)
  285. dialog --title $"Enable Icecast login" \
  286. --msgbox $"Icecast logins are now enabled with the password $ICECAST_USER_PASSWORD" 6 65
  287. ICECAST_USER_PASSWORD=
  288. ;;
  289. 1) if ! grep -q '#auth_basic' /etc/nginx/sites-available/icecast; then
  290. sed -i 's|auth_basic|#auth_basic|g' /etc/nginx/sites-available/icecast
  291. systemctl restart nginx
  292. fi
  293. dialog --title $"Disable Icecast login" \
  294. --msgbox $"Icecast logins are now disabled. Anyone can access your stream." 6 65
  295. ;;
  296. esac
  297. }
  298. function icecast_set_stream_name {
  299. data=$(mktemp 2>/dev/null)
  300. dialog --backtitle $"Freedombone Control Panel" \
  301. --title $"Change Icecast stream details" \
  302. --form "\\n" 8 60 4 \
  303. $"Stream name:" 1 1 "Example stream name" 1 18 40 1000 \
  304. $"Description:" 2 1 "A short description of your stream" 2 18 40 1000 \
  305. $"Genre:" 3 1 "Example genre" 3 18 40 1000 \
  306. 2> "$data"
  307. sel=$?
  308. case $sel in
  309. 1) rm -f "$data"
  310. return;;
  311. 255) rm -f "$data"
  312. return;;
  313. esac
  314. stream_name=$(sed -n 1p < "$data")
  315. stream_description=$(sed -n 2p < "$data")
  316. stream_genre=$(sed -n 3p < "$data")
  317. if [ ${#stream_name} -gt 2 ]; then
  318. sed -i "s|<name>.*|<name>${stream_name}</name>|g" /etc/ices2/ices-playlist.xml
  319. fi
  320. if [ ${#stream_description} -gt 2 ]; then
  321. sed -i "s|<description>.*|<description>${stream_description}</description>|g" /etc/ices2/ices-playlist.xml
  322. fi
  323. if [ ${#stream_genre} -gt 2 ]; then
  324. sed -i "s|<genre>.*|<genre>${stream_genre}</genre>|g" /etc/ices2/ices-playlist.xml
  325. fi
  326. rm -f "$data"
  327. stop_icecast
  328. start_icecast
  329. }
  330. function icecast_set_maximum_streams {
  331. data=$(mktemp 2>/dev/null)
  332. dialog --title $"Set the maximum clients" \
  333. --backtitle $"Freedombone Control Panel" \
  334. --inputbox $"Maximum number of clients" 8 40 "10" 2>"$data"
  335. sel=$?
  336. case $sel in
  337. 0)
  338. max_clients=$(<"$data")
  339. if [ ${#max_clients} -gt 0 ]; then
  340. if [[ "$max_clients" != *' '* ]]; then
  341. # shellcheck disable=SC2076
  342. if [[ "$max_clients" =~ '^[0-9]+$' ]] ; then
  343. sed -i "s|<clients>.*|<clients>${max_clients}</clients>|g" /etc/icecast2/icecast.xml
  344. stop_icecast
  345. start_icecast
  346. dialog --title $"Set the maximum clients" \
  347. --msgbox $"\\nMaximum Icecast clients was set to ${max_clients}" 8 50
  348. fi
  349. fi
  350. fi
  351. ;;
  352. esac
  353. rm -f "$data"
  354. }
  355. function configure_interactive_icecast {
  356. while true
  357. do
  358. data=$(mktemp 2>/dev/null)
  359. dialog --backtitle $"Freedombone Control Panel" \
  360. --title $"Icecast" \
  361. --radiolist $"Choose an operation:" 19 70 12 \
  362. 1 $"Import stream files from directory" off \
  363. 2 $"Import stream files from USB drive" off \
  364. 3 $"Format a USB drive for stream file storage" off \
  365. 4 $"Export stream files to USB drive" off \
  366. 5 $"Manually edit playlist" off \
  367. 6 $"Enable login for stream visitors" off \
  368. 7 $"Change password for stream visitors" off \
  369. 8 $"Re-scan playlist" off \
  370. 9 $"Restart stream" off \
  371. 10 $"Set Stream Name/Description/Genre" off \
  372. 11 $"Set maximum number of clients/streams" off \
  373. 12 $"Exit" on 2> "$data"
  374. sel=$?
  375. case $sel in
  376. 1) rm -f "$data"
  377. break;;
  378. 255) rm -f "$data"
  379. break;;
  380. esac
  381. case $(cat "$data") in
  382. 1) icecast_import_from_directory;;
  383. 2) icecast_import_from_usb;;
  384. 3) icecast_format_drive;;
  385. 4) icecast_export_to_usb;;
  386. 5) icecast_edit_playlist;;
  387. 6) icecast_enable_login;;
  388. 7) icecast_change_login;;
  389. 8) clear
  390. echo $'Rescanning Icecast playlist'
  391. icecast_rescan;;
  392. 9) clear
  393. echo $'Restarting Icecast stream'
  394. stop_icecast
  395. start_icecast;;
  396. 10) icecast_set_stream_name;;
  397. 11) icecast_set_maximum_streams;;
  398. 12) rm -f "$data"
  399. break;;
  400. esac
  401. rm -f "$data"
  402. done
  403. }
  404. function upgrade_icecast {
  405. icecast_update_daemon
  406. }
  407. function backup_local_icecast {
  408. if [ ! -d $ICECAST_DIR ]; then
  409. return
  410. fi
  411. stop_icecast
  412. cp /etc/nginx/.icepasswd $ICECAST_DIR
  413. cp /etc/ices2/ices-playlist.xml $ICECAST_DIR
  414. function_check backup_directory_to_usb
  415. backup_directory_to_usb $ICECAST_DIR icecast
  416. rm $ICECAST_DIR/.icepasswd
  417. start_icecast
  418. }
  419. function restore_local_icecast {
  420. if [ ! -d $ICECAST_DIR ]; then
  421. return
  422. fi
  423. stop_icecast
  424. temp_restore_dir=/root/tempicecast
  425. function_check restore_directory_from_usb
  426. restore_directory_from_usb $temp_restore_dir icecast
  427. if [ -d $temp_restore_dir$ICECAST_DIR ]; then
  428. cp -r $temp_restore_dir$ICECAST_DIR $ICECAST_DIR/
  429. else
  430. cp -r $temp_restore_dir/* $ICECAST_DIR/*
  431. fi
  432. cp $ICECAST_DIR/.icepasswd /etc/nginx/.icepasswd
  433. rm $ICECAST_DIR/.icepasswd
  434. cp $ICECAST_DIR/ices-playlist.xml /etc/ices2
  435. rm $ICECAST_DIR/ices-playlist.xml
  436. chown -R icecast2:icecast $ICECAST_DIR
  437. chown -R icecast2:icecast /etc/ices2
  438. start_icecast
  439. rm -rf $temp_restore_dir
  440. }
  441. function backup_remote_icecast {
  442. if [ ! -d $ICECAST_DIR ]; then
  443. return
  444. fi
  445. stop_icecast
  446. cp /etc/nginx/.icepasswd $ICECAST_DIR
  447. cp /etc/ices2/ices-playlist.xml $ICECAST_DIR
  448. function_check backup_directory_to_friend
  449. backup_directory_to_friend $ICECAST_DIR icecast
  450. rm $ICECAST_DIR/.icepasswd
  451. start_icecast
  452. }
  453. function restore_remote_icecast {
  454. if [ ! -d $ICECAST_DIR ]; then
  455. return
  456. fi
  457. stop_icecast
  458. temp_restore_dir=/root/tempicecast
  459. function_check restore_directory_from_friend
  460. restore_directory_from_friend $temp_restore_dir icecast
  461. if [ -d $temp_restore_dir$ICECAST_DIR ]; then
  462. cp -r $temp_restore_dir$ICECAST_DIR $ICECAST_DIR/
  463. else
  464. cp -r $temp_restore_dir/* $ICECAST_DIR/*
  465. fi
  466. cp $ICECAST_DIR/.icepasswd /etc/nginx/.icepasswd
  467. rm $ICECAST_DIR/.icepasswd
  468. cp $ICECAST_DIR/ices-playlist.xml /etc/ices2
  469. rm $ICECAST_DIR/ices-playlist.xml
  470. chown -R icecast2:icecast $ICECAST_DIR
  471. start_icecast
  472. rm -rf $temp_restore_dir
  473. }
  474. function remove_icecast {
  475. nginx_dissite icecast
  476. sed -i '/start_icecast/d' /etc/crontab
  477. stop_icecast
  478. systemctl disable icecast2
  479. rm /etc/systemd/system/icecast2.service
  480. rm /usr/bin/start_icecast
  481. rm /usr/bin/stop_icecast
  482. if [ -f /etc/nginx/sites-available/icecast ]; then
  483. rm /etc/nginx/sites-available/icecast
  484. fi
  485. if [ -d /var/www/icecast ]; then
  486. rm -rf /var/www/icecast
  487. fi
  488. apt-get -yq remove --purge icecast2
  489. if [ -d /etc/icecast2 ]; then
  490. rm -rf /etc/icecast2
  491. fi
  492. if [ -d /etc/ices2 ]; then
  493. rm -rf /etc/ices2
  494. fi
  495. function_check remove_onion_service
  496. remove_onion_service icecast ${ICECAST_ONION_PORT}
  497. sed -i '/icecast/d' "$COMPLETION_FILE"
  498. }
  499. function install_icecast {
  500. apt-get -yq install software-properties-common debconf-utils
  501. apt-get -yq update
  502. debconf-set-selections <<< "icecast2 icecast2/icecast-setup boolean false"
  503. apt-get -yq install icecast2
  504. apt-get -yq install ices2 ffmpeg apache2-utils
  505. if [ ! -f /etc/icecast2/icecast.xml ]; then
  506. echo $'Icecast not installed'
  507. exit 7923528
  508. fi
  509. if [ ! "${ICECAST_PASSWORD}" ]; then
  510. if [ -f "${IMAGE_PASSWORD_FILE}" ]; then
  511. ICECAST_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
  512. else
  513. ICECAST_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  514. fi
  515. fi
  516. ICECAST_ONION_HOSTNAME=$(add_onion_service icecast 80 ${ICECAST_ONION_PORT})
  517. sed -i -e "s|<source-password>[^<]*</source-password>|<source-password>$ICECAST_PASSWORD</source-password>|" \
  518. -e "s|<relay-password>[^<]*</relay-password>|<relay-password>$ICECAST_PASSWORD</relay-password>|" \
  519. -e "s|<admin-password>[^<]*</admin-password>|<admin-password>$ICECAST_PASSWORD</admin-password>|" \
  520. -e "s|<hostname>[^<]*</hostname>|<hostname>$ICECAST_ONION_HOSTNAME</hostname>|" \
  521. /etc/icecast2/icecast.xml
  522. sed -i "s|<clients>.*|<clients>10</clients>|g" /etc/icecast2/icecast.xml
  523. sed -i "s|<port>.*|<port>$ICECAST_PORT</port>|g" /etc/icecast2/icecast.xml
  524. sed -i "s|<admin-user>.*|<admin-user>$MY_USERNAME</admin-user>|g" /etc/icecast2/icecast.xml
  525. sed -i "s|<admin>.*|<admin>$MY_EMAIL_ADDRESS</admin>|g" /etc/icecast2/icecast.xml
  526. sed -i "s|<location>.*|<location>The Interwebs</location>|g" /etc/icecast2/icecast.xml
  527. #sed -i 's|<!-- <bind-address>.*|<bind-address>127.0.0.1</bind-address>|g' /etc/icecast2/icecast.xml
  528. if [ ! -d /var/www/icecast/htdocs ]; then
  529. mkdir -p /var/www/icecast/htdocs
  530. fi
  531. icecast_nginx_site=/etc/nginx/sites-available/icecast
  532. { echo 'server {';
  533. echo " listen 127.0.0.1:$ICECAST_ONION_PORT default_server;";
  534. echo " server_name $ICECAST_ONION_HOSTNAME;";
  535. echo '';
  536. echo ' # Logs';
  537. echo ' access_log /dev/null;';
  538. echo ' error_log /dev/null;';
  539. echo '';
  540. echo ' location / {'; } > $icecast_nginx_site
  541. function_check nginx_limits
  542. nginx_limits "$ICECAST_ONION_HOSTNAME" '15m'
  543. { echo " proxy_pass http://localhost:$ICECAST_PORT;";
  544. echo " #auth_basic \"${ICECAST_LOGIN_TEXT}\";";
  545. echo ' #auth_basic_user_file /etc/nginx/.icepasswd;';
  546. echo ' }';
  547. echo '}'; } >> $icecast_nginx_site
  548. if [ ! -d /var/log/ices ]; then
  549. mkdir -p /var/log/ices
  550. fi
  551. if [ ! -d /etc/ices2 ]; then
  552. mkdir -p /etc/ices2
  553. fi
  554. { echo '<?xml version="1.0"?>';
  555. echo '<ices>';
  556. echo ' <!-- run in background -->';
  557. echo ' <background>1</background>';
  558. echo ' <!-- where logs, etc go. -->';
  559. echo ' <logpath>/var/log/ices</logpath>';
  560. echo ' <logfile>ices.log</logfile>';
  561. echo ' <!-- 1=error,2=warn,3=info,4=debug -->';
  562. echo ' <loglevel>1</loglevel>';
  563. echo ' <!-- set this to 1 to log to the console instead of to the file above -->';
  564. echo ' <consolelog>0</consolelog>';
  565. echo '';
  566. echo ' <!-- optional filename to write process id to -->';
  567. echo ' <!-- <pidfile>/home/ices/ices.pid</pidfile> -->';
  568. echo '';
  569. echo ' <stream>';
  570. echo ' <!-- metadata used for stream listing (not currently used) -->';
  571. echo ' <metadata>';
  572. echo ' <name>Example stream name</name>';
  573. echo ' <genre>Example genre</genre>';
  574. echo ' <description>A short description of your stream</description>';
  575. echo ' </metadata>';
  576. echo '';
  577. echo ' <!-- input module';
  578. echo '';
  579. echo ' The module used here is the playlist module - it has ';
  580. echo ' "submodules" for different types of playlist. There are';
  581. echo ' two currently implemented, "basic", which is a simple';
  582. echo ' file-based playlist, and "script" which invokes a command';
  583. echo ' to returns a filename to start playing. -->';
  584. echo '';
  585. echo ' <input>';
  586. echo ' <module>playlist</module>';
  587. echo ' <param name="type">basic</param>';
  588. echo " <param name=\"file\">$ICECAST_PLAYLIST_FILE</param>";
  589. echo ' <!-- random play -->';
  590. echo ' <param name="random">0</param>';
  591. echo ' <!-- if the playlist get updated that start at the beginning -->';
  592. echo ' <param name="restart-after-reread">0</param>';
  593. echo ' <!-- if set to 1 , plays once through, then exits. -->';
  594. echo ' <param name="once">0</param>';
  595. echo ' </input>';
  596. echo '';
  597. echo ' <!-- Stream instance';
  598. echo ' You may have one or more instances here. This allows you to ';
  599. echo ' send the same input data to one or more servers (or to different';
  600. echo ' mountpoints on the same server). Each of them can have different';
  601. echo ' parameters. This is primarily useful for a) relaying to multiple';
  602. echo ' independent servers, and b) encoding/reencoding to multiple';
  603. echo ' bitrates.';
  604. echo ' If one instance fails (for example, the associated server goes';
  605. echo ' down, etc), the others will continue to function correctly.';
  606. echo ' This example defines two instances as two mountpoints on the';
  607. echo ' same server. -->';
  608. echo ' <instance>';
  609. echo ' <!-- Server details:';
  610. echo ' You define hostname and port for the server here, along with';
  611. echo ' the source password and mountpoint. -->';
  612. echo ' <hostname>localhost</hostname>';
  613. echo " <port>$ICECAST_PORT</port>";
  614. echo " <password>$ICECAST_PASSWORD</password>";
  615. echo ' <mount>/example1.ogg</mount>';
  616. echo ' <!-- Reconnect parameters:';
  617. echo ' When something goes wrong (e.g. the server crashes, or the';
  618. echo ' network drops) and ices disconnects from the server, these';
  619. echo ' control how often it tries to reconnect, and how many times';
  620. echo ' it tries to reconnect. Delay is in seconds.';
  621. echo ' If you set reconnectattempts to -1, it will continue ';
  622. echo ' indefinately. Suggest setting reconnectdelay to a large value';
  623. echo ' if you do this.';
  624. echo ' -->';
  625. echo ' <reconnectdelay>2</reconnectdelay>';
  626. echo ' <reconnectattempts>5</reconnectattempts> ';
  627. echo '';
  628. echo ' <!-- maxqueuelength:';
  629. echo ' This describes how long the internal data queues may be. This';
  630. echo ' basically lets you control how much data gets buffered before';
  631. echo ' ices decides it cant send to the server fast enough, and ';
  632. echo ' either shuts down or flushes the queue (dropping the data)';
  633. echo ' and continues. ';
  634. echo ' For advanced users only.';
  635. echo ' -->';
  636. echo ' <maxqueuelength>80</maxqueuelength>';
  637. echo '';
  638. echo ' <!-- Live encoding/reencoding:';
  639. echo ' Currrently, the parameters given here for encoding MUST';
  640. echo ' match the input data for channels and sample rate. That ';
  641. echo ' restriction will be relaxed in the future.';
  642. echo ' Remove this section if you dont want your files getting reencoded.';
  643. echo ' -->';
  644. echo ' <encode> ';
  645. echo ' <nominal-bitrate>64000</nominal-bitrate>';
  646. echo ' <samplerate>22050</samplerate>';
  647. echo ' <channels>1</channels>';
  648. echo ' </encode>';
  649. echo ' </instance>';
  650. echo '';
  651. echo ' </stream>';
  652. echo '</ices>'; } > /etc/ices2/ices-playlist.xml
  653. sed -i 's|ENABLE=.*|ENABLE=true|g' /etc/default/icecast2
  654. if [ ! -d $ICECAST_DIR ]; then
  655. mkdir $ICECAST_DIR
  656. fi
  657. chown -R icecast2:icecast $ICECAST_DIR
  658. # create a password for users
  659. ICECAST_USER_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
  660. if grep -q "$MY_USERNAME:" /etc/nginx/.icepasswd; then
  661. sed -i "/$MY_USERNAME:/d" /etc/nginx/.icepasswd
  662. fi
  663. echo "$ICECAST_USER_PASSWORD" | htpasswd -i -s -c /etc/nginx/.icepasswd "$MY_USERNAME"
  664. if [ ! -f /etc/nginx/.icepasswd ]; then
  665. echo $'/etc/nginx/.icepasswd not found'
  666. exit 73528235
  667. fi
  668. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a icecast -p "$ICECAST_PASSWORD"
  669. "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a icecastuser -p "$ICECAST_USER_PASSWORD"
  670. groupadd icecast
  671. useradd -c "Icecast system account" -d /etc/icecast2 -m -r -g icecast icecast2
  672. icecast_update_daemon
  673. nginx_ensite icecast
  674. systemctl restart nginx
  675. icecast_rescan
  676. start_icecast
  677. APP_INSTALLED=1
  678. }
  679. function install_interactive_icecast {
  680. install_icecast
  681. }
  682. # NOTE: deliberately no exit 0