This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
184 lines (158 loc) · 5.97 KB
/
app.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
184
import binascii
import json
import os
import socket
import ssl
import sys
import time
from cryptography import x509
from flask import Flask, Response, abort, request
from flask_caching import Cache
from OpenSSL import SSL
from prometheus_client import (REGISTRY, Counter, Gauge, Histogram, Summary,
generate_latest)
app = Flask(__name__)
cache = Cache(app, config={"CACHE_TYPE": "SimpleCache"})
FLASK_REQUEST_LATENCY = Histogram(
"flask_request_latency_seconds", "Flask Request Latency", ["method", "endpoint"]
)
FLASK_REQUEST_COUNT = Counter(
"flask_request_count", "Flask Request Count", ["method", "endpoint", "http_status"]
)
FLASK_REQUEST_SIZE = Gauge(
"flask_request_size_bytes",
"Flask Response Size",
["method", "endpoint", "http_status"],
)
update_time = Summary("update_seconds", "Time spent loading data upstream")
request_bytes = Gauge("request_bytes", "proxied file size", ["endpoint"])
def dumpname(x):
r = {}
for e in x:
r[e.oid._name] = e.value
return r
def dumpcert(x):
r = {}
c = x.to_cryptography()
r["version"] = c.version.value
r["serial_number"] = c.serial_number
r["not_valid_before"] = c.not_valid_before.isoformat()
r["not_valid_after"] = c.not_valid_after.isoformat()
r["issuer"] = dumpname(c.issuer)
r["subject"] = dumpname(c.subject)
r["signature_algorithm"] = c.signature_algorithm_oid._name
r["signature"] = {
"hex": binascii.hexlify(c.signature).decode("ascii").strip(),
"base64": binascii.b2a_base64(c.signature).decode("ascii").strip(),
}
for e in c.extensions:
if isinstance(e.value, x509.KeyUsage) or isinstance(
e.value, x509.BasicConstraints
):
# _properties
re = {}
for (key, value) in e.value.__dict__.items():
re[key[1:]] = value
elif isinstance(e.value, x509.ExtendedKeyUsage):
# list of oids
re = []
for usage in e.value:
re.append(usage._name)
elif isinstance(e.value, x509.SubjectAlternativeName):
# list of oids
re = []
for name in e.value:
re.append(name.value)
elif isinstance(e.value, x509.AuthorityInformationAccess):
re = []
for desc in e.value:
res = {}
res["access_method"] = desc.access_method._name
res["access_location"] = desc.access_location.value
re.append(res)
elif isinstance(e.value, x509.CRLDistributionPoints):
re = []
for crl in e.value:
res = {}
for (key, value) in crl.__dict__.items():
if isinstance(value, list):
result = []
for element in value:
if isinstance(element, x509.UniformResourceIdentifier):
result.append(element.value)
else:
result.append(str(element))
res[key[1:]] = result
else:
res[key[1:]] = value
re.append(res)
elif (
isinstance(e.value, x509.AuthorityKeyIdentifier)
or isinstance(e.value, x509.CertificatePolicies)
or isinstance(e.value, x509.SubjectKeyIdentifier)
):
# ignore these extensions for now because they contain binary data or 'Unknown OID' policies
re = None
else:
re = str(e.value)
if re != None:
r[e.oid._name] = re
return r
@app.route("/metrics")
def metrics():
return generate_latest(REGISTRY)
@app.route("/")
def empty():
return ""
@app.route("/<host>:<int:port>")
@app.route("/<host>")
def cache_lookup(host, port=443):
cachekey = host + ":" + str(port)
data = cache.get(cachekey)
if data is None:
data = lookup(host, port)
cache.set(cachekey, data, timeout=5 * 60)
return data
@update_time.time()
def lookup(host, port=443):
try:
ctx = SSL.Context(SSL.SSLv23_METHOD) # Autonegotiating including TLS
sock = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_STREAM))
sock.connect((host, int(port)))
sock.set_tlsext_host_name(host.encode("utf8"))
sock.set_alpn_protos([b"http/1.1", b"spdy/2", b"http/2"])
sock.do_handshake()
info = {}
(info["peer_ip"], info["peer_port"]) = sock.getpeername()
info["server_name"] = sock.get_servername().decode("ascii")
info["cipher_name"] = sock.get_cipher_name()
info["cipher_bits"] = sock.get_cipher_bits()
info["cipher_version"] = sock.get_cipher_version()
info[
"application_layer_protocol_negotiated"
] = sock.get_alpn_proto_negotiated().decode("ascii")
info["state_string"] = sock.get_state_string().decode("ascii")
info["client_ca_list"] = [dumpname(ca) for ca in sock.get_client_ca_list()]
info["certificate"] = dumpcert(sock.get_peer_certificate())
info["chain"] = [dumpcert(cert) for cert in sock.get_peer_cert_chain()]
sock.shutdown()
sock.close()
return json.dumps(info, sort_keys=True, indent=4)
except:
abort(400)
def before_request():
request.start_time = time.time()
def after_request(response):
request_latency = max(
time.time() - request.start_time, 0
) # time can go backwards...
FLASK_REQUEST_LATENCY.labels(request.method, request.path).observe(request_latency)
FLASK_REQUEST_SIZE.labels(request.method, request.path, response.status_code).set(
len(response.data)
)
FLASK_REQUEST_COUNT.labels(request.method, request.path, response.status_code).inc()
return response
if __name__ == "__main__":
app.before_request(before_request)
app.after_request(after_request)
app.run(host="0.0.0.0", port=os.environ.get("listenport", 8080))