socket_timeout = dconf.get_property("pkg", "socket_timeout") if not socket_timeout: dconf.set_property("pkg", "socket_timeout", SOCKET_TIMEOUT_DEFAULT) socket_timeout = dconf.get_property("pkg", "socket_timeout") threads = dconf.get_property("pkg", "threads") if not threads: dconf.set_property("pkg", "threads", THREADS_DEFAULT) threads = dconf.get_property("pkg", "threads") # If the program is going to reindex, the port is irrelevant since # the program will not bind to a port. if not exit_ready: try: portend.Checker().assert_free(address, port) except Exception as e: emsg("pkg.depotd: unable to bind to the specified " "port: {0:d}. Reason: {1}".format(port, e)) sys.exit(1) else: # Not applicable if we're not going to serve content dconf.set_property("pkg", "content_root", "") # Any relative paths should be made absolute using pkg_root. 'pkg_root' # is a special property that was added to enable internal deployment of # multiple disparate versions of the pkg.depotd software. pkg_root = dconf.get_property("pkg", "pkg_root") repo_config_file = dconf.get_property("pkg", "cfg_file") if repo_config_file and not os.path.isabs(repo_config_file):
def initialize(options): # HTTPS stuff stolen from sickbeard enable_https = options['enable_https'] https_cert = options['https_cert'] https_key = options['https_key'] https_chain = options['https_chain'] if enable_https: # If either the HTTPS certificate or key do not exist, try to make # self-signed ones. if not (https_cert and os.path.exists(https_cert)) or not (https_key and os.path.exists(https_key)): if not create_https_certificates(https_cert, https_key): logger.warn("Unable to create certificate and key. Disabling " \ "HTTPS") enable_https = False if not (os.path.exists(https_cert) and os.path.exists(https_key)): logger.warn("Disabled HTTPS because of missing certificate and " \ "key.") enable_https = False options_dict = { 'server.socket_port': options['http_port'], 'server.socket_host': options['http_host'], 'server.thread_pool': 10, 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8', 'tools.decode.on': True, 'log.screen': True, 'engine.autoreload.on': False, } if enable_https: options_dict['server.ssl_certificate'] = https_cert options_dict['server.ssl_private_key'] = https_key if https_chain: options_dict['server.ssl_certificate_chain'] = https_chain protocol = "https" else: protocol = "http" logger.info("Starting Mylar on %s://%s:%d%s" % (protocol,options['http_host'], options['http_port'], options['http_root'])) cherrypy.config.update(options_dict) conf = { '/': { 'tools.staticdir.root': os.path.join(mylar.PROG_DIR, 'data'), 'tools.proxy.on': True # pay attention to X-Forwarded-Proto header }, '/interfaces': { 'tools.staticdir.on': True, 'tools.staticdir.dir': "interfaces" }, '/images': { 'tools.staticdir.on': True, 'tools.staticdir.dir': "images" }, '/css': { 'tools.staticdir.on': True, 'tools.staticdir.dir': "css" }, '/js': { 'tools.staticdir.on': True, 'tools.staticdir.dir': "js" }, '/favicon.ico': { 'tools.staticfile.on': True, 'tools.staticfile.filename': os.path.join(os.path.abspath(os.curdir), 'images' + os.sep + 'favicon.ico') }, '/cache': { 'tools.staticdir.on': True, 'tools.staticdir.dir': mylar.CONFIG.CACHE_DIR } } if options['http_password'] is not None: #userpassdict = dict(zip((options['http_username'].encode('utf-8'),), (options['http_password'].encode('utf-8'),))) #get_ha1= cherrypy.lib.auth_digest.get_ha1_dict_plain(userpassdict) if options['authentication'] == 2: # Set up a sessions based login page instead of using basic auth, # using the credentials set for basic auth. Attempting to browse to # a restricted page without a session token will result in a # redirect to the login page. A sucessful login should then redirect # to the originally requested page. # # Login sessions timeout after 43800 minutes (1 month) unless # changed in the config. cherrypy.tools.sessions.timeout = options['login_timeout'] conf['/'].update({ 'tools.sessions.on': True, 'tools.auth.on': True, 'auth.forms_username': options['http_username'], 'auth.forms_password': options['http_password'], # Set all pages to require authentication. # You can also set auth requirements on a per-method basis by # using the @require() decorator on the methods in webserve.py 'auth.require': [] }) # exempt api, login page and static elements from authentication requirements for i in ('/api', '/auth/login', '/css', '/images', '/js', 'favicon.ico'): if i in conf: conf[i].update({'tools.auth.on': False}) else: conf[i] = {'tools.auth.on': False} elif options['authentication'] == 1: conf['/'].update({ 'tools.auth_basic.on': True, 'tools.auth_basic.realm': 'Mylar', 'tools.auth_basic.checkpassword': cherrypy.lib.auth_basic.checkpassword_dict( {options['http_username']: options['http_password']}) }) conf['/api'] = {'tools.auth_basic.on': False} rest_api = { '/': { # the api uses restful method dispatching 'request.dispatch': cherrypy.dispatch.MethodDispatcher(), # all api calls require that the client passes HTTP basic authentication 'tools.auth_basic.on' : False, } } if options['opds_authentication']: user_list = {} if len(options['opds_username']) > 0: user_list[options['opds_username']] = options['opds_password'] if options['http_password'] is not None and options['http_username'] != options['opds_username']: user_list[options['http_username']] = options['http_password'] conf['/opds'] = {'tools.auth.on': False, 'tools.auth_basic.on': True, 'tools.auth_basic.realm': 'Mylar OPDS', 'tools.auth_basic.checkpassword': cherrypy.lib.auth_basic.checkpassword_dict(user_list)} else: conf['/opds'] = {'tools.auth_basic.on': False, 'tools.auth.on': False} # Prevent time-outs #cherrypy.engine.timeout_monitor.unsubscribe() cherrypy.tree.mount(WebInterface(), str(options['http_root']), config = conf) restroot = REST() restroot.comics = restroot.Comics() restroot.comic = restroot.Comic() restroot.watchlist = restroot.Watchlist() #restroot.issues = restroot.comic.Issues() #restroot.issue = restroot.comic.Issue() cherrypy.tree.mount(restroot, '/rest', config = rest_api) try: portend.Checker().assert_free(options['http_host'], options['http_port']) cherrypy.server.start() except Exception as e: logger.error('[ERROR] %s' % e) print('Failed to start on port: %i. Is something else running?' % (options['http_port'])) sys.exit(0) cherrypy.server.wait()
def initialize(options=None): if options is None: options = {} https_enabled = options['https_enabled'] https_cert = options['https_cert'] https_key = options['https_key'] if https_enabled: if not (os.path.exists(https_cert) and os.path.exists(https_key)): logger.warn( "Disabled HTTPS because of missing certificate and key.") https_enabled = False options_dict = { 'log.screen': False, 'server.thread_pool': 10, 'server.socket_port': options['http_port'], 'server.socket_host': options['http_host'], 'engine.autoreload.on': False, 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8', 'tools.decode.on': True, 'error_page.401': lazylibrarian.common.error_page_401, } if https_enabled: options_dict['server.ssl_certificate'] = https_cert options_dict['server.ssl_private_key'] = https_key protocol = "https" else: protocol = "http" logger.info("Starting LazyLibrarian web server on %s://%s:%d/" % (protocol, options['http_host'], options['http_port'])) cherrypy_cors.install() cherrypy.config.update(options_dict) conf = { '/': { # 'tools.staticdir.on': True, # 'tools.staticdir.dir': os.path.join(lazylibrarian.PROG_DIR, 'data'), 'tools.staticdir.root': os.path.join(lazylibrarian.PROG_DIR, 'data'), 'tools.proxy.on': options['http_proxy'] # pay attention to X-Forwarded-Proto header }, '/api': { 'cors.expose.on': True, }, '/interfaces': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(lazylibrarian.PROG_DIR, 'data', 'interfaces') }, '/images': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(lazylibrarian.PROG_DIR, 'data', 'images') }, '/cache': { 'tools.staticdir.on': True, 'tools.staticdir.dir': lazylibrarian.CACHEDIR }, '/css': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(lazylibrarian.PROG_DIR, 'data', 'css') }, '/js': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(lazylibrarian.PROG_DIR, 'data', 'js') }, '/favicon.ico': { 'tools.staticfile.on': True, # 'tools.staticfile.filename': "images/favicon.ico" 'tools.staticfile.filename': os.path.join(lazylibrarian.PROG_DIR, 'data', 'images', 'favicon.ico') } } if lazylibrarian.CONFIG['PROXY_LOCAL']: conf['/'].update({ # NOTE default if not specified is to use apache style X-Forwarded-Host # 'tools.proxy.local': 'X-Forwarded-Host' # this is for apache2 # 'tools.proxy.local': 'Host' # this is for nginx # 'tools.proxy.local': 'X-Host' # this is for lighthttpd 'tools.proxy.local': lazylibrarian.CONFIG['PROXY_LOCAL'] }) if options['http_pass'] != "": logger.info("Web server authentication is enabled, username is '%s'" % options['http_user']) conf['/'].update({ 'tools.auth_basic.on': True, 'tools.auth_basic.realm': 'LazyLibrarian', 'tools.auth_basic.checkpassword': cherrypy.lib.auth_basic.checkpassword_dict( {options['http_user']: options['http_pass']}) }) conf['/api'].update({ 'tools.auth_basic.on': False, }) # Prevent time-outs cherrypy.engine.timeout_monitor.unsubscribe() cherrypy.tree.mount(WebInterface(), str(options['http_root']), config=conf) cherrypy.engine.autoreload.subscribe() try: if cp_ver and int(cp_ver.split('.')[0]) >= 10: portend.Checker().assert_free(str(options['http_host']), options['http_port']) else: cherrypy.process.servers.check_port(str(options['http_host']), options['http_port']) cherrypy.server.start() except Exception as e: print(str(e)) print('Failed to start on port: %i. Is something else running?' % (options['http_port'])) sys.exit(1) cherrypy.server.wait()
def is_occupied(): try: portend.Checker().assert_free('localhost', port) except Exception: return True return False
def initialize(options=None): if options is None: options = {} https_enabled = options['https_enabled'] https_cert = options['https_cert'] https_key = options['https_key'] if https_enabled: if not (os.path.exists(https_cert) and os.path.exists(https_key)): logger.warn( "Disabled HTTPS because of missing certificate and key.") https_enabled = False options_dict = { 'log.screen': False, 'server.thread_pool': 10, 'server.socket_port': options['http_port'], 'server.socket_host': options['http_host'], 'engine.autoreload.on': False, 'tools.encode.on': True, 'tools.encode.encoding': 'utf-8', 'tools.decode.on': True, 'error_page.401': lazylibrarian.common.error_page_401, } if https_enabled: options_dict['server.ssl_certificate'] = https_cert options_dict['server.ssl_private_key'] = https_key protocol = "https" else: protocol = "http" logger.info("Starting LazyLibrarian web server on %s://%s:%d/" % (protocol, options['http_host'], options['http_port'])) cherrypy_cors.install() cherrypy.config.update(options_dict) conf = { '/': { # 'tools.staticdir.on': True, # 'tools.staticdir.dir': os.path.join(lazylibrarian.PROG_DIR, 'data'), 'tools.staticdir.root': os.path.join(lazylibrarian.PROG_DIR, 'data'), 'tools.proxy.on': options['http_proxy'] # pay attention to X-Forwarded-Proto header }, '/api': { 'cors.expose.on': True, }, '/interfaces': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(lazylibrarian.PROG_DIR, 'data', 'interfaces') }, '/images': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(lazylibrarian.PROG_DIR, 'data', 'images') }, '/cache': { 'tools.staticdir.on': True, 'tools.staticdir.dir': lazylibrarian.CACHEDIR }, '/css': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(lazylibrarian.PROG_DIR, 'data', 'css') }, '/js': { 'tools.staticdir.on': True, 'tools.staticdir.dir': os.path.join(lazylibrarian.PROG_DIR, 'data', 'js') }, '/favicon.ico': { 'tools.staticfile.on': True, # 'tools.staticfile.filename': "images/favicon.ico" 'tools.staticfile.filename': os.path.join(lazylibrarian.PROG_DIR, 'data', 'images', 'favicon.ico') }, '/opensearch.xml': { 'tools.staticfile.on': True, 'tools.staticfile.filename': os.path.join(lazylibrarian.CACHEDIR, 'opensearch.xml') }, '/opensearchbooks.xml': { 'tools.staticfile.on': True, 'tools.staticfile.filename': os.path.join(lazylibrarian.CACHEDIR, 'opensearchbooks.xml') }, '/opensearchgenres.xml': { 'tools.staticfile.on': True, 'tools.staticfile.filename': os.path.join(lazylibrarian.CACHEDIR, 'opensearchgenres.xml') }, '/opensearchmagazines.xml': { 'tools.staticfile.on': True, 'tools.staticfile.filename': os.path.join(lazylibrarian.CACHEDIR, 'opensearchmagazines.xml') }, '/opensearchseries.xml': { 'tools.staticfile.on': True, 'tools.staticfile.filename': os.path.join(lazylibrarian.CACHEDIR, 'opensearchseries.xml') }, '/opensearchauthors.xml': { 'tools.staticfile.on': True, 'tools.staticfile.filename': os.path.join(lazylibrarian.CACHEDIR, 'opensearchauthors.xml') } } if lazylibrarian.CONFIG['PROXY_LOCAL']: conf['/'].update({ # NOTE default if not specified is to use apache style X-Forwarded-Host # 'tools.proxy.local': 'X-Forwarded-Host' # this is for apache2 # 'tools.proxy.local': 'Host' # this is for nginx # 'tools.proxy.local': 'X-Host' # this is for lighthttpd 'tools.proxy.local': lazylibrarian.CONFIG['PROXY_LOCAL'] }) if options['http_pass'] != "": logger.info("Web server authentication is enabled, username is '%s'" % options['http_user']) conf['/'].update({ 'tools.auth_basic.on': True, 'tools.auth_basic.realm': 'LazyLibrarian', 'tools.auth_basic.checkpassword': cherrypy.lib.auth_basic.checkpassword_dict( {options['http_user']: options['http_pass']}) }) conf['/api'].update({ 'tools.auth_basic.on': False, }) if options['opds_authentication']: user_list = {} if len(options['opds_username']) > 0: user_list[options['opds_username']] = options['opds_password'] if options['http_pass'] is not None and options[ 'http_user'] != options['opds_username']: user_list[options['http_user']] = options['http_pass'] conf['/opds'] = { 'tools.auth_basic.on': True, 'tools.auth_basic.realm': 'LazyLibrarian OPDS', 'tools.auth_basic.checkpassword': cherrypy.lib.auth_basic.checkpassword_dict(user_list) } else: conf['/opds'] = {'tools.auth_basic.on': False} opensearch = os.path.join(lazylibrarian.PROG_DIR, 'data', 'opensearch.template') if os.path.exists(opensearch): with open(opensearch, 'r') as s: data = s.read().splitlines() # (title, function) for item in [('Authors', 'Authors'), ('Magazines', 'RecentMags'), ('Books', 'RecentBooks'), ('Genres', 'Genres'), ('Series', 'Series')]: with open( os.path.join(lazylibrarian.CACHEDIR, 'opensearch%s.xml' % item[0].lower()), 'w') as t: for l in data: t.write( l.replace('{label}', item[0]).replace( '{func}', 't=%s&' % item[1]).replace( '{webroot}', options['http_root'])) t.write('\n') # Prevent time-outs cherrypy.engine.timeout_monitor.unsubscribe() cherrypy.tree.mount(WebInterface(), str(options['http_root']), config=conf) if lazylibrarian.CHERRYPYLOG: cherrypy.config.update({ 'log.access_file': os.path.join(lazylibrarian.CONFIG['LOGDIR'], 'cherrypy.access.log'), 'log.error_file': os.path.join(lazylibrarian.CONFIG['LOGDIR'], 'cherrypy.error.log'), }) cherrypy.engine.autoreload.subscribe() try: if cp_ver and int(cp_ver.split('.')[0]) >= 10: portend.Checker().assert_free(str(options['http_host']), options['http_port']) else: cherrypy.process.servers.check_port(str(options['http_host']), options['http_port']) cherrypy.server.start() except Exception as e: print(str(e)) print('Failed to start on port: %i. Is something else running?' % (options['http_port'])) sys.exit(1) cherrypy.server.wait()
def test_check_port_nonlistening(self, nonlistening_addr): portend.Checker().assert_free(nonlistening_addr)
def test_check_port_listening(self, listening_addr): with pytest.raises(portend.PortNotFree): portend.Checker().assert_free(listening_addr)