Created
August 19, 2022 22:43
-
-
Save 0xpizza/05d37aa623c618a4f56583f4f06cba34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python3 | |
#coding:ascii | |
try: | |
import lzma | |
except ImportError: | |
print('Error: lzma module required.') | |
raise SystemExit(1) | |
import os | |
import pathlib | |
import secrets | |
import sys | |
import zipfile | |
import flask.sessions | |
flask.sessions.SecureCookieSessionInterface.salt = secrets.token_bytes(32) | |
from flask import Flask, request | |
from werkzeug.utils import secure_filename | |
app = Flask(__name__) | |
app.secret_key = secrets.token_bytes(32) | |
INDEX_HTML = ''' | |
<html> | |
<head> | |
<script> | |
function sendForm(){ | |
document.getElementById('uploadsubmit').setAttribute('disabled', ''); | |
document.getElementById('uploadform').submit(); | |
} | |
</script> | |
</head> | |
<body> | |
<form id=uploadform action="/" method="POST" enctype="multipart/form-data"> | |
<label for=outputname>Zipfile name:</labe> | |
<input type=text id=outputname name=outputname> | |
<span>(Leave blank for a random string)</span><br> | |
<label for="files">Select files:</label> | |
<input type="file" id="fileupload" name="fileupload" multiple><br> | |
<input | |
id=uploadsubmit type="submit" | |
onclick="sendForm();" | |
> | |
</form> | |
</body> | |
</html> | |
''' | |
WORKING_DIR = pathlib.Path(__file__).absolute().parent | |
def sanitize_filename(filename=''): | |
if not filename: | |
return secrets.token_urlsafe(8) | |
return secure_filename(filename)[:64] | |
@app.route('/', methods=['GET', 'POST']) | |
def index(): | |
if request.method == 'GET': | |
return INDEX_HTML | |
elif request.method == 'POST': | |
zip_name = sanitize_filename( | |
request.form.get('outputname', '').replace('.zip', '') | |
) + '.zip' | |
try: | |
zip_file = zipfile.ZipFile( | |
zip_name, 'x', compression=zipfile.ZIP_LZMA | |
) | |
except FileExistsError: | |
print(zip_name, 'already exists!!') | |
try: | |
zip_name = '{}-{}.zip'.format( | |
zip_name[:-4], secrets.token_bytes(3).hex() | |
) | |
zip_file = zipfile.ZipFile( | |
zip_name, 'x', compression=zipfile.ZIP_LZMA | |
) | |
except FileExistsError: | |
print('Something weird is happening. Good bye :)') | |
raise SystemExit | |
with zip_file: | |
for inputname in request.files.keys(): | |
for request_file in request.files.getlist(inputname): | |
zipped_filename = sanitize_filename(request_file.filename) | |
with zip_file.open(zipped_filename, 'w') as f: | |
request_file.save(f) | |
return (''' | |
<html> | |
<body> | |
<p>Created: <code>{}</code></p> | |
<form action="/"> | |
<input type=submit value="Ok"/> | |
</form> | |
</body> | |
</html> | |
'''.format(WORKING_DIR / zip_name)) | |
else: | |
return 405 | |
def main(): | |
if sys.platform == 'win32': | |
os.system('ipconfig') | |
elif sys.platform == 'linux': | |
os.system('ip a') | |
print() | |
app.run(host='0.0.0.0', port=8080, debug=False) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment