A fork of pappy proxy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.5 KiB

9 years ago
import sys
9 years ago
import base64
import json
from twisted.protocols.basic import LineReceiver
from twisted.internet import defer
from util import PappyException
"""
comm.py
Handles creating a listening server bound to localhost that other processes can
use to interact with the proxy.
"""
9 years ago
debug = False
9 years ago
class CommServer(LineReceiver):
9 years ago
MAX_LENGTH=sys.maxint
9 years ago
def __init__(self):
self.delimiter = '\n'
self.action_handlers = {
'ping': self.action_ping,
'get_request': self.action_get_request,
'get_response': self.action_get_response,
'submit': self.action_submit_request,
}
def lineReceived(self, line):
9 years ago
line = line.strip()
9 years ago
9 years ago
if line == '':
return
9 years ago
#try:
command_data = json.loads(line)
command = command_data['action']
valid = False
if command in self.action_handlers:
valid = True
result = {'success': True}
func_defer = self.action_handlers[command](command_data)
func_defer.addCallback(self.action_result_handler, result)
func_defer.addErrback(self.action_error_handler, result)
if not valid:
raise PappyException('%s is an invalid command' % command_data['action'])
# except PappyException as e:
# return_data = {'success': False, 'message': str(e)}
# self.sendLine(json.dumps(return_data))
9 years ago
def action_result_handler(self, data, result):
result.update(data)
self.sendLine(json.dumps(result))
def action_error_handler(self, error, result):
if debug:
print error.getTraceback()
8 years ago
return_data = {'success': False, 'message': str(error.getTraceback())}
9 years ago
result.update(result)
error.trap(Exception)
self.sendLine(json.dumps(return_data))
9 years ago
return True
def action_ping(self, data):
return defer.succeed({'ping': 'pong'})
@defer.inlineCallbacks
def action_get_request(self, data):
9 years ago
from .http import Request
9 years ago
try:
reqid = data['reqid']
9 years ago
req = yield Request.load_request(reqid)
9 years ago
except KeyError:
raise PappyException("Request with given ID does not exist")
dat = json.loads(req.to_json())
defer.returnValue(dat)
@defer.inlineCallbacks
def action_get_response(self, data):
9 years ago
from .http import Request, Response
9 years ago
try:
reqid = data['reqid']
9 years ago
req = yield Request.load_request(reqid)
9 years ago
except KeyError:
raise PappyException("Request with given ID does not exist, cannot fetch associated response.")
if req.response:
8 years ago
rsp = req.response
dat = json.loads(rsp.to_json())
else:
dat = {}
9 years ago
defer.returnValue(dat)
@defer.inlineCallbacks
def action_submit_request(self, data):
9 years ago
from .http import Request
9 years ago
from .plugin import active_intercepting_macros
9 years ago
message = base64.b64decode(data['full_message'])
9 years ago
req = Request(message)
req.host = data['host'].encode('utf-8')
req.port = data['port']
req.is_ssl = data['is_ssl']
8 years ago
yield req.async_submit(mangle=True)
9 years ago
if 'tags' in data:
9 years ago
req.tags = set(data['tags'])
yield req.async_deep_save()
9 years ago
retdata = {}
retdata['request'] = json.loads(req.to_json())
if req.response:
retdata['response'] = json.loads(req.response.to_json())
9 years ago
defer.returnValue(retdata)