caterpillar/web.py

115 lines
3.0 KiB
Python
Raw Normal View History

2024-05-19 17:20:49 +00:00
#!/usr/bin/python3
#
# web.py
2024-05-19 17:22:20 +00:00
# server file with HTTP connection mode
2024-05-19 17:20:49 +00:00
#
2024-06-20 08:21:19 +00:00
# Caterpillar Proxy - The simple web debugging proxy (formerly, php-httpproxy)
2024-05-19 17:20:49 +00:00
# Namyheon Go (Catswords Research) <gnh1201@gmail.com>
# https://github.com/gnh1201/caterpillar
# Created at: 2024-05-20
2024-10-24 23:59:49 +00:00
# Updated at: 2024-10-25
2024-05-19 17:20:49 +00:00
#
2024-05-19 17:16:12 +00:00
import os
import sys
from decouple import config
2024-07-11 07:02:51 +00:00
from flask import Flask, request, render_template
2024-10-24 23:59:49 +00:00
from flask_cors import CORS
2024-07-11 07:02:51 +00:00
from base import Extension, jsonrpc2_error_encode, Logger
2024-05-19 17:16:12 +00:00
# TODO: 나중에 Flask 커스텀 핸들러 구현 해야 함
logger = Logger(name="web")
2024-05-19 17:16:12 +00:00
app = Flask(__name__)
2024-10-24 23:59:49 +00:00
CORS(app)
2024-07-11 10:02:08 +00:00
app.config["UPLOAD_FOLDER"] = "data/"
if not os.path.exists(app.config["UPLOAD_FOLDER"]):
os.makedirs(app.config["UPLOAD_FOLDER"])
2024-05-19 17:16:12 +00:00
2024-07-11 07:02:51 +00:00
2024-07-11 10:02:08 +00:00
@app.route("/")
2024-05-19 17:16:12 +00:00
def upload_form():
2024-07-11 10:02:08 +00:00
return render_template("upload.html")
2024-05-19 17:16:12 +00:00
2024-07-11 07:02:51 +00:00
2024-07-11 10:02:08 +00:00
@app.route("/upload", methods=["POST"])
2024-05-19 17:16:12 +00:00
def process_upload():
# make connection profile from Flask request
conn = Connection(request)
# pass to the method
2024-07-11 10:02:08 +00:00
method = request.form["method"]
filename = request.files["file"].filename
params = {"filename": filename}
2024-07-11 07:02:51 +00:00
2024-05-19 17:16:12 +00:00
# just do it
2024-07-11 10:02:08 +00:00
return Extension.dispatch_rpcmethod(method, "call", "", params, conn)
2024-05-19 17:16:12 +00:00
2024-07-11 07:02:51 +00:00
2024-07-11 10:02:08 +00:00
@app.route("/jsonrpc2", methods=["POST"])
2024-05-19 17:16:12 +00:00
def process_jsonrpc2():
# make connection profile from Flask request
conn = Connection(request)
# JSON-RPC 2.0 request
2024-08-31 06:48:35 +00:00
json_data = request.get_json(silent=True)
if json_data["jsonrpc"] == "2.0":
2024-10-24 23:59:49 +00:00
result = Extension.dispatch_rpcmethod(
json_data["method"], "call", json_data["id"], json_data["params"], conn)
return {
"jsonrpc": "2.0",
"result": {
"data": result
},
"id": None
}
2024-05-19 17:16:12 +00:00
# when error
2024-08-31 06:48:35 +00:00
return jsonrpc2_error_encode({"message": "Not valid JSON-RPC 2.0 request"})
2024-05-19 17:16:12 +00:00
2024-07-11 07:02:51 +00:00
2024-08-31 06:48:35 +00:00
def jsonrpc2_server(conn, _id, method, params):
return Extension.dispatch_rpcmethod(method, "call", _id, params, conn)
2024-05-19 17:16:12 +00:00
2024-07-11 07:02:51 +00:00
2024-07-11 10:02:08 +00:00
class Connection:
2024-05-19 17:16:12 +00:00
def send(self, data):
self.messages.append(data)
2024-05-19 17:40:00 +00:00
def recv(self, size):
logger.info("Not allowed method")
2024-05-19 17:16:12 +00:00
def close(self):
logger.info("Not allowed method")
2024-05-19 17:16:12 +00:00
def __init__(self, req):
self.messages = []
self.request = req
2024-07-11 07:02:51 +00:00
2024-05-19 17:16:12 +00:00
if __name__ == "__main__":
2024-07-11 07:02:51 +00:00
# initialization
2024-05-19 17:16:12 +00:00
try:
2024-07-11 10:02:08 +00:00
listening_port = config("PORT", default=5555, cast=int)
client_encoding = config("CLIENT_ENCODING", default="utf-8")
use_extensions = config("USE_EXTENSIONS", default="")
2024-05-19 17:16:12 +00:00
except KeyboardInterrupt:
logger.warning("[*] User has requested an interrupt")
logger.warning("[*] Application Exiting.....")
2024-05-19 17:16:12 +00:00
sys.exit()
except Exception as e:
logger.error("[*] Failed to initialize", exc_info=e)
2024-05-19 17:16:12 +00:00
# set environment of Extension
2024-07-11 10:02:08 +00:00
Extension.set_protocol("http")
2024-05-19 17:16:12 +00:00
2024-07-11 07:02:51 +00:00
# Fix Value error
if use_extensions:
# load extensions
2024-07-11 10:02:08 +00:00
for s in use_extensions.split(","):
2024-07-11 07:02:51 +00:00
Extension.register(s)
else:
logger.warning("[*] No extensions registered")
2024-05-19 17:16:12 +00:00
2024-07-11 10:02:08 +00:00
app.run(debug=True, host="0.0.0.0", port=listening_port)