diff --git a/modules/auth.py b/modules/auth.py index f1611a7..004979f 100644 --- a/modules/auth.py +++ b/modules/auth.py @@ -1,4 +1,6 @@ import json +import sys + import modules.vars as horsy_vars import ctypes @@ -33,6 +35,22 @@ def get_auth(is_gui=False, login_ui=None, Ui_LoginWindow=None): login_ui.login_button.clicked.connect(load_login_now) +def get_auth_without_login(is_gui=False): + with open(horsy_vars.horsypath + 'config.cfg') as f: + config = json.load(f) + + try: + if config['auth']: + return config['auth'] + else: + raise Exception('No auth found') + except: + ctypes.windll.user32.MessageBoxW(0, "Login not found. Please, use the login button on account tab. " + "horsy will close now, but you don't need to restart it later", + "No auth", 0) + sys.exit(0) + + def get_gui_auth(login_ui, Ui_LoginWindow): with open(horsy_vars.horsypath + 'config.cfg') as f: config = json.load(f) diff --git a/modules/download.py b/modules/download.py new file mode 100644 index 0000000..6bf2edb --- /dev/null +++ b/modules/download.py @@ -0,0 +1,20 @@ +import threading +from tqdm import tqdm +import requests + + +def dl(urls, save_to): + for url in urls: + def dl_thread(url_in_thread): + with requests.get(url_in_thread, stream=True) as r: + with open(save_to, "wb") as f: + pbar = tqdm(unit="B", unit_scale=True, total=int(r.headers['Content-Length']), + position=urls.index(url_in_thread)) + for chunk in r.iter_content(chunk_size=1024): + if chunk: + if pbar.n < pbar.total - 1: + pbar.update(len(chunk)) + f.write(chunk) + pbar.clear() + + threading.Thread(target=dl_thread, args=(url,)).start() diff --git a/modules/http_status.py b/modules/http_status.py index 6333088..fc6b67e 100644 --- a/modules/http_status.py +++ b/modules/http_status.py @@ -73,7 +73,7 @@ codes = {'CONTINUE': 100, 406: 'NOT_ACCEPTABLE', 407: 'PROXY_AUTHENTICATION_REQUIRED', 408: 'REQUEST_TIMEOUT', - 409: 'CONFLICT', + 409: 'You are trying to create a resource with name that already exists', 410: 'GONE', 411: 'LENGTH_REQUIRED', 412: 'PRECONDITION_FAILED', diff --git a/modules/manager.py b/modules/manager.py index 6fbf096..98472b2 100644 --- a/modules/manager.py +++ b/modules/manager.py @@ -7,24 +7,17 @@ from tqdm import tqdm import os import zipfile from modules.virustotal import get_key, scan_file, get_report +from modules.http_status import handle def install(package): - r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}").text - if r == "": - print(f"[red]Package {package} not found[/]") - return - try: - r = json.loads(r) - except: - print("[red]Error with unsupported message[/]") - return - try: - if r["message"] == "Internal server error": - print("[red]Internal server error[/]") - return - except: - pass + r = requests.get(f"{horsy_vars.protocol}{horsy_vars.server_url}/packages/json/{package}") + r_code = handle(r.status_code) + r = r.text + r = json.loads(r) + + if r_code[1] not in [403, 401]: + print(r_code[0]) try: print(f"[green]App {r['name']} found, information loaded[/]") @@ -32,7 +25,6 @@ def install(package): if not os.path.exists('{1}apps/{0}'.format(r['name'], horsy_vars.horsypath)): os.makedirs('{1}apps/{0}'.format(r['name'], horsy_vars.horsypath)) - # if not is_gui: print(f"Downloading {r['url'].split('/')[-1]}") chunk_size = 1024 @@ -110,6 +102,7 @@ def install(package): threading.Thread(target=os.system, args=('{2}apps/{0}/{1}'.format(r['name'], r['install'], horsy_vars.horsypath),)).start() + # Launch script print(f"Generating launch script") with open('{1}apps/{0}.bat'.format(r['name'], horsy_vars.horsypath), 'w') as f: @@ -118,6 +111,7 @@ def install(package): f.write(f"cd %horsypath%/apps/{r['name']}\n") f.write(f"{r['run']} %*\n") + # Done message print(f"[green][OK] All done![/]") print(f"[green]You can run your app by entering [italic white]{r['name']}[/] in terminal[/]") diff --git a/modules/uploader.py b/modules/uploader.py index 81ff2f3..fa71c46 100644 --- a/modules/uploader.py +++ b/modules/uploader.py @@ -2,7 +2,7 @@ import json import time import requests from rich import print -from modules.auth import get_auth, del_auth +from modules.auth import get_auth, del_auth, get_auth_without_login import re import modules.vars as horsy_vars import os @@ -94,7 +94,7 @@ def upload(is_gui=False, ui=None, login_ui=None, Ui_LoginWindow=None): } else: - auth = get_auth(is_gui, login_ui, Ui_LoginWindow) + auth = get_auth_without_login(is_gui) project_name = ui.packagename_box.text() if not matches(project_name) or len(project_name) > 64 or len(project_name) < 3: @@ -153,7 +153,7 @@ def upload(is_gui=False, ui=None, login_ui=None, Ui_LoginWindow=None): print(r) r = None - elif r_code == 200: + elif r_code[1] in [200, 201]: print('[green]Success, your project is created. You can install it by running[/] ' '[i]horsy i {0}[/]'.format(request['name'])) return 'Success, your project is created. You can install it by running horsy i {0}'.format(