| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- #!/usr/bin/env python3
- import requests
- import configure
- from vs_types import NotFoundError, BizLogicError
- def switch_next_node():
- headers = {
- "Authorization": f"Bearer {configure.CLASH_API_KEY}",
- "Content-Type": "application/json"
- }
- try:
- resp = requests.get(f"{configure.CLASH_API_URL}/proxies", headers=headers, timeout=5)
- except Exception as e:
- raise BizLogicError(f'Fetch proxies error, e={e}')
- proxies_data = resp.json()
- proxies = proxies_data.get('proxies')
-
- if configure.CLASH_GROUP_NAME not in proxies:
- raise NotFoundError(message=f"Group '{configure.CLASH_GROUP_NAME}' not found")
-
- group = proxies[configure.CLASH_GROUP_NAME]
- all_nodes = group['all']
- current_node = group['now']
-
- try:
- idx = all_nodes.index(current_node)
- next_idx = (idx + 1) % len(all_nodes)
- next_node = all_nodes[next_idx]
- except ValueError:
- next_node = all_nodes[0]
-
- data = {"name": next_node}
- try:
- resp = requests.put(f"{configure.CLASH_API_URL}/proxies/{configure.CLASH_GROUP_NAME}", headers=headers, json=data, timeout=5)
- except Exception as e:
- raise BizLogicError(f'Switch to {next_node} error, e={e}')
- return next_node
- if __name__ == "__main__":
- switch_next_node()
|