#!/bin/bash set -eu # Allows to execute wget using a socks proxy if the environment variable # socks_proxy is set. # # The socks_proxy variable shall have one of the forms: # socks://username:address@hidden:port # socks4://username:address@hidden:port # socks5://username:address@hidden:port # with username, password and port fields being optional # # As socksification applies to the whole process, domains defined in the # no_proxy setting are not excluded. # WGET=wget if [ -z "${socks_proxy:-}" ]; then exec "$WGET" "$@" fi CONFIG="" if [[ "${socks_proxy}" =~ ^socks[45]?:// ]]; then if [[ "${socks_proxy:5:1}" != ":" ]]; then CONFIG+="server_type = ${socks_proxy:5:1}" socks_proxy="${socks_proxy:9}" else socks_proxy="${socks_proxy:8}" fi elif [[ "${socks_proxy}" =~ ^[[:alnum:]]*:// ]]; then echo "Bad value specified for socks_proxy: $socks_proxy" >&2 exit 2 fi if [[ "${socks_proxy}" =~ ^([^@:]*)(:(address@hidden))?@ ]]; then unset TSOCKS_USERNAME CONFIG+=" default_user = ${BASH_REMATCH[1]}" if [ ! -z "${BASH_REMATCH[3]}" ]; then unset TSOCKS_PASSWORD CONFIG+=" default_pass = ${BASH_REMATCH[3]}" fi socks_proxy="${socks_proxy:${#BASH_REMATCH[0]}}" fi # Get rid of trailing slashes if [[ "${socks_proxy}" =~ ^([^/]*)/ ]]; then socks_proxy="${socks_proxy:0:${#BASH_REMATCH[1]}}" fi if [[ "${socks_proxy}" =~ :([0-9]+)$ ]]; then CONFIG+=" server_port = ${BASH_REMATCH[1]}" socks_proxy=${socks_proxy:0:${#socks_proxy} - ${#BASH_REMATCH[0]}} fi CONFIG+=" server = ${socks_proxy}" TSOCKS_CONF_FILE=<(echo "$CONFIG") exec tsocks "$WGET" --no-proxy "$@"