2015-04-23 05:11:20 +00:00
|
|
|
import copy
|
2014-10-24 01:01:34 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2014-10-24 04:19:55 +00:00
|
|
|
import sys
|
2015-04-23 05:11:20 +00:00
|
|
|
import threading
|
|
|
|
import urllib
|
2015-06-05 00:04:40 +00:00
|
|
|
import time
|
2015-06-04 07:09:38 +00:00
|
|
|
|
2015-06-11 14:13:22 +00:00
|
|
|
from netlib import tcp, http, http2, wsgi, certutils, websockets, odict
|
2014-10-24 01:01:34 +00:00
|
|
|
|
2015-05-30 05:43:01 +00:00
|
|
|
from . import version, app, language, utils, log
|
2015-05-02 04:17:00 +00:00
|
|
|
import language.http
|
2015-05-02 20:02:13 +00:00
|
|
|
import language.actions
|
2015-06-04 11:57:23 +00:00
|
|
|
import language.exceptions
|
|
|
|
import language.websockets
|
2012-06-19 04:57:57 +00:00
|
|
|
|
2014-03-02 00:45:35 +00:00
|
|
|
|
|
|
|
DEFAULT_CERT_DOMAIN = "pathod.net"
|
|
|
|
CONFDIR = "~/.mitmproxy"
|
2014-03-05 02:03:31 +00:00
|
|
|
CERTSTORE_BASENAME = "mitmproxy"
|
2014-03-02 00:45:35 +00:00
|
|
|
CA_CERT_NAME = "mitmproxy-ca.pem"
|
2015-06-11 14:36:58 +00:00
|
|
|
DEFAULT_CRAFT_ANCHOR = "/p/"
|
2014-03-02 00:45:35 +00:00
|
|
|
|
2012-08-23 00:00:16 +00:00
|
|
|
logger = logging.getLogger('pathod')
|
|
|
|
|
2014-07-21 12:08:09 +00:00
|
|
|
|
2014-10-24 01:01:34 +00:00
|
|
|
class PathodError(Exception):
|
|
|
|
pass
|
2012-06-24 04:38:32 +00:00
|
|
|
|
2012-04-28 22:56:33 +00:00
|
|
|
|
2015-06-18 09:07:33 +00:00
|
|
|
class SSLOptions(object):
|
2015-05-31 02:12:51 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
confdir=CONFDIR,
|
|
|
|
cn=None,
|
|
|
|
sans=(),
|
|
|
|
not_after_connect=None,
|
|
|
|
request_client_cert=False,
|
|
|
|
sslversion=tcp.SSLv23_METHOD,
|
|
|
|
ciphers=None,
|
2015-06-08 15:12:51 +00:00
|
|
|
certs=None,
|
2015-06-11 14:13:22 +00:00
|
|
|
alpn_select=http2.HTTP2Protocol.ALPN_PROTO_H2,
|
2015-05-31 02:12:51 +00:00
|
|
|
):
|
2014-03-02 00:45:35 +00:00
|
|
|
self.confdir = confdir
|
|
|
|
self.cn = cn
|
2015-06-08 15:12:51 +00:00
|
|
|
self.sans = sans
|
|
|
|
self.not_after_connect = not_after_connect
|
|
|
|
self.request_client_cert = request_client_cert
|
|
|
|
self.sslversion = sslversion
|
|
|
|
self.ciphers = ciphers
|
|
|
|
self.alpn_select = alpn_select
|
2014-03-05 02:03:31 +00:00
|
|
|
self.certstore = certutils.CertStore.from_store(
|
|
|
|
os.path.expanduser(confdir),
|
|
|
|
CERTSTORE_BASENAME
|
|
|
|
)
|
|
|
|
for i in certs or []:
|
|
|
|
self.certstore.add_cert_file(*i)
|
2013-01-05 03:48:49 +00:00
|
|
|
|
2014-03-02 02:13:56 +00:00
|
|
|
def get_cert(self, name):
|
|
|
|
if self.cn:
|
|
|
|
name = self.cn
|
|
|
|
elif not name:
|
|
|
|
name = DEFAULT_CERT_DOMAIN
|
2015-04-18 22:43:16 +00:00
|
|
|
return self.certstore.get_cert(name, self.sans)
|
2014-03-02 02:13:56 +00:00
|
|
|
|
2013-01-05 03:48:49 +00:00
|
|
|
|
2012-06-19 01:23:07 +00:00
|
|
|
class PathodHandler(tcp.BaseHandler):
|
2012-06-26 02:49:40 +00:00
|
|
|
wbufsize = 0
|
2012-06-25 22:15:11 +00:00
|
|
|
sni = None
|
2014-07-21 12:08:09 +00:00
|
|
|
|
2015-06-11 14:13:22 +00:00
|
|
|
def __init__(self, connection, address, server, logfp, settings, http2_framedump=False):
|
2015-04-23 05:11:20 +00:00
|
|
|
tcp.BaseHandler.__init__(self, connection, address, server)
|
2015-06-11 14:13:22 +00:00
|
|
|
self.logfp = logfp
|
2015-04-23 05:11:20 +00:00
|
|
|
self.settings = copy.copy(settings)
|
2015-06-11 14:13:22 +00:00
|
|
|
self.protocol = None
|
|
|
|
self.use_http2 = False
|
|
|
|
self.http2_framedump = http2_framedump
|
2015-04-23 05:11:20 +00:00
|
|
|
|
2015-06-11 14:13:22 +00:00
|
|
|
def _handle_sni(self, connection):
|
2012-06-25 22:15:11 +00:00
|
|
|
self.sni = connection.get_servername()
|
|
|
|
|
2015-06-07 01:18:33 +00:00
|
|
|
def http_serve_crafted(self, crafted):
|
2015-06-11 14:13:22 +00:00
|
|
|
"""
|
|
|
|
This method is HTTP/1 and HTTP/2 capable.
|
|
|
|
"""
|
|
|
|
|
2015-04-22 20:24:26 +00:00
|
|
|
error, crafted = self.server.check_policy(
|
2015-04-23 05:11:20 +00:00
|
|
|
crafted, self.settings
|
2015-04-22 20:24:26 +00:00
|
|
|
)
|
2015-04-22 04:38:54 +00:00
|
|
|
if error:
|
2015-06-11 14:13:22 +00:00
|
|
|
err = self.make_http_error_response(error)
|
2015-04-23 05:11:20 +00:00
|
|
|
language.serve(err, self.wfile, self.settings)
|
2015-05-31 06:03:41 +00:00
|
|
|
return None, dict(
|
2014-07-21 12:08:09 +00:00
|
|
|
type="error",
|
2015-04-22 04:38:54 +00:00
|
|
|
msg = error
|
2012-10-27 04:40:22 +00:00
|
|
|
)
|
|
|
|
|
2015-06-11 14:13:22 +00:00
|
|
|
if self.server.explain and not hasattr(crafted, 'is_error_response'):
|
2015-04-23 05:11:20 +00:00
|
|
|
crafted = crafted.freeze(self.settings)
|
2015-05-31 06:03:41 +00:00
|
|
|
log.write(self.logfp, ">> Spec: %s" % crafted.spec())
|
2015-06-11 14:13:22 +00:00
|
|
|
|
2014-10-25 02:30:54 +00:00
|
|
|
response_log = language.serve(
|
|
|
|
crafted,
|
|
|
|
self.wfile,
|
2015-04-23 05:11:20 +00:00
|
|
|
self.settings
|
2014-10-25 02:30:54 +00:00
|
|
|
)
|
2012-07-23 09:39:31 +00:00
|
|
|
if response_log["disconnect"]:
|
2015-05-31 02:12:51 +00:00
|
|
|
return None, response_log
|
|
|
|
return self.handle_http_request, response_log
|
2012-07-23 09:39:31 +00:00
|
|
|
|
2015-05-31 04:54:52 +00:00
|
|
|
def handle_websocket(self):
|
|
|
|
lr = self.rfile if self.server.logreq else None
|
|
|
|
lw = self.wfile if self.server.logresp else None
|
2015-06-04 11:57:23 +00:00
|
|
|
while True:
|
|
|
|
with log.Log(self.logfp, self.server.hexdump, lr, lw) as lg:
|
2015-06-05 00:04:40 +00:00
|
|
|
started = time.time()
|
2015-05-31 04:54:52 +00:00
|
|
|
try:
|
|
|
|
frm = websockets.Frame.from_file(self.rfile)
|
2015-06-04 11:57:23 +00:00
|
|
|
except tcp.NetLibIncomplete, e:
|
|
|
|
lg("Error reading websocket frame: %s"%e)
|
2015-05-31 04:54:52 +00:00
|
|
|
break
|
2015-06-05 00:04:40 +00:00
|
|
|
ended = time.time()
|
2015-06-04 11:57:23 +00:00
|
|
|
lg(frm.human_readable())
|
2015-06-05 00:04:40 +00:00
|
|
|
retlog = dict(
|
|
|
|
type = "inbound",
|
|
|
|
protocol = "websockets",
|
|
|
|
started = started,
|
|
|
|
duration = ended - started,
|
|
|
|
frame = dict(
|
|
|
|
),
|
|
|
|
cipher=None,
|
|
|
|
)
|
|
|
|
if self.ssl_established:
|
|
|
|
retlog["cipher"] = self.get_current_cipher()
|
|
|
|
self.addlog(retlog)
|
|
|
|
ld = language.websockets.NESTED_LEADER
|
|
|
|
if frm.payload.startswith(ld):
|
|
|
|
nest = frm.payload[len(ld):]
|
|
|
|
try:
|
2015-06-07 22:58:12 +00:00
|
|
|
wf_gen = language.parse_websocket_frame(nest)
|
2015-06-05 00:04:40 +00:00
|
|
|
except language.exceptions.ParseException, v:
|
2015-06-08 03:23:56 +00:00
|
|
|
log.write(
|
|
|
|
self.logfp,
|
2015-06-05 00:04:40 +00:00
|
|
|
"Parse error in reflected frame specifcation:"
|
|
|
|
" %s" % v.msg
|
|
|
|
)
|
|
|
|
break
|
2015-06-07 22:58:12 +00:00
|
|
|
for frm in wf_gen:
|
|
|
|
with log.Log(self.logfp, self.server.hexdump, lr, lw) as lg:
|
|
|
|
frame_log = language.serve(
|
|
|
|
frm,
|
|
|
|
self.wfile,
|
|
|
|
self.settings
|
|
|
|
)
|
|
|
|
lg("crafting websocket spec: %s" % frame_log["spec"])
|
|
|
|
self.addlog(frame_log)
|
2015-05-31 04:54:52 +00:00
|
|
|
return self.handle_websocket, None
|
|
|
|
|
2015-06-04 06:37:13 +00:00
|
|
|
def handle_http_connect(self, connect, lg):
|
|
|
|
"""
|
2015-06-11 14:13:22 +00:00
|
|
|
This method is HTTP/1 only.
|
|
|
|
|
2015-06-04 06:37:13 +00:00
|
|
|
Handle a CONNECT request.
|
|
|
|
"""
|
2015-06-05 00:04:40 +00:00
|
|
|
http.read_headers(self.rfile)
|
2015-06-04 06:37:13 +00:00
|
|
|
self.wfile.write(
|
|
|
|
'HTTP/1.1 200 Connection established\r\n' +
|
|
|
|
('Proxy-agent: %s\r\n' % version.NAMEVERSION) +
|
|
|
|
'\r\n'
|
|
|
|
)
|
|
|
|
self.wfile.flush()
|
|
|
|
if not self.server.ssloptions.not_after_connect:
|
|
|
|
try:
|
|
|
|
cert, key, chain_file = self.server.ssloptions.get_cert(
|
|
|
|
connect[0]
|
|
|
|
)
|
|
|
|
self.convert_to_ssl(
|
|
|
|
cert,
|
|
|
|
key,
|
2015-06-11 14:13:22 +00:00
|
|
|
handle_sni=self._handle_sni,
|
2015-06-04 06:37:13 +00:00
|
|
|
request_client_cert=self.server.ssloptions.request_client_cert,
|
|
|
|
cipher_list=self.server.ssloptions.ciphers,
|
|
|
|
method=self.server.ssloptions.sslversion,
|
2015-06-08 15:12:51 +00:00
|
|
|
alpn_select=self.server.ssloptions.alpn_select,
|
2015-06-04 06:37:13 +00:00
|
|
|
)
|
|
|
|
except tcp.NetLibError as v:
|
|
|
|
s = str(v)
|
|
|
|
lg(s)
|
|
|
|
return None, dict(type="error", msg=s)
|
|
|
|
return self.handle_http_request, None
|
|
|
|
|
|
|
|
def handle_http_app(self, method, path, headers, content, lg):
|
|
|
|
"""
|
2015-06-11 14:13:22 +00:00
|
|
|
This method is HTTP/1 only.
|
|
|
|
|
2015-06-04 06:37:13 +00:00
|
|
|
Handle a request to the built-in app.
|
|
|
|
"""
|
|
|
|
if self.server.noweb:
|
2015-06-11 14:13:22 +00:00
|
|
|
crafted = self.make_http_error_response("Access Denied")
|
2015-06-04 06:37:13 +00:00
|
|
|
language.serve(crafted, self.wfile, self.settings)
|
|
|
|
return None, dict(
|
|
|
|
type="error",
|
|
|
|
msg="Access denied: web interface disabled"
|
|
|
|
)
|
|
|
|
lg("app: %s %s" % (method, path))
|
|
|
|
req = wsgi.Request("http", method, path, headers, content)
|
|
|
|
flow = wsgi.Flow(self.address, req)
|
|
|
|
sn = self.connection.getsockname()
|
|
|
|
a = wsgi.WSGIAdaptor(
|
|
|
|
self.server.app,
|
|
|
|
sn[0],
|
|
|
|
self.server.address.port,
|
|
|
|
version.NAMEVERSION
|
|
|
|
)
|
|
|
|
a.serve(flow, self.wfile)
|
|
|
|
return self.handle_http_request, None
|
|
|
|
|
2015-05-31 02:12:51 +00:00
|
|
|
def handle_http_request(self):
|
2012-07-21 08:50:41 +00:00
|
|
|
"""
|
2015-06-11 14:13:22 +00:00
|
|
|
This method is HTTP/1 and HTTP/2 capable.
|
|
|
|
|
2015-05-31 02:12:51 +00:00
|
|
|
Returns a (handler, log) tuple.
|
2012-09-30 23:48:26 +00:00
|
|
|
|
2015-05-31 02:12:51 +00:00
|
|
|
handler: Handler for the next request, or None to disconnect
|
2012-09-30 23:48:26 +00:00
|
|
|
log: A dictionary, or None
|
2012-07-21 08:50:41 +00:00
|
|
|
"""
|
2015-05-30 05:43:01 +00:00
|
|
|
lr = self.rfile if self.server.logreq else None
|
|
|
|
lw = self.wfile if self.server.logresp else None
|
|
|
|
with log.Log(self.logfp, self.server.hexdump, lr, lw) as lg:
|
2015-06-11 14:13:22 +00:00
|
|
|
if self.use_http2:
|
|
|
|
self.protocol.perform_server_connection_preface()
|
|
|
|
stream_id, headers, body = self.protocol.read_request()
|
|
|
|
method = headers[':method']
|
|
|
|
path = headers[':path']
|
|
|
|
headers = odict.ODict(headers)
|
2015-06-16 11:52:41 +00:00
|
|
|
httpversion = ""
|
2015-05-30 05:43:01 +00:00
|
|
|
else:
|
2015-06-11 14:13:22 +00:00
|
|
|
req = self.read_http_request(lg)
|
|
|
|
if 'next_handle' in req:
|
|
|
|
return req['next_handle']
|
|
|
|
if 'errors' in req:
|
|
|
|
return None, req['errors']
|
2015-06-18 09:07:33 +00:00
|
|
|
if 'method' not in req or 'path' not in req:
|
2015-06-11 14:13:22 +00:00
|
|
|
return None, None
|
|
|
|
method = req['method']
|
|
|
|
path = req['path']
|
|
|
|
headers = req['headers']
|
|
|
|
body = req['body']
|
2015-06-16 11:52:41 +00:00
|
|
|
httpversion = req['httpversion']
|
2015-05-30 05:43:01 +00:00
|
|
|
|
|
|
|
clientcert = None
|
|
|
|
if self.clientcert:
|
|
|
|
clientcert = dict(
|
|
|
|
cn=self.clientcert.cn,
|
|
|
|
subject=self.clientcert.subject,
|
|
|
|
serial=self.clientcert.serial,
|
|
|
|
notbefore=self.clientcert.notbefore.isoformat(),
|
|
|
|
notafter=self.clientcert.notafter.isoformat(),
|
|
|
|
keyinfo=self.clientcert.keyinfo,
|
|
|
|
)
|
|
|
|
|
|
|
|
retlog = dict(
|
|
|
|
type="crafted",
|
2015-06-05 00:04:40 +00:00
|
|
|
protocol="http",
|
2015-05-30 05:43:01 +00:00
|
|
|
request=dict(
|
|
|
|
path=path,
|
|
|
|
method=method,
|
|
|
|
headers=headers.lst,
|
2015-06-16 11:52:41 +00:00
|
|
|
httpversion=httpversion,
|
2015-05-30 05:43:01 +00:00
|
|
|
sni=self.sni,
|
|
|
|
remote_address=self.address(),
|
|
|
|
clientcert=clientcert,
|
|
|
|
),
|
|
|
|
cipher=None,
|
2014-07-21 12:08:09 +00:00
|
|
|
)
|
2015-05-30 05:43:01 +00:00
|
|
|
if self.ssl_established:
|
|
|
|
retlog["cipher"] = self.get_current_cipher()
|
|
|
|
|
2015-06-04 08:23:05 +00:00
|
|
|
m = utils.MemBool()
|
|
|
|
websocket_key = websockets.check_client_handshake(headers)
|
|
|
|
self.settings.websocket_key = websocket_key
|
|
|
|
|
|
|
|
# If this is a websocket initiation, we respond with a proper
|
|
|
|
# server response, unless over-ridden.
|
|
|
|
if websocket_key:
|
2015-06-07 22:58:12 +00:00
|
|
|
anchor_gen = language.parse_pathod("ws")
|
2015-06-04 08:23:05 +00:00
|
|
|
else:
|
2015-06-07 22:58:12 +00:00
|
|
|
anchor_gen = None
|
2015-06-11 14:13:22 +00:00
|
|
|
|
|
|
|
for regex, spec in self.server.anchors:
|
|
|
|
if regex.match(path):
|
|
|
|
anchor_gen = language.parse_pathod(spec, self.use_http2)
|
2015-06-04 08:23:05 +00:00
|
|
|
break
|
|
|
|
else:
|
2015-06-11 14:36:58 +00:00
|
|
|
if m(path.startswith(self.server.craftanchor)):
|
|
|
|
spec = urllib.unquote(path)[len(self.server.craftanchor):]
|
2015-06-04 08:23:05 +00:00
|
|
|
if spec:
|
|
|
|
try:
|
2015-06-11 14:13:22 +00:00
|
|
|
anchor_gen = language.parse_pathod(spec, self.use_http2)
|
2015-06-04 08:23:05 +00:00
|
|
|
except language.ParseException as v:
|
|
|
|
lg("Parse error: %s" % v.msg)
|
2015-06-11 14:13:22 +00:00
|
|
|
anchor_gen = iter([self.make_http_error_response(
|
2015-06-04 08:23:05 +00:00
|
|
|
"Parse Error",
|
|
|
|
"Error parsing response spec: %s\n" % (
|
|
|
|
v.msg + v.marked()
|
|
|
|
)
|
2015-06-07 04:11:32 +00:00
|
|
|
)])
|
2015-06-11 14:13:22 +00:00
|
|
|
else:
|
|
|
|
if self.use_http2:
|
2015-06-16 09:33:10 +00:00
|
|
|
anchor_gen = iter([self.make_http_error_response(
|
|
|
|
"Spec Error",
|
|
|
|
"HTTP/2 only supports request/response with the craft anchor point: %s" %
|
|
|
|
self.server.craftanchor
|
|
|
|
)])
|
2015-06-11 14:13:22 +00:00
|
|
|
|
2015-05-30 05:43:01 +00:00
|
|
|
|
2015-06-07 22:58:12 +00:00
|
|
|
if anchor_gen:
|
2015-06-08 02:01:04 +00:00
|
|
|
spec = anchor_gen.next()
|
2015-06-11 14:13:22 +00:00
|
|
|
|
|
|
|
if self.use_http2 and isinstance(spec, language.http2.Response):
|
|
|
|
spec.stream_id = stream_id
|
|
|
|
|
2015-06-08 02:01:04 +00:00
|
|
|
lg("crafting spec: %s" % spec)
|
2015-06-07 01:18:33 +00:00
|
|
|
nexthandler, retlog["response"] = self.http_serve_crafted(
|
2015-06-08 02:01:04 +00:00
|
|
|
spec
|
2015-06-04 08:23:05 +00:00
|
|
|
)
|
2015-05-31 06:03:41 +00:00
|
|
|
if nexthandler and websocket_key:
|
|
|
|
return self.handle_websocket, retlog
|
|
|
|
else:
|
|
|
|
return nexthandler, retlog
|
2015-05-30 05:43:01 +00:00
|
|
|
else:
|
2015-06-11 14:13:22 +00:00
|
|
|
return self.handle_http_app(method, path, headers, body, lg)
|
2012-07-21 08:50:41 +00:00
|
|
|
|
2015-06-11 14:13:22 +00:00
|
|
|
def read_http_request(self, lg):
|
|
|
|
"""
|
|
|
|
This method is HTTP/1 only.
|
|
|
|
"""
|
|
|
|
line = http.get_request_line(self.rfile)
|
|
|
|
if not line:
|
|
|
|
# Normal termination
|
|
|
|
return dict()
|
|
|
|
|
|
|
|
m = utils.MemBool()
|
|
|
|
if m(http.parse_init_connect(line)):
|
|
|
|
return dict(next_handle=self.handle_http_connect(m.v, lg))
|
|
|
|
elif m(http.parse_init_proxy(line)):
|
|
|
|
method, _, _, _, path, httpversion = m.v
|
|
|
|
elif m(http.parse_init_http(line)):
|
|
|
|
method, path, httpversion = m.v
|
|
|
|
else:
|
|
|
|
s = "Invalid first line: %s" % repr(line)
|
|
|
|
lg(s)
|
|
|
|
return dict(errors=dict(type="error", msg=s))
|
|
|
|
|
|
|
|
headers = http.read_headers(self.rfile)
|
|
|
|
if headers is None:
|
|
|
|
s = "Invalid headers"
|
|
|
|
lg(s)
|
|
|
|
return dict(errors=dict(type="error", msg=s))
|
|
|
|
|
|
|
|
try:
|
|
|
|
body = http.read_http_body(
|
|
|
|
self.rfile,
|
|
|
|
headers,
|
|
|
|
None,
|
|
|
|
method,
|
|
|
|
None,
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
except http.HttpError as s:
|
|
|
|
s = str(s)
|
|
|
|
lg(s)
|
|
|
|
return dict(errors=dict(type="error", msg=s))
|
|
|
|
|
|
|
|
return dict(
|
|
|
|
method=method,
|
|
|
|
path=path,
|
|
|
|
headers=headers,
|
2015-06-16 11:52:41 +00:00
|
|
|
body=body,
|
|
|
|
httpversion=httpversion)
|
2015-06-11 14:13:22 +00:00
|
|
|
|
|
|
|
def make_http_error_response(self, reason, body=None):
|
|
|
|
"""
|
|
|
|
This method is HTTP/1 and HTTP/2 capable.
|
|
|
|
"""
|
|
|
|
if self.use_http2:
|
|
|
|
resp = language.http2.make_error_response(reason, body)
|
|
|
|
else:
|
|
|
|
resp = language.http.make_error_response(reason, body)
|
|
|
|
resp.is_error_response = True
|
|
|
|
return resp
|
2015-04-22 20:43:57 +00:00
|
|
|
|
2012-06-16 19:57:24 +00:00
|
|
|
def handle(self):
|
2015-06-11 14:13:22 +00:00
|
|
|
self.settimeout(self.server.timeout)
|
|
|
|
|
2013-01-05 03:48:49 +00:00
|
|
|
if self.server.ssl:
|
2012-06-25 23:03:35 +00:00
|
|
|
try:
|
2015-04-19 20:56:47 +00:00
|
|
|
cert, key, _ = self.server.ssloptions.get_cert(None)
|
2012-06-25 23:03:35 +00:00
|
|
|
self.convert_to_ssl(
|
2015-05-30 00:03:13 +00:00
|
|
|
cert,
|
|
|
|
key,
|
2015-06-11 14:13:22 +00:00
|
|
|
handle_sni=self._handle_sni,
|
2014-07-21 12:08:09 +00:00
|
|
|
request_client_cert=self.server.ssloptions.request_client_cert,
|
|
|
|
cipher_list=self.server.ssloptions.ciphers,
|
|
|
|
method=self.server.ssloptions.sslversion,
|
2015-06-08 15:12:51 +00:00
|
|
|
alpn_select=self.server.ssloptions.alpn_select,
|
2012-06-25 23:03:35 +00:00
|
|
|
)
|
2015-05-30 00:03:13 +00:00
|
|
|
except tcp.NetLibError as v:
|
2012-07-20 01:21:33 +00:00
|
|
|
s = str(v)
|
|
|
|
self.server.add_log(
|
|
|
|
dict(
|
2014-07-21 12:08:09 +00:00
|
|
|
type="error",
|
|
|
|
msg=s
|
2012-07-20 01:21:33 +00:00
|
|
|
)
|
|
|
|
)
|
2015-05-31 06:03:41 +00:00
|
|
|
log.write(self.logfp, s)
|
2012-07-21 08:50:41 +00:00
|
|
|
return
|
2015-06-11 14:13:22 +00:00
|
|
|
|
|
|
|
alp = self.get_alpn_proto_negotiated()
|
|
|
|
if alp == http2.HTTP2Protocol.ALPN_PROTO_H2:
|
|
|
|
self.protocol = http2.HTTP2Protocol(self, is_server=True, dump_frames=self.http2_framedump)
|
|
|
|
self.use_http2 = True
|
|
|
|
|
|
|
|
# if not self.protocol:
|
|
|
|
# # TODO: create HTTP or Websockets protocol
|
|
|
|
# self.protocol = None
|
|
|
|
|
|
|
|
self.settings.protocol = self.protocol
|
|
|
|
|
2015-05-31 02:12:51 +00:00
|
|
|
handler = self.handle_http_request
|
2015-06-11 14:13:22 +00:00
|
|
|
|
2012-06-24 23:34:29 +00:00
|
|
|
while not self.finished:
|
2015-05-31 06:03:41 +00:00
|
|
|
handler, l = handler()
|
|
|
|
if l:
|
|
|
|
self.addlog(l)
|
2015-05-31 02:12:51 +00:00
|
|
|
if not handler:
|
2012-07-21 08:50:41 +00:00
|
|
|
return
|
2012-06-19 04:57:57 +00:00
|
|
|
|
2015-06-11 14:13:22 +00:00
|
|
|
def addlog(self, log):
|
|
|
|
# FIXME: The bytes in the log should not be escaped. We do this at the
|
|
|
|
# moment because JSON encoding can't handle binary data, and I don't
|
|
|
|
# want to base64 everything.
|
|
|
|
if self.server.logreq:
|
2015-06-18 09:07:33 +00:00
|
|
|
encoded_bytes = self.rfile.get_log().encode("string_escape")
|
|
|
|
log["request_bytes"] = encoded_bytes
|
2015-06-11 14:13:22 +00:00
|
|
|
if self.server.logresp:
|
2015-06-18 09:07:33 +00:00
|
|
|
encoded_bytes = self.wfile.get_log().encode("string_escape")
|
|
|
|
log["response_bytes"] = encoded_bytes
|
2015-06-11 14:13:22 +00:00
|
|
|
self.server.add_log(log)
|
|
|
|
|
2012-04-28 22:56:33 +00:00
|
|
|
|
2012-06-19 01:23:07 +00:00
|
|
|
class Pathod(tcp.TCPServer):
|
2012-06-21 02:29:49 +00:00
|
|
|
LOGBUF = 500
|
2014-07-21 12:08:09 +00:00
|
|
|
|
|
|
|
def __init__(
|
2014-10-25 02:30:54 +00:00
|
|
|
self,
|
|
|
|
addr,
|
|
|
|
ssl=False,
|
|
|
|
ssloptions=None,
|
2015-06-11 14:36:58 +00:00
|
|
|
craftanchor=DEFAULT_CRAFT_ANCHOR,
|
2014-10-25 02:30:54 +00:00
|
|
|
staticdir=None,
|
|
|
|
anchors=(),
|
|
|
|
sizelimit=None,
|
|
|
|
noweb=False,
|
|
|
|
nocraft=False,
|
|
|
|
noapi=False,
|
|
|
|
nohang=False,
|
|
|
|
timeout=None,
|
|
|
|
logreq=False,
|
|
|
|
logresp=False,
|
|
|
|
explain=False,
|
2015-04-22 03:58:25 +00:00
|
|
|
hexdump=False,
|
2015-06-11 14:13:22 +00:00
|
|
|
http2_framedump=False,
|
2015-04-22 03:58:25 +00:00
|
|
|
webdebug=False,
|
2015-05-30 05:43:01 +00:00
|
|
|
logfp=sys.stdout,
|
2014-07-21 12:08:09 +00:00
|
|
|
):
|
2012-06-24 04:20:50 +00:00
|
|
|
"""
|
|
|
|
addr: (address, port) tuple. If port is 0, a free port will be
|
|
|
|
automatically chosen.
|
2013-05-12 21:03:48 +00:00
|
|
|
ssloptions: an SSLOptions object.
|
2015-06-11 14:36:58 +00:00
|
|
|
craftanchor: URL prefix specifying the path under which to anchor
|
2014-10-25 02:30:54 +00:00
|
|
|
response generation.
|
2012-06-24 04:20:50 +00:00
|
|
|
staticdir: path to a directory of static resources, or None.
|
2014-10-25 02:30:54 +00:00
|
|
|
anchors: List of (regex object, language.Request object) tuples, or
|
|
|
|
None.
|
2012-07-23 02:37:00 +00:00
|
|
|
sizelimit: Limit size of served data.
|
2012-07-26 08:01:51 +00:00
|
|
|
nocraft: Disable response crafting.
|
|
|
|
noapi: Disable the API.
|
|
|
|
nohang: Disable pauses.
|
2012-06-24 04:20:50 +00:00
|
|
|
"""
|
2012-06-19 01:23:07 +00:00
|
|
|
tcp.TCPServer.__init__(self, addr)
|
2013-01-05 03:48:49 +00:00
|
|
|
self.ssl = ssl
|
|
|
|
self.ssloptions = ssloptions or SSLOptions()
|
2012-06-24 03:07:45 +00:00
|
|
|
self.staticdir = staticdir
|
2012-07-24 09:51:43 +00:00
|
|
|
self.craftanchor = craftanchor
|
2012-07-23 02:37:00 +00:00
|
|
|
self.sizelimit = sizelimit
|
2014-10-25 02:30:54 +00:00
|
|
|
self.noweb, self.nocraft = noweb, nocraft
|
|
|
|
self.noapi, self.nohang = noapi, nohang
|
|
|
|
self.timeout, self.logreq = timeout, logreq
|
|
|
|
self.logresp, self.hexdump = logresp, hexdump
|
2015-06-11 14:13:22 +00:00
|
|
|
self.http2_framedump = http2_framedump
|
2012-10-30 22:23:53 +00:00
|
|
|
self.explain = explain
|
2015-05-30 05:43:01 +00:00
|
|
|
self.logfp = logfp
|
2012-09-30 23:48:26 +00:00
|
|
|
|
2015-04-22 03:58:25 +00:00
|
|
|
self.app = app.make_app(noapi, webdebug)
|
2012-06-19 04:57:57 +00:00
|
|
|
self.app.config["pathod"] = self
|
2012-06-21 02:29:49 +00:00
|
|
|
self.log = []
|
|
|
|
self.logid = 0
|
2014-10-25 02:30:54 +00:00
|
|
|
self.anchors = anchors
|
2012-06-07 04:35:54 +00:00
|
|
|
|
2015-04-22 03:49:17 +00:00
|
|
|
self.settings = language.Settings(
|
|
|
|
staticdir = self.staticdir
|
|
|
|
)
|
|
|
|
|
2012-10-27 23:56:08 +00:00
|
|
|
def check_policy(self, req, settings):
|
2012-07-23 03:38:06 +00:00
|
|
|
"""
|
|
|
|
A policy check that verifies the request size is withing limits.
|
|
|
|
"""
|
2015-06-04 07:09:38 +00:00
|
|
|
if self.nocraft:
|
|
|
|
return "Crafting disabled.", None
|
2012-10-27 23:56:08 +00:00
|
|
|
try:
|
2015-04-22 04:38:54 +00:00
|
|
|
req = req.resolve(settings)
|
2012-10-30 03:04:48 +00:00
|
|
|
l = req.maximum_length(settings)
|
2014-01-19 05:20:01 +00:00
|
|
|
except language.FileAccessDenied:
|
2015-04-22 04:38:54 +00:00
|
|
|
return "File access denied.", None
|
2012-10-27 23:56:08 +00:00
|
|
|
if self.sizelimit and l > self.sizelimit:
|
2015-04-22 04:38:54 +00:00
|
|
|
return "Response too large.", None
|
2015-05-02 20:02:13 +00:00
|
|
|
pauses = [isinstance(i, language.actions.PauseAt) for i in req.actions]
|
2015-05-02 04:17:00 +00:00
|
|
|
if self.nohang and any(pauses):
|
2015-04-22 04:38:54 +00:00
|
|
|
return "Pauses have been disabled.", None
|
|
|
|
return None, req
|
2012-07-23 03:38:06 +00:00
|
|
|
|
2014-01-09 17:04:04 +00:00
|
|
|
def handle_client_connection(self, request, client_address):
|
2015-05-30 05:43:01 +00:00
|
|
|
h = PathodHandler(
|
|
|
|
request,
|
|
|
|
client_address,
|
|
|
|
self,
|
|
|
|
self.logfp,
|
2015-06-11 14:13:22 +00:00
|
|
|
self.settings,
|
|
|
|
self.http2_framedump,
|
2015-05-30 05:43:01 +00:00
|
|
|
)
|
2012-07-29 23:58:29 +00:00
|
|
|
try:
|
|
|
|
h.handle()
|
|
|
|
h.finish()
|
2014-07-21 12:08:09 +00:00
|
|
|
except tcp.NetLibDisconnect: # pragma: no cover
|
2015-05-31 06:03:41 +00:00
|
|
|
log.write(self.logfp, "Disconnect")
|
2012-07-29 23:58:29 +00:00
|
|
|
self.add_log(
|
|
|
|
dict(
|
2014-07-21 12:08:09 +00:00
|
|
|
type="error",
|
|
|
|
msg="Disconnect"
|
2012-07-29 23:58:29 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return
|
2012-10-30 23:32:13 +00:00
|
|
|
except tcp.NetLibTimeout:
|
2015-05-31 06:03:41 +00:00
|
|
|
log.write(self.logfp, "Timeout")
|
2012-09-30 23:01:02 +00:00
|
|
|
self.add_log(
|
|
|
|
dict(
|
2014-07-21 12:08:09 +00:00
|
|
|
type="timeout",
|
2012-09-30 23:01:02 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return
|
2012-06-21 02:29:49 +00:00
|
|
|
|
|
|
|
def add_log(self, d):
|
2012-07-24 10:30:15 +00:00
|
|
|
if not self.noapi:
|
|
|
|
lock = threading.Lock()
|
|
|
|
with lock:
|
|
|
|
d["id"] = self.logid
|
|
|
|
self.log.insert(0, d)
|
|
|
|
if len(self.log) > self.LOGBUF:
|
|
|
|
self.log.pop()
|
|
|
|
self.logid += 1
|
|
|
|
return d["id"]
|
2012-06-21 02:29:49 +00:00
|
|
|
|
|
|
|
def clear_log(self):
|
|
|
|
lock = threading.Lock()
|
|
|
|
with lock:
|
|
|
|
self.log = []
|
|
|
|
|
|
|
|
def log_by_id(self, id):
|
|
|
|
for i in self.log:
|
|
|
|
if i["id"] == id:
|
|
|
|
return i
|
|
|
|
|
|
|
|
def get_log(self):
|
|
|
|
return self.log
|
2014-10-24 04:12:54 +00:00
|
|
|
|
|
|
|
|
2015-05-30 00:03:13 +00:00
|
|
|
def main(args): # pragma: nocover
|
2014-10-24 04:12:54 +00:00
|
|
|
ssloptions = SSLOptions(
|
|
|
|
cn = args.cn,
|
|
|
|
confdir = args.confdir,
|
|
|
|
not_after_connect = args.ssl_not_after_connect,
|
|
|
|
ciphers = args.ciphers,
|
|
|
|
sslversion = utils.SSLVERSIONS[args.sslversion],
|
2015-04-18 22:51:03 +00:00
|
|
|
certs = args.ssl_certs,
|
2015-06-08 15:12:51 +00:00
|
|
|
sans = args.sans,
|
2014-10-24 04:12:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
root = logging.getLogger()
|
|
|
|
if root.handlers:
|
|
|
|
for handler in root.handlers:
|
|
|
|
root.removeHandler(handler)
|
|
|
|
|
|
|
|
log = logging.getLogger('pathod')
|
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
fmt = logging.Formatter(
|
|
|
|
'%(asctime)s: %(message)s',
|
|
|
|
datefmt='%d-%m-%y %H:%M:%S',
|
|
|
|
)
|
|
|
|
if args.logfile:
|
|
|
|
fh = logging.handlers.WatchedFileHandler(args.logfile)
|
|
|
|
fh.setFormatter(fmt)
|
|
|
|
log.addHandler(fh)
|
|
|
|
if not args.daemonize:
|
|
|
|
sh = logging.StreamHandler()
|
|
|
|
sh.setFormatter(fmt)
|
|
|
|
log.addHandler(sh)
|
|
|
|
|
|
|
|
try:
|
|
|
|
pd = Pathod(
|
|
|
|
(args.address, args.port),
|
|
|
|
craftanchor = args.craftanchor,
|
|
|
|
ssl = args.ssl,
|
|
|
|
ssloptions = ssloptions,
|
|
|
|
staticdir = args.staticdir,
|
2014-10-24 04:19:55 +00:00
|
|
|
anchors = args.anchors,
|
|
|
|
sizelimit = args.sizelimit,
|
2014-10-24 04:12:54 +00:00
|
|
|
noweb = args.noweb,
|
|
|
|
nocraft = args.nocraft,
|
|
|
|
noapi = args.noapi,
|
|
|
|
nohang = args.nohang,
|
|
|
|
timeout = args.timeout,
|
|
|
|
logreq = args.logreq,
|
|
|
|
logresp = args.logresp,
|
|
|
|
hexdump = args.hexdump,
|
2015-06-11 14:13:22 +00:00
|
|
|
http2_framedump = args.http2_framedump,
|
2014-10-24 04:12:54 +00:00
|
|
|
explain = args.explain,
|
2015-04-22 03:58:25 +00:00
|
|
|
webdebug = args.webdebug
|
2014-10-24 04:12:54 +00:00
|
|
|
)
|
2015-05-30 00:03:13 +00:00
|
|
|
except PathodError as v:
|
|
|
|
print >> sys.stderr, "Error: %s" % v
|
2014-10-24 04:19:55 +00:00
|
|
|
sys.exit(1)
|
2015-05-30 00:03:13 +00:00
|
|
|
except language.FileAccessDenied as v:
|
|
|
|
print >> sys.stderr, "Error: %s" % v
|
2014-10-24 04:19:55 +00:00
|
|
|
|
|
|
|
if args.daemonize:
|
|
|
|
utils.daemonize()
|
2014-10-24 04:12:54 +00:00
|
|
|
|
|
|
|
try:
|
2015-05-30 00:03:13 +00:00
|
|
|
print "%s listening on %s:%s" % (
|
2014-10-24 04:12:54 +00:00
|
|
|
version.NAMEVERSION,
|
|
|
|
pd.address.host,
|
|
|
|
pd.address.port
|
|
|
|
)
|
|
|
|
pd.serve_forever()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|