Example #1
0
    def run(self):
        self.started = True

        # load languages
        tornado.locale.load_gettext_translations(sickrage.LOCALE_DIR,
                                                 'messages')

        # clear mako cache folder
        mako_cache = os.path.join(sickrage.app.cache_dir, 'mako')
        if os.path.isdir(mako_cache):
            shutil.rmtree(mako_cache)

        # video root
        if sickrage.app.config.root_dirs:
            root_dirs = sickrage.app.config.root_dirs.split('|')
            self.video_root = root_dirs[int(root_dirs[0]) + 1]

        # web root
        if sickrage.app.config.web_root:
            sickrage.app.config.web_root = sickrage.app.config.web_root = (
                '/' + sickrage.app.config.web_root.lstrip('/').strip('/'))

        # api root
        self.api_root = r'%s/api/%s' % (sickrage.app.config.web_root,
                                        sickrage.app.config.api_key)

        # tornado setup
        if sickrage.app.config.enable_https:
            # If either the HTTPS certificate or key do not exist, make some self-signed ones.
            if not (sickrage.app.config.https_cert and os.path.exists(
                    sickrage.app.config.https_cert)) or not (
                        sickrage.app.config.https_key
                        and os.path.exists(sickrage.app.config.https_key)):
                if not create_https_certificates(
                        sickrage.app.config.https_cert,
                        sickrage.app.config.https_key):
                    sickrage.app.log.info(
                        "Unable to create CERT/KEY files, disabling HTTPS")
                    sickrage.app.config.enable_https = False

            if not (os.path.exists(sickrage.app.config.https_cert)
                    and os.path.exists(sickrage.app.config.https_key)):
                sickrage.app.log.warning(
                    "Disabled HTTPS because of missing CERT and KEY files")
                sickrage.app.config.enable_https = False

        # Load the app
        self.app = Application(
            [
                # api
                (r'%s(/?.*)' % self.api_root, ApiHandler),

                # redirect to web root
                (r"(?!%s)(.*)" % sickrage.app.config.web_root, RedirectHandler,
                 {
                     "url": "%s/{0}" % sickrage.app.config.web_root
                 }),

                # api key
                (r'%s/getkey(/?.*)' % sickrage.app.config.web_root, KeyHandler
                 ),

                # api builder
                (r'%s/api/builder' % sickrage.app.config.web_root,
                 RedirectHandler, {
                     "url": sickrage.app.config.web_root + '/apibuilder/'
                 }),

                # login
                (r'%s/login(/?)' % sickrage.app.config.web_root, LoginHandler),

                # logout
                (r'%s/logout(/?)' % sickrage.app.config.web_root, LogoutHandler
                 ),

                # calendar
                (r'%s/calendar' % sickrage.app.config.web_root, CalendarHandler
                 ),

                # favicon
                (r'%s/(favicon\.ico)' % sickrage.app.config.web_root,
                 StaticFileHandler, {
                     "path":
                     os.path.join(sickrage.app.config.gui_static_dir,
                                  'images/favicon.ico')
                 }),

                # images
                (r'%s/images/(.*)' % sickrage.app.config.web_root,
                 StaticImageHandler, {
                     "path":
                     os.path.join(sickrage.app.config.gui_static_dir, 'images')
                 }),

                # css
                (r'%s/css/(.*)' % sickrage.app.config.web_root,
                 StaticFileHandler, {
                     "path":
                     os.path.join(sickrage.app.config.gui_static_dir, 'css')
                 }),

                # scss
                (r'%s/scss/(.*)' % sickrage.app.config.web_root,
                 StaticFileHandler, {
                     "path":
                     os.path.join(sickrage.app.config.gui_static_dir, 'scss')
                 }),

                # fonts
                (r'%s/fonts/(.*)' % sickrage.app.config.web_root,
                 StaticFileHandler, {
                     "path":
                     os.path.join(sickrage.app.config.gui_static_dir, 'fonts')
                 }),

                # javascript
                (r'%s/js/(.*)' % sickrage.app.config.web_root,
                 StaticFileHandler, {
                     "path": os.path.join(sickrage.app.config.gui_static_dir,
                                          'js')
                 }),

                # videos
                (r'%s/videos/(.*)' % sickrage.app.config.web_root,
                 StaticFileHandler, {
                     "path": self.video_root
                 }),
            ] + Route.get_routes(sickrage.app.config.web_root),
            debug=True,
            autoreload=False,
            gzip=sickrage.app.config.web_use_gzip,
            xheaders=sickrage.app.config.handle_reverse_proxy,
            cookie_secret=sickrage.app.config.web_cookie_secret,
            login_url='%s/login/' % sickrage.app.config.web_root)

        self.server = HTTPServer(self.app, no_keep_alive=True)

        if sickrage.app.config.enable_https:
            self.server.ssl_options = {
                "certfile": sickrage.app.config.https_cert,
                "keyfile": sickrage.app.config.https_key
            }

        try:
            self.server.listen(sickrage.app.config.web_port, None)

            sickrage.app.log.info("SiCKRAGE :: STARTED")
            sickrage.app.log.info("SiCKRAGE :: VERSION:[{}]".format(
                sickrage.app.version_updater.version))
            sickrage.app.log.info("SiCKRAGE :: CONFIG:[{}] [v{}]".format(
                sickrage.app.config_file, sickrage.app.config.config_version))
            sickrage.app.log.info("SiCKRAGE :: URL:[{}://{}:{}/]".format(
                ('http', 'https')[sickrage.app.config.enable_https],
                sickrage.app.config.web_host, sickrage.app.config.web_port))

            # launch browser window
            if all([
                    not sickrage.app.no_launch,
                    sickrage.app.config.launch_browser
            ]):
                threading.Thread(
                    None,
                    lambda: launch_browser(
                        ('http', 'https')[sickrage.app.config.enable_https
                                          ], sickrage.app.config.web_host,
                        sickrage.app.config.web_port),
                    name="LAUNCH-BROWSER").start()

            sickrage.app.io_loop.start()
        except socket.error as e:
            sickrage.app.log.warning(e.strerror)
            raise SystemExit
