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