summaryrefslogtreecommitdiff
path: root/scripts/functions
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/functions')
-rw-r--r--scripts/functions151
1 files changed, 129 insertions, 22 deletions
diff --git a/scripts/functions b/scripts/functions
index d71a340..a494f62 100644
--- a/scripts/functions
+++ b/scripts/functions
@@ -288,6 +288,106 @@ CT_GetFileExtension() {
return 0
}
+# Set environment for proxy access
+# Usage: CT_DoSetProxy <proxy_type>
+# where proxy_type is one of 'none', 'http', 'sockssys', 'socks4' or 'socks5'
+CT_DoSetProxy() {
+ case "${CT_PROXY_TYPE}" in
+ http)
+ http_proxy="http://"
+ case "${CT_PROXY_USER}:${CT_PROXY_PASS}" in
+ :) ;;
+ :*) http_proxy="${http_proxy}:${CT_PROXY_PASS}@";;
+ *:) http_proxy="${http_proxy}${CT_PROXY_USER}@";;
+ *:*) http_proxy="${http_proxy}${CT_PROXY_USER}:${CT_PROXY_PASS}@";;
+ esac
+ export http_proxy="${http_proxy}${CT_PROXY_HOST}:${CT_PROXY_PORT}/"
+ export https_proxy="${http_proxy}"
+ export ftp_proxy="${http_proxy}"
+ CT_DoLog DEBUG "http_proxy='${http_proxy}'"
+ ;;
+ sockssys)
+ # Force not using HTTP proxy
+ unset http_proxy ftp_proxy https_proxy
+ CT_HasOrAbort tsocks
+ . tsocks -on
+ ;;
+ socks*)
+ # 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
+ 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 |awk -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<pairs; i++ ) {
+ if( ( masked_ip == ips[i] ) && ( mask == masks[i] ) ) {
+ next;
+ }
+ }
+ ips[pairs] = masked_ip;
+ masks[pairs] = mask;
+ pairs++;
+ printf( "local = %d.%d.%d.%d/%d.%d.%d.%d\n",
+ and( 0xFF, masked_ip / 2^24 ),
+ and( 0xFF, masked_ip / 2^16 ),
+ and( 0xFF, masked_ip / 2^8 ),
+ and( 0xFF, masked_ip / 2^0 ),
+ and( 0xFF, mask / 2^24 ),
+ and( 0xFF, mask / 2^16 ),
+ and( 0xFF, mask / 2^8 ),
+ and( 0xFF, mask / 2^0 ) );
+ }
+ }
+ ' >"${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}";
+ ) >>"${CT_BUILD_DIR}/tsocks.conf"
+ case "${CT_PROXY_TYPE/socks}" in
+ 4|5) proxy_type="${CT_PROXY_TYPE/socks}";;
+ auto)
+ 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;;
+ *) CT_Abort "Unable to determine SOCKS proxy type for '${CT_PROXY_HOST}:${CT_PROXY_PORT}'"
+ esac
+ ;;
+ esac
+ echo "server_type = ${proxy_type}" >> "${CT_BUILD_DIR}/tsocks.conf"
+ CT_HasOrAbort tsocks
+ # If tsocks was found, then validateconf is present (distributed with tsocks).
+ CT_DoExecLog DEBUG validateconf -f "${CT_BUILD_DIR}/tsocks.conf"
+ export TSOCKS_CONF_FILE="${CT_BUILD_DIR}/tsocks.conf"
+ . tsocks -on
+ ;;
+ esac
+}
+
# Download an URL using wget
# Usage: CT_DoGetFileWget <URL>
CT_DoGetFileWget() {
@@ -353,35 +453,39 @@ CT_GetFile() {
return 0
fi
+ # Try to retrieve the file
CT_Pushd "${CT_TARBALLS_DIR}"
- # We'd rather have a bzip2'ed tarball, then gzipped tarball, plain tarball,
- # or, as a failover, a file without extension.
- # Try local copy first, if it exists
- for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
- CT_DoLog DEBUG "Trying '${CT_LOCAL_TARBALLS_DIR}/${file}${ext}'"
- if [ -r "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" -a \
- "${CT_FORCE_DOWNLOAD}" != "y" ]; then
- CT_DoLog EXTRA "Using '${file}' from local storage"
- ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
- return 0
- fi
- done
+
+ if [ -n "${CT_LOCAL_TARBALLS_DIR}" ]; then
+ CT_DoLog DEBUG "Trying to retrieve an already downloaded copy of '${file}'"
+ # We'd rather have a bzip2'ed tarball, then gzipped tarball, plain tarball,
+ # or, as a failover, a file without extension.
+ # Try local copy first, if it exists
+ for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
+ CT_DoLog DEBUG "Trying '${CT_LOCAL_TARBALLS_DIR}/${file}${ext}'"
+ if [ -r "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" -a \
+ "${CT_FORCE_DOWNLOAD}" != "y" ]; then
+ CT_DoLog EXTRA "Got '${file}' from local storage"
+ ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
+ return 0
+ fi
+ done
+ fi
# Not found locally, try from the network
- CT_DoLog EXTRA "Retrieving '${file}' from network"
# Start with LAN mirror
if [ "${CT_USE_LAN_MIRROR}" = "y" ]; then
- LAN_URLs=
- for pat in ${CT_LAN_MIRROR_PATTERNS}; do
+ CT_DoSetProxy ${CT_LAN_MIRROR_USE_PROXY:+${CT_PROXY_TYPE}}
+ CT_DoLog DEBUG "Trying to retrieve a copy of '${file}' from LAN mirror '${CT_LAN_MIRROR_HOSTNAME}'"
+ CT_TestOrAbort "Please set the LAN mirror hostname" -n "${CT_LAN_MIRROR_HOSTNAME}"
+ CT_TestOrAbort "Please tell me where to find tarballs on the LAN mirror '${CT_LAN_MIRROR_HOSTNAME}'" -n "${CT_LAN_MIRROR_BASE}"
+ for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
# Please note: we just have the file's basename in a single piece.
# So we have to just try and split it back into name and version... :-(
- pat="${pat//\%pkg/${file%-*}}"
- pat="${pat//\%ver/${file##*-}}"
- LAN_URLs="${LAN_URLs} ${CT_LAN_MIRROR_SCHEME}://${CT_LAN_MIRROR_HOSTNAME}/${pat}"
- done
- for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
- for url in ${LAN_URLs}; do
+ for url in "${CT_LAN_MIRROR_SCHEME}://${CT_LAN_MIRROR_HOSTNAME}/${CT_LAN_MIRROR_BASE}/${file%-*}" \
+ "${CT_LAN_MIRROR_SCHEME}://${CT_LAN_MIRROR_HOSTNAME}/${CT_LAN_MIRROR_BASE}"; \
+ do
CT_DoLog DEBUG "Trying '${url}/${file}${ext}'"
CT_DoGetFile "${url}/${file}${ext}"
if [ -f "${file}${ext}" ]; then
@@ -393,6 +497,7 @@ CT_GetFile() {
mv "${file}${ext}" "${CT_LOCAL_TARBALLS_DIR}" |CT_DoLog ALL
ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
fi
+ CT_DoLog EXTRA "Got '${file}' from the LAN mirror"
return 0
fi
done
@@ -400,6 +505,7 @@ CT_GetFile() {
fi
# OK, available neither localy, nor from the LAN mirror (if any).
+ CT_DoSetProxy ${CT_PROXY_TYPE}
for ext in ${first_ext} .tar.bz2 .tar.gz .tgz .tar ''; do
# Try all urls in turn
for url in "$@"; do
@@ -414,13 +520,14 @@ CT_GetFile() {
mv "${file}${ext}" "${CT_LOCAL_TARBALLS_DIR}" |CT_DoLog ALL
ln -sv "${CT_LOCAL_TARBALLS_DIR}/${file}${ext}" "${file}${ext}" |CT_DoLog ALL
fi
+ CT_DoLog EXTRA "Got '${file}' from the Internet"
return 0
fi
done
done
CT_Popd
- CT_Abort "Could not download '${file}', and not present in '${CT_LOCAL_TARBALLS_DIR}'"
+ CT_Abort "Could not retrieve '${file}'."
}
# Extract a tarball and patch the resulting sources if necessary.