设为首页收藏本站

ZMX - IT技术交流论坛 - 无限Perfect,追求梦想 - itzmx.com

 找回密码
 注册论坛

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

用百度帐号登录

只需两步,快速登录

搜索
查看: 222|回复: 1

Cloudflare cf使用worker搭建一个简单的免费页面反代系统,不符合HTTP协议标准规范但是能凑合用,腾讯云函数

[复制链接]
 成长值: 28745

签到天数: 3796 天

[LV.Master]伴坛终老

发表于 2022/3/19 06:15 | 显示全部楼层 |阅读模式 |Google Chrome 99.0.4844.51|Windows 10
天涯海角搜一下: 百度 谷歌 360 搜狗 有道 雅虎 必应 即刻
Cloudflare cf使用worker搭建一个简单的免费页面反代系统,不符合HTTP协议标准规范但是能凑合用,腾讯云函数

比如说,无法响应206等状态码,仅支持200输出
放入worker即可使用,无需更改。api接口:worker链接/api,user接口:worker链接/user

  1. addEventListener('fetch', event => {


  2.     const request = event.request;
  3.     const url = new URL(request.url);
  4.     console.log("test",request.url);

  5.     var api_to_url = "https://api-drive.mypikpak.com";
  6.     var user_to_url = "https://user.mypikpak.com";



  7.     console.log("标头",url.protocol);
  8.     //console.log("路径",url.pathname);
  9.     console.log("host",url.host);

  10.     //req_url = to_url + url.pathname

  11.     //console.log("处理后链接",req_url);
  12.     if ( request.url.search("/api/") != -1 )
  13.     {   
  14.         temp_path = url.protocol + "//" + url.host + "/api"
  15.         console.log("替换链接",temp_path);
  16.         req_url = request.url.replace(temp_path,api_to_url);
  17.         console.log("处理后链接",req_url);
  18.         let request_headers = request.headers;
  19.         let new_request_headers = new Headers(request_headers);
  20.         new_request_headers.set('Host', "api-drive.mypikpak.com");

  21.         const response = fetch(req_url, {
  22.                 method: request.method,
  23.                 headers: new_request_headers,
  24.                 body: request.body,
  25.             });

  26.             event.respondWith(response);
  27.     }
  28.     else if ( request.url.search("/user/") != -1 )
  29.     {   
  30.         temp_path = url.protocol + "//" + url.host + "/user"
  31.         console.log("替换链接",temp_path);
  32.         req_url = request.url.replace(temp_path,user_to_url);
  33.         console.log("处理后链接",req_url);
  34.         let request_headers = request.headers;
  35.         let new_request_headers = new Headers(request_headers);
  36.         new_request_headers.set('Host', "user.mypikpak.com");

  37.         const response = fetch(req_url, {
  38.                 method: request.method,
  39.                 headers: new_request_headers,
  40.                 body: request.body,
  41.             });

  42.             event.respondWith(response);
  43.     }



  44. });


复制代码



