From 4ac7faaf3b172ca3243e8f095e263cd274e2cc77 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sat, 26 Jan 2013 21:50:44 +0100 Subject: mega.co.nz hoster plugin --- module/plugins/hoster/MegaNz.py | 124 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 module/plugins/hoster/MegaNz.py (limited to 'module/plugins/hoster/MegaNz.py') diff --git a/module/plugins/hoster/MegaNz.py b/module/plugins/hoster/MegaNz.py new file mode 100644 index 000000000..5041fb868 --- /dev/null +++ b/module/plugins/hoster/MegaNz.py @@ -0,0 +1,124 @@ +# -*- coding: utf-8 -*- + +import re +import random +from array import array +from os import remove +from base64 import standard_b64decode + +from Crypto.Cipher import AES +from Crypto.Util import Counter + +from module.common.json_layer import json +from module.plugins.Hoster import Hoster + +#def getInfo(urls): +# pass + +class MegaNz(Hoster): + __name__ = "MegaNz" + __type__ = "hoster" + __pattern__ = r"https?://([a-z0-9]+\.)?mega\.co\.nz/#!([a-zA-Z0-9!_\-]+)" + __version__ = "0.1" + __description__ = """mega.co.nz hoster plugin""" + __author_name__ = ("RaNaN", ) + __author_mail__ = ("ranan@pyload.org", ) + + API_URL = "https://g.api.mega.co.nz/cs?id=%d" + FILE_PREFIX = ".crypted" + + def b64_decode(self, data): + return standard_b64decode(data.replace("-", "+").replace("_", "/")+ "=") + + def getCipherKey(self, key): + """ Construct the cipher key from the given data """ + a = array("I", key) + key_array = array("I", [a[0] ^ a[4], a[1] ^a[5], a[2] ^ a[6], a[3] ^a[7]]) + return key_array + + def callApi(self, **kwargs): + """ Dispatch a call to the api, see https://mega.co.nz/#developers """ + # generate a session id, no idea where to obtain elsewhere + uid = random.randint(10 << 9, 10 ** 10) + + resp = self.load(self.API_URL % uid, post=json.dumps([kwargs])) + self.logDebug("Api Response: " + resp) + return json.loads(resp) + + def decryptAttr(self, data, key): + + cbc = AES.new(self.getCipherKey(key), AES.MODE_CBC, "\0" * 16) + attr = cbc.decrypt(self.b64_decode(data)) + self.logDebug("Decrypted Attr: " + attr) + if not attr.startswith("MEGA"): + self.fail(_("Decryption failed")) + + # Data is padded, 0-bytes must be stripped + return json.loads(attr.replace("MEGA", "").strip("\0").strip()) + + def decryptFile(self, key): + """ Decrypts the file at lastDownload` """ + + # upper 64 bit of counter start + n = key[16:24] + + # convert counter to long and shift bytes + ctr = Counter.new(128, initial_value=long(n.encode("hex"),16) << 64) + cipher = AES.new(self.getCipherKey(key), AES.MODE_CTR, counter=ctr) + + self.pyfile.setStatus("decrypting") + f = open(self.lastDownload, "rb") + df = open(self.lastDownload.rstrip(self.FILE_PREFIX), "wb") + + # TODO: calculate CBC-MAC for checksum + + size = 2 ** 15 # buffer size, 32k + while True: + buf = f.read(size) + if not buf: break + + df.write(cipher.decrypt(buf)) + + f.close() + df.close() + remove(self.lastDownload) + + def process(self, pyfile): + + key = None + + # match is guaranteed because plugin was chosen to handle url + node = re.search(self.__pattern__, pyfile.url).group(2) + if "!" in node: + node, key = node.split("!") + + self.logDebug("File id: %s | Key: %s" % (node, key)) + + if not key: + self.fail(_("No file key provided in the URL")) + + # g is for requesting a download url + # this is like the calls in the mega js app, documentation is very bad + dl = self.callApi(a="g", g=1, p=node, ssl=1)[0] + + if "e" in dl: + e = dl["e"] + # ETEMPUNAVAIL (-18): Resource temporarily not available, please try again later + if e == -18: + self.retry() + else: + self.fail(_("Error code:") + e) + + # TODO: map other error codes, e.g + # EACCESS (-11): Access violation (e.g., trying to write to a read-only share) + + key = self.b64_decode(key) + attr = self.decryptAttr(dl["at"], key) + + pyfile.name = attr["n"] + self.FILE_PREFIX + + self.download(dl["g"]) + self.decryptFile(key) + + # Everything is finished and final name can be set + pyfile.name = attr["n"] -- cgit v1.2.3 From 727ffe0aaa733a23e3db1aa7ef6a7e2068f565c5 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sat, 9 Feb 2013 12:19:31 +0100 Subject: closed #764 --- module/plugins/hoster/MegaNz.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'module/plugins/hoster/MegaNz.py') diff --git a/module/plugins/hoster/MegaNz.py b/module/plugins/hoster/MegaNz.py index 5041fb868..1c48906ca 100644 --- a/module/plugins/hoster/MegaNz.py +++ b/module/plugins/hoster/MegaNz.py @@ -25,7 +25,7 @@ class MegaNz(Hoster): __author_mail__ = ("ranan@pyload.org", ) API_URL = "https://g.api.mega.co.nz/cs?id=%d" - FILE_PREFIX = ".crypted" + FILE_SUFFIX = ".crypted" def b64_decode(self, data): return standard_b64decode(data.replace("-", "+").replace("_", "/")+ "=") @@ -54,7 +54,7 @@ class MegaNz(Hoster): self.fail(_("Decryption failed")) # Data is padded, 0-bytes must be stripped - return json.loads(attr.replace("MEGA", "").strip("\0").strip()) + return json.loads(attr.replace("MEGA", "").rstrip("\0").strip()) def decryptFile(self, key): """ Decrypts the file at lastDownload` """ @@ -68,7 +68,7 @@ class MegaNz(Hoster): self.pyfile.setStatus("decrypting") f = open(self.lastDownload, "rb") - df = open(self.lastDownload.rstrip(self.FILE_PREFIX), "wb") + df = open(self.lastDownload.rstrip(self.FILE_SUFFIX), "wb") # TODO: calculate CBC-MAC for checksum @@ -98,7 +98,7 @@ class MegaNz(Hoster): self.fail(_("No file key provided in the URL")) # g is for requesting a download url - # this is like the calls in the mega js app, documentation is very bad + # this is similar to the calls in the mega js app, documentation is very bad dl = self.callApi(a="g", g=1, p=node, ssl=1)[0] if "e" in dl: @@ -115,7 +115,7 @@ class MegaNz(Hoster): key = self.b64_decode(key) attr = self.decryptAttr(dl["at"], key) - pyfile.name = attr["n"] + self.FILE_PREFIX + pyfile.name = attr["n"] + self.FILE_SUFFIX self.download(dl["g"]) self.decryptFile(key) -- cgit v1.2.3 From 5221eda1ad0fbfa26b01cf66354a5415141adf4a Mon Sep 17 00:00:00 2001 From: Gonzalo SR Date: Thu, 14 Feb 2013 13:02:11 +0100 Subject: Support for incorrectly padded Mega.co.nz keys Sometimes base64 Mega keys are incorrectly padded. Ugly hack to add padding characters as needed. --- module/plugins/hoster/MegaNz.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'module/plugins/hoster/MegaNz.py') diff --git a/module/plugins/hoster/MegaNz.py b/module/plugins/hoster/MegaNz.py index 1c48906ca..6644d372c 100644 --- a/module/plugins/hoster/MegaNz.py +++ b/module/plugins/hoster/MegaNz.py @@ -28,7 +28,8 @@ class MegaNz(Hoster): FILE_SUFFIX = ".crypted" def b64_decode(self, data): - return standard_b64decode(data.replace("-", "+").replace("_", "/")+ "=") + data = data.replace("-", "+").replace("_", "/") + return standard_b64decode(data + '=' * (-len(data) % 4)) def getCipherKey(self, key): """ Construct the cipher key from the given data """ -- cgit v1.2.3 From 9ac39154d4709439e506e523b5fe543bae29edbc Mon Sep 17 00:00:00 2001 From: RaNaN Date: Thu, 14 Feb 2013 17:54:13 +0100 Subject: bump version number for MegaNz --- module/plugins/hoster/MegaNz.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'module/plugins/hoster/MegaNz.py') diff --git a/module/plugins/hoster/MegaNz.py b/module/plugins/hoster/MegaNz.py index 6644d372c..a28ddca9d 100644 --- a/module/plugins/hoster/MegaNz.py +++ b/module/plugins/hoster/MegaNz.py @@ -19,7 +19,7 @@ class MegaNz(Hoster): __name__ = "MegaNz" __type__ = "hoster" __pattern__ = r"https?://([a-z0-9]+\.)?mega\.co\.nz/#!([a-zA-Z0-9!_\-]+)" - __version__ = "0.1" + __version__ = "0.11" __description__ = """mega.co.nz hoster plugin""" __author_name__ = ("RaNaN", ) __author_mail__ = ("ranan@pyload.org", ) -- cgit v1.2.3