scripts/functions
changeset 754 b13657cd64b3
parent 738 e8c68d0d1a0c
child 767 fe5e42bf7bbc
     1.1 --- a/scripts/functions	Sun Jul 27 14:25:19 2008 +0000
     1.2 +++ b/scripts/functions	Fri Aug 01 09:23:58 2008 +0000
     1.3 @@ -288,6 +288,106 @@
     1.4      return 0
     1.5  }
     1.6  
     1.7 +# Set environment for proxy access
     1.8 +# Usage: CT_DoSetProxy <proxy_type>
     1.9 +# where proxy_type is one of 'none', 'http', 'sockssys', 'socks4' or 'socks5'
    1.10 +CT_DoSetProxy() {
    1.11 +    case "${CT_PROXY_TYPE}" in
    1.12 +        http)
    1.13 +            http_proxy="http://"
    1.14 +            case  "${CT_PROXY_USER}:${CT_PROXY_PASS}" in
    1.15 +                :)      ;;
    1.16 +                :*)     http_proxy="${http_proxy}:${CT_PROXY_PASS}@";;
    1.17 +                *:)     http_proxy="${http_proxy}${CT_PROXY_USER}@";;
    1.18 +                *:*)    http_proxy="${http_proxy}${CT_PROXY_USER}:${CT_PROXY_PASS}@";;
    1.19 +            esac
    1.20 +            export http_proxy="${http_proxy}${CT_PROXY_HOST}:${CT_PROXY_PORT}/"
    1.21 +            export https_proxy="${http_proxy}"
    1.22 +            export ftp_proxy="${http_proxy}"
    1.23 +            CT_DoLog DEBUG "http_proxy='${http_proxy}'"
    1.24 +            ;;
    1.25 +        sockssys)
    1.26 +            # Force not using HTTP proxy
    1.27 +            unset http_proxy ftp_proxy https_proxy
    1.28 +            CT_HasOrAbort tsocks
    1.29 +            . tsocks -on
    1.30 +            ;;
    1.31 +        socks*)
    1.32 +            # Force not using HTTP proxy
    1.33 +            unset http_proxy ftp_proxy https_proxy
    1.34 +            # Remove any lingering config file from any previous run
    1.35 +            rm -f "${CT_BUILD_DIR}/tsocks.conf"
    1.36 +            # Find all interfaces and build locally accessible networks
    1.37 +            server_ip=$(ping -c 1 -W 2 "${CT_PROXY_HOST}" |head -n 1 |sed -r -e 's/^[^\(]+\(([^\)]+)\).*$/\1/;' || true)
    1.38 +            CT_TestOrAbort "SOCKS proxy '${CT_PROXY_HOST}' has no IP." -n "${server_ip}"
    1.39 +            /sbin/ifconfig |awk -v server_ip="${server_ip}" '
    1.40 +                BEGIN {
    1.41 +                    split( server_ip, tmp, "\\." );
    1.42 +                    server_ip_num = tmp[1] * 2^24 + tmp[2] * 2^16 + tmp[3] * 2^8 + tmp[4] * 2^0;
    1.43 +                    pairs = 0;
    1.44 +                }
    1.45 +
    1.46 +                $0 ~ /^[[:space:]]*inet addr:/ {
    1.47 +                    split( $2, tmp, ":|\\." );
    1.48 +                    if( ( tmp[2] == 127 ) && ( tmp[3] == 0 ) && ( tmp[4] == 0 ) && ( tmp[5] == 1 ) ) {
    1.49 +                        /* Skip 127.0.0.1, it'\''s taken care of by tsocks itself */
    1.50 +                        next;
    1.51 +                    }
    1.52 +                    ip_num = tmp[2] * 2^24 + tmp[3] * 2^16 + tmp[4] * 2 ^8 + tmp[5] * 2^0;
    1.53 +                    i = 32;
    1.54 +                    do {
    1.55 +                        i--;
    1.56 +                        mask = 2^32 - 2^i;
    1.57 +                    } while( (i!=0) && ( and( server_ip_num, mask ) == and( ip_num, mask ) ) );
    1.58 +                    mask = and( 0xFFFFFFFF, lshift( mask, 1 ) );
    1.59 +                    if( (i!=0) && (mask!=0) ) {
    1.60 +                        masked_ip = and( ip_num, mask );
    1.61 +                        for( i=0; i<pairs; i++ ) {
    1.62 +                            if( ( masked_ip == ips[i] ) && ( mask == masks[i] ) ) {
    1.63 +                                next;
    1.64 +                            }
    1.65 +                        }
    1.66 +                        ips[pairs] = masked_ip;
    1.67 +                        masks[pairs] = mask;
    1.68 +                        pairs++;
    1.69 +                        printf( "local = %d.%d.%d.%d/%d.%d.%d.%d\n",
    1.70 +                                and( 0xFF, masked_ip / 2^24 ),
    1.71 +                                and( 0xFF, masked_ip / 2^16 ),
    1.72 +                                and( 0xFF, masked_ip / 2^8 ),
    1.73 +                                and( 0xFF, masked_ip / 2^0 ),
    1.74 +                                and( 0xFF, mask / 2^24 ),
    1.75 +                                and( 0xFF, mask / 2^16 ),
    1.76 +                                and( 0xFF, mask / 2^8 ),
    1.77 +                                and( 0xFF, mask / 2^0 ) );
    1.78 +                    }
    1.79 +                }
    1.80 +            ' >"${CT_BUILD_DIR}/tsocks.conf"
    1.81 +            ( echo "server = ${server_ip}";
    1.82 +              echo "server_port = ${CT_PROXY_PORT}";
    1.83 +              [ -n "${CT_PROXY_USER}"   ] && echo "default_user=${CT_PROXY_USER}";
    1.84 +              [ -n "${CT_PROXY_PASS}" ] && echo "default_pass=${CT_PROXY_PASS}";
    1.85 +            ) >>"${CT_BUILD_DIR}/tsocks.conf"
    1.86 +            case "${CT_PROXY_TYPE/socks}" in
    1.87 +                4|5) proxy_type="${CT_PROXY_TYPE/socks}";;
    1.88 +                auto)
    1.89 +                    reply=$(inspectsocks "${server_ip}" "${CT_PROXY_PORT}" 2>&1 || true)
    1.90 +                    case "${reply}" in
    1.91 +                        *"server is a version 4 socks server") proxy_type=4;;
    1.92 +                        *"server is a version 5 socks server") proxy_type=5;;
    1.93 +                        *) CT_Abort "Unable to determine SOCKS proxy type for '${CT_PROXY_HOST}:${CT_PROXY_PORT}'"
    1.94 +                    esac
    1.95 +                    ;;
    1.96 +            esac
    1.97 +            echo "server_type = ${proxy_type}" >> "${CT_BUILD_DIR}/tsocks.conf"
    1.98 +            CT_HasOrAbort tsocks
    1.99 +            # If tsocks was found, then validateconf is present (distributed with tsocks).
   1.100 +            CT_DoExecLog DEBUG validateconf -f "${CT_BUILD_DIR}/tsocks.conf"
   1.101 +            export TSOCKS_CONF_FILE="${CT_BUILD_DIR}/tsocks.conf"
   1.102 +            . tsocks -on
   1.103 +            ;;
   1.104 +    esac
   1.105 +}
   1.106 +
   1.107  # Download an URL using wget
   1.108  # Usage: CT_DoGetFileWget <URL>
   1.109  CT_DoGetFileWget() {
   1.110 @@ -353,35 +453,39 @@
   1.111          return 0
   1.112      fi
   1.113  
   1.114 +    # Try to retrieve the file
   1.115      CT_Pushd "${CT_TARBALLS_DIR}"
   1.116 -    # We'd rather have a bzip2'ed tarball, then gzipped tarball, plain tarball,
   1.117 -    # or, as a failover, a file without extension.
   1.118 -    # Try local copy first, if it exists
   1.119 -    for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
   1.120 -        CT_DoLog DEBUG "Trying '${CT_LOCAL_TARBALLS_DIR}/${file}${ext}'"
   1.121 -        if [ -r "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" -a \
   1.122 -             "${CT_FORCE_DOWNLOAD}" != "y" ]; then
   1.123 -            CT_DoLog EXTRA "Using '${file}' from local storage"
   1.124 -            ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
   1.125 -            return 0
   1.126 -        fi
   1.127 -    done
   1.128 +
   1.129 +    if [ -n "${CT_LOCAL_TARBALLS_DIR}" ]; then
   1.130 +        CT_DoLog DEBUG "Trying to retrieve an already downloaded copy of '${file}'"
   1.131 +        # We'd rather have a bzip2'ed tarball, then gzipped tarball, plain tarball,
   1.132 +        # or, as a failover, a file without extension.
   1.133 +        # Try local copy first, if it exists
   1.134 +        for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
   1.135 +            CT_DoLog DEBUG "Trying '${CT_LOCAL_TARBALLS_DIR}/${file}${ext}'"
   1.136 +            if [ -r "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" -a \
   1.137 +                 "${CT_FORCE_DOWNLOAD}" != "y" ]; then
   1.138 +                CT_DoLog EXTRA "Got '${file}' from local storage"
   1.139 +                ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
   1.140 +                return 0
   1.141 +            fi
   1.142 +        done
   1.143 +    fi
   1.144  
   1.145      # Not found locally, try from the network
   1.146 -    CT_DoLog EXTRA "Retrieving '${file}' from network"
   1.147  
   1.148      # Start with LAN mirror
   1.149      if [ "${CT_USE_LAN_MIRROR}" = "y" ]; then
   1.150 -        LAN_URLs=
   1.151 -        for pat in ${CT_LAN_MIRROR_PATTERNS}; do
   1.152 +        CT_DoSetProxy ${CT_LAN_MIRROR_USE_PROXY:+${CT_PROXY_TYPE}}
   1.153 +        CT_DoLog DEBUG "Trying to retrieve a copy of '${file}' from LAN mirror '${CT_LAN_MIRROR_HOSTNAME}'"
   1.154 +        CT_TestOrAbort "Please set the LAN mirror hostname" -n "${CT_LAN_MIRROR_HOSTNAME}"
   1.155 +        CT_TestOrAbort "Please tell me where to find tarballs on the LAN mirror '${CT_LAN_MIRROR_HOSTNAME}'" -n "${CT_LAN_MIRROR_BASE}"
   1.156 +        for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
   1.157              # Please note: we just have the file's basename in a single piece.
   1.158              # So we have to just try and split it back into name and version... :-(
   1.159 -            pat="${pat//\%pkg/${file%-*}}"
   1.160 -            pat="${pat//\%ver/${file##*-}}"
   1.161 -            LAN_URLs="${LAN_URLs} ${CT_LAN_MIRROR_SCHEME}://${CT_LAN_MIRROR_HOSTNAME}/${pat}"
   1.162 -        done
   1.163 -        for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
   1.164 -            for url in ${LAN_URLs}; do
   1.165 +            for url in "${CT_LAN_MIRROR_SCHEME}://${CT_LAN_MIRROR_HOSTNAME}/${CT_LAN_MIRROR_BASE}/${file%-*}"  \
   1.166 +                       "${CT_LAN_MIRROR_SCHEME}://${CT_LAN_MIRROR_HOSTNAME}/${CT_LAN_MIRROR_BASE}";            \
   1.167 +            do
   1.168                  CT_DoLog DEBUG "Trying '${url}/${file}${ext}'"
   1.169                  CT_DoGetFile "${url}/${file}${ext}"
   1.170                  if [ -f "${file}${ext}" ]; then
   1.171 @@ -393,6 +497,7 @@
   1.172                          mv "${file}${ext}" "${CT_LOCAL_TARBALLS_DIR}" |CT_DoLog ALL
   1.173                          ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
   1.174                      fi
   1.175 +                    CT_DoLog EXTRA "Got '${file}' from the LAN mirror"
   1.176                      return 0
   1.177                  fi
   1.178              done
   1.179 @@ -400,6 +505,7 @@
   1.180      fi
   1.181  
   1.182      # OK, available neither localy, nor from the LAN mirror (if any).
   1.183 +    CT_DoSetProxy ${CT_PROXY_TYPE}
   1.184      for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
   1.185          # Try all urls in turn
   1.186          for url in "$@"; do
   1.187 @@ -414,13 +520,14 @@
   1.188                      mv "${file}${ext}" "${CT_LOCAL_TARBALLS_DIR}" |CT_DoLog ALL
   1.189                      ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
   1.190                  fi
   1.191 +                CT_DoLog EXTRA "Got '${file}' from the Internet"
   1.192                  return 0
   1.193              fi
   1.194          done
   1.195      done
   1.196      CT_Popd
   1.197  
   1.198 -    CT_Abort "Could not download '${file}', and not present in '${CT_LOCAL_TARBALLS_DIR}'"
   1.199 +    CT_Abort "Could not retrieve '${file}'."
   1.200  }
   1.201  
   1.202  # Extract a tarball and patch the resulting sources if necessary.