80 lines
2.2 KiB
Python
80 lines
2.2 KiB
Python
|
#!/usr/local/bin/python3.sh
|
||
|
# -*-mode: python; indent-tabs-mode: nil; py-indent-offset: 4; coding: utf-8 -*
|
||
|
|
||
|
import sys
|
||
|
import os
|
||
|
import traceback
|
||
|
|
||
|
from phantompy import Render
|
||
|
|
||
|
global LOG
|
||
|
import logging
|
||
|
import warnings
|
||
|
warnings.filterwarnings('ignore')
|
||
|
LOG = logging.getLogger()
|
||
|
|
||
|
class LookFor(Render):
|
||
|
|
||
|
def __init__(self, url, outfile, jsfile=None):
|
||
|
self.uri = url
|
||
|
Render.__init__(self, url, outfile, jsfile)
|
||
|
|
||
|
def ilookfor(self, html):
|
||
|
import json
|
||
|
marker = '<pre style="word-wrap: break-word; white-space: pre-wrap;">'
|
||
|
if marker not in html: return ''
|
||
|
i = html.find(marker) + len(marker)
|
||
|
html = html[i:]
|
||
|
assert html[0] == '{', html
|
||
|
i = html.find('</pre')
|
||
|
html = html[:i]
|
||
|
assert html[-1] == '}', html
|
||
|
LOG.debug(f"Found {len(html)} json")
|
||
|
o = json.loads(html)
|
||
|
if "Answer" not in o.keys() or type(o["Answer"]) != list:
|
||
|
LOG.warn(f"FAIL {self.uri}")
|
||
|
return 1
|
||
|
for elt in o["Answer"]:
|
||
|
assert type(elt) == dict, elt
|
||
|
assert 'type' in elt, elt
|
||
|
if elt['type'] != 16: continue
|
||
|
assert 'data' in elt, elt
|
||
|
if elt['data'] == 'we-run-this-tor-relay':
|
||
|
LOG.info(f"OK {self.uri}")
|
||
|
return 0
|
||
|
LOG.warn(f"BAD {self.uri}")
|
||
|
return 2
|
||
|
|
||
|
def _html_callback(self, *args):
|
||
|
"""print(self, QPrinter, Callable[[bool], None])"""
|
||
|
if type(args[0]) is str:
|
||
|
self._save(args[0])
|
||
|
i = self.ilookfor(args[0])
|
||
|
self._exit(i)
|
||
|
|
||
|
def _save(self, html):
|
||
|
sfile = self.outfile.replace('.pdf','.out')
|
||
|
# CompleteHtmlSaveFormat SingleHtmlSaveFormat MimeHtmlSaveFormat
|
||
|
with open(sfile, 'wt') as ofd:
|
||
|
ofd.write(html)
|
||
|
LOG.debug(f"Saved {sfile}")
|
||
|
|
||
|
def _loadFinished(self, result):
|
||
|
LOG.debug("phantom.py: Loading finished!")
|
||
|
self.toHtml(self._html_callback)
|
||
|
|
||
|
def main():
|
||
|
if (len(sys.argv) < 3):
|
||
|
LOG.info("USAGE: lookupdns.py <url> <pdf-file> [<javascript-file>]")
|
||
|
else:
|
||
|
url = sys.argv[1]
|
||
|
outfile = sys.argv[2]
|
||
|
jsfile = sys.argv[3] if len(sys.argv) > 3 else None
|
||
|
r = LookFor(url, outfile, jsfile)
|
||
|
|
||
|
sys.exit(0)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|
||
|
|