public
Last active

  • Download Gist
apt-avahi-discover.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#!/usr/bin/python
#
# use avahi to find a _apt_proxy._tcp provider and return
# a http proxy string suitable for apt
 
import socket
from subprocess import Popen, PIPE
 
 
def is_ipv6(a):
return ':' in a
 
 
def get_proxy_host_port_from_avahi():
service = '_apt_proxy._tcp'
 
# Obtain all of the services addresses from avahi, pulling the IPv6
# addresses to the top.
addr4 = []
addr6 = []
p = Popen(['avahi-browse', '-kprt', service], stdout=PIPE)
for line in p.stdout:
if line.startswith('='):
bits = line.split(';')
if is_ipv6(bits[7]):
addr6.append([bits[7], bits[8]])
else:
addr4.append([bits[7], bits[8]])
 
# Run through the offered addresses and see if we we have a bound local
# address for it.
for (ip, port) in addr6 + addr4:
try:
res = socket.getaddrinfo(ip, port, 0, 0, 0, socket.AI_ADDRCONFIG)
if res:
return (ip, port)
except socket.gaierror:
pass
# nothing found
return None
 
 
if __name__ == "__main__":
# Dump the approved address out in an appropriate format.
address = get_proxy_host_port_from_avahi()
if address:
(ip, port) = address
if is_ipv6(ip):
print "http://[%s]:%s/" % (ip, port)
else:
print "http://%s:%s/" % (ip, port)
etc_apt_apt..conf.d_30autoproxy
1
Acquire::http::ProxyAutoDetect "/path/to/apt-avahi-discover.py";
etc_avahi_services_apt-cacher-ng.service
1 2 3 4 5 6 7 8 9
<?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>
etc_schroot_setup.d_51aptproxy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#!/bin/sh
# Copyright © 2013 Dmitrijs Ledkovs <xnox@debian.org>
#
# schroot is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# schroot is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
#
#####################################################################
 
set -e
 
. "$SETUP_DATA_DIR/common-data"
. "$SETUP_DATA_DIR/common-functions"
. "$SETUP_DATA_DIR/common-config"
 
APT_AVAHI_DISCOVER=/usr/share/squid-deb-proxy-client/apt-avahi-discover
 
if [ $STAGE = "setup-start" ] || [ $STAGE = "setup-recover" ]; then
if type apt-avahi-discover 1>/dev/null; then
APT_AVAHI_DISCOVER=apt-avahi-discover
fi
APT_PROXY=`$APT_AVAHI_DISCOVER`
if [ -n "$APT_PROXY" ]; then
info "Setting apt-proxy to avahi proxy at ${APT_PROXY}"
printf 'Acquire::http { Proxy "%s";};\n' ${APT_PROXY} > "${CHROOT_PATH}/etc/apt/apt.conf.d/30schrootautoproxy"
fi
fi

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.