forked from twisted-infra/twisted-benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_https.py
183 lines (144 loc) · 5.01 KB
/
web_https.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import datetime
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, asymmetric
from cryptography.hazmat.primitives.serialization import (Encoding,
PrivateFormat,
NoEncryption)
from cryptography import x509
from twisted.web import client
from twisted.web.server import Site
from twisted.web.static import Data
from twisted.web.resource import Resource
from twisted.internet.ssl import (Certificate, KeyPair, PrivateCertificate,
trustRootFromCertificates)
from twisted.internet.interfaces import IHostnameResolver, IHostResolution
from twisted.internet.address import IPv4Address
from zope.interface import implementer
from benchlib import Client, driver
"""
This benchmark starts a Twisted Web server secured with TLS and makes
as many requests as possible in a fixed period of time. A certificate
chain is generated as part of the test and consists of:
Server -> Intermediate -> Root
To simulate actual validation overhead.
The following borrowed heavily from test_authentication in the txkube
project.
https://github.com/LeastAuthority/txkube
"""
rootKey, intermediateKey, serverKey = tuple(
asymmetric.rsa.generate_private_key(public_exponent=65537,
key_size=2048,
backend=default_backend())
for i in range(3)
)
def createCert(issuer, subject, privateKey, canSign, signingKey):
issuer = x509.Name([
x509.NameAttribute(x509.NameOID.COMMON_NAME, issuer)])
subject = x509.Name([
x509.NameAttribute(x509.NameOID.COMMON_NAME, subject)])
builder = x509.CertificateBuilder().subject_name(
subject
).issuer_name(
issuer
).public_key(
privateKey.public_key()
).serial_number(
x509.random_serial_number()
).not_valid_before(
datetime.datetime.utcnow()
).not_valid_after(
datetime.datetime.utcnow() + datetime.timedelta(days=1)
).add_extension(
x509.SubjectAlternativeName([x509.DNSName(u"localhost")]),
critical=False
)
if canSign:
builder = builder.add_extension(
x509.BasicConstraints(True, None),
critical=True
)
return builder.sign(signingKey, hashes.SHA256(), default_backend())
rootCert = createCert(u"root", u"root", rootKey, True, rootKey)
intermediateCert = createCert(
u"root",
u"intermediate",
intermediateKey,
True,
rootKey
)
serverCert = createCert(
u"intermediate",
u"server",
serverKey,
False,
intermediateKey
)
serverPrivate = serverKey.private_bytes(
Encoding.DER,
PrivateFormat.TraditionalOpenSSL,
NoEncryption()
)
trustRoot = trustRootFromCertificates(
[Certificate.loadPEM(rootCert.public_bytes(Encoding.PEM)),
Certificate.loadPEM(intermediateCert.public_bytes(Encoding.PEM))]
)
privCert = PrivateCertificate.fromCertificateAndKeyPair(
Certificate.loadPEM(serverCert.public_bytes(Encoding.PEM)),
KeyPair.load(serverPrivate)
)
root = Resource()
root.putChild(b'', Data(b"Hello, world", "text/plain"))
@implementer(IHostResolution)
class Localhost(object):
name = u'localhost'
@implementer(IHostnameResolver)
class LocalhostNameResolver(object):
"""
A resolver for bypassing actual hostname lookup.
"""
def resolveHostName(self, resolutionReceiver,
hostName,
portNumber=0,
addressTypes=None,
transportSemantics='TCP'):
resolutionReceiver.resolutionBegan(Localhost())
resolutionReceiver.addressResolved(
IPv4Address('TCP', '127.0.0.1', portNumber)
)
resolutionReceiver.resolutionComplete()
class TLSClient(Client):
def __init__(self, reactor, port):
self._host = b'https://localhost:%d/' % port.getHost().port
cf = client.BrowserLikePolicyForHTTPS(trustRoot=trustRoot)
self._agent = client.Agent(reactor, contextFactory=cf)
super(TLSClient, self).__init__(reactor)
def _request(self):
d = self._agent.request(b'GET', self._host)
d.addCallbacks(self._read, self._stop)
def _read(self, response):
d = client.readBody(response)
d.addCallback(self._continue)
d.addErrback(self._stop)
def main(reactor, duration):
concurrency = 10
resolver = LocalhostNameResolver()
reactor.installNameResolver(resolver)
port = reactor.listenSSL(
0,
Site(root),
privCert.options(),
backlog=128,
interface='127.0.0.1'
)
client = TLSClient(reactor, port)
d = client.run(concurrency, duration)
def cleanup(passthrough):
d = port.stopListening()
d.addCallback(lambda ignored: passthrough)
return d
d.addBoth(cleanup)
return d
if __name__ == '__main__':
import sys
import web_https
driver(web_https.main, sys.argv)