freedombone-app-icecast 27KB

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