-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxui.py
More file actions
166 lines (143 loc) · 4.98 KB
/
xui.py
File metadata and controls
166 lines (143 loc) · 4.98 KB
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
import json
import os
import time
import requests
import urllib3
from dotenv import load_dotenv
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
load_dotenv(".env")
BASE_URL = os.getenv("BASE_URL")
USERNAME = os.getenv("USERNAME")
PASSWORD = os.getenv("PASSWORD")
INBOUND_ID = os.getenv("INBOUND_ID")
def auth():
"""Auth in x-ui api."""
session = requests.Session()
login_data = {"username": USERNAME, "password": PASSWORD}
resp = session.post(f"{BASE_URL}/login", data=login_data, verify=False)
if resp.status_code != 200:
print(resp)
raise ValueError("Auth failed.")
print("✅ Authorized...")
return session
def add_xui_client(user_id: int, nickname: str, obfuscated_user: str, days: int):
"""Adding new x-ui client via api on https."""
print(f"Adding new x-ui client {nickname}")
session = auth()
expiry_timestamp = int((time.time() + days * 86400) * 1000)
clients_json = {
"clients": [
{
"id": obfuscated_user,
"tgId": str(user_id),
"email": f"{obfuscated_user}@vray",
"enable": True,
"limitIp": 0,
"totalGB": 0,
"expiryTime": expiry_timestamp,
"subId": obfuscated_user,
}
]
}
payload = {"id": int(INBOUND_ID), "settings": json.dumps(clients_json)}
response = session.post(
f"{BASE_URL}/panel/api/inbounds/addClient", json=payload, verify=False
)
print(f"Server response:\n{response.text}")
if response.ok and response.json().get("success"):
print("✅ Client successfully added!")
else:
print("❌ Client was not added.")
def get_client_info(email: str):
"""Get client info."""
session = auth()
response = session.get(
f"{BASE_URL}/panel/api/inbounds/get/{INBOUND_ID}", verify=False
)
if response.status_code == 200:
data = response.json()
clients = json.loads(data["obj"]["settings"])["clients"]
client = next((c for c in clients if c.get("email") == email), None)
sub_id = client["subId"]
print(sub_id)
return sub_id
return []
def delete_xui_client(email: str):
"""Delete x-ui client."""
session = auth()
response = session.get(
f"{BASE_URL}/panel/api/inbounds/get/{INBOUND_ID}", verify=False
)
if response.status_code != 200:
print(
f"❌ Failed to retrieve clients. Server responded with code {response.status_code}."
)
return
data = response.json()
clients = json.loads(data["obj"]["settings"])["clients"]
client = next((c for c in clients if c.get("email") == email), None)
client_uuid = client["id"]
response = session.post(
f"{BASE_URL}/panel/api/inbounds/{INBOUND_ID}/delClient/{client_uuid}",
verify=False,
)
if response.status_code == 200:
data = response.json()
if data.get("success"):
print(f"✅ Client with email {email} successfully deleted!")
else:
print(f"❌ Client with email {email} was not deleted.")
else:
print(
f"❌ Failed to delete client with email {email}. "
f"Server responded with status code {response.status_code}."
)
def update_xui_client(email: str, period: int):
"""Update x-ui client period."""
session = auth()
response = session.get(
f"{BASE_URL}/panel/api/inbounds/get/{INBOUND_ID}", verify=False
)
if response.status_code != 200:
print(
f"❌ Failed to retrieve clients. Server responded with code {response.status_code}."
)
return
data = response.json()
clients = json.loads(data["obj"]["settings"])["clients"]
client = next((c for c in clients if c.get("email") == email), None)
client_uuid = client["id"]
current_expiry = client["expiryTime"]
new_expiry = current_expiry + period * 86400 * 1000
settings_obj = {
"clients": [
{
"id": client_uuid,
"email": email,
"tgId": client.get("tgId", ""),
"expiryTime": new_expiry,
"enable": True,
"subId": client["subId"],
}
]
}
update_data = {
"id": int(INBOUND_ID),
"settings": json.dumps(settings_obj, separators=(",", ":")),
}
response = session.post(
f"{BASE_URL}/panel/api/inbounds/updateClient/{client_uuid}",
json=update_data,
verify=False,
)
if response.status_code == 200:
data = response.json()
if data.get("success"):
print(f"✅ Client with email {email} successfully updated!")
else:
print(f"❌ Client with email {email} was not updated.")
if __name__ == "__main__":
# add_xui_client(os.getenv("ADMIN"), "test_nick2", "test", 30)
# delete_xui_client("test@vray")
# update_xui_client("test@vray", 30)
get_client_info("test@vray")