| 1 |
import sys |
| 2 |
import logging |
| 3 |
import abc |
| 4 |
|
| 5 |
from ..exceptions import CloudflareSolveError |
| 6 |
|
| 7 |
if sys.version_info >= (3, 4): |
| 8 |
ABC = abc.ABC # noqa |
| 9 |
else: |
| 10 |
ABC = abc.ABCMeta('ABC', (), {}) |
| 11 |
|
| 12 |
# ------------------------------------------------------------------------------- # |
| 13 |
|
| 14 |
interpreters = {} |
| 15 |
|
| 16 |
# ------------------------------------------------------------------------------- # |
| 17 |
|
| 18 |
|
| 19 |
class JavaScriptInterpreter(ABC): |
| 20 |
|
| 21 |
# ------------------------------------------------------------------------------- # |
| 22 |
|
| 23 |
@abc.abstractmethod |
| 24 |
def __init__(self, name): |
| 25 |
interpreters[name] = self |
| 26 |
|
| 27 |
# ------------------------------------------------------------------------------- # |
| 28 |
|
| 29 |
@classmethod |
| 30 |
def dynamicImport(cls, name): |
| 31 |
if name not in interpreters: |
| 32 |
try: |
| 33 |
__import__('{}.{}'.format(cls.__module__, name)) |
| 34 |
if not isinstance(interpreters.get(name), JavaScriptInterpreter): |
| 35 |
raise ImportError('The interpreter was not initialized.') |
| 36 |
except ImportError: |
| 37 |
logging.error('Unable to load {} interpreter'.format(name)) |
| 38 |
raise |
| 39 |
|
| 40 |
return interpreters[name] |
| 41 |
|
| 42 |
# ------------------------------------------------------------------------------- # |
| 43 |
|
| 44 |
@abc.abstractmethod |
| 45 |
def eval(self, jsEnv, js): |
| 46 |
pass |
| 47 |
|
| 48 |
# ------------------------------------------------------------------------------- # |
| 49 |
|
| 50 |
def solveChallenge(self, body, domain): |
| 51 |
try: |
| 52 |
return '{0:.10f}'.format(float(self.eval(body, domain))) |
| 53 |
except Exception: |
| 54 |
raise CloudflareSolveError( |
| 55 |
'Error trying to solve Cloudflare IUAM Javascript, they may have changed their technique.' |
| 56 |
) |