2013-10-02 18:59:40 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from __future__ import with_statement
|
2019-06-24 16:49:38 +00:00
|
|
|
from __future__ import print_function
|
2013-10-02 18:59:40 +00:00
|
|
|
|
|
|
|
__license__ = 'GPL v3'
|
|
|
|
|
|
|
|
# Standard Python modules.
|
2020-01-20 12:52:54 +00:00
|
|
|
import os, sys, re, hashlib, traceback
|
2013-10-02 18:59:40 +00:00
|
|
|
from calibre_plugins.dedrm.__init__ import PLUGIN_NAME, PLUGIN_VERSION
|
|
|
|
|
|
|
|
def WineGetKeys(scriptpath, extension, wineprefix=""):
|
|
|
|
import subprocess
|
|
|
|
from subprocess import Popen, PIPE, STDOUT
|
|
|
|
|
|
|
|
import subasyncio
|
|
|
|
from subasyncio import Process
|
|
|
|
|
|
|
|
if extension == u".k4i":
|
|
|
|
import json
|
|
|
|
|
|
|
|
basepath, script = os.path.split(scriptpath)
|
2019-06-24 16:49:38 +00:00
|
|
|
print(u"{0} v{1}: Running {2} under Wine".format(PLUGIN_NAME, PLUGIN_VERSION, script))
|
2013-10-02 18:59:40 +00:00
|
|
|
|
|
|
|
outdirpath = os.path.join(basepath, u"winekeysdir")
|
|
|
|
if not os.path.exists(outdirpath):
|
|
|
|
os.makedirs(outdirpath)
|
|
|
|
|
2019-06-12 19:13:25 +00:00
|
|
|
if wineprefix != "":
|
|
|
|
wineprefix = os.path.abspath(os.path.expanduser(os.path.expandvars(wineprefix)))
|
|
|
|
|
2013-10-02 18:59:40 +00:00
|
|
|
if wineprefix != "" and os.path.exists(wineprefix):
|
|
|
|
cmdline = u"WINEPREFIX=\"{2}\" wine python.exe \"{0}\" \"{1}\"".format(scriptpath,outdirpath,wineprefix)
|
|
|
|
else:
|
|
|
|
cmdline = u"wine python.exe \"{0}\" \"{1}\"".format(scriptpath,outdirpath)
|
2019-06-24 16:49:38 +00:00
|
|
|
print(u"{0} v{1}: Command line: '{2}'".format(PLUGIN_NAME, PLUGIN_VERSION, cmdline))
|
2013-10-02 18:59:40 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
|
|
|
p2 = Process(cmdline, shell=True, bufsize=1, stdin=None, stdout=sys.stdout, stderr=STDOUT, close_fds=False)
|
|
|
|
result = p2.wait("wait")
|
|
|
|
except Exception, e:
|
2019-06-24 16:49:38 +00:00
|
|
|
print(u"{0} v{1}: Wine subprocess call error: {2}".format(PLUGIN_NAME, PLUGIN_VERSION, e.args[0]))
|
2016-01-11 06:44:44 +00:00
|
|
|
if wineprefix != "" and os.path.exists(wineprefix):
|
|
|
|
cmdline = u"WINEPREFIX=\"{2}\" wine C:\\Python27\\python.exe \"{0}\" \"{1}\"".format(scriptpath,outdirpath,wineprefix)
|
2016-01-14 17:15:43 +00:00
|
|
|
else:
|
2016-01-11 06:44:44 +00:00
|
|
|
cmdline = u"wine C:\\Python27\\python.exe \"{0}\" \"{1}\"".format(scriptpath,outdirpath)
|
2019-06-24 16:49:38 +00:00
|
|
|
print(u"{0} v{1}: Command line: “{2}”".format(PLUGIN_NAME, PLUGIN_VERSION, cmdline))
|
2013-10-02 18:59:40 +00:00
|
|
|
|
2016-01-14 17:15:43 +00:00
|
|
|
try:
|
2016-01-11 06:44:44 +00:00
|
|
|
cmdline = cmdline.encode(sys.getfilesystemencoding())
|
|
|
|
p2 = Process(cmdline, shell=True, bufsize=1, stdin=None, stdout=sys.stdout, stderr=STDOUT, close_fds=False)
|
|
|
|
result = p2.wait("wait")
|
2016-01-14 17:15:43 +00:00
|
|
|
except Exception, e:
|
2019-06-24 16:49:38 +00:00
|
|
|
print(u"{0} v{1}: Wine subprocess call error: {2}".format(PLUGIN_NAME, PLUGIN_VERSION, e.args[0]))
|
2016-01-11 06:44:44 +00:00
|
|
|
|
|
|
|
# try finding winekeys anyway, even if above code errored
|
2013-10-02 18:59:40 +00:00
|
|
|
winekeys = []
|
|
|
|
# get any files with extension in the output dir
|
|
|
|
files = [f for f in os.listdir(outdirpath) if f.endswith(extension)]
|
|
|
|
for filename in files:
|
|
|
|
try:
|
|
|
|
fpath = os.path.join(outdirpath, filename)
|
|
|
|
with open(fpath, 'rb') as keyfile:
|
|
|
|
if extension == u".k4i":
|
|
|
|
new_key_value = json.loads(keyfile.read())
|
|
|
|
else:
|
|
|
|
new_key_value = keyfile.read()
|
|
|
|
winekeys.append(new_key_value)
|
|
|
|
except:
|
2019-06-24 16:49:38 +00:00
|
|
|
print(u"{0} v{1}: Error loading file {2}".format(PLUGIN_NAME, PLUGIN_VERSION, filename))
|
2013-10-02 18:59:40 +00:00
|
|
|
traceback.print_exc()
|
|
|
|
os.remove(fpath)
|
2019-06-24 16:49:38 +00:00
|
|
|
print(u"{0} v{1}: Found and decrypted {2} {3}".format(PLUGIN_NAME, PLUGIN_VERSION, len(winekeys), u"key file" if len(winekeys) == 1 else u"key files"))
|
2013-10-02 18:59:40 +00:00
|
|
|
return winekeys
|