Example #1
0
def get_traffic() -> jsonify:
    if not check_login(): return redirect('/login')
    data = {
        'message': "获取成功",
        "data": attacks_total.attacks,
        "type": "INFO",
        'code': CodeRequestOk
    }
    return jsonify(data)
Example #2
0
def remove_client() -> Resnponse:
    """
    将连接对象添加到发送对象列表中
    """
    if not check_login(): return redirect('/login')
    key = request.form.get('key')
    removed, msg = service.remove_client_from_sendlist(key)
    if removed: return Resnponse.success(msg)
    return Resnponse.error(msg, CodeRequestOkButNoChange)
def login():
    if check_login(): return redirect('/')
    if request.method == "POST":
        username = request.form.get('username')
        passowrd = request.form.get('password')
        admin = Administrator(username, passowrd)
        if admin.check():
            admin.login()
            return {"code": 1, 'message': '登录成功', 'data': {}}, 200
        return {"code": 0, 'message': admin.errors, 'data': {}}, 400
    return render_template('login.html')
def get_service_status() -> Resnponse:
    """
    获取当前服务器信息, 只接收GET请求
    """
    if not check_login(): return redirect('/login')
    return Resnponse.defined({
        'code': CodeRequestOk,
        'log': log.read(),
        'message': '初始化获取数据成功',
        'type': 'INFO'
    })
def set_options() -> Resnponse:
    """

    :return: Response
    """
    if not check_login(): return redirect('/login')
    host = str(request.form.get('host'))
    port = int(request.form.get('port'))
    err, msg = service.set_options(host, port)
    if err:
        return Resnponse.success(msg)
    else:
        return Resnponse.error(msg)
Example #6
0
def get_client_usage() -> Resnponse:
    if not check_login(): return redirect('/login')
    client_key = request.form.get('client_key')
    if client_key:
        if service.client_monitor is None:
            try:
                service.client[client_key].send(b'[::usage]')
            except KeyError:
                return Resnponse.error('没有这个连接对象')
            service.client_monitor = client_key
            return Resnponse.success('打开客户端 {} 监控成功'.format(client_key), type='SYSTEM')
        return Resnponse.error('已有在监控的客户端了', code=CodeRequestOkButNoChange)
    return Resnponse.error('打开客户端监控失败')
def wait_on_connect():
    """
    socketio连接成功后执行该函数
    """

    # 要先开启服务器才能接入socket
    if service.status and check_login():
        with thread_lock:
            if service.first_connect:
                # 判断是否已经开启过后台监听了, 不然多连会出事
                thread.threads.append(
                    socketio.start_background_task(target=push_cpu_usage))
                thread.threads.append(
                    socketio.start_background_task(target=wait_connect))
                service.first_connect = False
def stop_server() -> Resnponse:
    """
    关闭服务器, 只接收POST请求
    """
    if not check_login(): return redirect('/login')
    # 判断服务器是否有在运行
    if not service.status:
        return Resnponse.error("服务器没有在运行",
                               code=CodeRequestOkButNoChange,
                               status=StatusBadRequest)
    # 关闭服务器
    service.close()
    # 杀手后台线程
    thread.stop_thread()
    return Resnponse.success("关闭成功")
Example #9
0
def stop_client_usage() -> Resnponse:
    if not check_login(): return redirect('/login')
    client_key = request.form.get('client_key')
    if client_key and service.client_monitor is not None:
        try:
            service.client[client_key].send(b'[::nusage]')
        except KeyError:
            service.client_monitor = None
            return Resnponse.error('没有这个连接对象')
        except ConnectionResetError:
            socketio.emit('server_response', {'action': 'on_disconected', 'key': client_key}, namespace='/channel')
            service.remove_client(client_key)
        service.client_monitor = None
        return Resnponse.success('关闭客户端 {} 监控成功'.format(client_key), type='SYSTEM')
    return Resnponse.error('打开客户端监控失败')
def run_server() -> Resnponse:
    """
    开启服务器, 只接收POST请求
    """
    if not check_login(): return redirect('/login')
    # 判断服务器是否已经在运行
    if service.status:
        return Resnponse.error("服务器, 已经在运行",
                               code=CodeRequestOkButNoChange,
                               status=StatusBadRequest)
    try:
        service.run()
        service.status = True
    except OSError:
        return Resnponse.error(
            "端口 {} 已被占用, 通常每个套接字地址(协议/网络地址/端口)只允许使用一次".format(service.port))
    return Resnponse.success("服务器启动成功")
Example #11
0
def send_file() -> Resnponse:
    # 验证登录
    if not check_login(): return redirect('/login')

    # 保存文件
    try:
        f = request.files['files']
        f.save(os.path.join(UPLOAD_PATH, f.filename))
    except Exception:
        return Resnponse.error("文件呢????", CodeRequestNo, StatusBadRequest)

    # 获取发送列表
    host_arr = request.form.get("host_arr")
    if not host_arr and not service.client:
        return Resnponse.error("没有连接对象,或没有选择连接对象", CodeRequestNo, StatusBadRequest)

    threading.Thread(target=service.send_file, args=(host_arr, f.filename)).start()

    return Resnponse.success("操作成功, 正在发送, 请耐心等待")
def index():
    if not check_login(): return redirect('/login')
    return render_template('index.html')
Example #13
0
def disconnect_client():
    if not check_login(): return redirect('/login')
    client_key = request.form.get('key')
    suc, msg = service.disconnect(client_key)
    if suc: return Resnponse.success(msg)
    return Resnponse.error(msg)
Example #14
0
def send() -> Resnponse:
    if not check_login(): return redirect('/login')
    msg = request.form.get('msg')
    service.send(msg)
    log.write(msg + '\n', 'SYSTEM')
    return Resnponse.success(msg, type='SYSTEM')