From b07a4b79579c8816a73fe41617e1803209e74f12 Mon Sep 17 00:00:00 2001 From: kageru Date: Mon, 20 Nov 2017 15:12:02 +0100 Subject: [PATCH 1/3] started undoing bad ideas --- config.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/config.py b/config.py index e3a033b..3c03ba9 100644 --- a/config.py +++ b/config.py @@ -3,25 +3,21 @@ uploader = 'sftp' # (S)FTP credentials, only if you want to use SFTP for uploads # Set all to None that you don’t need -sftp_address = '' +sftp_address = 'your_domain.com' +sftp_port = 22 username = None password = None private_key = None - -# curl parameters, if you're using pyshare_receivere -curl_target = 'your_domain.com:5000' -curl_user = 'user1' -curl_password = 'password' +private_key_pass = None # This should contain a complete curl command with a {} to insert the filename. -# Setting this to anything but None will result in the other curl parameters to be ignored # 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/{}' From 7884339cbb63b3edb5a35d932ae738d992b96dde Mon Sep 17 00:00:00 2001 From: kageru Date: Mon, 20 Nov 2017 15:13:47 +0100 Subject: [PATCH 2/3] simplified arguments --- pyshare.py | 60 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/pyshare.py b/pyshare.py index 02b4c74..997e9a2 100644 --- a/pyshare.py +++ b/pyshare.py @@ -6,7 +6,7 @@ from pysftp import Connection from subprocess import call from random import choices import pyperclip -import config as config +import config import sys import os import re @@ -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,16 +30,28 @@ 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(filename: str) -> None: - call(["escrotum", filename, "-s"]) +def take_screenshot() -> None: + tmppath = os.path.join(config.local_directory, 'tmp') + tmpdir = os.listdir(tmppath) + for f in tmpdir: + os.remove(os.path.join(tmppath, f)) + # you can also use programs like escrotum here, but i3-scrot was much faster for me + call(['i3-scrot', '-s']) + file = os.path.join(config.local_directory, 'tmp', os.listdir(tmppath)[0]) + ftp_upload(ext='png', sourcefile=file) + os.remove(file) -def ftp_upload(mode='screenshot', ext=None, sourcefile=None) -> tuple: +def ftp_upload(sourcefile, *, mode=None, ext=None) -> tuple: if ext is None: + # TODO files without extension exts = { 'screenshot': 'png', 'text': 'txt', @@ -55,29 +59,22 @@ def ftp_upload(mode='screenshot', 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) fullpath = os.path.join(config.local_directory, filename) - if mode == 'screenshot': - take_screenshot(fullpath) - conn.put(fullpath) - notify_user(config.url_template.format(filename)) - elif mode == 'file': + if mode == 'file': conn.put(sourcefile, filename) + + notify_user(config.url_template.format(filename)) return fullpath, filename 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): @@ -115,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: @@ -124,8 +126,8 @@ if __name__ == '__main__': elif args.mode == 'text': parse_clipboard(args) else: - ftp_upload(mode='screenshot') - """ + take_screenshot() + elif args.files is not None: if config.uploader in ['ftp', 'sftp']: From b842051db4d5479812a618194c8e8b7addc5b890 Mon Sep 17 00:00:00 2001 From: kageru Date: Mon, 20 Nov 2017 15:15:08 +0100 Subject: [PATCH 3/3] removed the receiver --- pyshare_receiver.py | 70 --------------------------------------------- 1 file changed, 70 deletions(-) delete mode 100644 pyshare_receiver.py 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)