抓包服务实现(7)—Django服务实现Map Remote
Django服务针对Map Remote如何设计?
- 保存Map Setting配置并支持修改、删除
- 能选择Map Setting确定Map Remote
核心问题是如何暂存确定Map Remote的Map Setting
保存Map Remote配置
我们希望有一个Map Remote总表,用来记录并提供选择去确定Map
from native_project.response import Response
from native_project.mongdb import MongoDB
Response = Response()
MappingMongoDB = MongoDB('mapping_settings')
def save_mapping(request):
if request.method == 'GET':
map_title = request.GET.get('map_title')
protocol_origin = request.GET.get('protocol_origin')
host_origin = request.GET.get('host_origin')
protocol_map = request.GET.get('protocol_map')
host_map = request.GET.get('host_map')
if MappingMongoDB.find_one({'host_origin': host_origin}):
return Response.fail('host is duplicate')
else:
map_setting = {
'map_title': map_title,
'protocol_origin': protocol_origin,
'host_origin': host_origin,
'protocol_map': protocol_map,
'host_map': host_map,
'isMap': 'False'
}
MappingMongoDB.insert_one(map_setting)
return Response.success(0)
- 可以设置每个Map Remote配置的title,这样更加清晰的表达此配置的作用
- 本次只设计host和protocol两个字段,后续字段可以自己叠加,而且设置host不能重复
- 默认值isMap=false,该字段表示是否正在使用中
获取Map Remote配置表
def list_mapping(request):
if request.method == 'GET':
mapping_data = MappingMongoDB.find({})
new_list = []
for mapping in mapping_data:
data = {
'mapping_id': str(mapping['_id']),
'map_title': mapping['map_title'],
'protocol_origin': mapping['protocol_origin'],
'host_origin': mapping['host_origin'],
'protocol_map': mapping['protocol_map'],
'host_map': mapping['host_map'],
'isMap': mapping['isMap']
}
new_list.append(data)
return Response.success(new_list)
暂存Map Remote配置
暂存意思是本次抓包都是根据选择的Remote配置进行Map的
from native_project.response import Response
from native_project.mongdb import MongoDB
import json
Response = Response()
MappingMongoDB=MongoDB('mapping_settings')
TemporaryMongoDB = MongoDB('temporary_mapping')
def temporary_mapping(request):
if request.method == 'GET':
mapping=request.GET.get('mapping')
map_list=json.loads(mapping)
TemporaryMongoDB.delete_many({})
MappingMongoDB.update_many({},{'isMap':'False'})
for map_temporary in map_list:
map_setting={
'map_title':map_temporary['map_title'],
'protocol_origin':map_temporary['protocol_origin'],
'host_origin':map_temporary['host_origin'],
'protocol_map':map_temporary['protocol_map'],
'host_map':map_temporary['host_map']
}
TemporaryMongoDB.insert_one(map_setting)
MappingMongoDB.update_one({'host_origin':map_temporary['host_origin']},{'isMap':'True'})
return Response.success(0)
- 先清空temporary_mapping临时表,并且把Remote Setting表所有配置都变成isMap=False
- 然后遍历所选的配置,插入到临时表,并且更新isMap=True
删除Map Remote配置
def delete_mapping(request):
if request.method == 'GET':
host_origin = request.GET.get('host_origin')
query = {'host_origin': host_origin}
MappingMongoDB.delete_one(query)
TemporaryMongoDB.delete_one(query)
return Response.success(0)
删除settings配置的时候同时删除该临时配置