oe-git-proxy 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. if [ $# -lt 2 -o "$1" = '--help' -o "$1" = '-h' ] ; then
  21. echo 'oe-git-proxy: error: the following arguments are required: host port'
  22. echo 'Usage: oe-git-proxy host port'
  23. echo ''
  24. echo 'OpenEmbedded git-proxy - a simple tool to be used via GIT_PROXY_COMMAND.'
  25. echo 'It uses socat to make SOCKS or HTTPS proxy connections.'
  26. echo 'It uses ALL_PROXY to determine the proxy server, protocol, and port.'
  27. echo 'It uses NO_PROXY to skip using the proxy for a comma delimited list'
  28. echo 'of hosts, host globs (*.example.com), IPs, or CIDR masks (192.168.1.0/24).'
  29. echo 'It is known to work with both bash and dash shells.runs native tools'
  30. echo ''
  31. echo 'arguments:'
  32. echo ' host proxy host to use'
  33. echo ' port proxy port to use'
  34. echo ''
  35. echo 'options:'
  36. echo ' -h, --help show this help message and exit'
  37. echo ''
  38. exit 2
  39. fi
  40. # Locate the netcat binary
  41. SOCAT=$(which socat 2>/dev/null)
  42. if [ $? -ne 0 ]; then
  43. echo "ERROR: socat binary not in PATH" 1>&2
  44. exit 1
  45. fi
  46. METHOD=""
  47. # Test for a valid IPV4 quad with optional bitmask
  48. valid_ipv4() {
  49. 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]))?$"
  50. return $?
  51. }
  52. # Convert an IPV4 address into a 32bit integer
  53. ipv4_val() {
  54. IP="$1"
  55. SHIFT=24
  56. VAL=0
  57. for B in ${IP//./ }; do
  58. VAL=$(($VAL+$(($B<<$SHIFT))))
  59. SHIFT=$(($SHIFT-8))
  60. done
  61. echo "$VAL"
  62. }
  63. # Determine if two IPs are equivalent, or if the CIDR contains the IP
  64. match_ipv4() {
  65. CIDR=$1
  66. IP=$2
  67. if [ -z "${IP%%$CIDR}" ]; then
  68. return 0
  69. fi
  70. # Determine the mask bitlength
  71. BITS=${CIDR##*/}
  72. [ "$BITS" != "$CIDR" ] || BITS=32
  73. if [ -z "$BITS" ]; then
  74. return 1
  75. fi
  76. IPVAL=$(ipv4_val $IP)
  77. IP2VAL=$(ipv4_val ${CIDR%%/*})
  78. # OR in the unmasked bits
  79. for i in $(seq 0 $((32-$BITS))); do
  80. IP2VAL=$(($IP2VAL|$((1<<$i))))
  81. IPVAL=$(($IPVAL|$((1<<$i))))
  82. done
  83. if [ $IPVAL -eq $IP2VAL ]; then
  84. return 0
  85. fi
  86. return 1
  87. }
  88. # Test to see if GLOB matches HOST
  89. match_host() {
  90. HOST=$1
  91. GLOB=$2
  92. if [ -z "${HOST%%$GLOB}" ]; then
  93. return 0
  94. fi
  95. # Match by netmask
  96. if valid_ipv4 $GLOB; then
  97. for HOST_IP in $(getent ahostsv4 $HOST | grep ' STREAM ' | cut -d ' ' -f 1) ; do
  98. if valid_ipv4 $HOST_IP; then
  99. match_ipv4 $GLOB $HOST_IP
  100. if [ $? -eq 0 ]; then
  101. return 0
  102. fi
  103. fi
  104. done
  105. fi
  106. return 1
  107. }
  108. # If no proxy is set or needed, just connect directly
  109. METHOD="TCP:$1:$2"
  110. [ -z "${ALL_PROXY}" ] && ALL_PROXY=$all_proxy
  111. [ -z "${ALL_PROXY}" ] && ALL_PROXY=$http_proxy
  112. if [ -z "$ALL_PROXY" ]; then
  113. exec $SOCAT STDIO $METHOD
  114. fi
  115. # Connect directly to hosts in NO_PROXY
  116. for H in "${NO_PROXY//,/ }"; do
  117. if match_host $1 "$H"; then
  118. exec $SOCAT STDIO $METHOD
  119. fi
  120. done
  121. # Proxy is necessary, determine protocol, server, and port
  122. # extract protocol
  123. PROTO=${ALL_PROXY%://*}
  124. # strip protocol:// from string
  125. ALL_PROXY=${ALL_PROXY#*://}
  126. # extract host & port parts:
  127. # 1) drop username/password
  128. PROXY=${ALL_PROXY##*@}
  129. # 2) remove optional trailing /?
  130. PROXY=${PROXY%%/*}
  131. # 3) extract optional port
  132. PORT=${PROXY##*:}
  133. if [ "$PORT" = "$PROXY" ]; then
  134. PORT=""
  135. fi
  136. # 4) remove port
  137. PROXY=${PROXY%%:*}
  138. # extract username & password
  139. PROXYAUTH="${ALL_PROXY%@*}"
  140. [ "$PROXYAUTH" = "$ALL_PROXY" ] && PROXYAUTH=
  141. [ -n "${PROXYAUTH}" ] && PROXYAUTH=",proxyauth=${PROXYAUTH}"
  142. if [ "$PROTO" = "socks" ] || [ "$PROTO" = "socks4a" ]; then
  143. if [ -z "$PORT" ]; then
  144. PORT="1080"
  145. fi
  146. METHOD="SOCKS4A:$PROXY:$1:$2,socksport=$PORT"
  147. elif [ "$PROTO" = "socks4" ]; then
  148. if [ -z "$PORT" ]; then
  149. PORT="1080"
  150. fi
  151. METHOD="SOCKS4:$PROXY:$1:$2,socksport=$PORT"
  152. else
  153. # Assume PROXY (http, https, etc)
  154. if [ -z "$PORT" ]; then
  155. PORT="8080"
  156. fi
  157. METHOD="PROXY:$PROXY:$1:$2,proxyport=${PORT}${PROXYAUTH}"
  158. fi
  159. exec $SOCAT STDIO "$METHOD"