예제 #1
0
파일: DB.py 프로젝트: hvuhsg/codeNet
    def __init__(self, path):
        self.path = path
        self.stop = False
        self.old_db = None
        self.load_is_need = False
        self._db_log = log("logs\db_log.log")
        self._EDY = EDY

        self.commend_list = {
            "'a'": "a",
            "456": '456',
            "max(5, 9)": '9',
            "print('hello')": "None",
            "eval('1+2')": '3',
            "min(7, 5)": '5',
            '7+5': '12'
        }
        self.users = {}
        self._users_connect = {}
        self.errors = errors.LIST_OF_ERRORS

        self.server_list = set()
        server.meneger(self)

        self.groups = {}

        self.bots = {}
        self.public_bots_data = {}
        self.token = 0

        save_thread = Thread(target=self.save)
        load_thread = Thread(target=self.load)
        load_thread.start()
        save_thread.start()
예제 #2
0
파일: verify.py 프로젝트: asmundh/Progsek
    def GET(self, verification_key):
        """
        Get the verification form

            :return: A page with the verification form
        """
        user_was_verified = models.user.verify_user_by_email(verification_key)

        session = web.ctx.session
        nav = get_nav_bar(session)

        if (user_was_verified):
            return render.verify(nav, "Success", "")

        log("VERIFY", web.ctx['ip'],
            [('Username', session.username),
             ("Response: ", "Verify OK, user verified")])
        return render.verify(nav, "", "User was verified")
예제 #3
0
파일: protocol.py 프로젝트: hvuhsg/codeNet
    def __init__(self):
        self.db = DB.DB(r'DB\dby.db')

        self.main_log = log('logs\protocol.log')

        self.protocols = [
            users_protocol(self.db, self.main_log),
            servers_protocol(self.db, self.main_log),
            groups_protocol(self.db, self.main_log),
            bots_protocol(self.db, self.main_log)
        ]
예제 #4
0
    def set_container_logs_and_errors(self, containers, results, job):
        for container in containers:
            image_name = container.attrs["Config"]["Image"]
            container_id = \
                f'{image_name}_{container.short_id}'
            run_logs = container.logs(timestamps=True).decode()
            results.json_results_from_logs = self.get_json_out(run_logs)
            log.log('CONTAINER', f'{container_id} logs begin \n' + ('-' * 80))
            log.log('CONTAINER', run_logs)
            log.log('CONTAINER', f'{container_id} logs end \n' + ('-' * 80))
            log_url = self.upload_logs(
                run_logs, filename=f'{image_name}_job-{job.id}.txt')

            exit_code = container.attrs['State']['ExitCode']
            if exit_code != 0:
                results.errors[container_id] = f'Container failed with' \
                    f' exit code {exit_code}'
                log.error(f'Container {container_id} failed with {exit_code}'
                          f' for job {box2json(job)}, logs: {log_url}')
            elif container.status == 'dead':
                results.errors[container_id] = f'Container died, please retry.'
                log.error(f'Container {container_id} died'
                          f' for job {box2json(job)}, logs: {log_url}')

            log.info(f'Uploaded logs for {container_id} to {log_url}')
            results.logs[container_id] = log_url
예제 #5
0
    def __init__(self, port):
        self.socket_list = []
        self.port = port

        self.main_socket = socket.socket()
        self.main_socket.bind(('0.0.0.0', port))
        self.main_socket.listen(10)

        self.users_data = {}
        self.protocol = protocol()
        self.server_log = log("logs\server.log")

        self.address_of_sockets = {}
예제 #6
0
    def POST(self):
        """
        Renders an authorization page to be scanned by google authorizer app
        :return:
        """
        session = web.ctx.session
        session.auth = False
        data = web.input(key="")

        inp = web.input()
        if not ('csrf_token' in inp and inp.csrf_token == session.pop('csrf_token', None)):
            raise web.badrequest()

        # Check if inputted is correct
        validated = validate_key(session.unauth_username, data.key)
        if not validated:
            log("TWO-FACTOR", web.ctx['ip'], [('Username',session.unauth_username), ("Key", data.key), ("Response: ", "Login failed, auth key is wrong")])
            session.csrf_token = uuid4().hex
            return render.qr_verify(session.auth_url, qr_verify_form, "Wrong authenticator code", session.csrf_token) 
        if validated:
            log("TWO-FACTOR", web.ctx['ip'], [('Username',session.unauth_username), ("Key", data.key), ("Response: ", "Login OK, auth key correct")])
            Login.login(session.unauth_userid, session.unauth_username, session.unauth_remember)
            raise web.seeother("/")
