| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/bash | 
 | 2 |  | 
 | 3 | # oe-git-proxy is a simple tool to be via GIT_PROXY_COMMAND. It uses socat | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 4 | # to make SOCKS5 or HTTPS proxy connections. | 
 | 5 | # It uses ALL_PROXY or all_proxy or http_proxy to determine the proxy server, | 
 | 6 | # protocol, and port. | 
 | 7 | # It uses NO_PROXY to skip using the proxy for a comma delimited list of | 
 | 8 | # hosts, host globs (*.example.com), IPs, or CIDR masks (192.168.1.0/24). It | 
 | 9 | # is known to work with both bash and dash shells. | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | # | 
 | 11 | # Example ALL_PROXY values: | 
 | 12 | # ALL_PROXY=socks://socks.example.com:1080 | 
 | 13 | # ALL_PROXY=https://proxy.example.com:8080 | 
 | 14 | # | 
 | 15 | # Copyright (c) 2013, Intel Corporation. | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 16 | # | 
 | 17 | # SPDX-License-Identifier: GPL-2.0-only | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | # | 
 | 19 | # AUTHORS | 
 | 20 | # Darren Hart <dvhart@linux.intel.com> | 
 | 21 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 22 | if [ $# -lt 2 -o "$1" = '--help' -o "$1" = '-h' ] ; then | 
 | 23 |     echo 'oe-git-proxy: error: the following arguments are required: host port' | 
 | 24 |     echo 'Usage: oe-git-proxy host port' | 
 | 25 |     echo '' | 
 | 26 |     echo 'OpenEmbedded git-proxy - a simple tool to be used via GIT_PROXY_COMMAND.' | 
 | 27 |     echo 'It uses socat to make SOCKS or HTTPS proxy connections.' | 
 | 28 |     echo 'It uses ALL_PROXY to determine the proxy server, protocol, and port.' | 
 | 29 |     echo 'It uses NO_PROXY to skip using the proxy for a comma delimited list' | 
 | 30 |     echo 'of hosts, host globs (*.example.com), IPs, or CIDR masks (192.168.1.0/24).' | 
 | 31 |     echo 'It is known to work with both bash and dash shells.runs native tools' | 
 | 32 |     echo '' | 
 | 33 |     echo 'arguments:' | 
 | 34 |     echo '  host                proxy host to use' | 
 | 35 |     echo '  port                proxy port to use' | 
 | 36 |     echo '' | 
 | 37 |     echo 'options:' | 
 | 38 |     echo '  -h, --help          show this help message and exit' | 
 | 39 |     echo '' | 
 | 40 |     exit 2 | 
 | 41 | fi | 
 | 42 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | # Locate the netcat binary | 
 | 44 | SOCAT=$(which socat 2>/dev/null) | 
 | 45 | if [ $? -ne 0 ]; then | 
 | 46 | 	echo "ERROR: socat binary not in PATH" 1>&2 | 
 | 47 | 	exit 1 | 
 | 48 | fi | 
 | 49 | METHOD="" | 
 | 50 |  | 
 | 51 | # Test for a valid IPV4 quad with optional bitmask | 
 | 52 | valid_ipv4() { | 
 | 53 | 	echo $1 | egrep -q "^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}(/(3[0-2]|[1-2]?[0-9]))?$" | 
 | 54 | 	return $? | 
 | 55 | } | 
 | 56 |  | 
 | 57 | # Convert an IPV4 address into a 32bit integer | 
 | 58 | ipv4_val() { | 
 | 59 | 	IP="$1" | 
 | 60 | 	SHIFT=24 | 
 | 61 | 	VAL=0 | 
 | 62 | 	for B in ${IP//./ }; do | 
 | 63 | 		VAL=$(($VAL+$(($B<<$SHIFT)))) | 
 | 64 | 		SHIFT=$(($SHIFT-8)) | 
 | 65 | 	done | 
 | 66 | 	echo "$VAL" | 
 | 67 | } | 
 | 68 |  | 
 | 69 | # Determine if two IPs are equivalent, or if the CIDR contains the IP | 
 | 70 | match_ipv4() { | 
 | 71 | 	CIDR=$1 | 
 | 72 | 	IP=$2 | 
 | 73 |  | 
 | 74 | 	if [ -z "${IP%%$CIDR}" ]; then | 
 | 75 | 		return 0 | 
 | 76 | 	fi | 
 | 77 |  | 
 | 78 | 	# Determine the mask bitlength | 
 | 79 | 	BITS=${CIDR##*/} | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 80 | 	[ "$BITS" != "$CIDR" ] || BITS=32 | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 81 | 	if [ -z "$BITS" ]; then | 
 | 82 | 		return 1 | 
 | 83 | 	fi | 
 | 84 |  | 
 | 85 | 	IPVAL=$(ipv4_val $IP) | 
 | 86 | 	IP2VAL=$(ipv4_val ${CIDR%%/*}) | 
 | 87 |  | 
 | 88 | 	# OR in the unmasked bits | 
 | 89 | 	for i in $(seq 0 $((32-$BITS))); do | 
 | 90 | 		IP2VAL=$(($IP2VAL|$((1<<$i)))) | 
 | 91 | 		IPVAL=$(($IPVAL|$((1<<$i)))) | 
 | 92 | 	done | 
 | 93 |  | 
 | 94 | 	if [ $IPVAL -eq $IP2VAL ]; then | 
 | 95 | 		return 0 | 
 | 96 | 	fi | 
 | 97 | 	return 1 | 
 | 98 | } | 
 | 99 |  | 
 | 100 | # Test to see if GLOB matches HOST | 
 | 101 | match_host() { | 
 | 102 | 	HOST=$1 | 
 | 103 | 	GLOB=$2 | 
 | 104 |  | 
 | 105 | 	if [ -z "${HOST%%$GLOB}" ]; then | 
 | 106 | 		return 0 | 
 | 107 | 	fi | 
 | 108 |  | 
 | 109 | 	# Match by netmask | 
 | 110 | 	if valid_ipv4 $GLOB; then | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 111 | 		for HOST_IP in $(getent ahostsv4 $HOST | grep ' STREAM ' | cut -d ' ' -f 1) ; do | 
 | 112 | 			if valid_ipv4 $HOST_IP; then | 
 | 113 | 				match_ipv4 $GLOB $HOST_IP | 
 | 114 | 				if [ $? -eq 0 ]; then | 
 | 115 | 					return 0 | 
 | 116 | 				fi | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | 			fi | 
| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 118 | 		done | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 119 | 	fi | 
 | 120 |  | 
 | 121 | 	return 1 | 
 | 122 | } | 
 | 123 |  | 
 | 124 | # If no proxy is set or needed, just connect directly | 
 | 125 | METHOD="TCP:$1:$2" | 
 | 126 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 127 | [ -z "${ALL_PROXY}" ] && ALL_PROXY=$all_proxy | 
 | 128 | [ -z "${ALL_PROXY}" ] && ALL_PROXY=$http_proxy | 
 | 129 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 130 | if [ -z "$ALL_PROXY" ]; then | 
 | 131 | 	exec $SOCAT STDIO $METHOD | 
 | 132 | fi | 
 | 133 |  | 
 | 134 | # Connect directly to hosts in NO_PROXY | 
| Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 135 | for H in "${NO_PROXY//,/ }"; do | 
 | 136 | 	if match_host $1 "$H"; then | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 137 | 		exec $SOCAT STDIO $METHOD | 
 | 138 | 	fi | 
 | 139 | done | 
 | 140 |  | 
 | 141 | # Proxy is necessary, determine protocol, server, and port | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 142 | # extract protocol | 
 | 143 | PROTO=${ALL_PROXY%://*} | 
 | 144 | # strip protocol:// from string | 
 | 145 | ALL_PROXY=${ALL_PROXY#*://} | 
 | 146 | # extract host & port parts: | 
 | 147 | #   1) drop username/password | 
 | 148 | PROXY=${ALL_PROXY##*@} | 
 | 149 | #   2) remove optional trailing /? | 
 | 150 | PROXY=${PROXY%%/*} | 
 | 151 | #   3) extract optional port | 
 | 152 | PORT=${PROXY##*:} | 
 | 153 | if [ "$PORT" = "$PROXY" ]; then | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 154 | 	PORT="" | 
 | 155 | fi | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 156 | #   4) remove port | 
 | 157 | PROXY=${PROXY%%:*} | 
 | 158 |  | 
 | 159 | # extract username & password | 
 | 160 | PROXYAUTH="${ALL_PROXY%@*}" | 
 | 161 | [ "$PROXYAUTH" = "$ALL_PROXY" ] && PROXYAUTH= | 
 | 162 | [ -n "${PROXYAUTH}" ] && PROXYAUTH=",proxyauth=${PROXYAUTH}" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 163 |  | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 164 | if [ "$PROTO" = "socks" ] || [ "$PROTO" = "socks4a" ]; then | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 165 | 	if [ -z "$PORT" ]; then | 
 | 166 | 		PORT="1080" | 
 | 167 | 	fi | 
 | 168 | 	METHOD="SOCKS4A:$PROXY:$1:$2,socksport=$PORT" | 
| Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 169 | elif [ "$PROTO" = "socks4" ]; then | 
 | 170 | 	if [ -z "$PORT" ]; then | 
 | 171 | 		PORT="1080" | 
 | 172 | 	fi | 
 | 173 | 	METHOD="SOCKS4:$PROXY:$1:$2,socksport=$PORT" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 174 | else | 
 | 175 | 	# Assume PROXY (http, https, etc) | 
 | 176 | 	if [ -z "$PORT" ]; then | 
 | 177 | 		PORT="8080" | 
 | 178 | 	fi | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 179 | 	METHOD="PROXY:$PROXY:$1:$2,proxyport=${PORT}${PROXYAUTH}" | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 180 | fi | 
 | 181 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 182 | exec $SOCAT STDIO "$METHOD" |