长亭百川云 - 文章详情

腾讯云函数代理 - sevck

博客园 - sevck

47

2024-07-19

网上好多代码都是抄袭的,也有一些BUG,例如在3.6 做proxy 请求shell会404

解决如下:

  • 原因获取headers中有host,直接带入requests headers,导致host不一致需要删除
  • 支持GET请求转发
  • 解决content-length
# -\*- coding: utf8 -\*-
import requests
import json

def geturl(urlstr):
    jurlstr \= json.dumps(urlstr)
    dict\_url \= json.loads(jurlstr)
    return dict\_url\['u'\]

def send\_post(event):
    """
    POST 代理
    """
    url \= geturl(event\['queryString'\])
    data \= event\['body'\]
    headers \= event\['headers'\]
    del headers\['host'\]
    del headers\['content-length'\]
    headers\['user-agent'\] = "Mozilla/5.0 (Linux; U; Android 4.1.2; zh-cn; GT-I9300 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/5.2.380"
    resp \= requests.post(url=url, data=data, headers=headers, verify=False)
    response\={
        "isBase64Encoded": False,
        "statusCode": 200,
        "headers": {'Content-Type': 'text/html;charset='+resp.apparent\_encoding},
        "body": resp.text,
    }
    return response

def send\_get(event):
    """
    GET 请求
    """
    url \= geturl(event\['queryString'\])
    headers \= event\['headers'\]
    del headers\['host'\]
    # del headers\['content-length'\]
    headers\['user-agent'\] = "Mozilla/5.0 (Linux; U; Android 4.1.2; zh-cn; GT-I9300 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 MicroMessenger/5.2.380"
    resp \= requests.get(url=url, headers=headers, verify=False)
    response\={
        "isBase64Encoded": False,
        "statusCode": 200,
        "headers": {'Content-Type': 'text/html;charset='+resp.apparent\_encoding},
        "body": resp.text,
    }
    return response

def main\_handler(event, context):
    try:
        if event\['httpMethod'\] == "POST":
            return send\_post(event)
        if event\['httpMethod'\] == "GET":
            return send\_get(event)
    except Exception as e:
        response \= {
        "isBase64Encoded": False,
        "statusCode": 200,
        "headers": {'Content-Type': 'text/html;charset='},
        "body": str(e)
        }
        return response
相关推荐
关注或联系我们
添加百川云公众号,移动管理云安全产品
咨询热线:
4000-327-707
百川公众号
百川公众号
百川云客服
百川云客服

Copyright ©2024 北京长亭科技有限公司
icon
京ICP备 2024055124号-2