From b127b57f97d3c4c4f111e270afcbf4349e731c7b Mon Sep 17 00:00:00 2001 From: Vishal Gowda Date: Thu, 9 Mar 2017 13:32:39 +0530 Subject: [PATCH] Cycle passwords across sessions --- hedroid/master/droid_keeper.thrift | 1 + hedroid/master/tgen/droid_keeper/ttypes.py | 15 +++++++++++- hedroid/master/thrift_server.py | 1 + hedroid/worker/droid_service.thrift | 1 + hedroid/worker/helpers.py | 12 ++++++++-- hedroid/worker/tgen/droid_service/ttypes.py | 15 +++++++++++- hedroid/worker/thrift_server.py | 6 +++-- hedroid/worker/utils.py | 9 ++++++- hedroid/worker/vnc.py | 26 +++++++++++++++++---- 9 files changed, 75 insertions(+), 11 deletions(-) diff --git a/hedroid/master/droid_keeper.thrift b/hedroid/master/droid_keeper.thrift index 1308b4f..ecc635a 100644 --- a/hedroid/master/droid_keeper.thrift +++ b/hedroid/master/droid_keeper.thrift @@ -7,6 +7,7 @@ struct DroidRequest { struct ConnParams { 1: string host, 2: string port, + 3: optional string password, } exception ApplicationException { diff --git a/hedroid/master/tgen/droid_keeper/ttypes.py b/hedroid/master/tgen/droid_keeper/ttypes.py index bd2aeb6..4640d48 100644 --- a/hedroid/master/tgen/droid_keeper/ttypes.py +++ b/hedroid/master/tgen/droid_keeper/ttypes.py @@ -113,17 +113,20 @@ class ConnParams: Attributes: - host - port + - password """ thrift_spec = ( None, # 0 (1, TType.STRING, 'host', None, None, ), # 1 (2, TType.STRING, 'port', None, None, ), # 2 + (3, TType.STRING, 'password', None, None, ), # 3 ) - def __init__(self, host=None, port=None,): + def __init__(self, host=None, port=None, password=None,): self.host = host self.port = port + self.password = password def read(self, iprot): if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None: @@ -144,6 +147,11 @@ def read(self, iprot): self.port = iprot.readString() else: iprot.skip(ftype) + elif fid == 3: + if ftype == TType.STRING: + self.password = iprot.readString() + else: + iprot.skip(ftype) else: iprot.skip(ftype) iprot.readFieldEnd() @@ -162,6 +170,10 @@ def write(self, oprot): oprot.writeFieldBegin('port', TType.STRING, 2) oprot.writeString(self.port) oprot.writeFieldEnd() + if self.password is not None: + oprot.writeFieldBegin('password', TType.STRING, 3) + oprot.writeString(self.password) + oprot.writeFieldEnd() oprot.writeFieldStop() oprot.writeStructEnd() @@ -173,6 +185,7 @@ def __hash__(self): value = 17 value = (value * 31) ^ hash(self.host) value = (value * 31) ^ hash(self.port) + value = (value * 31) ^ hash(self.password) return value def __repr__(self): diff --git a/hedroid/master/thrift_server.py b/hedroid/master/thrift_server.py index af2fe9c..1981a56 100644 --- a/hedroid/master/thrift_server.py +++ b/hedroid/master/thrift_server.py @@ -59,6 +59,7 @@ def get_endpoint_for_user(self, user): cpars = ConnParams() cpars.host = endpoint_cpars.host cpars.port = endpoint_cpars.port + cpars.password = endpoint_cpars.password return cpars def interact_with_endpoint(self, dr): diff --git a/hedroid/worker/droid_service.thrift b/hedroid/worker/droid_service.thrift index d1f165e..8414675 100644 --- a/hedroid/worker/droid_service.thrift +++ b/hedroid/worker/droid_service.thrift @@ -1,6 +1,7 @@ struct ConnParams { 1: string host, 2: string port, + 3: optional string password, } exception ApplicationException { diff --git a/hedroid/worker/helpers.py b/hedroid/worker/helpers.py index 8b240bf..ce6c03f 100644 --- a/hedroid/worker/helpers.py +++ b/hedroid/worker/helpers.py @@ -1,7 +1,8 @@ import os +from hedroid.common_settings import DEBUG from hedroid.worker.zk_droid import DroidZkClient -from hedroid.worker.utils import restart_adb_server +from hedroid.worker.utils import restart_adb_server, generate_password from hedroid.logger import logger from hedroid.worker.xvfb import Xvfb from hedroid.worker.emulator import Emulator @@ -50,7 +51,10 @@ def stop(self): def cleanup(self): logger.debug('Cleaning up droid') + # Cleanup emulator self.emulator.cleanup() + # Cycle password for vnc server + self.x11vnc.cycle_password() class DroidBuilder(object): @@ -105,6 +109,7 @@ def build(self): x11vnc = X11VNC( display=self.display, clip=self.clip, + password=generate_password(), ) websockify = Websockify( source_port=self.ws_port, @@ -146,7 +151,10 @@ def setup(self): except IndexError: break except Exception as err: - import ipdb; ipdb.set_trace() + print err + if DEBUG: + import ipdb + ipdb.set_trace() def count(self): return len(self.initialised) diff --git a/hedroid/worker/tgen/droid_service/ttypes.py b/hedroid/worker/tgen/droid_service/ttypes.py index 07c7cbf..aa89205 100644 --- a/hedroid/worker/tgen/droid_service/ttypes.py +++ b/hedroid/worker/tgen/droid_service/ttypes.py @@ -22,17 +22,20 @@ class ConnParams: Attributes: - host - port + - password """ thrift_spec = ( None, # 0 (1, TType.STRING, 'host', None, None, ), # 1 (2, TType.STRING, 'port', None, None, ), # 2 + (3, TType.STRING, 'password', None, None, ), # 3 ) - def __init__(self, host=None, port=None,): + def __init__(self, host=None, port=None, password=None,): self.host = host self.port = port + self.password = password def read(self, iprot): if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated and isinstance(iprot.trans, TTransport.CReadableTransport) and self.thrift_spec is not None and fastbinary is not None: @@ -53,6 +56,11 @@ def read(self, iprot): self.port = iprot.readString() else: iprot.skip(ftype) + elif fid == 3: + if ftype == TType.STRING: + self.password = iprot.readString() + else: + iprot.skip(ftype) else: iprot.skip(ftype) iprot.readFieldEnd() @@ -71,6 +79,10 @@ def write(self, oprot): oprot.writeFieldBegin('port', TType.STRING, 2) oprot.writeString(self.port) oprot.writeFieldEnd() + if self.password is not None: + oprot.writeFieldBegin('password', TType.STRING, 3) + oprot.writeString(self.password) + oprot.writeFieldEnd() oprot.writeFieldStop() oprot.writeStructEnd() @@ -82,6 +94,7 @@ def __hash__(self): value = 17 value = (value * 31) ^ hash(self.host) value = (value * 31) ^ hash(self.port) + value = (value * 31) ^ hash(self.password) return value def __repr__(self): diff --git a/hedroid/worker/thrift_server.py b/hedroid/worker/thrift_server.py index 706ad16..dbdbe75 100644 --- a/hedroid/worker/thrift_server.py +++ b/hedroid/worker/thrift_server.py @@ -1,7 +1,6 @@ import os import atexit -import requests import shutil from hedroid.logger import logger @@ -10,7 +9,8 @@ from hedroid.worker.utils import get_package_name_from_url from hedroid.common_settings import VAR_DIR -from hedroid.worker.tgen.droid_service.ttypes import ConnParams, ApplicationException +from hedroid.worker.tgen.droid_service.ttypes import ConnParams +from hedroid.worker.tgen.droid_service.ttypes import ApplicationException from hedroid.worker.tgen.droid_service import DroidService from thrift.transport import TSocket @@ -66,6 +66,7 @@ def get_endpoint(self, endpoint_id): cp = ConnParams() cp.host = get_public_hostname() cp.port = endpoint.websockify.source_port + cp.password = endpoint.x11vnc.password return cp def run_operation(self, endpoint_id, operation, apk_url): @@ -111,5 +112,6 @@ def start_server(): handler.pre_server_start_log() server.serve() + if __name__ == '__main__': start_server() diff --git a/hedroid/worker/utils.py b/hedroid/worker/utils.py index c082a10..6596a2b 100644 --- a/hedroid/worker/utils.py +++ b/hedroid/worker/utils.py @@ -2,6 +2,7 @@ import json import os import socket +import uuid from copy import deepcopy from tempfile import NamedTemporaryFile @@ -9,7 +10,7 @@ from requests.exceptions import ConnectTimeout import subprocess32 as subprocess -from hedroid.common_settings import CONFIG_PATH, HOSTNAME +from hedroid.common_settings import CONFIG_PATH, HOSTNAME, DEBUG from hedroid.logger import logger @@ -123,5 +124,11 @@ def get_config(): return deepcopy(CachedGlobals.CONFIG) +def generate_password(): + if DEBUG: + return "youknowthis" + return uuid.uuid4().hex + + if __name__ == '__main__': print is_open_port(5672) diff --git a/hedroid/worker/vnc.py b/hedroid/worker/vnc.py index 45ba1f9..4fa7414 100644 --- a/hedroid/worker/vnc.py +++ b/hedroid/worker/vnc.py @@ -6,14 +6,16 @@ from hedroid.logger import logger from hedroid.common_settings import VAR_DIR from hedroid.worker.executor import Executor +from hedroid.worker.utils import generate_password class X11VNCExecutor(Executor): - def __init__(self, display, rfb_port, clip): + def __init__(self, display, rfb_port, clip, password): super(X11VNCExecutor, self).__init__(display) self.display = display self.rfb_port = rfb_port self.clip = clip + self.password = password self._portfile_p = os.path.join(VAR_DIR, "vnc-port-{}".format(display)) @@ -33,8 +35,10 @@ def portfile_wait(self): def _start(self): cmd = ["x11vnc", "-forever", "-rfbport", str(self.rfb_port), "-display", ":{}".format(self.display), + "-noxdamage", "-xkb", "-noscr", "-noxfixes", "-nowf", "-ncache", "10", - "-clip", self.clip, "-flag", self._portfile_p] + "-clip", self.clip, "-flag", self._portfile_p, + "-passwd", self.password] # Remove portfile if it previously exists try: os.remove(self._portfile_p) @@ -60,9 +64,10 @@ def stop(self): class X11VNC(object): - def __init__(self, display=None, clip=None): + def __init__(self, display=None, clip=None, password="youknowthis"): self.display = display or os.environ.get('DISPLAY_PORT', '1') self.clip = clip or '530x965+15+30' + self.password = password self.executor = None @@ -72,10 +77,23 @@ def get_rfbport(self): def start(self): logger.debug('Starting X11VNC') - self.executor = X11VNCExecutor(self.display, self.get_rfbport(), self.clip) + self.executor = X11VNCExecutor( + self.display, self.get_rfbport(), self.clip, self.password) self.executor.start() logger.debug('X11VNC Ready!') + def cycle_password(self): + logger.debug('Cycling passwords for x11vnc') + # Set new password + self.password = generate_password() + # Stop the server + self.executor.stop() + self.executor = X11VNCExecutor( + self.display, self.get_rfbport(), self.clip, self.password) + # Start the server again + self.executor.start() + logger.debug('Password cycle complete!') + def stop(self): logger.debug('Stopping X11VNC') self.executor.stop()