From 4484668750d3029e67d6848647447feb5ebf9d44 Mon Sep 17 00:00:00 2001 From: Donald Zou Date: Mon, 5 Aug 2024 19:57:17 -0400 Subject: [PATCH] Backend for peer sharing is done --- src/dashboard.py | 68 +++++++++++++++++++ .../peerSettingsDropdown.vue | 5 ++ 2 files changed, 73 insertions(+) diff --git a/src/dashboard.py b/src/dashboard.py index cedfd05..19c038c 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -334,6 +334,72 @@ class PeerJobs: if operator == "lst": return x < y +class PeerShareLink: + def __init__(self, ShareID:str, Peer: str, Configuration: str, SharedDate: datetime, ExpireDate: datetime): + self.ShareID = ShareID + self.Peer = Peer + self.Configuration = Configuration + self.SharedDate = SharedDate + self.ExpireData = ExpireDate + + def toJson(self): + return { + "SharedID": self.ShareID, + "Peer": self.Peer, + "Configuration": self.Configuration, + "ShareDate": self.SharedDate + } + +class PeerShareLinks: + def __init__(self): + self.Links: list[PeerShareLink] = [] + + existingTables = cursor.execute("SELECT name FROM sqlite_master WHERE type='table' and name = 'PeerShareLinks'").fetchall() + if len(existingTables) == 0: + cursor.execute( + """ + CREATE TABLE PeerShareLinks ( + ShareID VARCHAR NOT NULL PRIMARY KEY, Configuration VARCHAR NOT NULL, Peer VARCHAR NOT NULL, + ExpireDate DATETIME, + SharedDate DATETIME DEFAULT (datetime('now', 'localtime')) + ) + """ % self.Name + ) + sqldb.commit() + + def __getSharedLinks(self): + self.Links.clear() + allLinks = cursor.execute("SELECT * FROM PeerShareLinks WHERE ExpireDate IS NULL OR ExpireDate > datetime('now', 'localtime')").fetchall() + for link in allLinks: + self.Links.append(*link) + + def getLink(self, Configuration: str, Peer: str): + return list(filter(lambda x : x.Configuration == Configuration and x.Peer == Peer, self.Links)) + + def getLink(self, ShareID: str): + return list(filter(lambda x : x.ShareID == ShareID, self.Links)) + + def addLink(self, Configuration: str, Peer: str, ExpireDate: datetime = None) -> tuple[bool, message]: + try: + newShareID = str(uuid.uuid4()) + if len(self.getLink(Configuration, Peer)) > 0: + cursor.execute("UPDATE PeerShareLinks SET ExpireDate = datetime('now', 'localtime') WHERE Configuration = ? AND Peer = ?", (Configuration, Peer, )) + cursor.execute("INSERT INTO PeerShareLinks VALUES (?, ?, ?, ?)", (newShareID, Configuration, Peer, ExpireDate, )) + sqldb.commit() + self.__getSharedLinks() + except Exception as e: + return False, str(e) + return True + + def updateLinkExpireDate(self, ShareID, ExpireDate): + cursor.execute("UPDATE PeerShareLinks SET ExpireDate = datetime('now', 'localtime') WHERE ShareID = ?", (ShareID, )) + sqldb.commit() + self.__getSharedLinks() + + + + + class WireguardConfiguration: class InvalidConfigurationFileException(Exception): def __init__(self, m): @@ -472,6 +538,8 @@ class WireguardConfiguration: """ % self.Name ) sqldb.commit() + + def __getPublicKey(self) -> str: return _generatePublicKey(self.PrivateKey)[1] diff --git a/src/static/app/src/components/configurationComponents/peerSettingsDropdown.vue b/src/static/app/src/components/configurationComponents/peerSettingsDropdown.vue index 69d215f..2e92229 100644 --- a/src/static/app/src/components/configurationComponents/peerSettingsDropdown.vue +++ b/src/static/app/src/components/configurationComponents/peerSettingsDropdown.vue @@ -122,6 +122,11 @@ export default { QR Code +
  • + + Share + +