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