def logout(): try: resp = Response(json.dumps({'content': '登出成功'}), mimetype='application/json', status=200) if flask_utils.is_local_server: # Conf.set('USER', 'account', '') # Conf.set('USER', 'password', '') # Conf.set('USER', 'dynamicPass', '') # Conf.set('USER', 'remember_pass', '') # Conf.set('USER', 'auto_login', 'false') Conf.set('USER', 'is_login', 'false') # session.pop('account', None) else: session.pop('account', None) session.pop('password', None) session.pop('dynamicPass', None) session.pop('remember_pass', None) session.pop('auto_login', None) resp.set_cookie('account', '') resp.headers.extend(flask_utils.Cross_Origin_Headers(request)) return resp except HTTPException as e: raise e except Exception as e: current_app.logger.error(traceback.format_exc()) abort(500)
def get_or_post(api_route): try: path = api_route.replace('.', '/') if path == 'api/login': raise Exception('此方法不能用于登录') query_string = request.query_string.decode() if flask_utils.is_local_server: query_string = 'account={0}&password={1}&{2}'.format( Conf.get('USER', 'account'), Conf.get('USER', 'password'), query_string) else: query_string = 'account={0}&password={1}&{2}'.format( session['account'], session['password'], query_string) if request.method == 'GET': response = requests.get('{0}{1}?{2}'.format( flask_utils.vm_server(), path, query_string), verify=False) elif request.method == 'POST': response = requests.post('{0}{1}?{2}'.format( flask_utils.vm_server(), path, query_string), data=request.form.to_dict(), verify=False) else: raise Exception('目前只支持get和post两种方法') if response.status_code == 200: if path == 'api/EditPassword' and response.json( )['errorinfo'] == 'success': if flask_utils.is_local_server: Conf.set('USER', 'password', request.args['newPassword']) else: if flask_utils.is_local_server: Conf.set('USER', 'password', request.args['newPassword']) else: session['password'] = request.args['newPassword'] return json.loads(response.content.decode()) else: raise Exception(str(response.status_code)) except HTTPException as e: raise e except Exception as e: current_app.logger.error(traceback.format_exc()) return {'content': e.message}, 500
def set_vm_server(): ''' 设置服务器 :return: ''' try: data = request.args if flask_utils.is_local_server: Conf.set('SERVER', 'ip', data['vm_server_host']) Conf.set('SERVER', 'port', data['vm_server_port']) else: session['vm_server_host'] = data['vm_server_host'] session['vm_server_port'] = data['vm_server_port'] return '设置默认虚机服务器成功' except HTTPException as e: raise e except Exception as e: current_app.logger.error(traceback.format_exc()) abort(500)
def set_default_vm(): ''' 登录前获取记住的账号信息以便自动填写 :return: ''' try: data = request.args if flask_utils.is_local_server: Conf.set('VMS', 'default_vm', data['default_vm']) resp = Response('设置默认虚机成功', status=200) else: session['default_vm'] = data['default_vm'] resp = Response('设置默认虚机成功', status=200) resp.set_cookie('default_vm', data['default_vm'], 3600 * 24 * 30) resp.set_cookie('default_vm_name', data['default_vm_name'], 3600 * 24 * 30) resp.headers.extend(flask_utils.Cross_Origin_Headers(request)) return resp except HTTPException as e: raise e except Exception as e: current_app.logger.error(traceback.format_exc()) abort(500, '设置默认虚机失败')
from datetime import timedelta from app import windows from router import blueprints from flask import Flask, render_template, session, request, Response, abort, jsonify from Utils import log_utils from Utils import flask_utils from Utils.conf import Conf import logging from logging.handlers import RotatingFileHandler import multiprocessing # 初始化config.ini的某些配置 Conf.set('USER', 'is_login', 'false') app = Flask(__name__, static_folder='Front/dist/static', template_folder='Front/dist') app.secret_key = '123456' app.config.update( DEBUG=True if Conf.get('BACKEND_SERVER', 'mode') == 'debug' else False, # PERMANENT_SESSION_LIFETIME=timedelta(days=30), SESSION_PERMANENT=False, SESSION_COOKIE_PATH='/', # SESSION_COOKIE_NAME='ccw_test' ) # 解决jinja和vue的冲突 app.jinja_env.variable_start_string = '#{ '
def login(): try: path = 'api/login' query_string = request.query_string.decode() response = requests.get('{0}/{1}?{2}'.format(flask_utils.vm_server(), path, query_string), verify=False) if not response.status_code == 200: raise Exception(str(response.status_code) + ':' + response.json()['errorinfo']) if response.json()['errorinfo'] == 'success': resp = Response(json.dumps({'content': response.json()['errorinfo']}), mimetype='application/json', status=200) if flask_utils.is_local_server: Conf.set('USER', 'account', request.args.to_dict()['account']) Conf.set('USER', 'password', request.args.to_dict()['password']) Conf.set('USER', 'dynamicPass', request.args.to_dict()['dynamicPass']) Conf.set('USER', 'remember_pass', request.args.to_dict()['remember_pass']) Conf.set('USER', 'auto_login', request.args.to_dict()['auto_login']) Conf.set('USER', 'is_login', 'true') # session.permanent=False # session['account'] = request.args.to_dict()['account'] else: session['account'] = request.args.to_dict()['account'] session['password'] = request.args.to_dict()['password'] session['dynamicPass'] = request.args.to_dict()['dynamicPass'] session['remember_pass'] = request.args.to_dict()['remember_pass'] session['auto_login'] = request.args.to_dict()['auto_login'] if request.args.to_dict()['remember_pass'] == 'false': resp.set_cookie('account', request.args.to_dict()['account']) if request.args.to_dict()['remember_pass'] == 'true': resp.set_cookie('account', request.args.to_dict()['account'], 3600 * 24 * 30) # 如果记住密码,则保存一个月 resp.headers.extend(flask_utils.Cross_Origin_Headers(request)) return resp else: abort(400,response.json()['errorinfo']) except HTTPException as e: raise e except Exception as e: current_app.logger.error(traceback.format_exc()) abort(500)