From 5c0a275a128afb7255c99a42bf599c3f2cc9e24b Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Thu, 20 Sep 2018 20:58:08 +0300 Subject: [PATCH] get client without auth --- code/app.py | 17 ++++++----- code/templates/client.py | 62 ++++++++++++++++++++++++++++++++++------ 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/code/app.py b/code/app.py index 5bcd053..a324c88 100644 --- a/code/app.py +++ b/code/app.py @@ -14,7 +14,7 @@ from utils.utils import * from utils.crypt import * -__FLEES_VERSION__ = "20180919.0" +__FLEES_VERSION__ = "20180920.0" app = Flask(__name__) app.config.from_object(__name__) config_values = read_config(app) @@ -550,15 +550,13 @@ def download_zip(name, token = None): ) +@app.route('/script/client', methods=['GET']) @app.route('/script/client//', methods=['GET']) -def script_client(name = None, token = None): - (ok,share) = get_share(name, token = token) - if not ok: - notify({ - "share": name, - "operation": "unauthorized_script_client" - }) - return share +def script_client(name = "", token = ""): + notify({ + 'share': name, + 'operation': 'script_client' + }) return render_template( "client.py", name = name, @@ -566,6 +564,7 @@ def script_client(name = None, token = None): rooturl = request.url_root ) + @app.route('/script/flip//', methods=['GET']) @app.route('/script/flip', methods=['GET']) def script_flip(name = "", token = ""): diff --git a/code/templates/client.py b/code/templates/client.py index 6a52218..d38b0c4 100644 --- a/code/templates/client.py +++ b/code/templates/client.py @@ -72,6 +72,22 @@ class Completer(object): return results[state] +def check_connection(opts): + p = Popen( + [ + 'curl','-s','-f', + '%s%s/%s/%s'%(opts.rooturl, "file/list", opts.share, opts.token) + ], + stdout=PIPE, + stderr=PIPE + ) + p.communicate() + rc = p.returncode + if rc != 0: + return False + return True + + def download_file(file, opts, filename = False): print("Download " + file['name']) if not filename: @@ -121,12 +137,17 @@ def edit_file(file): def menu(opts): - commands = { - '1': {'name': 'Download', 'cmd': menu_download}, - '2': {'name': 'Upload', 'cmd': menu_upload}, - '3': {'name': 'Edit', 'cmd': menu_edit}, - } print_title("Main menu") + commands = { + '4': {'name': 'Change login', 'cmd': menu_login} + } + if check_connection(opts): + commands['1'] = {'name': 'Download', 'cmd': menu_download} + commands['2'] = {'name': 'Upload', 'cmd': menu_upload} + commands['3'] = {'name': 'Edit', 'cmd': menu_edit} + else: + print(" Not a valid login") + for command in sorted(commands): print(" %s. %s"%( command, commands[command]['name'])) comp = Completer(choices = commands.keys()) @@ -134,20 +155,24 @@ def menu(opts): print("\n[Empty to exit]") choice = user_input("Number of action: ").strip() if choice in commands: - commands[choice]['cmd'](opts) + value = commands[choice]['cmd'](opts) + if isinstance(value, dict): + opts = value else: sys.exit(0) + return opts def menu_download(opts): while True: print_title("Download files") + files = json.loads(run_command("file/details", opts)) file_table = [] for f in files: file_table.append(( - f['name'], - float(f['size'].replace(",","")), + f['name'], + float(f['size'].replace(",","")), f['mtime'] )) print(tabulate(file_table, headers = ("Name", "Size [Mb]", "Modified"))) @@ -206,6 +231,25 @@ def menu_edit(opts): os.remove(tmp) +def menu_login(opts): + print_title("Login") + print("\n[Empty to keep previous value]") + + + choice = user_input('Host [%s]: '%( opts.rooturl, )).strip() + if choice != "": + opts.rooturl = choice + + choice = user_input('Share name [%s]: '%( opts.share, )).strip() + if choice != "": + opts.share = choice + + choice = user_input('Token [%s]: '%( opts.token, )).strip() + if choice != "": + opts.token = choice + + return opts + def menu_upload(opts): while True: print_title("Upload") @@ -301,7 +345,7 @@ def run_command(command, opts): def run_loop(opts): try: while True: - menu(opts) + opts = menu(opts) except KeyboardInterrupt: print("") return