Skip to content

Instantly share code, notes, and snippets.

@xnox
Last active February 22, 2018 13:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnox/5234296 to your computer and use it in GitHub Desktop.
Save xnox/5234296 to your computer and use it in GitHub Desktop.
#!/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)
Acquire::http::ProxyAutoDetect "/path/to/apt-avahi-discover.py";
<?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>
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment