blob: 12479024062828818266f0ebaf6173efe87d92c1 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/bin/bash
2
3# oe-git-proxy is a simple tool to be via GIT_PROXY_COMMAND. It uses socat
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05004# 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 Williamsc124f4f2015-09-15 14:41:29 -050010#
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
22SOCAT=$(which socat 2>/dev/null)
23if [ $? -ne 0 ]; then
24 echo "ERROR: socat binary not in PATH" 1>&2
25 exit 1
26fi
27METHOD=""
28
29# Test for a valid IPV4 quad with optional bitmask
30valid_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
36ipv4_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
48match_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 Williamsf1e5d692016-03-30 15:21:19 -050058 [ "$BITS" != "$CIDR" ] || BITS=32
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 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
79match_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
102METHOD="TCP:$1:$2"
103
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500104[ -z "${ALL_PROXY}" ] && ALL_PROXY=$all_proxy
105[ -z "${ALL_PROXY}" ] && ALL_PROXY=$http_proxy
106
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107if [ -z "$ALL_PROXY" ]; then
108 exec $SOCAT STDIO $METHOD
109fi
110
111# Connect directly to hosts in NO_PROXY
112for H in ${NO_PROXY//,/ }; do
113 if match_host $1 $H; then
114 exec $SOCAT STDIO $METHOD
115 fi
116done
117
118# Proxy is necessary, determine protocol, server, and port
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500119# extract protocol
120PROTO=${ALL_PROXY%://*}
121# strip protocol:// from string
122ALL_PROXY=${ALL_PROXY#*://}
123# extract host & port parts:
124# 1) drop username/password
125PROXY=${ALL_PROXY##*@}
126# 2) remove optional trailing /?
127PROXY=${PROXY%%/*}
128# 3) extract optional port
129PORT=${PROXY##*:}
130if [ "$PORT" = "$PROXY" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500131 PORT=""
132fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500133# 4) remove port
134PROXY=${PROXY%%:*}
135
136# extract username & password
137PROXYAUTH="${ALL_PROXY%@*}"
138[ "$PROXYAUTH" = "$ALL_PROXY" ] && PROXYAUTH=
139[ -n "${PROXYAUTH}" ] && PROXYAUTH=",proxyauth=${PROXYAUTH}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500141if [ "$PROTO" = "socks" ] || [ "$PROTO" = "socks4a" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500142 if [ -z "$PORT" ]; then
143 PORT="1080"
144 fi
145 METHOD="SOCKS4A:$PROXY:$1:$2,socksport=$PORT"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500146elif [ "$PROTO" = "socks4" ]; then
147 if [ -z "$PORT" ]; then
148 PORT="1080"
149 fi
150 METHOD="SOCKS4:$PROXY:$1:$2,socksport=$PORT"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151else
152 # Assume PROXY (http, https, etc)
153 if [ -z "$PORT" ]; then
154 PORT="8080"
155 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500156 METHOD="PROXY:$PROXY:$1:$2,proxyport=${PORT}${PROXYAUTH}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500157fi
158
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500159exec $SOCAT STDIO "$METHOD"