gitscope
https://dev.kronoz.win/r/marzneshin.git
app/db/models.py · v0.6.0
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 00d6be5 Erfan 2y ago sub_revoked_at = Column(DateTime)
210 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
211 6457378 Dawsh 2y ago note = Column(String(500))
212 6457378 Dawsh 2y ago online_at = Column(DateTime)
213 6457378 Dawsh 2y ago edit_at = Column(DateTime)
214 a47eb4c Dawsh 2y ago
215 db68a58 Dawsh 2y ago @property
216 db68a58 Dawsh 2y ago def service_ids(self):
217 db68a58 Dawsh 2y ago return [service.id for service in self.services]
218 db68a58 Dawsh 2y ago
219 d377c4c Dawsh 2y ago @hybrid_property
220 d377c4c Dawsh 2y ago def expired(self):
221 d377c4c Dawsh 2y ago if self.expire_strategy == "fixed_date":
222 d377c4c Dawsh 2y ago return self.expire_date < datetime.utcnow()
223 d377c4c Dawsh 2y ago return False
224 d377c4c Dawsh 2y ago
225 d377c4c Dawsh 2y ago @expired.expression
226 d377c4c Dawsh 2y ago def expired(cls):
227 d377c4c Dawsh 2y ago return and_(
228 d377c4c Dawsh 2y ago cls.expire_strategy == "fixed_date", cls.expire_date < func.now()
229 d377c4c Dawsh 2y ago )
230 d377c4c Dawsh 2y ago
231 d377c4c Dawsh 2y ago @hybrid_property
232 d377c4c Dawsh 2y ago def data_limit_reached(self):
233 d377c4c Dawsh 2y ago if self.data_limit is not None:
234 d377c4c Dawsh 2y ago return self.used_traffic >= self.data_limit
235 d377c4c Dawsh 2y ago return False
236 d377c4c Dawsh 2y ago
237 d377c4c Dawsh 2y ago @data_limit_reached.expression
238 d377c4c Dawsh 2y ago def data_limit_reached(cls):
239 d377c4c Dawsh 2y ago return and_(
240 d377c4c Dawsh 2y ago cls.data_limit.isnot(None), cls.used_traffic >= cls.data_limit
241 d377c4c Dawsh 2y ago )
242 d377c4c Dawsh 2y ago
243 d377c4c Dawsh 2y ago @hybrid_property
244 d377c4c Dawsh 2y ago def is_active(self):
245 d377c4c Dawsh 2y ago return (
246 24826a2 Dawsh 2y ago self.enabled
247 24826a2 Dawsh 2y ago and not self.expired
248 24826a2 Dawsh 2y ago and not self.data_limit_reached
249 24826a2 Dawsh 2y ago and not self.removed
250 d377c4c Dawsh 2y ago )
251 d377c4c Dawsh 2y ago
252 d377c4c Dawsh 2y ago @is_active.expression
253 d377c4c Dawsh 2y ago def is_active(cls):
254 24826a2 Dawsh 2y ago return and_(
255 24826a2 Dawsh 2y ago cls.enabled == True,
256 24826a2 Dawsh 2y ago ~cls.expired,
257 24826a2 Dawsh 2y ago ~cls.data_limit_reached,
258 24826a2 Dawsh 2y ago ~cls.removed,
259 24826a2 Dawsh 2y ago )
260 d377c4c Dawsh 2y ago
261 d377c4c Dawsh 2y ago @property
262 d377c4c Dawsh 2y ago def status(self):
263 d377c4c Dawsh 2y ago return UserStatus.ACTIVE if self.is_active else UserStatus.INACTIVE
264 d377c4c Dawsh 2y ago
265 4708fcb Dawsh 2y ago @property
266 4708fcb Dawsh 2y ago def subscription_url(self):
267 b4ec790 Dawsh 2y ago prefix = (
268 b4ec790 Dawsh 2y ago self.admin.subscription_url_prefix if self.admin else None
269 b4ec790 Dawsh 2y ago ) or SUBSCRIPTION_URL_PREFIX
270 4708fcb Dawsh 2y ago return (
271 4708fcb Dawsh 2y ago prefix.replace("*", secrets.token_hex(8))
272 4708fcb Dawsh 2y ago + f"/sub/{self.username}/{self.key}"
273 4708fcb Dawsh 2y ago )
274 4708fcb Dawsh 2y ago
275 d0daa61 Mardin 2y ago @hybrid_property
276 d0daa61 Mardin 2y ago def owner_username(self):
277 b4ec790 Dawsh 2y ago return self.admin.username if self.admin else None
278 d0daa61 Mardin 2y ago
279 a47eb4c Dawsh 2y ago
280 f5c168d Mardin 2y ago class Backend(Base):
281 f5c168d Mardin 2y ago __tablename__ = "backends"
282 f5c168d Mardin 2y ago
283 f5c168d Mardin 2y ago id = Column(Integer, primary_key=True)
284 f5c168d Mardin 2y ago name = Column(String(64), nullable=False)
285 f5c168d Mardin 2y ago node_id = Column(Integer, ForeignKey("nodes.id"), index=True)
286 f5c168d Mardin 2y ago node = relationship("Node", back_populates="backends")
287 f5c168d Mardin 2y ago backend_type = Column(String(32), nullable=False)
288 f5c168d Mardin 2y ago version = Column(String(32))
289 f5c168d Mardin 2y ago running = Column(Boolean, default=True, nullable=False)
290 f5c168d Mardin 2y ago
291 f5c168d Mardin 2y ago
292 a47eb4c Dawsh 2y ago class Inbound(Base):
293 a47eb4c Dawsh 2y ago __tablename__ = "inbounds"
294 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("node_id", "tag"),)
295 a47eb4c Dawsh 2y ago
296 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
297 a47eb4c Dawsh 2y ago protocol = Column(Enum(ProxyTypes))
298 6457378 Dawsh 2y ago tag = Column(String(256), nullable=False)
299 010b05d Dawsh 2y ago config = Column(String(512), nullable=False)
300 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"), index=True)
301 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="inbounds")
302 01cdf2e Dawsh 2y ago services = relationship(
303 01cdf2e Dawsh 2y ago "Service", secondary=inbounds_services, back_populates="inbounds"
304 01cdf2e Dawsh 2y ago )
305 a47eb4c Dawsh 2y ago hosts = relationship(
306 f1ab16c Dawsh 2y ago "InboundHost",
307 f1ab16c Dawsh 2y ago back_populates="inbound",
308 f1ab16c Dawsh 2y ago cascade="all, delete, delete-orphan",
309 a47eb4c Dawsh 2y ago )
310 a47eb4c Dawsh 2y ago
311 db68a58 Dawsh 2y ago @property
312 db68a58 Dawsh 2y ago def service_ids(self):
313 db68a58 Dawsh 2y ago return [service.id for service in self.services]
314 db68a58 Dawsh 2y ago
315 a47eb4c Dawsh 2y ago
316 a47eb4c Dawsh 2y ago class InboundHost(Base):
317 a47eb4c Dawsh 2y ago __tablename__ = "hosts"
318 a47eb4c Dawsh 2y ago
319 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
320 6457378 Dawsh 2y ago remark = Column(String(256), nullable=False)
321 6457378 Dawsh 2y ago address = Column(String(256), nullable=False)
322 6457378 Dawsh 2y ago port = Column(Integer)
323 6457378 Dawsh 2y ago path = Column(String(256))
324 1fde8fd Dawsh 2y ago sni = Column(String(1024))
325 1fde8fd Dawsh 2y ago host = Column(String(1024))
326 a47eb4c Dawsh 2y ago security = Column(
327 a47eb4c Dawsh 2y ago Enum(InboundHostSecurity),
328 a47eb4c Dawsh 2y ago nullable=False,
329 a47eb4c Dawsh 2y ago default=InboundHostSecurity.inbound_default,
330 a47eb4c Dawsh 2y ago )
331 a47eb4c Dawsh 2y ago alpn = Column(
332 244c9d8 Dawsh 2y ago String(32),
333 244c9d8 Dawsh 2y ago server_default=sqlalchemy.sql.null(),
334 a47eb4c Dawsh 2y ago )
335 a47eb4c Dawsh 2y ago fingerprint = Column(
336 a47eb4c Dawsh 2y ago Enum(InboundHostFingerprint),
337 a47eb4c Dawsh 2y ago nullable=False,
338 a47eb4c Dawsh 2y ago default=InboundHostSecurity.none,
339 01cdf2e Dawsh 2y ago server_default=InboundHostSecurity.none.name,
340 a47eb4c Dawsh 2y ago )
341 a47eb4c Dawsh 2y ago
342 0b152b5 Mrdn 2y ago mux = Column(
343 f1ab16c Dawsh 2y ago Boolean,
344 f1ab16c Dawsh 2y ago default=False,
345 f1ab16c Dawsh 2y ago nullable=False,
346 4708fcb Dawsh 2y ago server_default=sqlalchemy.sql.false(),
347 0b152b5 Mrdn 2y ago )
348 0b152b5 Mrdn 2y ago fragment = Column(JSON())
349 0b152b5 Mrdn 2y ago
350 a47eb4c Dawsh 2y ago inbound_id = Column(Integer, ForeignKey("inbounds.id"), nullable=False)
351 a47eb4c Dawsh 2y ago inbound = relationship("Inbound", back_populates="hosts")
352 6457378 Dawsh 2y ago allowinsecure = Column(Boolean, default=False)
353 6457378 Dawsh 2y ago is_disabled = Column(Boolean, default=False)
354 e422d72 Dawsh 2y ago weight = Column(Integer, default=1, nullable=False, server_default="1")
355 a47eb4c Dawsh 2y ago
356 a47eb4c Dawsh 2y ago
357 a47eb4c Dawsh 2y ago class System(Base):
358 a47eb4c Dawsh 2y ago __tablename__ = "system"
359 a47eb4c Dawsh 2y ago
360 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
361 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
362 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
363 a47eb4c Dawsh 2y ago
364 a47eb4c Dawsh 2y ago
365 a47eb4c Dawsh 2y ago class JWT(Base):
366 a47eb4c Dawsh 2y ago __tablename__ = "jwt"
367 a47eb4c Dawsh 2y ago
368 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
369 a47eb4c Dawsh 2y ago secret_key = Column(
370 a47eb4c Dawsh 2y ago String(64), nullable=False, default=lambda: os.urandom(32).hex()
371 a47eb4c Dawsh 2y ago )
372 a47eb4c Dawsh 2y ago
373 a47eb4c Dawsh 2y ago
374 a47eb4c Dawsh 2y ago class TLS(Base):
375 a47eb4c Dawsh 2y ago __tablename__ = "tls"
376 a47eb4c Dawsh 2y ago
377 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
378 a47eb4c Dawsh 2y ago key = Column(String(4096), nullable=False)
379 a47eb4c Dawsh 2y ago certificate = Column(String(2048), nullable=False)
380 a47eb4c Dawsh 2y ago
381 a47eb4c Dawsh 2y ago
382 a47eb4c Dawsh 2y ago class Node(Base):
383 a47eb4c Dawsh 2y ago __tablename__ = "nodes"
384 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("address", "port"),)
385 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
386 6457378 Dawsh 2y ago name = Column(String(256), unique=True)
387 010b05d Dawsh 2y ago connection_backend = Column(String(32))
388 6457378 Dawsh 2y ago address = Column(String(256))
389 6457378 Dawsh 2y ago port = Column(Integer)
390 6457378 Dawsh 2y ago xray_version = Column(String(32))
391 f1ab16c Dawsh 2y ago inbounds = relationship(
392 f1ab16c Dawsh 2y ago "Inbound", back_populates="node", cascade="all, delete"
393 f1ab16c Dawsh 2y ago )
394 f5c168d Mardin 2y ago backends = relationship(
395 f5c168d Mardin 2y ago "Backend", back_populates="node", cascade="all, delete"
396 f5c168d Mardin 2y ago )
397 f1ab16c Dawsh 2y ago status = Column(
398 f1ab16c Dawsh 2y ago Enum(NodeStatus), nullable=False, default=NodeStatus.unhealthy
399 f1ab16c Dawsh 2y ago )
400 a47eb4c Dawsh 2y ago last_status_change = Column(DateTime, default=datetime.utcnow)
401 6457378 Dawsh 2y ago message = Column(String(1024))
402 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
403 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
404 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
405 01cdf2e Dawsh 2y ago user_usages = relationship(
406 f1ab16c Dawsh 2y ago "NodeUserUsage",
407 f1ab16c Dawsh 2y ago back_populates="node",
408 f1ab16c Dawsh 2y ago cascade="all, delete, delete-orphan",
409 01cdf2e Dawsh 2y ago )
410 01cdf2e Dawsh 2y ago usages = relationship(
411 f1ab16c Dawsh 2y ago "NodeUsage",
412 f1ab16c Dawsh 2y ago back_populates="node",
413 f1ab16c Dawsh 2y ago cascade="all, delete, delete-orphan",
414 01cdf2e Dawsh 2y ago )
415 01cdf2e Dawsh 2y ago usage_coefficient = Column(
416 01cdf2e Dawsh 2y ago Float, nullable=False, server_default=text("1.0"), default=1
417 01cdf2e Dawsh 2y ago )
418 a47eb4c Dawsh 2y ago
419 db68a58 Dawsh 2y ago @property
420 db68a58 Dawsh 2y ago def inbound_ids(self):
421 db68a58 Dawsh 2y ago return [inbound.id for inbound in self.inbounds]
422 db68a58 Dawsh 2y ago
423 a47eb4c Dawsh 2y ago
424 a47eb4c Dawsh 2y ago class NodeUserUsage(Base):
425 a47eb4c Dawsh 2y ago __tablename__ = "node_user_usages"
426 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("created_at", "user_id", "node_id"),)
427 a47eb4c Dawsh 2y ago
428 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
429 6457378 Dawsh 2y ago created_at = Column(DateTime, nullable=False) # one hour per record
430 a47eb4c Dawsh 2y ago user_id = Column(Integer, ForeignKey("users.id"))
431 a47eb4c Dawsh 2y ago user = relationship("User", back_populates="node_usages")
432 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"))
433 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="user_usages")
434 a47eb4c Dawsh 2y ago used_traffic = Column(BigInteger, default=0)
435 a47eb4c Dawsh 2y ago
436 a47eb4c Dawsh 2y ago
437 a47eb4c Dawsh 2y ago class NodeUsage(Base):
438 a47eb4c Dawsh 2y ago __tablename__ = "node_usages"
439 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("created_at", "node_id"),)
440 a47eb4c Dawsh 2y ago
441 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
442 6457378 Dawsh 2y ago created_at = Column(DateTime, nullable=False) # one hour per record
443 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"))
444 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="usages")
445 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
446 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
447 a47eb4c Dawsh 2y ago
448 a47eb4c Dawsh 2y ago
449 a47eb4c Dawsh 2y ago class NotificationReminder(Base):
450 a47eb4c Dawsh 2y ago __tablename__ = "notification_reminders"
451 a47eb4c Dawsh 2y ago
452 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
453 a47eb4c Dawsh 2y ago user_id = Column(Integer, ForeignKey("users.id"))
454 a47eb4c Dawsh 2y ago user = relationship("User", back_populates="notification_reminders")
455 a47eb4c Dawsh 2y ago type = Column(Enum(ReminderType), nullable=False)
456 6457378 Dawsh 2y ago expires_at = Column(DateTime)
457 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
458 0178858 Dawsh 2y ago
459 0178858 Dawsh 2y ago
460 0178858 Dawsh 2y ago class Settings(Base):
461 0178858 Dawsh 2y ago __tablename__ = "settings"
462 0178858 Dawsh 2y ago
463 0178858 Dawsh 2y ago id = Column(Integer, primary_key=True, server_default=text("0"))
464 0178858 Dawsh 2y ago subscription = Column(JSON, nullable=False)
465 0178858 Dawsh 2y ago telegram = Column(JSON)