-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (84 loc) · 3.34 KB
/
Copy pathmain.py
File metadata and controls
103 lines (84 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import sys
import threading
import os
from fastapi import FastAPI
import uvicorn
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import Qt
from config import CERTS_DIR
from ui.popups import UIBroker
from core.workflow import AIBOSWorkflow
from api.routes import router
import api.routes
app = FastAPI(title="AI BOS API", description="Secure Mission Transfer Node")
# Include the API router
app.include_router(router)
def generate_self_signed_cert(cert_path: str, key_path: str):
"""Generates a self-signed TLS certificate if none exists for secure Uvicorn execution."""
if os.path.exists(cert_path) and os.path.exists(key_path):
return
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
import datetime
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
with open(key_path, "wb") as f:
f.write(key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
))
subject = issuer = x509.Name([
x509.NameAttribute(NameOID.COMMON_NAME, u"localhost"),
])
cert = x509.CertificateBuilder().subject_name(
subject
).issuer_name(
issuer
).public_key(
key.public_key()
).serial_number(
x509.random_serial_number()
).not_valid_before(
datetime.datetime.now(datetime.timezone.utc)
).not_valid_after(
datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(days=3650)
).add_extension(
x509.SubjectAlternativeName([x509.DNSName(u"localhost")]),
critical=False,
).sign(key, hashes.SHA256())
with open(cert_path, "wb") as f:
f.write(cert.public_bytes(serialization.Encoding.PEM))
def run_api():
"""Runs the FastAPI application using Uvicorn with TLS enabled."""
cert_file = os.path.join(CERTS_DIR, "cert.pem")
key_file = os.path.join(CERTS_DIR, "key.pem")
generate_self_signed_cert(cert_file, key_file)
uvicorn.run(
app,
host="0.0.0.0",
port=8443,
ssl_keyfile=key_file,
ssl_certfile=cert_file,
log_level="warning"
)
if __name__ == "__main__":
print("Initializing AI BOS Core and Backend API...")
# 1. Initialize the existing AIBOS Workflow (which loads the AI Model)
from core.workflow import AIBOSWorkflow
from ui.popups import UIBroker
ui_broker = UIBroker() # Required for the background FastAPI to trigger popups
api.routes.workflow_instance = AIBOSWorkflow(ui_broker=ui_broker)
# 2. Start External Network API in background
api_thread = threading.Thread(target=run_api, daemon=True)
api_thread.start()
print("Backend secure API active.")
print("Launching Desktop Management Console...")
# 3. Start the fully integrated Desktop UI Application
from ui.app import AIBOSApplication
desktop_app = AIBOSApplication(sys.argv)
# Map the existing ui_broker signals to the new MainWindow if needed
# Run the application event loop (Blocks until Tray -> Exit Securely is clicked)
sys.exit(desktop_app.run())