一、场景
流量录制,需要对播放的流量进程定制化处理,那么可以使用中间件来实现
二、官网
https://pypi.org/project/gor/
三、编写中间件代码
# coding: utf-8 import sys from gor.middleware import AsyncioGordef on_request(proxy, msg, **kwargs):proxy.on('response', on_response, idx=msg.id, req=msg)def on_response(proxy, msg, **kwargs):proxy.on('replay', on_replay, idx=kwargs['req'].id, req=kwargs['req'], resp=msg)def on_replay(proxy, msg, **kwargs):replay_status = proxy.http_status(msg.http)resp_status = proxy.http_status(kwargs['resp'].http)if replay_status != resp_status:sys.stderr.write('replay status [%s] diffs from response status [%s]\n' % (replay_status, resp_status))else:sys.stderr.write('replay status is same as response status\n')sys.stderr.flush()if __name__ == '__main__':proxy = AsyncioGor()proxy.on('request', on_request)proxy.run()
有on_request, on_response, on_replay三个关键方法
四、使用
sudo ./gor --input-raw :8000 --middleware "python3 /path/to/middleware.py" --output-http-track-response --input-raw-track-response --output-http="http://192.168.3.123:8001"
可能会报错
需要使用sudo安装gor库
$ sudo pip3 install gor Collecting gorDownloading gor-0.2.3-py3-none-any.whl (6.9 kB) Installing collected packages: gor Successfully installed gor-0.2.3
五、我们查看效果
参考链接:
https://pypi.org/project/gor/