Skip to content

Instantly share code, notes, and snippets.

@grimpy
Created November 9, 2020 16:15
Show Gist options
  • Save grimpy/145cf207f99e1185d691aa165c46f3b7 to your computer and use it in GitHub Desktop.
Save grimpy/145cf207f99e1185d691aa165c46f3b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import requests
import sys
def encrypt(word):
# from the javascript
phrase = "e5dl12XYVggihggafXWf0f2YSf2Xngd1"
# (a[2 * n] = (240 & t[n % t.length].charCodeAt()) | ((15 & r) ^ (15 & t[n % t.length].charCodeAt()))), (a[2 * n + 1] = (240 & t[n % t.length].charCodeAt()) | ((r >> 4) ^ (15 & t[n % t.length].charCodeAt())));
a = []
for idx, char in enumerate(word):
r = ord(char)
positioncode = ord(phrase[idx % len(phrase)])
a.append((240 & positioncode) | ((15 & r) ^ (15 & positioncode)))
a.append((240 & positioncode) | ((r >> 4) ^ (15 & positioncode)))
return "".join(chr(char) for char in a)
class APIError(Exception):
pass
class API:
def __init__(self, url):
self.session = requests.Session()
self._url = url
self.session.headers = {
"Accept": "application/json, text/javascript, */*; " + "q=0.01",
"Content-Type": "application/json",
"Referer": f"{url}/index.html",
"_TclRequestVerificationKey": "KSDHSDFOGQ5WERYTUIQWERTYUISDFG1HJZXCVCXBN2GDSMNDHKVKFsVBNf",
}
def _command(self, command, **kwargs):
message = {"jsonrpc": "2.0", "method": command, "id": "12", "params": kwargs}
resp = self.session.post(self._url + "/jrd/webapi", json=message)
result = resp.json()
if "result" not in result:
raise APIError(result["error"])
return result["result"]
def login(self, password, username="admin"):
result = self._command(
"Login", UserName=encrypt(username), Password=encrypt(password)
)
token = result["token"]
self.session.headers["_TclRequestVerificationToken"] = encrypt(str(token))
def reboot(self):
self._command("SetDeviceReboot")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--url", required=True)
parser.add_argument("--password", required=True)
parser.add_argument("--cmd", default="reboot")
options = parser.parse_args()
api = API(options.url)
api.login(options.password)
func = getattr(api, options.cmd, None)
if not func:
print("No such command", file=sys.stderr)
else:
print(func())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment