如果代理不可用,如何忽略代理?

Lek*_*eyn 18 apt networking proxy

按照在LAN 上缓存 apt 下载的最佳方式中的说明进行操作,我已经在本地网络中设置了缓存代理。由于那台机器并不总是启动,如果不可用,我希望能够在不使用该代理的情况下刷新源列表并安装包。

我已经阅读了手册页中的获取组部分apt.conf(5),但我找不到像“Silent-Fail”这样的选项。

目前,sudo apt-get update相关命令失败,因为无法建立连接。那么如何配置客户端,以便在代理不可用时忽略它呢?

Lek*_*eyn 23

有一个未记录的设置,Acquire::http::ProxyAutoDetect. 此设置应包含二进制文件的完整路径,并且不能有参数。该命令应输出要使用的代理(例如:)http://10.0.0.1:8000

鉴于上述信息,可以创建一个脚本,在设置代理之前尝试代理。如果没有可用的代理,则应使用直接连接。

下面是这样一个代理检测脚本,它尝试http://10.0.0.1:8000/http://10.0.0.2:8000代理。

将代码放入/etc/apt/detect-http-proxy

#!/bin/bash
# detect-http-proxy - Returns a HTTP proxy which is available for use

# Author: Lekensteyn <lekensteyn@gmail.com>

# Supported since APT 0.7.25.3ubuntu1 (Lucid) and 0.7.26~exp1 (Debian Squeeze)
# Unsupported: Ubuntu Karmic and before, Debian Lenny and before

# Put this file in /etc/apt/detect-http-proxy and create and add the below
# configuration in /etc/apt/apt.conf.d/30detectproxy
#    Acquire::http::ProxyAutoDetect "/etc/apt/detect-http-proxy";

# APT calls this script for each host that should be connected to. Therefore
# you may see the proxy messages multiple times (LP 814130). If you find this
# annoying and wish to disable these messages, set show_proxy_messages to 0
show_proxy_messages=1

# on or more proxies can be specified. Note that each will introduce a routing
# delay and therefore its recommended to put the proxy which is most likely to
# be available on the top. If no proxy is available, a direct connection will
# be used
try_proxies=(
10.0.0.1:8000
10.0.0.2:8000
)

print_msg() {
    # \x0d clears the line so [Working] is hidden
    [ "$show_proxy_messages" = 1 ] && printf '\x0d%s\n' "$1" >&2
}

for proxy in "${try_proxies[@]}"; do
    # if the host machine / proxy is reachable...
    if nc -z ${proxy/:/ }; then
        proxy=http://$proxy
        print_msg "Proxy that will be used: $proxy"
        echo "$proxy"
        exit
    fi
done
print_msg "No proxy will be used"

# Workaround for Launchpad bug 654393 so it works with Debian Squeeze (<0.8.11)
echo DIRECT
Run Code Online (Sandbox Code Playgroud)

现在,APT 必须配置为使用上述代理检测脚本,因此将以下代码放入/etc/apt/apt.conf.d/30detectproxy

# Fail immediately if a file could not be retrieved. Comment if you have a bad
# Internet connection
Acquire::Retries 0;

# undocumented feature which was found in the source. It should be an absolute
# path to the program, no arguments are allowed. stdout contains the proxy
# server, stderr is shown (in stderr) but ignored by APT
Acquire::http::ProxyAutoDetect "/etc/apt/detect-http-proxy";
Run Code Online (Sandbox Code Playgroud)

我还将下一个代码放入文件中,以防止某些主机被代理。

# Override the default proxy, DIRECT causes a direct connection to be used
Acquire::http::Proxy {
    deb.opera.com DIRECT;
    dl.google.com DIRECT;
};
Run Code Online (Sandbox Code Playgroud)

默认情况下,脚本输出是否使用代理。要禁用它,请编辑/etc/apt/detect-http-proxy并更改show_proxy_messages=1show_proxy_messages=0.

  • 我很高兴我在不久前设置 apt-cacher-ng 时找到了这个答案。我在其中一台家用电脑 (192.168.0.2) 上安装了 apt-cacher-ng。该脚本对我的情况感觉有点过头了,所以我写了一个简化的detect-http-proxy:`if nc -w1 -z 192.168.0.2 3142; 然后 printf http://192.168.0.2:3142; 否则 printf 直接; 菲`。让我们只希望未记录的功能被删除:) (2认同)
  • 为了您的利益,我广泛记录了脚本,如果您关心大小,您可以使用 `i=192.168.0.2;nc -zw1 $i 3142&amp;&amp;echo http://$i:3142/||echo DIRECT` 使其更短:p (2认同)

小智 8

现在有一种官方支持的方法来做到这一点 - 使用选项 - Acquire::http::Proxy-Auto-Detect(参见apt.conf手册页)。行为类似于旧的未记录Acquire::http::ProxyAutoDetect(注意新/旧配置选项中是否存在连字符),它在很大程度上向后兼容,但已扩展......

我正在向 apt 维护者提交补丁以改进文档,但由于这不太可能使其成为随发行版发行很长时间的 apt 版本,因此我将包含以下文本这里建议的补丁:

Acquire::http::Proxy-Auto-Detect可用于指定外部命令来发现要使用的 http 代理。APT 可能会多次调用该命令,并将一个 URI 作为其第一个也是唯一的参数传递给该命令。APT 期望命令输出代理,该代理将用于联系其标准输出上的有问题的 URI,作为 style 中的一行http://proxy:port/,或者DIRECT如果不使用代理,则输出单词。无输出表示应使用通用代理设置。

请注意,如果已通过 设置了特定于主机的代理配置,则不会对主机使用自动检测Acquire::http::Proxy::HOST

要诊断与外部命令的交互,请设置Debug::Acquire::http=yes和/或Debug::Acquire::https=yes例如使用-o命令行参数。

请注意,使用 apt 的预发布版本,版本 1.3~exp2 到 1.3 然后有一个错误(可能由 1.3.1 修复)导致 apt 解析外部命令的stderr和 stdout。