Example #2
0
    def start(self):
        self.started = True

        # video root
        self.video_root = None
        if sickrage.srCore.srConfig.ROOT_DIRS:
            root_dirs = sickrage.srCore.srConfig.ROOT_DIRS.split('|')
            self.video_root = root_dirs[int(root_dirs[0]) + 1]

        # web root
        if sickrage.srCore.srConfig.WEB_ROOT:
            sickrage.srCore.srConfig.WEB_ROOT = sickrage.srCore.srConfig.WEB_ROOT = (
                '/' + sickrage.srCore.srConfig.WEB_ROOT.lstrip('/').strip('/'))

        # api root
        if not sickrage.srCore.srConfig.API_KEY:
            sickrage.srCore.srConfig.API_KEY = generateApiKey()
        self.api_root = r'%s/api/%s' % (sickrage.srCore.srConfig.WEB_ROOT,
                                        sickrage.srCore.srConfig.API_KEY)

        # tornado setup
        if sickrage.srCore.srConfig.ENABLE_HTTPS:
            # If either the HTTPS certificate or key do not exist, make some self-signed ones.
            if not (sickrage.srCore.srConfig.HTTPS_CERT and os.path.exists(
                    sickrage.srCore.srConfig.HTTPS_CERT)) or not (
                        sickrage.srCore.srConfig.HTTPS_KEY and os.path.exists(
                            sickrage.srCore.srConfig.HTTPS_KEY)):
                if not create_https_certificates(
                        sickrage.srCore.srConfig.HTTPS_CERT,
                        sickrage.srCore.srConfig.HTTPS_KEY):
                    sickrage.srCore.srLogger.info(
                        "Unable to create CERT/KEY files, disabling HTTPS")
                    sickrage.srCore.srConfig.ENABLE_HTTPS = False

            if not (os.path.exists(sickrage.srCore.srConfig.HTTPS_CERT)
                    and os.path.exists(sickrage.srCore.srConfig.HTTPS_KEY)):
                sickrage.srCore.srLogger.warning(
                    "Disabled HTTPS because of missing CERT and KEY files")
                sickrage.srCore.srConfig.ENABLE_HTTPS = False

        # Load the app
        self.app = Application(
            [],
            debug=False,
            autoreload=False,
            gzip=sickrage.srCore.srConfig.WEB_USE_GZIP,
            xheaders=sickrage.srCore.srConfig.HANDLE_REVERSE_PROXY,
            cookie_secret=sickrage.srCore.srConfig.WEB_COOKIE_SECRET,
            login_url='%s/login/' % sickrage.srCore.srConfig.WEB_ROOT)

        # Main Handlers
        self.app.add_handlers(
            '.*$',
            [
                # webapi handler
                (r'%s(/?.*)' % self.api_root, ApiHandler),

                # webapi key retrieval
                (r'%s/getkey(/?.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 KeyHandler),

                # webapi builder redirect
                (r'%s/api/builder' % sickrage.srCore.srConfig.WEB_ROOT,
                 RedirectHandler, {
                     "url": sickrage.srCore.srConfig.WEB_ROOT + '/apibuilder/'
                 }),

                # webui login/logout handlers
                (r'%s/login(/?)' % sickrage.srCore.srConfig.WEB_ROOT,
                 LoginHandler),
                (r'%s/logout(/?)' % sickrage.srCore.srConfig.WEB_ROOT,
                 LogoutHandler),

                # webui handlers
            ] + Route.get_routes(sickrage.srCore.srConfig.WEB_ROOT))

        # Web calendar handler (Needed because option Unprotected calendar)
        self.app.add_handlers('.*$', [
            (r'%s/calendar' % sickrage.srCore.srConfig.WEB_ROOT,
             CalendarHandler),
        ])

        # Static File Handlers
        self.app.add_handlers(
            ".*$",
            [
                # favicon
                (r'%s/(favicon\.ico)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticFileHandler, {
                     "path":
                     os.path.join(sickrage.srCore.srConfig.GUI_DIR,
                                  'images/ico/favicon.ico')
                 }),

                # images
                (r'%s.*?/images/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticImageHandler, {
                     "path":
                     os.path.join(sickrage.srCore.srConfig.GUI_DIR, 'images')
                 }),

                # css
                (r'%s/css/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticFileHandler, {
                     "path": os.path.join(sickrage.srCore.srConfig.GUI_DIR,
                                          'css')
                 }),

                # scss
                (r'%s/scss/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticFileHandler, {
                     "path": os.path.join(sickrage.srCore.srConfig.GUI_DIR,
                                          'scss')
                 }),

                # fonts
                (r'%s/fonts/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticFileHandler, {
                     "path":
                     os.path.join(sickrage.srCore.srConfig.GUI_DIR, 'fonts')
                 }),

                # javascript
                (r'%s/js/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticFileHandler, {
                     "path": os.path.join(sickrage.srCore.srConfig.GUI_DIR,
                                          'js')
                 }),

                # videos
            ] + [(r'%s/videos/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                  StaticFileHandler, {
                      "path": self.video_root
                  })])

        self.server = HTTPServer(self.app, no_keep_alive=True)

        if sickrage.srCore.srConfig.ENABLE_HTTPS:
            self.server.ssl_options = {
                "certfile": sickrage.srCore.srConfig.HTTPS_CERT,
                "keyfile": sickrage.srCore.srConfig.HTTPS_KEY
            }

        try:
            self.server.listen(sickrage.srCore.srConfig.WEB_PORT, None)
        except socket.error as e:
            print(e.message)
            raise

        # launch browser window
        if all(
            [not sickrage.NOLAUNCH, sickrage.srCore.srConfig.LAUNCH_BROWSER]):
            threading.Thread(
                None, lambda: launch_browser(
                    ('http', 'https')[sickrage.srCore.srConfig.ENABLE_HTTPS],
                    get_lan_ip(), sickrage.srCore.srConfig.WEB_PORT)).start()

        # clear mako cache folder
        makocache = os.path.join(sickrage.srCore.srConfig.CACHE_DIR, 'mako')
        if os.path.isdir(makocache):
            shutil.rmtree(makocache)

        sickrage.srCore.srLogger.info("SiCKRAGE :: STARTED")
        sickrage.srCore.srLogger.info("SiCKRAGE :: VERSION:[{}]".format(
            sickrage.srCore.VERSIONUPDATER.updater.version))
        sickrage.srCore.srLogger.info("SiCKRAGE :: CONFIG:[{}]".format(
            sickrage.CONFIG_FILE))
        sickrage.srCore.srLogger.info("SiCKRAGE :: URL:[{}://{}:{}/]".format(
            ('http', 'https')[sickrage.srCore.srConfig.ENABLE_HTTPS],
            get_lan_ip(), sickrage.srCore.srConfig.WEB_PORT))
