| from flask import Flask, request, jsonify, redirect, Response, abort |
| import os |
| import requests |
| import subprocess |
| import sys |
|
|
| app = Flask(__name__) |
| @app.route("/") |
| def index(): |
| return "Hello world" |
|
|
| processes = { |
| "watch": { |
| "file": "watch.py", |
| "proc": None, |
| }, |
| "chatgpt": { |
| "file": "chatgpt.py", |
| "proc": None, |
| }, |
| } |
|
|
| def start_processes(): |
| for name, info in processes.items(): |
| proc = info["proc"] |
|
|
| if proc is None or proc.poll() is not None: |
| info["proc"] = subprocess.Popen( |
| [sys.executable, info["file"]], |
| stdout=sys.stdout, |
| stderr=sys.stderr, |
| env=os.environ, |
| ) |
| print(f"{info['file']} を起動しました") |
|
|
| |
|
|
| @app.route("/drive.com/files") |
| def drive(): |
| ip = request.remote_addr |
| print(f"アクセスIP: {ip}") |
| return redirect("https://drive.google.com/") |
|
|
| @app.route("/channel-io-managers") |
| def get_managers(): |
| limit = request.args.get("limit") |
| since = request.args.get("since") |
|
|
| params = {} |
| if limit: |
| params["limit"] = limit |
| if since: |
| params["since"] = since |
|
|
| channel_id = request.args.get("channelid", "200605") |
| url = f"https://desk-api.channel.io/desk/channels/{channel_id}/managers" |
|
|
| headers = { |
| "accept": "application/json", |
| "x-account": os.getenv("channeliotokenmain"), |
| } |
|
|
| res = requests.get(url, headers=headers, params=params) |
|
|
| if res.status_code != 200: |
| return jsonify({"error": res.text}), res.status_code |
|
|
| return jsonify(res.json().get("managers", [])) |
|
|
|
|
|
|
| FORWARD_HEADERS = { |
| "User-Agent", |
| "Accept", |
| "Content-Type", |
| "Authorization", |
| } |
|
|
| @app.route("/cors-proxy", methods=["GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"]) |
| def cors_proxy(): |
| |
| if request.method == "OPTIONS": |
| return Response( |
| "", |
| 204, |
| headers={ |
| "Access-Control-Allow-Origin": "*", |
| "Access-Control-Allow-Headers": "*", |
| "Access-Control-Allow-Methods": "GET, POST, PATCH, PUT, DELETE, OPTIONS", |
| }, |
| ) |
|
|
| url = request.args.get("url") |
| if not url: |
| return "url パラメータが必要です", 400 |
|
|
| if not url.startswith(("http://", "https://")): |
| return "http または https のURLのみ使用できます", 400 |
|
|
| |
| headers = { |
| k: v for k, v in request.headers.items() |
| if k in FORWARD_HEADERS |
| } |
|
|
| |
| headers.pop("Accept-Encoding", None) |
|
|
| resp = requests.request( |
| method=request.method, |
| url=url, |
| headers=headers, |
| data=request.get_data(), |
| params=request.args.to_dict(flat=True), |
| timeout=30, |
| ) |
|
|
| response = Response(resp.content, resp.status_code) |
|
|
| |
| response.headers["Access-Control-Allow-Origin"] = "*" |
| response.headers["Access-Control-Allow-Headers"] = "*" |
| response.headers["Access-Control-Allow-Methods"] = "GET, POST, PATCH, PUT, DELETE, OPTIONS" |
|
|
| |
| if "Content-Type" in resp.headers: |
| response.headers["Content-Type"] = resp.headers["Content-Type"] |
|
|
| return response |
|
|
| @app.errorhandler(404) |
| def cors_proxy_404(e): |
| baseurl = request.args.get("baseurl23896") |
| if not baseurl: |
| return abort(404) |
|
|
| |
| forward_params = { |
| k: v for k, v in request.args.items() |
| if k != "baseurl23896" |
| } |
|
|
| |
| target_url = baseurl.rstrip("/") + request.path |
| if forward_params: |
| target_url += "?" + urlencode(forward_params, doseq=True) |
|
|
| try: |
| resp = requests.request( |
| method=request.method, |
| url=target_url, |
| headers={ |
| k: v for k, v in request.headers |
| if k.lower() not in ["host", "content-length"] |
| }, |
| data=request.get_data(), |
| cookies=request.cookies, |
| allow_redirects=False, |
| timeout=10, |
| ) |
| except requests.RequestException: |
| return abort(502) |
|
|
| excluded_headers = [ |
| "content-encoding", |
| "content-length", |
| "transfer-encoding", |
| "connection", |
| ] |
| headers = [ |
| (k, v) for k, v in resp.headers.items() |
| if k.lower() not in excluded_headers |
| ] |
|
|
| return Response(resp.content, resp.status_code, headers) |
|
|
| if __name__ == "__main__": |
| if os.environ.get("WERKZEUG_RUN_MAIN") != "true": |
| start_processes() |
| app.run(host="0.0.0.0", port=7860) |