Skip to content

Instantly share code, notes, and snippets.

@CheeseCake87
Last active November 26, 2024 18:54
Show Gist options
  • Select an option

  • Save CheeseCake87/83f6ba5f8563f3a4ec2424b547eedf25 to your computer and use it in GitHub Desktop.

Select an option

Save CheeseCake87/83f6ba5f8563f3a4ec2424b547eedf25 to your computer and use it in GitHub Desktop.
Example of some code that is able to delete devices from RustDesk
"""
requirements:
python-dotenv
requests
"""
import os
from json import JSONDecodeError
import requests
from dotenv import load_dotenv
load_dotenv()
DOMAIN = os.getenv("DOMAIN")
USERNAME = os.getenv("USERNAME")
PASSWORD = os.getenv("PASSWORD")
ADDRESS_BOOK_GUID = "01935934-18e8-7d60-8e77-308bc39b576b"
USER_PAYLOAD = {
"username": USERNAME,
"password": PASSWORD,
"autoLogin": False,
"uuid": "9166b42f9d63332c5cafa3e461e7ff29",
"type": "account",
"deviceInfo": {"os": "macos", "type": "browser", "name": "Chrome - 131.0.0.0"},
}
LOGIN_URL = f"{DOMAIN}/api/login"
ADDRESS_BOOK_URL = f"{DOMAIN}/api/ab/peers?current=1&pageSize=500&ab={ADDRESS_BOOK_GUID}"
DEVICES_URL = f"{DOMAIN}/api/devices"
JSON_HEADER = {"Content-Type": "application/json"}
AUTH_HEADER = {
"authorization": None,
}
def get_authenticated_user_payload():
_ = requests.post(LOGIN_URL, json=USER_PAYLOAD, headers=JSON_HEADER)
_.raise_for_status()
return _.json()
def get_address_book_devices():
_ = requests.get(ADDRESS_BOOK_URL, headers=AUTH_HEADER)
_.raise_for_status()
return _.json()
def get_all_devices():
_ = requests.get(
f"{DEVICES_URL}?current=1&pageSize=500", headers=AUTH_HEADER
)
_.raise_for_status()
return _.json()
def disable_device(guid: str):
_ = requests.post(
f"{DEVICES_URL}/{guid}/disable", headers=AUTH_HEADER
)
_.raise_for_status()
try:
print(f"{_.json()}")
except JSONDecodeError:
pass
def delete_device(guid: str):
_ = requests.delete(
f"{DEVICES_URL}/{guid}", headers=AUTH_HEADER
)
_.raise_for_status()
try:
print(f"{_.json()}")
except JSONDecodeError:
pass
auth_response = get_authenticated_user_payload()
access_token = auth_response.get("access_token")
if access_token:
AUTH_HEADER["authorization"] = f"Bearer {access_token}"
address_book_devices = get_address_book_devices()
if address_book_devices.get("data"):
devices_eligible_for_deletion = [
x.get("id") for x in address_book_devices.get("data")
]
all_devices = get_all_devices()
for device in all_devices.get("data", []):
if device.get("id", 0) in devices_eligible_for_deletion:
device_guid = device.get("guid")
enabled = True if device.get("status", 0) == 1 else False
is_online = device.get("is_online", False)
if not is_online:
if enabled:
disable_device(device_guid)
delete_device(device_guid)
else:
print(f"Device {device_guid} is still online")
else:
print("No devices in the _adhoc address book")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment