Page 1 of 4

Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 21st, 2009, 8:34 am
by qwerty967
It would be great to add support for zeroconf so that the user could open up the web interface by clicking from a list of Bonjour enabled web servers. SABnzbd would advertise its http service to all computers on the local area network and users can open up the SABnzbd interface by clicking from a list in Safari or Internet Explorer.

Two things would need to be done. 1) Include pybonjour (MIT-licensed) and 2) write few lines of code to register a service with Bonjour.

From the end users perspective, Mac users are set. Bonjour is included in Mac OS X and Safari comes with a http service browser. Windows users would need to install Apple's Bonjour package to get the services browser plugin for Internet Explorer. Ubuntu comes with Avahi but pybonjour requires the Bonjour compatibility layer (libavahi-compat-libdnssd1) to be installed.

All this works in conjunction with the current networking system. So you can run your own DHCP, DNS, or manually add IP addresses in addition to using zeroconf.

pybonjour, Mirror

Code: Select all

#
# Example code to register http service with Bonjour
#
# It advertises http://hostname.local.:8080/ 
# instead of    http://hostname.local:8080/sabnzbd/
#

import pybonjour, select

name = 'SABnzbd'
regtype = '_http._tcp'
port    = int(8080)


def register_callback(sdRef, flags, errorCode, name, regtype, domain):
    if errorCode == pybonjour.kDNSServiceErr_NoError:
        print 'Registered service:'
        print '  name    =', name
        print '  regtype =', regtype
        print '  domain  =', domain


sdRef = pybonjour.DNSServiceRegister(name = name,
                                     regtype = regtype,
                                     port = port,
                                     callBack = register_callback)

try:
    try:
        while True:
            ready = select.select([sdRef], [], [])
            if sdRef in ready[0]:
                pybonjour.DNSServiceProcessResult(sdRef)
    except KeyboardInterrupt:
        pass
finally:
    sdRef.close()

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 21st, 2009, 11:48 am
by switch
Looks like it's also supported in windows, although I haven't tried it: http://support.apple.com/downloads/Bonjour_for_Windows

I've created a ticket for it, #243, however I can't promise when it will get looked at.

If bonjour does work properly in windows, and there is requests from other users for it (or just support for it in this thread) then it will likely get looked at sooner.

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 23rd, 2009, 6:40 am
by qwerty967
I've attached screenshots of Internet Explorer and Safari to show how Zeroconf works. Also, I've created a build of SABnzbd+ 0.4.6 with Zeroconf enabled for people to try out. If Zeroconf is supported in SABnzbd, then third party add-ons such as SABnzbdX can present to the user a list of SABnzbd servers running in the local network.

Windows: SABnzbd-0.4.6-win32-bin-ZEROCONF.zip You must first install Apple Bonjour for Windows.

Source: SABnzbd-0.4.6-src-ZEROCONF.tar.gz You must first resolve SABnzbd dependencies yourself. Bonjour or Avahi with the Bonjour compatibility layer must be installed (already included in Mac OS X and Ubuntu).

I only made two changes to the source. I added pybonjour.py to sabnzbd.utils and included the following code in SABnzbd.py at line 792. It handles the absence of Bonjour/Avahi gracefully.

Code: Select all

####Start Zeroconf patch########################################################
    try:
        from sabnzbd.utils import pybonjour
    except OSError:
        logging.info('Zeroconf service could not be registered')
    else:
        def zeroconf_callback(sdRef, flags, errorCode, name, regtype, domain):
            if errorCode == pybonjour.kDNSServiceErr_NoError:
                logging.info('Registering Zeroconf service')

        zeroconf_sdRef = pybonjour.DNSServiceRegister(
            name = 'SABnzbd',
            regtype = '_http._tcp',
            port = int(cfg['misc']['port']),
            txtRecord = pybonjour.TXTRecord({'path': '/sabnzbd/'}),
            callBack = zeroconf_callback)

        pybonjour.DNSServiceProcessResult(zeroconf_sdRef)
####End Zeroconf patch##########################################################

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 23rd, 2009, 7:07 am
by rAf
Nice, I'll build an OSX version to test tonight...

Thanks for your work !

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 23rd, 2009, 8:19 am
by rAf
I've made some tests and it works great if you configure SABnzbd to listen on 0.0.0.0 or your ip address.
But if we include this, we shouldn't activate Zeroconf if SABnzbd listen on 127.0.0.1 or localhost.

I've tested from Safari on Windows and Safari on OSX (10.4).

This remarks scars me a little, I don't know if it's true or not... :
http://en.wikipedia.org/wiki/Bonjour_%2 ... #Criticism