腾讯云函数也可以实现相同的效果:https://curl.qcloud.com/W4ANcAUL
新建flask模板,app.py替换为以下代码,无需更改。
部署成功地址:
api:云函数地址/api
user:云函数地址/user
  1. import os
  2. from flask import Flask, jsonify, render_template, request, url_for, send_from_directory
  3. from werkzeug.utils import secure_filename
  4. from flask import Response
  5. import flask,requests
  6. from flask import Flask,redirect


  7. IS_SERVERLESS = bool(os.environ.get('SERVERLESS'))
  8. print(IS_SERVERLESS)

  9. app = Flask(__name__)

  10. api_site = "https://api-drive.mypikpak.com/"

  11. @app.route("/")
  12. def index():
  13.         if request.method == 'GET':
  14.             url = f'https://www.itzmx.com'
  15.             headers = dict(flask.request.headers)
  16.             print(headers)
  17.             headers['Host'] = "itzmx.com"
  18.             resp = requests.get(url=url, headers=headers)
  19.             print(resp.text)
  20.             excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
  21.             headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
  22.             response = Response(resp.content, resp.status_code, headers)
  23.             return response

  24. @app.route('/user/<path:path>',methods=['GET',"POST",'DELETE','PATCH'])
  25. def proxy_user(path):
  26.     headers = dict(flask.request.headers)
  27.     print(headers)
  28.     headers['Host'] = "user.mypikpak.com"
  29.     par = flask.request.query_string.decode()
  30.     print(path)

  31.     if par != "":
  32.         url = f'{api_site}{path}?{par}'
  33.     else:
  34.         url = f'{api_site}{path}'


  35.     if request.method == 'GET':


  36.         resp = requests.get(url=url, headers=headers)
  37.         print(resp.text)
  38.         excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
  39.         headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
  40.         response = Response(resp.content, resp.status_code, headers)
  41.         return response
  42.     elif request.method == 'POST':

  43.         print(url)

  44.         data = flask.request.data
  45.         print(data)
  46.         resp = requests.post(url=url, headers=headers, data=data)
  47.         print(resp.text)
  48.         excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
  49.         headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
  50.         response = Response(resp.content, resp.status_code, headers)
  51.         return response

  52.     elif request.method == 'DELETE':


  53.         data = flask.request.data

  54.         resp = requests.delete(url=url, headers=headers, data=data)
  55.         print(resp.text)
  56.         excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
  57.         headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
  58.         response = Response(resp.content, resp.status_code, headers)
  59.         return response

  60.     elif request.method == 'PATCH':


  61.             data = flask.request.data

  62.             resp = requests.patch(url=url, headers=headers, data=data)
  63.             print(resp.text)
  64.             excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
  65.             headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
  66.             response = Response(resp.content, resp.status_code, headers)
  67.             return response

  68. @app.route('/api/<path:path>',methods=['GET',"POST",'DELETE','PATCH'])
  69. def proxy_api(path):
  70.     print(request.method)
  71.     headers = dict(flask.request.headers)
  72.     print(headers)
  73.     headers['Host'] = "api-drive.mypikpak.com"
  74.     par = flask.request.query_string.decode()
  75.     print(path)
  76.     if par != "":
  77.         url = f'{api_site}{path}?{par}'
  78.     else:
  79.         url = f'{api_site}{path}'
  80.     print(url)


  81.     if request.method == 'GET':


  82.         resp = requests.get(url=url, headers=headers)
  83.         print(resp.text)
  84.         excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
  85.         headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
  86.         response = Response(resp.content, resp.status_code, headers)
  87.         return response
  88.     elif request.method == 'POST':


  89.             data = flask.request.data

  90.             resp = requests.post(url=url, headers=headers, data=data)
  91.             print(resp.text)
  92.             excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
  93.             headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
  94.             response = Response(resp.content, resp.status_code, headers)
  95.             return response

  96.     elif request.method == 'DELETE':


  97.             data = flask.request.data

  98.             resp = requests.delete(url=url, headers=headers, data=data)
  99.             print(resp.text)
  100.             excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
  101.             headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
  102.             response = Response(resp.content, resp.status_code, headers)
  103.             return response

  104.     elif request.method == 'PATCH':


  105.             data = flask.request.data

  106.             resp = requests.patch(url=url, headers=headers, data=data)
  107.             print(resp.text)
  108.             excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
  109.             headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
  110.             response = Response(resp.content, resp.status_code, headers)
  111.             return response







  112. # 启动服务,监听 9000 端口,监听地址为 0.0.0.0
  113. 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

欢迎光临IT技术交流论坛:http://bbs.itzmx.com/
回复

使用道具 举报

签到天数: 220 天

[LV.7]常住居民III

发表于 2022/3/19 15:28 | 显示全部楼层 |Firefox 99.0|Windows 10
小樱啥时候出个比特彗星下载百度网盘的完整教程鸭
[发帖际遇]: oitre 在论坛发帖时没有注意,被小偷偷去了 2 樱币. 幸运榜 / 衰神榜
欢迎光临IT技术交流论坛:http://bbs.itzmx.com/
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册论坛 新浪微博账号登陆用百度帐号登录

本版积分规则

手机版|Archiver|Mail me|网站地图|IT技术交流论坛 ( 闽ICP备13013206号-7 )

GMT+8, 2022/5/26 07:32 , Processed in 0.405042 second(s), 21 queries , MemCache On.

Powered by itzmx! X3.4

© 2011- sakura

快速回复 返回顶部 返回列表