Added Pydantic for configurations info

This commit is contained in:
Donald Zou
2025-08-14 22:36:14 +08:00
parent ebbb681dd8
commit c757cee988
3 changed files with 45 additions and 2 deletions

View File

@@ -12,6 +12,7 @@ from .Peer import Peer
from .PeerJobs import PeerJobs
from .PeerShareLinks import PeerShareLinks
from .Utilities import StringToBoolean, GenerateWireguardPublicKey, RegexMatch
from .WireguardConfigurationInfo import WireguardConfigurationInfo
class WireguardConfiguration:
@@ -122,6 +123,8 @@ class WireguardConfiguration:
if self.getAutostartStatus() and not self.getStatus() and startup:
self.toggleConfiguration()
print(f"[WGDashboard] Autostart Configuration: {name}")
self.configurationInfo: WireguardConfigurationInfo | None = None
def __getProtocolPath(self):
return self.DashboardConfig.GetConfig("Server", "wg_conf_path")[1] if self.Protocol == "wg" \
@@ -296,6 +299,12 @@ class WireguardConfiguration:
sqlalchemy.Column('preshared_key', sqlalchemy.String(255)),
extend_existing=True
)
self.infoTable = sqlalchemy.Table(
'ConfigurationsInfo', self.metadata,
sqlalchemy.Column('ID', sqlalchemy.String(255), primary_key=True),
sqlalchemy.Column('Info', sqlalchemy.Text),
extend_existing=True
)
self.metadata.create_all(self.engine)
@@ -1048,4 +1057,13 @@ class WireguardConfiguration:
else:
return { "sent": 0, "recv": 0 }
else:
return { "sent": 0, "recv": 0 }
return { "sent": 0, "recv": 0 }
def readConfigurationInfo(self):
with self.engine.connect() as conn:
result = conn.execute(
self.infoTable.select().where(
self.infoTable.c.ID == self.Name
)
).mappings().fetchone()
return result

View File

@@ -0,0 +1,24 @@
from pydantic import BaseModel, PositiveInt
class OverridePeerSettingsClass(BaseModel):
DNS: str = ''
EndpointAllowedIPs: str = ''
MTU: str | int = ''
PersistentKeepalive: int | str = ''
PeerRemoteEndpoint: str = ''
ListenPort: int | str = ''
class PeerGroupsClass(BaseModel):
Description: str = ''
BackgroundColor: str = ''
Peers: list[str] = []
class WireguardConfigurationInfo(BaseModel):
Description: str = ''
OverridePeerSettings: OverridePeerSettingsClass = OverridePeerSettingsClass(**{})
PeerGroups: dict[str, PeerGroupsClass] = {}
if __name__ == '__main__':
d = WireguardConfigurationInfo.model_validate_json("{\"Description\": \"Hi!\"}")
print(d.model_dump())

View File

@@ -13,4 +13,5 @@ sqlalchemy_utils
psycopg
mysqlclient
tzlocal
python-jose
python-jose
pydantic