async def get_agent_url_by_token(req): access_token = get_access_token_from_req(req) user_info = await get_user_info_by_access_token(access_token) if not user_info: err_msg = f'Access Token Invalid {access_token}' logger.info(err_msg) return get_err_response({}, msg=err_msg, code=404) user_id = user_info['id'] bid = req.raw_args.get('bid', None) browser_name = req.raw_args.get('browser_name', None) agent = await get_agent_for_user(user_id, bid) if agent is None: if browser_name is not None: err_msg = (f'No Named Browser user:{user_info["id"]} ' f'browser_name: {browser_name}') logger.info(err_msg) return get_err_response({}, msg=err_msg, code=404) else: logger.info(f'No Alive Agent user:{user_info["id"]}') busy_event = BusyEventModel(user_id=user_id, agent_id=0) await ASSV.mysql_conn.add_busy_event(busy_event) return get_err_response({}, msg='No avaliable agent', code=404) advertise_address = agent['advertise_address'].split('//')[1] host, port = advertise_address.split(':') return get_ok_response({'host': host, 'port': int(port)})
async def get_agent_info(req): id = req.raw_args.get('id') name = req.raw_args.get('name') ret = await ASSV.mysql_conn.get_agent_info(id, name) if ret.error is None: return get_ok_response(ret.value) else: return get_err_response(None, msg='not found')
async def get_named_browser(req): browser_name = req.raw_args.get('browser_name') user_id = req.raw_args.get('user_id') ret = await ASV.mysql_conn.get_named_browser( user_id, browser_name) if ret.value: return get_ok_response(ret.value) else: return get_err_response(ret.value, msg='not found')
async def upsert_limit(req): id_ = req.json.get('id') owner_id = req.json.get('owner_id') type_ = req.json.get('type') max_limit = req.json.get('max_limit') min_limit = req.json.get('min_limit') request = req.json.get('request') current = req.json.get('current') if id_ is None: limit_model = LimitRulesModel(owner_id=owner_id, type=type_, max_limit=max_limit, min_limit=min_limit, request=request, current=current) ret = await ASSV.mysql_conn.add_rule(limit_model) if ret.lastrowid is None: return get_err_response(None, msg='add limit failed') else: return get_ok_response(True) else: limit_model = LimitRulesModel(id=id_, owner_id=owner_id, type=type_, max_limit=max_limit, min_limit=min_limit, request=request, current=current) ret = await ASSV.mysql_conn.modify_limit(limit_model) if ret.lastrowid is None: return get_err_response(None, msg='modify limit failed') else: return get_ok_response(True)
async def add_user(req): name = req.json.get('name') password = req.json.get('password') if not (password and name): return get_err_response(None, msg='missing name or password') steady_agent_count = req.json.get('steady_agent_count', 1) dynamic_agent_count = req.json.get('dynamic_agent_count', 1) user = UserModel(name=name, password=b64encode( sha256((PASSWORD_SECRET_TOKEN + password).encode('utf-8')).digest()), access_token=token_urlsafe(32)) ret = await ASSV.mysql_conn.add_user(user) if ret.error is not None: return get_err_response(ret.value, msg=ret.msg) if not ret.lastrowid: return get_err_response(None, msg='add user failed') new_user_id = ret.lastrowid limit_rule = LimitRulesModel(owner_id=new_user_id, type=LimitRulesTypeEnum.STEADY_AGENT_ON_USER, max_limit=steady_agent_count, min_limit=0, request=steady_agent_count, current=0) ret = await ASSV.mysql_conn.add_rule(limit_rule) if ret.error is not None: return get_err_response(None, msg='add user failed') limit_rule = LimitRulesModel(owner_id=new_user_id, type=LimitRulesTypeEnum.DYNAMIC_AGENT_ON_USER, max_limit=dynamic_agent_count, min_limit=0, request=0, current=0) ret = await ASSV.mysql_conn.add_rule(limit_rule) if ret.error is not None: return get_err_response(None, msg='add user failed') if ret.error is None: return get_ok_response(True) else: return get_err_response(ret.value, msg=ret.msg)
async def agent_heartbeat(req): agent_id = req.raw_args.get('agent_id') last_ack_time = req.raw_args.get('last_ack_time') busy_level = req.raw_args.get('busy_level') new_ack_time = int(time.time() * 1000) if await check_agent_id_auth_by_req(req, agent_id) is False: return get_err_response(None, msg='invalid user') ret_data = {'new_ack_time': 0} if agent_id is not None: ret = await ASSV.mysql_conn.update_agent_last_ack_time( agent_id, last_ack_time, new_ack_time, busy_level) if ret.error is None and ret.affected_rows == 1: ret_data['new_ack_time'] = new_ack_time return get_ok_response(ret_data)
async def add_busy_event(req): user_id = req.json.get('user_id') agent_id = req.json.get('agent_id') if not (user_id and agent_id): return get_err_response(None, msg='missing user_id or agent_id') if await check_agent_id_auth_by_req(req, agent_id) is False: return get_err_response(None, msg='invalid user') busy_event = BusyEventModel(user_id=user_id, agent_id=agent_id) ret = await ASSV.mysql_conn.add_busy_event(busy_event) if ret.error is None: return get_ok_response(True) else: return get_err_response(ret.value, msg=ret.msg)
async def remove_agent(req): agent_id = req.json.get('agent_id') user_id = req.json.get('user_id') is_steady = req.json.get('is_steady') if any((agent_id is None, user_id is None, is_steady is None)): return get_err_response(None, msg='missing param') if not await check_user_id_auth_by_req(req, user_id): return get_err_response(None, msg='invalid user') ret = await ASSV.mysql_conn.remove_agent(agent_id, user_id, is_steady=is_steady) if ret.error is None: return get_ok_response(True) else: return get_err_response(False, msg='remove agent failed')
async def delete_browser_agent_map(req): user_id = req.raw_args.get('user_id') bid = req.raw_args.get('bid') agent_id = req.raw_args.get('agent_id') if agent_id is None: auth_chk_ret = await check_user_id_auth_by_req(req, user_id) else: auth_chk_ret = await check_agent_id_auth_by_req(req, agent_id) if auth_chk_ret is False: return get_err_response(None, msg='invalid user') ret = await ASSV.mysql_conn.delete_browser_agent_map( user_id, bid, agent_id) if ret.value: return get_ok_response(True) else: return get_err_response(None, msg='not found')
async def add_browser_agent_map(req): agent_id = req.json.get('agent_id') user_id = req.json.get('user_id') bid = req.json.get('bid') if await check_agent_id_auth_by_req(req, agent_id) is False: return get_err_response(None, msg='invalid user') bam = BrowserAgentMapModel(user_id=user_id, bid=bid, agent_id=agent_id, create_time=int(time.time() * 1000)) ret = await ASSV.mysql_conn.add_browser_agent_map(bam) if ret.value: return get_ok_response(ret.value) else: return get_err_response(ret.value, msg='dup')
async def add_named_browser(req): browser_name = req.raw_args.get('browser_name') user_id = req.raw_args.get('user_id') if not browser_name: return get_err_response(None, msg='No browser_name') ret = await(ASV.mysql_conn. get_most_free_agent_for_named_browser(user_id)) if ret.value is None: return get_err_response(None, msg='No agent for user') selected_agent_id = ret.value['id'] ret = await ASV.mysql_conn.add_named_browser( user_id, selected_agent_id, browser_name) if not ret.error: return get_ok_response(None) else: return get_err_response(None, msg=ret.msg)
async def bind_to_free_slot(req): advertise_address = req.json.get('advertise_address') is_steady = req.json.get('is_steady') bind_token = req.json.get('bind_token') if (advertise_address is None or is_steady is None or bind_token is None): logger.error('bind_to_free_slot missing param') return get_err_response(None, msg='param error') if check_bind_token(bind_token) is False: logger.error(f'bind_to_free_slot check_bind_token error') return get_err_response(None, msg='param error') bind_ret = await ASSV.mysql_conn.bind_to_free_slot(advertise_address, is_steady) if bind_ret.value is None: return get_err_response(bind_ret.value, msg='not found') return get_ok_response(bind_ret.value)