oe-git-proxy 4.4 KB

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