gitscope
https://dev.kronoz.win/r/marzneshin.git
app/db/models.py · v0.3.0-alpha1
Source
1 a47eb4c Dawsh 2y ago import os
2 a47eb4c Dawsh 2y ago from datetime import datetime
3 a47eb4c Dawsh 2y ago
4 01cdf2e Dawsh 2y ago from sqlalchemy import (
5 01cdf2e Dawsh 2y ago BigInteger,
6 01cdf2e Dawsh 2y ago Boolean,
7 01cdf2e Dawsh 2y ago Column,
8 01cdf2e Dawsh 2y ago DateTime,
9 01cdf2e Dawsh 2y ago Enum,
10 01cdf2e Dawsh 2y ago Float,
11 01cdf2e Dawsh 2y ago ForeignKey,
12 01cdf2e Dawsh 2y ago Integer,
13 01cdf2e Dawsh 2y ago String,
14 01cdf2e Dawsh 2y ago Table,
15 01cdf2e Dawsh 2y ago UniqueConstraint,
16 01cdf2e Dawsh 2y ago )
17 a47eb4c Dawsh 2y ago from sqlalchemy.orm import relationship
18 a47eb4c Dawsh 2y ago from sqlalchemy.sql.expression import text
19 a47eb4c Dawsh 2y ago
20 a47eb4c Dawsh 2y ago from app.db.base import Base
21 a47eb4c Dawsh 2y ago from app.models.node import NodeStatus
22 01cdf2e Dawsh 2y ago from app.models.proxy import (
23 01cdf2e Dawsh 2y ago InboundHostALPN,
24 01cdf2e Dawsh 2y ago InboundHostFingerprint,
25 01cdf2e Dawsh 2y ago InboundHostSecurity,
26 01cdf2e Dawsh 2y ago ProxyTypes,
27 01cdf2e Dawsh 2y ago )
28 01cdf2e Dawsh 2y ago from app.models.user import ReminderType, UserDataLimitResetStrategy, UserStatus
29 a47eb4c Dawsh 2y ago
30 a47eb4c Dawsh 2y ago
31 a47eb4c Dawsh 2y ago class Admin(Base):
32 a47eb4c Dawsh 2y ago __tablename__ = "admins"
33 a47eb4c Dawsh 2y ago
34 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
35 a47eb4c Dawsh 2y ago username = Column(String(32), unique=True, index=True)
36 a47eb4c Dawsh 2y ago hashed_password = Column(String(128))
37 a47eb4c Dawsh 2y ago users = relationship("User", back_populates="admin")
38 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
39 a47eb4c Dawsh 2y ago is_sudo = Column(Boolean, default=False)
40 6457378 Dawsh 2y ago password_reset_at = Column(DateTime)
41 a47eb4c Dawsh 2y ago
42 a47eb4c Dawsh 2y ago
43 a47eb4c Dawsh 2y ago inbounds_services = Table(
44 a47eb4c Dawsh 2y ago "inbounds_services",
45 a47eb4c Dawsh 2y ago Base.metadata,
46 a47eb4c Dawsh 2y ago Column("inbound_id", ForeignKey("inbounds.id"), primary_key=True),
47 a47eb4c Dawsh 2y ago Column("service_id", ForeignKey("services.id"), primary_key=True),
48 a47eb4c Dawsh 2y ago )
49 a47eb4c Dawsh 2y ago
50 a47eb4c Dawsh 2y ago users_services = Table(
51 a47eb4c Dawsh 2y ago "users_services",
52 a47eb4c Dawsh 2y ago Base.metadata,
53 a47eb4c Dawsh 2y ago Column("user_id", ForeignKey("users.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
58 a47eb4c Dawsh 2y ago class Service(Base):
59 a47eb4c Dawsh 2y ago __tablename__ = "services"
60 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
61 6457378 Dawsh 2y ago name = Column(String(64))
62 6457378 Dawsh 2y ago # modifications = Column(String(1024))
63 a47eb4c Dawsh 2y ago users = relationship("User", secondary=users_services, back_populates="services")
64 01cdf2e Dawsh 2y ago inbounds = relationship(
65 01cdf2e Dawsh 2y ago "Inbound", secondary=inbounds_services, back_populates="services"
66 01cdf2e Dawsh 2y ago )
67 a47eb4c Dawsh 2y ago
68 a47eb4c Dawsh 2y ago
69 a47eb4c Dawsh 2y ago class User(Base):
70 a47eb4c Dawsh 2y ago __tablename__ = "users"
71 a47eb4c Dawsh 2y ago
72 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
73 6457378 Dawsh 2y ago username = Column(String(32), unique=True, index=True)
74 6457378 Dawsh 2y ago key = Column(String(64), unique=True)
75 01cdf2e Dawsh 2y ago services = relationship(
76 01cdf2e Dawsh 2y ago "Service", secondary=users_services, back_populates="users", lazy="joined"
77 01cdf2e Dawsh 2y ago )
78 12b255b Dawsh 2y ago inbounds = relationship(
79 12b255b Dawsh 2y ago "Inbound",
80 12b255b Dawsh 2y ago secondary="join(users_services, inbounds_services, inbounds_services.c.service_id == users_services.c.service_id)"
81 01cdf2e Dawsh 2y ago ".join(Inbound, Inbound.id == inbounds_services.c.inbound_id)",
82 01cdf2e Dawsh 2y ago viewonly=True,
83 12b255b Dawsh 2y ago distinct_target_key=True,
84 01cdf2e Dawsh 2y ago lazy="joined",
85 01cdf2e Dawsh 2y ago )
86 a47eb4c Dawsh 2y ago # proxies = relationship("Proxy", back_populates="user", cascade="all, delete-orphan")
87 a47eb4c Dawsh 2y ago status = Column(Enum(UserStatus), nullable=False, default=UserStatus.active)
88 a47eb4c Dawsh 2y ago used_traffic = Column(BigInteger, default=0)
89 01cdf2e Dawsh 2y ago node_usages = relationship(
90 01cdf2e Dawsh 2y ago "NodeUserUsage",
91 01cdf2e Dawsh 2y ago back_populates="user",
92 01cdf2e Dawsh 2y ago cascade="all,delete,delete-orphan",
93 01cdf2e Dawsh 2y ago lazy="joined",
94 01cdf2e Dawsh 2y ago )
95 01cdf2e Dawsh 2y ago notification_reminders = relationship(
96 01cdf2e Dawsh 2y ago "NotificationReminder",
97 01cdf2e Dawsh 2y ago back_populates="user",
98 01cdf2e Dawsh 2y ago cascade="all,delete,delete-orphan",
99 01cdf2e Dawsh 2y ago )
100 6457378 Dawsh 2y ago data_limit = Column(BigInteger)
101 a47eb4c Dawsh 2y ago data_limit_reset_strategy = Column(
102 a47eb4c Dawsh 2y ago Enum(UserDataLimitResetStrategy),
103 a47eb4c Dawsh 2y ago nullable=False,
104 a47eb4c Dawsh 2y ago default=UserDataLimitResetStrategy.no_reset,
105 a47eb4c Dawsh 2y ago )
106 a47eb4c Dawsh 2y ago ip_limit = Column(Integer, nullable=False, default=-1)
107 6457378 Dawsh 2y ago settings = Column(String)
108 01cdf2e Dawsh 2y ago usage_logs = relationship(
109 01cdf2e Dawsh 2y ago "UserUsageResetLogs", back_populates="user", lazy="joined"
110 01cdf2e Dawsh 2y ago )
111 3b7feef Mrdn 2y ago expire = Column(DateTime)
112 a47eb4c Dawsh 2y ago admin_id = Column(Integer, ForeignKey("admins.id"))
113 a47eb4c Dawsh 2y ago admin = relationship("Admin", back_populates="users")
114 6457378 Dawsh 2y ago sub_updated_at = Column(DateTime)
115 6457378 Dawsh 2y ago sub_last_user_agent = Column(String(512))
116 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
117 6457378 Dawsh 2y ago note = Column(String(500))
118 6457378 Dawsh 2y ago online_at = Column(DateTime)
119 6457378 Dawsh 2y ago on_hold_expire_duration = Column(BigInteger)
120 6457378 Dawsh 2y ago on_hold_timeout = Column(DateTime)
121 6457378 Dawsh 2y ago edit_at = Column(DateTime)
122 a47eb4c Dawsh 2y ago
123 a47eb4c Dawsh 2y ago @property
124 a47eb4c Dawsh 2y ago def lifetime_used_traffic(self):
125 a47eb4c Dawsh 2y ago return (
126 a47eb4c Dawsh 2y ago sum([log.used_traffic_at_reset for log in self.usage_logs])
127 a47eb4c Dawsh 2y ago + self.used_traffic
128 a47eb4c Dawsh 2y ago )
129 a47eb4c Dawsh 2y ago
130 a47eb4c Dawsh 2y ago @property
131 a47eb4c Dawsh 2y ago def last_traffic_reset_time(self):
132 a47eb4c Dawsh 2y ago return self.usage_logs[-1].reset_at if self.usage_logs else self.created_at
133 a47eb4c Dawsh 2y ago
134 a47eb4c Dawsh 2y ago
135 a47eb4c Dawsh 2y ago class UserUsageResetLogs(Base):
136 a47eb4c Dawsh 2y ago __tablename__ = "user_usage_logs"
137 a47eb4c Dawsh 2y ago
138 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
139 a47eb4c Dawsh 2y ago user_id = Column(Integer, ForeignKey("users.id"))
140 a47eb4c Dawsh 2y ago user = relationship("User", back_populates="usage_logs")
141 a47eb4c Dawsh 2y ago used_traffic_at_reset = Column(BigInteger, nullable=False)
142 a47eb4c Dawsh 2y ago reset_at = Column(DateTime, default=datetime.utcnow)
143 a47eb4c Dawsh 2y ago
144 a47eb4c Dawsh 2y ago
145 a47eb4c Dawsh 2y ago class Inbound(Base):
146 a47eb4c Dawsh 2y ago __tablename__ = "inbounds"
147 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("node_id", "tag"),)
148 a47eb4c Dawsh 2y ago
149 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
150 a47eb4c Dawsh 2y ago protocol = Column(Enum(ProxyTypes))
151 6457378 Dawsh 2y ago tag = Column(String(256), nullable=False)
152 4d647dd Dawsh 2y ago config = Column(String(), nullable=False)
153 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"), index=True)
154 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="inbounds")
155 01cdf2e Dawsh 2y ago services = relationship(
156 01cdf2e Dawsh 2y ago "Service", secondary=inbounds_services, back_populates="inbounds"
157 01cdf2e Dawsh 2y ago )
158 a47eb4c Dawsh 2y ago hosts = relationship(
159 a47eb4c Dawsh 2y ago "InboundHost", back_populates="inbound", cascade="all, delete, delete-orphan"
160 a47eb4c Dawsh 2y ago )
161 a47eb4c Dawsh 2y ago
162 a47eb4c Dawsh 2y ago
163 a47eb4c Dawsh 2y ago class InboundHost(Base):
164 a47eb4c Dawsh 2y ago __tablename__ = "hosts"
165 a47eb4c Dawsh 2y ago
166 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
167 6457378 Dawsh 2y ago remark = Column(String(256), nullable=False)
168 6457378 Dawsh 2y ago address = Column(String(256), nullable=False)
169 6457378 Dawsh 2y ago port = Column(Integer)
170 6457378 Dawsh 2y ago path = Column(String(256))
171 6457378 Dawsh 2y ago sni = Column(String(256))
172 6457378 Dawsh 2y ago host = Column(String(256))
173 a47eb4c Dawsh 2y ago security = Column(
174 a47eb4c Dawsh 2y ago Enum(InboundHostSecurity),
175 a47eb4c Dawsh 2y ago nullable=False,
176 a47eb4c Dawsh 2y ago default=InboundHostSecurity.inbound_default,
177 a47eb4c Dawsh 2y ago )
178 a47eb4c Dawsh 2y ago alpn = Column(
179 a47eb4c Dawsh 2y ago Enum(InboundHostALPN),
180 a47eb4c Dawsh 2y ago nullable=False,
181 a47eb4c Dawsh 2y ago default=InboundHostSecurity.none,
182 01cdf2e Dawsh 2y ago server_default=InboundHostSecurity.none.name,
183 a47eb4c Dawsh 2y ago )
184 a47eb4c Dawsh 2y ago fingerprint = Column(
185 a47eb4c Dawsh 2y ago Enum(InboundHostFingerprint),
186 a47eb4c Dawsh 2y ago nullable=False,
187 a47eb4c Dawsh 2y ago default=InboundHostSecurity.none,
188 01cdf2e Dawsh 2y ago server_default=InboundHostSecurity.none.name,
189 a47eb4c Dawsh 2y ago )
190 a47eb4c Dawsh 2y ago
191 a47eb4c Dawsh 2y ago inbound_id = Column(Integer, ForeignKey("inbounds.id"), nullable=False)
192 a47eb4c Dawsh 2y ago inbound = relationship("Inbound", back_populates="hosts")
193 6457378 Dawsh 2y ago allowinsecure = Column(Boolean, default=False)
194 6457378 Dawsh 2y ago is_disabled = Column(Boolean, default=False)
195 a47eb4c Dawsh 2y ago
196 a47eb4c Dawsh 2y ago
197 a47eb4c Dawsh 2y ago class System(Base):
198 a47eb4c Dawsh 2y ago __tablename__ = "system"
199 a47eb4c Dawsh 2y ago
200 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
201 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
202 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
203 a47eb4c Dawsh 2y ago
204 a47eb4c Dawsh 2y ago
205 a47eb4c Dawsh 2y ago class JWT(Base):
206 a47eb4c Dawsh 2y ago __tablename__ = "jwt"
207 a47eb4c Dawsh 2y ago
208 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
209 a47eb4c Dawsh 2y ago secret_key = Column(
210 a47eb4c Dawsh 2y ago String(64), nullable=False, default=lambda: os.urandom(32).hex()
211 a47eb4c Dawsh 2y ago )
212 a47eb4c Dawsh 2y ago
213 a47eb4c Dawsh 2y ago
214 a47eb4c Dawsh 2y ago class TLS(Base):
215 a47eb4c Dawsh 2y ago __tablename__ = "tls"
216 a47eb4c Dawsh 2y ago
217 a47eb4c Dawsh 2y ago id = Column(Integer, primary_key=True)
218 a47eb4c Dawsh 2y ago key = Column(String(4096), nullable=False)
219 a47eb4c Dawsh 2y ago certificate = Column(String(2048), nullable=False)
220 a47eb4c Dawsh 2y ago
221 a47eb4c Dawsh 2y ago
222 a47eb4c Dawsh 2y ago class Node(Base):
223 a47eb4c Dawsh 2y ago __tablename__ = "nodes"
224 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("address", "port"),)
225 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
226 6457378 Dawsh 2y ago name = Column(String(256), unique=True)
227 f6a32ea Dawsh 2y ago connection_backend = Column(String())
228 6457378 Dawsh 2y ago address = Column(String(256))
229 6457378 Dawsh 2y ago port = Column(Integer)
230 6457378 Dawsh 2y ago xray_version = Column(String(32))
231 6457378 Dawsh 2y ago inbounds = relationship("Inbound", back_populates="node", cascade="all, delete")
232 12b255b Dawsh 2y ago status = Column(Enum(NodeStatus), nullable=False, default=NodeStatus.unhealthy)
233 a47eb4c Dawsh 2y ago last_status_change = Column(DateTime, default=datetime.utcnow)
234 6457378 Dawsh 2y ago message = Column(String(1024))
235 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)
236 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
237 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
238 01cdf2e Dawsh 2y ago user_usages = relationship(
239 01cdf2e Dawsh 2y ago "NodeUserUsage", back_populates="node", cascade="all, delete, delete-orphan"
240 01cdf2e Dawsh 2y ago )
241 01cdf2e Dawsh 2y ago usages = relationship(
242 01cdf2e Dawsh 2y ago "NodeUsage", back_populates="node", cascade="all, delete, delete-orphan"
243 01cdf2e Dawsh 2y ago )
244 01cdf2e Dawsh 2y ago usage_coefficient = Column(
245 01cdf2e Dawsh 2y ago Float, nullable=False, server_default=text("1.0"), default=1
246 01cdf2e Dawsh 2y ago )
247 a47eb4c Dawsh 2y ago
248 a47eb4c Dawsh 2y ago
249 a47eb4c Dawsh 2y ago class NodeUserUsage(Base):
250 a47eb4c Dawsh 2y ago __tablename__ = "node_user_usages"
251 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("created_at", "user_id", "node_id"),)
252 a47eb4c Dawsh 2y ago
253 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
254 6457378 Dawsh 2y ago created_at = Column(DateTime, nullable=False) # one hour per record
255 a47eb4c Dawsh 2y ago user_id = Column(Integer, ForeignKey("users.id"))
256 a47eb4c Dawsh 2y ago user = relationship("User", back_populates="node_usages")
257 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"))
258 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="user_usages")
259 a47eb4c Dawsh 2y ago used_traffic = Column(BigInteger, default=0)
260 a47eb4c Dawsh 2y ago
261 a47eb4c Dawsh 2y ago
262 a47eb4c Dawsh 2y ago class NodeUsage(Base):
263 a47eb4c Dawsh 2y ago __tablename__ = "node_usages"
264 01cdf2e Dawsh 2y ago __table_args__ = (UniqueConstraint("created_at", "node_id"),)
265 a47eb4c Dawsh 2y ago
266 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
267 6457378 Dawsh 2y ago created_at = Column(DateTime, nullable=False) # one hour per record
268 a47eb4c Dawsh 2y ago node_id = Column(Integer, ForeignKey("nodes.id"))
269 a47eb4c Dawsh 2y ago node = relationship("Node", back_populates="usages")
270 a47eb4c Dawsh 2y ago uplink = Column(BigInteger, default=0)
271 a47eb4c Dawsh 2y ago downlink = Column(BigInteger, default=0)
272 a47eb4c Dawsh 2y ago
273 a47eb4c Dawsh 2y ago
274 a47eb4c Dawsh 2y ago class NotificationReminder(Base):
275 a47eb4c Dawsh 2y ago __tablename__ = "notification_reminders"
276 a47eb4c Dawsh 2y ago
277 6457378 Dawsh 2y ago id = Column(Integer, primary_key=True)
278 a47eb4c Dawsh 2y ago user_id = Column(Integer, ForeignKey("users.id"))
279 a47eb4c Dawsh 2y ago user = relationship("User", back_populates="notification_reminders")
280 a47eb4c Dawsh 2y ago type = Column(Enum(ReminderType), nullable=False)
281 6457378 Dawsh 2y ago expires_at = Column(DateTime)
282 a47eb4c Dawsh 2y ago created_at = Column(DateTime, default=datetime.utcnow)