def call_api(func, args=""): add_header(response) s = request.environ.get('beaker.session') if 'session' in request.POST: # removes "' so it works on json strings s = s.get_by_id(remove_chars(request.POST['session'], "'\"")) api = get_user_api(s) if not api: return HTTPError(403, dumps("Forbidden")) if not PYLOAD.isAuthorized(func, api.user): return HTTPError(401, dumps("Unauthorized")) args = args.split("/")[1:] kwargs = {} for x, y in chain(request.GET.iteritems(), request.POST.iteritems()): if x == "session": continue kwargs[x] = unquote(y) try: return callApi(func, *args, **kwargs) except Exception, e: print_exc() return HTTPError( 500, dumps({ "error": e.message, "traceback": format_exc() }))
def call_api(func, args=""): add_header(response) s = request.environ.get('beaker.session') if 'session' in request.POST: # removes "' so it works on json strings s = s.get_by_id(remove_chars(request.POST['session'], "'\"")) api = get_user_api(s) if not api: return HTTPError(403, dumps("Forbidden")) if not PYLOAD.isAuthorized(func, api.user): return HTTPError(401, dumps("Unauthorized")) args = args.split("/")[1:] kwargs = {} for x, y in chain(request.GET.iteritems(), request.POST.iteritems()): if x == "session": continue kwargs[x] = unquote(y) try: return callApi(func, *args, **kwargs) except Exception, e: print_exc() return HTTPError(500, dumps({"error": e.message, "traceback": format_exc()}))
def loadPatterns(self): page = getURL("http://linkdecrypter.com/") m = re.search(r'<b>Supported\(\d+\)</b>: <i>([^+<]*)', page) if not m: self.logError(_("Crypter list not found")) return builtin = [name.lower() for name in self.core.pluginManager.crypterPlugins.keys()] builtin.extend(["downloadserienjunkiesorg"]) crypter_pattern = re.compile("(\w[\w.-]+)") online = [] for crypter in m.group(1).split(', '): m = re.match(crypter_pattern, crypter) if m and remove_chars(m.group(1), "-.") not in builtin: online.append(m.group(1).replace(".", "\\.")) if not online: self.logError(_("Crypter list is empty")) return regexp = r"https?://([^.]+\.)*?(%s)/.*" % "|".join(online) dict = self.core.pluginManager.crypterPlugins[self.__name__] dict["pattern"] = regexp dict["re"] = re.compile(regexp) self.logDebug("REGEXP: " + regexp)
def downloadFile(self, pyfile): url = pyfile.url for i in range(5): header = self.load(url, just_header=True) # self.load does not raise a BadHeader on 404 responses, do it here if 'code' in header and header['code'] == 404: raise BadHeader(404) if 'location' in header: self.logDebug("Location: " + header['location']) url = unquote(header['location']) else: break name = html_unescape(unquote(urlparse(url).path.split("/")[-1])) if 'content-disposition' in header: self.logDebug("Content-Disposition: " + header['content-disposition']) m = search("filename(?P<type>=|\*=(?P<enc>.+)'')(?P<name>.*)", header['content-disposition']) if m: disp = m.groupdict() self.logDebug(disp) if not disp['enc']: disp['enc'] = 'utf-8' name = remove_chars(disp['name'], "\"';").strip() name = unicode(unquote(name), disp['enc']) if not name: name = url pyfile.name = name self.logDebug("Filename: %s" % pyfile.name) self.download(url, disposition=True)
def coreReady(self): page = getURL("http://linkdecrypter.com/") m = re.search(r'<b>Supported\(\d+\)</b>: <i>([^+<]*)', page) if not m: self.logError(_("Crypter list not found")) return builtin = [ name.lower() for name in self.core.pluginManager.crypterPlugins.keys() ] builtin.extend(["downloadserienjunkiesorg"]) crypter_pattern = re.compile("(\w[\w.-]+)") online = [] for crypter in m.group(1).split(', '): m = re.match(crypter_pattern, crypter) if m and remove_chars(m.group(1), "-.") not in builtin: online.append(m.group(1).replace(".", "\\.")) if not online: self.logError(_("Crypter list is empty")) return regexp = r"https?://([^.]+\.)*?(%s)/.*" % "|".join(online) dict = self.core.pluginManager.crypterPlugins[self.__name__] dict["pattern"] = regexp dict["re"] = re.compile(regexp) self.logDebug("REGEXP: " + regexp)
def call_api(func, args=""): add_header(response) s = request.environ.get('beaker.session') auth = parse_auth(request.get_header('Authorization', '')) # TODO: session as GET if 'session' in request.POST: # removes "' so it works on json strings s = s.get_by_id(remove_chars(request.POST['session'], "'\"")) elif auth: user = PYLOAD.checkAuth(auth[0], auth[1], request.environ.get('REMOTE_ADDR', None)) # if auth is correct create a pseudo session if user: s = {'uid': user.uid} api = get_user_api(s) if not api: return HTTPError(403, dumps("Forbidden")) if not PYLOAD.isAuthorized(func, api.user): return HTTPError(401, dumps("Unauthorized")) args = args.split("/")[1:] kwargs = {} for x, y in chain(request.GET.iteritems(), request.POST.iteritems()): if x == "session": continue kwargs[x] = unquote(y) try: return callApi(api, func, *args, **kwargs) except ExceptionObject, e: return HTTPError(400, dumps(e))
def downloadFile(self, pyfile): header = self.load(pyfile.url, just_header = True) #self.logDebug(header) # self.load does not raise a BadHeader on 404 responses, do it here if header.has_key('code') and header['code'] == 404: raise BadHeader(404) if 'location' in header: self.logDebug("Location: " + header['location']) url = unquote(header['location']) else: url = pyfile.url name = html_unescape(unquote(urlparse(url).path.split("/")[-1])) if 'content-disposition' in header: self.logDebug("Content-Disposition: " + header['content-disposition']) m = search("filename(?P<type>=|\*=(?P<enc>.+)'')(?P<name>.*)", header['content-disposition']) if m: disp = m.groupdict() self.logDebug(disp) if not disp['enc']: disp['enc'] = 'utf-8' name = remove_chars(disp['name'], "\"';").strip() name = unicode(unquote(name), disp['enc']) if not name: name = url pyfile.name = name self.logDebug("Filename: %s" % pyfile.name) self.download(url, disposition=True)
def overridePlugins(self): pluginMap = {} for name in self.core.pluginManager.hosterPlugins.keys(): pluginMap[name.lower()] = name accountList = [ name.lower() for name, data in self.core.accountManager.accounts.items() if data ] excludedList = [] for hoster in self.getHosterCached(): name = remove_chars(hoster.lower(), "-.") if name in accountList: excludedList.append(hoster) else: if name in pluginMap: self.supported.append(pluginMap[name]) else: self.new_supported.append(hoster) if not self.supported and not self.new_supported: self.logError(_("No Hoster loaded")) return module = self.core.pluginManager.getPlugin(self.__name__) klass = getattr(module, self.__name__) # inject plugin plugin self.logDebug("Overwritten Hosters: %s" % ", ".join(sorted(self.supported))) for hoster in self.supported: dict = self.core.pluginManager.hosterPlugins[hoster] dict["new_module"] = module dict["new_name"] = self.__name__ if excludedList: self.logInfo( "The following hosters were not overwritten - account exists: %s" % ", ".join(sorted(excludedList))) if self.new_supported: self.logDebug("New Hosters: %s" % ", ".join(sorted(self.new_supported))) # create new regexp regexp = r".*(%s).*" % "|".join( [x.replace(".", "\\.") for x in self.new_supported]) if hasattr(klass, "__pattern__") and isinstance( klass.__pattern__, basestring) and '://' in klass.__pattern__: regexp = r"%s|%s" % (klass.__pattern__, regexp) self.logDebug("Regexp: %s" % regexp) dict = self.core.pluginManager.hosterPlugins[self.__name__] dict["pattern"] = regexp dict["re"] = re.compile(regexp)
def overridePlugins(self): excludedList = [] if self.plugintype == "hoster": pluginMap = dict((name.lower(), name) for name in self.core.pluginManager.hosterPlugins.iterkeys()) accountList = [account.type.lower() for account in self.core.api.getAccounts(False) if account.valid and account.premium] else: pluginMap = {} accountList = [name[::-1].replace("Folder"[::-1], "", 1).lower()[::-1] for name in self.core.pluginManager.crypterPlugins.iterkeys()] for plugin in self.pluginsCached(): name = remove_chars(plugin, "-.") if name in accountList: excludedList.append(plugin) else: if name in pluginMap: self.supported.append(pluginMap[name]) else: self.new_supported.append(plugin) if not self.supported and not self.new_supported: self.logError(_("No %s loaded") % self.plugintype) return # inject plugin plugin self.logDebug("Overwritten %ss: %s" % (self.plugintype, ", ".join(sorted(self.supported)))) for plugin in self.supported: hdict = self.core.pluginManager.plugins[self.plugintype][plugin] hdict['new_module'] = self.pluginmodule hdict['new_name'] = self.pluginname if excludedList: self.logInfo(_("%ss not overwritten: %s") % (self.plugintype.capitalize(), ", ".join(sorted(excludedList)))) if self.new_supported: plugins = sorted(self.new_supported) self.logDebug("New %ss: %s" % (self.plugintype, ", ".join(plugins))) # create new regexp regexp = r'.*(?P<DOMAIN>%s).*' % "|".join(x.replace('.', '\.') for x in plugins) if hasattr(self.pluginclass, "__pattern__") and isinstance(self.pluginclass.__pattern__, basestring) and '://' in self.pluginclass.__pattern__: regexp = r'%s|%s' % (self.pluginclass.__pattern__, regexp) self.logDebug("Regexp: %s" % regexp) hdict = self.core.pluginManager.plugins[self.plugintype][self.pluginname] hdict['pattern'] = regexp hdict['re'] = re.compile(regexp)
def call_api(func, args=""): add_header(response) s = request.environ.get('beaker.session') # Accepts standard http auth auth = parse_auth(request.get_header('Authorization', '')) if 'session' in request.POST or 'session' in request.GET: # removes "' so it works on json strings s = s.get_by_id(remove_chars(request.params.get('session'), "'\"")) elif auth: user = PYLOAD.checkAuth(auth[0], auth[1], request.environ.get('REMOTE_ADDR', None)) # if auth is correct create a pseudo session if user: s = {'uid': user.uid} api = get_user_api(s) if not api: return HTTPError(403, dumps("Forbidden")) if not PYLOAD.isAuthorized(func, api.user): return HTTPError(401, dumps("Unauthorized")) if not hasattr(PYLOAD.EXTERNAL, func) or func.startswith("_"): print "Invalid API call", func return HTTPError(404, dumps("Not Found")) # TODO: possible encoding # TODO Better error codes on invalid input args = [loads(unquote(arg)) for arg in args.split("/")[1:]] kwargs = {} # accepts body as json dict if request.json: kwargs = request.json # convert arguments from json to obj separately for x, y in chain(request.GET.iteritems(), request.POST.iteritems()): if not x or not y or x == "session": continue kwargs[x] = loads(unquote(y)) try: result = getattr(api, func)(*args, **kwargs) # null is invalid json response if result is None: result = True return dumps(result) except ExceptionObject, e: return HTTPError(400, dumps(e))
def overridePlugins(self): pluginMap = {} for name in self.core.pluginManager.hosterPlugins.keys(): pluginMap[name.lower()] = name accountList = [name.lower() for name, data in self.core.accountManager.accounts.items() if data] excludedList = [] for hoster in self.getHosterCached(): name = remove_chars(hoster.lower(), "-.") if name in accountList: excludedList.append(hoster) else: if name in pluginMap: self.supported.append(pluginMap[name]) else: self.new_supported.append(hoster) if not self.supported and not self.new_supported: self.logError(_("No Hoster loaded")) return module = self.core.pluginManager.getPlugin(self.__name__) klass = getattr(module, self.__name__) # inject plugin plugin self.logDebug("Overwritten Hosters: %s" % ", ".join(sorted(self.supported))) for hoster in self.supported: dict = self.core.pluginManager.hosterPlugins[hoster] dict["new_module"] = module dict["new_name"] = self.__name__ if excludedList: self.logInfo("The following hosters were not overwritten - account exists: %s" % ", ".join(sorted(excludedList))) if self.new_supported: self.logDebug("New Hosters: %s" % ", ".join(sorted(self.new_supported))) # create new regexp regexp = r".*(%s).*" % "|".join([x.replace(".", "\\.") for x in self.new_supported]) if hasattr(klass, "__pattern__") and isinstance(klass.__pattern__, basestring) and '://' in klass.__pattern__: regexp = r"%s|%s" % (klass.__pattern__, regexp) self.logDebug("Regexp: %s" % regexp) dict = self.core.pluginManager.hosterPlugins[self.__name__] dict["pattern"] = regexp dict["re"] = re.compile(regexp)
def parseHeader(self): """parse data from received header""" for orgline in self.decodeResponse(self.header).splitlines(): line = orgline.strip().lower() if line.startswith("accept-ranges") and "bytes" in line: self.p.chunkSupport = True if "content-disposition" in line: m = search("filename(?P<type>=|\*=(?P<enc>.+)'')(?P<name>.*)", line) if m: name = remove_chars(m.groupdict()['name'], "\"';").strip() self.p._name = name self.log.debug("Content-Disposition: %s" % name) if not self.resume and line.startswith("content-length"): self.p.size = int(line.split(":")[1]) self.headerParsed = True
def downloadFile(self, pyfile): url = pyfile.url for i in range(5): header = self.load(url, just_header=True) # self.load does not raise a BadHeader on 404 responses, do it here if 'code' in header and header['code'] == 404: raise BadHeader(404) if 'location' in header: self.logDebug("Location: " + header['location']) base = search(r'https?://[^/]+', url).group(0) if header['location'].startswith("http"): url = unquote(header['location']) elif header['location'].startswith("/"): url = base + unquote(header['location']) else: url = '%s/%s' % (base, unquote(header['location'])) else: break name = html_unescape(unquote(urlparse(url).path.split("/")[-1])) if 'content-disposition' in header: self.logDebug("Content-Disposition: " + header['content-disposition']) m = search("filename(?P<type>=|\*=(?P<enc>.+)'')(?P<name>.*)", header['content-disposition']) if m: disp = m.groupdict() self.logDebug(disp) if not disp['enc']: disp['enc'] = 'utf-8' name = remove_chars(disp['name'], "\"';").strip() name = unicode(unquote(name), disp['enc']) if not name: name = url pyfile.name = name self.logDebug("Filename: %s" % pyfile.name) self.download(url, disposition=True)
def parseHeader(self): """parse data from received header""" for orgline in self.decodeResponse(self.header).splitlines(): line = orgline.strip().lower() if line.startswith("accept-ranges") and "bytes" in line: self.p.chunkSupport = True if "content-disposition" in line: m = search("filename(?P<type>=|\*=(?P<enc>.+)'')(?P<name>.*)", line) if m: name = remove_chars(m.groupdict()['name'], "\"';/").strip() self.p._name = name self.log.debug("Content-Disposition: %s" % name) if not self.resume and line.startswith("content-length"): self.p.size = int(line.split(":")[1]) self.headerParsed = True
def coreReady(self): page = getURL("http://linkdecrypter.com/") m = re.search(r"<b>Supported</b>: <i>([^+<]*)", page) if not m: self.logError(_("Crypter list not found")) return online = m.group(1).split(", ") builtin = [name.lower() for name in self.core.pluginManager.crypterPlugins.keys()] builtin.extend(["downloadserienjunkiesorg"]) online = [crypter.replace(".", "\\.") for crypter in online if remove_chars(crypter, "-.") not in builtin] if not online: self.logError(_("Crypter list is empty")) return regexp = r"https?://([^.]+\.)*?(%s)/.*" % "|".join(online) dict = self.core.pluginManager.crypterPlugins[self.__name__] dict["pattern"] = regexp dict["re"] = re.compile(regexp) self.logDebug("REGEXP: " + regexp)
def normalize(domain): """ Normalize domain/plugin name, so they are comparable """ return remove_chars(domain.strip().lower(), "-.")
def overridePlugins(self): excludedList = [] if self.plugintype == "hoster": pluginMap = dict( (name.lower(), name) for name in self.core.pluginManager.hosterPlugins.iterkeys()) accountList = [ account.type.lower() for account in self.core.api.getAccounts(False) if account.valid and account.premium ] else: pluginMap = {} accountList = [ name[::-1].replace("Folder"[::-1], "", 1).lower()[::-1] for name in self.core.pluginManager.crypterPlugins.iterkeys() ] for plugin in self.pluginsCached(): name = remove_chars(plugin, "-.") if name in accountList: excludedList.append(plugin) else: if name in pluginMap: self.supported.append(pluginMap[name]) else: self.new_supported.append(plugin) if not self.supported and not self.new_supported: self.logError(_("No %s loaded") % self.plugintype) return # inject plugin plugin self.logDebug("Overwritten %ss: %s" % (self.plugintype, ", ".join(sorted(self.supported)))) for plugin in self.supported: hdict = self.core.pluginManager.plugins[self.plugintype][plugin] hdict['new_module'] = self.pluginmodule hdict['new_name'] = self.pluginname if excludedList: self.logInfo( _("%ss not overwritten: %s") % (self.plugintype.capitalize(), ", ".join( sorted(excludedList)))) if self.new_supported: plugins = sorted(self.new_supported) self.logDebug("New %ss: %s" % (self.plugintype, ", ".join(plugins))) # create new regexp regexp = r'.*(?P<DOMAIN>%s).*' % "|".join( x.replace('.', '\.') for x in plugins) if hasattr(self.pluginclass, "__pattern__") and isinstance( self.pluginclass.__pattern__, basestring) and "://" in self.pluginclass.__pattern__: regexp = r'%s|%s' % (self.pluginclass.__pattern__, regexp) self.logDebug("Regexp: %s" % regexp) hdict = self.core.pluginManager.plugins[self.plugintype][ self.pluginname] hdict['pattern'] = regexp hdict['re'] = re.compile(regexp)