Example #3
0
    def start(self):
        self.started = True

        # clear mako cache folder
        mako_cache = os.path.join(sickrage.CACHE_DIR, 'mako')
        if os.path.isdir(mako_cache):
            shutil.rmtree(mako_cache)

        # video root
        if sickrage.srCore.srConfig.ROOT_DIRS:
            root_dirs = sickrage.srCore.srConfig.ROOT_DIRS.split('|')
            self.video_root = root_dirs[int(root_dirs[0]) + 1]

        # web root
        if sickrage.srCore.srConfig.WEB_ROOT:
            sickrage.srCore.srConfig.WEB_ROOT = sickrage.srCore.srConfig.WEB_ROOT = (
                '/' + sickrage.srCore.srConfig.WEB_ROOT.lstrip('/').strip('/'))

        # api root
        if not sickrage.srCore.srConfig.API_KEY:
            sickrage.srCore.srConfig.API_KEY = generateApiKey()
        self.api_root = r'%s/api/%s' % (sickrage.srCore.srConfig.WEB_ROOT,
                                        sickrage.srCore.srConfig.API_KEY)

        # tornado setup
        if sickrage.srCore.srConfig.ENABLE_HTTPS:
            # If either the HTTPS certificate or key do not exist, make some self-signed ones.
            if not (sickrage.srCore.srConfig.HTTPS_CERT and os.path.exists(
                    sickrage.srCore.srConfig.HTTPS_CERT)) or not (
                        sickrage.srCore.srConfig.HTTPS_KEY and os.path.exists(
                            sickrage.srCore.srConfig.HTTPS_KEY)):
                if not create_https_certificates(
                        sickrage.srCore.srConfig.HTTPS_CERT,
                        sickrage.srCore.srConfig.HTTPS_KEY):
                    sickrage.srCore.srLogger.info(
                        "Unable to create CERT/KEY files, disabling HTTPS")
                    sickrage.srCore.srConfig.ENABLE_HTTPS = False

            if not (os.path.exists(sickrage.srCore.srConfig.HTTPS_CERT)
                    and os.path.exists(sickrage.srCore.srConfig.HTTPS_KEY)):
                sickrage.srCore.srLogger.warning(
                    "Disabled HTTPS because of missing CERT and KEY files")
                sickrage.srCore.srConfig.ENABLE_HTTPS = False

        # Load the app
        self.app = Application(
            [
                # api
                (r'%s(/?.*)' % self.api_root, ApiHandler),

                # api key
                (r'%s/getkey(/?.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 KeyHandler),

                # api builder
                (r'%s/api/builder' % sickrage.srCore.srConfig.WEB_ROOT,
                 RedirectHandler, {
                     "url": sickrage.srCore.srConfig.WEB_ROOT + '/apibuilder/'
                 }),

                # login
                (r'%s/login(/?)' % sickrage.srCore.srConfig.WEB_ROOT,
                 LoginHandler),

                # logout
                (r'%s/logout(/?)' % sickrage.srCore.srConfig.WEB_ROOT,
                 LogoutHandler),

                # calendar
                (r'%s/calendar' % sickrage.srCore.srConfig.WEB_ROOT,
                 CalendarHandler),

                # favicon
                (r'%s/(favicon\.ico)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticFileHandler, {
                     "path":
                     os.path.join(sickrage.srCore.srConfig.GUI_DIR,
                                  'images/ico/favicon.ico')
                 }),

                # images
                (r'%s/images/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticImageHandler, {
                     "path":
                     os.path.join(sickrage.srCore.srConfig.GUI_DIR, 'images')
                 }),

                # css
                (r'%s/css/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticFileHandler, {
                     "path": os.path.join(sickrage.srCore.srConfig.GUI_DIR,
                                          'css')
                 }),

                # scss
                (r'%s/scss/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticFileHandler, {
                     "path": os.path.join(sickrage.srCore.srConfig.GUI_DIR,
                                          'scss')
                 }),

                # fonts
                (r'%s/fonts/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticFileHandler, {
                     "path":
                     os.path.join(sickrage.srCore.srConfig.GUI_DIR, 'fonts')
                 }),

                # javascript
                (r'%s/js/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticFileHandler, {
                     "path": os.path.join(sickrage.srCore.srConfig.GUI_DIR,
                                          'js')
                 }),

                # videos
                (r'%s/videos/(.*)' % sickrage.srCore.srConfig.WEB_ROOT,
                 StaticFileHandler, {
                     "path": self.video_root
                 }),
            ] + Route.get_routes(sickrage.srCore.srConfig.WEB_ROOT),
            debug=sickrage.DEBUG,
            autoreload=False,
            gzip=sickrage.srCore.srConfig.WEB_USE_GZIP,
            xheaders=sickrage.srCore.srConfig.HANDLE_REVERSE_PROXY,
            cookie_secret=sickrage.srCore.srConfig.WEB_COOKIE_SECRET,
            login_url='%s/login/' % sickrage.srCore.srConfig.WEB_ROOT)

        self.server = HTTPServer(self.app, no_keep_alive=True)

        if sickrage.srCore.srConfig.ENABLE_HTTPS:
            self.server.ssl_options = {
                "certfile": sickrage.srCore.srConfig.HTTPS_CERT,
                "keyfile": sickrage.srCore.srConfig.HTTPS_KEY
            }

        try:
            self.server.listen(sickrage.srCore.srConfig.WEB_PORT, None)
        except socket.error as e:
            print(e.message)
            raise
