From 5076603a8cb2332dbcf127de10db363cb462f1c7 Mon Sep 17 00:00:00 2001 From: Roshan Dash <83511412+roshan1337d@users.noreply.github.com> Date: Sat, 11 Dec 2021 23:14:14 +0530 Subject: [PATCH] Get ip address from login buffer --- clashapi/__init__.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/clashapi/__init__.py b/clashapi/__init__.py index 3e29cfd..1ca646f 100644 --- a/clashapi/__init__.py +++ b/clashapi/__init__.py @@ -1,14 +1,19 @@ import requests +from base64 import b64decode as base64_b64decode +from json import loads as json_loads def updatekeys(email, password, baseUrl, scopes): - #Getting the current ip address of the system - currentIP=requests.get("https://api.ipify.org").content.decode('utf8') - #Creating a session session=requests.Session() #Loggin in - session.post(url=f"{baseUrl}/api/login", json={"email": email,"password": password}) + loginData=session.post(url=f"{baseUrl}/api/login", json={"email": email,"password": password}) + + #Getting the current ip address of the system from the buffer returned by the login post. api.ipify.org is used as a fallback. + try: + currentIP=json_loads(base64_b64decode(loginData.json()["temporaryAPIToken"].split(".")[1]+"=").decode("utf-8"))["limits"][1]["cidrs"][0].split("/")[0] + except: + currentIP=requests.get("https://api.ipify.org").content.decode('utf8') #Getting the list of current keys #If the ip address of a key is not the current one, its id, name, and description is added to the KeysToUpdate list @@ -42,4 +47,4 @@ def coc(email, password): def cr(email, password): - return updatekeys(email,password,"https://developer.clashroyale.com","royale") \ No newline at end of file + return updatekeys(email,password,"https://developer.clashroyale.com","royale")