Skip to content

Instantly share code, notes, and snippets.

@unixfox
Created October 31, 2017 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unixfox/79c850f0d0ccc37f4f7e158e54fdc7d8 to your computer and use it in GitHub Desktop.
Save unixfox/79c850f0d0ccc37f4f7e158e54fdc7d8 to your computer and use it in GitHub Desktop.
Download a file using the current connection and a proxy at the same time
#!/bin/bash
# Variables
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
percentDownloadedFromSecondProxy=35
filename=$(basename "$1")
filesize=$(curl -sI $1 | tr -d '\r' | sed -En 's/^Content-Length: (.*)/\1/p')
onepart=$(echo $[ $filesize * 35 /100 ])
secondpart=$(echo $[ $onepart + 1 ])
proxyurl="socks5://192.168.100.89:1080"
# Check if an URL is provided
if [[ $1 =~ $regex ]]
then
echo "Link valid, downloading..."
else
echo "Link not valid"
exit 0
fi
# Progress Bar function
function ProgressBar {
let _progress=(${1}*100/${2}*100)/100
let _done=(${_progress}*4)/10
let _left=40-$_done
_done=$(printf "%${_done}s")
_left=$(printf "%${_left}s")
printf "\rProgress : [${_done// /#}${_left// /-}] ${_progress}%%"
}
# Download the file
curl -s -x $proxyurl --range 0-$onepart -o $filename.part1 $1 &
curl -s --range $secondpart- -o $filename.part2 $1 &
# Wait a bit to let curl creating the file
sleep 1s
# Show progress bar
while /usr/bin/pgrep curl >/dev/null; do
sleep 0.1
currentSizePart1=$(stat --printf="%s" $filename.part1)
currentSizePart2=$(stat --printf="%s" $filename.part2)
currentSizeTotal=$(echo $[ 100 * ($currentSizePart1 + currentSizePart2) / $filesize ])
ProgressBar $currentSizeTotal 100
done
# Cleanup
cat $filename.part* > $filename
rm $filename.part*
echo -e "\nDownload finished!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment