示例#1
0
def ErinaServer_Endpoint_Erina_Logs_WebSocketLogs(ws):
    message = ws.receive()
    try:
        data = json.loads(message)  # retrieve a message from the client
        if "token" in data:
            tokenVerification = authManagement.verifyToken(data)
            if tokenVerification.success:
                currentConnections.append(ws)
                ws.send(
                    json.dumps({
                        "api": "ErinaAdmin",
                        "message": "Successfully connected to ErinaLogs",
                        "error": False,
                        "timestamp": int(time())
                    }))
            else:
                ws.send(
                    json.dumps({
                        "api": "ErinaAdmin",
                        "message":
                        f"ErinaLogs: Authentification Error: {str(tokenVerification)}",
                        "error": True,
                        "timestamp": int(time())
                    }))
        else:
            ws.send(
                json.dumps({
                    "api": "ErinaAdmin",
                    "message":
                    "ErinaLogs: Your connection could not be authentificated",
                    "error": True,
                    "timestamp": int(time())
                }))
    except:
        try:
            ws.send(
                json.dumps({
                    "api": "ErinaAdmin",
                    "message":
                    "ErinaLogs: Your connection could not be authentificated",
                    "error": True,
                    "timestamp": int(time())
                }))
        except:
            log("ErinaAdmin", "ErinaConsole >>> Failed to send a message")

    try:
        while not ws.closed:
            try:
                ws.receive()
            except:
                currentConnections.remove(ws)
    except:
        currentConnections.remove(ws)

    if ws in currentConnections:
        currentConnections.remove(ws)
示例#2
0
def ErinaServer_Endpoint_Admin_Admin_resourceEndpoint(page):
    try:
        if page in ["overview", "api", "stats", "config"]:
            tokenVerification = authManagement.verifyToken(request.values)
            if tokenVerification.success:
                return send_from_directory(htmlLocation, page + ".html")
            else:
                return "ErinaAdminLoginRedirect"
        else:
            return send_from_directory(htmlLocation, "404.html"), 404
    except:
        return send_from_directory(htmlLocation, "500.html"), 500
示例#3
0
def ErinaServer_Endpoint_Auth_verify():
    try:
        if TextFile(erina_dir +
                    "/ErinaServer/Erina/auth/password.erina").read().replace(
                        " ", "") == "":
            return makeResponse(
                {
                    "success": False,
                    "error": "NOT_SET_PASSWORD",
                    "message": "Password Is Not Set"
                }, 400, request.args)
        tokenVerification = authManagement.verifyToken(request.values)
        if not tokenVerification.success:
            responseBody = None
            if tokenVerification.expired:
                responseBody = {
                    "success": False,
                    "error": "EXPIRED_TOKEN",
                    "message": str(tokenVerification)
                }
            elif tokenVerification.no_token:
                responseBody = {
                    "success": False,
                    "error": "NOT_PROVIDED_TOKEN",
                    "message": str(tokenVerification)
                }
            else:
                responseBody = {
                    "success": False,
                    "error": "WRONG_TOKEN",
                    "message": str(tokenVerification)
                }
            return makeResponse(responseBody, 401, request.values)
        else:
            return makeResponse({"success": True}, 200, request.values)
    except:
        return makeResponse({
            "success": False,
            "error": str(exc_info()[0])
        }, 500, request.values)
示例#4
0
def ErinaServer_Endpoint_Auth_logout():
    try:
        tokenVerification = authManagement.verifyToken(request.values)
        if not tokenVerification.success:
            responseBody = None
            if tokenVerification.expired:
                responseBody = {
                    "success": False,
                    "error": "EXPIRED_TOKEN",
                    "message": str(tokenVerification),
                    "data": None
                }
            elif tokenVerification.no_token:
                responseBody = {
                    "success": False,
                    "error": "NOT_PROVIDED_TOKEN",
                    "message": str(tokenVerification),
                    "data": None
                }
            else:
                responseBody = {
                    "success": False,
                    "error": "WRONG_TOKEN",
                    "message": str(tokenVerification),
                    "data": None
                }
            return makeResponse(responseBody, 401, request.values)
        else:
            authManagement.logout()
            return makeResponse({
                "success": True,
                "data": None
            }, 200, request.values)
    except:
        return makeResponse({
            "success": False,
            "error": str(exc_info()[0])
        }, 500, request.values)
