如果不可用,我如何忽略代理?
按照最佳方式在LAN上缓存apt下载的说明? ,我在本地网络中设置了一个缓存代理。 由于该机器并不总是运行,我希望能够刷新源列表并安装包,而不使用该代理(如果不可用)。
我已经阅读了apt.conf(5)
手册页中的Acquire group部分,但我找不到像“Silent-Fail”这样的选项。
目前, sudo apt-get update
和相关命令失败,因为无法建立连接。 那么如何配置客户端,以便在代理不可用时忽略代理?
有一个未记录的设置, Acquire::http::ProxyAutoDetect
。 此设置应包含二进制文件的完整路径,并且不能包含参数。 该命令应输出要使用的代理(例如: http://10.0.0.1:8000
: http://10.0.0.1:8000
: 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 # 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
现在,必须将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";
我还将下一个代码放到文件中以防止某些主机被代理。
# Override the default proxy, DIRECT causes a direct connection to be used Acquire::http::Proxy { deb.opera.com DIRECT; dl.google.com DIRECT; };
默认情况下,脚本输出是否使用代理。 要禁用它,请编辑/etc/apt/detect-http-proxy
并将show_proxy_messages=1
更改为show_proxy_messages=0
。
现在有一种官方支持的方法 – 使用选项apt.conf
Acquire::http::Proxy-Auto-Detect
(参见apt.conf
手册页)。 行为类似于旧的未记录的Acquire::http::ProxyAutoDetect
(注意新/旧配置选项中连字符的存在/不存在),它在很大程度上是向后兼容的,但已被扩展…
我正在向apt维护者提交补丁以改进文档,但由于这不太可能使其成为一个版本的发布版本附带发行版很长一段时间,我将包括文本这里提出的补丁:
Acquire::http::Proxy-Auto-Detect
可用于指定外部命令以发现要使用的http代理。 APT可以多次调用该命令,并将URI作为其第一个也是唯一的参数传递给该命令。 APT期望命令输出代理,该代理用于将其stdout上的有问题的URI作为样式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。