mirror of
https://github.com/donaldzou/WGDashboard.git
synced 2025-10-04 00:06:18 +00:00
Added peer traffic and session
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
"""
|
||||
Peer
|
||||
"""
|
||||
import datetime
|
||||
import os, subprocess, uuid, random, re
|
||||
from datetime import timedelta
|
||||
|
||||
import jinja2
|
||||
|
||||
import sqlalchemy as db
|
||||
from .PeerJob import PeerJob
|
||||
from .PeerShareLink import PeerShareLink
|
||||
from .Utilities import GenerateWireguardPublicKey, ValidateIPAddressesWithRange, ValidateDNSAddress
|
||||
@@ -232,4 +234,54 @@ class Peer:
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
return True
|
||||
return True
|
||||
|
||||
def getSessions(self, startDate: datetime.datetime = None, endDate: datetime.datetime = None):
|
||||
if endDate is None:
|
||||
endDate = datetime.datetime.now()
|
||||
|
||||
if startDate is None:
|
||||
startDate = (endDate - datetime.timedelta(days=1))
|
||||
|
||||
endDate = endDate.replace(hour=23, minute=59, second=59, microsecond=999999)
|
||||
startDate = startDate.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
|
||||
|
||||
with self.configuration.engine.connect() as conn:
|
||||
result = conn.execute(
|
||||
db.select(
|
||||
self.configuration.peersTransferTable.c.time
|
||||
).where(
|
||||
db.and_(
|
||||
self.configuration.peersTransferTable.c.id == self.id,
|
||||
self.configuration.peersTransferTable.c.time <= endDate,
|
||||
self.configuration.peersTransferTable.c.time >= startDate,
|
||||
)
|
||||
).order_by(
|
||||
self.configuration.peersTransferTable.c.time
|
||||
)
|
||||
).fetchall()
|
||||
# sessions = []
|
||||
# current_session = [time[0]]
|
||||
#
|
||||
# for ts in time[1:]:
|
||||
# if ts - current_session[-1] <= datetime.timedelta(minutes=3):
|
||||
# current_session.append(ts)
|
||||
# else:
|
||||
# sessions.append({
|
||||
# "duration": self.__duration(current_session[-1], current_session[0]),
|
||||
# "timestamps": current_session
|
||||
# })
|
||||
# current_session = [ts]
|
||||
# sessions.append({
|
||||
# "duration": self.__duration(current_session[-1], current_session[0]),
|
||||
# "timestamps": current_session
|
||||
# })
|
||||
return list(map(lambda x : x[0], result))
|
||||
|
||||
def __duration(self, t1: datetime.datetime, t2: datetime.datetime):
|
||||
delta = t1 - t2
|
||||
|
||||
hours, remainder = divmod(delta.total_seconds(), 3600)
|
||||
minutes, seconds = divmod(remainder, 60)
|
||||
return f"{int(hours):02}:{int(minutes):02}:{int(seconds):02}"
|
@@ -397,14 +397,13 @@ class WireguardConfiguration:
|
||||
if len(split) == 2:
|
||||
p[pCounter]["name"] = split[1]
|
||||
with self.engine.begin() as conn:
|
||||
|
||||
|
||||
for i in p:
|
||||
if "PublicKey" in i.keys():
|
||||
tempPeer = conn.execute(self.peersTable.select().where(
|
||||
self.peersTable.columns.id == i['PublicKey']
|
||||
)).mappings().fetchone()
|
||||
|
||||
tempPeer = conn.execute(
|
||||
self.peersTable.select().where(
|
||||
self.peersTable.columns.id == i['PublicKey']
|
||||
)
|
||||
).mappings().fetchone()
|
||||
if tempPeer is None:
|
||||
tempPeer = {
|
||||
"id": i['PublicKey'],
|
||||
@@ -439,6 +438,7 @@ class WireguardConfiguration:
|
||||
self.peersTable.columns.id == i['PublicKey']
|
||||
)
|
||||
)
|
||||
|
||||
tmpList.append(Peer(tempPeer, self))
|
||||
except Exception as e:
|
||||
if __name__ == '__main__':
|
||||
@@ -449,7 +449,26 @@ class WireguardConfiguration:
|
||||
for i in existingPeers:
|
||||
tmpList.append(Peer(i, self))
|
||||
self.Peers = tmpList
|
||||
|
||||
|
||||
def logPeersTraffic(self):
|
||||
with self.engine.begin() as conn:
|
||||
for tempPeer in self.Peers:
|
||||
if tempPeer.status == "running":
|
||||
print(tempPeer.id + " running")
|
||||
conn.execute(
|
||||
self.peersTransferTable.insert().values({
|
||||
"id": tempPeer.id,
|
||||
"total_receive": tempPeer.total_receive,
|
||||
"total_sent": tempPeer.total_sent,
|
||||
"total_data": tempPeer.total_data,
|
||||
"cumu_sent": tempPeer.cumu_sent,
|
||||
"cumu_receive": tempPeer.cumu_receive,
|
||||
"cumu_data": tempPeer.cumu_data,
|
||||
"time": datetime.now()
|
||||
})
|
||||
)
|
||||
|
||||
|
||||
def addPeers(self, peers: list) -> tuple[bool, dict]:
|
||||
result = {
|
||||
"message": None,
|
||||
@@ -750,9 +769,6 @@ class WireguardConfiguration:
|
||||
)
|
||||
)
|
||||
|
||||
# except Exception as e:
|
||||
# print(cur_i, cur_i['total_receive'])
|
||||
# print(f"[WGDashboard] {self.Name} getPeersTransfer() Error: {str(e)} {str(e.__traceback__)}")
|
||||
|
||||
|
||||
def getPeersEndpoint(self):
|
||||
|
Reference in New Issue
Block a user