6

I'm using apt-cacher-ng at my local network with the following configuration on the clients:

Acquire::http { Proxy "http://acng-host:3142"; };

Some of the clients are laptops, so how do I configure them to use the cache only when it's available on this network?

Braiam
  • 67,791
  • 32
  • 179
  • 269
itshorty
  • 163
  • 1
  • 4

3 Answers3

7

From the server you can announce to the network there is an apt-cacher-ng instance through avahi

From the client you can check if exist an apt-cacher-ng service and modify apt proxy settings accordly.


Server

Install

$ sudo apt-get install apt-cacher-ng squid-deb-proxy-client

For Ubuntu releases older than 14.04 put the following snippet in /etc/avahi/services/apt-cacher-ng.service:

<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
 <name replace-wildcards="yes">apt-cacher-ng proxy on %h</name>
 <service protocol="ipv4">
  <type>_apt_proxy._tcp</type>
  <port>3142</port>
 </service>
</service-group>

Client

Install

$ sudo apt-get install squid-deb-proxy-client

Here is a snippet to install server|client depending if there is already an apt-get proxy or not:

Source: http://blog.surgut.co.uk/2013/03/avahi-apt-cacher-ng-sbuild.html

LiveWireBT
  • 28,763
  • That's a nice find! I'm wondering why this isn't the default whenever apt-cacher-ng is suggested, Dimitri John Ledkov has a canonical.com address and the snippet is already included in 14.04. I just made an edit your answer. – LiveWireBT Nov 13 '16 at 22:59
  • So the magic on 18.04 appears to be in /etc/apt/apt.conf.d/30autoproxy, which contains the single line: Acquire::http::ProxyAutoDetect "/usr/share/squid-deb-proxy-client/apt-avahi-discover"; (referring to a Python script). Sweet. This gets installed by squid-deb-proxy-client. So for my own scenario I don't need that on the server at all! – 0xC0000022L Apr 29 '20 at 21:21
6

Something like this should work:

/etc/NetworkManager/dispatcher.d

#!/bin/bash
ip=10.0.1.13
port=3142
nc -w 1 $ip $port
proxy_file="/etc/apt/apt.conf.d/02local_proxy"
if [ $? -eq 0 ]; then
    echo "Acquire::http { Proxy \"http://$ip:$port\"; };" > $proxy_file
    echo 'Acquire::https { Proxy "false"; };' >> $proxy_file
else
    rm -f $proxy_file
fi

Fix permissions

sudo chmod +x /etc/NetworkManager/dispatcher.d/99SetAptProxy

Notes:

  • The "nc" command tests that it can connect to the 3142 port on the given IP address.
  • This script is run everytime the networking interfaces are changed by network manager.
  • Feel free to alter the way that you detect for the proxy, this works for me, but it is a security vulnerability if you install packages on a foreign network, for example.
dpb
  • 7,069
-1

Not an exact answer, since you'll have to change your existing cache setup, but squid-deb-proxy is a good solution to providing a cache that will be used transparently if available, and is very easy to set up on both cache server and clients. See the first answer to this question for more details.

chronitis
  • 12,367
  • ... that was hopefully meant to be "that will be used transparently if available and if avahi is installed on the client side" ;) – 0xC0000022L Feb 28 '13 at 20:34