If someone wants to test, the osx build is available here

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 23rd, 2009, 10:13 am
by qwerty967
The OS X build works great. I am able to discover the service from Safari on Mac OS X 10.5, Internet Explorer 8 on Windows 7 Beta, and avahi-browse on Ubuntu 8.10. You are right about the localhost/127.0.0.1 thing.
rAf wrote: This remarks scars me a little, I don't know if it's true or not... :
http://en.wikipedia.org/wiki/Bonjour_%2 ... #Criticism
Most of the things listed are non-issues. On Windows, Bonjour may be installed by some programs, but there is nothing wrong with making sure some library that a program uses is on the system. It's like SABnzbd including a Python interpreter. Also, over-sensitive security software might flag Bonjour requests, but those programs are the same ones that freak out over cookies. The only valid criticism is that it is incompatible with some VPN clients.

Bonjour is designed for small networks that aren't as well-managed as large ones. Bonjour takes precautions such as caching, long lived queries, and other techniques so that the network isn't flooded. As with any technology, it can be abused by malfunctioning applications. But if Zeroconf technology were bad, then most current Linux distributions wouldn't have adopted Avahi.

Zero Configuration networking with Bonjour [video]

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 23rd, 2009, 12:04 pm
by rAf
Maybe we could enable it by default an let an option for advanced users to disable it ?

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 23rd, 2009, 4:17 pm
by sander
I would like this bonjour / avahi feature.

The script works on my Ubuntu. See screendump

Can bonjour / pybonjour take care of the full URL, thus including the trailing "/sabnzbd", and not just the IP address and port number?

BTW: On Ubuntu, with avahi-discover, you can not click on services to start an application. Does anybody know if there is a program that allows clicking?

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 23rd, 2009, 4:23 pm
by sander
PS: Mini-HOWTO for installing pybonjour on Ubuntu:

download pybonjour from http://o2s.csail.mit.edu/download/pybonjour/ , unpack it and go into the pybonjour directory
python setup.py build
sudo python setup.py install
sudo apt-get install libavahi-compat-libdnssd1

After that you should be OK to use python programs that use pybonjour.

EDIT:
the above build and install is NOT needed if you just copy pybonjour into the sabnzbd-utils directory. And if pybonjour is included in the SABnzbd distribution, there is no downloading or copying needed at all.

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 23rd, 2009, 5:02 pm
by qwerty967
rAf wrote: Maybe we could enable it by default an let an option for advanced users to disable it ?
That would be good. The wrong way to do it would be to disable Zeroconf by default like the latest nightly builds of Transmission does. Making the user enable Zeroconf sort of defeats its purpose.
sander wrote:Can bonjour / pybonjour take care of the full URL, thus including the trailing "/sabnzbd", and not just the IP address and port number?
Hmm... It should include a TXT record of the path. It works on my SABnzbd on Ubuntu (see screenshot).

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 23rd, 2009, 5:33 pm
by sander
qwerty967 wrote: Hmm... It should include a TXT record of the path. It works on my SABnzbd on Ubuntu (see screenshot).
Does the TXT show when you avahi-discover on the Ubuntu machine itself?

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 23rd, 2009, 9:14 pm
by qwerty967
sander wrote:Does the TXT show when you avahi-discover on the Ubuntu machine itself?
Yes. Are you using the code from the first post of this thread or the third?

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 24th, 2009, 12:59 am
by qwerty967
sander wrote: BTW: On Ubuntu, with avahi-discover, you can not click on services to start an application. Does anybody know if there is a program that allows clicking?
Epiphany.

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 24th, 2009, 2:03 am
by sander
I installed epiphany and I'm using it to type this.

Just checking: Epiphany is a web browser that uses avahi / bonjour to find advertised *websites* (but not other services)?

EDIT:
removed my remark about the first code snippet (which does not show "sabnzbd' in the URL)

Re: Zeroconf (Bonjour/Avahi) support for HTTP

Posted: February 24th, 2009, 2:20 am
by sander
qwerty967 wrote:
sander wrote:Does the TXT show when you avahi-discover on the Ubuntu machine itself?
Yes. Are you using the code from the first post of this thread or the third?
I was using the first code snippet; the standalone python script.

EDIT:
I've used the second code snippet, and put it into sabnzbd.py. And I copied pybonjour.py into /SABnzbd-0.4.6/sabnzbd/utils/ :

Code: Select all

sander@linuxmint6 ~/pybonjour-1.1.1 $ cp ~/pybonjour-1.1.1/pybonjour.py /home/sander/SABnzbd-0.4.6/sabnzbd/utils/
I then started SABnzbd and ... it works! The SABnzbd service is found by epiphany (and avahi-discover). Impressive! Cool! I want this as a standard in SAB! ;-)

See screendump.