gists/networking/dynamic-ssh-forwarding.md

1.1 KiB

Set up port forwarding

  1. Set up port forwarding
    $ ssh -D 8080 -N ubuntu@mercurial-manfifold.eu
    
  2. Try out connection
    $ curl --socks5 127.0.0.1:8080 http://example.com
    

Set system-wide proxy configuration

  1. Create /etc/profile.d/proxy.sh with the following content:
    #!/bin/bash
    
     if [ -f "/etc/proxy-enabled" ]; then
         export all_proxy="socks5://127.0.0.1:8080"
         export http_proxy="socks5://127.0.0.1:8080"
         export https_proxy="socks5://127.0.0.1:8080"
         export ftp_proxy="socks5://127.0.0.1:8080"
         export HTTP_PROXY="socks5://127.0.0.1:8080"
         export HTTPS_PROXY="socks5://127.0.0.1:8080"
         export FTP_PROXY="socks5://127.0.0.1:8080"
         export no_proxy="localhost,127.0.0.1,::1"
         export NO_PROXY="localhost,127.0.0.1,::1"
     fi
    
  2. Add the following to .zshrc:
    alias proxy-on='sudo touch /etc/proxy-enabled'
    alias proxy-off='sudo rm -f /etc/proxy-enabled'
    

Note that enabling or disabling the proxy only takes effect after logging out and back in, as that is when /etc/profile.d/proxy.sh is sourced.