def prepare(self): if not self.request.headers.get("Referer"): raise APIException("auth_failed", "Error reporting cannot be made from an external address. (no referral)") refhost = urlsplit(self.request.headers.get("Referer")).hostname failed = True if refhost in config.station_hostnames: failed = False elif config.has("accept_error_reports_from_hosts") and refhost in config.get("accept_error_reports_from_hosts"): failed = False if failed: raise APIException("auth_failed", "Error reporting cannot be made from an external address. (%s)" % refhost) else: return super(ErrorReport, self).prepare()
def open(): global connection global c global c_old if c or c_old: close() type = config.get("db_type") name = config.get("db_name") host = config.get("db_host") port = config.get("db_port") user = config.get("db_user") password = config.get("db_password") if type == "postgres": psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY) base_connstr = "sslmode=disable " if host: base_connstr += "host=%s " % host if port: base_connstr += "port=%s " % port if user: base_connstr += "user=%s " % user if password: base_connstr += "password=%s " % password connection = psycopg2.connect(base_connstr + ("dbname=%s" % name)) connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) connection.autocommit = True c = connection.cursor(cursor_factory=PostgresCursor) if config.has("db_USE_LIVE_R3") and config.get("db_USE_LIVE_R3"): c_old = None connection = psycopg2.connect(base_connstr + "dbname=rainwave") connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) connection.autocommit = True c_old = connection.cursor(cursor_factory=PostgresCursor) else: c_old = c elif type == "sqlite": log.debug("dbopen", "Opening SQLite DB %s" % name) c = SQLiteCursor(name) c_old = c else: log.critical("dbopen", "Invalid DB type %s!" % type) return False return True
def prepare(self): if not self.request.headers.get("Referer"): raise APIException( "auth_failed", "Error reporting cannot be made from an external address. (no referral)", ) refhost = urlsplit(self.request.headers.get("Referer")).hostname failed = True if refhost == config.get("hostname"): failed = False elif config.has("accept_error_reports_from_hosts") and refhost in config.get( "accept_error_reports_from_hosts" ): failed = False if failed: raise APIException( "auth_failed", "Error reporting cannot be made from an external address. (%s)" % refhost, ) else: return super(ErrorReport, self).prepare()
def connect(): global _memcache global _memcache_ratings _memcache_behaviors["ketama"] = config.has( "memcached_ketama") and config.get("memcache_ketama") if _memcache: return if config.get("memcache_fake"): _memcache = TestModeCache() _memcache_ratings = TestModeCache() reset_station_caches() else: _memcache = libmc.Client(config.get("memcache_servers"), binary=True) _memcache.set_behaviors(_memcache_behaviors) # memcache doesn't test its connection on start, so we force a get _memcache.get("hello") _memcache_ratings = libmc.Client( config.get("memcache_ratings_servers"), binary=True) _memcache_ratings.set_behaviors(_memcache_behaviors) _memcache_ratings.get("hello")
class MainIndex(api.web.HTMLRequest): description = "Main Rainwave page." auth_required = config.has("index_requires_login") and config.get( "index_requires_login") login_required = config.has("index_requires_login") and config.get( "index_requires_login") sid_required = False beta = False page_template = "r5_index.html" js_dir = "js5" def set_default_headers(self): self.set_header("X-Frame-Options", "SAMEORIGIN") def prepare(self): super(MainIndex, self).prepare() if not cache.get_station(self.sid, "sched_current"): raise APIException( "server_just_started", "Rainwave is Rebooting, Please Try Again in a Few Minutes", http_code=500) # self.json_payload = {} self.jsfiles = None if not self.user: self.user = User(1) self.user.ensure_api_key() if self.beta or config.get("web_developer_mode") or config.get( "developer_mode") or config.get("test_mode"): buildtools.bake_beta_css() buildtools.bake_beta_templates() self.jsfiles = [] for root, subdirs, files in os.walk( os.path.join(os.path.dirname(__file__), "../static/%s" % self.js_dir)): #pylint: disable=W0612 for f in files: if f.endswith(".js"): self.jsfiles.append( os.path.join( root[root.find("static/%s" % self.js_dir):], f)) # def append(self, key, value): # self.json_payload[key] = value def get(self): self.mobile = self.request.headers.get("User-Agent").lower().find( "mobile") != -1 or self.request.headers.get( "User-Agent").lower().find("android") != -1 # if not self.beta: # info.attach_info_to_request(self, extra_list=self.get_cookie("r4_active_list")) # self.append("api_info", { "time": int(timestamp()) }) page_title = None if (self.sid == config.get("default_station")): page_title = self.locale.translate("page_title_on_google") else: page_title = "%s %s" % ( self.locale.translate("page_title_on_google"), self.locale.translate("station_name_%s" % self.sid)) self.render( self.page_template, request=self, site_description=self.locale.translate( "station_description_id_%s" % self.sid), revision_number=config.build_number, jsfiles=self.jsfiles, # api_url=config.get("api_external_url_prefix"), # cookie_domain=config.get("cookie_domain"), # locales=api.locale.locale_names_json, # relays=config.public_relays_json[self.sid], # stream_filename=config.get_station(self.sid, "stream_filename"), # station_list=config.station_list_json, mobile=self.mobile, station_name=page_title, dj=self.user.is_dj())
class MainIndex(api.web.HTMLRequest): description = "Main Rainwave page." auth_required = config.has("index_requires_login") and config.get( "index_requires_login" ) login_required = config.has("index_requires_login") and config.get( "index_requires_login" ) sid_required = False beta = False page_template = "r5_index.html" js_dir = "js5" jsfiles = None def set_default_headers(self): self.set_header("X-Frame-Options", "SAMEORIGIN") self.set_header("X-XSS-Protection", "1; mode=block") self.set_header("X-Content-Type-Options", "nosniff") if self.request.protocol == "https": self.set_header("Content-Security-Policy", config.csp_header) self.set_header("Referrer-Policy", "origin") self.set_header("Strict-Transport-Security", "max-age=63072000; preload") def prepare(self): if self.path_kwargs.get("station"): self.sid = config.stream_filename_to_sid.get(self.path_kwargs["station"]) if not self.sid: self.redirect(config.get("base_site_url")) return super(MainIndex, self).prepare() if self.path_kwargs.get("station") is None: self.redirect( "{}{}/".format( config.get("base_site_url"), config.station_mount_filenames[self.sid], ) ) return if config.get("enforce_ssl") and self.request.protocol != "https": self.redirect( "{}{}/".format( config.get("base_site_url"), config.station_mount_filenames[self.sid], ) ) return if not config.get("developer_mode") and self.request.host != config.get( "hostname" ): self.redirect( "{}{}/".format( config.get("base_site_url"), config.station_mount_filenames[self.sid], ) ) return if not cache.get_station(self.sid, "sched_current"): raise APIException( "server_just_started", "Rainwave is Rebooting, Please Try Again in a Few Minutes", http_code=500, ) self.jsfiles = None if not self.user: self.user = User(1) self.user.ip_address = self.request.remote_ip self.user.ensure_api_key() if ( self.beta or config.get("developer_mode") ): buildtools.bake_beta_css() buildtools.bake_beta_templates() self.jsfiles = [] for root, _subdirs, files in os.walk( os.path.join(os.path.dirname(__file__), "../static/%s" % self.js_dir) ): for f in files: if f.endswith(".js"): self.jsfiles.append( os.path.join( root[root.find("static/%s" % self.js_dir) :], f ) ) def get(self, station=None): self.mobile = False ua = self.request.headers.get("User-Agent") or '' if ua: self.mobile = ( ua.lower().find("mobile") != -1 or ua.lower().find("android") != -1 ) page_title = None if self.sid == config.get("default_station"): page_title = self.locale.translate("page_title_on_google") else: page_title = "%s %s" % ( self.locale.translate("page_title_on_google"), self.locale.translate("station_name_%s" % self.sid), ) self.render( self.page_template, request=self, site_description=self.locale.translate( "station_description_id_%s" % self.sid ), revision_number=config.build_number, jsfiles=self.jsfiles, mobile=self.mobile, station_name=page_title, dj=self.user.is_dj(), )
class MainIndex(api.web.HTMLRequest): description = "Main Rainwave page." auth_required = config.has("index_requires_login") and config.get( "index_requires_login") login_required = config.has("index_requires_login") and config.get( "index_requires_login") sid_required = False beta = False page_template = "r5_index.html" js_dir = "js5" def set_default_headers(self): self.set_header("X-Frame-Options", "SAMEORIGIN") self.set_header("X-XSS-Protection", "1; mode=block") self.set_header("X-Content-Type-Options", "nosniff") if self.request.protocol == 'https': self.set_header("Content-Security-Policy", config.csp_header) self.set_header("Referrer-Policy", "origin") self.set_header("Strict-Transport-Security", "max-age=63072000; includeSubDomains; preload") def prepare(self): if self.path_kwargs.get('station'): self.sid = config.stream_filename_to_sid.get( self.path_kwargs['station']) if not self.sid: self.redirect(config.get("base_site_url")) return super(MainIndex, self).prepare() if self.path_kwargs.get('station') is None: self.redirect("{}{}/".format( config.get("base_site_url"), config.station_mount_filenames[self.sid]), permanent=True) return if config.get("enforce_ssl") and self.request.protocol != "https": self.redirect("{}{}/".format( config.get("base_site_url"), config.station_mount_filenames[self.sid]), permanent=True) return if not config.get("developer_mode" ) and self.request.host != config.get("hostname"): self.redirect("{}{}/".format( config.get("base_site_url"), config.station_mount_filenames[self.sid]), permanent=True) return if not cache.get_station(self.sid, "sched_current"): raise APIException( "server_just_started", "Rainwave is Rebooting, Please Try Again in a Few Minutes", http_code=500) # self.json_payload = {} self.jsfiles = None if not self.user: self.user = User(1) self.user.ip_address = self.request.remote_ip self.user.ensure_api_key() if self.beta or config.get("web_developer_mode") or config.get( "developer_mode") or config.get("test_mode"): buildtools.bake_beta_css() buildtools.bake_beta_templates() self.jsfiles = [] for root, subdirs, files in os.walk( os.path.join(os.path.dirname(__file__), "../static/%s" % self.js_dir)): #pylint: disable=W0612 for f in files: if f.endswith(".js"): self.jsfiles.append( os.path.join( root[root.find("static/%s" % self.js_dir):], f)) # def append(self, key, value): # self.json_payload[key] = value def get(self, station=None): self.mobile = self.request.headers.get("User-Agent").lower().find( "mobile") != -1 or self.request.headers.get( "User-Agent").lower().find("android") != -1 # if not self.beta: # info.attach_info_to_request(self, extra_list=self.get_cookie("r4_active_list")) # self.append("api_info", { "time": int(timestamp()) }) page_title = None if (self.sid == config.get("default_station")): page_title = self.locale.translate("page_title_on_google") else: page_title = "%s %s" % ( self.locale.translate("page_title_on_google"), self.locale.translate("station_name_%s" % self.sid)) self.render( self.page_template, request=self, site_description=self.locale.translate( "station_description_id_%s" % self.sid), revision_number=config.build_number, jsfiles=self.jsfiles, # api_url=config.get("api_external_url_prefix"), # cookie_domain=config.get("cookie_domain"), # locales=api.locale.locale_names_json, # relays=config.public_relays_json[self.sid], # stream_filename=config.get_station(self.sid, "stream_filename"), # station_list=config.station_list_json, mobile=self.mobile, station_name=page_title, dj=self.user.is_dj())