def post(self): # ret = self.check_privilege(TP_PRIVILEGE_ASSET_CREATE | TP_PRIVILEGE_ASSET_DELETE | TP_PRIVILEGE_OPS | TP_PRIVILEGE_OPS_AUZ) # if ret != TPE_OK: # return args = self.get_argument('args', None) if args is None: return self.write_json(TPE_PARAM) try: args = json.loads(args) except: return self.write_json(TPE_JSON_FORMAT) # 有三种方式获取会话ID: # 1. 给定一个远程连接授权ID(普通用户进行远程连接) # 2. 给定要连接的主机ID和账号ID(管理员进行远程连接) # 3. 给定要连接的主机ID和账号信息(管理员测试远程连接是否可用) # # WEB服务根据上述信息产生临时的远程连接ID,核心服务通过此远程连接ID来获取远程连接所需数据,生成会话ID。 try: _mode = int(args['mode']) _protocol_type = int(args['protocol_type']) _protocol_sub_type = int(args['protocol_sub_type']) except: return self.write_json(TPE_PARAM) conn_info = dict() conn_info['_enc'] = 1 conn_info['host_id'] = 0 conn_info['client_ip'] = self.request.remote_ip conn_info['user_id'] = self.get_current_user()['id'] conn_info['user_username'] = self.get_current_user()['username'] # mode = 0: test connect # mode = 1: user connect # mode = 2: admin connect if _mode == 1: # 通过指定的auth_id连接(需要授权),必须具有远程运维的权限方可进行 ret = self.check_privilege(TP_PRIVILEGE_OPS) if ret != TPE_OK: return if 'auth_id' not in args or 'protocol_sub_type' not in args: return self.write_json(TPE_PARAM) # 根据auth_id从数据库中取得此授权相关的用户、主机、账号三者详细信息 auth_id = args['auth_id'] ops_auth, err = ops.get_auth(auth_id) if err != TPE_OK: return self.write_json(err) policy_id = ops_auth['p_id'] acc_id = ops_auth['a_id'] host_id = ops_auth['h_id'] err, policy_info = ops.get_by_id(policy_id) if err != TPE_OK: return self.write_json(err) err, acc_info = account.get_account_info(acc_id) if err != TPE_OK: return self.write_json(err) # log.v(acc_info) if acc_info['protocol_type'] == TP_PROTOCOL_TYPE_RDP: acc_info['protocol_flag'] = policy_info['flag_rdp'] elif acc_info['protocol_type'] == TP_PROTOCOL_TYPE_SSH: acc_info['protocol_flag'] = policy_info['flag_ssh'] elif acc_info['protocol_type'] == TP_PROTOCOL_TYPE_TELNET: acc_info['protocol_flag'] = policy_info['flag_telnet'] else: acc_info['protocol_flag'] = 0 acc_info['record_flag'] = policy_info['flag_record'] elif _mode == 2: # 直接连接(无需授权),必须具有运维授权管理的权限方可进行 ret = self.check_privilege(TP_PRIVILEGE_OPS_AUZ) if ret != TPE_OK: return acc_id = args['acc_id'] host_id = args['host_id'] err, acc_info = account.get_account_info(acc_id) if err != TPE_OK: return self.write_json(err) acc_info['protocol_flag'] = TP_FLAG_ALL acc_info['record_flag'] = TP_FLAG_ALL elif _mode == 0: # 测试连接,必须具有主机信息创建、编辑的权限方可进行 ret = self.check_privilege(TP_PRIVILEGE_ASSET_CREATE) if ret != TPE_OK: return conn_info['_test'] = 1 try: acc_id = int(args['acc_id']) host_id = int(args['host_id']) auth_type = int(args['auth_type']) username = args['username'] password = args['password'] pri_key = args['pri_key'] protocol_port = int(args['protocol_port']) username_prompt = args['username_prompt'] password_prompt = args['password_prompt'] except: return self.write_json(TPE_PARAM) if len(username) == 0: return self.write_json(TPE_PARAM) acc_info = dict() acc_info['auth_type'] = auth_type acc_info['protocol_type'] = _protocol_type acc_info['protocol_port'] = protocol_port acc_info['protocol_flag'] = TP_FLAG_ALL acc_info['record_flag'] = TP_FLAG_ALL acc_info['username'] = username acc_info['password'] = password acc_info['pri_key'] = pri_key acc_info['username_prompt'] = username_prompt acc_info['password_prompt'] = password_prompt conn_info['_enc'] = 0 if acc_id == -1: if auth_type == TP_AUTH_TYPE_PASSWORD and len(password) == 0: return self.write_json(TPE_PARAM) elif auth_type == TP_AUTH_TYPE_PRIVATE_KEY and len( pri_key) == 0: return self.write_json(TPE_PARAM) else: if (auth_type == TP_AUTH_TYPE_PASSWORD and len(password) == 0) or (auth_type == TP_AUTH_TYPE_PRIVATE_KEY and len(pri_key) == 0): err, _acc_info = account.get_account_info(acc_id) if err != TPE_OK: return self.write_json(err) acc_info['password'] = _acc_info['password'] acc_info['pri_key'] = _acc_info['pri_key'] conn_info['_enc'] = 1 else: return self.write_json(TPE_PARAM) # 获取要远程连接的主机信息(要访问的IP地址,如果是路由模式,则是路由主机的IP+端口) err, host_info = host.get_host_info(host_id) if err != TPE_OK: return self.write_json(err) conn_info['host_id'] = host_id conn_info['host_ip'] = host_info['ip'] if len(host_info['router_ip']) > 0: conn_info['conn_ip'] = host_info['router_ip'] conn_info['conn_port'] = host_info['router_port'] else: conn_info['conn_ip'] = host_info['ip'] conn_info['conn_port'] = acc_info['protocol_port'] conn_info['acc_id'] = acc_id conn_info['acc_username'] = acc_info['username'] conn_info['username_prompt'] = acc_info['username_prompt'] conn_info['password_prompt'] = acc_info['password_prompt'] conn_info['protocol_flag'] = acc_info['protocol_flag'] conn_info['record_flag'] = acc_info['record_flag'] conn_info['protocol_type'] = acc_info['protocol_type'] conn_info['protocol_sub_type'] = _protocol_sub_type conn_info['auth_type'] = acc_info['auth_type'] if acc_info['auth_type'] == TP_AUTH_TYPE_PASSWORD: conn_info['acc_secret'] = acc_info['password'] elif acc_info['auth_type'] == TP_AUTH_TYPE_PRIVATE_KEY: conn_info['acc_secret'] = acc_info['pri_key'] else: conn_info['acc_secret'] = '' with tmp_conn_id_lock: global tmp_conn_id_base tmp_conn_id_base += 1 conn_id = tmp_conn_id_base # log.v('CONN-INFO:', conn_info) tp_session().set('tmp-conn-info-{}'.format(conn_id), conn_info, 10) req = {'method': 'request_session', 'param': {'conn_id': conn_id}} _yr = core_service_async_post_http(req) _code, ret_data = yield _yr if _code != TPE_OK: return self.write_json(_code) if ret_data is None: return self.write_json(TPE_FAILED, '调用核心服务获取会话ID失败') if 'sid' not in ret_data: return self.write_json(TPE_FAILED, '核心服务获取会话ID时返回错误数据') data = dict() data['session_id'] = ret_data['sid'] data['host_ip'] = host_info['ip'] data['protocol_flag'] = acc_info['protocol_flag'] if conn_info['protocol_type'] == TP_PROTOCOL_TYPE_RDP: data['teleport_port'] = tp_cfg().core.rdp.port elif conn_info['protocol_type'] == TP_PROTOCOL_TYPE_SSH: data['teleport_port'] = tp_cfg().core.ssh.port elif conn_info['protocol_type'] == TP_PROTOCOL_TYPE_TELNET: data['teleport_port'] = tp_cfg().core.telnet.port return self.write_json(0, data=data)
def post(self): # ret = self.check_privilege(TP_PRIVILEGE_ASSET_CREATE | TP_PRIVILEGE_ASSET_DELETE | TP_PRIVILEGE_OPS | TP_PRIVILEGE_OPS_AUZ) # if ret != TPE_OK: # return args = self.get_argument('args', None) if args is None: return self.write_json(TPE_PARAM) try: args = json.loads(args) except: return self.write_json(TPE_JSON_FORMAT) # 有三种方式获取会话ID: # 1. 给定一个远程连接授权ID(普通用户进行远程连接) # 2. 给定要连接的主机ID和账号ID(管理员进行远程连接) # 3. 给定要连接的主机ID和账号信息(管理员测试远程连接是否可用) # # WEB服务根据上述信息产生临时的远程连接ID,核心服务通过此远程连接ID来获取远程连接所需数据,生成会话ID。 try: _mode = int(args['mode']) _protocol_type = int(args['protocol_type']) _protocol_sub_type = int(args['protocol_sub_type']) except: return self.write_json(TPE_PARAM) conn_info = dict() conn_info['_enc'] = 1 conn_info['host_id'] = 0 conn_info['client_ip'] = self.request.remote_ip conn_info['user_id'] = self.get_current_user()['id'] conn_info['user_username'] = self.get_current_user()['username'] # mode = 0: test connect # mode = 1: user connect # mode = 2: admin connect if _mode == 1: # 通过指定的auth_id连接(需要授权),必须具有远程运维的权限方可进行 ret = self.check_privilege(TP_PRIVILEGE_OPS) if ret != TPE_OK: return if 'auth_id' not in args or 'protocol_sub_type' not in args: return self.write_json(TPE_PARAM) # 根据auth_id从数据库中取得此授权相关的用户、主机、账号三者详细信息 auth_id = args['auth_id'] ops_auth, err = ops.get_auth(auth_id) if err != TPE_OK: return self.write_json(err) policy_id = ops_auth['p_id'] acc_id = ops_auth['a_id'] host_id = ops_auth['h_id'] err, policy_info = ops.get_by_id(policy_id) if err != TPE_OK: return self.write_json(err) err, acc_info = account.get_account_info(acc_id) if err != TPE_OK: return self.write_json(err) # log.v(acc_info) if acc_info['protocol_type'] == TP_PROTOCOL_TYPE_RDP: acc_info['protocol_flag'] = policy_info['flag_rdp'] elif acc_info['protocol_type'] == TP_PROTOCOL_TYPE_SSH: acc_info['protocol_flag'] = policy_info['flag_ssh'] elif acc_info['protocol_type'] == TP_PROTOCOL_TYPE_TELNET: acc_info['protocol_flag'] = policy_info['flag_telnet'] else: acc_info['protocol_flag'] = 0 acc_info['record_flag'] = policy_info['flag_record'] elif _mode == 2: # 直接连接(无需授权),必须具有运维授权管理的权限方可进行 ret = self.check_privilege(TP_PRIVILEGE_OPS_AUZ) if ret != TPE_OK: return acc_id = args['acc_id'] host_id = args['host_id'] err, acc_info = account.get_account_info(acc_id) if err != TPE_OK: return self.write_json(err) acc_info['protocol_flag'] = TP_FLAG_ALL acc_info['record_flag'] = TP_FLAG_ALL elif _mode == 0: # 测试连接,必须具有主机信息创建、编辑的权限方可进行 ret = self.check_privilege(TP_PRIVILEGE_ASSET_CREATE) if ret != TPE_OK: return conn_info['_test'] = 1 try: acc_id = int(args['acc_id']) host_id = int(args['host_id']) auth_type = int(args['auth_type']) username = args['username'] password = args['password'] pri_key = args['pri_key'] protocol_port = int(args['protocol_port']) username_prompt = args['username_prompt'] password_prompt = args['password_prompt'] except: return self.write_json(TPE_PARAM) if len(username) == 0: return self.write_json(TPE_PARAM) acc_info = dict() acc_info['auth_type'] = auth_type acc_info['protocol_type'] = _protocol_type acc_info['protocol_port'] = protocol_port acc_info['protocol_flag'] = TP_FLAG_ALL acc_info['record_flag'] = TP_FLAG_ALL acc_info['username'] = username acc_info['password'] = password acc_info['pri_key'] = pri_key acc_info['username_prompt'] = username_prompt acc_info['password_prompt'] = password_prompt conn_info['_enc'] = 0 if acc_id == -1: if auth_type == TP_AUTH_TYPE_PASSWORD and len(password) == 0: return self.write_json(TPE_PARAM) elif auth_type == TP_AUTH_TYPE_PRIVATE_KEY and len(pri_key) == 0: return self.write_json(TPE_PARAM) else: if (auth_type == TP_AUTH_TYPE_PASSWORD and len(password) == 0) or (auth_type == TP_AUTH_TYPE_PRIVATE_KEY and len(pri_key) == 0): err, _acc_info = account.get_account_info(acc_id) if err != TPE_OK: return self.write_json(err) acc_info['password'] = _acc_info['password'] acc_info['pri_key'] = _acc_info['pri_key'] conn_info['_enc'] = 1 else: return self.write_json(TPE_PARAM) # 获取要远程连接的主机信息(要访问的IP地址,如果是路由模式,则是路由主机的IP+端口) err, host_info = host.get_host_info(host_id) if err != TPE_OK: return self.write_json(err) conn_info['host_id'] = host_id conn_info['host_ip'] = host_info['ip'] if len(host_info['router_ip']) > 0: conn_info['conn_ip'] = host_info['router_ip'] conn_info['conn_port'] = host_info['router_port'] else: conn_info['conn_ip'] = host_info['ip'] conn_info['conn_port'] = acc_info['protocol_port'] conn_info['acc_id'] = acc_id conn_info['acc_username'] = acc_info['username'] conn_info['username_prompt'] = acc_info['username_prompt'] conn_info['password_prompt'] = acc_info['password_prompt'] conn_info['protocol_flag'] = acc_info['protocol_flag'] conn_info['record_flag'] = acc_info['record_flag'] conn_info['protocol_type'] = acc_info['protocol_type'] conn_info['protocol_sub_type'] = _protocol_sub_type conn_info['auth_type'] = acc_info['auth_type'] if acc_info['auth_type'] == TP_AUTH_TYPE_PASSWORD: conn_info['acc_secret'] = acc_info['password'] elif acc_info['auth_type'] == TP_AUTH_TYPE_PRIVATE_KEY: conn_info['acc_secret'] = acc_info['pri_key'] else: conn_info['acc_secret'] = '' with tmp_conn_id_lock: global tmp_conn_id_base tmp_conn_id_base += 1 conn_id = tmp_conn_id_base # log.v('CONN-INFO:', conn_info) tp_session().set('tmp-conn-info-{}'.format(conn_id), conn_info, 10) req = {'method': 'request_session', 'param': {'conn_id': conn_id}} _yr = core_service_async_post_http(req) _code, ret_data = yield _yr if _code != TPE_OK: return self.write_json(_code) if ret_data is None: return self.write_json(TPE_FAILED, '调用核心服务获取会话ID失败') if 'sid' not in ret_data: return self.write_json(TPE_FAILED, '核心服务获取会话ID时返回错误数据') data = dict() data['session_id'] = ret_data['sid'] data['host_ip'] = host_info['ip'] data['protocol_flag'] = acc_info['protocol_flag'] if conn_info['protocol_type'] == TP_PROTOCOL_TYPE_RDP: data['teleport_port'] = tp_cfg().core.rdp.port elif conn_info['protocol_type'] == TP_PROTOCOL_TYPE_SSH: data['teleport_port'] = tp_cfg().core.ssh.port elif conn_info['protocol_type'] == TP_PROTOCOL_TYPE_TELNET: data['teleport_port'] = tp_cfg().core.telnet.port return self.write_json(0, data=data)