oe-git-proxy 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/bash
  2. # oe-git-proxy is a simple tool to be via GIT_PROXY_COMMAND. It uses socat
  3. # to make SOCKS5 or HTTPS proxy connections.
  4. # It uses ALL_PROXY or all_proxy or http_proxy to determine the proxy server,
  5. # protocol, and port.
  6. # It uses NO_PROXY to skip using the proxy for a comma delimited list of
  7. # hosts, host globs (*.example.com), IPs, or CIDR masks (192.168.1.0/24). It
  8. # is known to work with both bash and dash shells.
  9. #
  10. # Example ALL_PROXY values:
  11. # ALL_PROXY=socks://socks.example.com:1080
  12. # ALL_PROXY=https://proxy.example.com:8080
  13. #
  14. # Copyright (c) 2013, Intel Corporation.
  15. #
  16. # SPDX-License-Identifier: GPL-2.0-only
  17. #
  18. # AUTHORS
  19. # Darren Hart <dvhart@linux.intel.com>
  20. # disable pathname expansion, NO_PROXY fields could start with "*" or be it
  21. set -f
  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. # Locate the netcat binary
  43. if [ -z "$SOCAT" ]; then
  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. fi
  50. METHOD=""
  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. # Convert an IPV4 address into a 32bit integer
  57. ipv4_val() {
  58. IP="$1"
  59. SHIFT=24
  60. VAL=0
  61. for B in $( echo "$IP" | tr '.' ' ' ); do
  62. VAL=$(($VAL+$(($B<<$SHIFT))))
  63. SHIFT=$(($SHIFT-8))
  64. done
  65. echo "$VAL"
  66. }
  67. # Determine if two IPs are equivalent, or if the CIDR contains the IP
  68. match_ipv4() {
  69. CIDR=$1
  70. IP=$2
  71. if [ -z "${IP%%$CIDR}" ]; then
  72. return 0
  73. fi
  74. # Determine the mask bitlength
  75. BITS=${CIDR##*/}
  76. [ "$BITS" != "$CIDR" ] || BITS=32
  77. if [ -z "$BITS" ]; then
  78. return 1
  79. fi
  80. IPVAL=$(ipv4_val $IP)
  81. IP2VAL=$(ipv4_val ${CIDR%%/*})
  82. # OR in the unmasked bits
  83. for i in $(seq 0 $((32-$BITS))); do
  84. IP2VAL=$(($IP2VAL|$((1<<$i))))
  85. IPVAL=$(($IPVAL|$((1<<$i))))
  86. done
  87. if [ $IPVAL -eq $IP2VAL ]; then
  88. return 0
  89. fi
  90. return 1
  91. }
  92. # Test to see if GLOB matches HOST
  93. match_host() {
  94. HOST=$1
  95. GLOB=$2
  96. if [ -z "${HOST%%*$GLOB}" ]; then
  97. return 0
  98. fi
  99. # Match by netmask
  100. if valid_ipv4 $GLOB; then
  101. for HOST_IP in $(getent ahostsv4 $HOST | grep ' STREAM ' | cut -d ' ' -f 1) ; do
  102. if valid_ipv4 $HOST_IP; then
  103. match_ipv4 $GLOB $HOST_IP
  104. if [ $? -eq 0 ]; then
  105. return 0
  106. fi
  107. fi
  108. done
  109. fi
  110. return 1
  111. }
  112. # If no proxy is set or needed, just connect directly
  113. METHOD="TCP:$1:$2"
  114. [ -z "${ALL_PROXY}" ] && ALL_PROXY=$all_proxy
  115. [ -z "${ALL_PROXY}" ] && ALL_PROXY=$http_proxy
  116. if [ -z "$ALL_PROXY" ]; then
  117. exec $SOCAT STDIO $METHOD
  118. fi
  119. # Connect directly to hosts in NO_PROXY
  120. for H in $( echo "$NO_PROXY" | tr ',' ' ' ); do
  121. if match_host $1 $H; then
  122. exec $SOCAT STDIO $METHOD
  123. fi
  124. done
  125. # Proxy is necessary, determine protocol, server, and port
  126. # extract protocol
  127. PROTO=${ALL_PROXY%://*}
  128. # strip protocol:// from string
  129. ALL_PROXY=${ALL_PROXY#*://}
  130. # extract host & port parts:
  131. # 1) drop username/password
  132. PROXY=${ALL_PROXY##*@}
  133. # 2) remove optional trailing /?
  134. PROXY=${PROXY%%/*}
  135. # 3) extract optional port
  136. PORT=${PROXY##*:}
  137. if [ "$PORT" = "$PROXY" ]; then
  138. PORT=""
  139. fi
  140. # 4) remove port
  141. PROXY=${PROXY%%:*}
  142. # extract username & password
  143. PROXYAUTH="${ALL_PROXY%@*}"
  144. [ "$PROXYAUTH" = "$ALL_PROXY" ] && PROXYAUTH=
  145. [ -n "${PROXYAUTH}" ] && PROXYAUTH=",proxyauth=${PROXYAUTH}"
  146. if [ "$PROTO" = "socks" ] || [ "$PROTO" = "socks4a" ]; then
  147. if [ -z "$PORT" ]; then
  148. PORT="1080"
  149. fi
  150. METHOD="SOCKS4A:$PROXY:$1:$2,socksport=$PORT"
  151. elif [ "$PROTO" = "socks4" ]; then
  152. if [ -z "$PORT" ]; then
  153. PORT="1080"
  154. fi
  155. METHOD="SOCKS4:$PROXY:$1:$2,socksport=$PORT"
  156. else
  157. # Assume PROXY (http, https, etc)
  158. if [ -z "$PORT" ]; then
  159. PORT="8080"
  160. fi
  161. METHOD="PROXY:$PROXY:$1:$2,proxyport=${PORT}${PROXYAUTH}"
  162. fi
  163. exec $SOCAT STDIO "$METHOD"