Example #4
0
    def start(self):
        self.started = True

        # load languages
        tornado.locale.load_gettext_translations(sickrage.LOCALE_DIR, 'messages')

        # clear mako cache folder
        mako_cache = os.path.join(sickrage.app.cache_dir, 'mako')
        if os.path.isdir(mako_cache):
            shutil.rmtree(mako_cache)

        # video root
        if sickrage.app.config.root_dirs:
            root_dirs = sickrage.app.config.root_dirs.split('|')
            self.video_root = root_dirs[int(root_dirs[0]) + 1]

        # web root
        if sickrage.app.config.web_root:
            sickrage.app.config.web_root = sickrage.app.config.web_root = (
                    '/' + sickrage.app.config.web_root.lstrip('/').strip('/'))

        # api root
        self.api_root = r'%s/api/%s' % (sickrage.app.config.web_root, sickrage.app.config.api_key)

        # tornado setup
        if sickrage.app.config.enable_https:
            # If either the HTTPS certificate or key do not exist, make some self-signed ones.
            if not (
                    sickrage.app.config.https_cert and os.path.exists(
                sickrage.app.config.https_cert)) or not (
                    sickrage.app.config.https_key and os.path.exists(sickrage.app.config.https_key)):
                if not create_https_certificates(sickrage.app.config.https_cert,
                                                 sickrage.app.config.https_key):
                    sickrage.app.log.info("Unable to create CERT/KEY files, disabling HTTPS")
                    sickrage.app.config.enable_https = False

            if not (os.path.exists(sickrage.app.config.https_cert) and os.path.exists(
                    sickrage.app.config.https_key)):
                sickrage.app.log.warning("Disabled HTTPS because of missing CERT and KEY files")
                sickrage.app.config.enable_https = False

        # Load the app
        self.app = Application(
            debug=True,
            autoreload=False,
            gzip=sickrage.app.config.web_use_gzip,
            cookie_secret=sickrage.app.config.web_cookie_secret,
            login_url='%s/login/' % sickrage.app.config.web_root)

        # Websocket handler
        self.app.add_handlers(".*$", [
            (r'%s/ws/ui' % sickrage.app.config.web_root, WebSocketUIHandler)
        ])

        # Static File Handlers
        self.app.add_handlers('.*$', [
            # api
            (r'%s/api/(\w{32})(/?.*)' % sickrage.app.config.web_root, ApiHandler),

            # redirect to home
            (r"(%s)" % sickrage.app.config.web_root, RedirectHandler,
             {"url": "%s/home" % sickrage.app.config.web_root}),

            # api builder
            (r'%s/api/builder' % sickrage.app.config.web_root, RedirectHandler,
             {"url": sickrage.app.config.web_root + '/apibuilder/'}),

            # login
            (r'%s/login(/?)' % sickrage.app.config.web_root, LoginHandler),

            # logout
            (r'%s/logout(/?)' % sickrage.app.config.web_root, LogoutHandler),

            # calendar
            (r'%s/calendar' % sickrage.app.config.web_root, CalendarHandler),

            # favicon
            (r'%s/(favicon\.ico)' % sickrage.app.config.web_root, StaticNoCacheFileHandler,
             {"path": os.path.join(sickrage.app.config.gui_static_dir, 'images/favicon.ico')}),

            # images
            (r'%s/images/(.*)' % sickrage.app.config.web_root, StaticImageHandler,
             {"path": os.path.join(sickrage.app.config.gui_static_dir, 'images')}),

            # css
            (r'%s/css/(.*)' % sickrage.app.config.web_root, StaticNoCacheFileHandler,
             {"path": os.path.join(sickrage.app.config.gui_static_dir, 'css')}),

            # scss
            (r'%s/scss/(.*)' % sickrage.app.config.web_root, StaticNoCacheFileHandler,
             {"path": os.path.join(sickrage.app.config.gui_static_dir, 'scss')}),

            # fonts
            (r'%s/fonts/(.*)' % sickrage.app.config.web_root, StaticNoCacheFileHandler,
             {"path": os.path.join(sickrage.app.config.gui_static_dir, 'fonts')}),

            # javascript
            (r'%s/js/(.*)' % sickrage.app.config.web_root, StaticNoCacheFileHandler,
             {"path": os.path.join(sickrage.app.config.gui_static_dir, 'js')}),

            # videos
            (r'%s/videos/(.*)' % sickrage.app.config.web_root, StaticNoCacheFileHandler,
             {"path": self.video_root}),
        ])

        # Web Handlers
        self.app.add_handlers('.*$', Route.get_routes(sickrage.app.config.web_root))

        # HTTPS Cert/Key object
        ssl_ctx = None
        if sickrage.app.config.enable_https:
            ssl_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
            ssl_ctx.load_cert_chain(sickrage.app.config.https_cert, sickrage.app.config.https_key)

        # Web Server
        self.server = HTTPServer(self.app, ssl_options=ssl_ctx, xheaders=sickrage.app.config.handle_reverse_proxy)

        try:
            self.server.listen(sickrage.app.config.web_port)

            sickrage.app.log.info(
                "SiCKRAGE :: STARTED")
            sickrage.app.log.info(
                "SiCKRAGE :: VERSION:[{}]".format(sickrage.version()))
            sickrage.app.log.info(
                "SiCKRAGE :: CONFIG:[{}] [v{}]".format(sickrage.app.config_file, sickrage.app.config.config_version))
            sickrage.app.log.info(
                "SiCKRAGE :: DATABASE:[v{}]".format(sickrage.app.main_db.version))
            sickrage.app.log.info(
                "SiCKRAGE :: URL:[{}://{}:{}{}]".format(('http', 'https')[sickrage.app.config.enable_https],
                                                        sickrage.app.config.web_host, sickrage.app.config.web_port,
                                                        sickrage.app.config.web_root))
        except socket.error as e:
            sickrage.app.log.warning(e.strerror)
            raise SystemExit
