#!/bin/sh

#################################################################################
#																				#
#	Home TV Repeater v1.6														#
#																				#
#	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: 27-04-2025													#
#																				#
#################################################################################

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

[ -z $FS_PREFIX ] && FS_PREFIX=""
[ -z $HD_HTTP_LOCAL_PORT ] && HD_HTTP_LOCAL_PORT="80"

CURL="$(pwd)/curl"

if [[ ! -e $CURL ]]; then
	source "$FS_PREFIX/tmp/run/versions.txt"
	if [ $platform_kind == android -o $platform_kind = "0" ]; then
		CURL="$FS_PREFIX/firmware/bin/curl"
	elif (echo $platform_kind | grep -E -q "87.."); then
		CURL="../../bin/curl.87xx"
	elif (echo $platform_kind | grep -E -q "864."); then
		CURL="../../bin/curl.864x"
	elif (echo $platform_kind | grep -E -q "867."); then
		CURL="../../bin/curl.867x"
	elif (echo $platform_kind | grep -E -q "865."); then
		CURL="../../bin/curl.865x"
	fi
fi

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

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

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

if (echo "$QUERY_STRING" | grep -E -q '(zabava|s|rt-).*\.ngenix\.net'); then
	HTTP_USER_AGENT="Mozilla/5.0"
	if [[ $QUERY_STRING == *.m3u8* ]]; then
		$CURL --url "$QUERY_STRING" --user-agent "$HTTP_USER_AGENT" | awk -v BASE_URL=$(echo ${QUERY_STRING%\/*} | sed "s|^http://|http://127.0.0.1:$HD_HTTP_LOCAL_PORT$SCRIPT_NAME?&|" | sed "s|^https://|http://127.0.0.1:$HD_HTTP_LOCAL_PORT$SCRIPT_NAME?&|") -v SCRIPT_URL=$(echo "http://127.0.0.1:$HD_HTTP_LOCAL_PORT$SCRIPT_NAME?") '{sub(/^\s*/,"");if(/^\s*http/){sub(/^http:\/\/|^https:\/\//,SCRIPT_URL"&");}else{if(/^[^#EXT]/){if(/^\s*\//){sub(/^.+/,BASE_URL$0);}else{sub(/^.+/,BASE_URL"/"$0);}}}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="Restream/5.24302.99 (mag250, mag250) SmartSDK/1.5.63-rt-38-22-g950636f Qt/4.7.3 API/0.30.0"

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