Skip to content

Instantly share code, notes, and snippets.

@little-dude
Created May 25, 2018 17:25
Show Gist options
  • Save little-dude/33c166d5dcc6e25b5160605587d1abf4 to your computer and use it in GitHub Desktop.
Save little-dude/33c166d5dcc6e25b5160605587d1abf4 to your computer and use it in GitHub Desktop.
set git proxies
# Set a git proxy for the SSH a git protocols in the current shell.
#
# Note that for SSH you can also set the Proxycommand in the ~/.config/ssh
# file, which might be more convenient than setting that in each shell
#
# Host gh
# User git
# Hostname github.com
# Port 22
# IdentityFile ~/.ssh/my_key
# Proxycommand socat - PROXY:myproxy:%h:%p,proxyport=8000,
#
# References:
#
# http://gitolite.com/git-over-proxy.html
# https://stackoverflow.com/a/28527476/1836144
set_git_proxy() {
# usage:
#
# set_git_proxy <yourproxy>
#
local proxy=${1}
local git_proxy_wrapper=/tmp/git_proxy_wrapper.sh
local git_ssh_proxy_wrapper=/tmp/git_ssh_proxy_wrapper.sh
# Proxy the git protocol
rm -f ${git_proxy_wrapper}
cat > ${git_proxy_wrapper} << EOF
#!/bin/sh
set -x
socat - PROXY:${proxy%:*}:\$1:\$2,proxyport=${proxy#*:}
EOF
chmod u+x ${git_proxy_wrapper}
export GIT_PROXY_COMMAND=${git_proxy_wrapper}
# Proxy for the SSH protocol
rm -f ${git_ssh_proxy_wrapper}
cat > ${git_ssh_proxy_wrapper} << EOF
#!/bin/sh
set -x
ssh -o ProxyCommand='socat - PROXY:${proxy%:*}:%h:%p,proxyport=${proxy#*:}' "\$@"
EOF
chmod u+x ${git_ssh_proxy_wrapper}
export GIT_SSH=${git_proxy_wrapper}
# Since git 2.3 we can use the GIT_SSH_COMMAND instead. It takes precedence over GIT_SSH.
# shellcheck disable=SC2089
GIT_SSH_COMMAND="ssh -o Proxycommand='socat - PROXY:${proxy%:*}:%h:%p,proxyport=${proxy#*:}'"
# shellcheck disable=SC2090
export GIT_SSH_COMMAND
}
unset_git_proxy() {
unset GIT_SSH
unset GIT_SSH_COMMAND
unset GIT_PROXY_COMMAND
rm -f /tmp/git_proxy_wrapper.sh /tmp/git_ssh_proxy_wrapper.sh
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment