-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdemo.py
131 lines (95 loc) · 4.22 KB
/
demo.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
from py3xui import Api
# In this short example we will update the client's traffic limit.
# Due to the way API returns the data, we need to get the needed inbound to
# obtain the actual UUID of the client.
# Because other endpoints return the numeric ID of the client in the given inbound,
# not the UUID.
# In the same way it can be implemented for AsyncApi, the methods are called
# exactly the same way, and has the same signatures.
# 1️⃣ Create an instance of the API class.
host = "**********"
username = "**********"
password = "**********"
email = "iwatkot"
api = Api(host, username, password)
# 2️⃣ Login to the API.
api.login()
inbound_id = 1 # ⬅️ Your inbound ID here.
# 3️⃣ Get the inbound.
inbound = api.inbound.get_by_id(inbound_id)
print(f"Inbound has {len(inbound.settings.clients)} clients")
# 4️⃣ Find the needed client in the inbound.
client = None
for c in inbound.settings.clients:
if c.email == email:
client = c
break
if client:
print(f"Found client with ID: {client.id}") # ⬅️ The actual Client UUID.
else:
raise ValueError(f"Client with email {email} not found")
cliend_uuid = client.id
# 5️⃣ Get the client by email.
client_by_email = api.client.get_by_email(email)
print(f"Client by email has ID: {client_by_email.id}") # ⬅️ The numeric ID here.
# 6️⃣ Update the client with needed parameters.
client_by_email.total_gb = 1000 * 1024 * 1024 # ⬅️ Your value here.
# 7️⃣ Update the client ID so it will be UUID, not numeric.
client_by_email.id = cliend_uuid
# 8️⃣ Update the client.
api.client.update(client_by_email.id, client_by_email)
# We are done! 🎉
# In this example we set the maximum traffic for the user with email "iwatkot" to 1GB.
# In this example we'll create a connection string which can be used to add a new profile
# to the VPN application.
from py3xui import Inbound
XUI_EXTERNAL_IP = "**********" # ⬅️ Your external IP here or domain name.
MAIN_REMARK = "gmfvbot" # ⬅️ It can be any string.
SERVER_PORT = 443 # ⬅️ Your server port here.
def get_connection_string(inbound: Inbound, user_uuid: str, user_email: int) -> str:
"""Prepare a connection string for the given inbound, user UUID and telegram ID.
Arguments:
inbound (Inbound): The inbound object.
user_uuid (str): The UUID of the user.
user_email (int): The email of the user.
Returns:
str: The connection string.
"""
public_key = inbound.stream_settings.reality_settings.get("settings").get("publicKey")
website_name = inbound.stream_settings.reality_settings.get("serverNames")[0]
short_id = inbound.stream_settings.reality_settings.get("shortIds")[0]
connection_string = (
f"vless://{user_uuid}@{XUI_EXTERNAL_IP}:{SERVER_PORT}"
f"?type=tcp&security=reality&pbk={public_key}&fp=firefox&sni={website_name}"
f"&sid={short_id}&spx=%2F#{MAIN_REMARK}-{user_email}"
)
return connection_string
# Now, if you want to create a QR code image from the connection string, you can use the
# qrcode library.
# Remember to install it first with `pip install qrcode`.
import os
import qrcode
# 1️⃣ Obtain the connection string.
connection_string = get_connection_string(inbound, "**********", email)
# 2️⃣ Create the QR code.
img = qrcode.make(connection_string)
# 3️⃣ Save the QR code to the file.
qrcode_path = os.path.join("qrcodes", f"{email}.png")
# Check if qrcodes directory exists and create it if not
if not os.path.exists("qrcodes"):
os.makedirs("qrcodes")
img.save(qrcode_path)
# Now you can use the `qrcode_path` to send the QR code to the user.
# This example demonstrates how to get server status and create a database backup
# 9️⃣ Get server status
server_status = api.server.get_status()
print(f"CPU Load: {server_status.cpu}%")
print(f"Memory Usage: {server_status.mem.current}/{server_status.mem.total} bytes")
print(f"Uptime: {server_status.uptime} seconds")
# 🔟 Create database backup
"""The get_db() method retrieves a database backup file and saves it locally.
The backup contains all XUI panel data including users, inbounds and settings.
"""
db_backup_path = "backup.db"
api.server.get_db(db_backup_path)
print(f"Database backup saved to {db_backup_path}")