#!/bin/bash
# -*- mode: sh; fill-column: 75; tab-width: 8; coding: utf-8-unix -*-

ROLE=proxy
#NO prog=proxy_export
. /usr/local/bin/usr_local_tput.bash || exit 2

[ -f /usr/local/bin/proxy_ping_lib.bash ] || \
    { ERROR missing /usr/local/bin/proxy_ping_lib.bash ; exit 1; }
# /sbin/ifconfig on Debian morons and /bin/ifconfig on Gentoo

# [ "$USER" != root ] && export PATH=/sbin:$PATH

## proxy_to_virbr1_15
proxy_to_virbr1_15 () {
   if ifconfig | grep -q virbr1 ; then
	PROXY_VIREXT_IP=$( ifconfig virbr1 | grep inet | sed -e 's/.*inet //' -e 's/ .*//' )
	[ $? -eq 0 -a -n "$PROXY_VIREXT_IP" ] && \
	    PROXY_VIREXT_HOST=$( echo $PROXY_VIREXT_IP | sed -e 's/2$/15/' ) && \
            [ -n "$PROXY_VIREXT_HOST" ] && \
	    export no_proxy="localhost,127.0.0.1,$PROXY_VIREXT_HOST" && \
	    export https_proxy=http://$PROXY_VIREXT_HOST:9128 && \
	    export socks_proxy=socks5://$PROXY_VIREXT_HOST:9050 && \
		export TOR_SOCKS_HOST=$PROXY_VIREXT_HOST && \
		export TOR_SOCKS_PORT=9050

   fi
   return 0
}

## proxy_http_host_httpproxy
proxy_http_host_httpproxy () {
    if netstat -nle4 | grep -q 127.0.0.1:3128 >/dev/null ; then
        export http_proxy=http://127.0.0.1:3128
	export https_proxy=http://127.0.0.1:3128
	export RSYNC_PROXY=127.0.0.1:3128
      fi
    return 0
}

proxy_https_host_selektor () { proxy_https_host_tor $* ; }
proxy_https_host_tor () {
    if netstat -nle4 | grep -q 127.0.0.1:9128 >/dev/null ; then
        export https_proxy=http://127.0.0.1:9128
      fi
    return 0
}

proxy_socks_host_tor () {
    local file=/etc/tor/torrc
    if [ -f $file ] ; then
	port=`grep -hi ^socksport /etc/tor/torrc /etc/tor/torrc-defaults | sed -e 's/SocksPort //' -e 's/.*://'`
        [ -z "$port" ] && port=9050
        export socks_proxy=socks5://127.0.0.1:$port
	return 0
    else
	[ -n "$DEBUG" ] && [ "$DEBUG" -ne 0 ] && \
	    echo >&2 WARN: $prog $file not found
        return 1
    fi
    if netstat -nle4 | grep -q 127.0.0.1:$port >/dev/null ; then
        export socks_proxy=socks5://127.0.0.1:$port
      fi
    return 0
}

proxy_socks_host_selektor () {
    local file=/var/lib/tor/.SelekTOR/3xx/SelekTOR.xml
    if [ -f $file ] ; then
	port=`grep PREF_LISTENPORT $file | sed -e 's/.*">//' -e 's/<.*//'`
        [ -z "$port" ] && port=9050
        export socks_proxy=socks5://127.0.0.1:$port
	return 0
    else
	[ -n "$DEBUG" ] && [ "$DEBUG" -ne 0 ] && \
	    echo >&2 WARN: $prog $file not found
    fi
    return 0
}

# proxy_export_mode
proxy_export_mode () {
    ppl=/usr/local/bin/proxy_ping_lib.bash

    [ -z "$MODE" ] && MODE=$( $ppl proxy_ping_mode )
    [ -z "$MODE" ] && MODE=host

    # $0=bash
    [ -n "$DEBUG" ] && [ "$DEBUG" -ne 0 ] && \
	debug $prog MODE=$MODE ONE=$MODE 0=$0 "$#" "$@"

    if [ "$MODE" = from -o "$MODE" = whonix ] ; then
	export no_proxy="localhost,127.0.0.1,10.0.2.15"
	proxy_http_host_httpproxy
	proxy_to_virbr1_15

    elif [ "$MODE" = client ] ; then
	# inherit the environment
	proxy_http_host_httpproxy
	proxy_https_host_tor
	proxy_socks_host_tor

    elif [ "$MODE" = nat ] ; then
	export no_proxy="localhost,127.0.0.1,10.0.2.2,10.0.2.0/24"
	# get external
	external=`grep external$ /etc/hosts|sed -e 's/ .*//'`
	if [ $? -eq 0 ] && [ -n "$external" ] ; then
	    # get the ports and PROXY_MODE of the host
	    export socks_proxy=socks5://$external:9050
	    export http_proxy=http://$external:3128
	    export https_proxy=http://$external:9128
	fi

    elif [ "$MODE" = vda -o "$MODE" = workstation ] ; then
	export no_proxy="localhost,127.0.0.1,10.152.152.10"
	export socks_proxy=socks5://10.152.152.10:9050
	proxy_http_host_httpproxy
	export https_proxy=http://10.152.152.10:9128

    elif [ "$MODE" = gateway ] ; then

	export no_proxy="localhost,127.0.0.1,10.0.2.2,10.0.2.15"
	export socks_proxy=socks5://10.0.2.15:9050
	proxy_http_host_httpproxy
	export https_proxy=http://10.0.2.15:9128

    elif [ "$MODE" = selektor ] ; then
        export http_proxy=http://127.0.0.1:3128
	export https_proxy=http://127.0.0.1:9128
	export no_proxy="localhost,127.0.0.1"

	proxy_http_host_httpproxy
	proxy_https_host_selektor
	proxy_socks_host_selektor

    elif true || [ "$MODE" = to -o "$MODE" = to_tor -o "$MODE" = tor ] ; then

        export http_proxy=http://127.0.0.1:3128
	export https_proxy=http://127.0.0.1:9128
	export socks_proxy=http://127.0.0.1:9050
	export no_proxy="localhost,127.0.0.1"
	proxy_http_host_httpproxy
	proxy_https_host_tor
	proxy_socks_host_tor
    fi
}

# echo $0 $* "$0" = 'tostop' -o
if [ "$0" = '-bash' -o "$0" = '/bin/bash'  -o "$0" = 'bash' ] ; then
       proxy_export_mode
       [ -n "$DEBUG" ] && [ "$DEBUG" -ne 0 ] && \
	   env | grep proxy | while read line ; do debug $line ; done
  elif [ -x /usr/bin/basename ] && \
       [ `basename -- "$0"` = 'proxy_export.bash' -o \
  	   "$( basename -- "`readlink $0`" )" = 'proxy_export.bash' ] ; then

    if [ "$#" -eq 1 ] && [ "$1" = '-h' -o "$1" = '--help' ] ; then
	echo USAGE: $0 && grep '^## ' $0 | sed -e 's/^## //'|sort

    elif [ "$#" -eq 0 ] || [ "$#" -eq 1 -a $1 = mode ]; then
	set -- proxy_export_mode
    fi

    eval "$@"
    exit $?
fi