Example #5
0
    def run(self):
        self.started = True

        # load languages
        tornado.locale.load_gettext_translations(sickrage.LOCALE_DIR, 'messages')

        # clear mako cache folder
        mako_cache = os.path.join(sickrage.app.cache_dir, 'mako')
        if os.path.isdir(mako_cache):
            shutil.rmtree(mako_cache)

        # video root
        if sickrage.app.config.root_dirs:
            root_dirs = sickrage.app.config.root_dirs.split('|')
            self.video_root = root_dirs[int(root_dirs[0]) + 1]

        # web root
        if sickrage.app.config.web_root:
            sickrage.app.config.web_root = sickrage.app.config.web_root = (
                '/' + sickrage.app.config.web_root.lstrip('/').strip('/'))

        # api root
        self.api_root = r'%s/api/%s' % (sickrage.app.config.web_root, sickrage.app.config.api_key)

        # tornado setup
        if sickrage.app.config.enable_https:
            # If either the HTTPS certificate or key do not exist, make some self-signed ones.
            if not (
                        sickrage.app.config.https_cert and os.path.exists(
                        sickrage.app.config.https_cert)) or not (
                        sickrage.app.config.https_key and os.path.exists(sickrage.app.config.https_key)):
                if not create_https_certificates(sickrage.app.config.https_cert,
                                                 sickrage.app.config.https_key):
                    sickrage.app.log.info("Unable to create CERT/KEY files, disabling HTTPS")
                    sickrage.app.config.enable_https = False

            if not (os.path.exists(sickrage.app.config.https_cert) and os.path.exists(
                    sickrage.app.config.https_key)):
                sickrage.app.log.warning("Disabled HTTPS because of missing CERT and KEY files")
                sickrage.app.config.enable_https = False

        # Load the app
        self.app = Application(
            [
                # api
                (r'%s(/?.*)' % self.api_root, ApiHandler),

                # redirect to web root
                (r"(?!%s)(.*)" % sickrage.app.config.web_root, RedirectHandler,
                 {"url": "%s/{0}" % sickrage.app.config.web_root}),

                # api key
                (r'%s/getkey(/?.*)' % sickrage.app.config.web_root, KeyHandler),

                # api builder
                (r'%s/api/builder' % sickrage.app.config.web_root, RedirectHandler,
                 {"url": sickrage.app.config.web_root + '/apibuilder/'}),

                # login
                (r'%s/login(/?)' % sickrage.app.config.web_root, LoginHandler),

                # logout
                (r'%s/logout(/?)' % sickrage.app.config.web_root, LogoutHandler),

                # calendar
                (r'%s/calendar' % sickrage.app.config.web_root, CalendarHandler),

                # favicon
                (r'%s/(favicon\.ico)' % sickrage.app.config.web_root, StaticFileHandler,
                 {"path": os.path.join(sickrage.app.config.gui_static_dir, 'images/favicon.ico')}),

                # images
                (r'%s/images/(.*)' % sickrage.app.config.web_root, StaticImageHandler,
                 {"path": os.path.join(sickrage.app.config.gui_static_dir, 'images')}),

                # css
                (r'%s/css/(.*)' % sickrage.app.config.web_root, StaticFileHandler,
                 {"path": os.path.join(sickrage.app.config.gui_static_dir, 'css')}),

                # scss
                (r'%s/scss/(.*)' % sickrage.app.config.web_root, StaticFileHandler,
                 {"path": os.path.join(sickrage.app.config.gui_static_dir, 'scss')}),

                # fonts
                (r'%s/fonts/(.*)' % sickrage.app.config.web_root, StaticFileHandler,
                 {"path": os.path.join(sickrage.app.config.gui_static_dir, 'fonts')}),

                # javascript
                (r'%s/js/(.*)' % sickrage.app.config.web_root, StaticFileHandler,
                 {"path": os.path.join(sickrage.app.config.gui_static_dir, 'js')}),

                # videos
                (r'%s/videos/(.*)' % sickrage.app.config.web_root, StaticFileHandler,
                 {"path": self.video_root}),
            ] + Route.get_routes(sickrage.app.config.web_root),
            debug=True,
            autoreload=False,
            gzip=sickrage.app.config.web_use_gzip,
            xheaders=sickrage.app.config.handle_reverse_proxy,
            cookie_secret=sickrage.app.config.web_cookie_secret,
            login_url='%s/login/' % sickrage.app.config.web_root)

        self.server = HTTPServer(self.app, no_keep_alive=True)

        if sickrage.app.config.enable_https: self.server.ssl_options = {
            "certfile": sickrage.app.config.https_cert,
            "keyfile": sickrage.app.config.https_key
        }

        try:
            self.server.listen(sickrage.app.config.web_port, None)

            sickrage.app.log.info(
                "SiCKRAGE :: STARTED")
            sickrage.app.log.info(
                "SiCKRAGE :: VERSION:[{}]".format(sickrage.app.version_updater.version))
            sickrage.app.log.info(
                "SiCKRAGE :: CONFIG:[{}] [v{}]".format(sickrage.app.config_file, sickrage.app.config.config_version))
            sickrage.app.log.info(
                "SiCKRAGE :: URL:[{}://{}:{}/]".format(
                    ('http', 'https')[sickrage.app.config.enable_https],
                    sickrage.app.config.web_host, sickrage.app.config.web_port))

            # launch browser window
            if all([not sickrage.app.no_launch, sickrage.app.config.launch_browser]):
                threading.Thread(None,
                                 lambda: launch_browser(
                                     ('http', 'https')[sickrage.app.config.enable_https],
                                     sickrage.app.config.web_host,
                                     sickrage.app.config.web_port
                                 ), name="LAUNCH-BROWSER").start()

            sickrage.app.io_loop.start()
        except socket.error as e:
            sickrage.app.log.warning(e.strerror)
            raise SystemExit
