diff options
Diffstat (limited to 'module/plugins/hoster/MegaCoNz.py')
-rw-r--r-- | module/plugins/hoster/MegaCoNz.py | 75 |
1 files changed, 40 insertions, 35 deletions
diff --git a/module/plugins/hoster/MegaCoNz.py b/module/plugins/hoster/MegaCoNz.py index aa7755af4..d8a29817c 100644 --- a/module/plugins/hoster/MegaCoNz.py +++ b/module/plugins/hoster/MegaCoNz.py @@ -12,7 +12,7 @@ from Crypto.Cipher import AES from Crypto.Util import Counter from module.common.json_layer import json_loads, json_dumps -from module.plugins.Hoster import Hoster +from module.plugins.internal.Hoster import Hoster from module.utils import decode, fs_decode, fs_encode @@ -48,9 +48,10 @@ from module.utils import decode, fs_decode, fs_encode class MegaCoNz(Hoster): __name__ = "MegaCoNz" __type__ = "hoster" - __version__ = "0.26" + __version__ = "0.31" + __status__ = "testing" - __pattern__ = r'(?:https?://(?:www\.)?mega\.co\.nz/|mega:|chrome:.+?)#(?P<TYPE>N|)!(?P<ID>[\w^_]+)!(?P<KEY>[\w,-]+)' + __pattern__ = r'(https?://(?:www\.)?mega(\.co)?\.nz/|mega:|chrome:.+?)#(?P<TYPE>N|)!(?P<ID>[\w^_]+)!(?P<KEY>[\w,-]+)' __description__ = """Mega.co.nz hoster plugin""" __license__ = "GPLv3" @@ -67,8 +68,10 @@ class MegaCoNz(Hoster): return standard_b64decode(data + '=' * (-len(data) % 4)) - def getCipherKey(self, key): - """ Construct the cipher key from the given data """ + def get_cipher_key(self, key): + """ + Construct the cipher key from the given data + """ a = array.array("I", self.b64_decode(key)) k = array.array("I", (a[0] ^ a[4], a[1] ^ a[5], a[2] ^ a[6], a[3] ^ a[7])) @@ -79,44 +82,46 @@ class MegaCoNz(Hoster): def api_response(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.random.randint(10 << 9, 10 ** 10) + """ + 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) res = self.load(self.API_URL, get={'id': uid}, post=json_dumps([kwargs])) - self.logDebug("Api Response: " + res) + self.log_debug("Api Response: " + res) return json_loads(res) - def decryptAttr(self, data, key): - k, iv, meta_mac = self.getCipherKey(key) + def decrypt_attr(self, data, key): + k, iv, meta_mac = self.get_cipher_key(key) cbc = AES.new(k, AES.MODE_CBC, "\0" * 16) attr = decode(cbc.decrypt(self.b64_decode(data))) - self.logDebug("Decrypted Attr: %s" % attr) + self.log_debug("Decrypted Attr: %s" % attr) if not attr.startswith("MEGA"): self.fail(_("Decryption failed")) - # Data is padded, 0-bytes must be stripped + #: Data is padded, 0-bytes must be stripped return json_loads(re.search(r'{.+?}', attr).group(0)) - def decryptFile(self, key): - """ Decrypts the file at lastDownload` """ - - # upper 64 bit of counter start + def decrypt_file(self, key): + """ + Decrypts the file at last_download` + """ + #: Upper 64 bit of counter start n = self.b64_decode(key)[16:24] - # convert counter to long and shift bytes - k, iv, meta_mac = self.getCipherKey(key) + #: Convert counter to long and shift bytes + k, iv, meta_mac = self.get_cipher_key(key) ctr = Counter.new(128, initial_value=long(n.encode("hex"), 16) << 64) cipher = AES.new(k, AES.MODE_CTR, counter=ctr) self.pyfile.setStatus("decrypting") self.pyfile.setProgress(0) - file_crypted = fs_encode(self.lastDownload) + file_crypted = fs_encode(self.last_download) file_decrypted = file_crypted.rsplit(self.FILE_SUFFIX)[0] try: @@ -126,7 +131,7 @@ class MegaCoNz(Hoster): except IOError, e: self.fail(e) - chunk_size = 2 ** 15 # buffer size, 32k + chunk_size = 2 ** 15 #: Buffer size, 32k # file_mac = [0, 0, 0, 0] # calculate CBC-MAC for checksum chunks = os.path.getsize(file_crypted) / chunk_size + 1 @@ -158,22 +163,22 @@ class MegaCoNz(Hoster): f.close() df.close() - # if file_mac[0] ^ file_mac[1], file_mac[2] ^ file_mac[3] != meta_mac: + # if file_mac[0] ^ file_mac[1], file_mac[2] ^ file_mac[3] is not meta_mac: # os.remove(file_decrypted) # self.fail(_("Checksum mismatch")) os.remove(file_crypted) - self.lastDownload = fs_decode(file_decrypted) + self.last_download = fs_decode(file_decrypted) - def checkError(self, code): + def check_error(self, code): ecode = abs(code) if ecode in (9, 16, 21): self.offline() elif ecode in (3, 13, 17, 18, 19): - self.tempOffline() + self.temp_offline() elif ecode in (1, 4, 6, 10, 15, 21): self.retry(5, 30, _("Error code: [%s]") % -ecode) @@ -186,23 +191,23 @@ class MegaCoNz(Hoster): pattern = re.match(self.__pattern__, pyfile.url).groupdict() id = pattern['ID'] key = pattern['KEY'] - public = pattern['TYPE'] == '' + public = pattern['TYPE'] == "" - self.logDebug("ID: %s" % id, "Key: %s" % key, "Type: %s" % ("public" if public else "node")) + self.log_debug("ID: %s" % id, "Key: %s" % key, "Type: %s" % ("public" if public else "node")) - # g is for requesting a download url - # this is similar to the calls in the mega js app, documentation is very bad + #: G is for requesting a download url + #: This is similar to the calls in the mega js app, documentation is very bad if public: mega = self.api_response(a="g", g=1, p=id, ssl=1)[0] else: mega = self.api_response(a="g", g=1, n=id, ssl=1)[0] if isinstance(mega, int): - self.checkError(mega) + self.check_error(mega) elif "e" in mega: - self.checkError(mega['e']) + self.check_error(mega['e']) - attr = self.decryptAttr(mega['at'], key) + attr = self.decrypt_attr(mega['at'], key) pyfile.name = attr['n'] + self.FILE_SUFFIX pyfile.size = mega['s'] @@ -211,7 +216,7 @@ class MegaCoNz(Hoster): self.download(mega['g']) - self.decryptFile(key) + self.decrypt_file(key) - # Everything is finished and final name can be set + #: Everything is finished and final name can be set pyfile.name = attr['n'] |