def to_service(self, team, service): flag = self.generate_flags() flag_id = self.generate_flag_ids() print (team['name'] + ' ' + service['name'] + ' ' + flag) #print (flag_id) self.db.flags.insert_one({ 'round': self.round_count, 'team': team, 'service': service, 'flag': flag, 'flag_id': flag_id, 'stolen': False, 'timestamp': time.time() }) path = self.path_to_checkers + service['name'] + '/' + self.filename_checkers action = '' try: action = 'check' self.checker.check(team['host'], path) action = 'put' self.checker.put(team['host'], path, flag, flag_id) action = 'get' self.checker.get(team['host'], path, flag, flag_id) self.update_scoreboard(team, service, 101) except Exception as error: code, message = error.args Message.fail(team['name'] + ' ' + service['name'] + ' ' + action + ' => error (message: ' + str(message) + ')') self.update_scoreboard(team, service, code, message)
def update_scoreboard(self, team, service, status_code, message=''): codes = { 101: 'UP', 102: 'CORRUPT', 103: 'MUMBLE', 104: 'DOWN' } # self.status_service[team['name'] + '_' + service['name']] = status_code if status_code not in codes: Message.fail('\t Invalid checker return code for ' + service['name']) status_code = 104 self.db.scoreboard.update_one( { 'team._id': team['_id'], 'service._id': service['_id'] }, { "$set": { "status": codes[status_code], 'message': message }, '$inc': { 'up_round': 1 if status_code == 101 else 0 } } )
def to_service(self, round, team, service, flag, flag_id): team = json_util.loads(json.dumps(team)) service = json_util.loads(json.dumps(service)) self.db.flags.insert_one({ 'round': round, 'team': team, 'service': service, 'flag': flag, 'flag_id': flag_id, 'stolen': False, 'timestamp': time.time() }) path = self.path_to_checkers + service['name'] + '/' + self.filename_checkers action = '' try: action = 'check' self.checker.check(team['host'], path) action = 'put' self.checker.put(team['host'], path, flag, flag_id) action = 'get' self.checker.get(team['host'], path, flag, flag_id) self.update_scoreboard(team, service, 101) except Exception as error: code, message = error.args print(error) Message.fail(team['name'] + ' ' + service['name'] + ' ' + action + ' => error (message: ' + str(message) + ')') self.update_scoreboard(team, service, code, message)
def __init__(self, method='json'): Message.info('Get config from ' + method) if method == 'json': self.from_json() else: self.from_api() if not self.loaded: Message.fail("Couldn't get config...Exit") sys.exit(0)
def __init__(self, method='api'): Message.info('Get config from ' + method) if method == 'json': self.from_json() else: self.from_api() if not self.loaded: Message.fail("Couldn't get config...Exit") sys.exit(0)
def from_api(self): try: response = requests.get("http://%s:%s" % (API['HOST'], API['PORT'])).json() data = response['response'] self.services = data["services"] self.teams = data["teams"] self.settings.update(data["settings"]) self.loaded = True except Exception as e: print(e) Message.fail('Error with requests in response')
def __init__(self, db): self.db = db self.conn = None self.address = None try: self.life = CHECKER['LENGTH'] * CHECKER['ROUND_LENGTH'] self.port = CHECKER['PORT'] except KeyError: Message.fail('Error with parse in response') sys.exit(0)
def __init__(self, args): self.args = args Message.info('Get config from ' + self.args.type) if self.args.type == 'json': self.from_json() else: self.from_api() if not self.loaded: Message.fail("Couldn't get config...Exit") sys.exit(0)
def __init__(self, db, config): self.db = db self.conn = None self.address = None config = ConfigJson('tmp.config.json'); try: lifetime = config.settings["flags"]["lifetime"] round_length = config.settings["round_length"] except KeyError: Message.fail('Error with parse in response') sys.exit(0) self.life = lifetime * round_length self.port = config.settings['flags']['port']
def from_api(self): try: response = requests.get("http://10.16.255.196:5000").json() data = response['response'] self.services = data["services"] self.teams = data["teams"] self.settings.update(data["settings"]) self.loaded = True except Exception as e: print(e) Message.fail('Error with requests in response')
def __init__(self, db): self.db = db self.conn = None self.address = None self.config = ConfigGet(self.db) try: lifetime, round_length = CHECKER["LENGTH"], CHECKER["ROUND_LENGTH"] except KeyError: Message.fail("Error with parse in response") sys.exit(0) self.life = lifetime * round_length self.port = 2605
def from_json(self): if not os.path.isfile(self.path_to_config_file): Message.fail('File not found: ' + self.path_to_config_file) return self.filename = self.path_to_config_file try: with open(self.filename) as data_file: data = json.load(data_file) self.settings = data['settings'] self.teams = data['teams'] self.services = get_checkers_src(data['services']) self.loaded = True except KeyError as e: Message.fail('Not found parameter ' + str(e) + ' in ' + self.path_to_config_file)
def __init__(self, db): self.db = db self.conn = None self.address = None self.config = ConfigGet(self.db) try: lifetime, round_length = CHECKER['LENGTH'], CHECKER['ROUND_LENGTH'] except KeyError: Message.fail('Error with parse in response') sys.exit(0) self.life = lifetime * round_length self.port = FLAGS['PORT']
def __init__(self, db): self.db = db self.conn = None self.address = None self.config = ConfigGet(self.db) try: lifetime = CHECKER['LENGTH'] round_length = CHECKER['ROUND_LENGTH'] except KeyError: Message.fail('Error with parse in response') sys.exit(0) self.life = lifetime * round_length self.port = 2605 #self.config.settings['flags']['port']
def __init__(self, filename): if not os.path.isfile(filename): self.loaded = False Message.fail('File not found: ' + filename) return self.filename = filename; try: with open(filename) as data_file: data = json.load(data_file) self.settings = data['settings'] self.teams = data['teams'] self.services = data['services'] self.loaded = True except Exception: Message.fail('Invalid format of file: ' + filename) self.loaded = False
def from_json(self): if not os.path.isfile(self.path_to_config_file): Message.fail('File not found: ' + self.path_to_config_file) return self.filename = self.path_to_config_file try: with open(self.filename) as data_file: data = json.load(data_file) self.settings = data['settings'] self.teams = data['teams'] self.services = data['services'] self.loaded = True except KeyError as e: Message.fail('Not found parameter ' + str(e) + ' in ' + self.path_to_config_file)
def from_api(self): try: # Определяем путь до API сервера (откуда мы получаем команды) api_url = self.args.url if self.args.url != '' else API_SERVER if api_url == '': Message.fail('API server url is not defined in config/main.py.') Message.fail('Use python3 main.py init --url=\'SOME_URL\' instead') sys.exit(0) response = requests.get(api_url).json() data = response['response'] self.services = data["services"] self.teams = data["teams"] self.settings.update(data["settings"]) self.loaded = True except Exception as e: print(e) Message.fail('Error with requests in response')