Skip to content

Commit bf9a1b6

Browse files
author
6nz
authored
Merge pull request #49 from 6nz/main
Added auto-login example, user data dumping to auth file on login,removed unused modules, replaced sys.exit with os._exit(1) cuz os._exit calls the C function _exit() which does an immediate program termination(sys.exit can't stop threads), added a few "detailed" comments +smaller keyauth.py file size when compiled bc removed all the unused modules
2 parents a5d86c2 + c97807a commit bf9a1b6

File tree

2 files changed

+142
-34
lines changed

2 files changed

+142
-34
lines changed

keyauth.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@
55

66
import binascii # hex encoding
77

8-
# https requests
9-
108
from uuid import uuid4 # gen random guid
11-
import webbrowser
129
import platform
13-
import subprocess
14-
import datetime
15-
import sys
1610
import os
17-
import requests
11+
import requests # https requests
1812
from requests_toolbelt.adapters.fingerprint import FingerprintAdapter
1913

2014
try:
@@ -70,7 +64,7 @@ def init(self):
7064

7165
if response == "KeyAuth_Invalid":
7266
print("The application doesn't exist")
73-
sys.exit()
67+
os._exit(1)
7468

7569
response = encryption.decrypt(response, self.secret, init_iv)
7670
json = jsond.loads(response)
@@ -80,14 +74,14 @@ def init(self):
8074
print("New Version Available")
8175
download_link = json["download"]
8276
os.system(f"start {download_link}")
83-
sys.exit()
77+
os._exit(1)
8478
else:
8579
print("Invalid Version, Contact owner to add download link to latest app version")
86-
sys.exit()
80+
os._exit(1)
8781

8882
if not json["success"]:
8983
print(json["message"])
90-
sys.exit()
84+
os._exit(1)
9185

9286
self.sessionid = json["sessionid"]
9387
self.initialized = True
@@ -123,7 +117,7 @@ def register(self, user, password, license, hwid=None):
123117
self.__load_user_data(json["info"])
124118
else:
125119
print(json["message"])
126-
sys.exit()
120+
os._exit(1)
127121

128122
def upgrade(self, user, license):
129123
self.checkinit()
@@ -148,10 +142,10 @@ def upgrade(self, user, license):
148142
if json["success"]:
149143
print("successfully upgraded user")
150144
print("please restart program and login")
151-
sys.exit()
145+
os._exit(1)
152146
else:
153147
print(json["message"])
154-
sys.exit()
148+
os._exit(1)
155149

156150
def login(self, user, password, hwid=None):
157151
self.checkinit()
@@ -182,7 +176,7 @@ def login(self, user, password, hwid=None):
182176
print("successfully logged in")
183177
else:
184178
print(json["message"])
185-
sys.exit()
179+
os._exit(1)
186180

187181
def license(self, key, hwid=None):
188182
self.checkinit()
@@ -211,7 +205,7 @@ def license(self, key, hwid=None):
211205
print("successfully logged into license")
212206
else:
213207
print(json["message"])
214-
sys.exit()
208+
os._exit(1)
215209

216210
def var(self, name):
217211
self.checkinit()
@@ -237,7 +231,7 @@ def var(self, name):
237231
else:
238232
print(json["message"])
239233
time.sleep(5)
240-
sys.exit()
234+
os._exit(1)
241235

242236
def getvar(self, var_name):
243237
self.checkinit()
@@ -260,7 +254,7 @@ def getvar(self, var_name):
260254
else:
261255
print(json["message"])
262256
time.sleep(5)
263-
sys.exit()
257+
os._exit(1)
264258

265259
def setvar(self, var_name, var_data):
266260
self.checkinit()
@@ -283,7 +277,7 @@ def setvar(self, var_name, var_data):
283277
else:
284278
print(json["message"])
285279
time.sleep(5)
286-
sys.exit()
280+
os._exit(1)
287281

288282
def ban(self):
289283
self.checkinit()
@@ -304,7 +298,7 @@ def ban(self):
304298
else:
305299
print(json["message"])
306300
time.sleep(5)
307-
sys.exit()
301+
os._exit(1)
308302

309303
def file(self, fileid):
310304
self.checkinit()
@@ -328,7 +322,7 @@ def file(self, fileid):
328322
if not json["success"]:
329323
print(json["message"])
330324
time.sleep(5)
331-
sys.exit()
325+
os._exit(1)
332326
return binascii.unhexlify(json["contents"])
333327

334328
def webhook(self, webid, param):
@@ -355,7 +349,7 @@ def webhook(self, webid, param):
355349
else:
356350
print(json["message"])
357351
time.sleep(5)
358-
sys.exit()
352+
os._exit(1)
359353

360354
def check(self):
361355
self.checkinit()
@@ -488,7 +482,7 @@ def chatSend(self, message, channel):
488482
def checkinit(self):
489483
if not self.initialized:
490484
print("Initialize first, in order to use the functions")
491-
sys.exit()
485+
os._exit(1)
492486

493487
def __do_request(self, post_data):
494488

@@ -572,7 +566,7 @@ def encrypt(message, enc_key, iv):
572566
return encryption.encrypt_string(message.encode(), _key.encode(), _iv.encode()).decode()
573567
except:
574568
print("Invalid Application Information. Long text is secret short text is ownerid. Name is supposed to be app name not username")
575-
sys.exit()
569+
os._exit(1)
576570

577571
@staticmethod
578572
def decrypt(message, enc_key, iv):
@@ -584,4 +578,4 @@ def decrypt(message, enc_key, iv):
584578
return encryption.decrypt_string(message.encode(), _key.encode(), _iv.encode()).decode()
585579
except:
586580
print("Invalid Application Information. Long text is secret short text is ownerid. Name is supposed to be app name not username")
587-
sys.exit()
581+
os._exit(1)

main.py

Lines changed: 123 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from keyauth import api
22
import os
3-
import sys
43
import os.path
5-
import platform
64
import hashlib
75
from time import sleep
86
from datetime import datetime
97

8+
#import json as jsond
9+
#^^ only for auto login/json writing/reading
10+
1011
# watch setup video if you need help https://www.youtube.com/watch?v=L2eAQOmuUiA
1112
os.system("cls")
1213
os.system("title Python Example")
@@ -23,11 +24,11 @@ def getchecksum():
2324
return digest
2425

2526
keyauthapp = api(
26-
name = "",
27-
ownerid = "",
28-
secret = "",
29-
version = "1.0",
30-
hash_to_check = getchecksum()
27+
name = "", #App name (Manage Applications --> Application name)
28+
ownerid = "", #Owner ID (Account-Settings --> OwnerID)
29+
secret = "", #App secret(Manage Applications --> App credentials code)
30+
version = "1.0",
31+
hash_to_check = getchecksum()
3132
)
3233

3334
print(f"""
@@ -66,7 +67,7 @@ def getchecksum():
6667
keyauthapp.license(key)
6768
else:
6869
print("\nNot Valid Option")
69-
sys.exit()
70+
os._exit(1)
7071

7172

7273
#region Extra Functions
@@ -114,8 +115,121 @@ def getchecksum():
114115
#* Send chat message
115116
#keyauthapp.chatSend("MESSAGE", "CHANNEL")
116117

118+
119+
#* Auto-Login Example (THIS IS JUST AN EXAMPLE --> YOU WILL HAVE TO EDIT THE CODE PROBABLY)
120+
#1. Checking and Reading JSON
121+
122+
#### Note: Remove the ''' on line 124 and 183
123+
124+
'''try:
125+
if os.path.isfile('auth.json'): #Checking if the auth file exist
126+
if jsond.load(open("auth.json"))["authusername"] == "": #Checks if the authusername is empty or not
127+
print("""
128+
1. Login
129+
2. Register
130+
""")
131+
ans=input("Select Option: ") #Skipping auto-login bc auth file is empty
132+
if ans=="1":
133+
user = input('Provide username: ')
134+
password = input('Provide password: ')
135+
keyauthapp.login(user,password)
136+
elif ans=="2":
137+
user = input('Provide username: ')
138+
password = input('Provide password: ')
139+
license = input('Provide License: ')
140+
keyauthapp.register(user,password,license)
141+
else:
142+
print("\nNot Valid Option")
143+
os._exit(1)
144+
else:
145+
try: #2. Auto login
146+
with open('auth.json', 'r') as f:
147+
authfile = jsond.load(f)
148+
authuser = authfile.get('authusername')
149+
authpass = authfile.get('authpassword')
150+
keyauthapp.login(authuser,authpass)
151+
except Exception as e: #Error stuff
152+
print(e)
153+
else: #Creating auth file bc its missing
154+
try:
155+
f = open("auth.json", "a") #Writing content
156+
f.write("""{
157+
"authusername": "",
158+
"authpassword": ""
159+
}""")
160+
f.close()
161+
print ("""
162+
1. Login
163+
2. Register
164+
""")#Again skipping auto-login bc the file is empty/missing
165+
ans=input("Select Option: ")
166+
if ans=="1":
167+
user = input('Provide username: ')
168+
password = input('Provide password: ')
169+
keyauthapp.login(user,password)
170+
elif ans=="2":
171+
user = input('Provide username: ')
172+
password = input('Provide password: ')
173+
license = input('Provide License: ')
174+
keyauthapp.register(user,password,license)
175+
else:
176+
print("\nNot Valid Option")
177+
os._exit(1)
178+
except Exception as e: #Error stuff
179+
print(e)
180+
os._exit(1)
181+
except Exception as e: #Error stuff
182+
print(e)
183+
os._exit(1)'''
184+
185+
186+
#Writing user data on login:
187+
#Check Keyauth.py file --> Line 152
188+
#Replace the whole login function with this login function (This has auto user data dumping, so the user only have to login once)
189+
#Note: The auto login function above is needed for this bc it creates the auth file, if the auth file is missing this wont work
190+
191+
'''def login(self, user, password, hwid=None):
192+
self.checkinit()
193+
if hwid is None:
194+
hwid = others.get_hwid()
195+
196+
init_iv = SHA256.new(str(uuid4())[:8].encode()).hexdigest()
197+
198+
post_data = {
199+
"type": binascii.hexlify(("login").encode()),
200+
"username": encryption.encrypt(user, self.enckey, init_iv),
201+
"pass": encryption.encrypt(password, self.enckey, init_iv),
202+
"hwid": encryption.encrypt(hwid, self.enckey, init_iv),
203+
"sessionid": binascii.hexlify(self.sessionid.encode()),
204+
"name": binascii.hexlify(self.name.encode()),
205+
"ownerid": binascii.hexlify(self.ownerid.encode()),
206+
"init_iv": init_iv
207+
}
208+
209+
response = self.__do_request(post_data)
210+
211+
response = encryption.decrypt(response, self.enckey, init_iv)
212+
213+
json = jsond.loads(response)
214+
215+
if json["success"]:
216+
self.__load_user_data(json["info"])
217+
if jsond.load(open("auth.json"))["authusername"] == "": #Check if the authusername is empty so it can write the user data
218+
authfile = jsond.load(open("Files/auth.json"))
219+
authfile["authusername"] = user #login(self, user)
220+
jsond.dump(authfile, open('Files/auth.json', 'w'), sort_keys=False, indent=4) #Dumping username to auth file/You can change the indent
221+
authfile["authpassword"] = password #login(self, password)
222+
jsond.dump(authfile, open('Files/auth.json', 'w'), sort_keys=False, indent=4) #Dumping password to auth file/You can change the indent
223+
else: #Auth file is filled with data so it skips the user data dumping
224+
pass
225+
print("successfully logged in")
226+
else:
227+
print(json["message"])
228+
os._exit(1)'''
229+
117230
#endregion
118231

232+
119233
print("\nUser data: ")
120234
print("Username: " + keyauthapp.user_data.username)
121235
print("IP address: " + keyauthapp.user_data.ip)
@@ -147,4 +261,4 @@ def getchecksum():
147261
print(f"Current Session Validation Status: {keyauthapp.check()}")
148262
print("Exiting in 10 secs....")
149263
sleep(10)
150-
sys.exit(0)
264+
os._exit(1)

0 commit comments

Comments
 (0)