Skip to content

Instantly share code, notes, and snippets.

@spoike
Forked from sit/gitproxy-socat
Last active March 5, 2022 16:47
Show Gist options
  • Save spoike/0b2261af38e734943c1e to your computer and use it in GitHub Desktop.
Save spoike/0b2261af38e734943c1e to your computer and use it in GitHub Desktop.
A simple wrapper around socat to use as a git proxy command
#!/bin/sh
# Use socat to proxy git through an HTTP CONNECT firewall.
# Useful if you are trying to clone git:// from inside a company.
# Requires that the proxy allows CONNECT to port 9418.
#
# Save this file as gitproxy somewhere in your path (e.g., ~/bin) and then run
# chmod +x gitproxy
# git config --global core.gitproxy gitproxy
#
# More details at http://tinyurl.com/8xvpny
# Configuration. Common proxy ports are 3128, 8123, 8000.
# As mentioned by James Hanley in the comments
# Gratuisly taken from bsdstats - http://ftp.netbsd.org/pub/NetB...
# Dynamically Handle HTTP proxy services
#
# HTTP_PROXY/http_proxy can take the following form:
# [http://][username:password@]proxy[:port][/]
# Authentication details may also be provided via HTTP_PROXY_AUTH:
# HTTP_PROXY_AUTH="basic:*:username:password"
if [ -z "${HTTP_PROXY}" -a -n "${http_proxy}" ]; then
HTTP_PROXY=${http_proxy}
fi
if [ -n "${HTTP_PROXY}" ]; then
# Attempt to resolve any HTTP authentication
if [ -n "${HTTP_PROXY_AUTH}" ]; then
PROXY_AUTH_USER=`echo ${HTTP_PROXY_AUTH} | sed -E 's/^.+:\*:(.+):.+$/\1/g'`
PROXY_AUTH_PASS=`echo ${HTTP_PROXY_AUTH} | sed -E 's/^.+:\*:.+:(.+)$/\1/g'`
else
# Check for authentication within HTTP_PROXY
HAS_HTTP_AUTH=`echo ${HTTP_PROXY} | sed -E 's/^(http:\/\/)?(.+:.+)?@.+/\2/'`
if [ -n "$HAS_HTTP_AUTH" ]; then
# Found HTTP authentication details
PROXY_AUTH_USER=`echo $HAS_HTTP_AUTH | cut -d: -f1`
PROXY_AUTH_PASS=`echo $HAS_HTTP_AUTH | cut -d: -f2`
fi
fi
# Determine the proxy components
PROXY_HOST=`echo ${HTTP_PROXY} | sed -E 's/^(http:\/\/)?(.+:.+@)?([^@:]+)(:.+)?/\3/'`
PROXY_PORT=`echo ${HTTP_PROXY} | sed -E 's/^(http:\/\/)?(.+:.+@)?(.+):([0-9]+)/\4/' | sed -e 's/[^0-9]//g'`
if [ -z "${PROXY_PORT}" ]; then
# Use default proxy port
PROXY_PORT=80
fi
fi
if [ -z "${PROXY_HOST}" ]; then
# Direct connect
exec socat STDIO TCP:${1}:${2}
elif [ -z "${PROXY_AUTH_USER}" ]; then
# No-Auth
exec socat STDIO PROXY:${PROXY_HOST}:${1}:${2},proxyport=${PROXY_PORT}
else
# Auth Proxy
exec socat STDIO PROXY:${PROXY_HOST}:${1}:${2},proxyport=${PROXY_PORT},proxyauth=${PROXY_AUTH_USER}:${PROXY_AUTH_PASS}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment