---
 README.md        | 18 ++++++++++++++++++
 auto-apt-proxy   | 28 ++++++++++++++++++++++++++++
 debian/changelog |  1 +
 debian/control   |  6 +++---
 4 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 0e51266..9600724 100644
--- a/README.md
+++ b/README.md
@@ -27,6 +27,24 @@ that for this to work, any programs invoked by the given 
command must have
 their own support for detecting HTTP proxies from environment variables, and
 for using them.
 
+# CONFIGURATION
+
+When your apt proxy is installed on localhost or your default gateway,
+it should Just Work.  If you install is somewhere else, you can create
+an explicit SRV record to tell auto-apt-proxy about it.
+
+Suppose your corporate domain is "example.com", and
+apt-cacher-ng is installed on "apt-cacher-ng.example.com", and
+auto-apt-proxy is installed on "alices-laptop.example.com".
+
+The appropriate SRV record in dnsmasq.conf would look like this:
+
+    srv-host=_apt_proxy._tcp.example.com,apt-cacher-ng.example.com,3142
+
+The appropriate nsd/bind zonefile entry would look like this (untested):
+
+    _apt_proxy._tcp.@  IN SRV 0 0 3142 apt-cacher-ng.@
+
 # EXAMPLES
 
 $ **auto-apt-proxy**
diff --git a/auto-apt-proxy b/auto-apt-proxy
index 889b704..64bfa74 100755
--- a/auto-apt-proxy
+++ b/auto-apt-proxy
@@ -46,7 +46,35 @@ detect_approx() {
   return 1
 }
 
+# NOTE: This does NOT check MDNS/DNS-SD (avahi/zeroconf/bonjour) records.
+#       If you want that, use squid-deb-proxy-client, which depends on avahi.
+#
+# FIXME: if there are multiple matching SRV records, we should make a
+#        weighted random choice from the one(s) with the highest priority.
+#        For now, we make a uniformly random choice from all records (shuf + 
exit).
+#
+# NOTE: We don't check that it "looks like" a known apt proxy (hit + grep -q).
+#       This is because
+#        1) the other detectors are just GUESSING hosts and ports.
+#           You might accidentally run a non-apt-proxy on 127.0.0.1:9999, but
+#           you can't accidentally create an _apt_proxy SRV record!
+#        2) refactoring the grep -q's out of detect_* is tedious and boring.
+#        3) there's no grep -q for squid, which I want to use. ;-)
+#
+# NOTE: no need for if/then/else and return 0/1 because:
+#        * if awk matches something, it prints it and exits zero.
+#        * if hostname or apt-helper fail, awk matches nothing, so exits 
non-zero.
+#        * set -e ignores errors from apt-helper (no pipefail) and hostname 
(no ???).
+detect_DNS_SRV_record() {
+  /usr/lib/apt/apt-helper srv-lookup _apt_proxy._tcp."$(hostname --domain)" 
2>/dev/null |
+  shuf |
+  awk '/^[^#]/{print "http://"; $1 ":" $4;found=1;exit}END{exit !found}'
+}
+
 detect() {
+  # If a SRV record is found, use it and guess no further.
+  detect_DNS_SRV_record && return 0
+
   if command -v ip >/dev/null; then
     gateway=$(ip route | awk '/default/ { print($3) }')
   elif busybox ip >/dev/null 2>&1; then
diff --git a/debian/changelog b/debian/changelog
index ddcf598..766d241 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -5,6 +5,7 @@ auto-apt-proxy (11+nmu1) UNRELEASED; urgency=medium
     Even though iproute2 is Priority: important, busybox is more common
     according to https://popcon.debian.org/by_inst.gz, probably due to
     initramfs-tools Recommends: busybox.
+  * Use apt-helper to find apt proxy via _apt_proxy._tcp DNS record.
 
  -- Trent W. Buck <trentb...@gmail.com>  Mon, 26 Aug 2019 13:31:23 +1000
 
diff --git a/debian/control b/debian/control
index 7755a21..1f40bcb 100644
--- a/debian/control
+++ b/debian/control
@@ -35,10 +35,10 @@ Description: automatic detector of common APT proxy settings
    * apt-cacher-ng
    * approx
    * apt-cacher
+   * any proxy advertised by an _apt_proxy._tcp SRV record in your domain
  .
  This package has a minimal set of dependencies in order to minimize the
  influence on systems where it is installed.
  .
- For corporate desktop/server deployments, where the APT proxy can be located
- at any arbitrary host, you should probably try the `squid-deb-proxy-client`
- package instead.
+ This package does not query mDNS SRV records, which would require avahi.
+ If you need that, consider the `squid-deb-proxy-client` package instead.
-- 
2.20.1

Reply via email to