-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.py
383 lines (314 loc) · 12.5 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from contextlib import AbstractContextManager
from httpx import Client, URL
from dataclasses import dataclass
from enum import Enum, auto
from functools import wraps
from exceptions import (
BearerRequired,
BadResponse,
ExistanceError,
PermissionDenied,
ValidationError,
NotExistsError,
)
from models import (
AccessToken,
Admin,
Subscription,
SubscriptionInfo,
Proxy,
LimitStrategy,
Status,
ClientType,
SystemStats,
Inbound
)
from datetime import datetime
class MarzbanState(Enum):
UNOPENED = auto()
OPENED = auto()
CLOSED = auto()
# TODO: Create method for all Admin operations
# TODO: Create object for the requests and responses
# TODO: Study more about decoraters and fix them
@dataclass
class Marzban(AbstractContextManager):
def __init__(self, base_url: URL | str) -> None:
if isinstance(base_url, str):
base_url = URL(base_url)
self.base_url = base_url
self._state = MarzbanState.UNOPENED
self.__bearer = None
def close(self):
if self._state != MarzbanState.CLOSED:
self._state = MarzbanState.CLOSED
self.__client.close()
def __enter__(self):
if self._state != MarzbanState.UNOPENED:
raise RuntimeError(
{
MarzbanState.CLOSED: "Can't reopen the connection when it is closed",
MarzbanState.OPENED: "Can't reopen the connection while it is open",
}[self._state]
)
self.__client = Client()
self._state = MarzbanState.OPENED
def __exit__(self, exc_type, exc_value, exc_traceback):
if self._state != MarzbanState.OPENED:
raise RuntimeError(
{
MarzbanState.CLOSED: "Can't close the connection when it is already closed",
MarzbanState.UNOPENED: "Can't close the connection while it is still unopened",
}[self._state]
)
self.__client.close()
self._state = MarzbanState.CLOSED
@staticmethod
def __flush_state(force_bearer: bool = True):
def decorator(func: callable):
@wraps(func)
def wrapper(*args, **kwargs):
args = list(args)
self = args[0]
args = tuple(args)
if force_bearer:
if self.__bearer is None:
raise BearerRequired("Bearer Token should be created")
if self._state == MarzbanState.UNOPENED:
self.__client = Client()
self._state = MarzbanState.OPENED
return func(*args, **kwargs)
elif self._state == MarzbanState.OPENED:
return func(*args, **kwargs)
raise RuntimeError("Can't do any action while connection is closed")
return wrapper
return decorator
@__flush_state(force_bearer=False)
def get_admin_token(self, admin: Admin):
request = self.__client.post(
self.base_url.join("/api/admin/token"),
data={"username": admin.username, "password": admin.password},
)
if request.status_code == 200:
response = request.json()
result = AccessToken(
access_token=response.get("access_token"),
token_type=response.get("token_type"),
)
self.__bearer = result.access_token
self.__client.headers.update({"Authorization": f"Bearer {self.__bearer}"})
return result
elif request.status_code == 401:
raise ValidationError("Invalid Login Credential")
@__flush_state()
def get_current_admin(self):
request = self.__client.get(self.base_url.join("/api/admin"))
if request.status_code != 200:
raise RuntimeError("Internal exception")
response = request.json()
match response.get("detail"):
case "Not authenticated":
raise BearerRequired("Bearer Token should be created")
return Admin(username=response.get("username"), is_sudo=response.get("is_sudo"))
@__flush_state()
def create_admin(self, admin: Admin) -> Admin:
request = self.__client.post(
self.base_url.join("/api/admin"),
json={
"username": admin.username,
"is_sudo": admin.is_sudo,
"password": admin.password,
},
)
response = request.json()
match response.get("detail"):
case "Admin already exists":
raise ExistanceError("Admin already exists")
case "You're not allowed":
raise PermissionDenied(
"Permission denied for this bearer token and admin user"
)
case "Validation Error":
raise ValidationError("Request Validation Error")
case "Not authenticated":
raise BearerRequired("Bearer Token should be created")
case _:
if response.get("username") != admin.username:
raise BadResponse("I/O Conflict")
return admin
@__flush_state()
def set_admin(self, admin: Admin) -> Admin:
request = self.__client.put(
self.base_url.join(f"/api/admin/{admin.username}"),
json={"password": admin.password, "is_sudo": admin.is_sudo},
)
response = request.json()
match response.get("detail"):
case "Admin already exists":
raise NotExistsError("Admin not found")
case "You're not allowed":
raise PermissionDenied(
"Permission denied for this bearer token and admin user"
)
case "Validation Error":
raise ValidationError("Request Validation Error")
case "Not authenticated":
raise BearerRequired("Bearer Token should be created")
case _:
if response.get("username") != admin.username:
raise BadResponse("I/O Conflict")
return admin
@__flush_state()
def delete_admin(self, admin: Admin):
request = self.__client.delete(
self.base_url.join(f"/api/admin/{admin.username}")
)
response = request.json()
match response.get("detail"):
case "Admin already exists":
raise NotExistsError("Admin not found")
case "You're not allowed":
raise PermissionDenied(
"Permission denied for this bearer token and admin user"
)
case "Validation Error":
raise ValidationError("Request Validation Error")
case "Not authenticated":
raise BearerRequired("Bearer Token should be created")
case _:
if response.get("username") != admin.username:
raise BadResponse("I/O Conflict")
return admin
@__flush_state()
def get_admins(self):
request = self.__client.get(self.base_url.join("/api/admins"))
response = request.json()
users = []
for user in response:
users.append(
Admin(username=user.get("username"), is_sudo=user.get("is_sudo"))
)
return users
@__flush_state()
def iget_admins(self):
request = self.__client.get(self.base_url.join("/api/admins"))
response = request.json()
for user in response:
yield Admin(username=user.get("username"), is_sudo=user.get("is_sudo"))
@__flush_state()
def get_admin(self, admin: Admin):
request = self.__client.get(self.base_url.join(f"/api/admins?username={admin.username}"))
response = request.json()
for user in response:
return Admin(username=user.get("username"), is_sudo=user.get("is_sudo"))
@__flush_state()
def subscription(self, subscription: Subscription):
request = self.__client.get(
self.base_url.join(f"/sub/{subscription.token}/{subscription.client_type}")
)
return request.text
def subscription_url_generator(self, subscription: Subscription) -> str:
return str(self.base_url.join(subscription.url))
@__flush_state()
def subscription_info(self, subscription: Subscription) -> SubscriptionInfo:
request = self.__client.get(
self.base_url.join(f"/sub/{subscription.token}/info")
)
response = request.json()
return SubscriptionInfo(
proxies=[Proxy(name=proxy.get("name"), id=proxy.get("id")) for proxy in response.get("proxies")],
expire=response.get("expire"),
data_limit=response.get("data_limit"),
data_limit_reset_strategy=response.get("data_limit_reset_strategy"),
inbounds=[
Inbound(
tag=inbound.get("tag"),
protocol=inbound.get("protocol"),
network=inbound.get("network"),
tls=inbound.get("tls"),
port=inbound.get("port")
)
for inbound in response.get('inbounds')
],
note=response.get("note"),
sub_updated_at=datetime.strftime(response.get("sub_updated_at")),
sub_last_user_agent=response.get("sub_last_user_agent"),
online_at=datetime.strftime(response.get("online_at")),
on_hold_expire_duration=response.get("on_hold_expire_duration"),
on_hold_timeout=datetime.strftime(response.get("on_hold_timeout")),
username=response.get("username"),
status=response.get("status"),
used_traffic=response.get("used_traffic"),
lifetime_used_traffic=response.get("lifetime_used_traffic"),
created_at=datetime.strftime(response.get("created_at")),
links=response.get("links"),
subscription_url=response.get("subscription_url"),
excluded_inbounds=response.get("excluded_inbounds")
)
@__flush_state()
def get_system_stats(self):
request = self.__client.get(
"/api/system"
)
response = request.json()
return SystemStats(
version=response.get("version"),
mem_total=response.get("mem_total"),
mem_used=response.get("mem_used"),
cpu_cores=response.get("cpu_cores"),
cpu_usage=response.get("cpu_usage"),
total_user=response.get("total_user"),
users_active=response.get("users_active"),
incoming_bandwidth=response.get("incoming_bandwidth"),
outgoing_bandwidth=response.get("outgoing_bandwidth"),
incoming_bandwidth_speed=response.get("incoming_bandwidth_speed"),
outgoing_bandwidth_speed=response.get("outgoing_bandwidth_speed")
)
@__flush_state()
def get_inbounds(self):
request = self.__client.get(
self.base_url.join("/api/inbounds")
)
response = request.json()
result = {}
for key in response.keys():
result[key] = [
Inbound(
tag=inbound.get("tag"),
protocol=inbound.get("protocol"),
network=inbound.get("network"),
tls=inbound.get("tls"),
port=inbound.get("port")
)
for inbound in response.get(key)
]
return result
@__flush_state()
def iget_inbounds(self):
request = self.__client.get(
self.base_url.join("/api/inbounds")
)
response = request.json()
for key in response.keys():
result = {
key: [
Inbound(
tag=inbound.get("tag"),
protocol=inbound.get("protocol"),
network=inbound.get("network"),
tls=inbound.get("tls"),
port=inbound.get("port")
)
for inbound in response.get(key)
]
}
yield result
#TODO
@__flush_state()
def get_hosts(self):
request = self.__client.get(
self.base_url.join("/api/hosts")
)
@__flush_state
def modify_hosts(self):
pass