2025-06-02 12:04:01 +08:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
from flask import Blueprint, render_template, abort, request, Flask, current_app, session
|
2025-05-29 16:23:20 +08:00
|
|
|
import os
|
2025-06-02 12:04:01 +08:00
|
|
|
|
|
|
|
from modules.WireguardConfiguration import WireguardConfiguration
|
|
|
|
from modules.DashboardConfig import DashboardConfig
|
2025-05-29 16:23:20 +08:00
|
|
|
|
2025-05-31 22:59:46 +08:00
|
|
|
def ResponseObject(status=True, message=None, data=None, status_code = 200) -> Flask.response_class:
|
|
|
|
response = Flask.make_response(current_app, {
|
|
|
|
"status": status,
|
|
|
|
"message": message,
|
|
|
|
"data": data
|
|
|
|
})
|
|
|
|
response.status_code = status_code
|
|
|
|
response.content_type = "application/json"
|
|
|
|
return response
|
|
|
|
|
2025-06-02 12:04:01 +08:00
|
|
|
def login_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def func(*args, **kwargs):
|
|
|
|
if session.get("username") is None or session.get("role") != "client":
|
|
|
|
return ResponseObject(False, "Unauthorized access.", data=None, status_code=401)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return func
|
2025-05-31 22:59:46 +08:00
|
|
|
|
2025-06-02 12:04:01 +08:00
|
|
|
def createClientBlueprint(wireguardConfigurations: dict[WireguardConfiguration], dashboardConfig: DashboardConfig):
|
|
|
|
from modules.DashboardClients import DashboardClients
|
|
|
|
DashboardClients = DashboardClients()
|
|
|
|
client = Blueprint('client', __name__, template_folder=os.path.abspath("./static/client/dist"))
|
|
|
|
prefix = f'{dashboardConfig.GetConfig("Server", "app_prefix")[1]}/client'
|
|
|
|
|
|
|
|
|
|
|
|
@client.before_request
|
|
|
|
def clientBeforeRequest():
|
|
|
|
if request.method.lower() == 'options':
|
|
|
|
return ResponseObject(True)
|
|
|
|
|
|
|
|
|
|
|
|
@client.post(f'{prefix}/api/signup')
|
|
|
|
def ClientAPI_SignUp():
|
|
|
|
data = request.json
|
|
|
|
status, msg = DashboardClients.SignUp(**data)
|
2025-06-02 19:23:04 +08:00
|
|
|
return ResponseObject(status, msg)
|
2025-06-02 12:04:01 +08:00
|
|
|
|
2025-06-02 19:23:04 +08:00
|
|
|
@client.post(f'{prefix}/api/signin')
|
|
|
|
def ClientAPI_SignIn():
|
|
|
|
data = request.json
|
|
|
|
status, msg = DashboardClients.SignIn(**data)
|
2025-06-02 12:04:01 +08:00
|
|
|
return ResponseObject(status, msg)
|
2025-06-02 19:23:04 +08:00
|
|
|
|
|
|
|
@client.get(prefix)
|
|
|
|
@login_required
|
|
|
|
def ClientIndex():
|
|
|
|
print(wireguardConfigurations.keys())
|
|
|
|
return render_template('client.html')
|
2025-06-02 12:04:01 +08:00
|
|
|
|
|
|
|
return client
|