the whole receiver thing was a bad ide anyway ¯\_(ツ)_/¯
This commit is contained in:
parent
d1373fda03
commit
22722d368e
|
@ -11,12 +11,12 @@ private_key_pass = None
|
||||||
|
|
||||||
# This should contain a complete curl command with a {} to insert the filename.
|
# This should contain a complete curl command with a {} to insert the filename.
|
||||||
# Example: 'curl -F"file=@{}" https://0x0.st'
|
# Example: 'curl -F"file=@{}" https://0x0.st'
|
||||||
custom_curl_command = None
|
curl_command = None
|
||||||
|
|
||||||
# This is where the screenshots are saved locally
|
# This is where the screenshots are saved locally
|
||||||
local_directory = '/home/kageru/pyshare/'
|
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/'
|
remote_directory = '/usr/share/nginx/html/pyshare/'
|
||||||
# Template for the link that the script will generate. {} is the filename
|
# Template for the link that the script will generate. {} is the filename
|
||||||
url_template = 'https://your_domain.com/pyshare/{}'
|
url_template = 'https://your_domain.com/pyshare/{}'
|
||||||
|
|
31
pyshare.py
31
pyshare.py
|
@ -14,14 +14,6 @@ import re
|
||||||
character_pool = ascii_letters + digits
|
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=''):
|
def generate_filename(length, ext, prefix=''):
|
||||||
return prefix + ''.join(choices(character_pool, k=length)) + '.' + ext
|
return prefix + ''.join(choices(character_pool, k=length)) + '.' + ext
|
||||||
|
|
||||||
|
@ -38,8 +30,11 @@ def find_valid_filename(prefix, length, ext, conn):
|
||||||
return filename
|
return filename
|
||||||
|
|
||||||
def upload_local_file(path: str) -> str:
|
def upload_local_file(path: str) -> str:
|
||||||
filename = ftp_upload(mode='file', sourcefile=path)[1]
|
if config.uploader in ['ftp', 'sftp']:
|
||||||
|
filename = ftp_upload(path)[1]
|
||||||
return config.url_template.format(filename)
|
return config.url_template.format(filename)
|
||||||
|
else:
|
||||||
|
return curl_upload(path)
|
||||||
|
|
||||||
|
|
||||||
def take_screenshot() -> None:
|
def take_screenshot() -> None:
|
||||||
|
@ -54,7 +49,7 @@ def take_screenshot() -> None:
|
||||||
os.remove(file)
|
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:
|
if ext is None:
|
||||||
# TODO files without extension
|
# TODO files without extension
|
||||||
exts = {
|
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
|
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,
|
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)
|
conn.chdir(config.remote_directory)
|
||||||
|
|
||||||
filename = find_valid_filename(prefix=config.prefix, length=config.length, ext=ext, conn=conn)
|
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):
|
def curl_upload(filename):
|
||||||
if config.custom_curl_command is not None:
|
|
||||||
return call(config.custom_curl_command)
|
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}')
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def notify_user(url):
|
def notify_user(url):
|
||||||
|
@ -122,8 +112,13 @@ def upload_text(text):
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
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 config.uploader in ['ftp', 'sftp']:
|
||||||
if args.files is not None:
|
if args.files is not None:
|
||||||
for file in args.files:
|
for file in args.files:
|
||||||
|
@ -132,7 +127,7 @@ if __name__ == '__main__':
|
||||||
parse_clipboard(args)
|
parse_clipboard(args)
|
||||||
else:
|
else:
|
||||||
take_screenshot()
|
take_screenshot()
|
||||||
"""
|
|
||||||
elif args.files is not None:
|
elif args.files is not None:
|
||||||
|
|
||||||
if config.uploader in ['ftp', 'sftp']:
|
if config.uploader in ['ftp', 'sftp']:
|
||||||
|
|
|
@ -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)
|
|
Loading…
Reference in New Issue
Block a user