From 22722d368ed4fbdd006bc774339bbf85851294ab Mon Sep 17 00:00:00 2001 From: kageru Date: Mon, 20 Nov 2017 15:03:59 +0100 Subject: [PATCH] =?UTF-8?q?the=20whole=20receiver=20thing=20was=20a=20bad?= =?UTF-8?q?=20ide=20anyway=20=C2=AF\=5F(=E3=83=84)=5F/=C2=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 4 +-- pyshare.py | 35 ++++++++++------------- pyshare_receiver.py | 70 --------------------------------------------- 3 files changed, 17 insertions(+), 92 deletions(-) delete mode 100644 pyshare_receiver.py diff --git a/config.py b/config.py index 347bf6e..0ebf0bc 100644 --- a/config.py +++ b/config.py @@ -11,12 +11,12 @@ private_key_pass = None # This should contain a complete curl command with a {} to insert the filename. # Example: 'curl -F"file=@{}" https://0x0.st' -custom_curl_command = None +curl_command = None # This is where the screenshots are saved locally local_directory = '/home/kageru/pyshare/' -# Directory on the ftp server where you want the screenshots to be sent to +# Directory on the ftp server where you want the screenshots to be sent remote_directory = '/usr/share/nginx/html/pyshare/' # Template for the link that the script will generate. {} is the filename url_template = 'https://your_domain.com/pyshare/{}' diff --git a/pyshare.py b/pyshare.py index c7fa7a4..997e9a2 100755 --- a/pyshare.py +++ b/pyshare.py @@ -14,14 +14,6 @@ import re character_pool = ascii_letters + digits -def parse_arguments(): - parser = ArgumentParser() - parser.add_argument('-m', '--mode', type=str, nargs='?', - help="Specify the mode. Can be 'screenshot' to open a screencap tool and upload the image or 'text' to perform an operation on the clipboard contents. Implicit if --file is specified.") - parser.add_argument('-f', '--files', type=str, nargs='*', help='List of files to be uploaded') - return parser.parse_args() - - def generate_filename(length, ext, prefix=''): return prefix + ''.join(choices(character_pool, k=length)) + '.' + ext @@ -38,8 +30,11 @@ def find_valid_filename(prefix, length, ext, conn): return filename def upload_local_file(path: str) -> str: - filename = ftp_upload(mode='file', sourcefile=path)[1] - return config.url_template.format(filename) + if config.uploader in ['ftp', 'sftp']: + filename = ftp_upload(path)[1] + return config.url_template.format(filename) + else: + return curl_upload(path) def take_screenshot() -> None: @@ -54,7 +49,7 @@ def take_screenshot() -> None: os.remove(file) -def ftp_upload(mode='file', ext=None, sourcefile=None) -> tuple: +def ftp_upload(sourcefile, *, mode=None, ext=None) -> tuple: if ext is None: # TODO files without extension exts = { @@ -64,7 +59,7 @@ def ftp_upload(mode='file', ext=None, sourcefile=None) -> tuple: ext = exts.get(mode, mode not in exts and sourcefile.split('.')[-1]) # Only do the split if necessary with Connection(config.sftp_address, username=config.username, password=config.password, - private_key=config.private_key) as conn: + private_key=config.private_key, private_key_pass=config.private_key_pass) as conn: conn.chdir(config.remote_directory) filename = find_valid_filename(prefix=config.prefix, length=config.length, ext=ext, conn=conn) @@ -79,12 +74,7 @@ def ftp_upload(mode='file', ext=None, sourcefile=None) -> tuple: def curl_upload(filename): - if config.custom_curl_command is not None: - return call(config.custom_curl_command) - else: - return call( - f'curl -k -F"file=@{filename}" -F"name={config.username}" -F"passwd={config.password}" {config.curl_target}') - + return call(config.custom_curl_command) def notify_user(url): @@ -122,8 +112,13 @@ def upload_text(text): if __name__ == '__main__': - args = parse_arguments() + if len(sys.argv) == 1: + take_screenshot() + else: + for text in sys.argv[1:]: + parse_clipboard(text) + """ if config.uploader in ['ftp', 'sftp']: if args.files is not None: for file in args.files: @@ -132,7 +127,7 @@ if __name__ == '__main__': parse_clipboard(args) else: take_screenshot() - """ + elif args.files is not None: if config.uploader in ['ftp', 'sftp']: diff --git a/pyshare_receiver.py b/pyshare_receiver.py deleted file mode 100644 index b36fcf0..0000000 --- a/pyshare_receiver.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 -# this file can be run on a server to act as the endpoint of the script. -# I mainly want to test whether this is faster than sftp - -from flask import Flask, request -from werkzeug.utils import secure_filename -from hashlib import sha3_256 -from users import users -import os -import config -from string import ascii_letters, digits -from random import choices - -character_pool = ascii_letters + digits - -app = Flask(__name__) - - -def salthash(password, salt): - return sha3_256((password + salt).encode('utf8')).hexdigest() - - -def authenticate(request): - print(request.form) - if 'name' not in request.form or 'passwd' not in request.form: - return False - name = request.form.get('name') - passwd = request.form.get('passwd') - if name in users: - user = users.get(name) - if salthash(passwd, user[1]) == user[0]: - return True - return False - - -def find_filename(length, ext): - def generate_filename(length, ext): - return ''.join(choices(character_pool, k=length)) + ext - - filename = generate_filename(length, ext) - i = 0 - while os.path.exists(os.path.join(config.remote_directory, filename)): - i += 1 - if i > 1000: - length += 1 - i = 0 - filename = generate_filename(length, ext) - return filename - - -@app.route('/', methods=['POST']) -def receive_file() -> tuple: - if 'file' in request.files: - if authenticate(request) is True: - file = request.files.get('file') - filename = secure_filename(file.filename) - if '.' in filename: - extension = '.' + filename.rsplit('.', 1)[1] - else: - extension = '' - storename = find_filename(config.length, extension) - file.save(os.path.join(config.remote_directory, storename)) - return config.url_template.format(storename), 201 - else: - return 'Wrong or no credentials', 403 - return 'you\'re doing this wrong', 418 - - -if __name__ == "__main__": - app.run(ssl_context='adhoc', port=config.remote_port)