gitscope
https://dev.kronoz.win/r/marzneshin.git
app/db/models.py · v0.5.6
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 f5c168d Mardin 2y ago class Backend(Base):
278 f5c168d Mardin 2y ago __tablename__ = "backends"
279 f5c168d Mardin 2y ago
280 f5c168d Mardin 2y ago id = Column(Integer, primary_key=True)
281 f5c168d Mardin 2y ago name = Column(String(64), nullable=False)
282 f5c168d Mardin 2y ago node_id = Column(Integer, ForeignKey("nodes.id"), index=True)
283 f5c168d Mardin 2y ago node = relationship("Node", back_populates="backends")
284 f5c168d Mardin 2y ago backend_type = Column(String(32), nullable=False)
285 f5c168d Mardin 2y ago version = Column(String(32))
286 f5c168d Mardin 2y ago running = Column(Boolean, default=True, nullable=False)
287 f5c168d Mardin 2y ago
288 f5c168d Mardin 2y ago
289 a47eb4c Dawsh 2y ago class Inbound(Base):
290 a47eb4c Dawsh 2y ago __tablename__ = "inbounds"
291 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("node_id", "tag"),)
292 a47eb4c Dawsh 2y ago
293 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
294 a47eb4c Dawsh 2y ago protocol = Column(Enum(ProxyTypes))
295 6457378 Dawsh 2y ago tag = Column(String(256), nullable=False)
296 010b05d Dawsh 2y ago config = Column(String(512), nullable=False)
297 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"), index=True)
298 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="inbounds")
299 01cdf2e Dawsh 2y ago services = relationship(
300 01cdf2e Dawsh 2y ago "Service", secondary=inbounds_services, back_populates="inbounds"
301 01cdf2e Dawsh 2y ago )
302 a47eb4c Dawsh 2y ago hosts = relationship(
303 f1ab16c Dawsh 2y ago "InboundHost",
304 f1ab16c Dawsh 2y ago back_populates="inbound",
305 f1ab16c Dawsh 2y ago cascade="all, delete, delete-orphan",
306 a47eb4c Dawsh 2y ago )
307 a47eb4c Dawsh 2y ago
308 db68a58 Dawsh 2y ago @property
309 db68a58 Dawsh 2y ago def service_ids(self):
310 db68a58 Dawsh 2y ago return [service.id for service in self.services]
311 db68a58 Dawsh 2y ago
312 a47eb4c Dawsh 2y ago
313 a47eb4c Dawsh 2y ago class InboundHost(Base):
314 a47eb4c Dawsh 2y ago __tablename__ = "hosts"
315 a47eb4c Dawsh 2y ago
316 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
317 6457378 Dawsh 2y ago remark = Column(String(256), nullable=False)
318 6457378 Dawsh 2y ago address = Column(String(256), nullable=False)
319 6457378 Dawsh 2y ago port = Column(Integer)
320 6457378 Dawsh 2y ago path = Column(String(256))
321 1fde8fd Dawsh 2y ago sni = Column(String(1024))
322 1fde8fd Dawsh 2y ago host = Column(String(1024))
323 a47eb4c Dawsh 2y ago security = Column(
324 a47eb4c Dawsh 2y ago Enum(InboundHostSecurity),
325 a47eb4c Dawsh 2y ago nullable=False,
326 a47eb4c Dawsh 2y ago default=InboundHostSecurity.inbound_default,
327 a47eb4c Dawsh 2y ago )
328 a47eb4c Dawsh 2y ago alpn = Column(
329 244c9d8 Dawsh 2y ago String(32),
330 244c9d8 Dawsh 2y ago server_default=sqlalchemy.sql.null(),
331 a47eb4c Dawsh 2y ago )
332 a47eb4c Dawsh 2y ago fingerprint = Column(
333 a47eb4c Dawsh 2y ago Enum(InboundHostFingerprint),
334 a47eb4c Dawsh 2y ago nullable=False,
335 a47eb4c Dawsh 2y ago default=InboundHostSecurity.none,
336 01cdf2e Dawsh 2y ago server_default=InboundHostSecurity.none.name,
337 a47eb4c Dawsh 2y ago )
338 a47eb4c Dawsh 2y ago
339 0b152b5 Mrdn 2y ago mux = Column(
340 f1ab16c Dawsh 2y ago Boolean,
341 f1ab16c Dawsh 2y ago default=False,
342 f1ab16c Dawsh 2y ago nullable=False,
343 4708fcb Dawsh 2y ago server_default=sqlalchemy.sql.false(),
344 0b152b5 Mrdn 2y ago )
345 0b152b5 Mrdn 2y ago fragment = Column(JSON())
346 0b152b5 Mrdn 2y ago
347 a47eb4c Dawsh 2y ago inbound_id = Column(Integer, ForeignKey("inbounds.id"), nullable=False)
348 a47eb4c Dawsh 2y ago inbound = relationship("Inbound", back_populates="hosts")
349 6457378 Dawsh 2y ago allowinsecure = Column(Boolean, default=False)
350 6457378 Dawsh 2y ago is_disabled = Column(Boolean, default=False)
351 e422d72 Dawsh 2y ago weight = Column(Integer, default=1, nullable=False, server_default="1")
352 a47eb4c Dawsh 2y ago
353 a47eb4c Dawsh 2y ago
354 a47eb4c Dawsh 2y ago class System(Base):
355 a47eb4c Dawsh 2y ago __tablename__ = "system"
356 a47eb4c Dawsh 2y ago
357 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
358 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
359 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
360 a47eb4c Dawsh 2y ago
361 a47eb4c Dawsh 2y ago
362 a47eb4c Dawsh 2y ago class JWT(Base):
363 a47eb4c Dawsh 2y ago __tablename__ = "jwt"
364 a47eb4c Dawsh 2y ago
365 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
366 a47eb4c Dawsh 2y ago secret_key = Column(
367 a47eb4c Dawsh 2y ago String(64), nullable=False, default=lambda: os.urandom(32).hex()
368 a47eb4c Dawsh 2y ago )
369 a47eb4c Dawsh 2y ago
370 a47eb4c Dawsh 2y ago
371 a47eb4c Dawsh 2y ago class TLS(Base):
372 a47eb4c Dawsh 2y ago __tablename__ = "tls"
373 a47eb4c Dawsh 2y ago
374 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
375 a47eb4c Dawsh 2y ago key = Column(String(4096), nullable=False)
376 a47eb4c Dawsh 2y ago certificate = Column(String(2048), nullable=False)
377 a47eb4c Dawsh 2y ago
378 a47eb4c Dawsh 2y ago
379 a47eb4c Dawsh 2y ago class Node(Base):
380 a47eb4c Dawsh 2y ago __tablename__ = "nodes"
381 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("address", "port"),)
382 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
383 6457378 Dawsh 2y ago name = Column(String(256), unique=True)
384 010b05d Dawsh 2y ago connection_backend = Column(String(32))
385 6457378 Dawsh 2y ago address = Column(String(256))
386 6457378 Dawsh 2y ago port = Column(Integer)
387 6457378 Dawsh 2y ago xray_version = Column(String(32))
388 f1ab16c Dawsh 2y ago inbounds = relationship(
389 f1ab16c Dawsh 2y ago "Inbound", back_populates="node", cascade="all, delete"
390 f1ab16c Dawsh 2y ago )
391 f5c168d Mardin 2y ago backends = relationship(
392 f5c168d Mardin 2y ago "Backend", back_populates="node", cascade="all, delete"
393 f5c168d Mardin 2y ago )
394 f1ab16c Dawsh 2y ago status = Column(
395 f1ab16c Dawsh 2y ago Enum(NodeStatus), nullable=False, default=NodeStatus.unhealthy
396 f1ab16c Dawsh 2y ago )
397 a47eb4c Dawsh 2y ago last_status_change = Column(DateTime, default=datetime.utcnow)
398 6457378 Dawsh 2y ago message = Column(String(1024))
399 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
400 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
401 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
402 01cdf2e Dawsh 2y ago user_usages = relationship(
403 f1ab16c Dawsh 2y ago "NodeUserUsage",
404 f1ab16c Dawsh 2y ago back_populates="node",
405 f1ab16c Dawsh 2y ago cascade="all, delete, delete-orphan",
406 01cdf2e Dawsh 2y ago )
407 01cdf2e Dawsh 2y ago usages = relationship(
408 f1ab16c Dawsh 2y ago "NodeUsage",
409 f1ab16c Dawsh 2y ago back_populates="node",
410 f1ab16c Dawsh 2y ago cascade="all, delete, delete-orphan",
411 01cdf2e Dawsh 2y ago )
412 01cdf2e Dawsh 2y ago usage_coefficient = Column(
413 01cdf2e Dawsh 2y ago Float, nullable=False, server_default=text("1.0"), default=1
414 01cdf2e Dawsh 2y ago )
415 a47eb4c Dawsh 2y ago
416 db68a58 Dawsh 2y ago @property
417 db68a58 Dawsh 2y ago def inbound_ids(self):
418 db68a58 Dawsh 2y ago return [inbound.id for inbound in self.inbounds]
419 db68a58 Dawsh 2y ago
420 a47eb4c Dawsh 2y ago
421 a47eb4c Dawsh 2y ago class NodeUserUsage(Base):
422 a47eb4c Dawsh 2y ago __tablename__ = "node_user_usages"
423 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("created_at", "user_id", "node_id"),)
424 a47eb4c Dawsh 2y ago
425 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
426 6457378 Dawsh 2y ago created_at = Column(DateTime, nullable=False) # one hour per record
427 a47eb4c Dawsh 2y ago user_id = Column(Integer, ForeignKey("users.id"))
428 a47eb4c Dawsh 2y ago user = relationship("User", back_populates="node_usages")
429 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"))
430 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="user_usages")
431 a47eb4c Dawsh 2y ago used_traffic = Column(BigInteger, default=0)
432 a47eb4c Dawsh 2y ago
433 a47eb4c Dawsh 2y ago
434 a47eb4c Dawsh 2y ago class NodeUsage(Base):
435 a47eb4c Dawsh 2y ago __tablename__ = "node_usages"
436 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("created_at", "node_id"),)
437 a47eb4c Dawsh 2y ago
438 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
439 6457378 Dawsh 2y ago created_at = Column(DateTime, nullable=False) # one hour per record
440 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"))
441 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="usages")
442 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
443 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
444 a47eb4c Dawsh 2y ago
445 a47eb4c Dawsh 2y ago
446 a47eb4c Dawsh 2y ago class NotificationReminder(Base):
447 a47eb4c Dawsh 2y ago __tablename__ = "notification_reminders"
448 a47eb4c Dawsh 2y ago
449 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
450 a47eb4c Dawsh 2y ago user_id = Column(Integer, ForeignKey("users.id"))
451 a47eb4c Dawsh 2y ago user = relationship("User", back_populates="notification_reminders")
452 a47eb4c Dawsh 2y ago type = Column(Enum(ReminderType), nullable=False)
453 6457378 Dawsh 2y ago expires_at = Column(DateTime)
454 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
455 0178858 Dawsh 2y ago
456 0178858 Dawsh 2y ago
457 0178858 Dawsh 2y ago class Settings(Base):
458 0178858 Dawsh 2y ago __tablename__ = "settings"
459 0178858 Dawsh 2y ago
460 0178858 Dawsh 2y ago id = Column(Integer, primary_key=True, server_default=text("0"))
461 0178858 Dawsh 2y ago subscription = Column(JSON, nullable=False)
462 0178858 Dawsh 2y ago telegram = Column(JSON)