gitscope
https://dev.kronoz.win/r/marzneshin.git
app/db/models.py · v0.4.1
Source
1 a47eb4c Dawsh 2y ago import os
2 4708fcb Dawsh 2y ago import secrets
3 a47eb4c Dawsh 2y ago from datetime import datetime
4 a47eb4c Dawsh 2y ago
5 cba9caa Dawsh 2y ago import sqlalchemy.sql
6 01cdf2e Dawsh 2y ago from sqlalchemy import (
7 01cdf2e Dawsh 2y ago BigInteger,
8 01cdf2e Dawsh 2y ago Boolean,
9 01cdf2e Dawsh 2y ago Column,
10 01cdf2e Dawsh 2y ago DateTime,
11 01cdf2e Dawsh 2y ago Enum,
12 01cdf2e Dawsh 2y ago Float,
13 01cdf2e Dawsh 2y ago ForeignKey,
14 01cdf2e Dawsh 2y ago Integer,
15 01cdf2e Dawsh 2y ago String,
16 01cdf2e Dawsh 2y ago Table,
17 01cdf2e Dawsh 2y ago UniqueConstraint,
18 0b152b5 Mrdn 2y ago JSON,
19 d377c4c Dawsh 2y ago and_,
20 d377c4c Dawsh 2y ago func,
21 4708fcb Dawsh 2y ago select,
22 01cdf2e Dawsh 2y ago )
23 d377c4c Dawsh 2y ago from sqlalchemy.ext.hybrid import hybrid_property
24 4708fcb Dawsh 2y ago from sqlalchemy.orm import relationship, column_property
25 a47eb4c Dawsh 2y ago from sqlalchemy.sql.expression import text
26 a47eb4c Dawsh 2y ago
27 4708fcb Dawsh 2y ago from app.config.env import SUBSCRIPTION_URL_PREFIX
28 a47eb4c Dawsh 2y ago from app.db.base import Base
29 a47eb4c Dawsh 2y ago from app.models.node import NodeStatus
30 01cdf2e Dawsh 2y ago from app.models.proxy import (
31 01cdf2e Dawsh 2y ago InboundHostFingerprint,
32 01cdf2e Dawsh 2y ago InboundHostSecurity,
33 01cdf2e Dawsh 2y ago ProxyTypes,
34 01cdf2e Dawsh 2y ago )
35 f1ab16c Dawsh 2y ago from app.models.user import (
36 f1ab16c Dawsh 2y ago ReminderType,
37 d377c4c Dawsh 2y ago UserDataUsageResetStrategy,
38 f1ab16c Dawsh 2y ago UserStatus,
39 d377c4c Dawsh 2y ago UserExpireStrategy,
40 f1ab16c Dawsh 2y ago )
41 a47eb4c Dawsh 2y ago
42 4708fcb Dawsh 2y ago admins_services = Table(
43 4708fcb Dawsh 2y ago "admins_services",
44 4708fcb Dawsh 2y ago Base.metadata,
45 4708fcb Dawsh 2y ago Column("admin_id", ForeignKey("admins.id"), primary_key=True),
46 4708fcb Dawsh 2y ago Column("service_id", ForeignKey("services.id"), primary_key=True),
47 4708fcb Dawsh 2y ago )
48 a47eb4c Dawsh 2y ago
49 a47eb4c Dawsh 2y ago inbounds_services = Table(
50 a47eb4c Dawsh 2y ago "inbounds_services",
51 a47eb4c Dawsh 2y ago Base.metadata,
52 a47eb4c Dawsh 2y ago Column("inbound_id", ForeignKey("inbounds.id"), primary_key=True),
53 a47eb4c Dawsh 2y ago Column("service_id", ForeignKey("services.id"), primary_key=True),
54 a47eb4c Dawsh 2y ago )
55 a47eb4c Dawsh 2y ago
56 a47eb4c Dawsh 2y ago users_services = Table(
57 a47eb4c Dawsh 2y ago "users_services",
58 a47eb4c Dawsh 2y ago Base.metadata,
59 a47eb4c Dawsh 2y ago Column("user_id", ForeignKey("users.id"), primary_key=True),
60 a47eb4c Dawsh 2y ago Column("service_id", ForeignKey("services.id"), primary_key=True),
61 a47eb4c Dawsh 2y ago )
62 a47eb4c Dawsh 2y ago
63 a47eb4c Dawsh 2y ago
64 4708fcb Dawsh 2y ago class Admin(Base):
65 4708fcb Dawsh 2y ago __tablename__ = "admins"
66 4708fcb Dawsh 2y ago
67 4708fcb Dawsh 2y ago id = Column(Integer, primary_key=True)
68 4708fcb Dawsh 2y ago username = Column(String(32), unique=True, index=True)
69 4708fcb Dawsh 2y ago hashed_password = Column(String(128))
70 4708fcb Dawsh 2y ago users = relationship("User", back_populates="admin")
71 4708fcb Dawsh 2y ago services = relationship(
72 4708fcb Dawsh 2y ago "Service",
73 4708fcb Dawsh 2y ago secondary=admins_services,
74 4708fcb Dawsh 2y ago back_populates="admins",
75 4708fcb Dawsh 2y ago lazy="joined",
76 4708fcb Dawsh 2y ago )
77 4708fcb Dawsh 2y ago enabled = Column(
78 4708fcb Dawsh 2y ago Boolean,
79 4708fcb Dawsh 2y ago nullable=False,
80 4708fcb Dawsh 2y ago default=True,
81 4708fcb Dawsh 2y ago server_default=sqlalchemy.sql.true(),
82 4708fcb Dawsh 2y ago )
83 4708fcb Dawsh 2y ago all_services_access = Column(
84 4708fcb Dawsh 2y ago Boolean,
85 4708fcb Dawsh 2y ago nullable=False,
86 4708fcb Dawsh 2y ago default=False,
87 4708fcb Dawsh 2y ago server_default=sqlalchemy.sql.false(),
88 4708fcb Dawsh 2y ago )
89 4708fcb Dawsh 2y ago modify_users_access = Column(
90 4708fcb Dawsh 2y ago Boolean,
91 4708fcb Dawsh 2y ago nullable=False,
92 4708fcb Dawsh 2y ago default=True,
93 4708fcb Dawsh 2y ago server_default=sqlalchemy.sql.true(),
94 4708fcb Dawsh 2y ago )
95 4708fcb Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
96 4708fcb Dawsh 2y ago is_sudo = Column(Boolean, default=False)
97 4708fcb Dawsh 2y ago password_reset_at = Column(DateTime)
98 4708fcb Dawsh 2y ago subscription_url_prefix = Column(
99 4708fcb Dawsh 2y ago String(256),
100 4708fcb Dawsh 2y ago nullable=False,
101 4708fcb Dawsh 2y ago default="",
102 4708fcb Dawsh 2y ago server_default=sqlalchemy.sql.text(""),
103 4708fcb Dawsh 2y ago )
104 4708fcb Dawsh 2y ago
105 4708fcb Dawsh 2y ago @property
106 4708fcb Dawsh 2y ago def service_ids(self):
107 4708fcb Dawsh 2y ago return [service.id for service in self.services]
108 4708fcb Dawsh 2y ago
109 4708fcb Dawsh 2y ago @classmethod
110 4708fcb Dawsh 2y ago def __declare_last__(cls):
111 4708fcb Dawsh 2y ago cls.users_data_usage = column_property(
112 4708fcb Dawsh 2y ago select(func.coalesce(func.sum(User.lifetime_used_traffic), 0))
113 4708fcb Dawsh 2y ago .where(User.admin_id == cls.id)
114 4708fcb Dawsh 2y ago .correlate_except(User)
115 4708fcb Dawsh 2y ago .scalar_subquery()
116 4708fcb Dawsh 2y ago )
117 4708fcb Dawsh 2y ago
118 4708fcb Dawsh 2y ago
119 a47eb4c Dawsh 2y ago class Service(Base):
120 a47eb4c Dawsh 2y ago __tablename__ = "services"
121 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
122 6457378 Dawsh 2y ago name = Column(String(64))
123 4708fcb Dawsh 2y ago admins = relationship(
124 4708fcb Dawsh 2y ago "Admin", secondary=admins_services, back_populates="services"
125 4708fcb Dawsh 2y ago )
126 f1ab16c Dawsh 2y ago users = relationship(
127 f1ab16c Dawsh 2y ago "User", secondary=users_services, back_populates="services"
128 f1ab16c Dawsh 2y ago )
129 01cdf2e Dawsh 2y ago inbounds = relationship(
130 01cdf2e Dawsh 2y ago "Inbound", secondary=inbounds_services, back_populates="services"
131 01cdf2e Dawsh 2y ago )
132 a47eb4c Dawsh 2y ago
133 db68a58 Dawsh 2y ago @property
134 db68a58 Dawsh 2y ago def inbound_ids(self):
135 db68a58 Dawsh 2y ago return [inbound.id for inbound in self.inbounds]
136 db68a58 Dawsh 2y ago
137 db68a58 Dawsh 2y ago @property
138 db68a58 Dawsh 2y ago def user_ids(self):
139 db68a58 Dawsh 2y ago return [user.id for user in self.users]
140 db68a58 Dawsh 2y ago
141 a47eb4c Dawsh 2y ago
142 a47eb4c Dawsh 2y ago class User(Base):
143 a47eb4c Dawsh 2y ago __tablename__ = "users"
144 a47eb4c Dawsh 2y ago
145 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
146 6457378 Dawsh 2y ago username = Column(String(32), unique=True, index=True)
147 6457378 Dawsh 2y ago key = Column(String(64), unique=True)
148 d377c4c Dawsh 2y ago activated = Column(Boolean, nullable=False, default=True)
149 cba9caa Dawsh 2y ago enabled = Column(
150 f1ab16c Dawsh 2y ago Boolean,
151 f1ab16c Dawsh 2y ago nullable=False,
152 f1ab16c Dawsh 2y ago default=True,
153 f1ab16c Dawsh 2y ago server_default=sqlalchemy.sql.true(),
154 cba9caa Dawsh 2y ago )
155 24826a2 Dawsh 2y ago removed = Column(
156 24826a2 Dawsh 2y ago Boolean,
157 24826a2 Dawsh 2y ago nullable=False,
158 24826a2 Dawsh 2y ago default=False,
159 24826a2 Dawsh 2y ago server_default=sqlalchemy.sql.false(),
160 24826a2 Dawsh 2y ago )
161 01cdf2e Dawsh 2y ago services = relationship(
162 f1ab16c Dawsh 2y ago "Service",
163 f1ab16c Dawsh 2y ago secondary=users_services,
164 f1ab16c Dawsh 2y ago back_populates="users",
165 f1ab16c Dawsh 2y ago lazy="joined",
166 01cdf2e Dawsh 2y ago )
167 12b255b Dawsh 2y ago inbounds = relationship(
168 12b255b Dawsh 2y ago "Inbound",
169 12b255b Dawsh 2y ago secondary="join(users_services, inbounds_services, inbounds_services.c.service_id == users_services.c.service_id)"
170 01cdf2e Dawsh 2y ago ".join(Inbound, Inbound.id == inbounds_services.c.inbound_id)",
171 01cdf2e Dawsh 2y ago viewonly=True,
172 12b255b Dawsh 2y ago distinct_target_key=True,
173 01cdf2e Dawsh 2y ago )
174 a47eb4c Dawsh 2y ago used_traffic = Column(BigInteger, default=0)
175 8ee2242 Dawsh 2y ago lifetime_used_traffic = Column(
176 8ee2242 Dawsh 2y ago BigInteger, default=0, server_default="0", nullable=False
177 8ee2242 Dawsh 2y ago )
178 42a1073 Dawsh 2y ago traffic_reset_at = Column(DateTime)
179 01cdf2e Dawsh 2y ago node_usages = relationship(
180 01cdf2e Dawsh 2y ago "NodeUserUsage",
181 01cdf2e Dawsh 2y ago back_populates="user",
182 01cdf2e Dawsh 2y ago cascade="all,delete,delete-orphan",
183 01cdf2e Dawsh 2y ago )
184 01cdf2e Dawsh 2y ago notification_reminders = relationship(
185 01cdf2e Dawsh 2y ago "NotificationReminder",
186 01cdf2e Dawsh 2y ago back_populates="user",
187 01cdf2e Dawsh 2y ago cascade="all,delete,delete-orphan",
188 01cdf2e Dawsh 2y ago )
189 6457378 Dawsh 2y ago data_limit = Column(BigInteger)
190 a47eb4c Dawsh 2y ago data_limit_reset_strategy = Column(
191 d377c4c Dawsh 2y ago Enum(UserDataUsageResetStrategy),
192 a47eb4c Dawsh 2y ago nullable=False,
193 d377c4c Dawsh 2y ago default=UserDataUsageResetStrategy.no_reset,
194 a47eb4c Dawsh 2y ago )
195 a47eb4c Dawsh 2y ago ip_limit = Column(Integer, nullable=False, default=-1)
196 010b05d Dawsh 2y ago settings = Column(String(1024))
197 d377c4c Dawsh 2y ago expire_strategy = Column(
198 d377c4c Dawsh 2y ago Enum(UserExpireStrategy),
199 d377c4c Dawsh 2y ago nullable=False,
200 d377c4c Dawsh 2y ago default=UserExpireStrategy.NEVER,
201 d377c4c Dawsh 2y ago )
202 d377c4c Dawsh 2y ago expire_date = Column(DateTime)
203 d377c4c Dawsh 2y ago usage_duration = Column(BigInteger)
204 d377c4c Dawsh 2y ago activation_deadline = Column(DateTime)
205 a47eb4c Dawsh 2y ago admin_id = Column(Integer, ForeignKey("admins.id"))
206 a47eb4c Dawsh 2y ago admin = relationship("Admin", back_populates="users")
207 6457378 Dawsh 2y ago sub_updated_at = Column(DateTime)
208 6457378 Dawsh 2y ago sub_last_user_agent = Column(String(512))
209 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
210 6457378 Dawsh 2y ago note = Column(String(500))
211 6457378 Dawsh 2y ago online_at = Column(DateTime)
212 6457378 Dawsh 2y ago edit_at = Column(DateTime)
213 a47eb4c Dawsh 2y ago
214 db68a58 Dawsh 2y ago @property
215 db68a58 Dawsh 2y ago def service_ids(self):
216 db68a58 Dawsh 2y ago return [service.id for service in self.services]
217 db68a58 Dawsh 2y ago
218 d377c4c Dawsh 2y ago @hybrid_property
219 d377c4c Dawsh 2y ago def expired(self):
220 d377c4c Dawsh 2y ago if self.expire_strategy == "fixed_date":
221 d377c4c Dawsh 2y ago return self.expire_date < datetime.utcnow()
222 d377c4c Dawsh 2y ago return False
223 d377c4c Dawsh 2y ago
224 d377c4c Dawsh 2y ago @expired.expression
225 d377c4c Dawsh 2y ago def expired(cls):
226 d377c4c Dawsh 2y ago return and_(
227 d377c4c Dawsh 2y ago cls.expire_strategy == "fixed_date", cls.expire_date < func.now()
228 d377c4c Dawsh 2y ago )
229 d377c4c Dawsh 2y ago
230 d377c4c Dawsh 2y ago @hybrid_property
231 d377c4c Dawsh 2y ago def data_limit_reached(self):
232 d377c4c Dawsh 2y ago if self.data_limit is not None:
233 d377c4c Dawsh 2y ago return self.used_traffic >= self.data_limit
234 d377c4c Dawsh 2y ago return False
235 d377c4c Dawsh 2y ago
236 d377c4c Dawsh 2y ago @data_limit_reached.expression
237 d377c4c Dawsh 2y ago def data_limit_reached(cls):
238 d377c4c Dawsh 2y ago return and_(
239 d377c4c Dawsh 2y ago cls.data_limit.isnot(None), cls.used_traffic >= cls.data_limit
240 d377c4c Dawsh 2y ago )
241 d377c4c Dawsh 2y ago
242 d377c4c Dawsh 2y ago @hybrid_property
243 d377c4c Dawsh 2y ago def is_active(self):
244 d377c4c Dawsh 2y ago return (
245 24826a2 Dawsh 2y ago self.enabled
246 24826a2 Dawsh 2y ago and not self.expired
247 24826a2 Dawsh 2y ago and not self.data_limit_reached
248 24826a2 Dawsh 2y ago and not self.removed
249 d377c4c Dawsh 2y ago )
250 d377c4c Dawsh 2y ago
251 d377c4c Dawsh 2y ago @is_active.expression
252 d377c4c Dawsh 2y ago def is_active(cls):
253 24826a2 Dawsh 2y ago return and_(
254 24826a2 Dawsh 2y ago cls.enabled == True,
255 24826a2 Dawsh 2y ago ~cls.expired,
256 24826a2 Dawsh 2y ago ~cls.data_limit_reached,
257 24826a2 Dawsh 2y ago ~cls.removed,
258 24826a2 Dawsh 2y ago )
259 d377c4c Dawsh 2y ago
260 d377c4c Dawsh 2y ago @property
261 d377c4c Dawsh 2y ago def status(self):
262 d377c4c Dawsh 2y ago return UserStatus.ACTIVE if self.is_active else UserStatus.INACTIVE
263 d377c4c Dawsh 2y ago
264 4708fcb Dawsh 2y ago @property
265 4708fcb Dawsh 2y ago def subscription_url(self):
266 4708fcb Dawsh 2y ago prefix = self.admin.subscription_url_prefix or SUBSCRIPTION_URL_PREFIX
267 4708fcb Dawsh 2y ago return (
268 4708fcb Dawsh 2y ago prefix.replace("*", secrets.token_hex(8))
269 4708fcb Dawsh 2y ago + f"/sub/{self.username}/{self.key}"
270 4708fcb Dawsh 2y ago )
271 4708fcb Dawsh 2y ago
272 d0daa61 Mardin 2y ago @hybrid_property
273 d0daa61 Mardin 2y ago def owner_username(self):
274 d0daa61 Mardin 2y ago return self.admin.username
275 d0daa61 Mardin 2y ago
276 a47eb4c Dawsh 2y ago
277 a47eb4c Dawsh 2y ago class Inbound(Base):
278 a47eb4c Dawsh 2y ago __tablename__ = "inbounds"
279 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("node_id", "tag"),)
280 a47eb4c Dawsh 2y ago
281 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
282 a47eb4c Dawsh 2y ago protocol = Column(Enum(ProxyTypes))
283 6457378 Dawsh 2y ago tag = Column(String(256), nullable=False)
284 010b05d Dawsh 2y ago config = Column(String(512), nullable=False)
285 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"), index=True)
286 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="inbounds")
287 01cdf2e Dawsh 2y ago services = relationship(
288 01cdf2e Dawsh 2y ago "Service", secondary=inbounds_services, back_populates="inbounds"
289 01cdf2e Dawsh 2y ago )
290 a47eb4c Dawsh 2y ago hosts = relationship(
291 f1ab16c Dawsh 2y ago "InboundHost",
292 f1ab16c Dawsh 2y ago back_populates="inbound",
293 f1ab16c Dawsh 2y ago cascade="all, delete, delete-orphan",
294 a47eb4c Dawsh 2y ago )
295 a47eb4c Dawsh 2y ago
296 db68a58 Dawsh 2y ago @property
297 db68a58 Dawsh 2y ago def service_ids(self):
298 db68a58 Dawsh 2y ago return [service.id for service in self.services]
299 db68a58 Dawsh 2y ago
300 a47eb4c Dawsh 2y ago
301 a47eb4c Dawsh 2y ago class InboundHost(Base):
302 a47eb4c Dawsh 2y ago __tablename__ = "hosts"
303 a47eb4c Dawsh 2y ago
304 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
305 6457378 Dawsh 2y ago remark = Column(String(256), nullable=False)
306 6457378 Dawsh 2y ago address = Column(String(256), nullable=False)
307 6457378 Dawsh 2y ago port = Column(Integer)
308 6457378 Dawsh 2y ago path = Column(String(256))
309 1fde8fd Dawsh 2y ago sni = Column(String(1024))
310 1fde8fd Dawsh 2y ago host = Column(String(1024))
311 a47eb4c Dawsh 2y ago security = Column(
312 a47eb4c Dawsh 2y ago Enum(InboundHostSecurity),
313 a47eb4c Dawsh 2y ago nullable=False,
314 a47eb4c Dawsh 2y ago default=InboundHostSecurity.inbound_default,
315 a47eb4c Dawsh 2y ago )
316 a47eb4c Dawsh 2y ago alpn = Column(
317 244c9d8 Dawsh 2y ago String(32),
318 244c9d8 Dawsh 2y ago server_default=sqlalchemy.sql.null(),
319 a47eb4c Dawsh 2y ago )
320 a47eb4c Dawsh 2y ago fingerprint = Column(
321 a47eb4c Dawsh 2y ago Enum(InboundHostFingerprint),
322 a47eb4c Dawsh 2y ago nullable=False,
323 a47eb4c Dawsh 2y ago default=InboundHostSecurity.none,
324 01cdf2e Dawsh 2y ago server_default=InboundHostSecurity.none.name,
325 a47eb4c Dawsh 2y ago )
326 a47eb4c Dawsh 2y ago
327 0b152b5 Mrdn 2y ago mux = Column(
328 f1ab16c Dawsh 2y ago Boolean,
329 f1ab16c Dawsh 2y ago default=False,
330 f1ab16c Dawsh 2y ago nullable=False,
331 4708fcb Dawsh 2y ago server_default=sqlalchemy.sql.false(),
332 0b152b5 Mrdn 2y ago )
333 0b152b5 Mrdn 2y ago fragment = Column(JSON())
334 0b152b5 Mrdn 2y ago
335 a47eb4c Dawsh 2y ago inbound_id = Column(Integer, ForeignKey("inbounds.id"), nullable=False)
336 a47eb4c Dawsh 2y ago inbound = relationship("Inbound", back_populates="hosts")
337 6457378 Dawsh 2y ago allowinsecure = Column(Boolean, default=False)
338 6457378 Dawsh 2y ago is_disabled = Column(Boolean, default=False)
339 a47eb4c Dawsh 2y ago
340 a47eb4c Dawsh 2y ago
341 a47eb4c Dawsh 2y ago class System(Base):
342 a47eb4c Dawsh 2y ago __tablename__ = "system"
343 a47eb4c Dawsh 2y ago
344 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
345 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
346 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
347 a47eb4c Dawsh 2y ago
348 a47eb4c Dawsh 2y ago
349 a47eb4c Dawsh 2y ago class JWT(Base):
350 a47eb4c Dawsh 2y ago __tablename__ = "jwt"
351 a47eb4c Dawsh 2y ago
352 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
353 a47eb4c Dawsh 2y ago secret_key = Column(
354 a47eb4c Dawsh 2y ago String(64), nullable=False, default=lambda: os.urandom(32).hex()
355 a47eb4c Dawsh 2y ago )
356 a47eb4c Dawsh 2y ago
357 a47eb4c Dawsh 2y ago
358 a47eb4c Dawsh 2y ago class TLS(Base):
359 a47eb4c Dawsh 2y ago __tablename__ = "tls"
360 a47eb4c Dawsh 2y ago
361 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
362 a47eb4c Dawsh 2y ago key = Column(String(4096), nullable=False)
363 a47eb4c Dawsh 2y ago certificate = Column(String(2048), nullable=False)
364 a47eb4c Dawsh 2y ago
365 a47eb4c Dawsh 2y ago
366 a47eb4c Dawsh 2y ago class Node(Base):
367 a47eb4c Dawsh 2y ago __tablename__ = "nodes"
368 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("address", "port"),)
369 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
370 6457378 Dawsh 2y ago name = Column(String(256), unique=True)
371 010b05d Dawsh 2y ago connection_backend = Column(String(32))
372 6457378 Dawsh 2y ago address = Column(String(256))
373 6457378 Dawsh 2y ago port = Column(Integer)
374 6457378 Dawsh 2y ago xray_version = Column(String(32))
375 f1ab16c Dawsh 2y ago inbounds = relationship(
376 f1ab16c Dawsh 2y ago "Inbound", back_populates="node", cascade="all, delete"
377 f1ab16c Dawsh 2y ago )
378 f1ab16c Dawsh 2y ago status = Column(
379 f1ab16c Dawsh 2y ago Enum(NodeStatus), nullable=False, default=NodeStatus.unhealthy
380 f1ab16c Dawsh 2y ago )
381 a47eb4c Dawsh 2y ago last_status_change = Column(DateTime, default=datetime.utcnow)
382 6457378 Dawsh 2y ago message = Column(String(1024))
383 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
384 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
385 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
386 01cdf2e Dawsh 2y ago user_usages = relationship(
387 f1ab16c Dawsh 2y ago "NodeUserUsage",
388 f1ab16c Dawsh 2y ago back_populates="node",
389 f1ab16c Dawsh 2y ago cascade="all, delete, delete-orphan",
390 01cdf2e Dawsh 2y ago )
391 01cdf2e Dawsh 2y ago usages = relationship(
392 f1ab16c Dawsh 2y ago "NodeUsage",
393 f1ab16c Dawsh 2y ago back_populates="node",
394 f1ab16c Dawsh 2y ago cascade="all, delete, delete-orphan",
395 01cdf2e Dawsh 2y ago )
396 01cdf2e Dawsh 2y ago usage_coefficient = Column(
397 01cdf2e Dawsh 2y ago Float, nullable=False, server_default=text("1.0"), default=1
398 01cdf2e Dawsh 2y ago )
399 a47eb4c Dawsh 2y ago
400 db68a58 Dawsh 2y ago @property
401 db68a58 Dawsh 2y ago def inbound_ids(self):
402 db68a58 Dawsh 2y ago return [inbound.id for inbound in self.inbounds]
403 db68a58 Dawsh 2y ago
404 a47eb4c Dawsh 2y ago
405 a47eb4c Dawsh 2y ago class NodeUserUsage(Base):
406 a47eb4c Dawsh 2y ago __tablename__ = "node_user_usages"
407 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("created_at", "user_id", "node_id"),)
408 a47eb4c Dawsh 2y ago
409 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
410 6457378 Dawsh 2y ago created_at = Column(DateTime, nullable=False) # one hour per record
411 a47eb4c Dawsh 2y ago user_id = Column(Integer, ForeignKey("users.id"))
412 a47eb4c Dawsh 2y ago user = relationship("User", back_populates="node_usages")
413 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"))
414 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="user_usages")
415 a47eb4c Dawsh 2y ago used_traffic = Column(BigInteger, default=0)
416 a47eb4c Dawsh 2y ago
417 a47eb4c Dawsh 2y ago
418 a47eb4c Dawsh 2y ago class NodeUsage(Base):
419 a47eb4c Dawsh 2y ago __tablename__ = "node_usages"
420 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("created_at", "node_id"),)
421 a47eb4c Dawsh 2y ago
422 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
423 6457378 Dawsh 2y ago created_at = Column(DateTime, nullable=False) # one hour per record
424 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"))
425 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="usages")
426 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
427 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
428 a47eb4c Dawsh 2y ago
429 a47eb4c Dawsh 2y ago
430 a47eb4c Dawsh 2y ago class NotificationReminder(Base):
431 a47eb4c Dawsh 2y ago __tablename__ = "notification_reminders"
432 a47eb4c Dawsh 2y ago
433 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
434 a47eb4c Dawsh 2y ago user_id = Column(Integer, ForeignKey("users.id"))
435 a47eb4c Dawsh 2y ago user = relationship("User", back_populates="notification_reminders")
436 a47eb4c Dawsh 2y ago type = Column(Enum(ReminderType), nullable=False)
437 6457378 Dawsh 2y ago expires_at = Column(DateTime)
438 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
439 0178858 Dawsh 2y ago
440 0178858 Dawsh 2y ago
441 0178858 Dawsh 2y ago class Settings(Base):
442 0178858 Dawsh 2y ago __tablename__ = "settings"
443 0178858 Dawsh 2y ago
444 0178858 Dawsh 2y ago id = Column(Integer, primary_key=True, server_default=text("0"))
445 0178858 Dawsh 2y ago subscription = Column(JSON, nullable=False)
446 0178858 Dawsh 2y ago telegram = Column(JSON)