def __init__(self, feedID, clientKey):
        Thread.__init__(self)
        self.setName("FeedHandler")
        self.terminator = Terminator.getInstance()

        self.certHandler = CertificateHandler(feedID, 'server')
        self.certHandler.prep()

        self.publicKey, privateKey = self.certHandler.getKeyPair()
        clientsPath = self.certHandler.getEnrolledKeysPath()

        self.ctxHandler = ContextHandler(clientsPath)
        context = self.ctxHandler.getContext()

        self.saveClientKey(clientKey)

        self.socket = context.socket(zmq.REP)
        self.socket.setsockopt(zmq.RCVTIMEO, 20000)

        monitorSocket = self.socket.get_monitor_socket()
        self.monitor = Monitor(monitorSocket, feedID)
        self.monitor.setDaemon(True)
        self.monitor.start()

        self.socket.curve_secretkey = privateKey
        self.socket.curve_publickey = self.publicKey
        self.socket.curve_server = True

        self.port = self.socket.bind_to_random_port('tcp://*',
                                                    min_port=49151,
                                                    max_port=65535)
        self.feedID = feedID
Beispiel #2
0
    def __init__(self, addr):
        Thread.__init__(self)
        self.setName("FeedListener")
        self.terminator = Terminator.getInstance()

        certHandler = CertificateHandler("front", 'server')

        publicKey, privateKey = certHandler.getKeyPair()
        clientsPath = certHandler.getEnrolledKeysPath()

        self.ctxHandler = ContextHandler(clientsPath)
        context = self.ctxHandler.getContext()
        self.socket = context.socket(zmq.REP)

        monitorSocket = self.socket.get_monitor_socket()
        monitor = Monitor(monitorSocket, 'front')
        monitor.setDaemon(True)
        monitor.start()

        self.socket.curve_secretkey = privateKey
        self.socket.curve_publickey = publicKey
        self.socket.curve_server = True

        self.socket.bind(addr)
        self.socket.setsockopt(zmq.RCVTIMEO, 10000)
        logging.info('Socket setup, public key: %s', publicKey)
    def __init__(self):
        Thread.__init__(self)
        self.terminator = Terminator.getInstance()
        self.setName("Authenticator")

        try:
            self.db = sql.connect(os.environ["DBHOST"], os.environ["DBUSER"],
                                  os.environ["DBPASS"], os.environ["DBNAME"])

            self.cursor = self.db.cursor()

            logging.debug('Connected to database')
        except:
            logging.critical('Exception occured connecting to database',
                             exc_info=True)
            self.terminator.autoTerminate()

        try:
            certHandler = CertificateHandler("front", 'server')
            publicKey, privateKey = certHandler.getKeyPair()
            clientKeysPath = certHandler.getEnrolledKeysPath()

            self.ctxHandler = ContextHandler(clientKeysPath)
            context = self.ctxHandler.getContext()
            self.socket = context.socket(zmq.REP)

            try:
                monitorSocket = self.socket.get_monitor_socket()
                self.monitor = Monitor(monitorSocket, 'Registration')
                self.monitor.setDaemon(True)
                self.monitor.start()

                logging.debug('Socket monitor thread started')
            except:
                logging.error(
                    'Exception occured starting socket monitor thread',
                    exc_info=True)

            self.socket.curve_secretkey = privateKey
            self.socket.curve_publickey = publicKey
            self.socket.curve_server = True
            self.socket.setsockopt(zmq.RCVTIMEO, 10000)
            self.socket.bind('tcp://0.0.0.0:5001')

            logging.debug('Socket setup and bound, listening')
        except:
            logging.critical('Exception occured setting up auth socket',
                             exc_info=True)
def setupGlobalSocket(addr):
	certHandler = CertificateHandler('front', 'client')
	serverKey = certHandler.getEnrolledKeys()
	publicKey, privateKey = certHandler.getKeyPair()

	ctxHandler = ContextHandler(certHandler.getEnrolledKeysPath())
	context = ctxHandler.getContext()

	socket = context.socket(zmq.REQ)
	socket.curve_secretkey = privateKey
	socket.curve_publickey = publicKey
	socket.curve_serverkey = serverKey

	socket.connect(mainAddr + registrationPort)

	return socket
