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