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