成长值: 28745
签到天数: 3796 天 [LV.Master]伴坛终老
|
发表于 2022/3/19 06:15
|
显示全部楼层
|阅读模式
| Google Chrome 99.0.4844.51 | Windows 10
Cloudflare cf使用worker搭建一个简单的免费页面反代系统,不符合HTTP协议标准规范但是能凑合用,腾讯云函数
比如说,无法响应206等状态码,仅支持200输出
放入worker即可使用,无需更改。api接口:worker链接/api,user接口:worker链接/user
- addEventListener('fetch', event => {
- const request = event.request;
- const url = new URL(request.url);
- console.log("test",request.url);
- var api_to_url = "https://api-drive.mypikpak.com";
- var user_to_url = "https://user.mypikpak.com";
- console.log("标头",url.protocol);
- //console.log("路径",url.pathname);
- console.log("host",url.host);
- //req_url = to_url + url.pathname
- //console.log("处理后链接",req_url);
- if ( request.url.search("/api/") != -1 )
- {
- temp_path = url.protocol + "//" + url.host + "/api"
- console.log("替换链接",temp_path);
- req_url = request.url.replace(temp_path,api_to_url);
- console.log("处理后链接",req_url);
- let request_headers = request.headers;
- let new_request_headers = new Headers(request_headers);
- new_request_headers.set('Host', "api-drive.mypikpak.com");
- const response = fetch(req_url, {
- method: request.method,
- headers: new_request_headers,
- body: request.body,
- });
- event.respondWith(response);
- }
- else if ( request.url.search("/user/") != -1 )
- {
- temp_path = url.protocol + "//" + url.host + "/user"
- console.log("替换链接",temp_path);
- req_url = request.url.replace(temp_path,user_to_url);
- console.log("处理后链接",req_url);
- let request_headers = request.headers;
- let new_request_headers = new Headers(request_headers);
- new_request_headers.set('Host', "user.mypikpak.com");
- const response = fetch(req_url, {
- method: request.method,
- headers: new_request_headers,
- body: request.body,
- });
- event.respondWith(response);
- }
- });
复制代码
腾讯云函数也可以实现相同的效果:https://curl.qcloud.com/W4ANcAUL
新建flask模板,app.py替换为以下代码,无需更改。
部署成功地址:
api:云函数地址/api
user:云函数地址/user
- import os
- from flask import Flask, jsonify, render_template, request, url_for, send_from_directory
- from werkzeug.utils import secure_filename
- from flask import Response
- import flask,requests
- from flask import Flask,redirect
- IS_SERVERLESS = bool(os.environ.get('SERVERLESS'))
- print(IS_SERVERLESS)
- app = Flask(__name__)
- api_site = "https://api-drive.mypikpak.com/"
- @app.route("/")
- def index():
- if request.method == 'GET':
- url = f'https://www.itzmx.com'
- headers = dict(flask.request.headers)
- print(headers)
- headers['Host'] = "itzmx.com"
- resp = requests.get(url=url, headers=headers)
- print(resp.text)
- 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
- @app.route('/user/<path:path>',methods=['GET',"POST",'DELETE','PATCH'])
- def proxy_user(path):
- headers = dict(flask.request.headers)
- print(headers)
- headers['Host'] = "user.mypikpak.com"
- par = flask.request.query_string.decode()
- print(path)
- if par != "":
- url = f'{api_site}{path}?{par}'
- else:
- url = f'{api_site}{path}'
- if request.method == 'GET':
- resp = requests.get(url=url, headers=headers)
- print(resp.text)
- 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':
- print(url)
- data = flask.request.data
- print(data)
- resp = requests.post(url=url, headers=headers, data=data)
- print(resp.text)
- 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 == 'DELETE':
- data = flask.request.data
- resp = requests.delete(url=url, headers=headers, data=data)
- print(resp.text)
- 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 == 'PATCH':
- data = flask.request.data
- resp = requests.patch(url=url, headers=headers, data=data)
- print(resp.text)
- 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
- @app.route('/api/<path:path>',methods=['GET',"POST",'DELETE','PATCH'])
- def proxy_api(path):
- print(request.method)
- headers = dict(flask.request.headers)
- print(headers)
- headers['Host'] = "api-drive.mypikpak.com"
- par = flask.request.query_string.decode()
- print(path)
- if par != "":
- url = f'{api_site}{path}?{par}'
- else:
- url = f'{api_site}{path}'
- print(url)
- if request.method == 'GET':
- resp = requests.get(url=url, headers=headers)
- print(resp.text)
- 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':
- data = flask.request.data
- resp = requests.post(url=url, headers=headers, data=data)
- print(resp.text)
- 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 == 'DELETE':
- data = flask.request.data
- resp = requests.delete(url=url, headers=headers, data=data)
- print(resp.text)
- 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 == 'PATCH':
- data = flask.request.data
- resp = requests.patch(url=url, headers=headers, data=data)
- print(resp.text)
- 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
- # 启动服务,监听 9000 端口,监听地址为 0.0.0.0
- app.run(debug=IS_SERVERLESS != True, port=9000, host='0.0.0.0')
复制代码
自己有服务器,想用kangle搭建一个网络更快的,只需操作文章中说的“国内服务器"
https://bbs.itzmx.com/thread-100060-1-1.html
用kangle自建时建议hosts文件绑定国内备案域名用国内反代服务器,需要使用https,例如反代地址 pikpak.oss-cn-hangzhou.aliyuncs.com 证书文件要在电脑上受信一下
https://bbs.itzmx.com/thread-90800-1-1.html
一个生成的证书例子:https://bbs.itzmx.com/thread-90896-1-1.html
|
|