def get_all_instances(): page=int(request.args['page']) instances=redis_store.hgetall('instances') ans=[] counter=0 if(len(instances)==0): load_to_redis() for inst in instances: if counter<(page-1)*20: counter+=1 continue inst=json.loads(instances[inst]) chal=json.loads(redis_store.hget('chals',inst['chalid'])) team=json.loads(redis_store.hget('teams',inst['teamid'])) ans.append({ 'id':inst['id'], 'name': inst['name'], 'teamname':team['nickname'], 'chalname':chal['name'], 'ip':inst['ip'], 'status':inst['status'], 'password':inst['password'] }) if len(ans)==20: break return jsonify({ 'total':len(instances), 'instances':ans })
def get_instances(chalid): instances=redis_store.hgetall('instances') ans=[] for inst in instances: instance=json.loads(instances[inst]) if instance['chalid']==chalid: ans.append(instance) return jsonify(ans)
def team_list(): ans = [] result_in_json = {} teams_in_redis = redis_store.hgetall('teams') if not teams_in_redis: total = db.session.query(db.func.count(Teams.id)).scalar() if request.args.has_key('page'): page = int(request.args['page']) teams = db.session.query(Teams).join(Origin).limit(20).offset( (page - 1) * 20).all() else: teams = db.session.query(Teams).join(Origin).all() for team in teams: json_team = { 'id': team.id, 'name': team.name, 'nickname': team.nickname, 'score': team.score, 'password': team.origin_pass[0].password, 'attackid': team.attackid, 'instances': [] } if not redis_store.hget('attackpack', team.attackid): redis_store.hset('attackpack', team.attackid, team.id) ans.append({ 'id': team.id, 'name': team.name, 'nickname': team.nickname, 'score': team.score }) result_in_json[team.id] = json.dumps(json_team) redis_store.hmset('teams', result_in_json) else: total = len(teams_in_redis) if request.args.has_key('page') and request.args['page'] != '': page = int(request.args['page']) has_page = True else: has_page = False counter = 0 for teamid in teams_in_redis: if has_page and counter < (page - 1) * 20: counter += 1 continue team = json.loads(teams_in_redis[teamid]) ans.append({ 'id': team['id'], 'name': team['name'], 'nickname': team['nickname'], 'score': team['score'] }) if has_page and len(ans) == 20: break return jsonify({'total': total, 'users': ans})
def chals(): ans=[] chals_in_redis=redis_store.hgetall('chals') if not chals_in_redis: total=db.session.query(db.func.count(Challenges.id)).scalar() if request.args.has_key('page'): page=int(request.args['page']) chals=Challenges.query.limit(20).offset((page-1)*20).all() else: chals=Challenges.query.all() result_json={} for chal in chals: chal_json={ 'id':chal.id, 'name':chal.name, 'dockername':chal.dockername, 'type':chal.type, 'score':chal.score, 'command':chal.command, 'flagcommand':chal.flagcommand, 'desc':chal.desc } ans.append(chal_json) result_json[chal.id]=json.dumps(chal_json) if len(result_json): redis_store.hmset('chals',result_json) else: total=len(chals_in_redis) if request.args.has_key('page'): page=int(request.args['page']) has_page=True else: has_page=False counter=0 for chalid in chals_in_redis: if has_page and counter<(page-1)*20: counter+=1 continue ans.append(json.loads(chals_in_redis[chalid])) if has_page and len(ans)==20: break return jsonify({ 'total':total, 'chals':ans })