| 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 |
4056c66 |
Dawsh |
1y ago |
from sqlalchemy.ext.orderinglist import ordering_list |
| 26 |
4708fcb |
Dawsh |
2y ago |
from sqlalchemy.orm import relationship, column_property |
| 27 |
a47eb4c |
Dawsh |
2y ago |
from sqlalchemy.sql.expression import text |
| 28 |
a47eb4c |
Dawsh |
2y ago |
|
| 29 |
4708fcb |
Dawsh |
2y ago |
from app.config.env import SUBSCRIPTION_URL_PREFIX |
| 30 |
a47eb4c |
Dawsh |
2y ago |
from app.db.base import Base |
| 31 |
a47eb4c |
Dawsh |
2y ago |
from app.models.node import NodeStatus |
| 32 |
01cdf2e |
Dawsh |
2y ago |
from app.models.proxy import ( |
| 33 |
01cdf2e |
Dawsh |
2y ago |
InboundHostFingerprint, |
| 34 |
01cdf2e |
Dawsh |
2y ago |
InboundHostSecurity, |
| 35 |
01cdf2e |
Dawsh |
2y ago |
ProxyTypes, |
| 36 |
01cdf2e |
Dawsh |
2y ago |
) |
| 37 |
f1ab16c |
Dawsh |
2y ago |
from app.models.user import ( |
| 38 |
d377c4c |
Dawsh |
2y ago |
UserDataUsageResetStrategy, |
| 39 |
f1ab16c |
Dawsh |
2y ago |
UserStatus, |
| 40 |
d377c4c |
Dawsh |
2y ago |
UserExpireStrategy, |
| 41 |
f1ab16c |
Dawsh |
2y ago |
) |
| 42 |
a47eb4c |
Dawsh |
2y ago |
|
| 43 |
4708fcb |
Dawsh |
2y ago |
admins_services = Table( |
| 44 |
4708fcb |
Dawsh |
2y ago |
"admins_services", |
| 45 |
4708fcb |
Dawsh |
2y ago |
Base.metadata, |
| 46 |
4708fcb |
Dawsh |
2y ago |
Column("admin_id", ForeignKey("admins.id"), primary_key=True), |
| 47 |
4708fcb |
Dawsh |
2y ago |
Column("service_id", ForeignKey("services.id"), primary_key=True), |
| 48 |
4708fcb |
Dawsh |
2y ago |
) |
| 49 |
a47eb4c |
Dawsh |
2y ago |
|
| 50 |
a47eb4c |
Dawsh |
2y ago |
inbounds_services = Table( |
| 51 |
a47eb4c |
Dawsh |
2y ago |
"inbounds_services", |
| 52 |
a47eb4c |
Dawsh |
2y ago |
Base.metadata, |
| 53 |
a47eb4c |
Dawsh |
2y ago |
Column("inbound_id", ForeignKey("inbounds.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 |
users_services = Table( |
| 58 |
a47eb4c |
Dawsh |
2y ago |
"users_services", |
| 59 |
a47eb4c |
Dawsh |
2y ago |
Base.metadata, |
| 60 |
a47eb4c |
Dawsh |
2y ago |
Column("user_id", ForeignKey("users.id"), primary_key=True), |
| 61 |
a47eb4c |
Dawsh |
2y ago |
Column("service_id", ForeignKey("services.id"), primary_key=True), |
| 62 |
a47eb4c |
Dawsh |
2y ago |
) |
| 63 |
a47eb4c |
Dawsh |
2y ago |
|
| 64 |
4056c66 |
Dawsh |
1y ago |
hosts_services = Table( |
| 65 |
4056c66 |
Dawsh |
1y ago |
"hosts_services", |
| 66 |
4056c66 |
Dawsh |
1y ago |
Base.metadata, |
| 67 |
4056c66 |
Dawsh |
1y ago |
Column("host_id", ForeignKey("hosts.id"), primary_key=True), |
| 68 |
4056c66 |
Dawsh |
1y ago |
Column("service_id", ForeignKey("services.id"), primary_key=True), |
| 69 |
4056c66 |
Dawsh |
1y ago |
) |
| 70 |
4056c66 |
Dawsh |
1y ago |
|
| 71 |
a47eb4c |
Dawsh |
2y ago |
|
| 72 |
4708fcb |
Dawsh |
2y ago |
class Admin(Base): |
| 73 |
4708fcb |
Dawsh |
2y ago |
__tablename__ = "admins" |
| 74 |
4708fcb |
Dawsh |
2y ago |
|
| 75 |
4708fcb |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True) |
| 76 |
4708fcb |
Dawsh |
2y ago |
username = Column(String(32), unique=True, index=True) |
| 77 |
4708fcb |
Dawsh |
2y ago |
hashed_password = Column(String(128)) |
| 78 |
4708fcb |
Dawsh |
2y ago |
users = relationship("User", back_populates="admin") |
| 79 |
4708fcb |
Dawsh |
2y ago |
services = relationship( |
| 80 |
4708fcb |
Dawsh |
2y ago |
"Service", |
| 81 |
4708fcb |
Dawsh |
2y ago |
secondary=admins_services, |
| 82 |
4708fcb |
Dawsh |
2y ago |
back_populates="admins", |
| 83 |
4708fcb |
Dawsh |
2y ago |
lazy="joined", |
| 84 |
4708fcb |
Dawsh |
2y ago |
) |
| 85 |
4708fcb |
Dawsh |
2y ago |
enabled = Column( |
| 86 |
4708fcb |
Dawsh |
2y ago |
Boolean, |
| 87 |
4708fcb |
Dawsh |
2y ago |
nullable=False, |
| 88 |
4708fcb |
Dawsh |
2y ago |
default=True, |
| 89 |
4708fcb |
Dawsh |
2y ago |
server_default=sqlalchemy.sql.true(), |
| 90 |
4708fcb |
Dawsh |
2y ago |
) |
| 91 |
4708fcb |
Dawsh |
2y ago |
all_services_access = Column( |
| 92 |
4708fcb |
Dawsh |
2y ago |
Boolean, |
| 93 |
4708fcb |
Dawsh |
2y ago |
nullable=False, |
| 94 |
4708fcb |
Dawsh |
2y ago |
default=False, |
| 95 |
4708fcb |
Dawsh |
2y ago |
server_default=sqlalchemy.sql.false(), |
| 96 |
4708fcb |
Dawsh |
2y ago |
) |
| 97 |
4708fcb |
Dawsh |
2y ago |
modify_users_access = Column( |
| 98 |
4708fcb |
Dawsh |
2y ago |
Boolean, |
| 99 |
4708fcb |
Dawsh |
2y ago |
nullable=False, |
| 100 |
4708fcb |
Dawsh |
2y ago |
default=True, |
| 101 |
4708fcb |
Dawsh |
2y ago |
server_default=sqlalchemy.sql.true(), |
| 102 |
4708fcb |
Dawsh |
2y ago |
) |
| 103 |
4708fcb |
Dawsh |
2y ago |
created_at = Column(DateTime, default=datetime.utcnow) |
| 104 |
4708fcb |
Dawsh |
2y ago |
is_sudo = Column(Boolean, default=False) |
| 105 |
4708fcb |
Dawsh |
2y ago |
password_reset_at = Column(DateTime) |
| 106 |
4708fcb |
Dawsh |
2y ago |
subscription_url_prefix = Column( |
| 107 |
4708fcb |
Dawsh |
2y ago |
String(256), |
| 108 |
4708fcb |
Dawsh |
2y ago |
nullable=False, |
| 109 |
4708fcb |
Dawsh |
2y ago |
default="", |
| 110 |
4708fcb |
Dawsh |
2y ago |
server_default=sqlalchemy.sql.text(""), |
| 111 |
4708fcb |
Dawsh |
2y ago |
) |
| 112 |
4708fcb |
Dawsh |
2y ago |
|
| 113 |
4708fcb |
Dawsh |
2y ago |
@property |
| 114 |
4708fcb |
Dawsh |
2y ago |
def service_ids(self): |
| 115 |
4708fcb |
Dawsh |
2y ago |
return [service.id for service in self.services] |
| 116 |
4708fcb |
Dawsh |
2y ago |
|
| 117 |
4708fcb |
Dawsh |
2y ago |
@classmethod |
| 118 |
4708fcb |
Dawsh |
2y ago |
def __declare_last__(cls): |
| 119 |
4708fcb |
Dawsh |
2y ago |
cls.users_data_usage = column_property( |
| 120 |
4708fcb |
Dawsh |
2y ago |
select(func.coalesce(func.sum(User.lifetime_used_traffic), 0)) |
| 121 |
4708fcb |
Dawsh |
2y ago |
.where(User.admin_id == cls.id) |
| 122 |
4708fcb |
Dawsh |
2y ago |
.correlate_except(User) |
| 123 |
4708fcb |
Dawsh |
2y ago |
.scalar_subquery() |
| 124 |
4708fcb |
Dawsh |
2y ago |
) |
| 125 |
4708fcb |
Dawsh |
2y ago |
|
| 126 |
4708fcb |
Dawsh |
2y ago |
|
| 127 |
a47eb4c |
Dawsh |
2y ago |
class Service(Base): |
| 128 |
a47eb4c |
Dawsh |
2y ago |
__tablename__ = "services" |
| 129 |
6457378 |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True) |
| 130 |
6457378 |
Dawsh |
2y ago |
name = Column(String(64)) |
| 131 |
4708fcb |
Dawsh |
2y ago |
admins = relationship( |
| 132 |
4708fcb |
Dawsh |
2y ago |
"Admin", secondary=admins_services, back_populates="services" |
| 133 |
4708fcb |
Dawsh |
2y ago |
) |
| 134 |
f1ab16c |
Dawsh |
2y ago |
users = relationship( |
| 135 |
f1ab16c |
Dawsh |
2y ago |
"User", secondary=users_services, back_populates="services" |
| 136 |
f1ab16c |
Dawsh |
2y ago |
) |
| 137 |
01cdf2e |
Dawsh |
2y ago |
inbounds = relationship( |
| 138 |
01cdf2e |
Dawsh |
2y ago |
"Inbound", secondary=inbounds_services, back_populates="services" |
| 139 |
01cdf2e |
Dawsh |
2y ago |
) |
| 140 |
a47eb4c |
Dawsh |
2y ago |
|
| 141 |
db68a58 |
Dawsh |
2y ago |
@property |
| 142 |
db68a58 |
Dawsh |
2y ago |
def inbound_ids(self): |
| 143 |
db68a58 |
Dawsh |
2y ago |
return [inbound.id for inbound in self.inbounds] |
| 144 |
db68a58 |
Dawsh |
2y ago |
|
| 145 |
db68a58 |
Dawsh |
2y ago |
@property |
| 146 |
db68a58 |
Dawsh |
2y ago |
def user_ids(self): |
| 147 |
c5be81c |
Erfan |
1y ago |
return [user.id for user in self.users if not user.removed] |
| 148 |
db68a58 |
Dawsh |
2y ago |
|
| 149 |
a47eb4c |
Dawsh |
2y ago |
|
| 150 |
a47eb4c |
Dawsh |
2y ago |
class User(Base): |
| 151 |
a47eb4c |
Dawsh |
2y ago |
__tablename__ = "users" |
| 152 |
a47eb4c |
Dawsh |
2y ago |
|
| 153 |
6457378 |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True) |
| 154 |
6457378 |
Dawsh |
2y ago |
username = Column(String(32), unique=True, index=True) |
| 155 |
6457378 |
Dawsh |
2y ago |
key = Column(String(64), unique=True) |
| 156 |
d377c4c |
Dawsh |
2y ago |
activated = Column(Boolean, nullable=False, default=True) |
| 157 |
cba9caa |
Dawsh |
2y ago |
enabled = Column( |
| 158 |
f1ab16c |
Dawsh |
2y ago |
Boolean, |
| 159 |
f1ab16c |
Dawsh |
2y ago |
nullable=False, |
| 160 |
f1ab16c |
Dawsh |
2y ago |
default=True, |
| 161 |
f1ab16c |
Dawsh |
2y ago |
server_default=sqlalchemy.sql.true(), |
| 162 |
cba9caa |
Dawsh |
2y ago |
) |
| 163 |
24826a2 |
Dawsh |
2y ago |
removed = Column( |
| 164 |
24826a2 |
Dawsh |
2y ago |
Boolean, |
| 165 |
24826a2 |
Dawsh |
2y ago |
nullable=False, |
| 166 |
24826a2 |
Dawsh |
2y ago |
default=False, |
| 167 |
24826a2 |
Dawsh |
2y ago |
server_default=sqlalchemy.sql.false(), |
| 168 |
24826a2 |
Dawsh |
2y ago |
) |
| 169 |
01cdf2e |
Dawsh |
2y ago |
services = relationship( |
| 170 |
f1ab16c |
Dawsh |
2y ago |
"Service", |
| 171 |
f1ab16c |
Dawsh |
2y ago |
secondary=users_services, |
| 172 |
f1ab16c |
Dawsh |
2y ago |
back_populates="users", |
| 173 |
f1ab16c |
Dawsh |
2y ago |
lazy="joined", |
| 174 |
01cdf2e |
Dawsh |
2y ago |
) |
| 175 |
12b255b |
Dawsh |
2y ago |
inbounds = relationship( |
| 176 |
12b255b |
Dawsh |
2y ago |
"Inbound", |
| 177 |
12b255b |
Dawsh |
2y ago |
secondary="join(users_services, inbounds_services, inbounds_services.c.service_id == users_services.c.service_id)" |
| 178 |
01cdf2e |
Dawsh |
2y ago |
".join(Inbound, Inbound.id == inbounds_services.c.inbound_id)", |
| 179 |
01cdf2e |
Dawsh |
2y ago |
viewonly=True, |
| 180 |
12b255b |
Dawsh |
2y ago |
distinct_target_key=True, |
| 181 |
01cdf2e |
Dawsh |
2y ago |
) |
| 182 |
a47eb4c |
Dawsh |
2y ago |
used_traffic = Column(BigInteger, default=0) |
| 183 |
8ee2242 |
Dawsh |
2y ago |
lifetime_used_traffic = Column( |
| 184 |
8ee2242 |
Dawsh |
2y ago |
BigInteger, default=0, server_default="0", nullable=False |
| 185 |
8ee2242 |
Dawsh |
2y ago |
) |
| 186 |
42a1073 |
Dawsh |
2y ago |
traffic_reset_at = Column(DateTime) |
| 187 |
01cdf2e |
Dawsh |
2y ago |
node_usages = relationship( |
| 188 |
01cdf2e |
Dawsh |
2y ago |
"NodeUserUsage", |
| 189 |
01cdf2e |
Dawsh |
2y ago |
back_populates="user", |
| 190 |
01cdf2e |
Dawsh |
2y ago |
cascade="all,delete,delete-orphan", |
| 191 |
01cdf2e |
Dawsh |
2y ago |
) |
| 192 |
6457378 |
Dawsh |
2y ago |
data_limit = Column(BigInteger) |
| 193 |
a47eb4c |
Dawsh |
2y ago |
data_limit_reset_strategy = Column( |
| 194 |
d377c4c |
Dawsh |
2y ago |
Enum(UserDataUsageResetStrategy), |
| 195 |
a47eb4c |
Dawsh |
2y ago |
nullable=False, |
| 196 |
d377c4c |
Dawsh |
2y ago |
default=UserDataUsageResetStrategy.no_reset, |
| 197 |
a47eb4c |
Dawsh |
2y ago |
) |
| 198 |
a47eb4c |
Dawsh |
2y ago |
ip_limit = Column(Integer, nullable=False, default=-1) |
| 199 |
010b05d |
Dawsh |
2y ago |
settings = Column(String(1024)) |
| 200 |
d377c4c |
Dawsh |
2y ago |
expire_strategy = Column( |
| 201 |
d377c4c |
Dawsh |
2y ago |
Enum(UserExpireStrategy), |
| 202 |
d377c4c |
Dawsh |
2y ago |
nullable=False, |
| 203 |
d377c4c |
Dawsh |
2y ago |
default=UserExpireStrategy.NEVER, |
| 204 |
d377c4c |
Dawsh |
2y ago |
) |
| 205 |
d377c4c |
Dawsh |
2y ago |
expire_date = Column(DateTime) |
| 206 |
d377c4c |
Dawsh |
2y ago |
usage_duration = Column(BigInteger) |
| 207 |
d377c4c |
Dawsh |
2y ago |
activation_deadline = Column(DateTime) |
| 208 |
a47eb4c |
Dawsh |
2y ago |
admin_id = Column(Integer, ForeignKey("admins.id")) |
| 209 |
a47eb4c |
Dawsh |
2y ago |
admin = relationship("Admin", back_populates="users") |
| 210 |
6457378 |
Dawsh |
2y ago |
sub_updated_at = Column(DateTime) |
| 211 |
6457378 |
Dawsh |
2y ago |
sub_last_user_agent = Column(String(512)) |
| 212 |
00d6be5 |
Erfan |
2y ago |
sub_revoked_at = Column(DateTime) |
| 213 |
a47eb4c |
Dawsh |
2y ago |
created_at = Column(DateTime, default=datetime.utcnow) |
| 214 |
6457378 |
Dawsh |
2y ago |
note = Column(String(500)) |
| 215 |
6457378 |
Dawsh |
2y ago |
online_at = Column(DateTime) |
| 216 |
6457378 |
Dawsh |
2y ago |
edit_at = Column(DateTime) |
| 217 |
a47eb4c |
Dawsh |
2y ago |
|
| 218 |
db68a58 |
Dawsh |
2y ago |
@property |
| 219 |
db68a58 |
Dawsh |
2y ago |
def service_ids(self): |
| 220 |
db68a58 |
Dawsh |
2y ago |
return [service.id for service in self.services] |
| 221 |
db68a58 |
Dawsh |
2y ago |
|
| 222 |
d377c4c |
Dawsh |
2y ago |
@hybrid_property |
| 223 |
d377c4c |
Dawsh |
2y ago |
def expired(self): |
| 224 |
d377c4c |
Dawsh |
2y ago |
if self.expire_strategy == "fixed_date": |
| 225 |
d377c4c |
Dawsh |
2y ago |
return self.expire_date < datetime.utcnow() |
| 226 |
d377c4c |
Dawsh |
2y ago |
return False |
| 227 |
d377c4c |
Dawsh |
2y ago |
|
| 228 |
d377c4c |
Dawsh |
2y ago |
@expired.expression |
| 229 |
d377c4c |
Dawsh |
2y ago |
def expired(cls): |
| 230 |
d377c4c |
Dawsh |
2y ago |
return and_( |
| 231 |
d377c4c |
Dawsh |
2y ago |
cls.expire_strategy == "fixed_date", cls.expire_date < func.now() |
| 232 |
d377c4c |
Dawsh |
2y ago |
) |
| 233 |
d377c4c |
Dawsh |
2y ago |
|
| 234 |
d377c4c |
Dawsh |
2y ago |
@hybrid_property |
| 235 |
d377c4c |
Dawsh |
2y ago |
def data_limit_reached(self): |
| 236 |
d377c4c |
Dawsh |
2y ago |
if self.data_limit is not None: |
| 237 |
d377c4c |
Dawsh |
2y ago |
return self.used_traffic >= self.data_limit |
| 238 |
d377c4c |
Dawsh |
2y ago |
return False |
| 239 |
d377c4c |
Dawsh |
2y ago |
|
| 240 |
d377c4c |
Dawsh |
2y ago |
@data_limit_reached.expression |
| 241 |
d377c4c |
Dawsh |
2y ago |
def data_limit_reached(cls): |
| 242 |
d377c4c |
Dawsh |
2y ago |
return and_( |
| 243 |
d377c4c |
Dawsh |
2y ago |
cls.data_limit.isnot(None), cls.used_traffic >= cls.data_limit |
| 244 |
d377c4c |
Dawsh |
2y ago |
) |
| 245 |
d377c4c |
Dawsh |
2y ago |
|
| 246 |
d377c4c |
Dawsh |
2y ago |
@hybrid_property |
| 247 |
d377c4c |
Dawsh |
2y ago |
def is_active(self): |
| 248 |
d377c4c |
Dawsh |
2y ago |
return ( |
| 249 |
24826a2 |
Dawsh |
2y ago |
self.enabled |
| 250 |
24826a2 |
Dawsh |
2y ago |
and not self.expired |
| 251 |
24826a2 |
Dawsh |
2y ago |
and not self.data_limit_reached |
| 252 |
24826a2 |
Dawsh |
2y ago |
and not self.removed |
| 253 |
d377c4c |
Dawsh |
2y ago |
) |
| 254 |
d377c4c |
Dawsh |
2y ago |
|
| 255 |
d377c4c |
Dawsh |
2y ago |
@is_active.expression |
| 256 |
d377c4c |
Dawsh |
2y ago |
def is_active(cls): |
| 257 |
24826a2 |
Dawsh |
2y ago |
return and_( |
| 258 |
24826a2 |
Dawsh |
2y ago |
cls.enabled == True, |
| 259 |
24826a2 |
Dawsh |
2y ago |
~cls.expired, |
| 260 |
24826a2 |
Dawsh |
2y ago |
~cls.data_limit_reached, |
| 261 |
24826a2 |
Dawsh |
2y ago |
~cls.removed, |
| 262 |
24826a2 |
Dawsh |
2y ago |
) |
| 263 |
d377c4c |
Dawsh |
2y ago |
|
| 264 |
d377c4c |
Dawsh |
2y ago |
@property |
| 265 |
d377c4c |
Dawsh |
2y ago |
def status(self): |
| 266 |
d377c4c |
Dawsh |
2y ago |
return UserStatus.ACTIVE if self.is_active else UserStatus.INACTIVE |
| 267 |
d377c4c |
Dawsh |
2y ago |
|
| 268 |
4708fcb |
Dawsh |
2y ago |
@property |
| 269 |
4708fcb |
Dawsh |
2y ago |
def subscription_url(self): |
| 270 |
b4ec790 |
Dawsh |
2y ago |
prefix = ( |
| 271 |
b4ec790 |
Dawsh |
2y ago |
self.admin.subscription_url_prefix if self.admin else None |
| 272 |
b4ec790 |
Dawsh |
2y ago |
) or SUBSCRIPTION_URL_PREFIX |
| 273 |
4708fcb |
Dawsh |
2y ago |
return ( |
| 274 |
4708fcb |
Dawsh |
2y ago |
prefix.replace("*", secrets.token_hex(8)) |
| 275 |
4708fcb |
Dawsh |
2y ago |
+ f"/sub/{self.username}/{self.key}" |
| 276 |
4708fcb |
Dawsh |
2y ago |
) |
| 277 |
4708fcb |
Dawsh |
2y ago |
|
| 278 |
d0daa61 |
Mardin |
2y ago |
@hybrid_property |
| 279 |
d0daa61 |
Mardin |
2y ago |
def owner_username(self): |
| 280 |
b4ec790 |
Dawsh |
2y ago |
return self.admin.username if self.admin else None |
| 281 |
d0daa61 |
Mardin |
2y ago |
|
| 282 |
a47eb4c |
Dawsh |
2y ago |
|
| 283 |
f5c168d |
Mardin |
2y ago |
class Backend(Base): |
| 284 |
f5c168d |
Mardin |
2y ago |
__tablename__ = "backends" |
| 285 |
f5c168d |
Mardin |
2y ago |
|
| 286 |
f5c168d |
Mardin |
2y ago |
id = Column(Integer, primary_key=True) |
| 287 |
f5c168d |
Mardin |
2y ago |
name = Column(String(64), nullable=False) |
| 288 |
f5c168d |
Mardin |
2y ago |
node_id = Column(Integer, ForeignKey("nodes.id"), index=True) |
| 289 |
f5c168d |
Mardin |
2y ago |
node = relationship("Node", back_populates="backends") |
| 290 |
f5c168d |
Mardin |
2y ago |
backend_type = Column(String(32), nullable=False) |
| 291 |
f5c168d |
Mardin |
2y ago |
version = Column(String(32)) |
| 292 |
f5c168d |
Mardin |
2y ago |
running = Column(Boolean, default=True, nullable=False) |
| 293 |
f5c168d |
Mardin |
2y ago |
|
| 294 |
f5c168d |
Mardin |
2y ago |
|
| 295 |
a47eb4c |
Dawsh |
2y ago |
class Inbound(Base): |
| 296 |
a47eb4c |
Dawsh |
2y ago |
__tablename__ = "inbounds" |
| 297 |
01cdf2e |
Dawsh |
2y ago |
__table_args__ = (UniqueConstraint("node_id", "tag"),) |
| 298 |
a47eb4c |
Dawsh |
2y ago |
|
| 299 |
a47eb4c |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True) |
| 300 |
a47eb4c |
Dawsh |
2y ago |
protocol = Column(Enum(ProxyTypes)) |
| 301 |
6457378 |
Dawsh |
2y ago |
tag = Column(String(256), nullable=False) |
| 302 |
010b05d |
Dawsh |
2y ago |
config = Column(String(512), nullable=False) |
| 303 |
a47eb4c |
Dawsh |
2y ago |
node_id = Column(Integer, ForeignKey("nodes.id"), index=True) |
| 304 |
a47eb4c |
Dawsh |
2y ago |
node = relationship("Node", back_populates="inbounds") |
| 305 |
01cdf2e |
Dawsh |
2y ago |
services = relationship( |
| 306 |
01cdf2e |
Dawsh |
2y ago |
"Service", secondary=inbounds_services, back_populates="inbounds" |
| 307 |
01cdf2e |
Dawsh |
2y ago |
) |
| 308 |
a47eb4c |
Dawsh |
2y ago |
hosts = relationship( |
| 309 |
f1ab16c |
Dawsh |
2y ago |
"InboundHost", |
| 310 |
f1ab16c |
Dawsh |
2y ago |
back_populates="inbound", |
| 311 |
f1ab16c |
Dawsh |
2y ago |
cascade="all, delete, delete-orphan", |
| 312 |
a47eb4c |
Dawsh |
2y ago |
) |
| 313 |
a47eb4c |
Dawsh |
2y ago |
|
| 314 |
db68a58 |
Dawsh |
2y ago |
@property |
| 315 |
db68a58 |
Dawsh |
2y ago |
def service_ids(self): |
| 316 |
db68a58 |
Dawsh |
2y ago |
return [service.id for service in self.services] |
| 317 |
db68a58 |
Dawsh |
2y ago |
|
| 318 |
a47eb4c |
Dawsh |
2y ago |
|
| 319 |
4056c66 |
Dawsh |
1y ago |
class HostChain(Base): |
| 320 |
4056c66 |
Dawsh |
1y ago |
__tablename__ = "host_chains" |
| 321 |
4056c66 |
Dawsh |
1y ago |
|
| 322 |
4056c66 |
Dawsh |
1y ago |
host_id = Column(Integer, ForeignKey("hosts.id"), primary_key=True) |
| 323 |
4056c66 |
Dawsh |
1y ago |
chained_host_id = Column(Integer, ForeignKey("hosts.id")) |
| 324 |
4056c66 |
Dawsh |
1y ago |
seq = Column(Integer, primary_key=True) |
| 325 |
4056c66 |
Dawsh |
1y ago |
|
| 326 |
4056c66 |
Dawsh |
1y ago |
host = relationship( |
| 327 |
4056c66 |
Dawsh |
1y ago |
"InboundHost", foreign_keys=[host_id], back_populates="chain" |
| 328 |
4056c66 |
Dawsh |
1y ago |
) |
| 329 |
4056c66 |
Dawsh |
1y ago |
chained_host = relationship( |
| 330 |
4056c66 |
Dawsh |
1y ago |
"InboundHost", |
| 331 |
4056c66 |
Dawsh |
1y ago |
foreign_keys=[chained_host_id], |
| 332 |
4056c66 |
Dawsh |
1y ago |
lazy="joined", |
| 333 |
4056c66 |
Dawsh |
1y ago |
) |
| 334 |
4056c66 |
Dawsh |
1y ago |
|
| 335 |
4056c66 |
Dawsh |
1y ago |
|
| 336 |
a47eb4c |
Dawsh |
2y ago |
class InboundHost(Base): |
| 337 |
a47eb4c |
Dawsh |
2y ago |
__tablename__ = "hosts" |
| 338 |
a47eb4c |
Dawsh |
2y ago |
|
| 339 |
a47eb4c |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True) |
| 340 |
6457378 |
Dawsh |
2y ago |
remark = Column(String(256), nullable=False) |
| 341 |
6457378 |
Dawsh |
2y ago |
address = Column(String(256), nullable=False) |
| 342 |
4056c66 |
Dawsh |
1y ago |
host_protocol = Column(String(32)) |
| 343 |
4056c66 |
Dawsh |
1y ago |
host_network = Column(String(32)) |
| 344 |
4056c66 |
Dawsh |
1y ago |
uuid = Column(String(36)) |
| 345 |
4056c66 |
Dawsh |
1y ago |
password = Column(String(128)) |
| 346 |
6457378 |
Dawsh |
2y ago |
port = Column(Integer) |
| 347 |
6457378 |
Dawsh |
2y ago |
path = Column(String(256)) |
| 348 |
1fde8fd |
Dawsh |
2y ago |
sni = Column(String(1024)) |
| 349 |
1fde8fd |
Dawsh |
2y ago |
host = Column(String(1024)) |
| 350 |
a47eb4c |
Dawsh |
2y ago |
security = Column( |
| 351 |
a47eb4c |
Dawsh |
2y ago |
Enum(InboundHostSecurity), |
| 352 |
a47eb4c |
Dawsh |
2y ago |
nullable=False, |
| 353 |
a47eb4c |
Dawsh |
2y ago |
default=InboundHostSecurity.inbound_default, |
| 354 |
a47eb4c |
Dawsh |
2y ago |
) |
| 355 |
a47eb4c |
Dawsh |
2y ago |
alpn = Column( |
| 356 |
244c9d8 |
Dawsh |
2y ago |
String(32), |
| 357 |
244c9d8 |
Dawsh |
2y ago |
server_default=sqlalchemy.sql.null(), |
| 358 |
a47eb4c |
Dawsh |
2y ago |
) |
| 359 |
a47eb4c |
Dawsh |
2y ago |
fingerprint = Column( |
| 360 |
a47eb4c |
Dawsh |
2y ago |
Enum(InboundHostFingerprint), |
| 361 |
a47eb4c |
Dawsh |
2y ago |
nullable=False, |
| 362 |
a47eb4c |
Dawsh |
2y ago |
default=InboundHostSecurity.none, |
| 363 |
01cdf2e |
Dawsh |
2y ago |
server_default=InboundHostSecurity.none.name, |
| 364 |
a47eb4c |
Dawsh |
2y ago |
) |
| 365 |
a47eb4c |
Dawsh |
2y ago |
|
| 366 |
0b152b5 |
Mrdn |
2y ago |
fragment = Column(JSON()) |
| 367 |
d636f3f |
Dawsh |
2y ago |
udp_noises = Column(JSON()) |
| 368 |
d636f3f |
Dawsh |
2y ago |
http_headers = Column(JSON()) |
| 369 |
d636f3f |
Dawsh |
2y ago |
dns_servers = Column(String(128)) |
| 370 |
d636f3f |
Dawsh |
2y ago |
mtu = Column(Integer) |
| 371 |
31462ae |
Dawsh |
1y ago |
allowed_ips = Column(Text()) |
| 372 |
4056c66 |
Dawsh |
1y ago |
header_type = Column(String(32)) |
| 373 |
4056c66 |
Dawsh |
1y ago |
reality_public_key = Column(String(128)) |
| 374 |
4056c66 |
Dawsh |
1y ago |
reality_short_ids = Column(JSON()) |
| 375 |
4056c66 |
Dawsh |
1y ago |
flow = Column(String(32)) |
| 376 |
4056c66 |
Dawsh |
1y ago |
shadowtls_version = Column(Integer) |
| 377 |
4056c66 |
Dawsh |
1y ago |
shadowsocks_method = Column(String(32)) |
| 378 |
4056c66 |
Dawsh |
1y ago |
splithttp_settings = Column(JSON()) |
| 379 |
4056c66 |
Dawsh |
1y ago |
mux_settings = Column(JSON()) |
| 380 |
4056c66 |
Dawsh |
1y ago |
early_data = Column(Integer) |
| 381 |
4056c66 |
Dawsh |
1y ago |
inbound_id = Column(Integer, ForeignKey("inbounds.id"), nullable=True) |
| 382 |
d636f3f |
Dawsh |
2y ago |
inbound = relationship("Inbound", back_populates="hosts", lazy="joined") |
| 383 |
6457378 |
Dawsh |
2y ago |
allowinsecure = Column(Boolean, default=False) |
| 384 |
6457378 |
Dawsh |
2y ago |
is_disabled = Column(Boolean, default=False) |
| 385 |
e422d72 |
Dawsh |
2y ago |
weight = Column(Integer, default=1, nullable=False, server_default="1") |
| 386 |
a47eb4c |
Dawsh |
2y ago |
|
| 387 |
4056c66 |
Dawsh |
1y ago |
universal = Column( |
| 388 |
4056c66 |
Dawsh |
1y ago |
Boolean, |
| 389 |
4056c66 |
Dawsh |
1y ago |
default=False, |
| 390 |
4056c66 |
Dawsh |
1y ago |
nullable=False, |
| 391 |
4056c66 |
Dawsh |
1y ago |
server_default=sqlalchemy.sql.false(), |
| 392 |
4056c66 |
Dawsh |
1y ago |
) |
| 393 |
4056c66 |
Dawsh |
1y ago |
services = relationship("Service", secondary=hosts_services) |
| 394 |
4056c66 |
Dawsh |
1y ago |
|
| 395 |
4056c66 |
Dawsh |
1y ago |
@property |
| 396 |
4056c66 |
Dawsh |
1y ago |
def service_ids(self): |
| 397 |
4056c66 |
Dawsh |
1y ago |
return [service.id for service in self.services] |
| 398 |
4056c66 |
Dawsh |
1y ago |
|
| 399 |
4056c66 |
Dawsh |
1y ago |
chain = relationship( |
| 400 |
4056c66 |
Dawsh |
1y ago |
"HostChain", |
| 401 |
4056c66 |
Dawsh |
1y ago |
foreign_keys="[HostChain.host_id]", |
| 402 |
4056c66 |
Dawsh |
1y ago |
order_by=HostChain.seq, |
| 403 |
4056c66 |
Dawsh |
1y ago |
collection_class=ordering_list("seq"), |
| 404 |
4056c66 |
Dawsh |
1y ago |
lazy="joined", |
| 405 |
4056c66 |
Dawsh |
1y ago |
cascade="all, delete-orphan", |
| 406 |
4056c66 |
Dawsh |
1y ago |
) |
| 407 |
4056c66 |
Dawsh |
1y ago |
|
| 408 |
4056c66 |
Dawsh |
1y ago |
@property |
| 409 |
4056c66 |
Dawsh |
1y ago |
def chain_ids(self): |
| 410 |
4056c66 |
Dawsh |
1y ago |
return [c.chained_host_id for c in self.chain] |
| 411 |
4056c66 |
Dawsh |
1y ago |
|
| 412 |
d636f3f |
Dawsh |
2y ago |
@property |
| 413 |
d636f3f |
Dawsh |
2y ago |
def protocol(self): |
| 414 |
4056c66 |
Dawsh |
1y ago |
return self.inbound.protocol if self.inbound else self.host_protocol |
| 415 |
4056c66 |
Dawsh |
1y ago |
|
| 416 |
4056c66 |
Dawsh |
1y ago |
@property |
| 417 |
4056c66 |
Dawsh |
1y ago |
def network(self): |
| 418 |
4056c66 |
Dawsh |
1y ago |
return self.host_network |
| 419 |
4056c66 |
Dawsh |
1y ago |
|
| 420 |
4056c66 |
Dawsh |
1y ago |
@property |
| 421 |
4056c66 |
Dawsh |
1y ago |
def noise(self): |
| 422 |
4056c66 |
Dawsh |
1y ago |
return self.udp_noises |
| 423 |
d636f3f |
Dawsh |
2y ago |
|
| 424 |
a47eb4c |
Dawsh |
2y ago |
|
| 425 |
a47eb4c |
Dawsh |
2y ago |
class System(Base): |
| 426 |
a47eb4c |
Dawsh |
2y ago |
__tablename__ = "system" |
| 427 |
a47eb4c |
Dawsh |
2y ago |
|
| 428 |
6457378 |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True) |
| 429 |
a47eb4c |
Dawsh |
2y ago |
uplink = Column(BigInteger, default=0) |
| 430 |
a47eb4c |
Dawsh |
2y ago |
downlink = Column(BigInteger, default=0) |
| 431 |
a47eb4c |
Dawsh |
2y ago |
|
| 432 |
a47eb4c |
Dawsh |
2y ago |
|
| 433 |
a47eb4c |
Dawsh |
2y ago |
class JWT(Base): |
| 434 |
a47eb4c |
Dawsh |
2y ago |
__tablename__ = "jwt" |
| 435 |
a47eb4c |
Dawsh |
2y ago |
|
| 436 |
a47eb4c |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True) |
| 437 |
a47eb4c |
Dawsh |
2y ago |
secret_key = Column( |
| 438 |
a47eb4c |
Dawsh |
2y ago |
String(64), nullable=False, default=lambda: os.urandom(32).hex() |
| 439 |
a47eb4c |
Dawsh |
2y ago |
) |
| 440 |
a47eb4c |
Dawsh |
2y ago |
|
| 441 |
a47eb4c |
Dawsh |
2y ago |
|
| 442 |
a47eb4c |
Dawsh |
2y ago |
class TLS(Base): |
| 443 |
a47eb4c |
Dawsh |
2y ago |
__tablename__ = "tls" |
| 444 |
a47eb4c |
Dawsh |
2y ago |
|
| 445 |
a47eb4c |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True) |
| 446 |
a47eb4c |
Dawsh |
2y ago |
key = Column(String(4096), nullable=False) |
| 447 |
a47eb4c |
Dawsh |
2y ago |
certificate = Column(String(2048), nullable=False) |
| 448 |
a47eb4c |
Dawsh |
2y ago |
|
| 449 |
a47eb4c |
Dawsh |
2y ago |
|
| 450 |
a47eb4c |
Dawsh |
2y ago |
class Node(Base): |
| 451 |
a47eb4c |
Dawsh |
2y ago |
__tablename__ = "nodes" |
| 452 |
01cdf2e |
Dawsh |
2y ago |
__table_args__ = (UniqueConstraint("address", "port"),) |
| 453 |
6457378 |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True) |
| 454 |
6457378 |
Dawsh |
2y ago |
name = Column(String(256), unique=True) |
| 455 |
010b05d |
Dawsh |
2y ago |
connection_backend = Column(String(32)) |
| 456 |
6457378 |
Dawsh |
2y ago |
address = Column(String(256)) |
| 457 |
6457378 |
Dawsh |
2y ago |
port = Column(Integer) |
| 458 |
6457378 |
Dawsh |
2y ago |
xray_version = Column(String(32)) |
| 459 |
f1ab16c |
Dawsh |
2y ago |
inbounds = relationship( |
| 460 |
f1ab16c |
Dawsh |
2y ago |
"Inbound", back_populates="node", cascade="all, delete" |
| 461 |
f1ab16c |
Dawsh |
2y ago |
) |
| 462 |
f5c168d |
Mardin |
2y ago |
backends = relationship( |
| 463 |
f5c168d |
Mardin |
2y ago |
"Backend", back_populates="node", cascade="all, delete" |
| 464 |
f5c168d |
Mardin |
2y ago |
) |
| 465 |
f1ab16c |
Dawsh |
2y ago |
status = Column( |
| 466 |
f1ab16c |
Dawsh |
2y ago |
Enum(NodeStatus), nullable=False, default=NodeStatus.unhealthy |
| 467 |
f1ab16c |
Dawsh |
2y ago |
) |
| 468 |
a47eb4c |
Dawsh |
2y ago |
last_status_change = Column(DateTime, default=datetime.utcnow) |
| 469 |
6457378 |
Dawsh |
2y ago |
message = Column(String(1024)) |
| 470 |
a47eb4c |
Dawsh |
2y ago |
created_at = Column(DateTime, default=datetime.utcnow) |
| 471 |
a47eb4c |
Dawsh |
2y ago |
uplink = Column(BigInteger, default=0) |
| 472 |
a47eb4c |
Dawsh |
2y ago |
downlink = Column(BigInteger, default=0) |
| 473 |
01cdf2e |
Dawsh |
2y ago |
user_usages = relationship( |
| 474 |
f1ab16c |
Dawsh |
2y ago |
"NodeUserUsage", |
| 475 |
f1ab16c |
Dawsh |
2y ago |
back_populates="node", |
| 476 |
00ae07e |
Dawsh |
1y ago |
cascade="save-update, merge", |
| 477 |
01cdf2e |
Dawsh |
2y ago |
) |
| 478 |
01cdf2e |
Dawsh |
2y ago |
usages = relationship( |
| 479 |
f1ab16c |
Dawsh |
2y ago |
"NodeUsage", |
| 480 |
f1ab16c |
Dawsh |
2y ago |
back_populates="node", |
| 481 |
971f47f |
Dawsh |
1y ago |
cascade="save-update, merge", |
| 482 |
01cdf2e |
Dawsh |
2y ago |
) |
| 483 |
01cdf2e |
Dawsh |
2y ago |
usage_coefficient = Column( |
| 484 |
01cdf2e |
Dawsh |
2y ago |
Float, nullable=False, server_default=text("1.0"), default=1 |
| 485 |
01cdf2e |
Dawsh |
2y ago |
) |
| 486 |
a47eb4c |
Dawsh |
2y ago |
|
| 487 |
db68a58 |
Dawsh |
2y ago |
@property |
| 488 |
db68a58 |
Dawsh |
2y ago |
def inbound_ids(self): |
| 489 |
db68a58 |
Dawsh |
2y ago |
return [inbound.id for inbound in self.inbounds] |
| 490 |
db68a58 |
Dawsh |
2y ago |
|
| 491 |
a47eb4c |
Dawsh |
2y ago |
|
| 492 |
a47eb4c |
Dawsh |
2y ago |
class NodeUserUsage(Base): |
| 493 |
a47eb4c |
Dawsh |
2y ago |
__tablename__ = "node_user_usages" |
| 494 |
01cdf2e |
Dawsh |
2y ago |
__table_args__ = (UniqueConstraint("created_at", "user_id", "node_id"),) |
| 495 |
a47eb4c |
Dawsh |
2y ago |
|
| 496 |
6457378 |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True) |
| 497 |
6457378 |
Dawsh |
2y ago |
created_at = Column(DateTime, nullable=False) # one hour per record |
| 498 |
a47eb4c |
Dawsh |
2y ago |
user_id = Column(Integer, ForeignKey("users.id")) |
| 499 |
a47eb4c |
Dawsh |
2y ago |
user = relationship("User", back_populates="node_usages") |
| 500 |
a47eb4c |
Dawsh |
2y ago |
node_id = Column(Integer, ForeignKey("nodes.id")) |
| 501 |
a47eb4c |
Dawsh |
2y ago |
node = relationship("Node", back_populates="user_usages") |
| 502 |
a47eb4c |
Dawsh |
2y ago |
used_traffic = Column(BigInteger, default=0) |
| 503 |
a47eb4c |
Dawsh |
2y ago |
|
| 504 |
a47eb4c |
Dawsh |
2y ago |
|
| 505 |
a47eb4c |
Dawsh |
2y ago |
class NodeUsage(Base): |
| 506 |
a47eb4c |
Dawsh |
2y ago |
__tablename__ = "node_usages" |
| 507 |
01cdf2e |
Dawsh |
2y ago |
__table_args__ = (UniqueConstraint("created_at", "node_id"),) |
| 508 |
a47eb4c |
Dawsh |
2y ago |
|
| 509 |
6457378 |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True) |
| 510 |
6457378 |
Dawsh |
2y ago |
created_at = Column(DateTime, nullable=False) # one hour per record |
| 511 |
a47eb4c |
Dawsh |
2y ago |
node_id = Column(Integer, ForeignKey("nodes.id")) |
| 512 |
a47eb4c |
Dawsh |
2y ago |
node = relationship("Node", back_populates="usages") |
| 513 |
a47eb4c |
Dawsh |
2y ago |
uplink = Column(BigInteger, default=0) |
| 514 |
a47eb4c |
Dawsh |
2y ago |
downlink = Column(BigInteger, default=0) |
| 515 |
a47eb4c |
Dawsh |
2y ago |
|
| 516 |
a47eb4c |
Dawsh |
2y ago |
|
| 517 |
0178858 |
Dawsh |
2y ago |
class Settings(Base): |
| 518 |
0178858 |
Dawsh |
2y ago |
__tablename__ = "settings" |
| 519 |
0178858 |
Dawsh |
2y ago |
|
| 520 |
0178858 |
Dawsh |
2y ago |
id = Column(Integer, primary_key=True, server_default=text("0")) |
| 521 |
0178858 |
Dawsh |
2y ago |
subscription = Column(JSON, nullable=False) |
| 522 |
0178858 |
Dawsh |
2y ago |
telegram = Column(JSON) |