Configure tsocks with a simple heuristic.
author"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sat Apr 26 21:31:05 2008 +0000 (2008-04-26)
changeset 454372b2f397baa
parent 453 2969b1d3884a
child 455 2d42e569debc
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(-)
scripts/crosstool.sh
     1.1 --- a/scripts/crosstool.sh	Sun Apr 20 18:04:20 2008 +0000
     1.2 +++ b/scripts/crosstool.sh	Sat Apr 26 21:31:05 2008 +0000
     1.3 @@ -226,19 +226,62 @@
     1.4      ;;
     1.5    sockssys)
     1.6      CT_HasOrAbort tsocks
     1.7 +    # Force not using HTTP proxy
     1.8 +    unset http_proxy ftp_proxy https_proxy
     1.9      . tsocks -on
    1.10      ;;
    1.11    socks*)
    1.12      CT_HasOrAbort tsocks
    1.13 +    # Force not using HTTP proxy
    1.14 +    unset http_proxy ftp_proxy https_proxy
    1.15      # Remove any lingering config file from any previous run
    1.16      rm -f "${CT_BUILD_DIR}/tsocks.conf"
    1.17      # Find all interfaces and build locally accessible networks
    1.18 -    /sbin/ifconfig |egrep 'inet addr' |while read inet addr bcast mask; do
    1.19 -      ip="${addr/*:}"
    1.20 -      mask="${mask/*:}"
    1.21 -      [ -n "${mask}" -a "${ip}" != "127.0.0.1" ] && echo "local = ${ip}/${mask}"
    1.22 -    done >"${CT_BUILD_DIR}/tsocks.conf"
    1.23 -    ( echo "server = ${CT_PROXY_HOST}";
    1.24 +    server_ip=$(ping -c 1 -W 2 "${CT_PROXY_HOST}" |head -n 1 |sed -r -e 's/^[^\(]+\(([^\)]+)\).*$/\1/;' || true)
    1.25 +    CT_TestOrAbort "SOCKS proxy '${CT_PROXY_HOST}' has no IP." -n "${server_ip}"
    1.26 +    /sbin/ifconfig |gawk -v server_ip="${server_ip}" '
    1.27 +      BEGIN {
    1.28 +        split( server_ip, tmp, "\\." );
    1.29 +        server_ip_num = tmp[1] * 2^24 + tmp[2] * 2^16 + tmp[3] * 2^8 + tmp[4] * 2^0;
    1.30 +        pairs = 0;
    1.31 +      }
    1.32 +
    1.33 +      $0 ~ /^[[:space:]]*inet addr:/ {
    1.34 +        split( $2, tmp, ":|\\." );
    1.35 +        if( ( tmp[2] == 127 ) && ( tmp[3] == 0 ) && ( tmp[4] == 0 ) && ( tmp[5] == 1 ) ) {
    1.36 +          /* Skip 127.0.0.1, it'\''s taken care of by tsocks itself */
    1.37 +          next;
    1.38 +        }
    1.39 +        ip_num = tmp[2] * 2^24 + tmp[3] * 2^16 + tmp[4] * 2 ^8 + tmp[5] * 2^0;
    1.40 +        i = 32;
    1.41 +        do {
    1.42 +          i--;
    1.43 +          mask = 2^32 - 2^i;
    1.44 +        } while( (i!=0) && ( and( server_ip_num, mask ) == and( ip_num, mask ) ) );
    1.45 +        mask = and( 0xFFFFFFFF, lshift( mask, 1 ) );
    1.46 +        if( (i!=0) && (mask!=0) ) {
    1.47 +          masked_ip = and( ip_num, mask );
    1.48 +          for( i=0; i<pairs; i++ ) {
    1.49 +            if( ( masked_ip == ips[i] ) && ( mask == masks[i] ) ) {
    1.50 +              next;
    1.51 +            }
    1.52 +          }
    1.53 +          ips[pairs] = masked_ip;
    1.54 +          masks[pairs] = mask;
    1.55 +          pairs++;
    1.56 +          printf( "local = %d.%d.%d.%d/%d.%d.%d.%d\n",
    1.57 +                  and( 0xFF, masked_ip / 2^24 ),
    1.58 +                  and( 0xFF, masked_ip / 2^16 ),
    1.59 +                  and( 0xFF, masked_ip / 2^8 ),
    1.60 +                  and( 0xFF, masked_ip / 2^0 ),
    1.61 +                  and( 0xFF, mask / 2^24 ),
    1.62 +                  and( 0xFF, mask / 2^16 ),
    1.63 +                  and( 0xFF, mask / 2^8 ),
    1.64 +                  and( 0xFF, mask / 2^0 ) );
    1.65 +        }
    1.66 +      }
    1.67 +    ' >"${CT_BUILD_DIR}/tsocks.conf"
    1.68 +    ( echo "server = ${server_ip}";
    1.69        echo "server_port = ${CT_PROXY_PORT}";
    1.70        [ -n "${CT_PROXY_USER}"   ] && echo "default_user=${CT_PROXY_USER}";
    1.71        [ -n "${CT_PROXY_PASS}" ] && echo "default_pass=${CT_PROXY_PASS}";
    1.72 @@ -246,10 +289,10 @@
    1.73      case "${CT_PROXY_TYPE/socks}" in
    1.74        4|5) proxy_type="${CT_PROXY_TYPE/socks}";;
    1.75        auto)
    1.76 -        reply=$(inspectsocks "${CT_PROXY_HOST}" "${CT_PROXY_PORT}" 2>&1 || true)
    1.77 +        reply=$(inspectsocks "${server_ip}" "${CT_PROXY_PORT}" 2>&1 || true)
    1.78          case "${reply}" in
    1.79 -          *"server is a version 4 socks server"*) proxy_type=4;;
    1.80 -          *"server is a version 5 socks server"*) proxy_type=5;;
    1.81 +          *"server is a version 4 socks server") proxy_type=4;;
    1.82 +          *"server is a version 5 socks server") proxy_type=5;;
    1.83            *) CT_Abort "Unable to determine SOCKS proxy type for '${CT_PROXY_HOST}:${CT_PROXY_PORT}'"
    1.84          esac
    1.85        ;;