示例#5
0
def ErinaServer_Endpoint_Admin_Console_ErinaConsole(ws):
    currentProcess = None
    message = ws.receive()
    try:
        data = json.loads(message)  # retrieve a message from the client
        if "token" in data:
            tokenVerification = authManagement.verifyToken(data)
            if tokenVerification.success:
                log("ErinaAdmin", "> New ErinaConsole connection!")
                bash = False
                if os.path.isfile("/bin/bash"):
                    bash = True
                    currentProcess = subprocess.Popen(
                        shlex.split("/bin/bash"),
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT)  # Open a bash prompt
                else:
                    currentProcess = subprocess.Popen(
                        shlex.split("/bin/sh"),
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.STDOUT)  # Open a shell prompt
                fcntl.fcntl(
                    currentProcess.stdout.fileno(), fcntl.F_SETFL,
                    os.O_NONBLOCK)  # Non blocking stdout and stderr reading
                threading.Thread(
                    target=_checkOutput,
                    args=[currentProcess, ws],
                    daemon=True).start(
                    )  # Start checking for new text in stdout and stderr
                if bash:
                    ws.send(
                        json.dumps({
                            "message":
                            f"ErinaConsole: Connection successfully established (bash) with PID: {str(currentProcess.pid)}",
                            "code": 0
                        })
                    )  # Send a message to notifiy that the process has started
                else:
                    ws.send(
                        json.dumps({
                            "message":
                            f"ErinaConsole: Connection successfully established (sh) with PID: {str(currentProcess.pid)}",
                            "code": 0
                        })
                    )  # Send a message to notifiy that the process has started
            else:
                ws.send(
                    json.dumps({
                        "message":
                        f"ErinaConsole: Authentification Error: {str(tokenVerification)}",
                        "code": 400
                    }))
        else:
            ws.send(
                json.dumps({
                    "message":
                    f"ErinaConsole: Your connection could not be authentificated",
                    "code": 400
                }))
    except:
        try:
            ws.send(
                json.dumps({
                    "message":
                    f"ErinaConsole: An error occured ({str(sys.exc_info()[0])}) while creating a new ErinaConsole instance",
                    "code": -1
                }))
        except:
            log("ErinaAdmin", "ErinaConsole >>> Failed to send a message")
    try:
        while not ws.closed:
            try:
                message = ws.receive()
                try:
                    data = json.loads(
                        message)  # retrieve a message from the client
                    tokenVerification = authManagement.verifyToken(data)
                    if tokenVerification.success:
                        if "input" in data:
                            userInput = str(data["input"])
                            #if userInput != "exit":
                            currentProcess.stdin.write(
                                str(userInput + "\n").encode("utf-8")
                            )  # Write user input (ws client) to stdin
                            currentProcess.stdin.flush()  # Run the command
                            log(
                                "ErinaAdmin",
                                "ErinaConsole >> Admin ran command > " +
                                str(userInput))
                            """
                            else:
                                currentProcess.terminate() # If "exit" sent by client, terminate the process
                            """
                    else:
                        ws.send(
                            json.dumps({
                                "message":
                                f"ErinaConsole: Authentification Error: {str(tokenVerification)}",
                                "code": 400
                            }))
                except:
                    try:
                        ws.send(
                            json.dumps({
                                "message":
                                f"ErinaConsole: An error occured ({str(sys.exc_info()[0])})",
                                "code": -1
                            }))
                    except:
                        log("ErinaAdmin",
                            "ErinaConsole >>> Failed to send a message")
            except:
                log("ErinaAdmin", "< ErinaConsole disconnected")
    except:
        log("ErinaAdmin", "< ErinaConsole disconnected")
    if currentProcess is not None:
        if currentProcess.poll() is None:
            currentProcess.terminate()
    log("ErinaAdmin", "< ErinaConsole disconnected")