Script to enable Ubuntu use SSH dynamic port forwarding as a global HTTP_PROXY

If you want to set a proxy for your Ubuntu server to access Internet or your company private hosts, you may get disappointed for googing all kinds of info.

Lazy people get ideas of being lazier.

Here we use SSH dynamic port fowarding to open a Socks5 local port serving as a socks5 proxy, then use privoxy to transform the proxy into a HTTP Proxy with customized forwarding proxy rules.

# in file ~/.bashrc
startSSHProxy(){
        sock5Port=7070
        httpPort=8118
        proxyUrl=http://127.0.0.1:${httpPort}
        privoxyFolder=/etc/privoxy
        privoxyConf=${privoxyFolder}/config
        echo "[INFO] Start SSH Proxy to VPN on Windows 10 Azure node which has VPN connection"
        remoteDomain=*.*.*.*
        username=paul

        # More config: http://www.privoxy.org/user-manual/config.html#SOCKS
        domainPatternToUseProxy=.corp.oracle.com
        targetWebsiteToTest=http://10.0.10.80:8000/

        killall ssh
        sshTunnelToRun="ssh -vfCN -D 127.0.0.1:${sock5Port} $username@$remoteDomain"
        echo "${sshTunnelToRun}"
        echo -ne "\x1b[31m"
        eval "$sshTunnelToRun"
        echo -ne "\x1b[0m"
        echo "[INFO] "
        echo "[INFO] socks5 proxy started at port ${sock5Port}"
        if [[ ! -z ${targetWebsiteToTest} ]]; then
                curl --socks5 localhost:${sock5Port} ${targetWebsiteToTest} > /dev/null
                if [[ ! "$?" -eq 0 ]];then
                        echo "[ERROR] SSH Port forwarding over Sock5 proxy port ${sock5Port} failed. "
                        return 1
                fi

        fi
        echo "[INFO] Test socks5 proxy"

        echo "[INFO] Start proxy conversion to HTTP Proxy via privoxy on port ${httpPort}"
        if [[ -z "$(dpkg -l privoxy)" ]];then
                sudo apt-get install -y privoxy
        fi

        if [[ -z "$(cat ${privoxyConf} | grep $domainPatternToUseProxy)" ]];then
                # More config: http://www.privoxy.org/user-manual/config.html#SOCKS
                # Say you want to use the proxy to access .corp.cloudsimple.com
                echo "forward / ." | sudo tee --append ${privoxyConf}
                echo "forward-socks5 ${domainPatternToUseProxy} localhost:${sock5Port} ." | sudo tee --append ${privoxyConf}
                echo "forward-socks5 10.*.*.*/ localhost:${sock5Port} ." | sudo tee --append ${privoxyConf}
                echo "debug 1" | sudo tee --append ${privoxyConf}
                sudo /etc/init.d/privoxy restart
        fi

        sudo /etc/init.d/privoxy restart

        sudo netstat -plnt

        proxyScript="export HTTPS_PROXY=${proxyUrl}; export HTTP_PROXY=${proxyUrl}; export PROXY=${proxyUrl}; export http_proxy=${proxyUrl}; export https_proxy=${proxyUrl}; export proxy=${proxyUrl}"

        if [[ ! -z ${targetWebsiteToTest} ]]; then
                eval "$proxyScript"
                curl  ${targetWebsiteToTest} > /dev/null
                curlExitCode=$?
                if [[ ! "${curlExitCode}" -eq 0 ]];then
                        echo "[ERROR] privoxy HTTP Proxy over ${proxyUrl} test failed. "
                        echo "[INFO] 'cat /var/log/privoxy/*' for more"
                        return 2
                fi

        fi

        echo "[INFO] run"
        echo "[INFO] "
        echo -ne "\x1b[31m"
        echo "${proxyScript}"
        echo -ne "\x1b[0m"
        echo "[INFO] "

}
export -f startSSHProxy


Run startSSHProxy to rock

Leave a Reply

Your email address will not be published. Required fields are marked *