From 27a9c4461e8e16e3c7c7d2f16f3b22c7a533a0a8 Mon Sep 17 00:00:00 2001 From: zhwesky2010 <1183042833@qq.com> Date: Sat, 14 Sep 2024 23:51:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=99=BE=E5=BA=A6=E4=BA=91?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README_zh.md | 8 +++++++ cmd/provider.go | 5 +++- function/http/baidu.py | 31 ++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ sdk/provider/baidu/provider.go | 44 ++++++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 function/http/baidu.py create mode 100644 sdk/provider/baidu/provider.go diff --git a/README_zh.md b/README_zh.md index 6927d69..a573061 100644 --- a/README_zh.md +++ b/README_zh.md @@ -2,6 +2,14 @@ SCFProxy 是一个基于多个云服务商提供的云函数及 API 网关实现 HTTP 代理、SOCKS 代理、反向代理的工具。 +它当前支持将以下云服务商的函数计算服务转换为 HTTP 和 SOCKS 代理: + +- 阿里云 +- 腾讯云 +- 华为云 +- AWS(亚马逊网络服务) +- 百度云 + # 安装 前往 [Release](https://github.com/shimmeris/SCFProxy/releases/) 页面下载对应系统压缩包即可。如仍需使用 Python diff --git a/cmd/provider.go b/cmd/provider.go index 3eb26f0..0903635 100644 --- a/cmd/provider.go +++ b/cmd/provider.go @@ -7,6 +7,7 @@ import ( "github.com/shimmeris/SCFProxy/sdk" "github.com/shimmeris/SCFProxy/sdk/provider/alibaba" "github.com/shimmeris/SCFProxy/sdk/provider/aws" + "github.com/shimmeris/SCFProxy/sdk/provider/baidu" "github.com/shimmeris/SCFProxy/sdk/provider/tencent" ) @@ -21,7 +22,7 @@ const ( var ( allProviders = []string{"alibaba", "tencent", "aws"} - httpProviders = []string{"alibaba", "tencent", "aws"} + httpProviders = []string{"alibaba", "tencent", "aws", "baidu"} socksProviders = []string{"alibaba", "tencent"} reverseProviders = []string{"tencent"} ) @@ -47,6 +48,8 @@ func listRegions(provider string) []string { return tencent.Regions() case "aws": return aws.Regions() + case "baidu": + return baidu.Regions() default: return nil } diff --git a/function/http/baidu.py b/function/http/baidu.py new file mode 100644 index 0000000..0a3e070 --- /dev/null +++ b/function/http/baidu.py @@ -0,0 +1,31 @@ +# -*- coding: utf8 -*- +import json +from base64 import b64decode, b64encode + +import urllib3 +urllib3.disable_warnings() + + +def handler(event: dict, context: dict): + data = event["body"] + kwargs = json.loads(data) + kwargs['body'] = b64decode(kwargs['body']) + + http = urllib3.PoolManager(cert_reqs="CERT_NONE") + # Prohibit automatic redirect to avoid network errors such as connection reset + r = http.request(**kwargs, retries=False, decode_content=False) + + headers = {k.lower(): v.lower() for k, v in r.headers.items()} + + response = { + "headers": headers, + "status_code": r.status, + "content": b64encode(r._body).decode('utf-8') + } + + return { + "isBase64Encoded": False, + "statusCode": 200, + "headers": {}, + "body": json.dumps(response) + } \ No newline at end of file diff --git a/go.mod b/go.mod index 2c5f302..4ffc630 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/aws/aws-sdk-go-v2/credentials v1.13.7 github.com/aws/aws-sdk-go-v2/service/apigateway v1.15.28 github.com/aws/aws-sdk-go-v2/service/lambda v1.26.2 + github.com/baidubce/bce-sdk-go v0.9.191 github.com/google/martian/v3 v3.3.2 github.com/hashicorp/yamux v0.1.1 github.com/huaweicloud/huaweicloud-sdk-go-v3 v0.1.7 diff --git a/go.sum b/go.sum index b840228..b58d819 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,8 @@ github.com/aws/aws-sdk-go-v2/service/ssooidc v1.13.11/go.mod h1:TZSH7xLO7+phDtVi github.com/aws/aws-sdk-go-v2/service/sts v1.17.7/go.mod h1:+lGbb3+1ugwKrNTWcf2RT05Xmp543B06zDFTwiTLp7I= github.com/aws/smithy-go v1.13.5 h1:hgz0X/DX0dGqTYpGALqXJoRKRj5oQ7150i5FdTePzO8= github.com/aws/smithy-go v1.13.5/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= +github.com/baidubce/bce-sdk-go v0.9.191 h1:6bSwdrU4YKZDsbcN+KiZ2U/atW3omvCOWRdK3GoZGzI= +github.com/baidubce/bce-sdk-go v0.9.191/go.mod h1:zbYJMQwE4IZuyrJiFO8tO8NbtYiKTFTbwh4eIsqjVdg= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/clbanning/mxj/v2 v2.5.5/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s= github.com/clbanning/mxj/v2 v2.5.7 h1:7q5lvUpaPF/WOkqgIDiwjBJaznaLCCBd78pi8ZyAnE0= diff --git a/sdk/provider/baidu/provider.go b/sdk/provider/baidu/provider.go new file mode 100644 index 0000000..98127bb --- /dev/null +++ b/sdk/provider/baidu/provider.go @@ -0,0 +1,44 @@ +package baidu + +import ( + "fmt" + "github.com/baidubce/bce-sdk-go/services/cfc" +) + +type Provider struct { + region string + fClient *cfc.Client +} + +func New(accessKeyId, accessKeySecret, region string) (*Provider, error) { + + // 用户指定的Endpoint + endpoint := fmt.Sprintf("https://cfc.%s.baidubce.com", region) + + // 初始化一个cfcClient + fClient, err := cfc.NewClient(accessKeyId, accessKeySecret, endpoint) + if err != nil { + return nil, err + } + provider := &Provider{ + region: region, + fClient: fClient, + } + return provider, nil +} + +func (p *Provider) Name() string { + return "baidu" +} + +func (p *Provider) Region() string { + return p.region +} + +func Regions() []string { + return []string{ + "bj", + "gz", + "sz", + } +}