Example #6
0
    def start(self):
        self.started = True

        # video root
        self.video_root = None
        if sickrage.srCore.srConfig.ROOT_DIRS:
            root_dirs = sickrage.srCore.srConfig.ROOT_DIRS.split('|')
            self.video_root = root_dirs[int(root_dirs[0]) + 1]

        # web root
        if sickrage.srCore.srConfig.WEB_ROOT:
            sickrage.srCore.srConfig.WEB_ROOT = sickrage.srCore.srConfig.WEB_ROOT = (
                '/' + sickrage.srCore.srConfig.WEB_ROOT.lstrip('/').strip('/'))

        # api root
        if not sickrage.srCore.srConfig.API_KEY:
            sickrage.srCore.srConfig.API_KEY = generateApiKey()
        self.api_root = r'%s/api/%s' % (sickrage.srCore.srConfig.WEB_ROOT, sickrage.srCore.srConfig.API_KEY)

        # tornado setup
        if sickrage.srCore.srConfig.ENABLE_HTTPS:
            # If either the HTTPS certificate or key do not exist, make some self-signed ones.
            if not (
                        sickrage.srCore.srConfig.HTTPS_CERT and os.path.exists(
                        sickrage.srCore.srConfig.HTTPS_CERT)) or not (
                        sickrage.srCore.srConfig.HTTPS_KEY and os.path.exists(sickrage.srCore.srConfig.HTTPS_KEY)):
                if not create_https_certificates(sickrage.srCore.srConfig.HTTPS_CERT,
                                                 sickrage.srCore.srConfig.HTTPS_KEY):
                    sickrage.srCore.srLogger.info("Unable to create CERT/KEY files, disabling HTTPS")
                    sickrage.srCore.srConfig.ENABLE_HTTPS = False

            if not (os.path.exists(sickrage.srCore.srConfig.HTTPS_CERT) and os.path.exists(
                    sickrage.srCore.srConfig.HTTPS_KEY)):
                sickrage.srCore.srLogger.warning("Disabled HTTPS because of missing CERT and KEY files")
                sickrage.srCore.srConfig.ENABLE_HTTPS = False

        # Load the app
        self.app = Application([],
                               debug=False,
                               autoreload=False,
                               gzip=sickrage.srCore.srConfig.WEB_USE_GZIP,
                               xheaders=sickrage.srCore.srConfig.HANDLE_REVERSE_PROXY,
                               cookie_secret=sickrage.srCore.srConfig.WEB_COOKIE_SECRET,
                               login_url='%s/login/' % sickrage.srCore.srConfig.WEB_ROOT)

        # Main Handlers
        self.app.add_handlers('.*$', [
            # webapi handler
            (r'%s(/?.*)' % self.api_root, ApiHandler),

            # webapi key retrieval
            (r'%s/getkey(/?.*)' % sickrage.srCore.srConfig.WEB_ROOT, KeyHandler),

            # webapi builder redirect
            (r'%s/api/builder' % sickrage.srCore.srConfig.WEB_ROOT, RedirectHandler,
             {"url": sickrage.srCore.srConfig.WEB_ROOT + '/apibuilder/'}),

            # webui login/logout handlers
            (r'%s/login(/?)' % sickrage.srCore.srConfig.WEB_ROOT, LoginHandler),
            (r'%s/logout(/?)' % sickrage.srCore.srConfig.WEB_ROOT, LogoutHandler),

            # webui handlers
        ] + Route.get_routes(sickrage.srCore.srConfig.WEB_ROOT))

        # Web calendar handler (Needed because option Unprotected calendar)
        self.app.add_handlers('.*$', [
            (r'%s/calendar' % sickrage.srCore.srConfig.WEB_ROOT, CalendarHandler),
        ])

        # Static File Handlers
        self.app.add_handlers(".*$", [
            # favicon
            (r'%s/(favicon\.ico)' % sickrage.srCore.srConfig.WEB_ROOT, StaticFileHandler,
             {"path": os.path.join(sickrage.srCore.srConfig.GUI_DIR, 'images/ico/favicon.ico')}),

            # images
            (r'%s.*?/images/(.*)' % sickrage.srCore.srConfig.WEB_ROOT, StaticImageHandler,
             {"path": os.path.join(sickrage.srCore.srConfig.GUI_DIR, 'images')}),

            # css
            (r'%s/css/(.*)' % sickrage.srCore.srConfig.WEB_ROOT, StaticFileHandler,
             {"path": os.path.join(sickrage.srCore.srConfig.GUI_DIR, 'css')}),

            # scss
            (r'%s/scss/(.*)' % sickrage.srCore.srConfig.WEB_ROOT, StaticFileHandler,
             {"path": os.path.join(sickrage.srCore.srConfig.GUI_DIR, 'scss')}),

            # fonts
            (r'%s/fonts/(.*)' % sickrage.srCore.srConfig.WEB_ROOT, StaticFileHandler,
             {"path": os.path.join(sickrage.srCore.srConfig.GUI_DIR, 'fonts')}),

            # javascript
            (r'%s/js/(.*)' % sickrage.srCore.srConfig.WEB_ROOT, StaticFileHandler,
             {"path": os.path.join(sickrage.srCore.srConfig.GUI_DIR, 'js')}),

            # videos
        ] + [(r'%s/videos/(.*)' % sickrage.srCore.srConfig.WEB_ROOT, StaticFileHandler,
              {"path": self.video_root})])

        self.server = HTTPServer(self.app, no_keep_alive=True)

        if sickrage.srCore.srConfig.ENABLE_HTTPS: self.server.ssl_options = {
            "certfile": sickrage.srCore.srConfig.HTTPS_CERT,
            "keyfile": sickrage.srCore.srConfig.HTTPS_KEY
        }

        try:
            self.server.listen(sickrage.srCore.srConfig.WEB_PORT, None)
        except socket.error as e:
            print(e.message)
            raise

        # launch browser window
        if all([not sickrage.NOLAUNCH, sickrage.srCore.srConfig.LAUNCH_BROWSER]):
            threading.Thread(None,
                             lambda: launch_browser(
                                 ('http', 'https')[sickrage.srCore.srConfig.ENABLE_HTTPS],
                                 get_lan_ip(),
                                 sickrage.srCore.srConfig.WEB_PORT
                             )).start()

        # clear mako cache folder
        makocache = os.path.join(sickrage.srCore.srConfig.CACHE_DIR, 'mako')
        if os.path.isdir(makocache):
            shutil.rmtree(makocache)

        sickrage.srCore.srLogger.info("SiCKRAGE :: STARTED")
        sickrage.srCore.srLogger.info("SiCKRAGE :: VERSION:[{}]".format(sickrage.srCore.VERSIONUPDATER.updater.version))
        sickrage.srCore.srLogger.info("SiCKRAGE :: CONFIG:[{}]".format(sickrage.CONFIG_FILE))
        sickrage.srCore.srLogger.info("SiCKRAGE :: URL:[{}://{}:{}/]".format(
            ('http', 'https')[sickrage.srCore.srConfig.ENABLE_HTTPS],
            get_lan_ip(), sickrage.srCore.srConfig.WEB_PORT))