Esempio n. 1
0
    def start(self, loop):
        from calibre.utils.mdns import publish, unpublish, get_external_ip, verify_ipV4_address
        ip_address, port = loop.bound_address[:2]
        self.zeroconf_ip_address = zipa = verify_ipV4_address(ip_address) or get_external_ip()
        prefix = loop.opts.url_prefix or ''
        # The Zeroconf module requires everything to be bytestrings

        def enc(x):
            if not isinstance(x, bytes):
                x = x.encode('ascii')
            return x
        mdns_services = (
            (enc(self.service_name), enc(self.service_type), port, {b'path':enc(prefix + self.path)}),
        )
        if self.shutdown.is_set():
            return
        self.services = []

        for s in mdns_services:
            self.services.append(publish(*s, use_ip_address=zipa, add_hostname=self.add_hostname))
        loop.log('OPDS feeds advertised via BonJour at: %s port: %s' % (zipa, port))
        self.advertised_port = port
        self.started.set()

        self.shutdown.wait()
        for s in mdns_services:
            unpublish(*s, add_hostname=self.add_hostname)
        self.stopped.set()
Esempio n. 2
0
    def start(self, loop):
        from calibre.utils.mdns import publish, unpublish, get_external_ip, verify_ipV4_address
        ip_address, port = loop.bound_address[:2]
        self.zeroconf_ip_address = zipa = verify_ipV4_address(
            ip_address) or get_external_ip()
        prefix = loop.opts.url_prefix or ''
        mdns_services = ((self.service_name, self.service_type, port, {
            'path': prefix + self.path
        }), )
        if self.shutdown.is_set():
            return
        self.services = []

        for s in mdns_services:
            self.services.append(
                publish(*s,
                        use_ip_address=zipa,
                        add_hostname=self.add_hostname))
        loop.log('OPDS feeds advertised via BonJour at: %s port: %s' %
                 (zipa, port))
        self.advertised_port = port
        self.started.set()

        self.shutdown.wait()
        for s in mdns_services:
            unpublish(*s,
                      add_hostname=self.add_hostname,
                      wait_for_stop=self.wait_for_stop)
        self.stopped.set()
Esempio n. 3
0
    def server_state_changed(self, running):
        from calibre.utils.mdns import get_external_ip, verify_ipV4_address

        text = _("Start Content Server")
        if running:
            listen_on = verify_ipV4_address(tweaks["server_listen_on"]) or get_external_ip()
            try:
                cs_port = content_server_config().parse().port
                ip_text = _(" [%(ip)s, port %(port)d]") % dict(ip=listen_on, port=cs_port)
            except:
                ip_text = " [%s]" % listen_on
            text = _("Stop Content Server") + ip_text
        self.toggle_server_action.setText(text)
Esempio n. 4
0
 def server_state_changed(self, running):
     from calibre.utils.mdns import get_external_ip, verify_ipV4_address
     text = _('Start Content Server')
     if running:
         listen_on = (verify_ipV4_address(tweaks['server_listen_on']) or
                 get_external_ip())
         try :
             cs_port = content_server_config().parse().port
             ip_text = _(' [%s, port %d]')%(listen_on, cs_port)
         except:
             ip_text = ' [%s]'%listen_on
         text = _('Stop Content Server') + ip_text
     self.toggle_server_action.setText(text)
Esempio n. 5
0
 def server_state_changed(self, running):
     from calibre.utils.mdns import get_external_ip, verify_ipV4_address
     text = _('Start Content Server')
     if running:
         listen_on = (verify_ipV4_address(tweaks['server_listen_on'])
                      or get_external_ip())
         try:
             cs_port = content_server_config().parse().port
             ip_text = _(' [%(ip)s, port %(port)d]') % dict(ip=listen_on,
                                                            port=cs_port)
         except:
             ip_text = ' [%s]' % listen_on
         text = _('Stop Content Server') + ip_text
     self.toggle_server_action.setText(text)
