Example #1
0
    def dict_from_request_body(self, flow: http.HTTPFlow, api: Api):
        d = None
        if api.content_type == 'json':
            try:
                d = json.loads(flow.request.text)
            except:
                pass

        elif api.content_type == 'urlencoded_form' or flow.request.urlencoded_form:
            # 返回的类型是 multidict.MultiDictView
            d = flow.request.urlencoded_form
            api.content_type = 'urlencoded_form'

        elif api.content_type == 'multipart_form' or flow.request.multipart_form:
            ctx.log.error('content-type = multipart_form')
            d = dict()
            for key, value in flow.request.multipart_form.items():
                key = key.decode(encoding='utf-8')
                value = value.decode(encoding='utf-8')
                d[key] = value
            api.content_type = 'multipart_form'

        elif api.content_type == 'get':
            pass

        else:
            # api.content_type没有决定出来:1,没有设置且第1次击中api;2,body没有内容也无法决定content_type;3,get请求一般没有body

            if flow.request.text:
                ctx.log.error('有请求内容')
            else:
                ctx.log.error('没有请求内容')
                if flow.request.method == 'GET':
                    api.content_type = 'get'
                    return d

            try:
                d = json.loads(flow.request.text)
                api.content_type = 'json'
            except:
                pass

        return d
Example #2
0
    def response(self, flow: http.HTTPFlow):
        # 不处理'options'方法的请求
        method = flow.request.method
        if method == 'OPTIONS':
            return

        ft = None
        for i, flt in enumerate(self.appfilters):
            if flt(flow):
                ft = flt
                if not flt == self.appfilters[0]:
                    self.appfilters.pop(i)
                    self.appfilters.insert(0, flt)
                break
        if ft:
            ctx.log.error('|' + '-' * 20 + '|')
            api: Api = ft.current_api
            request: http.HTTPRequest = flow.request

            parse_result = urlparse(request.url)
            url_path = parse_result.path
            if api.url_path and (not api.url_path == url_path):
                api = Api(url_path)
                ft.add(api)

            if len(api.api_ok):
                ft.api_ok[url_path] = api.api_ok

            ctx.log.error(f'触发 App = {ft.app_name}')
            ctx.log.error(f'触发 api = {api} {request.method}')

            function_name = re.sub(r'[./-]', '_', url_path).strip('_').lower()
            api_url = f'{request.scheme}://{request.pretty_host}{url_path}'

            # 如果App代码目录不存在,则创建目录
            session_dir = self.api_dir.joinpath(ft.app_name, self._session())
            if not session_dir.exists():
                session_dir.mkdir(parents=True, exist_ok=True)

            app_apis = self.app_apis.setdefault(ft.app_name, dict())
            if function_name not in app_apis:
                api.name = function_name
                api.url = api_url
                api.url_path = url_path
                api.method = request.method.lower()
                api.content_type = 'json' if 'json' in request.headers.get(
                    'content-type', '') else ''
                api.fun_params = api.str_fun_params()
                app_apis[function_name] = api

            with (session_dir / f'{function_name}.text').open('a') as f:
                headers_code = self.headers_string(flow)
                params_code = self.params_string(flow)
                data_code = self.data_string(flow, api)
                api.time = time.strftime('%m-%d %H:%M:%S')
                api.headers_code = headers_code
                api.params_code = params_code
                api.data_code = data_code
                api.response = flow.response.text

                code = self.api_template.render(request=api)
                f.write(code)

            device = self._session()

            self.gather_params_and_bodys(flow,
                                         api,
                                         device=device,
                                         app=ft.app_name)
            self.session_hit.add((device, ft.app_name))

            d_v = self.app_fn_url.setdefault(device, dict())
            fn_url = d_v.setdefault(ft.app_name, dict())
            fn_url[url_path] = api_url

            ctx.log.error('|' + '-' * 20 + '|')
Example #3
0
              f_p_kwarg={
                  "pkw1": 1,
                  "pkw2": '2'
              })  #时段签到
    print(api.str_fun_params())
    # api =Api(r'/mission/intPointReward',params_as_all=True, body_as_all=True,f_p_arg=['p1','p2'], f_b_arg={'b1', 'b2'},f_p_kwarg={"pkw1":1, "pkw2":'2'})#时段签到
    print(api.str_fun_params())
    api = Api(r'/mission/intPointReward', params_as_all=True,
              body_as_all=True)  #时段签到
    print(api.str_fun_params())
    api = Api(r'/mission/intPointReward', f_p_enc={'p_enc'})
    print(api.str_fun_params())

    api.name = 'mission_intPointReward'
    api.method = 'post'
    api.content_type = 'json'
    api.fun_params = api.str_fun_params()
    # api.params_as_all = True
    # api.body_as_all = True
    seq = [api]

    template_dir = os.path.dirname(__file__)
    tfile = f'{template_dir}/code_template.j2.py'
    # tfile = f'{template_dir}/api_template.j2.py'
    with open(tfile) as f:
        s = f.read()
        t = Template(s)
        ss = t.render(seq=seq)
        # ss = t.render(request=api)
        # print(ss)
    print('done')