From 3f2f7475f29ad7e2f5e6b7fcf1da1663c3138d1a Mon Sep 17 00:00:00 2001 From: pictures2333 Date: Thu, 7 Nov 2024 16:55:35 +0800 Subject: [PATCH] rev proxy --- app.py | 50 ++++++++++++++++++++++++++--- blueprints/admin.py | 0 blueprints/article.py | 73 ------------------------------------------- blueprints/log.py | 0 4 files changed, 46 insertions(+), 77 deletions(-) delete mode 100644 blueprints/admin.py delete mode 100644 blueprints/article.py delete mode 100644 blueprints/log.py diff --git a/app.py b/app.py index cf51fa0..3a87318 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,56 @@ -from flask import Flask, session, request, redirect -import os -from blueprints.article import article +from flask import Flask, session, request, redirect, Response +import os, requests, time +from hashlib import sha256 from supaclient import supaClient +from dotenv import load_dotenv +load_dotenv() + +URL = os.getenv("SUPABASE_IP") + "/" app = Flask(__name__) app.config["SECRET_KEY"] = os.urandom(64) app.shared_resource = supaClient() # blueprints -app.register_blueprint(article, url_prefix = '/api/article') + +# main +@app.route("/") +def index(): return "Hello, world!" + +# reverse proxy +@app.route('/r/',methods=['GET', 'POST']) +def proxy(path): + if request.method=='GET': + headers = request.headers + resp = requests.get(f'{URL}{path}', headers = headers) # forward + excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] + headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers] + response = Response(resp.content, resp.status_code, headers) + return response + elif request.method=='POST': + # get items + headers = request.headers + json_ctx = request.get_json() + + # process + if path == "rest/v1/niming_posts": # niming post + # hash + hash = sha256( (json_ctx["content"] + str(time.time())).encode() ).hexdigest() + + # ig posting + igid = None + + # edit payload + json_ctx["hash"] = hash + json_ctx["igid"] = igid + + # forward + resp = requests.post(f'{URL}{path}',json=json_ctx, headers = headers) # forward + excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] + headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers] + response = Response(resp.content, resp.status_code, headers) + + return response # run if __name__ == "__main__": diff --git a/blueprints/admin.py b/blueprints/admin.py deleted file mode 100644 index e69de29..0000000 diff --git a/blueprints/article.py b/blueprints/article.py deleted file mode 100644 index c3f91b2..0000000 --- a/blueprints/article.py +++ /dev/null @@ -1,73 +0,0 @@ -# /api/article -# 負責公開範圍內的資料上傳與存取 - -from flask import Blueprint, current_app, request -from supabase import Client -from hashlib import sha256 -import time, math - -article = Blueprint('article', __name__) - -# listing - public -# /api/article/list?page=&count= -@article.route("/list", methods = ["GET"]) -def list(): - page = int(request.args.get("page")) - count = int(request.args.get("count")) - - client: Client = current_app.shared_resource.client - response = client.table("niming_posts").select("id, content, igid, created_at").order("id", desc = True).range(start=page*count, end=(page+1)*count-1).execute() - - return response.data - -# fetching article - public -# /api/article/get?id= -@article.route("/get", methods = ["GET"]) -def get(): - id = str(int(request.args.get("id"))) - - client: Client = current_app.shared_resource.client - response = client.table("niming_posts").select("id, content, igid, created_at").eq("id", id).execute() - - return response.data - -# new post - public -@article.route("/post", methods = ["POST"]) -def post(): - # variables - ctx = request.form.get("ctx") - if ctx is None: return "No content" - else: ctx = str(ctx) - # future - images - # future - videos - - # IG API Part - - # hash - t = str(math.floor(time.time()*10000000)) - hash = sha256( ( ctx + t ).encode() ).hexdigest() - - # Supabase - client: Client = current_app.shared_resource.client - response = (client.table("niming_posts").insert({ - "content": ctx, - "hash": hash - }).execute()) - - # log - - return response.data - -# 發文者專用 - 發文者有義務記住並保護好自己發的文章的sha256 -# fetching article - article owner -@article.route("/owner/get", methods = ["GET"]) -def owner_list(): - hash = str(request.args.get("hash")) - - client: Client = current_app.shared_resource.client - response = client.table("niming_posts").select("id, content, igid, created_at, hash").eq("hash", hash).execute() - - return response.data - -# delete article - article owner -# Coming Soon... \ No newline at end of file diff --git a/blueprints/log.py b/blueprints/log.py deleted file mode 100644 index e69de29..0000000