Esempio n. 6
0
 def server_state_changed(self, running):
     from calibre.utils.mdns import get_external_ip, verify_ipV4_address
     text = _('Start Content server')
     if running:
         from calibre.srv.opts import server_config
         opts = server_config()
         listen_on = verify_ipV4_address(opts.listen_on) or get_external_ip()
         try:
             ip_text = _(' [%(ip)s, port %(port)d]')%dict(
                 ip=listen_on, port=opts.port)
         except Exception:
             ip_text = ' [%s]'%listen_on
         text = _('Stop Content server') + ip_text
     self.toggle_server_action.setText(text)
Esempio n. 7
0
    def start_cherrypy(self):
        try:
            cherrypy.engine.start()
        except:
            ip = get_external_ip()
            if not ip or ip.startswith('127.'):
                raise
            cherrypy.log('Trying to bind to single interface: '+ip)
            # Change the host we listen on
            cherrypy.config.update({'server.socket_host' : ip})
            # This ensures that the change is actually applied
            cherrypy.server.socket_host = ip
            cherrypy.server.httpserver = cherrypy.server.instance = None

            cherrypy.engine.start()
Esempio n. 8
0
    def start_cherrypy(self):
        try:
            cherrypy.engine.start()
        except:
            ip = get_external_ip()
            if not ip or ip.startswith('127.'):
                raise
            cherrypy.log('Trying to bind to single interface: '+ip)
            # Change the host we listen on
            cherrypy.config.update({'server.socket_host' : ip})
            # This ensures that the change is actually applied
            cherrypy.server.socket_host = ip
            cherrypy.server.httpserver = cherrypy.server.instance = None

            cherrypy.engine.start()
Esempio n. 9
0
    def serve_forever(self):
        """ Listen for incoming connections. """

        if self.pre_activated_socket is None:
            try:
                self.do_bind()
            except socket.error as err:
                if not self.opts.fallback_to_detected_interface:
                    raise
                ip = get_external_ip()
                if ip == self.bind_address[0]:
                    raise
                self.log.warn(
                    "Failed to bind to %s with error: %s. Trying to bind to the default interface: %s instead"
                    % (self.bind_address[0], as_unicode(err), ip)
                )
                self.bind_address = (ip, self.bind_address[1])
                self.do_bind()
        else:
            self.socket = self.pre_activated_socket
            self.pre_activated_socket = None
            self.setup_socket()

        self.connection_map = {}
        self.socket.listen(min(socket.SOMAXCONN, 128))
        self.bound_address = ba = self.socket.getsockname()
        if isinstance(ba, tuple):
            ba = ":".join(map(type(""), ba))
        self.pool.start()
        with TemporaryDirectory(prefix="srv-") as tdir:
            self.tdir = tdir
            self.ready = True
            if self.LISTENING_MSG:
                self.log(self.LISTENING_MSG, ba)
            self.plugin_pool.start()

            while self.ready:
                try:
                    self.tick()
                except SystemExit:
                    self.shutdown()
                    raise
                except KeyboardInterrupt:
                    break
                except:
                    self.log.exception("Error in ServerLoop.tick")
            self.shutdown()
Esempio n. 10
0
    def serve_forever(self):
        """ Listen for incoming connections. """

        if self.pre_activated_socket is None:
            try:
                self.do_bind()
            except socket.error as err:
                if not self.opts.fallback_to_detected_interface:
                    raise
                ip = get_external_ip()
                if ip == self.bind_address[0]:
                    raise
                self.log.warn(
                    'Failed to bind to %s with error: %s. Trying to bind to the default interface: %s instead'
                    % (self.bind_address[0], as_unicode(err), ip))
                self.bind_address = (ip, self.bind_address[1])
                self.do_bind()
        else:
            self.socket = self.pre_activated_socket
            self.pre_activated_socket = None
            self.setup_socket()

        self.connection_map = {}
        self.socket.listen(min(socket.SOMAXCONN, 128))
        self.bound_address = ba = self.socket.getsockname()
        if isinstance(ba, tuple):
            ba = ':'.join(map(type(''), ba))
        self.pool.start()
        with TemporaryDirectory(prefix='srv-') as tdir:
            self.tdir = tdir
            self.ready = True
            if self.LISTENING_MSG:
                self.log(self.LISTENING_MSG, ba)
            self.plugin_pool.start()

            while self.ready:
                try:
                    self.tick()
                except SystemExit:
                    self.shutdown()
                    raise
                except KeyboardInterrupt:
                    break
                except:
                    self.log.exception('Error in ServerLoop.tick')
            self.shutdown()
