]>
git.ipfire.org Git - thirdparty/openembedded/openembedded-core.git/blob - scripts/oe-git-proxy
3 # oe-git-proxy is a simple tool to be via GIT_PROXY_COMMAND. It uses socat
4 # to make SOCKS5 or HTTPS proxy connections.
5 # It uses ALL_PROXY or all_proxy or http_proxy to determine the proxy server,
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.
11 # Example ALL_PROXY values:
12 # ALL_PROXY=socks://socks.example.com:1080
13 # ALL_PROXY=https://proxy.example.com:8080
15 # Copyright (c) 2013, Intel Corporation.
17 # SPDX-License-Identifier: GPL-2.0-only
20 # Darren Hart <dvhart@linux.intel.com>
22 # disable pathname expansion, NO_PROXY fields could start with "*" or be it
25 if [ $# -lt 2 -o "$1" = '--help' -o "$1" = '-h' ] ; then
26 echo 'oe-git-proxy: error: the following arguments are required: host port'
27 echo 'Usage: oe-git-proxy host port'
29 echo 'OpenEmbedded git-proxy - a simple tool to be used via GIT_PROXY_COMMAND.'
30 echo 'It uses socat to make SOCKS or HTTPS proxy connections.'
31 echo 'It uses ALL_PROXY to determine the proxy server, protocol, and port.'
32 echo 'It uses NO_PROXY to skip using the proxy for a comma delimited list'
33 echo 'of hosts, host globs (*.example.com), IPs, or CIDR masks (192.168.1.0/24).'
34 echo 'It is known to work with both bash and dash shells.runs native tools'
37 echo ' host proxy host to use'
38 echo ' port proxy port to use'
41 echo ' -h, --help show this help message and exit'
46 # Locate the netcat binary
47 if [ -z "$SOCAT" ]; then
48 SOCAT
=$
(which socat
2>/dev
/null
)
50 echo "ERROR: socat binary not in PATH" 1>&2
56 # Test for a valid IPV4 quad with optional bitmask
58 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]))?$"
62 # Convert an IPV4 address into a 32bit integer
67 for B
in $
( echo "$IP" |
tr '.' ' ' ); do
68 VAL
=$
(($VAL+$
(($B<<$SHIFT))))
74 # Determine if two IPs are equivalent, or if the CIDR contains the IP
79 if [ -z "${IP%%$CIDR}" ]; then
83 # Determine the mask bitlength
85 [ "$BITS" != "$CIDR" ] || BITS
=32
86 if [ -z "$BITS" ]; then
91 IP2VAL
=$
(ipv4_val
${CIDR%%/*})
93 # OR in the unmasked bits
94 for i
in $
(seq 0 $
((32-$BITS))); do
95 IP2VAL
=$
(($IP2VAL|$
((1<<$i))))
96 IPVAL
=$
(($IPVAL|$
((1<<$i))))
99 if [ $IPVAL -eq $IP2VAL ]; then
105 # Test to see if GLOB matches HOST
110 if [ -z "${HOST%%*$GLOB}" ]; then
115 if valid_ipv4
$GLOB; then
116 for HOST_IP
in $
(getent ahostsv4
$HOST |
grep ' STREAM ' | cut
-d ' ' -f 1) ; do
117 if valid_ipv4
$HOST_IP; then
118 match_ipv4
$GLOB $HOST_IP
119 if [ $?
-eq 0 ]; then
129 # If no proxy is set or needed, just connect directly
132 [ -z "${ALL_PROXY}" ] && ALL_PROXY
=$all_proxy
133 [ -z "${ALL_PROXY}" ] && ALL_PROXY
=$http_proxy
135 if [ -z "$ALL_PROXY" ]; then
136 exec $SOCAT STDIO
$METHOD
139 # Connect directly to hosts in NO_PROXY
140 for H
in $
( echo "$NO_PROXY" |
tr ',' ' ' ); do
141 if match_host
$1 $H; then
142 exec $SOCAT STDIO
$METHOD
146 # Proxy is necessary, determine protocol, server, and port
148 PROTO
=${ALL_PROXY%://*}
149 # strip protocol:// from string
150 ALL_PROXY
=${ALL_PROXY#*://}
151 # extract host & port parts:
152 # 1) drop username/password
153 PROXY
=${ALL_PROXY##*@}
154 # 2) remove optional trailing /?
156 # 3) extract optional port
158 if [ "$PORT" = "$PROXY" ]; then
164 # extract username & password
165 PROXYAUTH
="${ALL_PROXY%@*}"
166 [ "$PROXYAUTH" = "$ALL_PROXY" ] && PROXYAUTH
=
167 [ -n "${PROXYAUTH}" ] && PROXYAUTH
=",proxyauth=${PROXYAUTH}"
169 if [ "$PROTO" = "socks" ] ||
[ "$PROTO" = "socks4a" ]; then
170 if [ -z "$PORT" ]; then
173 METHOD
="SOCKS4A:$PROXY:$1:$2,socksport=$PORT"
174 elif [ "$PROTO" = "socks4" ]; then
175 if [ -z "$PORT" ]; then
178 METHOD
="SOCKS4:$PROXY:$1:$2,socksport=$PORT"
180 # Assume PROXY (http, https, etc)
181 if [ -z "$PORT" ]; then
184 METHOD
="PROXY:$PROXY:$1:$2,proxyport=${PORT}${PROXYAUTH}"
187 exec $SOCAT STDIO
"$METHOD"