Ejemplo n.º 1
0
def start_tasks(task_id: str, target):
    if not task_id:
        msg = {'success': False, 'data': {}, 'message': 'task_id不能为空'}
        return msg
    targets = jsonable_encoder(target)
    if not targets['targets']:
        msg = {'success': False, 'data': {}, 'message': '扫描目标不能为空'}
        return msg
    rst: Query = db.query(Tasks).filter(Tasks.task_id == task_id).filter(Tasks.status==1).first()
    if not rst:
        msg = {'success': False, 'data': {}, 'message': '任务id:{}不存在,请先创建'.format(task_id)}
        return msg
    try:
        total = fetch_tasks(targets)

        db.query(Tasks).filter(Tasks.task_id==task_id).filter(Tasks.status==1).\
            update({'targets': targets['targets'], 'status': 2})
        db.commit()

        msg = {'success': True, 'data': {'targets': targets['targets'], 'total':total}, 'message': '任务启动成功'}
        return msg
    except Exception as e:
        db.rollback()
        logger.exception(e)
        msg = {'success': False, 'data': {}, 'message': '系统错误'}
        return msg
Ejemplo n.º 2
0
def login_access_token(login: Login):
    '''

    :param login:
    :return:
    '''
    appid = login.appId
    secret_key = login.secretKey
    try:
        db: Session = SessionLocal()
        invoker: Invoker = db.query(Invoker).filter(Invoker.app_id == appid).filter(Invoker.secret_key == secret_key)\
            .filter(Invoker.active==1).first()
        if invoker:
            # token = create_access_token({'appid': appid, 'nick_name': invoker.nick_name})
            token = create_access_token(appid)
            msg = {
                'success': True,
                'data': {
                    'access_token': token
                },
                'message': '成功'
            }
            return msg

    except Exception as e:
        logger.exception(e)
    msg = {'success': False, 'data': {}, 'message': '非法请求'}
    return msg
Ejemplo n.º 3
0
def new_tasks(invoker: Invoker):
    '''

    :param invoker:
    :return:
    '''
    task_id = task_id_generator()
    create_time = current_datetime()
    creator = invoker.nick_name
    task = Tasks(
        # id=1,
        task_id=task_id,
        create_time=create_time,
        status=1,
        creator=creator
    )
    try:
        db.add(task)
        msg = {'success': True, 'data': {'task_id': task_id, 'create_time': create_time, 'creator': creator},
               'message': '成功'}

        db.commit()
        # logger.info('task_id:{}|creator:{}'.format(task_id, creator))
    except Exception as e:
        db.rollback()
        msg = {'success': False, 'data': {}, 'message': '系统错误'}
        logger.exception(e)
    return msg
Ejemplo n.º 4
0
 def sPop(setName):
     try:
         return RedisHelper().__pool.spop(setName)
     except Exception as e:
         errMsg = 'a error was occurred in sAddMember when insert key:{}.'.format(
             setName)
         logger.exception(errMsg)
Ejemplo n.º 5
0
 def set_expire(key, time):
     try:
         RedisHelper().__pool.expire(key, time)
     except Exception as e:
         errMsg = 'a error was occurred when set expire time on key:{}, time {}'.format(
             key, time)
         logger.exception(errMsg)
         return False
Ejemplo n.º 6
0
 def sMembers(setName):
     if setName is None or setName == '':
         errMsg = 'error, set key can\'t be empty.'
         logger.error(errMsg)
         return False
     try:
         return RedisHelper().__pool.smembers(setName)
     except Exception as e:
         errMsg = 'a error was occurred in sMembers when load ({}) members.'.format(
             setName, )
         logger.exception(errMsg)
         return None
Ejemplo n.º 7
0
 def sRemMember(setName, value):
     if setName is None or setName == '':
         errMsg = 'error, set key can\'t be empty.'
         logger.error(errMsg)
         return False
     try:
         RedisHelper().__pool.srem(setName, value)
     except Exception as e:
         errMsg = 'a error was occurred in sRemMember when remove key:{} value:{}.'.format(
             setName, value)
         logger.exception(errMsg)
         return False