#!/bin/sh

#################################################################################
#																				#
#	Ретранслятор Домашнего ТВ v1.3												#
#	По мотивам Ретранслятора от Андрея Кузнецова aka andy555 (forum.hdtv.ru)	#
#																				#
#	Home TV Repeater v1.3														#
#	Based on Repeater by Andrey Kuznetsov aka andy555 (forum.hdtv.ru)			#
#																				#
#	Автор (Author): Brigadir (forum.hdtv.ru)									#
#	Мои благодарности (My thanks): ddaaff (forum.hdtv.ru)						#
#	Дата (Date): 01-07-2016														#
#	Последнее обновление (Latest update): 14-02-2020							#
#																				#
#################################################################################

if [[ -n $1 ]]; then
	QUERY_STRING=$1
	SCRIPT_NAME="/cgi-bin/repeater"
fi

if (! echo "$QUERY_STRING" | grep -E -q "hls/CH_|(zabava|s\d+).*cdn\.ngenix\.net"); then
	echo "HTTP/1.1 302 Found"
	echo "Content-Type: application/vnd.apple.mpegURL"
	echo -e "Location: $QUERY_STRING\n"
	exit
fi

CURL="$(pwd)/curl"
if [[ ! -e $CURL ]]; then
	if [[ -e "/system/dunehd/init" ]]; then
		CURL="/firmware/bin/curl"
	else
		CURL="/tmp/www/cgi-bin/curl"
	fi
fi

CURL="$CURL --silent --location -k"

export CURL_CA_BUNDLE=/firmware/certs/ca-bundle.crt

echo -e "Content-Type: application/vnd.apple.mpegURL\n"

if (echo "$QUERY_STRING" | grep -E -q '(zabava|rt-.+-htlive|).*\.ngenix\.net'); then
	HTTP_USER_AGENT="com.rostelecom.zabava.v4.ui.player.view.TvPlayerFragment/1.3.3.4 (Linux;Android 4.4.2) ExoPlayerLib/2.8.3"
	if [[ $QUERY_STRING == *.m3u8* ]]; then
		$CURL --url "$QUERY_STRING" --user-agent "$HTTP_USER_AGENT" | awk -v BASE_URL=$(echo ${QUERY_STRING%\/*} | sed "s|^http://\|^https://|http://127.0.0.1$SCRIPT_NAME?&|") -v SCRIPT_URL=$(echo "http://127.0.0.1$SCRIPT_NAME?") '{sub(/^\s*/,"");if(/^\s*[^#EXT|^http]/){if(/^\s*\//){print BASE_URL$0;}else{print BASE_URL"/"$0;}}else{sub(/^http:\/\/|^https:\/\//,SCRIPT_URL"&");print;}}'
	else
		$CURL --url "$QUERY_STRING" --user-agent "$HTTP_USER_AGENT"
	fi
	exit
fi

HTTP_HEADER_L0="Accept: */*"
HTTP_HEADER_L1="x-smartlabs-mac-address: `ifconfig  eth0 | head -1 | sed "s/^.*HWaddr //"`"
HTTP_HEADER_L2="x-smartlabs-session-id: `date +"%s"`"
HTTP_HEADER_L3="x-smartlabs-request-timestamp: 0"
HTTP_HEADER_L4="x-smartlabs-hls-buffer-total-bytes: 0"
HTTP_HEADER_L5="x-smartlabs-hls-buffer-free-bytes: 0"
HTTP_HEADER_L6="x-smartlabs-hls-chunk-duration: 5.57384"
HTTP_USER_AGENT="SmartLabs/1.51652.472 (sml723x; SML-482) SmartSDK/1.5.63-rt-21.1 Qt/4.7.3 API/20121210"

if [[ $QUERY_STRING == *.m3u8* ]]; then
	$CURL --url "$QUERY_STRING" --user-agent "$HTTP_USER_AGENT" --header "$HTTP_HEADER_L0" --header "$HTTP_HEADER_L1" --header "$HTTP_HEADER_L2" --header "$HTTP_HEADER_L3" | sed "s|http://\|https://|http://127.0.0.1$SCRIPT_NAME?&|;s|SM-CSA|NONE|g"
else
	$CURL --url "$QUERY_STRING" --user-agent "$HTTP_USER_AGENT" --header "$HTTP_HEADER_L0" --header "$HTTP_HEADER_L1" --header "$HTTP_HEADER_L2" --header "$HTTP_HEADER_L3" --header "$HTTP_HEADER_L4" --header "$HTTP_HEADER_L5" --header "$HTTP_HEADER_L6"
fi