import configparser import os from pathlib import Path import requests import argparse import sys parser = argparse.ArgumentParser(description='TiropitLauncher CLI & Back-End') parser.add_argument('-backend', help="Runs the app in backend mode (no UI)", action="store_true") #declare default vars class Config(): AccountKey = "NK" AccountID = "NA" version = 01.02 class ServerObj(): def __init__(self, isReachable=False): self.host = "0.0.0.0" + ":5000" self.isReachable = False self.state = "Resolving IP" @property def Icon(self): if self.isReachable: return "🌐" else: return "⛔" args = parser.parse_args() Server = ServerObj() #declare files config = configparser.ConfigParser() configPath = Path("Config.ini") class Display(): def Clear(): os.system('cls' if os.name == 'nt' else 'clear') def showIntroText(): print("Tiropit Launcher CLI - Release Branch") print(f"[TCLI]=============[Select option]==============[{Config.version}]") def WarnMissingConfig(ConfigError): print(f"[⚠️ WARNING ⚠️] - Your config file is corrupt , something that has to do with {ConfigError}") print("Program will exit before it crashes :)") reset = input("It is recommended that you reset the app data (you will be logged out) y (reset) / n (exit)") if reset == "y": os.remove(configPath) print("[⚠️ WARNING ⚠️] - Reset Done, Re-Open TiropitCLI") exit(0) def makeConfig(): config['DEFAULT'] = {'Version': Config.version} config['ACCOUNT'] = {} config['ACCOUNT']['KEY'] = "NK" config['ACCOUNT']["ID"] = "NA" with open('Config.ini', 'w') as configfile: config.write(configfile) def TiropitLogin(): class menuData(): def menuInstructions(): Display.Clear() showIntroText() print("|[About: Login Instructions] |") print("|1. Enter your username & password |") print("|2. Enter your pin (if you have) |") print("|3. Save Login key (autologin) |") print("|[e] Exit [Any] Login > |") print("======================================================") def menuUsername(): Display.Clear() showIntroText() print(f"|[LogIn: Login page] [{Server.Icon}]|") print("| Enter your username |") print("| |") print("| |") print("| |") print("======================================================") menuData.menuInstructions() option = input(">> ") match option: case "e": showMenu(1) return 0 case _: menuData.menuUsername() def loadConfig(): try: config.read(configPath) Config.version = config["DEFAULT"]["Version"] Config.AccountID = config["ACCOUNT"]["ID"] Config.AccountID = config["ACCOUNT"]["KEY"] except Exception as e: WarnMissingConfig(e) def disconnect(): Server.host = "0.0.0.0" Server.state = "Offline" Server.isReachable = False if args.backend: print("4:OFFLINE") def grabServerResolver(): Server.state = "Resolving IP" try: response = requests.get("https://kevinblog.sytes.net/APIS/test1.php") response.raise_for_status() data = response.json() server_ip = data.get("HostIp") Server.isReachable = True Server.state = "Connected" Server.host = server_ip if args.backend: print(f"2:CONNECTED TO: {server_ip}") return server_ip except requests.exceptions.RequestException as e: Server.isReachable = False Server.state = f"Failed/Timed Out - {e}" if args.backend: print(f"3:FAILED CONN TO: {e}") else: print("Error resolving a host, Considering service is offline.") pass def checkConfig(): if configPath.exists(): print("The Config file exists.") loadConfig() else: print("The file Config does not exist. - Creating") makeConfig() def debugMenu(): class menuData(): def mainMenu(): Display.Clear() showIntroText() print(f"|[DBG] [{Server.Icon}]|") print("|1. Read Server Data |") print("|2. |") print("|3. |") print("|[e] Exit |") print("======================================================") option = input(">> ") if option == "": menuData.mainMenu() match option: case "e": showMenu(1) return 0 case "1": menuData.menuServerData() case _: menuData.mainMenu() def menuServerData(): Display.Clear() showIntroText() print(f"|[DBG: Login page] [{Server.Icon}]|") print(f"| IsReachable : {Server.isReachable}") print(f"| Host : {Server.host}") print(f"| State : {Server.state}") print("[e] Exit | [R] Reconnect | [O] Offline (Disconnect)") print("======================================================") option = input(">> ") if option == "": menuData.menuServerData() match option: case "e": showMenu(1) return 0 case "r": grabServerResolver() menuData.menuServerData() case "o": disconnect() menuData.menuServerData() case _: menuData.menuServerData() menuData.mainMenu() option = input(">> ") if option == "": menuData.mainMenu() match option: case "e": showMenu(1) return 0 case "1": menuData.menuServerData() case _: menuData.mainMenu() def showMenu(menuID): Display.Clear() showIntroText() match menuID: case 1: print("|[Main menu] |") print("|[1] Log in to tiropit | [2] Reset Data & Config |") print("|[3] Send / Get Request| [4] My Profile |") print("|[5] Debug Data | |") print("|[e] Exit |") print("======================================================") option = input(">> ") if option == "": showMenu(1) print(option) match option: case "1": TiropitLogin() case "5": debugMenu() case "e": print("Cya!") exit(); case _: showMenu(1) pass #Begin if not args.backend: grabServerResolver() checkConfig() showMenu(1) else: print("1:=== BACKEND MODE ===") grabServerResolver()