def enroll():
	try:
		unsecuredCtx = zmq.Context()
		unsecuredSocket = unsecuredCtx.socket(zmq.REQ)
		unsecuredSocket.setsockopt(zmq.RCVTIMEO, 5000)
		unsecuredSocket.setsockopt(zmq.SNDTIMEO, 5000)
		unsecuredSocket.setsockopt(zmq.LINGER, 0)

		unsecuredSocket.connect(mainAddr + unsecuredEnrollPort)
		certHandler = CertificateHandler('front', 'client')
		certHandler.prep()
		publicKey, _ = certHandler.getKeyPair()

		publicKey = publicKey.decode('utf-8')

		unsecuredSocket.send_string(str(publicKey))
		serverKey = unsecuredSocket.recv_string()

		certHandler.savePublicKey(serverKey)
		logging.debug('Enrolled')

	except Exception as e:
		logging.critical("Could not enroll", exc_info=True)
		unsecuredSocket.close()
		return False
	return True
Beispiel #6
0
    def setupSocket(self, certID, serverKey=True):
        certHandler = CertificateHandler(certID, 'client')
        ctxHandler = ContextHandler(certHandler.getEnrolledKeysPath())

        context = ctxHandler.getContext()
        socket = context.socket(zmq.REQ)

        monitorSocket = socket.get_monitor_socket()
        monitor = Monitor(monitorSocket, certID)
        monitor.setDaemon(True)
        monitor.start()

        if certID != 'front':
            certHandler.prep()

        publicKey, privateKey = certHandler.getKeyPair()

        if serverKey:
            serverKey = certHandler.getEnrolledKeys()
            socket.curve_serverkey = serverKey

        socket.curve_secretkey = privateKey
        socket.curve_publickey = publicKey

        return socket, certHandler, ctxHandler
class Enroller(Thread):
    def __init__(self, feedListener, authListener):
        Thread.__init__(self)
        self.setName("Enroller")

        self.feedListener = feedListener
        self.authListener = authListener

        self.terminator = Terminator.getInstance()

        self.unsecuredCtx = zmq.Context()
        self.unsecuredSocket = self.unsecuredCtx.socket(zmq.REP)

        monitorSocket = self.unsecuredSocket.get_monitor_socket()
        self.monitor = Monitor(monitorSocket, "Enroller")
        self.monitor.setDaemon(True)
        self.monitor.start()

        self.unsecuredSocket.bind('tcp://0.0.0.0:5002')
        self.unsecuredSocket.setsockopt(zmq.RCVTIMEO, 10000)

        self.certHandler = CertificateHandler('front', 'server')
        self.publicKey, _ = self.certHandler.getKeyPair()

        self.publicKey = self.publicKey.decode('utf-8')

    def run(self):
        while not self.terminator.isTerminating():
            try:
                clientKey = self.unsecuredSocket.recv_string()
                self.certHandler.savePublicKey(clientKey)
                self.feedListener.ctxHandler.configureAuth()
                self.authListener.ctxHandler.configureAuth()
                self.unsecuredSocket.send_string(str(self.publicKey))
                logging.debug("New client enrolled %s", clientKey)
            except:
                logging.debug('Error enrolling')

        self.unsecuredSocket.close()
        self.monitor.stop = True
        logging.info('Enroller thread shutting down')
    def __init__(self, feedListener, authListener):
        Thread.__init__(self)
        self.setName("Enroller")

        self.feedListener = feedListener
        self.authListener = authListener

        self.terminator = Terminator.getInstance()

        self.unsecuredCtx = zmq.Context()
        self.unsecuredSocket = self.unsecuredCtx.socket(zmq.REP)

        monitorSocket = self.unsecuredSocket.get_monitor_socket()
        self.monitor = Monitor(monitorSocket, "Enroller")
        self.monitor.setDaemon(True)
        self.monitor.start()

        self.unsecuredSocket.bind('tcp://0.0.0.0:5002')
        self.unsecuredSocket.setsockopt(zmq.RCVTIMEO, 10000)

        self.certHandler = CertificateHandler('front', 'server')
        self.publicKey, _ = self.certHandler.getKeyPair()

        self.publicKey = self.publicKey.decode('utf-8')
