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