freedombone-app-icecast 27KB

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