Beispiel #9
0
    else:
        loggerMode = logging.INFO

    logging.basicConfig(
        format='%(levelname)s - %(asctime)s - %(threadName)s - %(message)s',
        level=loggerMode,
        handlers=[
            logging.FileHandler('../Logs/server_logs.txt'),
            logging.StreamHandler(sys.stdout)
        ])

    logging.info('\n\n\n\t\tBegin new set of logs:\n\n\n')

    terminator = Terminator.getInstance()

    certHandler = CertificateHandler('front', 'server')
    certHandler.prep()

    try:
        modelHandler = ModelHandler.getInstance()
    except:
        logging.critical('Exception loading model/graph', exc_info=True)
        terminator.autoTerminate()

    try:
        feedListener = FeedListener('tcp://0.0.0.0:5000')
        feedListener.setDaemon(True)
        feedListener.start()
        logging.debug('Feed listener thread started')
    except:
        logging.critical('Exception occured starting feed listener thread',
class FeedHandler(Thread):
    def __init__(self, feedID, clientKey):
        Thread.__init__(self)
        self.setName("FeedHandler")
        self.terminator = Terminator.getInstance()

        self.certHandler = CertificateHandler(feedID, 'server')
        self.certHandler.prep()

        self.publicKey, privateKey = self.certHandler.getKeyPair()
        clientsPath = self.certHandler.getEnrolledKeysPath()

        self.ctxHandler = ContextHandler(clientsPath)
        context = self.ctxHandler.getContext()

        self.saveClientKey(clientKey)

        self.socket = context.socket(zmq.REP)
        self.socket.setsockopt(zmq.RCVTIMEO, 20000)

        monitorSocket = self.socket.get_monitor_socket()
        self.monitor = Monitor(monitorSocket, feedID)
        self.monitor.setDaemon(True)
        self.monitor.start()

        self.socket.curve_secretkey = privateKey
        self.socket.curve_publickey = self.publicKey
        self.socket.curve_server = True

        self.port = self.socket.bind_to_random_port('tcp://*',
                                                    min_port=49151,
                                                    max_port=65535)
        self.feedID = feedID

    def getPort(self):
        return self.port

    def getPublicKey(self):
        return self.publicKey

    def saveClientKey(self, key):
        self.certHandler.savePublicKey(key)
        self.ctxHandler.configureAuth()

    def run(self):
        helper = FeedProcessHelper()
        modelHandler = ModelHandler.getInstance()

        model = modelHandler.getModel()
        session = modelHandler.getSession()
        graph = modelHandler.getGraph()

        resultHandler = ResultsHandler(16, 5)

        bgRemover = BackgroundRemover()

        with session.as_default():
            with graph.as_default():
                while not self.terminator.isTerminating():
                    try:
                        received = self.socket.recv_string()
                    except:
                        break

                    # handling timeout for deferred thread
                    if received == 'wait':
                        self.socket.send_string("ok")
                    else:
                        jpegStr = b64.b64decode(received)
                        jpeg = np.fromstring(jpegStr, dtype=np.uint8)
                        frame = cv2.imdecode(jpeg, 1)
                        height, width, _ = np.shape(frame)

                        regions, drawCoords = helper.extractRegions(
                            frame, 4, (64, 64))
                        results = np.around(model.predict(regions)[:, 10:],
                                            decimals=3)
                        resultHandler.append(results)
                        finalResult, bboxIndex = resultHandler.assess()

                        boundingBoxes = bgRemover.apply(frame)

                        response = pickle.dumps(
                            (finalResult, boundingBoxes,
                             (resultHandler.getAverages(), drawCoords)),
                            protocol=4)
                        self.socket.send(response)

        # self.socket.disable_monitor()
        self.monitor.stop = True
        self.ctxHandler.cleanup()
        self.certHandler.cleanup()
        self.socket.close()
        logging.debug("Ending thread %s", self.feedID)