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

Modified on: Thu, 14 Jun 2018 06:17:23 +0800

按照最佳方式在局域网上缓存apt下载?,我在本地网络中设置了一个缓存代理。由于该机器并不总是启动,我希望能够刷新源列表并安装包,而不使用该代理(如果不可用)。

我已经阅读了获取组部分.5.html“rel =”nofollow noreferrer“> apt.conf(5),但我找不到像”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.2:8000代理。< / p>

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

#!/bin/bash # detect-http-proxy - Returns a HTTP proxy which is available for use # Author: Lekensteyn &lt;lekensteyn@gmail.com&gt; # 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 ] &amp;&amp; printf '\x0d%s\n' "$1" &gt;&amp;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 (&lt;0.8.11) echo DIRECT

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

#!/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

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

# 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";

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

作者:Lekensteyn

相关问答

添加新评论