Search code examples
gitsshproxyssh-tunnel

How to force Git to use socks proxy over its ssh connection?


There are tons of identical solutions over the internet for defining proxy tunnel for git's downloads like this one, which all is by setting git's https.proxy & http.proxy config. but those answers are not working when you try to clone/push/pull etc. over the ssh protocol!

For example, by setting git config --global https.proxy socks5://127.0.0.1:9999 when you try to clone git clone [email protected]:user/repo.git it does not go through the defined sock5 tunnel!

I've tried various thing but none was working!

Question:

How to set git to use a local socks5 proxy (e.g. 127.0.0.1:9999) when it uses ssh connections?


Solution

  • After some visiting so many pages, I finally find the solution to my question:

    # [step 1] create a ssh-proxy
      ssh -D 9999 -qCN [email protected]
    
    # [step 2] make git connect through the ssh-proxy
      # [current script only]
      export GIT_SSH_COMMAND='ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"'
      # OR [git global setting] 
      git config --global core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"'
      # OR [one-time only use]
      git clone -c=core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"' [email protected]:user/repo.git
      # OR [current repository use only]
      git config core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"'
    

    To install connect on Ubuntu:

    sudo apt install connect-proxy