-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathflow.py
104 lines (90 loc) · 3.9 KB
/
flow.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
import requests
import logging
DOMAIN = 'domain.com' # Replace with your actual domain
PORT = 12345 # Replace with the actual port number
username = 'username' # Replace with your actual username
password = 'password' # Replace with your actual password
protocol = 'vless' # Replace with your actual protocol
flow = "xtls-rprx-vision" # Replace with your actual flow
def get_access_token(username, password):
url = f'https://{DOMAIN}:{PORT}/api/admin/token'
data = {
'username': username,
'password': password
}
try:
response = requests.post(url, data=data)
response.raise_for_status()
access_token = response.json()['access_token']
return access_token
except requests.exceptions.RequestException as e:
logging.error(f'Error occurred while obtaining access token: {e}')
return None
def get_users_list(access_token):
url = f'https://{DOMAIN}:{PORT}/api/users'
headers = {
'accept': 'application/json',
'Authorization': f'Bearer {access_token}'
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
users_list = response.json()
return users_list
except requests.exceptions.RequestException as e:
logging.error(f'Error occurred while retrieving users list: {e}')
return None
def flow_select(access_token, username, protocol, flow):
url = f'https://{DOMAIN}:{PORT}/api/user/{username}'
headers = {
'accept': 'application/json',
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
user_details = response.json()
if user_details['proxies'][protocol]['flow'] != flow:
user_details['proxies'][protocol]['flow'] = flow
# Modify 'links' and 'subscription_url'
user_details['links'] = []
user_details['subscription_url'] = ""
if user_details['status'] != "disabled":
user_details['status'] = "active"
# Create a list of keys to remove
keys_to_remove = []
for inbound_protocol in user_details.get('inbounds', {}):
if not user_details['inbounds'][inbound_protocol]:
keys_to_remove.append(inbound_protocol)
# Remove the keys outside of the loop
for key in keys_to_remove:
user_details['inbounds'].pop(key, None)
user_details['proxies'].pop(key, None)
response = requests.put(url, json=user_details, headers=headers)
response.raise_for_status()
return True
else:
logging.error(f'flow for user {username} already is {flow}')
return False
except requests.exceptions.RequestException as e:
logging.error(f'Error occurred while modifying user: {e}')
return False
# Configure logging settings
logging.basicConfig(filename='script_log.log', level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
access_token = get_access_token(username, password)
if access_token:
users_list = get_users_list(access_token)
if users_list:
for user in users_list['users']:
# Modify 'flow' for vless proxies
if 'proxies' in user and protocol in user['proxies']:
if flow_select(access_token, user['username'], protocol, flow):
print(f"User {user['username']} flow updated successfully.")
else:
print(f'flow for user {username} already is {flow}')
print("All users modified successfully.")
else:
print("Failed to retrieve the users list.")
else:
print("Failed to obtain the access token.")