Esempio n. 11
0
 def initialize_socket(self):
     if self.pre_activated_socket is None:
         try:
             self.do_bind()
         except socket.error as err:
             if not self.opts.fallback_to_detected_interface:
                 raise
             ip = get_external_ip()
             if ip == self.bind_address[0]:
                 raise
             self.log.warn('Failed to bind to %s with error: %s. Trying to bind to the default interface: %s instead' % (
                 self.bind_address[0], as_unicode(err), ip))
             self.bind_address = (ip, self.bind_address[1])
             self.do_bind()
     else:
         self.socket = self.pre_activated_socket
         self.pre_activated_socket = None
         self.setup_socket()
Esempio n. 12
0
 def initialize_socket(self):
     if self.pre_activated_socket is None:
         try:
             self.do_bind()
         except socket.error as err:
             if not self.opts.fallback_to_detected_interface:
                 raise
             ip = get_external_ip()
             if ip == self.bind_address[0]:
                 raise
             self.log.warn('Failed to bind to %s with error: %s. Trying to bind to the default interface: %s instead' % (
                 self.bind_address[0], as_unicode(err), ip))
             self.bind_address = (ip, self.bind_address[1])
             self.do_bind()
     else:
         self.socket = self.pre_activated_socket
         self.pre_activated_socket = None
         self.setup_socket()
Esempio n. 13
0
 def server_state_changed(self, running):
     from calibre.utils.mdns import get_external_ip, verify_ipV4_address
     text = _('Start Content server')
     if running:
         from calibre.srv.opts import server_config
         opts = server_config()
         listen_on = verify_ipV4_address(opts.listen_on) or get_external_ip()
         protocol = 'HTTPS' if opts.ssl_certfile and opts.ssl_keyfile else 'HTTP'
         try:
             ip_text = ' ' + _('[{ip}, port {port}, {protocol}]').format(
                     ip=listen_on, port=opts.port, protocol=protocol)
         except Exception:
             ip_text = ' [{} {}]'.format(listen_on, protocol)
         self.ip_text = ip_text
         self.server_state_changed_signal.emit(running, ip_text)
         text = _('Stop Content server') + ip_text
     else:
         self.ip_text = ''
     self.toggle_server_action.setText(text)
Esempio n. 14
0
    def start(self, loop):
        from calibre.utils.mdns import publish, unpublish, get_external_ip, verify_ipV4_address
        ip_address, port = loop.bound_address[:2]
        self.zeroconf_ip_address = zipa = verify_ipV4_address(ip_address) or get_external_ip()
        prefix = loop.opts.url_prefix or ''
        mdns_services = (
            (self.service_name, self.service_type, port, {'path':prefix + self.path}),
        )
        if self.shutdown.is_set():
            return
        self.services = []

        for s in mdns_services:
            self.services.append(publish(*s, use_ip_address=zipa, add_hostname=self.add_hostname))
        loop.log('OPDS feeds advertised via BonJour at: %s port: %s' % (zipa, port))
        self.advertised_port = port
        self.started.set()

        self.shutdown.wait()
        for s in mdns_services:
            unpublish(*s, add_hostname=self.add_hostname, wait_for_stop=self.wait_for_stop)
        self.stopped.set()
Esempio n. 15
0
 def server_state_changed(self, running):
     from calibre.utils.mdns import get_external_ip
     text = _('Start Content Server')
     if running:
         text = _('Stop Content Server') + ' [%s]'%get_external_ip()
     self.toggle_server_action.setText(text)
Esempio n. 16
0
 def server_state_changed(self, running):
     from calibre.utils.mdns import get_external_ip
     text = _('Start Content Server')
     if running:
         text = _('Stop Content Server') + ' [%s]' % get_external_ip()
     self.toggle_server_action.setText(text)