-
Notifications
You must be signed in to change notification settings - Fork 152
/
utils.py
434 lines (371 loc) · 14.4 KB
/
utils.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import re
import os, sys
import time
import threading
import ssl
import json
try:
import urllib.request as urllib_request # for Python 3
except ImportError:
import urllib2 as urllib_request # for Python 2 and Jython
try:
from urllib.parse import urlparse # for Python 3
except ImportError:
from urlparse import urlparse # for Python 2 and Jython
try:
from BaseHTTPServer import BaseHTTPRequestHandler
except ImportError:
from http.server import BaseHTTPRequestHandler
from io import BytesIO
def string_join(*ss):
"""
String joins with arbitrary lengthy parameters
:param ss: strings to be joined
:return: strings joined
"""
return "".join(ss)
def mkdir_p(path):
"""
Create Directory if it does not exist, exit otherwise
:param path:
:return:
"""
try:
os.makedirs(path)
except:
if os.path.isdir(path):
pass
else:
raise
def wrap_open(method, exceptions = (OSError, IOError)):
"""Wrap Open method in order to create containing directories if they does not exist"""
def fn(*args, **kwargs):
try:
mkdir_p(os.path.dirname(args[0]))
return method(*args, **kwargs)
except exceptions:
sys.exit('Can\'t open \'{0}\'. Error #{1[0]}: {1[1]}'.format(args[0], sys.exc_info()[1].args))
return fn
open = wrap_open(open)
def inherits_popup_menu(element):
"""
Inherits popup menu on each and every child widgets.
:param element: current widget.
:return: None
"""
element.setInheritsPopupMenu(True)
try:
for e in element.getComponents():
inherits_popup_menu(e)
except:
pass
class AttrDict(dict):
"""
HACK: this class will generate a class object with fields from a dict
"""
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
def override_headers(http_header, overrideheaders):
"""
Overrides headers with the defined overrides.
:param http_header: an HTTP header content
:param overrideheaders: an overrideheaders object.
:return: a new overridden headers string
"""
ree = [(
re.compile("^%s\s*:\s*[^\n]+$" % re.escape(header), re.MULTILINE),
"%s: %s" % (header, val))
for (header, val) in overrideheaders]
h = http_header
for find, replace in ree:
hn = re.sub(find, replace, h)
if hn == h:
h = "%s\n%s" % (hn, str(replace))
else:
h = hn
return h
def nop_evt(evt):
"""
Do nothing on events
:param evt: ignored
:return: None
"""
pass
def nop():
"""
Do nothing
:return: None
"""
pass
stop_watch = False
def stop():
global stop_watch
stop_watch = True
def watch(execute=nop, interval=60):
global stop_watch
def async_run():
try:
while not stop_watch:
execute()
time.sleep(interval)
sys.stdout.flush()
sys.stderr.flush()
finally:
sys.stdout.flush()
sys.stderr.flush()
t = threading.Thread(target=async_run)
t.start()
def run_async(execute=nop):
def async_run():
try:
execute()
finally:
sys.stdout.flush()
sys.stderr.flush()
threading.Thread(target=async_run).start()
def make_http_handler(http_mutator=None):
class GraphQLRequestHandler(BaseHTTPRequestHandler):
def graphiql_page(self, address, extrascript=""):
"""
Return a graphiql console page given a domain
:param listenin_on: address on which the graphiql server proxy is listening on
:param domain: input domain on which to perform queries
:return: a string representing the graphiql page
"""
return """<html>
<head>
<title>InQL GraphiQL Console</title>
<link href="https://unpkg.com/graphiql/graphiql.min.css" rel="stylesheet" />
</head>
<body style="margin: 0;">
<div id="graphiql" style="height: 100vh;"></div>
<script
crossorigin
src="https://unpkg.com/react/umd/react.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/react-dom/umd/react-dom.production.min.js"
></script>
<script
crossorigin
src="https://unpkg.com/graphiql/graphiql.min.js"
></script>
<script>
/**
* This GraphiQL example illustrates how to use some of GraphiQL's props
* in order to enable reading and updating the URL parameters, making
* link sharing of queries a little bit easier.
*
* This is only one example of this kind of feature, GraphiQL exposes
* various React params to enable interesting integrations.
*/
// Parse the search string to get url parameters.
var address = "%s";
var search = window.location.search;
var parameters = {};
search.substr(1).split('&').forEach(function (entry) {
var eq = entry.indexOf('=');
if (eq >= 0) {
parameters[decodeURIComponent(entry.slice(0, eq))] =
decodeURIComponent(entry.slice(eq + 1));
}
});
// if variables was provided, try to format it.
if (parameters.variables) {
try {
parameters.variables =
JSON.stringify(JSON.parse(parameters.variables), null, 2);
} catch (e) {
// Do nothing, we want to display the invalid JSON as a string, rather
// than present an error.
}
}
// When the query and variables string is edited, update the URL bar so
// that it can be easily shared
function onEditQuery(newQuery) {
parameters.query = newQuery;
updateURL();
}
function onEditVariables(newVariables) {
parameters.variables = newVariables;
updateURL();
}
function onEditOperationName(newOperationName) {
parameters.operationName = newOperationName;
updateURL();
}
function updateURL() {
var newSearch = '?' + Object.keys(parameters).filter(function (key) {
return Boolean(parameters[key]);
}).map(function (key) {
return encodeURIComponent(key) + '=' +
encodeURIComponent(parameters[key]);
}).join('&');
history.replaceState(null, null, newSearch);
}
const graphQLFetcher = graphQLParams =>
fetch(address, {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(graphQLParams),
})
.then(response => response.json())
.catch(() => response.text());
ReactDOM.render(
React.createElement(GraphiQL, {
fetcher: graphQLFetcher,
query: parameters.query,
variables: parameters.variables,
operationName: parameters.operationName,
onEditQuery: onEditQuery,
onEditVariables: onEditVariables,
onEditOperationName: onEditOperationName
}),
document.getElementById('graphiql'),
);
while (document.querySelector('.title') == null) {
// wait for the title to be something
}
document.querySelector('.title').innerHTML = '<a href="https://github.com/doyensec/inql"><img src="https://github.com/doyensec/inql/blob/master/docs/inql.png?raw=true" style="display: block; height:6em; z-index: 10; position: relative"></img></a>';
%s
</script>
</body>
</html>""" % (address, extrascript)
# Handler for the GET requests
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
# Send the html message
if http_mutator:
page = self.graphiql_page(self.path, extrascript="""(function() {
var toolbar = document.querySelector('.toolbar');
var sendToRepeater = document.createElement('button');
sendToRepeater.classList.add('toolbar-button');
sendToRepeater.innerHTML = 'Send To Repeater';
sendToRepeater.title = 'Send To Repeater';
sendToRepeater.setAttribute('aria-invalid', true);
sendToRepeater.onclick = function() {
var xhr = new XMLHttpRequest();
xhr.open("PUT", address, true);
xhr.setRequestHeader('Content-Type', 'application/json');
var params = JSON.parse(JSON.stringify(parameters));
params['variables'] = JSON.parse(params['variables']);
xhr.send(JSON.stringify(params));
}
toolbar.appendChild(sendToRepeater);
} ());""")
else:
page = self.graphiql_page(self.path)
self.wfile.write(page.encode())
return
def do_POST(self):
try:
content_len = int(self.headers.getheader('content-length', 0))
except AttributeError: # python3 has not the getheader type, use get instead
content_len = int(self.headers.get('Content-Length'))
host = None
body = None
try:
idx = self.path.find('?')
if idx != -1:
endpoint = self.path[1:idx]
else:
endpoint = self.path[1:]
url = urlparse(endpoint)
if url.scheme == "https" and url.port == 443 or url.scheme == "http" and url.port == 80:
host = url.hostname
else:
host = url.netloc
self.headers['Host'] = host
body = self.rfile.read(content_len)
if not http_mutator:
request = urllib_request.Request(endpoint, body, headers=self.headers)
else:
request = http_mutator.build_python_request(endpoint, host, body)
if 'http_proxy' in os.environ or 'https_proxy' in os.environ:
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
contents = urllib_request.urlopen(request, context=ctx).read()
else:
contents = urllib_request.urlopen(request).read()
jres = json.loads(contents)
if 'errors' in jres and len(jres['errors']) > 0 and "IntrospectionQuery" in body:
raise Exception("IntrospectionQuery request contains errors")
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(contents)
except Exception as ex:
if host and http_mutator and http_mutator.get_stub_response(host) and "IntrospectionQuery" in body:
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(http_mutator.get_stub_response(host))
return
print(ex)
self.send_response(400)
self.send_header('Content-type', 'application/json')
self.end_headers()
try:
# Try to get the 400 page error content since it is used by the GraphiQL Console
self.wfile.write(ex.read())
except:
pass
return
def do_PUT(self):
try:
content_len = int(self.headers.getheader('content-length', 0))
except AttributeError: # python3 has not the getheader type, use get instead
content_len = int(self.headers.get('Content-Length'))
if http_mutator:
body = self.rfile.read(content_len)
url = urlparse(self.path[1:])
if url.scheme == "https" and url.port == 443 or url.scheme == "http" and url.port == 80:
host = url.hostname
else:
host = url.netloc
http_mutator.send_to_repeater(host, body)
else:
print(self.path)
print(self.rfile.read(content_len))
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
return
return GraphQLRequestHandler
class HTTPRequest(BaseHTTPRequestHandler):
def __init__(self, request_text):
self.rfile = BytesIO(request_text)
self.raw_requestline = self.rfile.readline()
self.error_code = self.error_message = None
self.parse_request()
def send_error(self, code, message):
self.error_code = code
self.error_message = message
def raw_request(request):
"""
At this point it is completely built and ready
to be fired; it is "prepared".
However pay attention at the formatting used in
this function because it is programmed to be pretty
printed and may differ from the actual request.
"""
headers = request.headers.copy()
if 'Connection' not in headers:
headers['Connection'] = 'close'
if 'User-Agent' not in headers:
headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'
if 'Accept-Encoding' not in headers:
headers['Accept-Encoding'] = 'gzip, deflate'
url = urlparse(request.get_full_url())
headers['Host'] = url.netloc
path = url.path if len(url.path) else '/'
return '{}\r\n{}\r\n\r\n{}'.format(
request.get_method() + ' ' + path + ' HTTP/1.1',
'\r\n'.join('{}: {}'.format(k, v) for k, v in headers.items()),
request.data if request.data else '',
)