blob: bb2ed2a46e45deef0f67a4f54014d8cf659ab333 [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.
Brad Bishopc342db32019-05-15 21:57:59 -040016#
17# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018#
19# AUTHORS
20# Darren Hart <dvhart@linux.intel.com>
21
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022if [ $# -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
41fi
42
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043# Locate the netcat binary
44SOCAT=$(which socat 2>/dev/null)
45if [ $? -ne 0 ]; then
46 echo "ERROR: socat binary not in PATH" 1>&2
47 exit 1
48fi
49METHOD=""
50
51# Test for a valid IPV4 quad with optional bitmask
52valid_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
58ipv4_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
70match_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 Williamsf1e5d692016-03-30 15:21:19 -050080 [ "$BITS" != "$CIDR" ] || BITS=32
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 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
101match_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 Williamsc0f7c042017-02-23 20:41:17 -0600111 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 Williamsc124f4f2015-09-15 14:41:29 -0500117 fi
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600118 done
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119 fi
120
121 return 1
122}
123
124# If no proxy is set or needed, just connect directly
125METHOD="TCP:$1:$2"
126
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500127[ -z "${ALL_PROXY}" ] && ALL_PROXY=$all_proxy
128[ -z "${ALL_PROXY}" ] && ALL_PROXY=$http_proxy
129
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500130if [ -z "$ALL_PROXY" ]; then
131 exec $SOCAT STDIO $METHOD
132fi
133
134# Connect directly to hosts in NO_PROXY
Andrew Geissler99467da2019-02-25 18:54:23 -0600135for H in "${NO_PROXY//,/ }"; do
136 if match_host $1 "$H"; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137 exec $SOCAT STDIO $METHOD
138 fi
139done
140
141# Proxy is necessary, determine protocol, server, and port
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500142# extract protocol
143PROTO=${ALL_PROXY%://*}
144# strip protocol:// from string
145ALL_PROXY=${ALL_PROXY#*://}
146# extract host & port parts:
147# 1) drop username/password
148PROXY=${ALL_PROXY##*@}
149# 2) remove optional trailing /?
150PROXY=${PROXY%%/*}
151# 3) extract optional port
152PORT=${PROXY##*:}
153if [ "$PORT" = "$PROXY" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500154 PORT=""
155fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500156# 4) remove port
157PROXY=${PROXY%%:*}
158
159# extract username & password
160PROXYAUTH="${ALL_PROXY%@*}"
161[ "$PROXYAUTH" = "$ALL_PROXY" ] && PROXYAUTH=
162[ -n "${PROXYAUTH}" ] && PROXYAUTH=",proxyauth=${PROXYAUTH}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500163
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500164if [ "$PROTO" = "socks" ] || [ "$PROTO" = "socks4a" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500165 if [ -z "$PORT" ]; then
166 PORT="1080"
167 fi
168 METHOD="SOCKS4A:$PROXY:$1:$2,socksport=$PORT"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500169elif [ "$PROTO" = "socks4" ]; then
170 if [ -z "$PORT" ]; then
171 PORT="1080"
172 fi
173 METHOD="SOCKS4:$PROXY:$1:$2,socksport=$PORT"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500174else
175 # Assume PROXY (http, https, etc)
176 if [ -z "$PORT" ]; then
177 PORT="8080"
178 fi
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500179 METHOD="PROXY:$PROXY:$1:$2,proxyport=${PORT}${PROXYAUTH}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180fi
181
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500182exec $SOCAT STDIO "$METHOD"