2014-01-21 06:41:46 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from json import dumps, loads
|
2014-01-21 22:53:28 +00:00
|
|
|
import os,sys,argparse, urllib.request, urllib.error, urllib.parse
|
2014-01-21 06:41:46 +00:00
|
|
|
import websocket
|
|
|
|
import time
|
2014-01-21 21:32:33 +00:00
|
|
|
import uuid
|
|
|
|
import logging
|
|
|
|
import threading
|
2014-01-22 07:38:03 +00:00
|
|
|
from kombu import Connection, Exchange, Queue
|
2014-01-21 06:41:46 +00:00
|
|
|
|
2014-01-21 21:32:33 +00:00
|
|
|
class Umbra:
|
2014-01-27 19:15:31 -08:00
|
|
|
logger = logging.getLogger('umbra.Umbra')
|
|
|
|
def __init__(self, websocket_url, amqp_url):
|
2014-01-23 18:39:17 +00:00
|
|
|
self.cmd_id = 0
|
|
|
|
self.producer = None
|
2014-01-27 19:24:28 -08:00
|
|
|
self.browser_lock = threading.Lock()
|
2014-01-27 19:15:31 -08:00
|
|
|
self.amqp_url = amqp_url
|
2014-01-23 18:39:17 +00:00
|
|
|
self.producer_lock = threading.Lock()
|
2014-01-27 19:15:31 -08:00
|
|
|
self.websocket_url = websocket_url
|
|
|
|
self.websock = websocket.WebSocketApp(websocket_url, on_message = self.handle_message)
|
|
|
|
self.amqp_thread = threading.Thread(target=self.consume_amqp)
|
|
|
|
self.amqp_stop = threading.Event()
|
|
|
|
self.websock.on_open = lambda ws: self.amqp_thread.start()
|
|
|
|
threading.Thread(target=self.websock.run_forever).start()
|
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
self.logger.info("shutting down amqp consumer {}".format(self.amqp_url))
|
|
|
|
self.amqp_stop.set()
|
|
|
|
self.logger.info("shutting down websocket {}".format(self.websocket_url))
|
|
|
|
self.websock.close()
|
|
|
|
self.amqp_thread.join()
|
2014-01-22 07:38:03 +00:00
|
|
|
|
2014-01-23 18:39:17 +00:00
|
|
|
def handle_message(self, ws, message):
|
2014-01-27 19:15:31 -08:00
|
|
|
# self.logger.debug("handling message from websocket {} - {}".format(ws, message))
|
2014-01-23 18:39:17 +00:00
|
|
|
message = loads(message)
|
|
|
|
if "method" in message.keys() and message["method"] == "Network.requestWillBeSent":
|
2014-01-27 19:15:31 -08:00
|
|
|
request_queue = Queue('requests', routing_key='request',
|
|
|
|
exchange=self.umbra_exchange)
|
2014-01-23 18:39:17 +00:00
|
|
|
with self.producer_lock:
|
2014-01-27 19:15:31 -08:00
|
|
|
self.producer.publish(message['params']['request'],
|
|
|
|
routing_key='request', exchange=self.umbra_exchange,
|
|
|
|
declare=[request_queue])
|
2014-01-23 18:39:17 +00:00
|
|
|
|
2014-01-27 19:15:31 -08:00
|
|
|
def consume_amqp(self):
|
2014-01-23 18:39:17 +00:00
|
|
|
self.umbra_exchange = Exchange('umbra', 'direct', durable=True)
|
2014-01-27 19:15:31 -08:00
|
|
|
url_queue = Queue('urls', routing_key='url', exchange=self.umbra_exchange)
|
|
|
|
self.logger.info("connecting to amqp {} at {}".format(repr(self.umbra_exchange), self.amqp_url))
|
|
|
|
with Connection(self.amqp_url) as conn:
|
2014-01-23 18:39:17 +00:00
|
|
|
self.producer = conn.Producer(serializer='json')
|
|
|
|
with conn.Consumer(url_queue, callbacks=[self.fetch_url]) as consumer:
|
2014-01-27 19:15:31 -08:00
|
|
|
import socket
|
|
|
|
while not self.amqp_stop.is_set():
|
|
|
|
try:
|
|
|
|
conn.drain_events(timeout=0.5)
|
|
|
|
except socket.timeout:
|
|
|
|
pass
|
2014-01-23 18:39:17 +00:00
|
|
|
|
2014-01-27 19:15:31 -08:00
|
|
|
def send_command(self, **kwargs):
|
|
|
|
self.logger.debug("sending command kwargs={}".format(kwargs))
|
2014-01-23 18:39:17 +00:00
|
|
|
command = {}
|
|
|
|
command.update(kwargs)
|
|
|
|
self.cmd_id += 1
|
|
|
|
command['id'] = self.cmd_id
|
2014-01-27 19:15:31 -08:00
|
|
|
self.websock.send(dumps(command))
|
2014-01-23 18:39:17 +00:00
|
|
|
|
|
|
|
def fetch_url(self, body, message):
|
2014-01-27 19:15:31 -08:00
|
|
|
self.logger.debug("body={} message={} message.headers={} message.payload={}".format(body, message, message.headers, message.payload))
|
|
|
|
|
2014-01-23 18:39:17 +00:00
|
|
|
url = body['url']
|
2014-01-27 19:15:31 -08:00
|
|
|
|
2014-01-27 19:24:28 -08:00
|
|
|
with self.browser_lock:
|
|
|
|
self.send_command(method="Network.enable")
|
|
|
|
self.send_command(method="Runtime.evaluate", params={"expression":"document.location = '%s';" % url})
|
2014-01-27 19:15:31 -08:00
|
|
|
|
2014-01-27 19:24:28 -08:00
|
|
|
# XXX more logic goes here
|
|
|
|
time.sleep(10)
|
2014-01-27 19:15:31 -08:00
|
|
|
|
2014-01-27 19:24:28 -08:00
|
|
|
message.ack()
|
2014-01-21 06:41:46 +00:00
|
|
|
|
|
|
|
class Chrome():
|
2014-01-27 19:15:31 -08:00
|
|
|
logger = logging.getLogger('umbra.Chrome')
|
|
|
|
|
2014-01-23 18:39:17 +00:00
|
|
|
def __init__(self, port, executable, browser_wait):
|
|
|
|
self.port = port
|
|
|
|
self.executable = executable
|
2014-01-27 19:15:31 -08:00
|
|
|
self.browser_wait = browser_wait
|
|
|
|
|
|
|
|
def fetch_debugging_json():
|
|
|
|
raw_json = urllib.request.urlopen("http://localhost:%s/json" % self.port).read()
|
|
|
|
json = raw_json.decode('utf-8')
|
|
|
|
return loads(json)
|
2014-01-23 18:39:17 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
2014-01-23 16:18:13 -05:00
|
|
|
import subprocess
|
2014-01-27 19:15:31 -08:00
|
|
|
chrome_args = [self.executable, "--temp-profile",
|
|
|
|
"--remote-debugging-port=%s" % self.port,
|
|
|
|
"--disable-web-sockets", "--disable-cache",
|
|
|
|
"--window-size=1100,900", "--enable-logging"
|
|
|
|
"--homepage=about:blank", "about:blank"]
|
|
|
|
self.logger.info("running {}".format(chrome_args))
|
|
|
|
self.chrome_process = subprocess.Popen(chrome_args)
|
|
|
|
self.logger.info("chrome running, pid {}".format(self.chrome_process.pid))
|
2014-01-23 18:39:17 +00:00
|
|
|
start = time.time()
|
2014-01-21 06:41:46 +00:00
|
|
|
|
2014-01-27 19:15:31 -08:00
|
|
|
json_url = "http://localhost:%s/json" % self.port
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
raw_json = urllib.request.urlopen(json_url).read()
|
|
|
|
all_debug_info = loads(raw_json.decode('utf-8'))
|
|
|
|
debug_info = [x for x in all_debug_info if x['url'] == 'about:blank']
|
|
|
|
|
|
|
|
if debug_info and 'webSocketDebuggerUrl' in debug_info[0]:
|
|
|
|
self.logger.debug("{} returned {}".format(json_url, raw_json))
|
|
|
|
url = debug_info[0]['webSocketDebuggerUrl']
|
|
|
|
self.logger.info('got chrome window websocket debug url {} from {}'.format(url, json_url))
|
|
|
|
return url
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
if time.time() - start > float(self.browser_wait):
|
|
|
|
raise Exception("failed to retrieve {} after {} seconds".format(json_url, time.time() - start))
|
|
|
|
else:
|
|
|
|
time.sleep(0.5)
|
2014-01-21 06:41:46 +00:00
|
|
|
|
2014-01-23 18:39:17 +00:00
|
|
|
def __exit__(self, *args):
|
2014-01-27 19:15:31 -08:00
|
|
|
self.logger.info("killing chrome pid {}".format(self.chrome_process.pid))
|
2014-01-23 18:39:17 +00:00
|
|
|
self.chrome_process.kill()
|
2014-01-21 06:41:46 +00:00
|
|
|
|
2014-01-22 02:30:12 +00:00
|
|
|
def main():
|
2014-01-27 19:15:31 -08:00
|
|
|
# logging.basicConfig(stream=sys.stdout, level=logging.INFO,
|
|
|
|
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG,
|
|
|
|
format='%(asctime)s %(process)d %(levelname)s %(threadName)s %(name)s.%(funcName)s(%(filename)s:%(lineno)d) %(message)s')
|
|
|
|
|
2014-01-23 18:39:17 +00:00
|
|
|
arg_parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),
|
|
|
|
description='umbra - Browser automation tool',
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
|
|
|
arg_parser.add_argument('-w', '--browser-wait', dest='browser_wait', default='10',
|
|
|
|
help='Seconds to wait for browser initialization')
|
|
|
|
arg_parser.add_argument('-e', '--executable', dest='executable', default='chromium-browser',
|
|
|
|
help='Executable to use to invoke chrome')
|
|
|
|
arg_parser.add_argument('-p', '--port', dest='port', default='9222',
|
|
|
|
help='Port to have invoked chrome listen on for debugging connections')
|
2014-01-27 19:15:31 -08:00
|
|
|
arg_parser.add_argument('-u', '--url', dest='amqp_url', default='amqp://guest:guest@localhost:5672//',
|
2014-01-23 18:39:17 +00:00
|
|
|
help='URL identifying the amqp server to talk to')
|
|
|
|
args = arg_parser.parse_args(args=sys.argv[1:])
|
2014-01-27 19:15:31 -08:00
|
|
|
with Chrome(args.port, args.executable, args.browser_wait) as websocket_url:
|
|
|
|
umbra = Umbra(websocket_url, args.amqp_url)
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
time.sleep(0.5)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
umbra.shutdown()
|
|
|
|
|
2014-01-22 02:30:12 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-01-23 18:39:17 +00:00
|
|
|
main()
|