# HG changeset patch # User "Yann E. MORIN" # Date 1209245465 0 # Node ID 372b2f397baae5fb4f979ef5b912c22353734560 # Parent 2969b1d3884a873b5f0d6e16ee9e94c954c79843 Configure tsocks with a simple heuristic. Consider the proxy has to be in a 'local' network. It means it is directly reachable by the local machine, even if the local machine has to hop through one or more gates to reach the proxy (often the case in enterprise networks where class A 10.0.0.0/8 is in fact sub-divided into smaller networks, each one of them in a different location, eg. 10.1.0.0/16 in a place, while 10.2.0.0/16 would be on the other side of the world). Not being in the same subnet does not mean the proxy is not available. So we will build a mask with at most high bits set, which defines a network that has both the local machine and the proxy. Because a machine may have more than one interface, build a mask for each of them, removing 127.0.0.1 which is added automagically by tsocks, and removing duplicate masks. If all of this does not work, then it means the local machine can NOT in fact reach the proxy, which in turn means the user mis-configured something (most probably a typo...). /trunk/scripts/crosstool.sh | 61 52 9 0 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 9 deletions(-) diff -r 2969b1d3884a -r 372b2f397baa scripts/crosstool.sh --- a/scripts/crosstool.sh Sun Apr 20 18:04:20 2008 +0000 +++ b/scripts/crosstool.sh Sat Apr 26 21:31:05 2008 +0000 @@ -226,19 +226,62 @@ ;; sockssys) CT_HasOrAbort tsocks + # Force not using HTTP proxy + unset http_proxy ftp_proxy https_proxy . tsocks -on ;; socks*) CT_HasOrAbort tsocks + # Force not using HTTP proxy + unset http_proxy ftp_proxy https_proxy # Remove any lingering config file from any previous run rm -f "${CT_BUILD_DIR}/tsocks.conf" # Find all interfaces and build locally accessible networks - /sbin/ifconfig |egrep 'inet addr' |while read inet addr bcast mask; do - ip="${addr/*:}" - mask="${mask/*:}" - [ -n "${mask}" -a "${ip}" != "127.0.0.1" ] && echo "local = ${ip}/${mask}" - done >"${CT_BUILD_DIR}/tsocks.conf" - ( echo "server = ${CT_PROXY_HOST}"; + server_ip=$(ping -c 1 -W 2 "${CT_PROXY_HOST}" |head -n 1 |sed -r -e 's/^[^\(]+\(([^\)]+)\).*$/\1/;' || true) + CT_TestOrAbort "SOCKS proxy '${CT_PROXY_HOST}' has no IP." -n "${server_ip}" + /sbin/ifconfig |gawk -v server_ip="${server_ip}" ' + BEGIN { + split( server_ip, tmp, "\\." ); + server_ip_num = tmp[1] * 2^24 + tmp[2] * 2^16 + tmp[3] * 2^8 + tmp[4] * 2^0; + pairs = 0; + } + + $0 ~ /^[[:space:]]*inet addr:/ { + split( $2, tmp, ":|\\." ); + if( ( tmp[2] == 127 ) && ( tmp[3] == 0 ) && ( tmp[4] == 0 ) && ( tmp[5] == 1 ) ) { + /* Skip 127.0.0.1, it'\''s taken care of by tsocks itself */ + next; + } + ip_num = tmp[2] * 2^24 + tmp[3] * 2^16 + tmp[4] * 2 ^8 + tmp[5] * 2^0; + i = 32; + do { + i--; + mask = 2^32 - 2^i; + } while( (i!=0) && ( and( server_ip_num, mask ) == and( ip_num, mask ) ) ); + mask = and( 0xFFFFFFFF, lshift( mask, 1 ) ); + if( (i!=0) && (mask!=0) ) { + masked_ip = and( ip_num, mask ); + for( i=0; i"${CT_BUILD_DIR}/tsocks.conf" + ( echo "server = ${server_ip}"; echo "server_port = ${CT_PROXY_PORT}"; [ -n "${CT_PROXY_USER}" ] && echo "default_user=${CT_PROXY_USER}"; [ -n "${CT_PROXY_PASS}" ] && echo "default_pass=${CT_PROXY_PASS}"; @@ -246,10 +289,10 @@ case "${CT_PROXY_TYPE/socks}" in 4|5) proxy_type="${CT_PROXY_TYPE/socks}";; auto) - reply=$(inspectsocks "${CT_PROXY_HOST}" "${CT_PROXY_PORT}" 2>&1 || true) + reply=$(inspectsocks "${server_ip}" "${CT_PROXY_PORT}" 2>&1 || true) case "${reply}" in - *"server is a version 4 socks server"*) proxy_type=4;; - *"server is a version 5 socks server"*) proxy_type=5;; + *"server is a version 4 socks server") proxy_type=4;; + *"server is a version 5 socks server") proxy_type=5;; *) CT_Abort "Unable to determine SOCKS proxy type for '${CT_PROXY_HOST}:${CT_PROXY_PORT}'" esac ;;