SMOLNET PORTAL home about changes
#!/bin/sh

#exec 2> /br/gopher/SOURCE.log
#set -x

basedir="/br/radio/relay"
search="$1"
arguments="$2"
host="$3"
port="$4"
traversal="$5"
selector="$6"

# We were requested directly.
case "${selector}" in
"/"*)
	exit 0
	;;
esac

isshoutcast=0
isaudiocast=0
case "${selector}" in
*HTTP/*)
	isshoutcast=1
	httppath="$(printf "%s\n" "${selector}" | cut -d' ' -f 2)"
	;;
*)
	isaudiocast=1
	httppath="$(printf "%s\n" "${selector}" | cut -d' ' -f 3)"
	;;
esac

case "${httppath}" in
/radio/stream/*)
	streampublic=1
	streamname="Test"
	streamdesc="Some test stream"
	streamurl="gopher://bitreich.org/9/radio/listen";
	streamgenre="bit"
	streamct="audio/mpeg"
	streamhost="bitreich.org"
	streamaudioinfo="audio info"
	streambitrate="128"
	streamirc="ircs://irc.bitreich.org/#bitreich-en"
	streamaim="None"
	streamicq="None"
	dumpfile="/dev/null"
	authorization=""
	useragent="hurl/1.0"

	if [ $isaudiocast -eq 1 ];
	then
		password="$(printf "%s\n" "${selector}" | cut -d' ' -f 2)"
	fi

	while read -r header;
	do
		header="$(printf "%s\n" "${header}" | sed -e 's,\r,,g')"
		[ -z "${header}" ] && break
		headerval="$(printf "%s\n" "${header}" \
			| cut -d':' -f2- \
			| xargs)"
		header="$(printf "%s\n" "${header}" \
			| tr '[:upper:]' '[:lower:]')"

		case "$header" in
		authorization*)
			authorization="${headerval}"
			;;
		user-agent:*)
			useragent="${headerval}"
			;;
		content-type:*)
			streamct="${headerval}"
			;;
		ice-public:*|x-audiocast-public:*)
			streampublic="${headerval}"
			;;
		ice-name:*|x-audiocast-name:*|icy-name:*)
			streamname="${headerval}"
			;;
		ice-description:*|x-audiocast-description:*)
			streamdesc="${headerval}"
			;;
		ice-url:*|x-audiocast-url:*|icy-url:*)
			streamurl="${headerval}"
			;;
		ice-genre:*|x-audiocast-genre:*|icy-genre:*)
			streamgenre="${headerval}"
			;;
		ice-audio-info:*)
			streamaudioinfo="${headerval}"
			streambitrate="$(printf "%s\n" "${headerval}" \
				| sed -e 's,.*bitrate=\([0-9]*\).*,\1,g')"
			;;
		x-audiocast-dumpfile:*)
			dumpfile="${headerval}"
			;;
		x-audiocast-bitrate:*|icy-br:*)
			# Audiocast is mp3.
			streamct="audio/mpeg"
			streambitrate="${headerval}"
			;;
		ice-irc:*)
			streamirc="${headerval}"
			;;
		ice-aim:*)
			streamaim="${headerval}"
			;;
		ice-icq*)
			streamicq="${headerval}"
			;;
		*)
			;;
		esac
	done

	stream="$(basename "${httppath}")"
	streamdir="${basedir}/${stream}"
	if [ ! -d "${streamdir}" ];
	then
		printf "HTTP/1.0 404 stream ${stream} not found.\r\n"
		printf "Connection: close\r\n"
		printf "\r\n"
		exit 1
	fi
	cd "${streamdir}"
	if [ -e "${streamdir}/password" ];
	then
		checkpassword="$(cat "${streamdir}/password")"
		if [ $isshoutcast -eq 1 ];
		then
			if [ -z "${authorization}" ];
			then
				printf "HTTP/1.0 401 You need to authenticate.\r\n"
				printf "Connection: close\r\n"
				printf "\r\n"
				exit 1
			fi

			b64auth="$(printf "%s\n" "${authorization}" \
				| sed 's,.*Basic \([^ ]*\).*,\1,')"
			b64decoded="$(printf "%s\n" "${b64auth}" \
				| base64 -d)"
			username="$(printf "%s\n" "${b64decoded}" \
				| cut -d':' -f 1)"
			password="$(printf "%s\n" "${b64decoded}" \
				| cut -d':' -f 2)"
		fi

		if [ "${password}" != "${checkpassword}" ];
		then
			printf "HTTP/1.0 401 Password incorrect.\r\n"
			printf "Connection: close\r\n"
			printf "\r\n"
			exit 1
		fi
	fi

	if [ ! -S "${streamdir}/in" ];
	then
		printf "HTTP/1.0 500 Relay server for stream not running.\r\n"
		printf "Connection: close\r\n"
		printf "\r\n"
		exit 1
	fi

	#set -x
	#exec 2> /br/gopher/SOURCE.log
	printf "HTTP/1.0 200 OK\r\n"
	printf "Server: Icecast 2.5.0\r\n"
	printf "Connection: close\r\n"
	printf "Allow: SOURCE\r\n"
	printf "Date: $(LANG=C date -u "+%a, %e %b %Y %T %Z")\r\n"
	printf "Cache-Control: no-cache\r\n"
	printf "Expires: Mon, 26 Jul 1997 05:00:00 GMT\r\n"
	printf "Pragma: no-cache\r\n"
	printf "Access-Control-Allow-Origin: *\r\n"
	printf "\r\n"

	case "${streamct}" in
	"application/ogg"|"audio/aac")
		ffmpeg -i - -f mp3 2>/dev/null \
			| socat - unix:${streamdir}/in
		;;
	"audio/mpeg")
		ffmpeg -i mp3 -ab "${streambitrate}" -f mp3 2>/dev/null \
			| socat - unix:${streamdir}/in
		;;
	esac
	;;
esac

exit 0
Response: text/plain
Original URLgophers://bitreich.org/0/SOURCE
Content-Typetext/plain; charset=utf-8