mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 18:18:25 +00:00
Adjust pep8 parameters, reformat
This commit is contained in:
parent
5e4850d3b3
commit
f76bfabc5d
@ -96,7 +96,8 @@ def dummy_cert(privkey, cacert, commonname, sans):
|
||||
cert.set_serial_number(int(time.time() * 10000))
|
||||
if ss:
|
||||
cert.set_version(2)
|
||||
cert.add_extensions([OpenSSL.crypto.X509Extension("subjectAltName", False, ss)])
|
||||
cert.add_extensions(
|
||||
[OpenSSL.crypto.X509Extension("subjectAltName", False, ss)])
|
||||
cert.set_pubkey(cacert.get_pubkey())
|
||||
cert.sign(privkey, "sha256")
|
||||
return SSLCert(cert)
|
||||
@ -156,7 +157,12 @@ class CertStore(object):
|
||||
Implements an in-memory certificate store.
|
||||
"""
|
||||
|
||||
def __init__(self, default_privatekey, default_ca, default_chain_file, dhparams):
|
||||
def __init__(
|
||||
self,
|
||||
default_privatekey,
|
||||
default_ca,
|
||||
default_chain_file,
|
||||
dhparams):
|
||||
self.default_privatekey = default_privatekey
|
||||
self.default_ca = default_ca
|
||||
self.default_chain_file = default_chain_file
|
||||
@ -176,8 +182,10 @@ class CertStore(object):
|
||||
if bio != OpenSSL.SSL._ffi.NULL:
|
||||
bio = OpenSSL.SSL._ffi.gc(bio, OpenSSL.SSL._lib.BIO_free)
|
||||
dh = OpenSSL.SSL._lib.PEM_read_bio_DHparams(
|
||||
bio, OpenSSL.SSL._ffi.NULL, OpenSSL.SSL._ffi.NULL, OpenSSL.SSL._ffi.NULL
|
||||
)
|
||||
bio,
|
||||
OpenSSL.SSL._ffi.NULL,
|
||||
OpenSSL.SSL._ffi.NULL,
|
||||
OpenSSL.SSL._ffi.NULL)
|
||||
dh = OpenSSL.SSL._ffi.gc(dh, OpenSSL.SSL._lib.DH_free)
|
||||
return dh
|
||||
|
||||
@ -189,8 +197,12 @@ class CertStore(object):
|
||||
else:
|
||||
with open(ca_path, "rb") as f:
|
||||
raw = f.read()
|
||||
ca = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, raw)
|
||||
key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, raw)
|
||||
ca = OpenSSL.crypto.load_certificate(
|
||||
OpenSSL.crypto.FILETYPE_PEM,
|
||||
raw)
|
||||
key = OpenSSL.crypto.load_privatekey(
|
||||
OpenSSL.crypto.FILETYPE_PEM,
|
||||
raw)
|
||||
dh_path = os.path.join(path, basename + "-dhparam.pem")
|
||||
dh = cls.load_dhparam(dh_path)
|
||||
return cls(key, ca, ca_path, dh)
|
||||
@ -206,16 +218,28 @@ class CertStore(object):
|
||||
key, ca = create_ca(o=o, cn=cn, exp=expiry)
|
||||
# Dump the CA plus private key
|
||||
with open(os.path.join(path, basename + "-ca.pem"), "wb") as f:
|
||||
f.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key))
|
||||
f.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, ca))
|
||||
f.write(
|
||||
OpenSSL.crypto.dump_privatekey(
|
||||
OpenSSL.crypto.FILETYPE_PEM,
|
||||
key))
|
||||
f.write(
|
||||
OpenSSL.crypto.dump_certificate(
|
||||
OpenSSL.crypto.FILETYPE_PEM,
|
||||
ca))
|
||||
|
||||
# Dump the certificate in PEM format
|
||||
with open(os.path.join(path, basename + "-ca-cert.pem"), "wb") as f:
|
||||
f.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, ca))
|
||||
f.write(
|
||||
OpenSSL.crypto.dump_certificate(
|
||||
OpenSSL.crypto.FILETYPE_PEM,
|
||||
ca))
|
||||
|
||||
# Create a .cer file with the same contents for Android
|
||||
with open(os.path.join(path, basename + "-ca-cert.cer"), "wb") as f:
|
||||
f.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, ca))
|
||||
f.write(
|
||||
OpenSSL.crypto.dump_certificate(
|
||||
OpenSSL.crypto.FILETYPE_PEM,
|
||||
ca))
|
||||
|
||||
# Dump the certificate in PKCS12 format for Windows devices
|
||||
with open(os.path.join(path, basename + "-ca-cert.p12"), "wb") as f:
|
||||
@ -232,9 +256,14 @@ class CertStore(object):
|
||||
def add_cert_file(self, spec, path):
|
||||
with open(path, "rb") as f:
|
||||
raw = f.read()
|
||||
cert = SSLCert(OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, raw))
|
||||
cert = SSLCert(
|
||||
OpenSSL.crypto.load_certificate(
|
||||
OpenSSL.crypto.FILETYPE_PEM,
|
||||
raw))
|
||||
try:
|
||||
privatekey = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, raw)
|
||||
privatekey = OpenSSL.crypto.load_privatekey(
|
||||
OpenSSL.crypto.FILETYPE_PEM,
|
||||
raw)
|
||||
except Exception:
|
||||
privatekey = self.default_privatekey
|
||||
self.add_cert(
|
||||
@ -284,15 +313,22 @@ class CertStore(object):
|
||||
potential_keys.extend(self.asterisk_forms(s))
|
||||
potential_keys.append((commonname, tuple(sans)))
|
||||
|
||||
name = next(itertools.ifilter(lambda key: key in self.certs, potential_keys), None)
|
||||
name = next(
|
||||
itertools.ifilter(
|
||||
lambda key: key in self.certs,
|
||||
potential_keys),
|
||||
None)
|
||||
if name:
|
||||
entry = self.certs[name]
|
||||
else:
|
||||
entry = CertStoreEntry(
|
||||
cert=dummy_cert(self.default_privatekey, self.default_ca, commonname, sans),
|
||||
cert=dummy_cert(
|
||||
self.default_privatekey,
|
||||
self.default_ca,
|
||||
commonname,
|
||||
sans),
|
||||
privatekey=self.default_privatekey,
|
||||
chain_file=self.default_chain_file
|
||||
)
|
||||
chain_file=self.default_chain_file)
|
||||
self.certs[(commonname, tuple(sans))] = entry
|
||||
|
||||
return entry.cert, entry.privatekey, entry.chain_file
|
||||
@ -317,7 +353,8 @@ class _GeneralName(univ.Choice):
|
||||
|
||||
class _GeneralNames(univ.SequenceOf):
|
||||
componentType = _GeneralName()
|
||||
sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(1, 1024)
|
||||
sizeSpec = univ.SequenceOf.sizeSpec + \
|
||||
constraint.ValueSizeConstraint(1, 1024)
|
||||
|
||||
|
||||
class SSLCert(object):
|
||||
@ -345,7 +382,9 @@ class SSLCert(object):
|
||||
return klass.from_pem(pem)
|
||||
|
||||
def to_pem(self):
|
||||
return OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, self.x509)
|
||||
return OpenSSL.crypto.dump_certificate(
|
||||
OpenSSL.crypto.FILETYPE_PEM,
|
||||
self.x509)
|
||||
|
||||
def digest(self, name):
|
||||
return self.x509.digest(name)
|
||||
|
@ -94,7 +94,13 @@ class DataFrame(Frame):
|
||||
TYPE = 0x0
|
||||
VALID_FLAGS = [Frame.FLAG_END_STREAM, Frame.FLAG_PADDED]
|
||||
|
||||
def __init__(self, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, payload=b'', pad_length=0):
|
||||
def __init__(
|
||||
self,
|
||||
length=0,
|
||||
flags=Frame.FLAG_NO_FLAGS,
|
||||
stream_id=0x0,
|
||||
payload=b'',
|
||||
pad_length=0):
|
||||
super(DataFrame, self).__init__(length, flags, stream_id)
|
||||
self.payload = payload
|
||||
self.pad_length = pad_length
|
||||
@ -132,9 +138,22 @@ class DataFrame(Frame):
|
||||
|
||||
class HeadersFrame(Frame):
|
||||
TYPE = 0x1
|
||||
VALID_FLAGS = [Frame.FLAG_END_STREAM, Frame.FLAG_END_HEADERS, Frame.FLAG_PADDED, Frame.FLAG_PRIORITY]
|
||||
VALID_FLAGS = [
|
||||
Frame.FLAG_END_STREAM,
|
||||
Frame.FLAG_END_HEADERS,
|
||||
Frame.FLAG_PADDED,
|
||||
Frame.FLAG_PRIORITY]
|
||||
|
||||
def __init__(self, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, headers=None, pad_length=0, exclusive=False, stream_dependency=0x0, weight=0):
|
||||
def __init__(
|
||||
self,
|
||||
length=0,
|
||||
flags=Frame.FLAG_NO_FLAGS,
|
||||
stream_id=0x0,
|
||||
headers=None,
|
||||
pad_length=0,
|
||||
exclusive=False,
|
||||
stream_dependency=0x0,
|
||||
weight=0):
|
||||
super(HeadersFrame, self).__init__(length, flags, stream_id)
|
||||
|
||||
if headers is None:
|
||||
@ -157,7 +176,9 @@ class HeadersFrame(Frame):
|
||||
header_block_fragment = payload[0:]
|
||||
|
||||
if f.flags & self.FLAG_PRIORITY:
|
||||
f.stream_dependency, f.weight = struct.unpack('!LB', header_block_fragment[:5])
|
||||
f.stream_dependency, f.weight = struct.unpack(
|
||||
'!LB', header_block_fragment[
|
||||
:5])
|
||||
f.exclusive = bool(f.stream_dependency >> 31)
|
||||
f.stream_dependency &= 0x7FFFFFFF
|
||||
header_block_fragment = header_block_fragment[5:]
|
||||
@ -176,7 +197,9 @@ class HeadersFrame(Frame):
|
||||
b += struct.pack('!B', self.pad_length)
|
||||
|
||||
if self.flags & self.FLAG_PRIORITY:
|
||||
b += struct.pack('!LB', (int(self.exclusive) << 31) | self.stream_dependency, self.weight)
|
||||
b += struct.pack('!LB',
|
||||
(int(self.exclusive) << 31) | self.stream_dependency,
|
||||
self.weight)
|
||||
|
||||
b += Encoder().encode(self.headers)
|
||||
|
||||
@ -209,7 +232,14 @@ class PriorityFrame(Frame):
|
||||
TYPE = 0x2
|
||||
VALID_FLAGS = []
|
||||
|
||||
def __init__(self, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, exclusive=False, stream_dependency=0x0, weight=0):
|
||||
def __init__(
|
||||
self,
|
||||
length=0,
|
||||
flags=Frame.FLAG_NO_FLAGS,
|
||||
stream_id=0x0,
|
||||
exclusive=False,
|
||||
stream_dependency=0x0,
|
||||
weight=0):
|
||||
super(PriorityFrame, self).__init__(length, flags, stream_id)
|
||||
self.exclusive = exclusive
|
||||
self.stream_dependency = stream_dependency
|
||||
@ -227,12 +257,17 @@ class PriorityFrame(Frame):
|
||||
|
||||
def payload_bytes(self):
|
||||
if self.stream_id == 0x0:
|
||||
raise ValueError('PRIORITY frames MUST be associated with a stream.')
|
||||
raise ValueError(
|
||||
'PRIORITY frames MUST be associated with a stream.')
|
||||
|
||||
if self.stream_dependency == 0x0:
|
||||
raise ValueError('stream dependency is invalid.')
|
||||
|
||||
return struct.pack('!LB', (int(self.exclusive) << 31) | self.stream_dependency, self.weight)
|
||||
return struct.pack(
|
||||
'!LB',
|
||||
(int(
|
||||
self.exclusive) << 31) | self.stream_dependency,
|
||||
self.weight)
|
||||
|
||||
def payload_human_readable(self):
|
||||
s = []
|
||||
@ -246,7 +281,12 @@ class RstStreamFrame(Frame):
|
||||
TYPE = 0x3
|
||||
VALID_FLAGS = []
|
||||
|
||||
def __init__(self, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, error_code=0x0):
|
||||
def __init__(
|
||||
self,
|
||||
length=0,
|
||||
flags=Frame.FLAG_NO_FLAGS,
|
||||
stream_id=0x0,
|
||||
error_code=0x0):
|
||||
super(RstStreamFrame, self).__init__(length, flags, stream_id)
|
||||
self.error_code = error_code
|
||||
|
||||
@ -258,7 +298,8 @@ class RstStreamFrame(Frame):
|
||||
|
||||
def payload_bytes(self):
|
||||
if self.stream_id == 0x0:
|
||||
raise ValueError('RST_STREAM frames MUST be associated with a stream.')
|
||||
raise ValueError(
|
||||
'RST_STREAM frames MUST be associated with a stream.')
|
||||
|
||||
return struct.pack('!L', self.error_code)
|
||||
|
||||
@ -279,7 +320,12 @@ class SettingsFrame(Frame):
|
||||
SETTINGS_MAX_HEADER_LIST_SIZE=0x6,
|
||||
)
|
||||
|
||||
def __init__(self, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, settings=None):
|
||||
def __init__(
|
||||
self,
|
||||
length=0,
|
||||
flags=Frame.FLAG_NO_FLAGS,
|
||||
stream_id=0x0,
|
||||
settings=None):
|
||||
super(SettingsFrame, self).__init__(length, flags, stream_id)
|
||||
|
||||
if settings is None:
|
||||
@ -299,7 +345,8 @@ class SettingsFrame(Frame):
|
||||
|
||||
def payload_bytes(self):
|
||||
if self.stream_id != 0x0:
|
||||
raise ValueError('SETTINGS frames MUST NOT be associated with a stream.')
|
||||
raise ValueError(
|
||||
'SETTINGS frames MUST NOT be associated with a stream.')
|
||||
|
||||
b = b''
|
||||
for identifier, value in self.settings.items():
|
||||
@ -323,7 +370,14 @@ class PushPromiseFrame(Frame):
|
||||
TYPE = 0x5
|
||||
VALID_FLAGS = [Frame.FLAG_END_HEADERS, Frame.FLAG_PADDED]
|
||||
|
||||
def __init__(self, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, promised_stream=0x0, header_block_fragment=b'', pad_length=0):
|
||||
def __init__(
|
||||
self,
|
||||
length=0,
|
||||
flags=Frame.FLAG_NO_FLAGS,
|
||||
stream_id=0x0,
|
||||
promised_stream=0x0,
|
||||
header_block_fragment=b'',
|
||||
pad_length=0):
|
||||
super(PushPromiseFrame, self).__init__(length, flags, stream_id)
|
||||
self.pad_length = pad_length
|
||||
self.promised_stream = promised_stream
|
||||
@ -346,7 +400,8 @@ class PushPromiseFrame(Frame):
|
||||
|
||||
def payload_bytes(self):
|
||||
if self.stream_id == 0x0:
|
||||
raise ValueError('PUSH_PROMISE frames MUST be associated with a stream.')
|
||||
raise ValueError(
|
||||
'PUSH_PROMISE frames MUST be associated with a stream.')
|
||||
|
||||
if self.promised_stream == 0x0:
|
||||
raise ValueError('Promised stream id not valid.')
|
||||
@ -378,7 +433,12 @@ class PingFrame(Frame):
|
||||
TYPE = 0x6
|
||||
VALID_FLAGS = [Frame.FLAG_ACK]
|
||||
|
||||
def __init__(self, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, payload=b''):
|
||||
def __init__(
|
||||
self,
|
||||
length=0,
|
||||
flags=Frame.FLAG_NO_FLAGS,
|
||||
stream_id=0x0,
|
||||
payload=b''):
|
||||
super(PingFrame, self).__init__(length, flags, stream_id)
|
||||
self.payload = payload
|
||||
|
||||
@ -390,7 +450,8 @@ class PingFrame(Frame):
|
||||
|
||||
def payload_bytes(self):
|
||||
if self.stream_id != 0x0:
|
||||
raise ValueError('PING frames MUST NOT be associated with a stream.')
|
||||
raise ValueError(
|
||||
'PING frames MUST NOT be associated with a stream.')
|
||||
|
||||
b = self.payload[0:8]
|
||||
b += b'\0' * (8 - len(b))
|
||||
@ -404,7 +465,14 @@ class GoAwayFrame(Frame):
|
||||
TYPE = 0x7
|
||||
VALID_FLAGS = []
|
||||
|
||||
def __init__(self, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, last_stream=0x0, error_code=0x0, data=b''):
|
||||
def __init__(
|
||||
self,
|
||||
length=0,
|
||||
flags=Frame.FLAG_NO_FLAGS,
|
||||
stream_id=0x0,
|
||||
last_stream=0x0,
|
||||
error_code=0x0,
|
||||
data=b''):
|
||||
super(GoAwayFrame, self).__init__(length, flags, stream_id)
|
||||
self.last_stream = last_stream
|
||||
self.error_code = error_code
|
||||
@ -422,7 +490,8 @@ class GoAwayFrame(Frame):
|
||||
|
||||
def payload_bytes(self):
|
||||
if self.stream_id != 0x0:
|
||||
raise ValueError('GOAWAY frames MUST NOT be associated with a stream.')
|
||||
raise ValueError(
|
||||
'GOAWAY frames MUST NOT be associated with a stream.')
|
||||
|
||||
b = struct.pack('!LL', self.last_stream & 0x7FFFFFFF, self.error_code)
|
||||
b += bytes(self.data)
|
||||
@ -440,7 +509,12 @@ class WindowUpdateFrame(Frame):
|
||||
TYPE = 0x8
|
||||
VALID_FLAGS = []
|
||||
|
||||
def __init__(self, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, window_size_increment=0x0):
|
||||
def __init__(
|
||||
self,
|
||||
length=0,
|
||||
flags=Frame.FLAG_NO_FLAGS,
|
||||
stream_id=0x0,
|
||||
window_size_increment=0x0):
|
||||
super(WindowUpdateFrame, self).__init__(length, flags, stream_id)
|
||||
self.window_size_increment = window_size_increment
|
||||
|
||||
@ -455,7 +529,8 @@ class WindowUpdateFrame(Frame):
|
||||
|
||||
def payload_bytes(self):
|
||||
if self.window_size_increment <= 0 or self.window_size_increment >= 2 ** 31:
|
||||
raise ValueError('Window Szie Increment MUST be greater than 0 and less than 2^31.')
|
||||
raise ValueError(
|
||||
'Window Szie Increment MUST be greater than 0 and less than 2^31.')
|
||||
|
||||
return struct.pack('!L', self.window_size_increment & 0x7FFFFFFF)
|
||||
|
||||
@ -467,7 +542,12 @@ class ContinuationFrame(Frame):
|
||||
TYPE = 0x9
|
||||
VALID_FLAGS = [Frame.FLAG_END_HEADERS]
|
||||
|
||||
def __init__(self, length=0, flags=Frame.FLAG_NO_FLAGS, stream_id=0x0, header_block_fragment=b''):
|
||||
def __init__(
|
||||
self,
|
||||
length=0,
|
||||
flags=Frame.FLAG_NO_FLAGS,
|
||||
stream_id=0x0,
|
||||
header_block_fragment=b''):
|
||||
super(ContinuationFrame, self).__init__(length, flags, stream_id)
|
||||
self.header_block_fragment = header_block_fragment
|
||||
|
||||
@ -479,7 +559,8 @@ class ContinuationFrame(Frame):
|
||||
|
||||
def payload_bytes(self):
|
||||
if self.stream_id == 0x0:
|
||||
raise ValueError('CONTINUATION frames MUST be associated with a stream.')
|
||||
raise ValueError(
|
||||
'CONTINUATION frames MUST be associated with a stream.')
|
||||
|
||||
return self.header_block_fragment
|
||||
|
||||
|
@ -44,7 +44,9 @@ class H2Client(tcp.TCPClient):
|
||||
|
||||
alp = self.get_alpn_proto_negotiated()
|
||||
if alp != b'h2':
|
||||
raise NotImplementedError("H2Client can not handle unknown protocol: %s" % alp)
|
||||
raise NotImplementedError(
|
||||
"H2Client can not handle unknown protocol: %s" %
|
||||
alp)
|
||||
print "-> Successfully negotiated 'h2' application layer protocol."
|
||||
|
||||
if send_preface:
|
||||
@ -79,7 +81,9 @@ class H2Client(tcp.TCPClient):
|
||||
|
||||
self.settings[setting] = value
|
||||
print "-> Setting changed: %s to %d (was %s)" %
|
||||
(SettingsFrame.SETTINGS.get_name(setting), value, str(old_value))
|
||||
(SettingsFrame.SETTINGS.get_name(setting),
|
||||
value,
|
||||
str(old_value))
|
||||
|
||||
self.send_frame(SettingsFrame(flags=Frame.FLAG_ACK))
|
||||
print "-> New settings acknowledged."
|
||||
|
@ -124,7 +124,8 @@ def read_chunked(fp, limit, is_request):
|
||||
May raise HttpError.
|
||||
"""
|
||||
# FIXME: Should check if chunked is the final encoding in the headers
|
||||
# http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-16#section-3.3 3.3 2.
|
||||
# http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-16#section-3.3
|
||||
# 3.3 2.
|
||||
total = 0
|
||||
code = 400 if is_request else 502
|
||||
while True:
|
||||
|
@ -8,66 +8,37 @@ from __future__ import (absolute_import, print_function, division)
|
||||
# A collection of (name, shortcut, string) tuples.
|
||||
|
||||
UASTRINGS = [
|
||||
(
|
||||
"android",
|
||||
"a",
|
||||
"Mozilla/5.0 (Linux; U; Android 4.1.1; en-gb; Nexus 7 Build/JRO03D) AFL/01.04.02"
|
||||
),
|
||||
|
||||
(
|
||||
"blackberry",
|
||||
"l",
|
||||
"Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+"
|
||||
),
|
||||
|
||||
(
|
||||
"bingbot",
|
||||
"b",
|
||||
"Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"
|
||||
),
|
||||
|
||||
(
|
||||
"chrome",
|
||||
"c",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"
|
||||
),
|
||||
|
||||
(
|
||||
"firefox",
|
||||
"f",
|
||||
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:14.0) Gecko/20120405 Firefox/14.0a1"
|
||||
),
|
||||
|
||||
(
|
||||
"googlebot",
|
||||
"g",
|
||||
"Googlebot/2.1 (+http://www.googlebot.com/bot.html)"
|
||||
),
|
||||
|
||||
(
|
||||
"ie9",
|
||||
"i",
|
||||
"Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))"
|
||||
),
|
||||
|
||||
(
|
||||
"ipad",
|
||||
"p",
|
||||
"Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko ) Version/5.1 Mobile/9B176 Safari/7534.48.3"
|
||||
),
|
||||
|
||||
(
|
||||
"iphone",
|
||||
"h",
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 4_2_1 like Mac OS X) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5",
|
||||
),
|
||||
|
||||
(
|
||||
"safari",
|
||||
"s",
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10"
|
||||
)
|
||||
]
|
||||
("android",
|
||||
"a",
|
||||
"Mozilla/5.0 (Linux; U; Android 4.1.1; en-gb; Nexus 7 Build/JRO03D) AFL/01.04.02"),
|
||||
("blackberry",
|
||||
"l",
|
||||
"Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+"),
|
||||
("bingbot",
|
||||
"b",
|
||||
"Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"),
|
||||
("chrome",
|
||||
"c",
|
||||
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"),
|
||||
("firefox",
|
||||
"f",
|
||||
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:14.0) Gecko/20120405 Firefox/14.0a1"),
|
||||
("googlebot",
|
||||
"g",
|
||||
"Googlebot/2.1 (+http://www.googlebot.com/bot.html)"),
|
||||
("ie9",
|
||||
"i",
|
||||
"Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))"),
|
||||
("ipad",
|
||||
"p",
|
||||
"Mozilla/5.0 (iPad; CPU OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko ) Version/5.1 Mobile/9B176 Safari/7534.48.3"),
|
||||
("iphone",
|
||||
"h",
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 4_2_1 like Mac OS X) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5",
|
||||
),
|
||||
("safari",
|
||||
"s",
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10")]
|
||||
|
||||
|
||||
def get_by_shortcut(s):
|
||||
|
@ -48,7 +48,8 @@ class SSLKeyLogger(object):
|
||||
self.f = None
|
||||
self.lock = threading.Lock()
|
||||
|
||||
__name__ = "SSLKeyLogger" # required for functools.wraps, which pyOpenSSL uses.
|
||||
# required for functools.wraps, which pyOpenSSL uses.
|
||||
__name__ = "SSLKeyLogger"
|
||||
|
||||
def __call__(self, connection, where, ret):
|
||||
if where == SSL.SSL_CB_HANDSHAKE_DONE and ret == 1:
|
||||
@ -61,7 +62,10 @@ class SSLKeyLogger(object):
|
||||
self.f.write("\r\n")
|
||||
client_random = connection.client_random().encode("hex")
|
||||
masterkey = connection.master_key().encode("hex")
|
||||
self.f.write("CLIENT_RANDOM {} {}\r\n".format(client_random, masterkey))
|
||||
self.f.write(
|
||||
"CLIENT_RANDOM {} {}\r\n".format(
|
||||
client_random,
|
||||
masterkey))
|
||||
self.f.flush()
|
||||
|
||||
def close(self):
|
||||
@ -75,7 +79,8 @@ class SSLKeyLogger(object):
|
||||
return SSLKeyLogger(filename)
|
||||
return False
|
||||
|
||||
log_ssl_key = SSLKeyLogger.create_logfun(os.getenv("MITMPROXY_SSLKEYLOGFILE") or os.getenv("SSLKEYLOGFILE"))
|
||||
log_ssl_key = SSLKeyLogger.create_logfun(
|
||||
os.getenv("MITMPROXY_SSLKEYLOGFILE") or os.getenv("SSLKEYLOGFILE"))
|
||||
|
||||
|
||||
class _FileLike(object):
|
||||
@ -378,7 +383,8 @@ class _Connection(object):
|
||||
# Workaround for
|
||||
# https://github.com/pyca/pyopenssl/issues/190
|
||||
# https://github.com/mitmproxy/mitmproxy/issues/472
|
||||
context.set_mode(SSL._lib.SSL_MODE_AUTO_RETRY) # Options already set before are not cleared.
|
||||
# Options already set before are not cleared.
|
||||
context.set_mode(SSL._lib.SSL_MODE_AUTO_RETRY)
|
||||
|
||||
# Cipher List
|
||||
if cipher_list:
|
||||
@ -420,14 +426,17 @@ class TCPClient(_Connection):
|
||||
|
||||
def __init__(self, address, source_address=None):
|
||||
self.address = Address.wrap(address)
|
||||
self.source_address = Address.wrap(source_address) if source_address else None
|
||||
self.source_address = Address.wrap(
|
||||
source_address) if source_address else None
|
||||
self.connection, self.rfile, self.wfile = None, None, None
|
||||
self.cert = None
|
||||
self.ssl_established = False
|
||||
self.sni = None
|
||||
|
||||
def create_ssl_context(self, cert=None, alpn_protos=None, **sslctx_kwargs):
|
||||
context = self._create_ssl_context(alpn_protos=alpn_protos, **sslctx_kwargs)
|
||||
context = self._create_ssl_context(
|
||||
alpn_protos=alpn_protos,
|
||||
**sslctx_kwargs)
|
||||
# Client Certs
|
||||
if cert:
|
||||
try:
|
||||
@ -443,7 +452,9 @@ class TCPClient(_Connection):
|
||||
|
||||
options: A bit field consisting of OpenSSL.SSL.OP_* values
|
||||
"""
|
||||
context = self.create_ssl_context(alpn_protos=alpn_protos, **sslctx_kwargs)
|
||||
context = self.create_ssl_context(
|
||||
alpn_protos=alpn_protos,
|
||||
**sslctx_kwargs)
|
||||
self.connection = SSL.Connection(context, self.connection)
|
||||
if sni:
|
||||
self.sni = sni
|
||||
@ -469,7 +480,9 @@ class TCPClient(_Connection):
|
||||
self.rfile = Reader(connection.makefile('rb', self.rbufsize))
|
||||
self.wfile = Writer(connection.makefile('wb', self.wbufsize))
|
||||
except (socket.error, IOError) as err:
|
||||
raise NetLibError('Error connecting to "%s": %s' % (self.address.host, err))
|
||||
raise NetLibError(
|
||||
'Error connecting to "%s": %s' %
|
||||
(self.address.host, err))
|
||||
self.connection = connection
|
||||
|
||||
def settimeout(self, n):
|
||||
@ -535,7 +548,9 @@ class BaseHandler(_Connection):
|
||||
until then we're conservative.
|
||||
"""
|
||||
|
||||
context = self._create_ssl_context(alpn_select=alpn_select, **sslctx_kwargs)
|
||||
context = self._create_ssl_context(
|
||||
alpn_select=alpn_select,
|
||||
**sslctx_kwargs)
|
||||
|
||||
context.use_privatekey(key)
|
||||
context.use_certificate(cert.x509)
|
||||
@ -566,7 +581,11 @@ class BaseHandler(_Connection):
|
||||
For a list of parameters, see BaseHandler._create_ssl_context(...)
|
||||
"""
|
||||
|
||||
context = self.create_ssl_context(cert, key, alpn_select=alpn_select, **sslctx_kwargs)
|
||||
context = self.create_ssl_context(
|
||||
cert,
|
||||
key,
|
||||
alpn_select=alpn_select,
|
||||
**sslctx_kwargs)
|
||||
self.connection = SSL.Connection(context, self.connection)
|
||||
self.connection.set_accept_state()
|
||||
try:
|
||||
@ -611,7 +630,8 @@ class TCPServer(object):
|
||||
try:
|
||||
while not self.__shutdown_request:
|
||||
try:
|
||||
r, w, e = select.select([self.socket], [], [], poll_interval)
|
||||
r, w, e = select.select(
|
||||
[self.socket], [], [], poll_interval)
|
||||
except select.error as ex: # pragma: no cover
|
||||
if ex[0] == EINTR:
|
||||
continue
|
||||
|
@ -67,7 +67,9 @@ class TServer(tcp.TCPServer):
|
||||
file(self.ssl["cert"], "rb").read()
|
||||
)
|
||||
raw = file(self.ssl["key"], "rb").read()
|
||||
key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, raw)
|
||||
key = OpenSSL.crypto.load_privatekey(
|
||||
OpenSSL.crypto.FILETYPE_PEM,
|
||||
raw)
|
||||
if self.ssl["v3_only"]:
|
||||
method = tcp.SSLv3_METHOD
|
||||
options = OpenSSL.SSL.OP_NO_SSLv2 | OpenSSL.SSL.OP_NO_TLSv1
|
||||
|
@ -77,7 +77,8 @@ class WSGIAdaptor(object):
|
||||
}
|
||||
environ.update(extra)
|
||||
if flow.client_conn.address:
|
||||
environ["REMOTE_ADDR"], environ["REMOTE_PORT"] = flow.client_conn.address()
|
||||
environ["REMOTE_ADDR"], environ[
|
||||
"REMOTE_PORT"] = flow.client_conn.address()
|
||||
|
||||
for key, value in flow.request.headers.items():
|
||||
key = 'HTTP_' + key.upper().replace('-', '_')
|
||||
|
@ -1,7 +1,9 @@
|
||||
[flake8]
|
||||
max-line-length = 160
|
||||
max-line-length = 80
|
||||
max-complexity = 15
|
||||
|
||||
[pep8]
|
||||
max-line-length = 160
|
||||
max-line-length = 80
|
||||
max-complexity = 15
|
||||
exclude = */contrib/*
|
||||
ignore = E251,E309
|
||||
|
12
setup.py
12
setup.py
@ -34,17 +34,14 @@ setup(
|
||||
"Topic :: Software Development :: Testing",
|
||||
"Topic :: Software Development :: Testing :: Traffic Generation",
|
||||
],
|
||||
|
||||
packages=find_packages(),
|
||||
include_package_data=True,
|
||||
|
||||
install_requires=[
|
||||
"pyasn1>=0.1.7",
|
||||
"pyOpenSSL>=0.15.1",
|
||||
"cryptography>=0.9",
|
||||
"passlib>=1.6.2",
|
||||
"hpack>=1.0.1"
|
||||
],
|
||||
"hpack>=1.0.1"],
|
||||
extras_require={
|
||||
'dev': [
|
||||
"mock>=1.0.1",
|
||||
@ -53,7 +50,6 @@ setup(
|
||||
"coveralls>=0.4.1",
|
||||
"autopep8>=1.0.3",
|
||||
"autoflake>=0.6.6",
|
||||
"pathod>=%s, <%s" % (version.MINORVERSION, version.NEXT_MINORVERSION)
|
||||
]
|
||||
}
|
||||
)
|
||||
"pathod>=%s, <%s" %
|
||||
(version.MINORVERSION,
|
||||
version.NEXT_MINORVERSION)]})
|
||||
|
@ -7,7 +7,12 @@ from nose.tools import assert_equal
|
||||
|
||||
|
||||
def test_invalid_flags():
|
||||
tutils.raises(ValueError, DataFrame, ContinuationFrame.FLAG_END_HEADERS, 0x1234567, 'foobar')
|
||||
tutils.raises(
|
||||
ValueError,
|
||||
DataFrame,
|
||||
ContinuationFrame.FLAG_END_HEADERS,
|
||||
0x1234567,
|
||||
'foobar')
|
||||
|
||||
|
||||
def test_frame_equality():
|
||||
@ -24,8 +29,15 @@ def test_data_frame_to_bytes():
|
||||
f = DataFrame(6, Frame.FLAG_END_STREAM, 0x1234567, 'foobar')
|
||||
assert_equal(f.to_bytes().encode('hex'), '000006000101234567666f6f626172')
|
||||
|
||||
f = DataFrame(11, Frame.FLAG_END_STREAM | Frame.FLAG_PADDED, 0x1234567, 'foobar', pad_length=3)
|
||||
assert_equal(f.to_bytes().encode('hex'), '00000a00090123456703666f6f626172000000')
|
||||
f = DataFrame(
|
||||
11,
|
||||
Frame.FLAG_END_STREAM | Frame.FLAG_PADDED,
|
||||
0x1234567,
|
||||
'foobar',
|
||||
pad_length=3)
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'00000a00090123456703666f6f626172000000')
|
||||
|
||||
f = DataFrame(6, Frame.FLAG_NO_FLAGS, 0x0, 'foobar')
|
||||
tutils.raises(ValueError, f.to_bytes)
|
||||
@ -50,7 +62,12 @@ def test_data_frame_from_bytes():
|
||||
|
||||
|
||||
def test_data_frame_human_readable():
|
||||
f = DataFrame(11, Frame.FLAG_END_STREAM | Frame.FLAG_PADDED, 0x1234567, 'foobar', pad_length=3)
|
||||
f = DataFrame(
|
||||
11,
|
||||
Frame.FLAG_END_STREAM | Frame.FLAG_PADDED,
|
||||
0x1234567,
|
||||
'foobar',
|
||||
pad_length=3)
|
||||
assert f.human_readable()
|
||||
|
||||
|
||||
@ -68,7 +85,9 @@ def test_headers_frame_to_bytes():
|
||||
0x1234567,
|
||||
headers=[('host', 'foo.bar')],
|
||||
pad_length=3)
|
||||
assert_equal(f.to_bytes().encode('hex'), '00000b01080123456703668594e75e31d9000000')
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'00000b01080123456703668594e75e31d9000000')
|
||||
|
||||
f = HeadersFrame(
|
||||
10,
|
||||
@ -78,7 +97,9 @@ def test_headers_frame_to_bytes():
|
||||
exclusive=True,
|
||||
stream_dependency=0x7654321,
|
||||
weight=42)
|
||||
assert_equal(f.to_bytes().encode('hex'), '00000c012001234567876543212a668594e75e31d9')
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'00000c012001234567876543212a668594e75e31d9')
|
||||
|
||||
f = HeadersFrame(
|
||||
14,
|
||||
@ -89,7 +110,9 @@ def test_headers_frame_to_bytes():
|
||||
exclusive=True,
|
||||
stream_dependency=0x7654321,
|
||||
weight=42)
|
||||
assert_equal(f.to_bytes().encode('hex'), '00001001280123456703876543212a668594e75e31d9000000')
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'00001001280123456703876543212a668594e75e31d9000000')
|
||||
|
||||
f = HeadersFrame(
|
||||
14,
|
||||
@ -100,7 +123,9 @@ def test_headers_frame_to_bytes():
|
||||
exclusive=False,
|
||||
stream_dependency=0x7654321,
|
||||
weight=42)
|
||||
assert_equal(f.to_bytes().encode('hex'), '00001001280123456703076543212a668594e75e31d9000000')
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'00001001280123456703076543212a668594e75e31d9000000')
|
||||
|
||||
f = HeadersFrame(6, Frame.FLAG_NO_FLAGS, 0x0, 'foobar')
|
||||
tutils.raises(ValueError, f.to_bytes)
|
||||
@ -115,7 +140,8 @@ def test_headers_frame_from_bytes():
|
||||
assert_equal(f.stream_id, 0x1234567)
|
||||
assert_equal(f.headers, [('host', 'foo.bar')])
|
||||
|
||||
f = Frame.from_bytes('00000b01080123456703668594e75e31d9000000'.decode('hex'))
|
||||
f = Frame.from_bytes(
|
||||
'00000b01080123456703668594e75e31d9000000'.decode('hex'))
|
||||
assert isinstance(f, HeadersFrame)
|
||||
assert_equal(f.length, 11)
|
||||
assert_equal(f.TYPE, HeadersFrame.TYPE)
|
||||
@ -123,7 +149,8 @@ def test_headers_frame_from_bytes():
|
||||
assert_equal(f.stream_id, 0x1234567)
|
||||
assert_equal(f.headers, [('host', 'foo.bar')])
|
||||
|
||||
f = Frame.from_bytes('00000c012001234567876543212a668594e75e31d9'.decode('hex'))
|
||||
f = Frame.from_bytes(
|
||||
'00000c012001234567876543212a668594e75e31d9'.decode('hex'))
|
||||
assert isinstance(f, HeadersFrame)
|
||||
assert_equal(f.length, 12)
|
||||
assert_equal(f.TYPE, HeadersFrame.TYPE)
|
||||
@ -134,7 +161,8 @@ def test_headers_frame_from_bytes():
|
||||
assert_equal(f.stream_dependency, 0x7654321)
|
||||
assert_equal(f.weight, 42)
|
||||
|
||||
f = Frame.from_bytes('00001001280123456703876543212a668594e75e31d9000000'.decode('hex'))
|
||||
f = Frame.from_bytes(
|
||||
'00001001280123456703876543212a668594e75e31d9000000'.decode('hex'))
|
||||
assert isinstance(f, HeadersFrame)
|
||||
assert_equal(f.length, 16)
|
||||
assert_equal(f.TYPE, HeadersFrame.TYPE)
|
||||
@ -145,7 +173,8 @@ def test_headers_frame_from_bytes():
|
||||
assert_equal(f.stream_dependency, 0x7654321)
|
||||
assert_equal(f.weight, 42)
|
||||
|
||||
f = Frame.from_bytes('00001001280123456703076543212a668594e75e31d9000000'.decode('hex'))
|
||||
f = Frame.from_bytes(
|
||||
'00001001280123456703076543212a668594e75e31d9000000'.decode('hex'))
|
||||
assert isinstance(f, HeadersFrame)
|
||||
assert_equal(f.length, 16)
|
||||
assert_equal(f.TYPE, HeadersFrame.TYPE)
|
||||
@ -182,10 +211,22 @@ def test_headers_frame_human_readable():
|
||||
|
||||
|
||||
def test_priority_frame_to_bytes():
|
||||
f = PriorityFrame(5, Frame.FLAG_NO_FLAGS, 0x1234567, exclusive=True, stream_dependency=0x7654321, weight=42)
|
||||
f = PriorityFrame(
|
||||
5,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x1234567,
|
||||
exclusive=True,
|
||||
stream_dependency=0x7654321,
|
||||
weight=42)
|
||||
assert_equal(f.to_bytes().encode('hex'), '000005020001234567876543212a')
|
||||
|
||||
f = PriorityFrame(5, Frame.FLAG_NO_FLAGS, 0x1234567, exclusive=False, stream_dependency=0x7654321, weight=21)
|
||||
f = PriorityFrame(
|
||||
5,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x1234567,
|
||||
exclusive=False,
|
||||
stream_dependency=0x7654321,
|
||||
weight=21)
|
||||
assert_equal(f.to_bytes().encode('hex'), '0000050200012345670765432115')
|
||||
|
||||
f = PriorityFrame(5, Frame.FLAG_NO_FLAGS, 0x0, stream_dependency=0x1234567)
|
||||
@ -218,7 +259,13 @@ def test_priority_frame_from_bytes():
|
||||
|
||||
|
||||
def test_priority_frame_human_readable():
|
||||
f = PriorityFrame(5, Frame.FLAG_NO_FLAGS, 0x1234567, exclusive=False, stream_dependency=0x7654321, weight=21)
|
||||
f = PriorityFrame(
|
||||
5,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x1234567,
|
||||
exclusive=False,
|
||||
stream_dependency=0x7654321,
|
||||
weight=21)
|
||||
assert f.human_readable()
|
||||
|
||||
|
||||
@ -266,7 +313,9 @@ def test_settings_frame_to_bytes():
|
||||
settings={
|
||||
SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH: 1,
|
||||
SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS: 0x12345678})
|
||||
assert_equal(f.to_bytes().encode('hex'), '00000c040000000000000200000001000312345678')
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'00000c040000000000000200000001000312345678')
|
||||
|
||||
f = SettingsFrame(0, Frame.FLAG_NO_FLAGS, 0x1234567)
|
||||
tutils.raises(ValueError, f.to_bytes)
|
||||
@ -296,7 +345,8 @@ def test_settings_frame_from_bytes():
|
||||
assert_equal(len(f.settings), 1)
|
||||
assert_equal(f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH], 1)
|
||||
|
||||
f = Frame.from_bytes('00000c040000000000000200000001000312345678'.decode('hex'))
|
||||
f = Frame.from_bytes(
|
||||
'00000c040000000000000200000001000312345678'.decode('hex'))
|
||||
assert isinstance(f, SettingsFrame)
|
||||
assert_equal(f.length, 12)
|
||||
assert_equal(f.TYPE, SettingsFrame.TYPE)
|
||||
@ -304,7 +354,10 @@ def test_settings_frame_from_bytes():
|
||||
assert_equal(f.stream_id, 0x0)
|
||||
assert_equal(len(f.settings), 2)
|
||||
assert_equal(f.settings[SettingsFrame.SETTINGS.SETTINGS_ENABLE_PUSH], 1)
|
||||
assert_equal(f.settings[SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS], 0x12345678)
|
||||
assert_equal(
|
||||
f.settings[
|
||||
SettingsFrame.SETTINGS.SETTINGS_MAX_CONCURRENT_STREAMS],
|
||||
0x12345678)
|
||||
|
||||
|
||||
def test_settings_frame_human_readable():
|
||||
@ -322,11 +375,26 @@ def test_settings_frame_human_readable():
|
||||
|
||||
|
||||
def test_push_promise_frame_to_bytes():
|
||||
f = PushPromiseFrame(10, Frame.FLAG_NO_FLAGS, 0x1234567, 0x7654321, 'foobar')
|
||||
assert_equal(f.to_bytes().encode('hex'), '00000a05000123456707654321666f6f626172')
|
||||
f = PushPromiseFrame(
|
||||
10,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x1234567,
|
||||
0x7654321,
|
||||
'foobar')
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'00000a05000123456707654321666f6f626172')
|
||||
|
||||
f = PushPromiseFrame(14, HeadersFrame.FLAG_PADDED, 0x1234567, 0x7654321, 'foobar', pad_length=3)
|
||||
assert_equal(f.to_bytes().encode('hex'), '00000e0508012345670307654321666f6f626172000000')
|
||||
f = PushPromiseFrame(
|
||||
14,
|
||||
HeadersFrame.FLAG_PADDED,
|
||||
0x1234567,
|
||||
0x7654321,
|
||||
'foobar',
|
||||
pad_length=3)
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'00000e0508012345670307654321666f6f626172000000')
|
||||
|
||||
f = PushPromiseFrame(4, Frame.FLAG_NO_FLAGS, 0x0, 0x1234567)
|
||||
tutils.raises(ValueError, f.to_bytes)
|
||||
@ -344,7 +412,8 @@ def test_push_promise_frame_from_bytes():
|
||||
assert_equal(f.stream_id, 0x1234567)
|
||||
assert_equal(f.header_block_fragment, 'foobar')
|
||||
|
||||
f = Frame.from_bytes('00000e0508012345670307654321666f6f626172000000'.decode('hex'))
|
||||
f = Frame.from_bytes(
|
||||
'00000e0508012345670307654321666f6f626172000000'.decode('hex'))
|
||||
assert isinstance(f, PushPromiseFrame)
|
||||
assert_equal(f.length, 14)
|
||||
assert_equal(f.TYPE, PushPromiseFrame.TYPE)
|
||||
@ -354,16 +423,26 @@ def test_push_promise_frame_from_bytes():
|
||||
|
||||
|
||||
def test_push_promise_frame_human_readable():
|
||||
f = PushPromiseFrame(14, HeadersFrame.FLAG_PADDED, 0x1234567, 0x7654321, 'foobar', pad_length=3)
|
||||
f = PushPromiseFrame(
|
||||
14,
|
||||
HeadersFrame.FLAG_PADDED,
|
||||
0x1234567,
|
||||
0x7654321,
|
||||
'foobar',
|
||||
pad_length=3)
|
||||
assert f.human_readable()
|
||||
|
||||
|
||||
def test_ping_frame_to_bytes():
|
||||
f = PingFrame(8, PingFrame.FLAG_ACK, 0x0, payload=b'foobar')
|
||||
assert_equal(f.to_bytes().encode('hex'), '000008060100000000666f6f6261720000')
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'000008060100000000666f6f6261720000')
|
||||
|
||||
f = PingFrame(8, Frame.FLAG_NO_FLAGS, 0x0, payload=b'foobardeadbeef')
|
||||
assert_equal(f.to_bytes().encode('hex'), '000008060000000000666f6f6261726465')
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'000008060000000000666f6f6261726465')
|
||||
|
||||
f = PingFrame(8, Frame.FLAG_NO_FLAGS, 0x1234567)
|
||||
tutils.raises(ValueError, f.to_bytes)
|
||||
@ -393,13 +472,34 @@ def test_ping_frame_human_readable():
|
||||
|
||||
|
||||
def test_goaway_frame_to_bytes():
|
||||
f = GoAwayFrame(8, Frame.FLAG_NO_FLAGS, 0x0, last_stream=0x1234567, error_code=0x87654321, data=b'')
|
||||
assert_equal(f.to_bytes().encode('hex'), '0000080700000000000123456787654321')
|
||||
f = GoAwayFrame(
|
||||
8,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x0,
|
||||
last_stream=0x1234567,
|
||||
error_code=0x87654321,
|
||||
data=b'')
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'0000080700000000000123456787654321')
|
||||
|
||||
f = GoAwayFrame(14, Frame.FLAG_NO_FLAGS, 0x0, last_stream=0x1234567, error_code=0x87654321, data=b'foobar')
|
||||
assert_equal(f.to_bytes().encode('hex'), '00000e0700000000000123456787654321666f6f626172')
|
||||
f = GoAwayFrame(
|
||||
14,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x0,
|
||||
last_stream=0x1234567,
|
||||
error_code=0x87654321,
|
||||
data=b'foobar')
|
||||
assert_equal(
|
||||
f.to_bytes().encode('hex'),
|
||||
'00000e0700000000000123456787654321666f6f626172')
|
||||
|
||||
f = GoAwayFrame(8, Frame.FLAG_NO_FLAGS, 0x1234567, last_stream=0x1234567, error_code=0x87654321)
|
||||
f = GoAwayFrame(
|
||||
8,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x1234567,
|
||||
last_stream=0x1234567,
|
||||
error_code=0x87654321)
|
||||
tutils.raises(ValueError, f.to_bytes)
|
||||
|
||||
|
||||
@ -414,7 +514,8 @@ def test_goaway_frame_from_bytes():
|
||||
assert_equal(f.error_code, 0x87654321)
|
||||
assert_equal(f.data, b'')
|
||||
|
||||
f = Frame.from_bytes('00000e0700000000000123456787654321666f6f626172'.decode('hex'))
|
||||
f = Frame.from_bytes(
|
||||
'00000e0700000000000123456787654321666f6f626172'.decode('hex'))
|
||||
assert isinstance(f, GoAwayFrame)
|
||||
assert_equal(f.length, 14)
|
||||
assert_equal(f.TYPE, GoAwayFrame.TYPE)
|
||||
@ -426,18 +527,36 @@ def test_goaway_frame_from_bytes():
|
||||
|
||||
|
||||
def test_go_away_frame_human_readable():
|
||||
f = GoAwayFrame(14, Frame.FLAG_NO_FLAGS, 0x0, last_stream=0x1234567, error_code=0x87654321, data=b'foobar')
|
||||
f = GoAwayFrame(
|
||||
14,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x0,
|
||||
last_stream=0x1234567,
|
||||
error_code=0x87654321,
|
||||
data=b'foobar')
|
||||
assert f.human_readable()
|
||||
|
||||
|
||||
def test_window_update_frame_to_bytes():
|
||||
f = WindowUpdateFrame(4, Frame.FLAG_NO_FLAGS, 0x0, window_size_increment=0x1234567)
|
||||
f = WindowUpdateFrame(
|
||||
4,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x0,
|
||||
window_size_increment=0x1234567)
|
||||
assert_equal(f.to_bytes().encode('hex'), '00000408000000000001234567')
|
||||
|
||||
f = WindowUpdateFrame(4, Frame.FLAG_NO_FLAGS, 0x1234567, window_size_increment=0x7654321)
|
||||
f = WindowUpdateFrame(
|
||||
4,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x1234567,
|
||||
window_size_increment=0x7654321)
|
||||
assert_equal(f.to_bytes().encode('hex'), '00000408000123456707654321')
|
||||
|
||||
f = WindowUpdateFrame(4, Frame.FLAG_NO_FLAGS, 0x0, window_size_increment=0xdeadbeef)
|
||||
f = WindowUpdateFrame(
|
||||
4,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x0,
|
||||
window_size_increment=0xdeadbeef)
|
||||
tutils.raises(ValueError, f.to_bytes)
|
||||
|
||||
f = WindowUpdateFrame(4, Frame.FLAG_NO_FLAGS, 0x0, window_size_increment=0)
|
||||
@ -455,12 +574,20 @@ def test_window_update_frame_from_bytes():
|
||||
|
||||
|
||||
def test_window_update_frame_human_readable():
|
||||
f = WindowUpdateFrame(4, Frame.FLAG_NO_FLAGS, 0x1234567, window_size_increment=0x7654321)
|
||||
f = WindowUpdateFrame(
|
||||
4,
|
||||
Frame.FLAG_NO_FLAGS,
|
||||
0x1234567,
|
||||
window_size_increment=0x7654321)
|
||||
assert f.human_readable()
|
||||
|
||||
|
||||
def test_continuation_frame_to_bytes():
|
||||
f = ContinuationFrame(6, ContinuationFrame.FLAG_END_HEADERS, 0x1234567, 'foobar')
|
||||
f = ContinuationFrame(
|
||||
6,
|
||||
ContinuationFrame.FLAG_END_HEADERS,
|
||||
0x1234567,
|
||||
'foobar')
|
||||
assert_equal(f.to_bytes().encode('hex'), '000006090401234567666f6f626172')
|
||||
|
||||
f = ContinuationFrame(6, ContinuationFrame.FLAG_END_HEADERS, 0x0, 'foobar')
|
||||
@ -478,5 +605,9 @@ def test_continuation_frame_from_bytes():
|
||||
|
||||
|
||||
def test_continuation_frame_human_readable():
|
||||
f = ContinuationFrame(6, ContinuationFrame.FLAG_END_HEADERS, 0x1234567, 'foobar')
|
||||
f = ContinuationFrame(
|
||||
6,
|
||||
ContinuationFrame.FLAG_END_HEADERS,
|
||||
0x1234567,
|
||||
'foobar')
|
||||
assert f.human_readable()
|
||||
|
@ -42,7 +42,8 @@ class TestCertStore:
|
||||
ca2 = certutils.CertStore.from_store(d, "test")
|
||||
assert ca2.get_cert("foo", [])
|
||||
|
||||
assert ca.default_ca.get_serial_number() == ca2.default_ca.get_serial_number()
|
||||
assert ca.default_ca.get_serial_number(
|
||||
) == ca2.default_ca.get_serial_number()
|
||||
|
||||
def test_create_tmp(self):
|
||||
with tutils.tmpdir() as d:
|
||||
@ -78,7 +79,8 @@ class TestCertStore:
|
||||
with tutils.tmpdir() as d:
|
||||
ca1 = certutils.CertStore.from_store(os.path.join(d, "ca1"), "test")
|
||||
ca2 = certutils.CertStore.from_store(os.path.join(d, "ca2"), "test")
|
||||
assert not ca1.default_ca.get_serial_number() == ca2.default_ca.get_serial_number()
|
||||
assert not ca1.default_ca.get_serial_number(
|
||||
) == ca2.default_ca.get_serial_number()
|
||||
|
||||
dc = ca2.get_cert("foo.com", ["sans.example.com"])
|
||||
dcp = os.path.join(d, "dc")
|
||||
@ -93,8 +95,16 @@ class TestCertStore:
|
||||
def test_gen_pkey(self):
|
||||
try:
|
||||
with tutils.tmpdir() as d:
|
||||
ca1 = certutils.CertStore.from_store(os.path.join(d, "ca1"), "test")
|
||||
ca2 = certutils.CertStore.from_store(os.path.join(d, "ca2"), "test")
|
||||
ca1 = certutils.CertStore.from_store(
|
||||
os.path.join(
|
||||
d,
|
||||
"ca1"),
|
||||
"test")
|
||||
ca2 = certutils.CertStore.from_store(
|
||||
os.path.join(
|
||||
d,
|
||||
"ca2"),
|
||||
"test")
|
||||
cert = ca1.get_cert("foo.com", [])
|
||||
assert certffi.get_flags(ca2.gen_pkey(cert[0])) == 1
|
||||
finally:
|
||||
|
@ -13,7 +13,10 @@ class TestPassManNonAnon:
|
||||
class TestPassManHtpasswd:
|
||||
|
||||
def test_file_errors(self):
|
||||
tutils.raises("malformed htpasswd file", http_auth.PassManHtpasswd, tutils.test_data.path("data/server.crt"))
|
||||
tutils.raises(
|
||||
"malformed htpasswd file",
|
||||
http_auth.PassManHtpasswd,
|
||||
tutils.test_data.path("data/server.crt"))
|
||||
|
||||
def test_simple(self):
|
||||
pm = http_auth.PassManHtpasswd(tutils.test_data.path("data/htpasswd"))
|
||||
|
@ -61,7 +61,12 @@ def test_message_ipv6():
|
||||
# Test ATYP=0x04 (IPV6)
|
||||
ipv6_addr = "2001:db8:85a3:8d3:1319:8a2e:370:7344"
|
||||
|
||||
raw = tutils.treader("\x05\x01\x00\x04" + socket.inet_pton(socket.AF_INET6, ipv6_addr) + "\xDE\xAD\xBE\xEF")
|
||||
raw = tutils.treader(
|
||||
"\x05\x01\x00\x04" +
|
||||
socket.inet_pton(
|
||||
socket.AF_INET6,
|
||||
ipv6_addr) +
|
||||
"\xDE\xAD\xBE\xEF")
|
||||
out = StringIO()
|
||||
msg = socks.Message.from_file(raw)
|
||||
assert raw.read(2) == "\xBE\xEF"
|
||||
|
@ -75,7 +75,9 @@ class TestServerBind(test.ServerTestBase):
|
||||
for i in range(20):
|
||||
random_port = random.randrange(1024, 65535)
|
||||
try:
|
||||
c = tcp.TCPClient(("127.0.0.1", self.port), source_address=("127.0.0.1", random_port))
|
||||
c = tcp.TCPClient(
|
||||
("127.0.0.1", self.port), source_address=(
|
||||
"127.0.0.1", random_port))
|
||||
c.connect()
|
||||
assert c.rfile.readline() == str(("127.0.0.1", random_port))
|
||||
return
|
||||
@ -196,7 +198,8 @@ class TestSSLClientCert(test.ServerTestBase):
|
||||
def test_clientcert(self):
|
||||
c = tcp.TCPClient(("127.0.0.1", self.port))
|
||||
c.connect()
|
||||
c.convert_to_ssl(cert=tutils.test_data.path("data/clientcert/client.pem"))
|
||||
c.convert_to_ssl(
|
||||
cert=tutils.test_data.path("data/clientcert/client.pem"))
|
||||
assert c.rfile.readline().strip() == "1"
|
||||
|
||||
def test_clientcert_err(self):
|
||||
@ -305,7 +308,11 @@ class TestClientCipherListError(test.ServerTestBase):
|
||||
def test_echo(self):
|
||||
c = tcp.TCPClient(("127.0.0.1", self.port))
|
||||
c.connect()
|
||||
tutils.raises("cipher specification", c.convert_to_ssl, sni="foo.com", cipher_list="bogus")
|
||||
tutils.raises(
|
||||
"cipher specification",
|
||||
c.convert_to_ssl,
|
||||
sni="foo.com",
|
||||
cipher_list="bogus")
|
||||
|
||||
|
||||
class TestSSLDisconnect(test.ServerTestBase):
|
||||
@ -666,5 +673,7 @@ class TestSSLKeyLogger(test.ServerTestBase):
|
||||
tcp.log_ssl_key = _logfun
|
||||
|
||||
def test_create_logfun(self):
|
||||
assert isinstance(tcp.SSLKeyLogger.create_logfun("test"), tcp.SSLKeyLogger)
|
||||
assert isinstance(
|
||||
tcp.SSLKeyLogger.create_logfun("test"),
|
||||
tcp.SSLKeyLogger)
|
||||
assert not tcp.SSLKeyLogger.create_logfun(False)
|
||||
|
@ -63,7 +63,8 @@ class WebSocketsClient(tcp.TCPClient):
|
||||
resp = http.read_response(self.rfile, "get", None)
|
||||
server_nonce = websockets.check_server_handshake(resp.headers)
|
||||
|
||||
if not server_nonce == websockets.create_server_nonce(self.client_nonce):
|
||||
if not server_nonce == websockets.create_server_nonce(
|
||||
self.client_nonce):
|
||||
self.close()
|
||||
|
||||
def read_next_message(self):
|
||||
|
Loading…
Reference in New Issue
Block a user