예제 #7
0
파일: login.py 프로젝트: asmundh/Progsek
    def POST(self):
        """
        Log in to the web application and register the session
            :return:  The login page showing other users if logged in
        """
        session = web.ctx.session
        nav = get_nav_bar(session)
        data = web.input(username="", password="", remember=False)

        inp = web.input()
        if not ('csrf_token' in inp
                and inp.csrf_token == session.pop('csrf_token', None)):
            raise web.badrequest()

        # Validate login credential with database query
        user_exists = models.user.check_user_exists(data.username)
        print("USEREXIST: {}".format(user_exists))
        if not user_exists:
            # Lockout if too many failed attempts
            if not (write_to_logins(str(web.ctx['ip']))):
                return render.login(
                    nav, login_form,
                    "- Too many login attempts in short amount of time")

            log("LOGIN", web.ctx['ip'],
                [('Username', data.username),
                 ("Response: ", "Login failed, user does not exist")])
            session.csrf_token = uuid4().hex
            return render.login(nav, login_form,
                                "- User authentication failed",
                                session.csrf_token)

        user = None
        stored_password = models.user.get_password_by_user_name(data.username)
        if (verify_password(stored_password, data.password)):
            user = models.user.match_user(data.username, stored_password)
            userid = get_user_id_by_name(data.username)
            session.unauth_username = data.username
            session.unauth_userid = userid
            session.unauth_remember = 1 if data.remember else 0
            user = get_user(session.unauth_userid)
            email = user[0][5]
            qr_verification_key = get_key(session.unauth_username)
            if qr_verification_key != None:
                url = generate_url("beelance", email, qr_verification_key)
                session.auth_url = url

        user_is_verified = models.user.check_if_user_is_verified_by_username(
            data.username)

        # If there is a matching user/password in the database the user is logged in
        if user:
            if not user_is_verified:
                session.csrf_token = uuid4().hex
                # Lockout if failed attempts
                if not (write_to_logins(str(web.ctx['ip']))):
                    return render.login(
                        nav, login_form,
                        "- Too many login attempts in short amount of time",
                        session.csrf_token)

                log("LOGIN", web.ctx['ip'],
                    [('Username', data.username),
                     ("Password", stored_password),
                     ("Response: ", "Login failed, User not verified")])
                return render.login(nav, login_form, "- User not verified",
                                    session.csrf_token)

            if qr_verification_key == None:
                # Lockout if failed attempts
                session.csrf_token = uuid4().hex
                if not (write_to_logins(str(web.ctx['ip']))):
                    return render.login(
                        nav, login_form,
                        "- Too many login attempts in short amount of time",
                        session.csrf_token)

                log("LOGIN", web.ctx['ip'], [
                    ('Username', data.username), ("Password", stored_password),
                    ("Response: ", "Login failed, docker might have restarted")
                ])
                return render.login(
                    nav, login_form,
                    "- User authentication failed. This might be because docker demon has restarted",
                    session.csrf_token)
            else:
                log("LOGIN", web.ctx['ip'],
                    [('Username', data.username),
                     ("Password", stored_password),
                     ("Response: ",
                      "Login accepted, forwarded to two factor auth")])

                raise web.seeother("/qr_verify")
        else:
            log("LOGIN", web.ctx['ip'],
                [('Username', data.username), ("Password", stored_password),
                 ("Response: ", "Login failed, username/password mismatch")])
            session.csrf_token = uuid4().hex
            # Lockout if failed attempts
            if not (write_to_logins(str(web.ctx['ip']))):
                return render.login(
                    nav, login_form,
                    "- Too many login attempts in short amount of time",
                    session.csrf_token)

            return render.login(nav, login_form,
                                "- User authentication failed",
                                session.csrf_token)
예제 #8
0
    def monitor_containers(self, containers):
        success = True
        running = True
        failed = False
        dead = False
        last_timestamps = [None] * len(containers)
        last_loglines = [None] * len(containers)
        while running and not (failed or dead):
            # Refresh container status
            containers = [
                self.docker.containers.get(c.short_id) for c in containers
            ]

            running = [
                c for c in containers if c.status in ['created', 'running']
            ]

            for container_idx, container in enumerate(containers):
                # TODO: logger.add("special.log", level='CONTAINER')
                #  then use frontail to stream it from the server
                last_timestamp = last_timestamps[container_idx]
                if last_timestamp is None:
                    log_lines = container.logs(timestamps=True).decode()
                    log_lines = re.split('\n', log_lines.strip())
                    last_timestamp = self.get_last_timestamp(log_lines)
                else:
                    log_lines = container.\
                        logs(timestamps=True, since=last_timestamp).decode()
                    log_lines = re.split('\n', log_lines.strip())
                    last_logline = last_loglines[container_idx]
                    if last_logline is not None:
                        try:
                            # noinspection PyTypeChecker
                            dupe_index = log_lines.index(last_logline)
                            log_lines = log_lines[dupe_index + 1:]
                        except ValueError:
                            pass

                    last_timestamp = (self.get_last_timestamp(log_lines)
                                      or last_timestamp)

                if log_lines:
                    # noinspection PyTypeChecker
                    last_loglines[container_idx] = log_lines[-1]
                    log.log('CONTAINER', '\n'.join(log_lines))
                last_timestamps[container_idx] = last_timestamp

            # TODO: Do a container.logs(since=last, timestamps=True) and
            #   log those in real time.

            # TODO: For N bots or N problems, we probably want to make a best
            #  effort so long as at least 1 bot and one problem are still alive.
            dead = [c for c in containers if c.status == 'dead']
            failed = [
                c for c in containers if c.attrs['State']['ExitCode'] > 0
            ]

            if dead:
                success = False
                log.error(f'Dead container(s) found: {dead}')

            if failed:
                success = False
                log.error(f'Containers failed: {failed}')

            time.sleep(0.1)
        for container in running:
            log.error(f'Stopping orphaned container: {container}')
            container.stop(timeout=1)
        log.info('Finished running containers %s' % containers)
        return containers, success