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. |
| 16 | # All rights reserved. |
| 17 | # |
| 18 | # AUTHORS |
| 19 | # Darren Hart <dvhart@linux.intel.com> |
| 20 | |
| 21 | # Locate the netcat binary |
| 22 | SOCAT=$(which socat 2>/dev/null) |
| 23 | if [ $? -ne 0 ]; then |
| 24 | echo "ERROR: socat binary not in PATH" 1>&2 |
| 25 | exit 1 |
| 26 | fi |
| 27 | METHOD="" |
| 28 | |
| 29 | # Test for a valid IPV4 quad with optional bitmask |
| 30 | valid_ipv4() { |
| 31 | 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]))?$" |
| 32 | return $? |
| 33 | } |
| 34 | |
| 35 | # Convert an IPV4 address into a 32bit integer |
| 36 | ipv4_val() { |
| 37 | IP="$1" |
| 38 | SHIFT=24 |
| 39 | VAL=0 |
| 40 | for B in ${IP//./ }; do |
| 41 | VAL=$(($VAL+$(($B<<$SHIFT)))) |
| 42 | SHIFT=$(($SHIFT-8)) |
| 43 | done |
| 44 | echo "$VAL" |
| 45 | } |
| 46 | |
| 47 | # Determine if two IPs are equivalent, or if the CIDR contains the IP |
| 48 | match_ipv4() { |
| 49 | CIDR=$1 |
| 50 | IP=$2 |
| 51 | |
| 52 | if [ -z "${IP%%$CIDR}" ]; then |
| 53 | return 0 |
| 54 | fi |
| 55 | |
| 56 | # Determine the mask bitlength |
| 57 | BITS=${CIDR##*/} |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 58 | [ "$BITS" != "$CIDR" ] || BITS=32 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 59 | if [ -z "$BITS" ]; then |
| 60 | return 1 |
| 61 | fi |
| 62 | |
| 63 | IPVAL=$(ipv4_val $IP) |
| 64 | IP2VAL=$(ipv4_val ${CIDR%%/*}) |
| 65 | |
| 66 | # OR in the unmasked bits |
| 67 | for i in $(seq 0 $((32-$BITS))); do |
| 68 | IP2VAL=$(($IP2VAL|$((1<<$i)))) |
| 69 | IPVAL=$(($IPVAL|$((1<<$i)))) |
| 70 | done |
| 71 | |
| 72 | if [ $IPVAL -eq $IP2VAL ]; then |
| 73 | return 0 |
| 74 | fi |
| 75 | return 1 |
| 76 | } |
| 77 | |
| 78 | # Test to see if GLOB matches HOST |
| 79 | match_host() { |
| 80 | HOST=$1 |
| 81 | GLOB=$2 |
| 82 | |
| 83 | if [ -z "${HOST%%$GLOB}" ]; then |
| 84 | return 0 |
| 85 | fi |
| 86 | |
| 87 | # Match by netmask |
| 88 | if valid_ipv4 $GLOB; then |
| 89 | HOST_IP=$(gethostip -d $HOST) |
| 90 | if valid_ipv4 $HOST_IP; then |
| 91 | match_ipv4 $GLOB $HOST_IP |
| 92 | if [ $? -eq 0 ]; then |
| 93 | return 0 |
| 94 | fi |
| 95 | fi |
| 96 | fi |
| 97 | |
| 98 | return 1 |
| 99 | } |
| 100 | |
| 101 | # If no proxy is set or needed, just connect directly |
| 102 | METHOD="TCP:$1:$2" |
| 103 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 104 | [ -z "${ALL_PROXY}" ] && ALL_PROXY=$all_proxy |
| 105 | [ -z "${ALL_PROXY}" ] && ALL_PROXY=$http_proxy |
| 106 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 107 | if [ -z "$ALL_PROXY" ]; then |
| 108 | exec $SOCAT STDIO $METHOD |
| 109 | fi |
| 110 | |
| 111 | # Connect directly to hosts in NO_PROXY |
| 112 | for H in ${NO_PROXY//,/ }; do |
| 113 | if match_host $1 $H; then |
| 114 | exec $SOCAT STDIO $METHOD |
| 115 | fi |
| 116 | done |
| 117 | |
| 118 | # Proxy is necessary, determine protocol, server, and port |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 119 | # extract protocol |
| 120 | PROTO=${ALL_PROXY%://*} |
| 121 | # strip protocol:// from string |
| 122 | ALL_PROXY=${ALL_PROXY#*://} |
| 123 | # extract host & port parts: |
| 124 | # 1) drop username/password |
| 125 | PROXY=${ALL_PROXY##*@} |
| 126 | # 2) remove optional trailing /? |
| 127 | PROXY=${PROXY%%/*} |
| 128 | # 3) extract optional port |
| 129 | PORT=${PROXY##*:} |
| 130 | if [ "$PORT" = "$PROXY" ]; then |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 131 | PORT="" |
| 132 | fi |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 133 | # 4) remove port |
| 134 | PROXY=${PROXY%%:*} |
| 135 | |
| 136 | # extract username & password |
| 137 | PROXYAUTH="${ALL_PROXY%@*}" |
| 138 | [ "$PROXYAUTH" = "$ALL_PROXY" ] && PROXYAUTH= |
| 139 | [ -n "${PROXYAUTH}" ] && PROXYAUTH=",proxyauth=${PROXYAUTH}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 140 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 141 | if [ "$PROTO" = "socks" ] || [ "$PROTO" = "socks4a" ]; then |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 142 | if [ -z "$PORT" ]; then |
| 143 | PORT="1080" |
| 144 | fi |
| 145 | METHOD="SOCKS4A:$PROXY:$1:$2,socksport=$PORT" |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 146 | elif [ "$PROTO" = "socks4" ]; then |
| 147 | if [ -z "$PORT" ]; then |
| 148 | PORT="1080" |
| 149 | fi |
| 150 | METHOD="SOCKS4:$PROXY:$1:$2,socksport=$PORT" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | else |
| 152 | # Assume PROXY (http, https, etc) |
| 153 | if [ -z "$PORT" ]; then |
| 154 | PORT="8080" |
| 155 | fi |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 156 | METHOD="PROXY:$PROXY:$1:$2,proxyport=${PORT}${PROXYAUTH}" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 157 | fi |
| 158 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 159 | exec $SOCAT STDIO "$METHOD" |