oe-git-proxy 3.5 KB

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