Spaces:
Running
Running
Update app.py
Browse files169 unmodified lines1701711721731741757 unmodified lines1831841851861871881891901911921931941951961971981 unmodified line2002012022032042052062072082092102112122 unmodified lines2152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712727 unmodified lines2802812822832842852866 unmodified lines2932942952962972981 unmodified line300301302303304305169 unmodified lines"Authorization",}@app.route("/cors-proxy", methods=["GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"], strict_slashes=False)def cors_proxy():# Preflight 対応7 unmodified lines"Access-Control-Allow-Methods": "GET, POST, PATCH, PUT, DELETE, OPTIONS",},)# cookie パラメータ取得cookie_param = request.args.get("cookie")cookies = Noneif cookie_param:cookies = {}for item in cookie_param.split(";"):if "=" in item:k, v = item.split("=", 1)cookies[k.strip()] = v.strip()url = request.args.get("url")if not url:return "url パラメータが必要です", 4001 unmodified lineif not url.startswith(("http://", "https://")):return "http または https のURLのみ使用できます", 400# 追加転送ヘッダ取得(例: send=x-games-flags,x-games-sdk-auth)extra_headers = set()send_param = request.args.get("send")if send_param:extra_headers = {h.strip() for h in send_param.split(",") if h.strip()}# 転送対象ヘッダを動的に構築(小文字比較で安全に)allowed_headers = {h.lower() for h in FORWARD_HEADERS}allowed_headers.update(h.lower() for h in extra_headers)2 unmodified linesif k.lower() in allowed_headers}# gzip 問題回避headers.pop("Accept-Encoding", None)# url パラメータと send パラメータは転送しないforward_params = request.args.to_dict(flat=True)forward_params.pop("url", None)forward_params.pop("send", None)forward_params.pop("cookie", None)resp = requests.request(method=request.method,url=url,headers=headers,data=request.get_data(),params=forward_params,cookies=cookies, # ←追加timeout=300,)response = Response(resp.content, resp.status_code)# CORS ヘッダ付与response.headers["Access-Control-Allow-Origin"] = "*"response.headers["Access-Control-Allow-Headers"] = "*"response.headers["Access-Control-Allow-Methods"] = "GET, POST, PATCH, PUT, DELETE, OPTIONS"# Content-Type は元のまま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)# baseurl23896 以外のパラメータをそのまま渡すforward_params = {k: v for k, v in request.args.items()if k != "baseurl23896"}cookie_param = request.args.get("cookie")cookies = Noneif cookie_param:cookies = {}for item in cookie_param.split(";"):if "=" in item:k, v = item.split("=", 1)cookies[k.strip()] = v.strip()# 転送先URLを組み立てtarget_url = baseurl.rstrip("/") + request.pathif forward_params:target_url += "?" + urlencode(forward_params, doseq=True)7 unmodified linesif k.lower() not in ["host", "content-length"]},data=request.get_data(),cookies=cookies, # ←追加allow_redirects=False,timeout=10,)6 unmodified lines"transfer-encoding","connection",]headers = [(k, v) for k, v in resp.headers.items()if k.lower() not in excluded_headers1 unmodified linereturn Response(resp.content, resp.status_code, headers)#------------------169 unmodified lines1701711721731741751761771781791801811821831841851861871881897 unmodified lines1971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282291 unmodified line2312322332342352362372382392402412422 unmodified lines2452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133147 unmodified lines3223233243253263273286 unmodified lines3353363373383393403411 unmodified line343344345346347348349169 unmodified lines"Authorization",}def parse_cookie_txt(text):cookies = {}for line in text.splitlines():line = line.strip()if not line or line.startswith("#"):continueparts = line.split("\t")if len(parts) >= 7:name = parts[5]value = parts[6]cookies[name] = valuereturn cookies@app.route("/cors-proxy", methods=["GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"], strict_slashes=False)def cors_proxy():# Preflight 対応7 unmodified lines"Access-Control-Allow-Methods": "GET, POST, PATCH, PUT, DELETE, OPTIONS",},)# cookie パラメータ取得cookie_param = request.args.get("cookie")cookie_txt_url = request.args.get("cookie_txt_url")cookies = {}# cookie= からif cookie_param:for item in cookie_param.split(";"):if "=" in item:k, v = item.split("=", 1)cookies[k.strip()] = v.strip()# cookie_txt_url からif cookie_txt_url:try:txt_resp = requests.get(cookie_txt_url, timeout=10)txt_resp.raise_for_status()txt_cookies = parse_cookie_txt(txt_resp.text)cookies.update(txt_cookies)except requests.RequestException:return "cookie_txt_url の取得に失敗しました", 400if not cookies:cookies = Noneurl = request.args.get("url")if not url:return "url パラメータが必要です", 4001 unmodified lineif not url.startswith(("http://", "https://")):return "http または https のURLのみ使用できます", 400# 追加転送ヘッダ取得extra_headers = set()send_param = request.args.get("send")if send_param:extra_headers = {h.strip() for h in send_param.split(",") if h.strip()}allowed_headers = {h.lower() for h in FORWARD_HEADERS}allowed_headers.update(h.lower() for h in extra_headers)2 unmodified linesif k.lower() in allowed_headers}headers.pop("Accept-Encoding", None)forward_params = request.args.to_dict(flat=True)forward_params.pop("url", None)forward_params.pop("send", None)forward_params.pop("cookie", None)forward_params.pop("cookie_txt_url", None)resp = requests.request(method=request.method,url=url,headers=headers,data=request.get_data(),params=forward_params,cookies=cookies,timeout=300,)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"}cookie_param = request.args.get("cookie")cookie_txt_url = request.args.get("cookie_txt_url")cookies = {}if cookie_param:for item in cookie_param.split(";"):if "=" in item:k, v = item.split("=", 1)cookies[k.strip()] = v.strip()if cookie_txt_url:try:txt_resp = requests.get(cookie_txt_url, timeout=10)txt_resp.raise_for_status()txt_cookies = parse_cookie_txt(txt_resp.text)cookies.update(txt_cookies)except requests.RequestException:return abort(400)if not cookies:cookies = Nonetarget_url = baseurl.rstrip("/") + request.pathif forward_params:target_url += "?" + urlencode(forward_params, doseq=True)7 unmodified linesif k.lower() not in ["host", "content-length"]},data=request.get_data(),cookies=cookies,allow_redirects=False,timeout=10,)6 unmodified lines"transfer-encoding","connection",]headers = [(k, v) for k, v in resp.headers.items()if k.lower() not in excluded_headers1 unmodified linereturn Response(resp.content, resp.status_code, headers)#------------------
app.py+60-16