抓包服务实现(5)—Django服务处理抓包数据
Django服务处理抓包数据如何设计?
- 为提高性能,先获取所有flow的请求数据
- 再通过每个flow的id属性查询flow的完整数据
- 由于flow的短暂性,支持可一键删除源数据
核心问题是如何提高性能,减少抓包数据展现的时间gap
获取请求信息
我们希望是先获取到flow的关键信息,不用全部返回,这样既能减少接口的负担,也能减少前端渲染的性能消耗
import pymongo
from native_project.response import Response
from native_project.mongdb import MongoDB
Response = Response()
MongoDB = MongoDB('all_capture')
def get_capture(request):
if request.method == 'GET':
host = request.GET.get('host')
if host:
query = {"host": host}
data = MongoDB.find(query)
else:
data = MongoDB.find({})
if data:
new_list = []
for item in data:
request_data = {
'request_id': str(item['_id']),
'status': item['status'],
'method': item['method'],
'protocol': item['protocol'],
'host': item['host'],
'path': item['path'],
'size': item['size'],
'duration': item['duration'],
'time_start': item['time_start']
}
new_list.append(request_data)
return Response.success(new_list)
else:
message = '域名未准确匹配'
return Response.fail(message)
之所以加参数host,是给后面filter使用
获取flow完整信息
通过flow的id属性返回整个详情
def search_one_capture(request):
if request.method == 'GET':
request_id = request.GET.get('request_id')
query = {'_id': ObjectId(str(request_id))}
search_data = MongoDB.find_one(query)
search_data.pop('_id')
return Response.success(search_data)
清空表数据
def delete_capture(request):
if request.method == 'GET':
MongoDB.delete_many({})
return Response.success(0)