Skip to content

Instantly share code, notes, and snippets.

@bizonix
Created January 10, 2018 20:12
Show Gist options
  • Save bizonix/9795895cdca018dd7a0b8c2e86a9fa91 to your computer and use it in GitHub Desktop.
Save bizonix/9795895cdca018dd7a0b8c2e86a9fa91 to your computer and use it in GitHub Desktop.
urlencode()
{
local s c
local strlen=${#1}
for (( i=0; i<strlen; i++ )); do
c=${1:$i:1}
case "$c" in
+)
c="%2B"
;;
/)
c="%2F"
;;
=)
c="%3D"
;;
esac
s=$s$c
done
echo -n $s
}
hexstr2bin()
{
local s
local strlen=${#1}
for (( i=0; i<strlen; i+=2 )); do
s=$s"\\x"${1:$i:2}
done
echo -ne "$s"
}
hexstr2base64()
{
hexstr2bin $1 | base64
}
function decrypt_acelive
{
# $1 - filename
local TEMPENC=/tmp/e.bin
[ -f $TEMPENC ] && rm -f $TEMPENC
dd if=$1 of=$TEMPENC skip=1 bs=20 2>/dev/null
openssl aes-128-cbc -d -K a50c4e33a2f48cc50ce275c9ff3a31bf -iv 74e9cdd6391bcbd565f99503313329a3 -in $TEMPENC -out $1.plain 2>/dev/null
[ -f $TEMPENC ] && rm -f $TEMPENC
}
DLAceReqSignature()
{
local s
s="${1//&/#}H!+:H1NnvvX\\x0bS'(;0/A\\nR{\${\\n/3%1\\x0b*[r0o>QzNGKkXT@v\\x0b3DN;gx_66L2 {\`F0,\\tKm>XoG~iY(\\x0bu]6E}\\t~07&H;9qE1d?d-A7S("
read -d " " s <<<$(echo -n $s | sha1sum)
echo $s
}
DLAceGetUri()
{
# $1 - id type : pid (hex),infohash (base64)
# $2 - id
local s ss
ss="_n=3.1.16&_p=win32&_r="$(($RANDOM*32768+$RANDOM))"&_v=3011600&$1="
s="/gettorrent?"$ss$(urlencode "$2")"&_s="$(DLAceReqSignature "$ss$2")
echo $s
}
DownloadAceliveFromServer()
{
# $1 - server
# $2 - id type : pid (hex),infohash (base64)
# $3 - id
# $4 - filename to save
local url torrent
[ -f "$4" ] && rm -f "$4"
url="http://"$1$(DLAceGetUri $2 $3)
echo URL for $2 $3 : $url
xml=$(curl --fail -A "ACEStream/3.1.16" --max-time 10 --max-filesize 204800 "$url") || return
[ -z "$xml" ] && return 0 # empty response
torrent=$(echo $xml | sed -nre "s/^.*<torrent>(.*)<\/torrent>.*$/\1/p")
[ -z "$torrent" ] && {
retry=$(echo $xml | sed -nre "s/^.*<interval>([0-9]*)<\/interval>.*$/\1/p")
[ -z "$retry" ] && {
echo Download acelive got garbage from $1 : $xml
return 1
}
echo We are told to sleep for $retry msec
retry=$((retry/1000))
sleep $retry
return 1
}
echo $torrent | base64 -d >$4
return 0
}
DownloadAcelive()
{
# $1 - id type : pid (hex),infohash (base64)
# $2 - id
# $3 - filename to save
# on return :
# retval!=0 - was error
# retval==0 - either file was created or server did not return response and file was not created. must check for file existence
[ -f "$3" ] && rm -f "$3"
servers=( \
"s1.torrentstream.org" "s1.torrentstream.net" "s1.torrentstream.info" \
"s2.torrentstream.org" "s2.torrentstream.net" "s2.torrentstream.info" \
"s3.torrentstream.org" "s3.torrentstream.net" "s3.torrentstream.info" )
i=$(( RANDOM % ${#servers[@]} ))
v=${servers[0]}
servers[0]=${servers[$i]}
servers[$i]=$v
for server in ${servers[*]} ; do
DownloadAceliveFromServer $server $1 $2 $3 && break
done
}
API_URL_TTS_RAW=http://api.torrentstream.net/upload/raw
get_cid()
{
# $1 - acelive filename
b64text=`base64 $1`
json=`curl -A "Python-urllib/2.7" --max-time 5 --max-filesize 1024 --retry 2 -X POST -H "Content-Type: application/octet-stream" -d "$b64text" "$API_URL_TTS_RAW" 2>/dev/null`
echo "$json" | sed -re "s/.*\"content_id\":\"((.){40})\".*/\1/g"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment