From 13f06f130cd24059b6bec382f23fe9a913b6ce9f Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Mon, 20 May 2024 02:16:12 +0900 Subject: [PATCH] Create web.py --- web.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 web.py diff --git a/web.py b/web.py new file mode 100644 index 0000000..f63be60 --- /dev/null +++ b/web.py @@ -0,0 +1,87 @@ +from flask import Flask, request, redirect, url_for, render_template +import os +import sys +import json +import importlib + +import hashlib +from decouple import config + +from base import Extension, jsonrpc2_create_id, jsonrpc2_result_encode, jsonrpc2_error_encode + +app = Flask(__name__) +app.config['UPLOAD_FOLDER'] = 'data/' + +if not os.path.exists(app.config['UPLOAD_FOLDER']): + os.makedirs(app.config['UPLOAD_FOLDER']) + +@app.route('/') +def upload_form(): + return render_template('upload.html') + +@app.route('/upload', methods=['POST']) +def process_upload(): + # make connection profile from Flask request + conn = Connection(request) + + # pass to the method + method = request.form['method'] + filename = request.files['file'].filename + params = { + 'filename': filename + } + + # just do it + return Extension.dispatch_rpcmethod(method, 'call', '', params, conn) + +@app.route('/jsonrpc2', methods=['POST']) +def process_jsonrpc2(): + # make connection profile from Flask request + conn = Connection(request) + + # JSON-RPC 2.0 request + jsondata = request.get_json(silent=True) + if jsondata['jsonrpc'] == "2.0": + return Extension.dispatch_rpcmethod(jsondata['method'], 'call', jsondata['id'], jsondata['params'], conn) + + # when error + return jsonrpc2_error_encode({ + 'message': "Not vaild JSON-RPC 2.0 request" + }) + +def jsonrpc2_server(conn, id, method, params): + return Extension.dispatch_rpcmethod(method, "call", id, params, conn) + +class Connection(): + def send(self, data): + self.messages.append(data) + + def recv(self): + print ("Not allowed") + + def close(self): + print ("Not allowed") + + def __init__(self, req): + self.messages = [] + self.request = req + +if __name__ == "__main__": + # initalization + try: + listening_port = config('PORT', default=5555, cast=int) + client_encoding = config('CLIENT_ENCODING', default='utf-8') + except KeyboardInterrupt: + print("\n[*] User has requested an interrupt") + print("[*] Application Exiting.....") + sys.exit() + except Exception as e: + print("[*] Failed to initialize:", str(e)) + + # set environment of Extension + Extension.set_protocol('http') + + # load extensions + #Extension.register(importlib.import_module("plugins.yourownplugin").YourOwnPlugin()) + + app.run(debug=True, host='0.0.0.0', port=listening_port)